-
Notifications
You must be signed in to change notification settings - Fork 10
/
train_source.py
213 lines (154 loc) · 7.28 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
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 *
import argparse
import numpy as np
import torchvision.transforms as transforms
import wandb
from sklearn.metrics import accuracy_score
from torch.optim.lr_scheduler import *
from utils import *
from datasets import *
from os.path import join
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)
model = model.cuda()
return model
arch = 'resnet18'
if args.dataset.split('/')[0] == 'pacs':
train_dataset = dataset(dataset=args.dataset, root=join(args.data_dir, 'PACS'),
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'),
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))])
)
elif args.dataset.split('/')[0] == 'visdac':
train_dataset = dataset(dataset=args.dataset, root=join(args.data_dir, 'VISDA-C'),
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-C'),
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 args.dataset.split('/')[0] == 'domainnet':
train_dataset = dataset(dataset=args.dataset, root=join(args.data_dir, 'domainnet-126'),
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-126'),
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()
best = 0
for epoch in range(args.num_epochs+1):
print('Train Nets')
train(epoch, net, optimizer, train_loader) # train net1
acc = test(epoch,net)
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