-
Notifications
You must be signed in to change notification settings - Fork 4
/
transferlearning.py
337 lines (273 loc) · 12.9 KB
/
transferlearning.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
# this is for transfer learning experiment on DDSM
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
import torch.nn.init as init
import torchvision
from torchvision import transforms
from torch.autograd import Variable
import torch.backends.cudnn as cudnn
from triplet_image_loader import TripletImageLoader
import scipy.io as sio
import resnet_new
import numpy as np
# insert this to the top of your scripts (usually main.py)
import sys, warnings, traceback, torch
def warn_with_traceback(message, category, filename, lineno, file=None, line=None):
sys.stderr.write(warnings.formatwarning(message, category, filename, lineno, line))
traceback.print_stack(sys._getframe(2))
warnings.showwarning = warn_with_traceback; warnings.simplefilter('always', UserWarning);
torch.utils.backcompat.broadcast_warning.enabled = True
torch.utils.backcompat.keepdim_warning.enabled = True
# Training settings
parser = argparse.ArgumentParser(description='Transfer Learning DDSM')
parser.add_argument('--batch_size', type=int, default=16, metavar='N',
help='input batch size for training (default: 256)')
parser.add_argument('--val_batch_size', type=int, default=4, metavar='N',
help='input batch size for training (default: 256)')
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=1e-4, 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=3, 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='transferleanring', type=str,
help='name of experiment')
parser.add_argument('--embed_loss', type=float, default= 0, metavar='M',
help='parameter for loss for embedding norm default 5e-3')
parser.add_argument('--triplet_loss', type=float, default=1, metavar='M',
help='parameter for loss for embedding norm')
parser.add_argument('--num_traintriplets', type=int, default=50000, metavar='N',
help='how many unique training triplets (default: 50000)')
parser.add_argument('--num_valtriplets', type=int, default=20000, metavar='N',
help='how many unique validation triplets (default: 10000)')
parser.add_argument('--num_testtriplets', type=int, default=40000, metavar='N',
help='how many unique test triplets (default: 20000)')
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('--image_size', type=int, default=224, #112, #changed from 128
help='height/width length of the input images, default=64')
parser.add_argument('--beta1', type=float, default=0.9,
help='beta1 for adam, default=0.1')
parser.add_argument('--beta2', type=float, default=0.999,
help='beta2 for adam, default=0.001')
parser.add_argument('--nc', type=int, default=3,
help='number of input channel in data. 3 for rgb, 1 for grayscale')
parser.set_defaults(test=False)
parser.set_defaults(learned=False)
parser.set_defaults(prein=False)
parser.set_defaults(visdom=False)
best_acc = 0
#class for returning triplet distances and embeddings
class Tripletnet(nn.Module):
def __init__(self, embeddingnet):
super(Tripletnet, self).__init__()
self.embeddingnet = embeddingnet
def forward(self, x, y, z):
embedded_x = self.embeddingnet(x)
embedded_y = self.embeddingnet(y)
embedded_z = self.embeddingnet(z)
dist_a = F.pairwise_distance(embedded_x, embedded_y, 2)
dist_b = F.pairwise_distance(embedded_x, embedded_z, 2)
return dist_a, dist_b, embedded_x, embedded_y, embedded_z
def main():
global args, best_acc
args = parser.parse_args()
print(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)
kwargs = {'num_workers': 4, 'pin_memory': True} if args.cuda else {}
train_loader = torch.utils.data.DataLoader(
TripletImageLoader('../data', '', 'train/train_data.json',
'train', n_triplets=args.num_traintriplets,
transform=transforms.Compose([
transforms.Scale(args.image_size),
transforms.CenterCrop(args.image_size),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
])),
batch_size=args.batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(
TripletImageLoader('../data', '', 'test/test_data.json',
'test', n_triplets=args.num_testtriplets,
transform=transforms.Compose([
transforms.Scale(args.image_size),
transforms.CenterCrop(args.image_size),
transforms.ToTensor(),
])),
batch_size=args.val_batch_size, shuffle=True, **kwargs)
val_loader = torch.utils.data.DataLoader(
TripletImageLoader('../data', '', 'val/val_data.json',
'val', n_triplets=args.num_valtriplets,
transform=transforms.Compose([
transforms.Scale(args.image_size),
transforms.CenterCrop(args.image_size),
transforms.ToTensor(),
])),
batch_size=args.val_batch_size, shuffle=True, **kwargs)
model = torchvision.models.resnet101(pretrained=True)
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, args.dim_embed)
tnet = Tripletnet(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()
# switch to train mode
tnet.train()
for batch_idx, (data1, data2, data3) in enumerate(train_loader):
if args.cuda:
data1, data2, data3 = data1.cuda(), data2.cuda(), data3.cuda()
data1, data2, data3 = Variable(data1), Variable(data2), Variable(data3)
# compute output
dista, distb, embedded_x, embedded_y, embedded_z = tnet(data1, data2, data3)
# 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 = embedded_x.norm(2) + embedded_y.norm(2) + embedded_z.norm(2)
loss = loss_triplet + args.embed_loss * loss_embedd
# measure accuracy and record loss
acc = accuracy(dista, distb)
losses.update(loss_triplet.data[0], data1.size(0))
accs.update(acc, data1.size(0))
emb_norms.update(loss_embedd.data[0]/3, data1.size(0))
# compute gradient and do optimizer step
optimizer.zero_grad()
loss.backward()
optimizer.step()
if batch_idx % args.log_interval == 0:
if epoch %5 == 0: print(dista-distb)
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))
def test(test_loader, tnet, criterion, epoch):
losses = AverageMeter()
accs = AverageMeter()
# switch to evaluation mode
tnet.eval()
for batch_idx, (data1, data2, data3) in enumerate(test_loader):
if args.cuda:
data1, data2, data3 = data1.cuda(), data2.cuda(), data3.cuda()
data1, data2, data3 = Variable(data1), Variable(data2), Variable(data3)
# compute output
dista, distb, _, _, _ = tnet(data1, data2, data3)
target = torch.FloatTensor(dista.size()).fill_(1)
if args.cuda:
target = target.cuda()
target = Variable(target)
test_loss = criterion(dista, distb, target).data[0]
# measure accuracy and record loss
acc = accuracy(dista, distb)
accs.update(acc, 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))
return accs.avg
#function for saving checkpoing while training
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 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)
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()