-
Notifications
You must be signed in to change notification settings - Fork 4
/
cnn.py
executable file
·294 lines (247 loc) · 11.3 KB
/
cnn.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
import os
import sys
import numpy as np
import random
import logging
import datetime
import shutil
import os.path as osp
import configargparse
import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
from tqdm import tqdm
import omniglot
import memory
parser = configargparse.ArgParser()
parser.add('-c', '--config', required=False,
is_config_file=True, help='config file')
parser.add_argument('--seed', default=43, type=int, help='Random Seed')
parser.add_argument('--memory-size', default=2048, type=int, help='Memory size')
parser.add_argument('--key-dim', default=128, type=int, help='Key dimension')
parser.add_argument('--batch-size', default=16, type=int, help='Training episode batch size')
parser.add_argument('--episode-length', default=30, type=int, help='Episode length')
parser.add_argument('--episode-width', default=5, type=int, help='Number of distinct class in one episode')
parser.add_argument('--val-shot', default=5, type=int, help='Validation shot')
parser.add_argument('--val-way', default=5, type=int, help='Validation way')
parser.add_argument('--validation-frequency', default=50, help='Every so often validate the model')
parser.add_argument('--lr', default=1e-4, type=float, help='Learning rate for Adam')
parser.add_argument('--eps', default=1e-4, type=float, help='Eps for Adam')
parser.add_argument('--margin', default=0.1, type=float, help='Triplet loss margin')
parser.add_argument('--train-model', action='store_true', help='Train the model')
parser.add_argument('--load-model', action='store_true', help='Load the previous model')
parser.add_argument('--save-model', action='store_true', help='Save the model')
parser.add_argument('--do-eval', action='store_true', help='Evaluate the model by N-way K-shot')
parser.add_argument('--eval-way', default=5, type=int, help='Evaluation way')
parser.add_argument('--eval-shot', default=1, type=int, help='Evaluation shot')
parser.add_argument('--eval-episode', default=1000, type=int, help='Evaluation episode')
parser.add_argument('--savedir', default=None, type=str, help='Model saving directory')
parser.add_argument('--ch-last', default=128, type=int,
help='Channel number of the last convolution layers in CNN, to match the parameter count')
args = parser.parse_args()
class Net(nn.Module):
def __init__(self, input_shape, keydim=128, ch_last=args.ch_last):
super(Net, self).__init__()
# Constants
kernel = 3
pad = int((kernel - 1) / 2.0)
p = 0.3
ch, row, col = input_shape
self.conv1 = nn.Conv2d(ch, 64, kernel, padding=(0, 0))
self.conv2 = nn.Conv2d(64, 64, kernel, padding=(0, 0))
self.conv3 = nn.Conv2d(64, 128, kernel, padding=(pad, pad))
self.conv4 = nn.Conv2d(128, 128, kernel, padding=(pad, pad))
self.conv5 = nn.Conv2d(128, ch_last, kernel, padding=(pad, pad))
self.conv6 = nn.Conv2d(ch_last, ch_last, kernel, padding=(pad, pad))
self.pool = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(9 * ch_last, keydim)
self.dropout = nn.Dropout(p)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = self.pool(x)
x = F.relu(self.conv3(x))
x = F.relu(self.conv4(x))
x = self.pool(x)
x = F.relu(self.conv5(x))
x = F.relu(self.conv6(x))
x = self.pool(x)
x = x.view(x.size(0), -1)
x = self.dropout(x)
x = self.fc1(x)
x = self.dropout(x)
return x
def weight_init(m):
if isinstance(m, nn.Conv2d):
nn.init.xavier_uniform_(m.weight)
m.bias.data.fill_(0.01)
if isinstance(m, nn.Linear):
nn.init.xavier_uniform_(m.weight)
m.bias.data.fill_(0.01)
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.enabled = True
def save_checkpoint(state, is_best, folder, filename='model_best.pth.tar'):
if not osp.exists(folder):
os.umask(0)
os.makedirs(folder, mode=0o777, exist_ok=False)
torch.save(state, folder + '/' + filename)
if is_best:
shutil.copyfile(folder + '/' + filename, folder + '/' + 'model_best.pth.tar')
def load_checkpoint(folder, is_best=True):
filename = 'model_best.pth.tar' if is_best else 'model_best.pth.tar'
path = osp.join(folder, filename)
loaded_checkpoint = torch.load(path, map_location='cuda')
return loaded_checkpoint
def eval_fewshot(model, mem, support_x, support_y, query_x, query_y):
"""
Perform one N-way K-shot evaluation
Return:
"""
model.eval()
mem.build() # clear the memory
# Update the memory for N-way K-shot images
for xx, yy in zip(support_x, support_y):
xx_cuda, yy_cuda = xx.cuda(), yy.cuda()
query = model(xx_cuda)
mem.query(query, yy_cuda, True)
# Use remaining images to do evaluation on the updated memory
query_x_cuda = query_x.cuda()
query = model(query_x_cuda)
yy_hat, _ = mem.predict(query)
evaluation = torch.eq(yy_hat.detach().cpu(), query_y.unsqueeze(dim=1)).squeeze().numpy().astype('float')
return evaluation
# Set up logging
datestr = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)
fh = logging.FileHandler('log/' + datestr + '.log')
fh.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
console = logging.StreamHandler()
console.setFormatter(formatter)
logger.addHandler(console)
# Training parameters
setup_seed(args.seed)
logger.info(f'Memory size: {args.memory_size}')
logger.info(f'Batch size: {args.batch_size}')
logger.info(f'Key dimension: {args.key_dim}')
logger.info(f'Training episode length: {args.episode_length}')
logger.info(f'Training episode width: {args.episode_width}')
logger.info(f'Validation frequency: {args.validation_frequency}')
logger.info(f'Test way: {args.test_way}')
logger.info(f'Test shot: {args.test_shot}')
logger.info(f'Learning rate: {args.lr}')
logger.info(f'Eps for Adam: {args.eps}')
logger.info(f'Seed: {args.seed}')
logger.info(f'Triplet loss margin: {args.margin}')
# Dataset loading
DATA_FILE_FORMAT = os.path.join(os.getcwd(), '%s_omni.pkl')
train_filepath = DATA_FILE_FORMAT % 'train'
trainset = omniglot.OmniglotDataset(train_filepath)
trainloader = trainset.sample_episode_batch(args.episode_length, args.episode_width, args.batch_size, N=10000)
test_filepath = DATA_FILE_FORMAT % 'test'
testset = omniglot.OmniglotDataset(test_filepath)
logger.info('Dataset loaded')
# Network initializing
net = Net(input_shape=(1, 28, 28), keydim=args.key_dim)
mem = memory.Memory(args.memory_size, args.key_dim, margin=args.margin)
net.add_module("memory", mem)
net.cuda()
net.apply(weight_init)
optimizer = optim.Adam(net.parameters(), lr=args.lr, eps=args.eps)
lrscheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', patience=20, verbose=True)
cummulative_loss = 0
counter = 0
best_val_acc = 0
episode_start = 0
if args.load_model:
checkpoint_pre = load_checkpoint(args.savedir, True)
net.load_state_dict(checkpoint_pre['model_state_dict'])
optimizer.load_state_dict(checkpoint_pre['optimizer_state_dict'])
lrscheduler.load_state_dict(checkpoint_pre['scheduler_state_dict'])
episode_start = checkpoint_pre['episode']
best_val_acc = checkpoint_pre['best_val_acc']
logger.info('Load previous model')
if args.train_model:
logger.info('Start Training')
for i, data in tqdm(enumerate(trainloader, episode_start)):
# erase memory before training episode
net.train()
mem.build()
x, y = data
is_best = False
for xx, yy in zip(x, y):
optimizer.zero_grad()
xx_cuda, yy_cuda = xx.cuda(), yy.cuda()
embed = net(xx_cuda)
yy_hat, softmax_embed, loss = mem.query(embed, yy_cuda, False)
loss.backward()
nn.utils.clip_grad_norm_(net.parameters(), max_norm=5.0)
optimizer.step()
cummulative_loss += loss.detach() # loss across the whole (episode * val_frequency)
counter += 1
with torch.no_grad():
if i % args.validation_frequency == 0:
# validation
correct = []
correct_by_k_shot = dict((k, list()) for k in range(args.val_shot + 1))
testloader = testset.sample_episode_batch((args.val_shot + 1) * args.val_way, args.val_way, batch_size=1, N=100)
net.eval()
for data in testloader:
# erase memory before validation episode
mem.build()
x, y = data
y_hat = []
for xx, yy in zip(x, y):
xx_cuda, yy_cuda = xx.cuda(), yy.cuda()
query = net(xx_cuda)
yy_hat, embed, loss = mem.query(query, yy_cuda, True)
y_hat.append(yy_hat)
correct.append(float(torch.equal(yy_hat.cpu(), torch.unsqueeze(yy, dim=1))))
# compute per_shot accuracies
seen_count = [0 for idx in range(args.val_way)]
# loop over episode steps
for yy, yy_hat in zip(y, y_hat):
count = seen_count[yy[0] % args.val_way]
if count < (args.val_shot + 1):
correct_by_k_shot[count].append(float(torch.equal(yy_hat.cpu(), torch.unsqueeze(yy, dim=1))))
seen_count[yy[0] % args.val_way] += 1
temp_acc = np.mean(correct)
if temp_acc > best_val_acc:
is_best = True
best_val_acc = temp_acc
logger.info("episode batch: {0:d} average loss: {1:.6f}".format(i, (cummulative_loss / counter)))
logger.info("validation overall accuracy {0:f}".format(temp_acc))
for idx in range(args.val_shot + 1):
logger.info("{0:d}-shot: {1:.3f}".format(idx, np.mean(correct_by_k_shot[idx])))
cummulative_loss = 0
counter = 0
lrscheduler.step(temp_acc) # ReduceOnPlateu scheduler
if args.save_model:
checkpoint = {
'episode': i,
'best_val_acc': best_val_acc,
'model_state_dict': net.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'scheduler_state_dict': lrscheduler.state_dict()
}
save_checkpoint(checkpoint, is_best, args.savedir)
if args.do_eval:
logger.info(f"Evaluate specific {args.eval_way}-way {args.eval_shot}-shot")
evalloader = testset.test_sampler(args.eval_way, args.eval_shot, args.eval_episode)
evaluation_all = []
for data in tqdm(evalloader):
support_x, support_y, query_x, query_y = data
evaluation = eval_fewshot(net, mem, support_x, support_y, query_x, query_y)
evaluation_all.extend(evaluation)
logger.info(f"{args.eval_way}-way {args.eval_shot}-shot: {np.mean(evaluation_all)}")