-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
324 lines (232 loc) · 11.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import os
import argparse
import torch
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
from torch.optim import lr_scheduler
from torch.utils.data import DataLoader
from src.dataset import CUB as Dataset
from src.sampler import Sampler
from src.train_sampler import Train_Sampler
from src.utils import count_acc, Averager, csv_write, square_euclidean_metric
from model import FewShotModel
from src.test_dataset import CUB as Test_Dataset
from src.test_sampler import Test_Sampler
" User input value "
TOTAL = 10000 # total step of training
PRINT_FREQ = 50 # frequency of print loss and accuracy at training step
VAL_FREQ = 100 # frequency of model eval on validation dataset
SAVE_FREQ = 100 # frequency of saving model
TEST_SIZE = 200 # fixed
" fixed value "
VAL_TOTAL = 100
def Test_phase(model, args, k):
model.eval()
csv = csv_write(args)
dataset = Test_Dataset(args.dpath)
test_sampler = Test_Sampler(dataset._labels, n_way=args.nway, k_shot=args.kshot, query=args.query)
test_loader = DataLoader(dataset=dataset, batch_sampler=test_sampler, num_workers=4, pin_memory=True)
print('Test start!')
for i in range(TEST_SIZE):
for episode in test_loader:
data = episode.cuda()
data_shot, data_query = data[:k], data[k:]
""" TEST Method """
""" Predict the query images belong to which classes
At the training phase, you measured logits.
The logits can be distance or similarity between query images and 5 images of each classes.
From logits, you can pick a one class that have most low distance or high similarity.
ex) # when logits is distance
pred = torch.argmin(logits, dim=1)
# when logits is prob
pred = torch.argmax(logits, dim=1)
pred is torch.tensor with size [20] and the each component value is zero to four
"""
embedding_vectors = model(data_shot).cuda()
size = embedding_vectors.size()
mean_vectors = torch.zeros(args.nway, size[1]).cuda()
for j in range(args.nway):
mean_vectors[j] = torch.mean(embedding_vectors[args.kshot*j:args.kshot*(j+1)], 0)
query_vectors = model(data_query).cuda()
logits = square_euclidean_metric(query_vectors,mean_vectors).cuda()
pred = torch.argmin(logits, dim=1)
# save your prediction as StudentID_Name.csv file
csv.add(pred)
csv.close()
print('Test finished, check the csv file!')
exit()
def train(args):
# the number of N way, K shot images
k = args.nway * args.kshot
# Train data loading
dataset = Dataset(args.dpath, state='train')
train_sampler = Train_Sampler(dataset._labels, n_way=args.nway, k_shot=args.kshot, query=args.query)
data_loader = DataLoader(dataset=dataset, batch_sampler=train_sampler, num_workers=4, pin_memory=True)
# Validation data loading
val_dataset = Dataset(args.dpath, state='val')
val_sampler = Sampler(val_dataset._labels, n_way=args.nway, k_shot=args.kshot, query=args.query)
val_data_loader = DataLoader(dataset=val_dataset, batch_sampler=val_sampler, num_workers=4, pin_memory=True)
""" TODO 1.a """
" Make your own model for Few-shot Classification in 'model.py' file."
# model setting
model = FewShotModel()
""" TODO 1.a END """
model.cuda()
"""
# pretrained model load
"""
if args.restore_ckpt is not None:
state_dict = torch.load(args.restore_ckpt)
model.load_state_dict(state_dict)
if args.test_mode == 1:
Test_phase(model, args, k)
model = torch.nn.DataParallel(model, device_ids=range(0,8))
cudnn.benchmark = True
model.train()
""" TODO 1.b (optional) """
" Set an optimizer or scheduler for Few-shot classification (optional) "
# Default optimizer setting
optimizer = torch.optim.Adam(model.parameters(), lr=0.0001)
#scheduler = lr_scheduler.MultiStepLR(optimizer, milestones=[1000,3000,5000], gamma= 0.1)
""" TODO 1.b (optional) END """
tl = Averager() # save average loss
ta = Averager() # save average accuracy
# training start
print('train start')
for i in range(TOTAL):
for episode in data_loader:
optimizer.zero_grad()
data, label = [_.cuda() for _ in episode] # load an episode
# split an episode images and labels into shots and query set
# note! data_shot shape is ( nway * kshot, 3, h, w ) not ( kshot * nway, 3, h, w )
# Take care when reshape the data shot
data_shot, data_query = data[:k], data[k:]
label_shot, label_query = label[:k], label[k:]
label_shot = sorted(list(set(label_shot.tolist())))
# convert labels into 0-4 values
label_query = label_query.tolist()
labels = []
for j in range(len(label_query)):
label = label_shot.index(label_query[j])
labels.append(label)
labels = torch.tensor(labels).cuda()
""" TODO 2 ( Same as above TODO 2 ) """
""" Train the model
Input:
data_shot : torch.tensor, shot images, [args.nway * args.kshot, 3, h, w]
be careful when using torch.reshape or .view functions
data_query : torch.tensor, query images, [args.query, 3, h, w]
labels : torch.tensor, labels of query images, [args.query]
output:
loss : torch scalar tensor which used for updating your model
logits : A value to measure accuracy and loss
"""
embedding_vectors = model(data_shot).cuda()
size = embedding_vectors.size()
mean_vectors = torch.zeros(args.nway, size[1]).cuda()
for j in range(args.nway):
mean_vectors[j] = torch.mean(embedding_vectors[args.kshot*j:args.kshot*(j+1)], 0)
query_vectors = model(data_query).cuda()
logits = square_euclidean_metric(query_vectors,mean_vectors).cuda()
loss_label = torch.zeros(args.query).cuda()
loss = F.log_softmax(-logits,dim=1).cuda()
loss_label = torch.zeros(args.query).cuda()
for j in range(args.query):
loss_label[j] = loss[j, labels[j]]
loss = loss_label * (-1)
loss = loss.mean()
#return
""" TODO 2 END """
acc = count_acc(logits, labels)
tl.add(loss.item())
ta.add(acc)
loss.backward()
optimizer.step()
proto = None; logits = None; loss = None
#scheduler.step()
if (i+1) % PRINT_FREQ == 0:
print('train {}, loss={:.4f} acc={:.4f}'.format(i+1, tl.item(), ta.item()))
# initialize loss and accuracy mean
tl = None
ta = None
tl = Averager()
ta = Averager()
# validation start
if (i+1) % VAL_FREQ == 0:
print('validation start')
model.eval()
with torch.no_grad():
vl = Averager() # save average loss
va = Averager() # save average accuracy
for j in range(VAL_TOTAL):
for episode in val_data_loader:
data, label = [_.cuda() for _ in episode]
data_shot, data_query = data[:k], data[k:] # load an episode
label_shot, label_query = label[:k], label[k:]
label_shot = sorted(list(set(label_shot.tolist())))
label_query = label_query.tolist()
labels = []
for j in range(len(label_query)):
label = label_shot.index(label_query[j])
labels.append(label)
labels = torch.tensor(labels).cuda()
""" TODO 2 ( Same as above TODO 2 ) """
""" Train the model
Input:
data_shot : torch.tensor, shot images, [args.nway * args.kshot, 3, h, w]
be careful when using torch.reshape or .view functions
data_query : torch.tensor, query images, [args.query, 3, h, w]
labels : torch.tensor, labels of query images, [args.query]
output:
loss : torch scalar tensor which used for updating your model
logits : A value to measure accuracy and loss
"""
embedding_vectors = model(data_shot).cuda()
size = embedding_vectors.size()
mean_vectors = torch.zeros(args.nway, size[1]).cuda()
for j in range(args.nway):
mean_vectors[j] = torch.mean(embedding_vectors[args.kshot*j:args.kshot*(j+1)], 0)
query_vectors = model(data_query).cuda()
logits = square_euclidean_metric(query_vectors,mean_vectors).cuda()
loss_label = torch.zeros(args.query).cuda()
loss = F.log_softmax(-logits,dim=1).cuda()
loss_label = torch.zeros(args.query).cuda()
for j in range(args.query):
loss_label[j] = loss[j, labels[j]]
loss = loss_label * (-1)
loss = loss.mean()
""" TODO 2 END """
acc = count_acc(logits, labels)
vl.add(loss.item())
va.add(acc)
proto = None; logits = None; loss = None
print('val accuracy mean : %.4f' % va.item())
print('val loss mean : %.4f' % vl.item())
# initialize loss and accuracy mean
vl = None
va = None
vl = Averager()
va = Averager()
model.train()
if (i+1) % SAVE_FREQ == 0:
PATH = 'checkpoints/%d_%s.pth' % (i + 1, args.name)
torch.save(model.module.state_dict(), PATH)
print('model saved, iteration : %d' % i)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--name', default='model', help="name your experiment")
parser.add_argument('--dpath', '--d', default='./dataset/CUB_200_2011', type=str,
help='the path where dataset is located')
parser.add_argument('--restore_ckpt', default='./checkpoints/best.pth', type=str, help="restore checkpoint")
parser.add_argument('--nway', '--n', default=5, type=int, help='number of class in the support set (5 or 20)')
parser.add_argument('--kshot', '--k', default=5, type=int,
help='number of data in each class in the support set (1 or 5)')
parser.add_argument('--query', '--q', default=20, type=int, help='number of query data')
parser.add_argument('--ntest', default=100, type=int, help='number of tests')
parser.add_argument('--gpus', type=int, nargs='+', default=1)
parser.add_argument('--test_mode', type=int, default=1, help="if you want to test the model, change the value to 1")
args = parser.parse_args()
if not os.path.isdir('checkpoints'):
os.mkdir('checkpoints')
#torch.cuda.set_device(args.gpus)
train(args)