forked from vladan-stojnic/CMC-RSSR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_CMC.py
230 lines (183 loc) · 7.87 KB
/
train_CMC.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
from __future__ import print_function
import os
import sys
import time
import warnings
import torch
import torch.backends.cudnn as cudnn
from torchvision import transforms
from dataset import (ImageDataset, MultispectralImageDataset,
MultispectralRandomHorizontalFlip,
MultispectralRandomResizedCrop, RGB2Lab, ScalerPCA)
from models.alexnet import alexnet, multispectral_alexnet
from models.resnet import ResNetV2, multispectral_ResNetV2
from NCE.NCEAverage import NCEAverage
from NCE.NCECriterion import NCECriterion
from util import AverageMeter, adjust_learning_rate, parse_option
warnings.filterwarnings("ignore")
def get_train_loader(args):
"""get the train loader"""
data_folder = args.data_folder
image_list = args.image_list
if not args.multispectral:
normalize = transforms.Normalize(mean=[(0 + 100) / 2, (-86.183 + 98.233) / 2, (-107.857 + 94.478) / 2],
std=[(100 - 0) / 2, (86.183 + 98.233) / 2, (107.857 + 94.478) / 2])
transformations = [transforms.RandomResizedCrop(224, scale=(args.crop_low, 1.)),
transforms.RandomHorizontalFlip()]
if args.resize_image_aug:
transformations.insert(0, transforms.Resize((256, 256)))
transformations += [RGB2Lab(), transforms.ToTensor(), normalize]
train_transform = transforms.Compose(transformations)
train_dataset = ImageDataset(data_folder, image_list, transform=train_transform)
train_sampler = None
else:
transformations = [MultispectralRandomResizedCrop(224, scale=(args.crop_low, 1.)),
MultispectralRandomHorizontalFlip()]
transformations += [ScalerPCA('./scaler_pca', use_pca=args.pca), transforms.ToTensor()]
train_transform = transforms.Compose(transformations)
train_dataset = MultispectralImageDataset(data_folder, image_list, transform=train_transform)
train_sampler = None
# train loader
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=args.batch_size, shuffle=(train_sampler is None),
num_workers=args.num_workers, pin_memory=True, sampler=train_sampler)
# num of samples
n_data = len(train_dataset)
print('number of samples: {}'.format(n_data))
return train_loader, n_data
def set_model(args, n_data):
# set the model
if args.model == 'alexnet':
if args.multispectral:
model = multispectral_alexnet(args.feat_dim)
else:
model = alexnet(args.feat_dim)
elif args.model.startswith('resnet'):
if args.multispectral:
model = multispectral_ResNetV2(args.model)
else:
model = ResNetV2(args.model)
else:
raise ValueError('model not supported yet {}'.format(args.model))
contrast = NCEAverage(args.feat_dim, n_data, args.nce_k, args.nce_t, args.nce_m)
criterion_l = NCECriterion(n_data)
criterion_ab = NCECriterion(n_data)
if torch.cuda.is_available():
if torch.cuda.device_count() > 1:
model = torch.nn.DataParallel(model).cuda()
else:
model = model.cuda()
contrast = contrast.cuda()
criterion_ab = criterion_ab.cuda()
criterion_l = criterion_l.cuda()
cudnn.benchmark = True
return model, contrast, criterion_ab, criterion_l
def set_optimizer(args, model):
# return optimizer
optimizer = torch.optim.SGD(model.parameters(),
lr=args.learning_rate,
momentum=args.momentum,
weight_decay=args.weight_decay)
return optimizer
def train(epoch, train_loader, model, contrast, criterion_l, criterion_ab, optimizer, opt):
"""
one epoch training
"""
model.train()
contrast.train()
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
l_loss_meter = AverageMeter()
ab_loss_meter = AverageMeter()
l_prob_meter = AverageMeter()
ab_prob_meter = AverageMeter()
end = time.time()
for idx, (inputs, _, index) in enumerate(train_loader):
data_time.update(time.time() - end)
bsz = inputs.size(0)
inputs = inputs.float()
if torch.cuda.is_available():
index = index.cuda(async=True)
inputs = inputs.cuda()
# ===================forward=====================
feat_l, feat_ab = model(inputs)
out_l, out_ab = contrast(feat_l, feat_ab, index)
l_loss = criterion_l(out_l)
ab_loss = criterion_ab(out_ab)
l_prob = out_l[:, 0].mean()
ab_prob = out_ab[:, 0].mean()
loss = l_loss + ab_loss
# ===================backward=====================
optimizer.zero_grad()
loss.backward()
optimizer.step()
# ===================meters=====================
losses.update(loss.item(), bsz)
l_loss_meter.update(l_loss.item(), bsz)
l_prob_meter.update(l_prob.item(), bsz)
ab_loss_meter.update(ab_loss.item(), bsz)
ab_prob_meter.update(ab_prob.item(), bsz)
batch_time.update(time.time() - end)
end = time.time()
# print info
if (idx + 1) % opt.print_freq == 0:
print('Train: [{0}][{1}/{2}]\t'
'BT {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'DT {data_time.val:.3f} ({data_time.avg:.3f})\t'
'loss {loss.val:.3f} ({loss.avg:.3f})\t'
'l_p {lprobs.val:.3f} ({lprobs.avg:.3f})\t'
'ab_p {abprobs.val:.3f} ({abprobs.avg:.3f})'.format(
epoch, idx + 1, len(train_loader), batch_time=batch_time,
data_time=data_time, loss=losses, lprobs=l_prob_meter,
abprobs=ab_prob_meter))
print(out_l.shape)
sys.stdout.flush()
return l_loss_meter.avg, l_prob_meter.avg, ab_loss_meter.avg, ab_prob_meter.avg
def main():
# parse the args
args = parse_option(True)
# set the loader
train_loader, n_data = get_train_loader(args)
# set the model
model, contrast, criterion_ab, criterion_l = set_model(args, n_data)
# set the optimizer
optimizer = set_optimizer(args, model)
# optionally resume from a checkpoint
args.start_epoch = 1
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'] + 1
model.load_state_dict(checkpoint['model'])
optimizer.load_state_dict(checkpoint['optimizer'])
contrast.load_state_dict(checkpoint['contrast'])
print("=> loaded checkpoint '{}' (epoch {})"
.format(args.resume, checkpoint['epoch']))
else:
print("=> no checkpoint found at '{}'".format(args.resume))
# routine
for epoch in range(args.start_epoch, args.epochs + 1):
adjust_learning_rate(epoch, args, optimizer)
print("==> training...")
time1 = time.time()
l_loss, l_prob, ab_loss, ab_prob = train(epoch, train_loader, model, contrast, criterion_l, criterion_ab,
optimizer, args)
time2 = time.time()
print('epoch {}, total time {:.2f}'.format(epoch, time2 - time1))
# save model
if epoch % args.save_freq == 0:
print('==> Saving...')
state = {
'opt': args,
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'contrast': contrast.state_dict(),
'epoch': epoch,
}
save_file = os.path.join(args.model_folder, 'ckpt_epoch_{epoch}.pth'.format(epoch=epoch))
torch.save(state, save_file)
pass
if __name__ == '__main__':
main()