forked from andreasveit/conditional-similarity-networks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
347 lines (311 loc) · 13.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
from __future__ import print_function
import argparse
import os
import sys
import shutil
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import transforms
from torch.autograd import Variable
import torch.backends.cudnn as cudnn
from triplet_image_loader import TripletImageLoader
from tripletnet import CS_Tripletnet
from visdom import Visdom
import numpy as np
import Resnet_18
from csn import ConditionalSimNet
# Training settings
parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
parser.add_argument('--batch-size', type=int, default=256, metavar='N',
help='input batch size for training (default: 64)')
parser.add_argument('--epochs', type=int, default=200, metavar='N',
help='number of epochs to train (default: 200)')
parser.add_argument('--start_epoch', type=int, default=1, metavar='N',
help='number of start epoch (default: 1)')
parser.add_argument('--lr', type=float, default=5e-5, metavar='LR',
help='learning rate (default: 5e-5)')
parser.add_argument('--seed', type=int, default=1, metavar='S',
help='random seed (default: 1)')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='enables CUDA training')
parser.add_argument('--log-interval', type=int, default=20, metavar='N',
help='how many batches to wait before logging training status')
parser.add_argument('--margin', type=float, default=0.2, metavar='M',
help='margin for triplet loss (default: 0.2)')
parser.add_argument('--resume', default='', type=str,
help='path to latest checkpoint (default: none)')
parser.add_argument('--name', default='Conditional_Similarity_Network', type=str,
help='name of experiment')
parser.add_argument('--embed_loss', type=float, default=5e-3, metavar='M',
help='parameter for loss for embedding norm')
parser.add_argument('--mask_loss', type=float, default=5e-4, metavar='M',
help='parameter for loss for mask norm')
parser.add_argument('--num_traintriplets', type=int, default=100000, metavar='N',
help='how many unique training triplets (default: 100000)')
parser.add_argument('--dim_embed', type=int, default=64, metavar='N',
help='how many dimensions in embedding (default: 64)')
parser.add_argument('--test', dest='test', action='store_true',
help='To only run inference on test set')
parser.add_argument('--learned', dest='learned', action='store_true',
help='To learn masks from random initialization')
parser.add_argument('--prein', dest='prein', action='store_true',
help='To initialize masks to be disjoint')
parser.add_argument('--visdom', dest='visdom', action='store_true',
help='Use visdom to track and plot')
parser.add_argument('--conditions', nargs='*', type=int,
help='Set of similarity notions')
parser.set_defaults(test=False)
parser.set_defaults(learned=False)
parser.set_defaults(prein=False)
parser.set_defaults(visdom=False)
best_acc = 0
def main():
global args, best_acc
args = parser.parse_args()
args.cuda = not args.no_cuda and torch.cuda.is_available()
torch.manual_seed(args.seed)
if args.cuda:
torch.cuda.manual_seed(args.seed)
if args.visdom:
global plotter
plotter = VisdomLinePlotter(env_name=args.name)
"""
*할일* mean, std 수치에 대해서 알아보자
"""
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
global conditions
if args.conditions is not None:
conditions = args.conditions
else:
conditions = [0,1,2,3,4] #texture, fabric, shape, part, style
kwargs = {'num_workers': 4, 'pin_memory': True} if args.cuda else {}
train_loader = torch.utils.data.DataLoader(
TripletImageLoader('./data',
conditions, 'train', n_triplets=args.num_traintriplets,
transform=transforms.Compose([
transforms.Resize(112),
transforms.CenterCrop(112),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])),
batch_size=args.batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(
TripletImageLoader('./data',
conditions, 'test', n_triplets=160000,
transform=transforms.Compose([
transforms.Resize(112),
transforms.CenterCrop(112),
transforms.ToTensor(),
normalize,
])),
batch_size=args.batch_size, shuffle=True, **kwargs)
val_loader = torch.utils.data.DataLoader(
TripletImageLoader('./data',
conditions, 'val', n_triplets=80000,
transform=transforms.Compose([
transforms.Resize(112),
transforms.CenterCrop(112),
transforms.ToTensor(),
normalize,
])),
batch_size=args.batch_size, shuffle=True, **kwargs)
model = Resnet_18.resnet18(pretrained=True, embedding_size=args.dim_embed)
csn_model = ConditionalSimNet(model, n_conditions=len(conditions),
embedding_size=args.dim_embed, learnedmask=args.learned, prein=args.prein)
global mask_var
mask_var = csn_model.masks.weight
tnet = CS_Tripletnet(csn_model)
if args.cuda:
tnet.cuda()
# optionally resume from a checkpoint
if args.resume:
if os.path.isfile(args.resume):
print("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint['epoch']
best_prec1 = checkpoint['best_prec1']
tnet.load_state_dict(checkpoint['state_dict'])
print("=> loaded checkpoint '{}' (epoch {})"
.format(args.resume, checkpoint['epoch']))
else:
print("=> no checkpoint found at '{}'".format(args.resume))
cudnn.benchmark = True
criterion = torch.nn.MarginRankingLoss(margin = args.margin)
parameters = filter(lambda p: p.requires_grad, tnet.parameters())
optimizer = optim.Adam(parameters, lr=args.lr)
n_parameters = sum([p.data.nelement() for p in tnet.parameters()])
print(' + Number of params: {}'.format(n_parameters))
if args.test:
test_acc = test(test_loader, tnet, criterion, 1)
sys.exit()
for epoch in range(args.start_epoch, args.epochs + 1):
# update learning rate
adjust_learning_rate(optimizer, epoch)
# train for one epoch
train(train_loader, tnet, criterion, optimizer, epoch)
# evaluate on validation set
acc = test(val_loader, tnet, criterion, epoch)
# remember best acc and save checkpoint
is_best = acc > best_acc
best_acc = max(acc, best_acc)
save_checkpoint({
'epoch': epoch + 1,
'state_dict': tnet.state_dict(),
'best_prec1': best_acc,
}, is_best)
def train(train_loader, tnet, criterion, optimizer, epoch):
losses = AverageMeter()
accs = AverageMeter()
emb_norms = AverageMeter()
mask_norms = AverageMeter()
# switch to train mode
tnet.train()
for batch_idx, (data1, data2, data3, c) in enumerate(train_loader):
if args.cuda:
data1, data2, data3, c = data1.cuda(), data2.cuda(), data3.cuda(), c.cuda()
data1, data2, data3, c = Variable(data1), Variable(data2), Variable(data3), Variable(c)
# compute output
dista, distb, mask_norm, embed_norm, mask_embed_norm = tnet(data1, data2, data3, c)
# 1 means, dista should be larger than distb
target = torch.FloatTensor(dista.size()).fill_(1)
if args.cuda:
target = target.cuda()
target = Variable(target)
loss_triplet = criterion(dista, distb, target)
loss_embedd = embed_norm / np.sqrt(data1.size(0))
loss_mask = mask_norm / data1.size(0)
loss = loss_triplet + args.embed_loss * loss_embedd + args.mask_loss * loss_mask
# measure accuracy and record loss
acc = accuracy(dista, distb)
losses.update(loss_triplet.data.item(), data1.size(0))
accs.update(acc, data1.size(0))
emb_norms.update(loss_embedd.data.item())
mask_norms.update(loss_mask.data.item())
# compute gradient and do optimizer step
optimizer.zero_grad()
loss.backward()
optimizer.step()
if batch_idx % args.log_interval == 0:
print('Train Epoch: {} [{}/{}]\t'
'Loss: {:.4f} ({:.4f}) \t'
'Acc: {:.2f}% ({:.2f}%) \t'
'Emb_Norm: {:.2f} ({:.2f})'.format(
epoch, batch_idx * len(data1), len(train_loader.dataset),
losses.val, losses.avg,
100. * accs.val, 100. * accs.avg, emb_norms.val, emb_norms.avg))
# log avg values to visdom
if args.visdom:
plotter.plot('acc', 'train', epoch, accs.avg)
plotter.plot('loss', 'train', epoch, losses.avg)
plotter.plot('emb_norms', 'train', epoch, emb_norms.avg)
plotter.plot('mask_norms', 'train', epoch, mask_norms.avg)
if epoch % 10 == 0:
plotter.plot_mask(torch.nn.functional.relu(mask_var).data.cpu().numpy().T, epoch)
def test(test_loader, tnet, criterion, epoch):
losses = AverageMeter()
accs = AverageMeter()
accs_cs = {}
for condition in conditions:
accs_cs[condition] = AverageMeter()
# switch to evaluation mode
tnet.eval()
for batch_idx, (data1, data2, data3, c) in enumerate(test_loader):
if args.cuda:
data1, data2, data3, c = data1.cuda(), data2.cuda(), data3.cuda(), c.cuda()
data1, data2, data3, c = Variable(data1), Variable(data2), Variable(data3), Variable(c)
c_test = c
# compute output
dista, distb, _, _, _ = tnet(data1, data2, data3, c)
target = torch.FloatTensor(dista.size()).fill_(1)
if args.cuda:
target = target.cuda()
target = Variable(target)
test_loss = criterion(dista, distb, target).data.item()
# measure accuracy and record loss
acc = accuracy(dista, distb)
accs.update(acc, data1.size(0))
for condition in conditions:
accs_cs[condition].update(accuracy_id(dista, distb, c_test, condition), data1.size(0))
losses.update(test_loss, data1.size(0))
print('\nTest set: Average loss: {:.4f}, Accuracy: {:.2f}%\n'.format(
losses.avg, 100. * accs.avg))
if args.visdom:
for condition in conditions:
plotter.plot('accs', 'acc_{}'.format(condition), epoch, accs_cs[condition].avg)
plotter.plot(args.name, args.name, epoch, accs.avg, env='overview')
plotter.plot('acc', 'test', epoch, accs.avg)
plotter.plot('loss', 'test', epoch, losses.avg)
return accs.avg
def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
"""Saves checkpoint to disk"""
directory = "runs/%s/"%(args.name)
if not os.path.exists(directory):
os.makedirs(directory)
filename = directory + filename
torch.save(state, filename)
if is_best:
shutil.copyfile(filename, 'runs/%s/'%(args.name) + 'model_best.pth.tar')
class VisdomLinePlotter(object):
"""Plots to Visdom"""
def __init__(self, env_name='main'):
self.viz = Visdom()
self.env = env_name
self.plots = {}
def plot(self, var_name, split_name, x, y, env=None):
if env is not None:
print_env = env
else:
print_env = self.env
if var_name not in self.plots:
self.plots[var_name] = self.viz.line(X=np.array([x,x]), Y=np.array([y,y]), env=print_env, opts=dict(
legend=[split_name],
title=var_name,
xlabel='Epochs',
ylabel=var_name
))
else:
self.viz.updateTrace(X=np.array([x]), Y=np.array([y]), env=print_env, win=self.plots[var_name], name=split_name)
def plot_mask(self, masks, epoch):
self.viz.bar(
X=masks,
env=self.env,
opts=dict(
stacked=True,
title=epoch,
)
)
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def adjust_learning_rate(optimizer, epoch):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
lr = args.lr * ((1 - 0.015) ** epoch)
if args.visdom:
plotter.plot('lr', 'learning rate', epoch, lr)
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def accuracy(dista, distb):
margin = 0
pred = (dista - distb - margin).cpu().data
return (pred > 0).sum()*1.0/dista.size()[0]
def accuracy_id(dista, distb, c, c_id):
margin = 0
pred = (dista - distb - margin).cpu().data
return ((pred > 0)*(c.cpu().data == c_id)).sum()*1.0/(c.cpu().data == c_id).sum()
if __name__ == '__main__':
main()