-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
289 lines (244 loc) · 13.9 KB
/
train.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
# -*- coding: utf-8 -*-
from __future__ import print_function, division
import os
import time
import argparse
import warnings
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
from torch.autograd import Variable
from torch.cuda.amp import autocast,GradScaler
from models.create_model import create_model
from datasets.make_dataloader import make_dataset
from utils.utils_optimizer import make_optimizer
from utils.utils_server import save_network, copyfiles2checkpoints
from utils.utils_loss_func import cal_kl_loss, cal_loss, cal_triplet_loss, Tripletloss, TripletLoss
warnings.filterwarnings("ignore")
version = torch.__version__
#fp16
try:
from apex.fp16_utils import *
from apex import amp, optimizers
except ImportError: # will be 3.x series
print('This is not an error. If you want to use low precision, i.e., fp16, please install the apex with cuda support (https://github.com/NVIDIA/apex) and update pytorch to 1.0')
######################################################################
# Options
# --------
def get_parse():
parser = argparse.ArgumentParser(description='Training')
parser.add_argument('--gpu_ids', default='0', type=str, help='gpu_ids: e.g. 0 0,1,2 0,2')
parser.add_argument('--name', default='Safe-Net', type=str, help='output model name')
parser.add_argument('--data_dir', default='./data/University-Release/train', type=str, help='training dir path')
parser.add_argument('--train_all', action='store_true', help='use all training data' )
parser.add_argument('--color_jitter', action='store_true', help='use color jitter in training' )
parser.add_argument('--num_worker', default=4, type=int, help='number of worker' )
parser.add_argument('--batchsize', default=8, type=int, help='batchsize')
parser.add_argument('--pad', default=0, type=int, help='padding')
parser.add_argument('--h', default=256, type=int, help='height')
parser.add_argument('--w', default=256, type=int, help='width')
parser.add_argument('--views', default=2, type=int, help='the number of views')
parser.add_argument('--erasing_p', default=0, type=float, help='Random Erasing probability, in [0,1]')
parser.add_argument('--warm_epoch', default=0, type=int, help='the first K epoch that needs warm up')
parser.add_argument('--lr', default=0.01, type=float, help='learning rate')
parser.add_argument('--moving_avg', default=1.0, type=float, help='moving average')
parser.add_argument('--DA', action='store_true', help='use Color Data Augmentation' )
parser.add_argument('--share', action='store_true', default=True, help='share weight between different view' )
parser.add_argument('--fp16', action='store_true', default=False, help='use float16 instead of float32, which will save about 50% memory' )
parser.add_argument('--autocast', action='store_true', default=True, help='use mix precision' )
parser.add_argument('--block', default=4, type=int, help='')
parser.add_argument('--kl_loss', action='store_true', default=False, help='kl_loss' )
parser.add_argument('--triplet_loss', default=0.3, type=float, help='')
parser.add_argument('--sample_num', default=1, type=float, help='num of repeat sampling' )
parser.add_argument('--num_epochs', default=120, type=int, help='' )
parser.add_argument('--steps', default=[70,110], type=int, help='' )
opt = parser.parse_args()
return opt
### training model
def train_model(model, opt, optimizer, scheduler, dataloaders, dataset_sizes):
use_gpu = opt.use_gpu
num_epochs = opt.num_epochs
since = time.time()
warm_up = 0.1 # We start from the 0.1*lrRate
warm_iteration = round(dataset_sizes['satellite'] / opt.batchsize) * opt.warm_epoch # first 5 epoch
scaler = GradScaler()
criterion = nn.CrossEntropyLoss()
loss_kl = nn.KLDivLoss(reduction='batchmean')
triplet_loss = Tripletloss(margin=opt.triplet_loss)
for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch, num_epochs - 1))
print('-' * 10)
# Each epoch has a training and validation phase
for phase in ['train']:
if phase == 'train':
model.train(True) # Set model to training mode
else:
model.train(False) # Set model to evaluate mode
running_cls_loss = 0.0
running_triplet = 0.0
running_kl_loss = 0.0
running_loss = 0.0
running_corrects = 0.0
running_corrects2 = 0.0
running_corrects3 = 0.0
for data, data2, data3 in dataloaders:
# satallite # street # drone
loss = 0.0
# get the inputs
inputs, labels = data
inputs2, labels2 = data2
inputs3, labels3 = data3
now_batch_size, c, h, w = inputs.shape
if now_batch_size < opt.batchsize: # skip the last batch
continue
if use_gpu:
inputs = Variable(inputs.cuda().detach())
inputs2 = Variable(inputs2.cuda().detach())
inputs3 = Variable(inputs3.cuda().detach())
labels = Variable(labels.cuda().detach())
labels2 = Variable(labels2.cuda().detach())
labels3 = Variable(labels3.cuda().detach())
else:
inputs, labels = Variable(inputs), Variable(labels)
# zero the parameter gradients
optimizer.zero_grad()
# forward
if phase == 'val':
with torch.no_grad():
outputs, outputs2 = model(inputs, inputs3)
else:
if opt.views == 2:
with autocast():
outputs, outputs2 = model(inputs, inputs3) # satellite and drone
elif opt.views == 3:
outputs, outputs2, outputs3 = model(inputs, inputs2, inputs3)
f_triplet_loss = torch.tensor((0))
if opt.triplet_loss>0:
features = outputs[1]
features2 = outputs2[1]
split_num = opt.batchsize//opt.sample_num
f_triplet_loss = cal_triplet_loss(features, features2, labels, triplet_loss, split_num)
outputs = outputs[0]
outputs2 = outputs2[0]
if isinstance(outputs, list):
preds = []
preds2 = []
for out,out2 in zip(outputs, outputs2):
preds.append(torch.max(out.data, 1)[1])
preds2.append(torch.max(out2.data, 1)[1])
else:
_, preds = torch.max(outputs.data, 1)
_, preds2 = torch.max(outputs2.data, 1)
kl_loss = torch.tensor((0))
if opt.views == 2:
cls_loss = cal_loss(outputs, labels, criterion) + cal_loss(outputs2, labels3, criterion) # only compute the global branch
if opt.kl_loss:
kl_loss = cal_kl_loss(outputs, outputs2, loss_kl)
elif opt.views == 3:
if isinstance(outputs, list):
preds3 = []
for out3 in outputs3:
preds3.append(torch.max(out3.data, 1)[1])
cls_loss = cal_loss(outputs, labels, criterion) + cal_loss(outputs2, labels2, criterion) + cal_loss(outputs3, labels3, criterion)
loss += cls_loss
else:
_, preds3 = torch.max(outputs3.data, 1)
cls_loss = cal_loss(outputs, labels, criterion) + cal_loss(outputs2, labels2, criterion) + cal_loss(outputs3, labels3, criterion)
loss += cls_loss
loss = kl_loss + cls_loss + f_triplet_loss
# backward + optimize only if in training phase
if epoch < opt.warm_epoch and phase == 'train':
warm_up = min(1.0, warm_up + 0.9 / warm_iteration)
loss *= warm_up
if phase == 'train':
if opt.autocast:
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
else:
loss.backward()
optimizer.step()
# statistics
if int(version[0]) > 0 or int(version[2]) > 3: # for the new version like 0.4.0, 0.5.0 and 1.0.0
running_loss += loss.item() * now_batch_size
running_cls_loss += cls_loss.item() * now_batch_size
running_triplet += f_triplet_loss.item() * now_batch_size
running_kl_loss += kl_loss.item() * now_batch_size
else: # for the old version like 0.3.0 and 0.3.1
running_loss += loss.data[0] * now_batch_size
running_cls_loss += cls_loss.data[0] * now_batch_size
running_triplet += f_triplet_loss.data[0] * now_batch_size
running_kl_loss += kl_loss.data[0] * now_batch_size
if isinstance(preds, list) and isinstance(preds2, list):
running_corrects += sum([float(torch.sum(pred == labels.data)) for pred in preds]) / len(preds)
if opt.views==2:
running_corrects2 += sum([float(torch.sum(pred == labels3.data)) for pred in preds2]) / len(preds2)
else:
running_corrects2 += sum([float(torch.sum(pred == labels2.data)) for pred in preds2]) / len(preds2)
else:
running_corrects += float(torch.sum(preds == labels.data))
if opt.views == 2:
running_corrects2 += float(torch.sum(preds2 == labels3.data))
else:
running_corrects2 += float(torch.sum(preds2 == labels2.data))
if opt.views == 3:
if isinstance(preds, list) and isinstance(preds2, list):
running_corrects3 += sum([float(torch.sum(pred == labels3.data)) for pred in preds3])/len(preds3)
else:
running_corrects3 += float(torch.sum(preds3 == labels3.data))
epoch_cls_loss = running_cls_loss / dataset_sizes['satellite']
epoch_kl_loss = running_kl_loss / dataset_sizes['satellite']
epoch_triplet_loss = running_triplet / dataset_sizes['satellite']
epoch_loss = running_loss / dataset_sizes['satellite']
epoch_acc = running_corrects / dataset_sizes['satellite']
epoch_acc2 = running_corrects2 / dataset_sizes['satellite']
lr_backbone = optimizer.state_dict()['param_groups'][0]['lr']
lr_other = optimizer.state_dict()['param_groups'][1]['lr']
if opt.views == 2:
print('{} Loss: {:.4f} Cls_Loss:{:.4f} KL_Loss:{:.4f} Triplet_Loss {:.4f} Satellite_Acc: {:.4f} Drone_Acc: {:.4f} lr_backbone:{:.6f} lr_other {:.6f}'
.format(phase, epoch_loss, epoch_cls_loss, epoch_kl_loss,
epoch_triplet_loss, epoch_acc,
epoch_acc2, lr_backbone, lr_other))
elif opt.views == 3:
epoch_acc3 = running_corrects3 / dataset_sizes['satellite']
print('{} Loss: {:.4f} Satellite_Acc: {:.4f} Street_Acc: {:.4f} Drone_Acc: {:.4f}'.format(phase,
epoch_loss,
epoch_acc,
epoch_acc2,
epoch_acc3))
# deep copy the model
if phase == 'train':
scheduler.step()
if epoch % 10 == 9 and epoch>=110:
save_network(model, opt.name, epoch)
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
time_elapsed // 60, time_elapsed % 60))
print()
if __name__ == '__main__':
opt = get_parse()
str_ids = opt.gpu_ids.split(',')
gpu_ids = []
for str_id in str_ids:
gid = int(str_id)
if gid >= 0:
gpu_ids.append(gid)
os.environ["CUDA_VISIBLE_DEVICES"] = opt.gpu_ids
use_gpu = torch.cuda.is_available()
opt.use_gpu = use_gpu
# set gpu ids
if len(gpu_ids) > 0:
# torch.cuda.set_device(gpu_ids[0])
cudnn.benchmark = True
dataloaders, class_names, dataset_sizes = make_dataset(opt)
opt.nclasses = len(class_names)
model = create_model(opt)
optimizer_ft, exp_lr_scheduler = make_optimizer(model, opt)
# os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
if len(gpu_ids) > 1:
model = nn.DataParallel(model) # Mulit GPU
model = model.cuda() # Single GPU
copyfiles2checkpoints(opt)
if opt.fp16:
model, optimizer_ft = amp.initialize(model, optimizer_ft, opt_level="O1")
train_model(model,opt, optimizer_ft, exp_lr_scheduler, dataloaders, dataset_sizes)