-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_couple_Net.py
341 lines (280 loc) · 11.4 KB
/
train_couple_Net.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
import argparse
import time
from dataload import load_file_list,load_test_list,get_batch,get_test
import torch
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim
import torch.utils.data
import os
import numpy as np
from models import *
model_names = sorted(name for name in couple_Net.__dict__
if name.islower() and not name.startswith("__")
and name.startswith("couple_Net")
and callable(couple_Net.__dict__[name]))
print(model_names)
parser = argparse.ArgumentParser(description='PyTorch ImageNet Training')
parser.add_argument('--arch', '-a', metavar='ARCH', default='couple_Net', # 'vgg19',
choices=model_names,
help='model architecture: ' + ' | '.join(model_names) +
' (default: couple_Net)')
parser.add_argument('-j', '--workers', default=4, type=int, metavar='N',
help='number of data loading workers (default: 4)')
parser.add_argument('--epochs', default=20, type=int, metavar='N',
help='number of total epochs to run')
parser.add_argument('--start-epoch', default=0, type=int, metavar='N',
help='manual epoch number (useful on restarts)')
parser.add_argument('-b', '--batch-size', default=128, type=int,
metavar='N', help='mini-batch size (default: 128)')
parser.add_argument('--lr', '--learning-rate', default=0.0001, type=float,
metavar='LR', help='initial learning rate')
parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
help='momentum')
parser.add_argument('--weight-decay', '--wd', default=5e-4, type=float,
metavar='W', help='weight decay (default: 5e-4)')
parser.add_argument('--print-freq', '-p', default=40, type=int,
metavar='N', help='print frequency (default: 20)')
parser.add_argument('--resume', default='', type=str, metavar='PATH',
help='path to latest checkpoint (default: none)')
parser.add_argument('-e', '--evaluate', dest='evaluate', action='store_true',
help='evaluate model on validation set')
parser.add_argument('--pretrained', dest='pretrained', action='store_true',
help='use pre-trained model')
parser.add_argument('--half', dest='half', action='store_true',
help='use half-precision(16-bit) ')
parser.add_argument('--cpu', dest='cpu', action='store_true',
help='use cpu')
parser.add_argument('--save-dir', dest='save_dir',
help='The directory used to save the trained models',
default='', type=str)
best_prec1 = 0
def one_hot(a, n):
a = a.cpu()
b = a.shape[0]
c = np.zeros([b, n])
for i in range(b):
c[i][int(a[i])] = 1
return c
def cross_entropy_loss(out1,out2,out3,label,mlabel,clabel):
# convert out to softmax probability
prob = torch.clamp(torch.softmax(out1, 1), 1e-10, 1.0)
prob2 = torch.clamp(2*torch.softmax(out2, 1), 1e-10, 2.0)
prob3 = torch.clamp(3*torch.softmax(out3, 1), 1e-10, 3.0)
loss1 = torch.sum(-clabel * torch.log(prob + 1e-8))
loss2 = torch.sum(-mlabel * torch.log(prob2 + 1e-8))
loss3 = torch.sum(-label * torch.log(prob3 + 1e-8))
loss = 0.2 * loss1 + 0.5 * loss2 + 0.3* loss3
# cost4 = tf.reduce_sum(tf.abs(self.logits_scaled2[:, :14] - self.logits_scaled1[:, :14]))
# cost5 = tf.reduce_sum(tf.abs(self.logits_scaled3[:, :19] - self.logits_scaled2[:, :19]))
return loss
"""
--------------------------------------------------------------------------------
"""
def main():
global args, best_prec1
args = parser.parse_args()
# Check the save_dir exists or not
if not os.path.exists(args.save_dir):
os.makedirs(args.save_dir)
model = couple_Net.__dict__[args.arch]()
#model.features = torch.nn.DataParallel(model.features)
if args.cpu:
model.cpu()
else:
model.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']
model.load_state_dict(checkpoint['state_dict'])
print("=> loaded checkpoint '{}' (epoch {})"
.format(args.evaluate, checkpoint['epoch']))
else:
print("=> no checkpoint found at '{}'".format(args.resume))
cudnn.benchmark = True
train_num = load_file_list()
test_num = load_test_list()
# define loss function (criterion) and pptimizer
criterion = cross_entropy_loss # nn.CrossEntropyLoss()
# if args.cpu:
# criterion = criterion.cpu()
# else:
# criterion = criterion.cuda()
#optimizer = torch.optim.SGD(model.parameters(), args.lr,momentum=args.momentum,weight_decay=args.weight_decay)
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
if args.evaluate:
validate(test_num, model, criterion)
return
for epoch in range(args.start_epoch, args.epochs):
adjust_learning_rate(optimizer, epoch)
# train for one epoch
train(train_num, model, criterion, optimizer, epoch)
# evaluate on validation set
prec1 = validate(test_num, model, criterion)
# remember best prec@1 and save checkpoint
is_best = prec1 > best_prec1
best_prec1 = max(prec1, best_prec1)
save_checkpoint({
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'best_prec1': best_prec1,
}, is_best, filename=os.path.join(args.save_dir, 'checkpoint_{}.tar'.format(epoch)))
def train(train_num, model, criterion, optimizer, epoch):
"""
Run one train epoch
"""
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top2 = AverageMeter()
top3 = AverageMeter()
# switch to train mode
model.train()
end = time.time()
iters = train_num//args.batch_size
for iter in range(iters):
# measure data loading time
data_time.update(time.time() - end)
input,label,mlabel,clabel = get_batch(args.batch_size)
input = torch.FloatTensor(input)
label = torch.FloatTensor(label)
mlabel = torch.FloatTensor(mlabel)
clabel = torch.FloatTensor(clabel)
if args.cpu == False:
input = input.cuda(async=True)
label = label.cuda(async=True)
mlabel = mlabel.cuda(async=True)
clabel = clabel.cuda(async=True)
if args.half:
input = input.half()
# compute output conB_Fea,conM_Fea,conD_Fea clabel,mlabel,label
out,out2,out3 = model(input)
loss = criterion(out,out2,out3, label,mlabel,clabel)
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
out = out.float()
out2 = out2.float()
out3 = out3.float()
loss = loss.float()
# measure accuracy and record loss
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if iter % args.print_freq == 0:
prec1 = accuracy(out.data, clabel,1)
prec2 = accuracy(out2.data, mlabel,2)
prec3 = accuracy(out3.data, label,3)
losses.update(loss.item(), input.size(0))
print('Epoch: [{0}][{1}/{2}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Prec@1 {top1:.3f}\t'
'Prec@2 {top2:.3f}\t'
'Prec@3 {top3:.3f}'.format(
epoch, iter, iters, batch_time=batch_time,
data_time=data_time, loss=losses, top1=prec1, top2=prec2, top3=prec3))
def validate(test_num, model, criterion):
"""
Run evaluation
"""
batch_time = AverageMeter()
losses = AverageMeter()
# switch to evaluate mode
model.eval()
iters = test_num//args.batch_size
for iter in range(iters):
# measure data loading time
input,label,mlabel,clabel = get_test(args.batch_size)
input = torch.FloatTensor(input)
label = torch.FloatTensor(label)
mlabel = torch.FloatTensor(mlabel)
clabel = torch.FloatTensor(clabel)
if args.cpu == False:
input = input.cuda(async=True)
label = label.cuda(async=True)
mlabel = mlabel.cuda(async=True)
clabel = clabel.cuda(async=True)
if args.half:
input = input.half()
# compute output
with torch.no_grad():
out,out2,out3 = model(input)
loss = criterion(out,out2,out3, label,mlabel,clabel)
out = out.float()
out2 = out2.float()
out3 = out3.float()
loss = loss.float()
# measure accuracy and record loss
prec1 = np.array(accuracy(out.data, clabel,1))
prec2 = np.array(accuracy(out2.data, mlabel,2))
prec3 = np.array(accuracy(out3.data, label,3))
losses.update(loss.item(), input.size(0))
if iter % args.print_freq == 0:
print('Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Prec@1 {top1:.3f}\t'
'Prec@2 {top2:.3f}\t'
'Prec@3 {top3:.3f}'.format(
iter, iters, batch_time=batch_time,
loss=losses, top1=prec1, top2=prec2, top3=prec3))
# with open("record.txt", "a+") as f:
# f.write("\t" + 'blabel:'+'{top1:.3f}'.format(top1=prec1))
# f.write("\t" + 'mlabel:'+'{top1:.3f}'.format(top1=prec2))
# f.write("\t" + 'blabel:'+'{top1:.3f}'.format(top1=prec3))
return prec1
def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
"""
Save the training model
"""
torch.save(state, filename)
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 2 every 30 epochs"""
lr = args.lr * (0.5 ** (epoch // 30))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def accuracy(data1,data2,value):
temp1 = MaxNum(data1, value)
temp2 = MaxNum(data2, value)
return np.mean(acc(temp1,temp2))
def MaxNum(nums, value):
temp1 = []
nums = list(nums)
for i in range(args.batch_size):
temp = []
Inf = 0
nt = list(nums[i])
for t in range(value):
temp.append(nt.index(max(nt)))
nt[nt.index(max(nt))] = Inf
temp.sort()
temp1.append(temp)
return temp1
def acc(temp, index):
accuracy = [] # print(np.array(temp).shape)
for k in range(len(temp)):
accuracy.append((temp[k] == index[k]))
return accuracy
if __name__ == '__main__':
main()