-
Notifications
You must be signed in to change notification settings - Fork 2
/
ocda.py
332 lines (273 loc) · 12.5 KB
/
ocda.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
#!/usr/bin/env python3 -u
# Copyright (c) 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the LICENSE file in
# the root directory of this source tree.
from __future__ import print_function
import argparse
import csv
import os
import numpy as np
import torch
from torch.autograd import Variable
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import network
from helper.mixup_utils import progress_bar
from helper.data_list import ImageList_idx, ImageList, ImageList_MixUp, ImageList_ocda
from torch.utils.data import DataLoader
import ml_collections
import random
def lr_scheduler(optimizer, iter_num, max_iter, gamma=10, power=0.75):
decay = (1 + gamma * iter_num / max_iter) ** (-power)
for param_group in optimizer.param_groups:
param_group['lr'] = param_group['lr0'] * decay
param_group['weight_decay'] = 1e-3
param_group['momentum'] = 0.9
param_group['nesterov'] = True
# wandb.log({'MISC/LR': param_group['lr']})
return optimizer
def op_copy(optimizer):
for param_group in optimizer.param_groups:
param_group['lr0'] = param_group['lr']
return optimizer
def init_src_model_load(args):
## set base network
if args.net[0:3] == 'res':
netF = network.ResBase(res_name=args.net, se=args.se, nl=args.nl).cuda()
elif args.net[0:3] == 'vgg':
netF = network.VGGBase(vgg_name=args.net).cuda()
elif args.net == 'vit':
netF = network.ViT().cuda()
elif args.net == 'deit_s':
netF = torch.hub.load('facebookresearch/deit:main', 'deit_small_patch16_224', pretrained=True).cuda()
netF.in_features = 1000
netB = network.feat_bootleneck(type='bn', feature_dim=netF.in_features,bottleneck_dim=256).cuda()
netC = network.feat_classifier(type='wn', class_num=args.class_num, bottleneck_dim=256).cuda()
return netF, netB, netC
def image_train(resize_size=256, crop_size=224, alexnet=False):
if not alexnet:
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
return transforms.Compose([
transforms.Resize((resize_size, resize_size)),
transforms.RandomCrop(crop_size),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize
])
def data_load(args):
## prepare data
def image_train(resize_size=256, crop_size=224, alexnet=False):
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
return transforms.Compose([
transforms.Resize((crop_size, crop_size)),
transforms.ToTensor(),
normalize
])
dsets = {}
dset_loaders = {}
txt_tar = open(f'{args.txt_folder}/{args.dset}/{names[args.s]}.csv').readlines()
print("Source Domain: ", names[args.s], "No. of Images: ", len(txt_tar))
dsets['train'] = ImageList_ocda(txt_tar, transform=image_train(), target = args.t, train=True)
print("Since ODCA only using: ", len(dsets['train']))
dset_loaders['train'] = DataLoader(dsets['train'], batch_size=args.batch_size, shuffle=True, drop_last=False)
dsets['test'] = ImageList_ocda(txt_tar, transform=image_train(), target = args.t, train=False)
print("target images: ", len(dsets['test']))
dset_loaders['test'] = DataLoader(dsets['test'], batch_size=args.batch_size, shuffle=True, drop_last=False)
return dset_loaders,dsets
def separate_classwise_idx(args, dset, num_classes): #!@ args
all_data_clswise = np.array(dset.imgs)
numbers = np.array(list(map(int, all_data_clswise[:,2])))
classwise_dset = {}
classwise_loaders = {}
for i in range(num_classes):
idx_dict = np.argwhere(numbers==i).squeeze().tolist()
classwise_dset[i] = torch.utils.data.Subset(dset, idx_dict)
classwise_loaders[i] = DataLoader(classwise_dset[i], batch_size=args.batch_size, shuffle=True, num_workers=args.worker, drop_last=True)
return classwise_loaders, classwise_dset
def mixup_data(x, y, alpha=1.0, use_cuda=True):
'''Returns mixed inputs, pairs of targets, and lambda'''
if alpha > 0:
lam = np.random.beta(alpha, alpha)
else:
lam = 1
batch_size = x.size()[0]
if use_cuda:
index = torch.randperm(batch_size).cuda()
else:
index = torch.randperm(batch_size)
mixed_x = lam * x + (1 - lam) * x[index, :]
y_a, y_b = y, y[index]
return mixed_x, y_a, y_b, lam
def mixup_criterion(criterion, pred, y_a, y_b, lam):
return lam * criterion(pred, y_a) + (1 - lam) * criterion(pred, y_b)
def train(args, epoch,train_loader):
print('\nEpoch: %d' % epoch)
netF.train()
netB.train()
netC.train()
train_loss = 0
reg_loss = 0
correct = 0
total = 0
for batch_idx, (inputs, pseudo_lbl, targets, domain) in enumerate(train_loader):
if use_cuda:
inputs, targets, pseudo_lbl, domain = inputs.cuda(), targets.cuda(), pseudo_lbl.cuda(), domain.cuda()
# optim.lr_scheduler.CosineAnnealingLR(optimizer, args.epoch, eta_min = 1e-6, last_epoch = epoch)
inputs, targets_a, targets_b, lam = mixup_data(inputs, pseudo_lbl, args.alpha, use_cuda)
inputs, targets_a, targets_b = map(Variable, (inputs, targets_a, targets_b))
outputs = netC(netB(netF(inputs)))
loss = mixup_criterion(criterion, outputs, targets_a, targets_b, lam)
train_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += pseudo_lbl.size(0)
correct += (lam * predicted.eq(targets_a.data).cpu().sum().float()
+ (1 - lam) * predicted.eq(targets_b.data).cpu().sum().float())
optimizer.zero_grad()
loss.backward()
optimizer.step()
progress_bar(batch_idx, len(train_loader),
'Loss: %.3f | Acc: %.3f%% (%d/%d)'
% (train_loss/(batch_idx+1),100.*correct/total, correct, total))
# return (train_loss/batch_idx, reg_loss/batch_idx, 100.*correct/total)
# checkpoint(args, netF, netB, netC)
return (train_loss/batch_idx, reg_loss/batch_idx, 100.*correct/total)
def test(epoch,testloader):
global best_acc
netF.eval()
netB.eval()
netC.eval()
test_loss = 0
correct = 0
total = 0
with torch.no_grad():
for batch_idx, (inputs, pseudo_lbl, targets, domain) in enumerate(testloader):
if use_cuda:
inputs, targets = inputs.cuda(), targets.cuda(),
inputs, targets = Variable(inputs), Variable(targets)
outputs = netC(netB(netF(inputs)))
loss = criterion(outputs, targets)
test_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
correct += predicted.eq(targets.data).cpu().sum()
progress_bar(batch_idx, len(testloader),'Loss: %.3f | Acc: %.3f%% (%d/%d)'% (test_loss/(batch_idx+1), 100.*correct/total, correct, total))
acc = 100.*correct/total
# if epoch == start_epoch + args.epoch - 1 or acc > best_acc:
# checkpoint(args, netF, netB, netC)
if acc > best_acc:
best_acc = acc
return (test_loss/batch_idx, 100.*correct/total)
def checkpoint(args, netF, netB, netC):
# Save checkpoint.
save_pth = os.path.join(args.save_weights, args.dset, names[args.s])
if not os.path.exists(save_pth):
os.makedirs(save_pth)
torch.save(netF.state_dict(), os.path.join(save_pth, "target_F.pt"))
torch.save(netB.state_dict(), os.path.join(save_pth, "target_B.pt"))
torch.save(netC.state_dict(), os.path.join(save_pth, "target_C.pt"))
print('Model saved to',save_pth )
def adjust_learning_rate(optimizer, epoch):
"""decrease the learning rate at 100 and 150 epoch"""
lr = args.lr
if epoch >= 100:
lr /= 10
if epoch >= 150:
lr /= 10
for param_group in optimizer.param_groups:
param_group['lr'] = lr
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='PyTorch CIFAR10 Training')
parser.add_argument('--gpu_id', type=str, nargs='?', default='0', help="device id to run")
parser.add_argument('--lr', default=1e-3, type=float, help='learning rate')
parser.add_argument('--resume', '-r', action='store_true', help='resume from checkpoint')
parser.add_argument('--net', default="deit_s", type=str, help='model type (default: ResNet18)')
parser.add_argument('--name', default='0', type=str, help='name of run')
parser.add_argument('--suffix', default='ocda', type=str, help='name of run')
parser.add_argument('--seed', default=0, type=int, help='random seed')
parser.add_argument('--batch_size', default=32, type=int, help='batch size')
parser.add_argument('--epoch', default=200, type=int, help='total epochs to run')
parser.add_argument('--interval', default=2, type=int)
parser.add_argument('--no-augment', dest='augment', action='store_false', help='use standard augmentation (default: True)')
parser.add_argument('--decay', default=1e-4, type=float, help='weight decay')
parser.add_argument('--alpha', default=1., type=float, help='mixup interpolation coefficient (default: 1)')
parser.add_argument('--s', default=0, type=int)
parser.add_argument('--t', default=1, type=int)
parser.add_argument('--txt_folder', default='csv', type=str)
parser.add_argument('--save_weights', default='OCDA_wts', type=str)
parser.add_argument('--dset', type=str, default='office-home', choices=['office-home'])
args = parser.parse_args()
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_id
use_cuda = torch.cuda.is_available()
best_acc = 0 # best test accuracy
start_epoch = 1 # start from epoch 0 or last checkpoint epoch
if args.seed != 0:
torch.manual_seed(args.seed)
if args.dset == 'office-home':
names = ['Art', 'Clipart', 'Product', 'RealWorld']
args.class_num = 65
# Data
print('==> Preparing data..')
if args.augment:
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465),
(0.2023, 0.1994, 0.2010)),
])
else:
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465),
(0.2023, 0.1994, 0.2010)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
if not os.path.isdir('results'):
os.mkdir('results')
print('==> Perparing Dataloaders and Building model..')
all_loader,all_dset = data_load(args)
netF, netB, netC = init_src_model_load(args)
if use_cuda:
netF.cuda()
netB.cuda()
netC.cuda()
# net = torch.nn.DataParallel(net)
cudnn.benchmark = True
print('Using CUDA..')
criterion = nn.CrossEntropyLoss()
param_group = []
for k, v in netF.named_parameters():
param_group += [{'params': v, 'lr': args.lr*0.1}]
for k, v in netB.named_parameters():
param_group += [{'params': v, 'lr': args.lr}]
for k, v in netC.named_parameters():
param_group += [{'params': v, 'lr': args.lr}]
optimizer = optim.SGD(param_group, lr=args.lr, momentum=0.9, weight_decay=args.decay)
optimizer = op_copy(optimizer)
logname = ('results/log' + '.csv')
if not os.path.exists(logname):
with open(logname, 'w') as logfile:
logwriter = csv.writer(logfile, delimiter=',')
logwriter.writerow(['epoch', 'train loss', 'reg loss', 'train acc',
'test loss', 'test acc'])
print(f'\nStarting training {names[args.s]} to others.')
train_len = len(all_dset['train'])
test_len = len(all_dset['test'])
print(f'Training: {train_len} Images \t Testing: {test_len} Images')
for epoch in range(start_epoch, args.epoch+1):
train_loss, reg_loss, train_acc = train(args, epoch, all_loader['train'])
checkpoint(args, netF, netB, netC)
optimizer = lr_scheduler(optimizer, iter_num=epoch, max_iter=args.epoch)
if epoch % args.interval == 0:
print('\n Start Testing')
test_loss, test_acc = test(epoch, all_loader['test'])