-
Notifications
You must be signed in to change notification settings - Fork 26
/
train.py
executable file
·354 lines (316 loc) · 17.3 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
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
348
349
350
351
352
353
354
import argparse
import os
import os.path as osp
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import domain_adaptive_module.network as network
import domain_adaptive_module.loss as loss
import domain_adaptive_module.pre_process as prep
from torch.utils.data import DataLoader
import domain_adaptive_module.lr_schedule as lr_schedule
from torch.autograd import Variable
from data_loader import *
import random
import pdb
import math
from prototypical_module.utils import pprint, set_gpu, ensure_path, Averager, Timer, count_acc, euclidean_metric
from torch.autograd import Variable
from torch.nn.parameter import Parameter
class learnedweight(nn.Module):
def __init__(self):
super(learnedweight, self).__init__()
self.fsl_weight = Parameter(torch.ones(1), requires_grad=True)
self.da_weight = Parameter(torch.ones(1), requires_grad=True)
def forward(self, fsl_loss, da_loss):
final_loss = self.fsl_weight + torch.exp(-1 * self.fsl_weight)*fsl_loss + self.da_weight + torch.exp(-1 * self.da_weight)*da_loss
return final_loss
def image_classification_test(loader, model, test_10crop=True):
start_test = True
with torch.no_grad():
if test_10crop:
iter_test = [iter(loader['test'][i]) for i in range(10)]
for i in range(len(loader['test'][0])):
data = [iter_test[j].next() for j in range(10)]
inputs = [data[j][0] for j in range(10)]
labels = data[0][1]
for j in range(10):
inputs[j] = inputs[j].cuda()
labels = labels
outputs = []
for j in range(10):
_, predict_out = model(inputs[j])
outputs.append(nn.Softmax(dim=1)(predict_out))
outputs = sum(outputs)
if start_test:
all_output = outputs.float().cpu()
all_label = labels.float()
start_test = False
else:
all_output = torch.cat((all_output, outputs.float().cpu()), 0)
all_label = torch.cat((all_label, labels.float()), 0)
else:
iter_test = iter(loader["test"])
for i in range(len(loader['test'])):
data = iter_test.next()
inputs = data[0]
labels = data[1]
inputs = inputs.cuda()
labels = labels.cuda()
_, outputs = model(inputs)
if start_test:
all_output = outputs.float().cpu()
all_label = labels.float()
start_test = False
else:
all_output = torch.cat((all_output, outputs.float().cpu()), 0)
all_label = torch.cat((all_label, labels.float()), 0)
_, predict = torch.max(all_output, 1)
accuracy = torch.sum(torch.squeeze(predict).float() == all_label).item() / float(all_label.size()[0])
return accuracy
def train(config):
## set pre-process
prep_dict = {}
prep_config = config["prep"]
prep_dict["source"] = prep.image_train(**config["prep"]['params'])
prep_dict["target"] = prep.image_train(**config["prep"]['params'])
if prep_config["test_10crop"]:
prep_dict["test"] = prep.image_test_10crop(**config["prep"]['params'])
else:
prep_dict["test"] = prep.image_test(**config["prep"]['params'])
## prepare data
dsets = {}
dset_loaders = {}
data_config = config["data"]
train_bs = data_config["source"]["batch_size"]
test_bs = data_config["test"]["batch_size"]
dsets["target"] = MiniImageNet(root=data_config["target"]["root"], dataset=config["dataset"], mode=data_config["target"]["split"])
dset_loaders["target"] = DataLoader(dsets["target"], batch_size=config["shot"] * config["train_way"], \
shuffle=True, num_workers=4, drop_last=True)
dsets["source"] = MiniImageNet(root=data_config["source"]["root"], dataset=config["dataset"], mode=data_config["source"]["split"])
fsl_train_sampler = CategoriesSampler(dsets["source"].label, 100,
config["train_way"], config["shot"] + config["query"])
dset_loaders["source"] = DataLoader(dataset=dsets["source"], batch_sampler=fsl_train_sampler,
num_workers=4, pin_memory=True)
fsl_valset = MiniImageNet(root=data_config["fsl_test"]["root"], dataset=config["dataset"], mode=data_config["fsl_test"]["split"])
fsl_val_sampler = CategoriesSampler(fsl_valset.label, 400,
config["test_way"], config["shot"] + config["query"])
fsl_val_loader = DataLoader(dataset=fsl_valset, batch_sampler=fsl_val_sampler,
num_workers=4, pin_memory=True)
class_num = config["network"]["params"]["class_num"]
## set base network
net_config = config["network"]
base_network = net_config["name"](**net_config["params"])
base_network = base_network.cuda()
## add additional network for some methods
if config["loss"]["random"]:
random_layer = network.RandomLayer([base_network.output_num(), class_num], config["loss"]["random_dim"])
ad_net = network.AdversarialNetwork(config["loss"]["random_dim"], 1024)
else:
random_layer = None
ad_net = network.AdversarialNetwork(base_network.output_num() * class_num, 1024)
if config["loss"]["random"]:
random_layer.cuda()
ad_net = ad_net.cuda()
autoweight = learnedweight().cuda()
#print(base_network.get_parameters())
#print([{'params': autoweight.parameters(), 'lr_mult': 1, 'decay_mult': 2}])
parameter_list = base_network.get_parameters() + ad_net.get_parameters() + [{'params': autoweight.parameters(), 'lr_mult': 1, 'decay_mult': 2}]
## set optimizer
optimizer_config = config["optimizer"]
optimizer = optimizer_config["type"](parameter_list, \
**(optimizer_config["optim_params"]))
param_lr = []
for param_group in optimizer.param_groups:
param_lr.append(param_group["lr"])
schedule_param = optimizer_config["lr_param"]
lr_scheduler = lr_schedule.schedule_dict[optimizer_config["lr_type"]]
gpus = config['gpu'].split(',')
if len(gpus) > 1:
ad_net = nn.DataParallel(ad_net, device_ids=[int(i) for i in gpus])
base_network = nn.DataParallel(base_network, device_ids=[int(i) for i in gpus])
autoweight = nn.DataParallel(autoweight, device_ids=[int(i) for i in gpus])
## train
len_train_source = len(dset_loaders["source"])
len_train_target = len(dset_loaders["target"])
transfer_loss_value = classifier_loss_value = total_loss_value = 0.0
best_acc = 0.0
start = 0
for i in range(config["num_iterations"]):
if i % config["test_interval"] == config["test_interval"] - 1:
base_network.train(False)
# temp_acc = image_classification_test(dset_loaders, \
# base_network, test_10crop=prep_config["test_10crop"])
# temp_model = nn.Sequential(base_network)
# if temp_acc > best_acc:
# best_acc = temp_acc
# best_model = temp_model
# log_str = "iter: {:05d}, precision: {:.5f}".format(i, temp_acc)
# config["out_file"].write(log_str+"\n")
# config["out_file"].flush()
# print(log_str)
for i, batch in enumerate(fsl_val_loader, 1):
data, _ = [_.cuda() for _ in batch]
k = config["test_way"] * config["shot"]
data_shot, data_query = data[:k], data[k:]
x, _ = base_network(data_shot)
x = x.reshape(config["shot"], config["test_way"], -1).mean(dim=0)
p = x
proto_query, _ = base_network(data_query)
proto_query = proto_query.reshape(config["shot"], config["train_way"], -1).mean(dim=0)
logits = euclidean_metric(proto_query, p)
label = torch.arange(config["test_way"]).repeat(config["query"])
label = label.type(torch.cuda.LongTensor)
acc = count_acc(logits, label)
ave_acc.add(acc)
print('batch {}: {:.2f}({:.2f})'.format(i, ave_acc.item() * 100, acc * 100))
x = None; p = None; logits = None
if i % config["snapshot_interval"] == 0:
torch.save(nn.Sequential(base_network), osp.join(config["output_path"], \
"iter_{:05d}_model.pth.tar".format(i)))
loss_params = config["loss"]
## train one iter
base_network.train(True)
ad_net.train(True)
optimizer = lr_scheduler(optimizer, i, **schedule_param)
optimizer.zero_grad()
if i % len_train_source == 0:
iter_fsl_train = iter(dset_loaders["source"])
if i % len_train_target == 0:
iter_target = iter(dset_loaders["target"])
inputs_target, labels_target = iter_target.next()
inputs_fsl, labels_fsl = iter_fsl_train.next()
inputs_target = inputs_target.cuda()
inputs_fsl, labels_fsl = inputs_fsl.cuda(), labels_fsl.cuda()
p = config["shot"] * config["train_way"]
data_shot, data_query = inputs_fsl[:p], inputs_fsl[p:]
labels_source = labels_fsl[:p]
proto_source, outputs_source = base_network(data_shot)
features_target, outputs_target = base_network(inputs_target)
proto = proto_source.reshape(config["shot"], config["train_way"], -1).mean(dim=0)
label = torch.arange(config["train_way"]).repeat(config["query"])
label = label.type(torch.cuda.LongTensor)
query_proto, _ = base_network(data_query)
logits = euclidean_metric(query_proto, proto)
# fsl_loss = F.cross_entropy(logits, label)
fsl_loss = nn.CrossEntropyLoss()(logits, label)
fsl_acc = count_acc(logits, label)
features = torch.cat((proto_source, features_target), dim=0)
outputs = torch.cat((outputs_source, outputs_target), dim=0)
softmax_out = nn.Softmax(dim=1)(outputs)
if config['method'] == 'CDAN+E':
entropy = loss.Entropy(softmax_out)
transfer_loss = loss.CDAN([features, softmax_out], ad_net, entropy, network.calc_coeff(i), random_layer)
elif config['method'] == 'CDAN':
transfer_loss = loss.CDAN([features, softmax_out], ad_net, None, None, random_layer)
elif config['method'] == 'DANN':
transfer_loss = loss.DANN(features, ad_net)
else:
raise ValueError('Method cannot be recognized.')
# classifier_loss = nn.CrossEntropyLoss()(outputs_source, labels_source)
if i % 1 == 0:
print('iter: ', i, 'transfer_loss: ', transfer_loss.data, 'fsl_loss: ', fsl_loss.data, 'fsl_acc: ', fsl_acc)
# total_loss = loss_params["trade_off"] * transfer_loss + 0.2 * fsl_loss
total_loss = autoweight(fsl_loss, transfer_loss)/10
print(total_loss)
total_loss.backward()
optimizer.step()
torch.save(best_model, osp.join(config["output_path"], "best_model.pth.tar"))
return best_acc
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Conditional Domain Adversarial Network')
parser.add_argument('--method', type=str, default='CDAN+E', choices=['CDAN', 'CDAN+E', 'DANN'])
parser.add_argument('--gpu_id', type=str, nargs='?', default='0', help="device id to run")
parser.add_argument('--net', type=str, default='ResNet50', choices=["ResNet18", "ResNet34", "ResNet50", "ResNet101", "ResNet152", "VGG11", "VGG13", "VGG16", "VGG19", "VGG11BN", "VGG13BN", "VGG16BN", "VGG19BN", "AlexNet"])
parser.add_argument('--dset', type=str, default='office', choices=['office', 'image-clef', 'visda', 'office-home', 'mini-imagenet', 'tiered-imagenet'], help="The dataset or source dataset used")
parser.add_argument('--s_dset_path', type=str, default='dataset/mini-imagenet/train', help="The dataset path")
parser.add_argument('--fsl_test_path', type=str, default='dataset/mini-imagenet/test_new_domain', help="The dataset path")
parser.add_argument('--test_interval', type=int, default=10000, help="interval of two continuous test phase")
parser.add_argument('--snapshot_interval', type=int, default=500, help="interval of two continuous output model")
parser.add_argument('--output_dir', type=str, default='mini_auto_weight10', help="output directory of our model (in ../snapshot directory)")
parser.add_argument('--lr', type=float, default=0.0005, help="learning rate")
parser.add_argument('--random', type=bool, default=False, help="whether use random projection")
parser.add_argument('--shot', type=int, default=1)
parser.add_argument('--query', type=int, default=15)
parser.add_argument('--train-way', type=int, default=30)
parser.add_argument('--test-way', type=int, default=5)
parser.add_argument('--pretrained', type=str, default='tiered_checkpoint.pth.tar')
args = parser.parse_args()
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_id
#os.environ["CUDA_VISIBLE_DEVICES"] = '0,1,2,3'
# train config
config = {}
config['method'] = args.method
config["gpu"] = args.gpu_id
config["num_iterations"] = 100004
config["test_interval"] = args.test_interval
config["snapshot_interval"] = args.snapshot_interval
config["output_for_test"] = True
config["output_path"] = "snapshot/" + args.output_dir
if not osp.exists(config["output_path"]):
os.system('mkdir -p '+config["output_path"])
config["out_file"] = open(osp.join(config["output_path"], "log.txt"), "w")
if not osp.exists(config["output_path"]):
os.mkdir(config["output_path"])
config["prep"] = {"test_10crop":True, 'params':{"resize_size":256, "crop_size":224, 'alexnet':False}}
config["loss"] = {"trade_off":1.0}
if "AlexNet" in args.net:
config["prep"]['params']['alexnet'] = True
config["prep"]['params']['crop_size'] = 227
config["network"] = {"name":network.AlexNetFc, \
"params":{"use_bottleneck":True, "bottleneck_dim":256, "new_cls":True} }
elif "ResNet" in args.net:
config["network"] = {"name":network.ResNetFc, \
"params":{"resnet_name":args.net, "use_bottleneck":True, "bottleneck_dim":256, "new_cls":True, "pretrained_model":args.pretrained} }
elif "VGG" in args.net:
config["network"] = {"name":network.VGGFc, \
"params":{"vgg_name":args.net, "use_bottleneck":True, "bottleneck_dim":256, "new_cls":True} }
config["loss"]["random"] = args.random
config["loss"]["random_dim"] = 1024
config["optimizer"] = {"type":optim.SGD, "optim_params":{'lr':args.lr, "momentum":0.9, \
"weight_decay":0.0005, "nesterov":True}, "lr_type":"inv", \
"lr_param":{"lr":args.lr, "gamma":0.001, "power":0.75} }
config["dataset"] = args.dset
config["data"] = {"source":{"root":args.s_dset_path, "split":"train", "batch_size":50}, \
"target":{"root":args.s_dset_path, "split":"val_new_domain", "batch_size":8}, \
"test":{"root":args.s_dset_path, "split":"val_new_domain", "batch_size":4}, \
"fsl_test":{"root":args.fsl_test_path, "split":"val_new_domain_fsl", "batch_size":4}}
if config["dataset"] == "office":
if ("amazon" in args.s_dset_path and "webcam" in args.t_dset_path) or \
("webcam" in args.s_dset_path and "dslr" in args.t_dset_path) or \
("webcam" in args.s_dset_path and "amazon" in args.t_dset_path) or \
("dslr" in args.s_dset_path and "amazon" in args.t_dset_path):
config["optimizer"]["lr_param"]["lr"] = 0.001 # optimal parameters
elif ("amazon" in args.s_dset_path and "dslr" in args.t_dset_path) or \
("dslr" in args.s_dset_path and "webcam" in args.t_dset_path):
config["optimizer"]["lr_param"]["lr"] = 0.0003 # optimal parameters
config["network"]["params"]["class_num"] = 31
elif config["dataset"] == "image-clef":
config["optimizer"]["lr_param"]["lr"] = 0.001 # optimal parameters
config["network"]["params"]["class_num"] = 12
elif config["dataset"] == "visda":
config["optimizer"]["lr_param"]["lr"] = 0.001 # optimal parameters
config["network"]["params"]["class_num"] = 12
config['loss']["trade_off"] = 1.0
elif config["dataset"] == "office-home":
config["optimizer"]["lr_param"]["lr"] = 0.001 # optimal parameters
config["network"]["params"]["class_num"] = 65
elif config["dataset"] == "mini-imagenet":
config["optimizer"]["lr_param"]["lr"] = 0.001 # optimal parameters
config["network"]["params"]["class_num"] = 64
config['loss']["trade_off"] = 1.0
elif config["dataset"] == "tiered-imagenet":
config["optimizer"]["lr_param"]["lr"] = 0.001 # optimal parameters
config["network"]["params"]["class_num"] = 351
config['loss']["trade_off"] = 1.0
else:
raise ValueError('Dataset cannot be recognized. Please define your own dataset here.')
config["out_file"].write(str(config))
config["out_file"].flush()
config["shot"] = args.shot
config["query"] = args.query
config["train_way"] = args.train_way
config["test_way"] = args.test_way
train(config)