-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_source.py
266 lines (202 loc) · 9.39 KB
/
train_source.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
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import random
from model import Resnet
import argparse
import numpy as np
import torchvision.transforms as transforms
import wandb
import pickle
import time
from sklearn.metrics import accuracy_score
from torch.optim.lr_scheduler import *
from utils import save_weights
from datasets import dataset
from os.path import join
from os import makedirs
parser = argparse.ArgumentParser(description='PyTorch Training')
parser.add_argument('--data_dir', type=str, default='data')
parser.add_argument('--dataset', default='visdac/source', type=str)
parser.add_argument('--num_class', default=10, type=int)
parser.add_argument('--batch_size', default=256, type=int, help='train batchsize')
parser.add_argument('--lr', '--learning_rate', default=0.001, type=float, help='initial learning rate')
parser.add_argument('--num_epochs', default=300, type=int)
parser.add_argument('--alfa', default=0.1, type=float)
parser.add_argument('--seed', default=123)
parser.add_argument('--gpuid', default=0, type=int)
parser.add_argument('--run_name', type=str)
parser.add_argument('--wandb', action='store_true', help="Use wandb")
args = parser.parse_args()
torch.cuda.set_device(args.gpuid)
random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
if args.wandb:
wandb.init(project="Guiding Pseudo-labels with Uncertainty Estimation for Test-Time Adaptation", name = args.run_name)
def smoothed_cross_entropy(logits, labels, num_classes, epsilon=0):
log_probs = F.log_softmax(logits, dim=1)
with torch.no_grad():
targets = torch.zeros_like(log_probs).scatter_(1, labels.unsqueeze(1), 1)
targets = (1 - epsilon) * targets + epsilon / num_classes
loss = (-targets * log_probs).sum(dim=1).mean()
return loss
# Training
def train(epoch, net, optimizer, trainloader):
loss = []
acc = []
net.train()
for batch_idx, batch in enumerate(trainloader):
x = batch[0].cuda()
y = batch[2].cuda()
_, outputs = net(x)
l = smoothed_cross_entropy(outputs, y, args.num_class, args.alfa)
l.backward()
optimizer.step()
optimizer.zero_grad()
accuracy = 100.*accuracy_score(y.to('cpu'), outputs.to('cpu').max(1)[1])
loss.append(l.item())
acc.append(accuracy)
if batch_idx % 100 == 0:
print('Epoch [%3d/%3d] Iter[%3d/%3d]\t '
%(epoch, args.num_epochs, batch_idx+1, len(trainloader)))
loss = np.mean( np.array(loss) )
acc = np.mean( np.array(acc) )
print("Training acc = ", acc)
if args.wandb:
wandb.log({
'train_loss': loss, \
'train_acc': acc, \
}, step=epoch)
def test(epoch,net):
net.eval()
correct = 0
total = 0
it = 0
loss = 0
with torch.no_grad():
for batch_idx, batch in enumerate(test_loader):
inputs, targets = batch[0].cuda(), batch[2].cuda()
_, outputs = net(inputs)
_, predicted = torch.max(outputs, 1)
total += targets.size(0)
correct += predicted.eq(targets).cpu().sum().item()
loss += CEloss(outputs, targets)
it += 1
acc = 100.*correct/total
loss = loss/it
print("\n| Test Epoch #%d\t Accuracy: %.2f%%\n" %(epoch,acc))
if args.wandb:
wandb.log({
'val_loss': loss, \
'val_net1_accuracy': acc, \
}, step=epoch)
return acc
def create_model(arch, args):
model = Resnet(arch, args.num_class, pretrained=None)
model = model.cuda()
return model
# Main code starts here
dataset_name = args.dataset.split('/')[0]
if dataset_name == 'officehome':
if args.run_name[-13:] == "no-imbalanced":
imbalanced = None
else:
imbalanced = "_RS"
elif dataset_name == 'visdac':
if args.run_name[-2:] == "10":
imbalanced = "10"
elif args.run_name[-2:] == "50":
imbalanced = "50"
elif args.run_name[-3:] == "100":
imbalanced = "100"
else:
imbalanced = None
elif dataset_name == 'domainnet':
if args.run_name[-3:] == "126":
imbalanced = "126"
elif args.run_name[-4:] == "mini":
imbalanced = "mini"
else:
print("ERROR: Unknown config for domainnet")
exit()
else:
print("ERROR: Unknown dataset %s" % dataset_name)
exit()
arch = 'resnet18'
if dataset_name == 'pacs':
train_dataset = dataset(dataset=args.dataset, root=join(args.data_dir, 'PACS'), imb=imbalanced,
mode='train',
transform=transforms.Compose([transforms.Resize(256), transforms.RandomCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
)
test_dataset = dataset(dataset=args.dataset, root=join(args.data_dir, 'PACS'), imb=imbalanced,
mode='test',
transform=transforms.Compose([transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
)
if dataset_name == 'officehome':
train_dataset = dataset(dataset=args.dataset, root=join(args.data_dir, 'officeHome'), imb=imbalanced,
mode='train',
transform=transforms.Compose([transforms.Resize(256), transforms.RandomCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
)
test_dataset = dataset(dataset=args.dataset, root=join(args.data_dir, 'officeHome'), imb=imbalanced,
mode='test',
transform=transforms.Compose([transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
)
arch = 'resnet50'
elif dataset_name == 'visdac':
train_dataset = dataset(dataset=args.dataset, root=join(args.data_dir, 'VISDA'), imb=imbalanced,
mode='train',
transform=transforms.Compose([transforms.Resize(256), transforms.RandomCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
)
test_dataset = dataset(dataset=args.dataset, root=join(args.data_dir, 'VISDA'), imb=imbalanced,
mode='test',
transform=transforms.Compose([transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
)
arch = 'resnet101'
elif dataset_name == 'domainnet':
train_dataset = dataset(dataset=args.dataset, root=join(args.data_dir, 'domainNet'), imb=imbalanced, noisy_path=None,
mode='train',
transform=transforms.Compose([transforms.Resize(256), transforms.RandomCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
)
test_dataset = dataset(dataset=args.dataset, root=join(args.data_dir, 'domainNet'), imb=imbalanced, noisy_path=None,
mode='test',
transform=transforms.Compose([transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
)
arch = 'resnet50'
logdir = 'logs/' + args.run_name
net = create_model(arch, args)
cudnn.benchmark = True
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=args.batch_size,
num_workers=2,
drop_last=True,
shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=args.batch_size,
num_workers=2,
drop_last=True,
shuffle=False)
optimizer = optim.SGD(net.parameters(), lr=args.lr, weight_decay=5e-4, momentum=0.5, nesterov=False)
CE = nn.CrossEntropyLoss(reduction='none')
CEloss = nn.CrossEntropyLoss()
makedirs(logdir, exist_ok=True)
best = 0
accuracy_val = []
print("Training started!")
for epoch in range(args.num_epochs+1):
start_time = time.time()
train(epoch, net, optimizer, train_loader) # train net1
acc = test(epoch,net)
print("Time elapsed = %.1fs" % (time.time() - start_time))
accuracy_val.append(acc)
with open(join(logdir, "accuracy_val.pkl"), "wb") as fp: # Pickling
pickle.dump(accuracy_val, fp)
if acc > best:
save_weights(net, epoch, logdir + '/weights_best.tar')
best = acc
print("Saving best!")
if args.wandb:
wandb.run.summary['best_acc'] = best
print("\n *** Accuracy for the best source: %.2f *** " % best)