-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathmain.py
183 lines (143 loc) · 5.83 KB
/
main.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
from __future__ import division
import argparse
import torch
from torch.utils import model_zoo
from torch.autograd import Variable
import models
import utils
from data_loader import get_train_test_loader, get_office31_dataloader
CUDA = True if torch.cuda.is_available() else False
LEARNING_RATE = 1e-3
WEIGHT_DECAY = 5e-4
MOMENTUM = 0.9
BATCH_SIZE = [200, 56]
EPOCHS = 20
source_loader = get_office31_dataloader(case='amazon', batch_size=BATCH_SIZE[0])
target_loader = get_office31_dataloader(case='webcam', batch_size=BATCH_SIZE[1])
def train(model, optimizer, epoch, _lambda):
result = []
# Expected size : xs -> (batch_size, 3, 300, 300), ys -> (batch_size)
source, target = list(enumerate(source_loader)), list(enumerate(target_loader))
train_steps = min(len(source), len(target))
for batch_idx in range(train_steps):
_, (source_data, source_label) = source[batch_idx]
_, (target_data, _) = target[batch_idx]
if CUDA:
source_data = source_data.cuda()
source_label = source_label.cuda()
target_data = target_data.cuda()
source_data, source_label = Variable(source_data), Variable(source_label)
target_data = Variable(target_data)
optimizer.zero_grad()
out1, out2 = model(source_data, target_data)
classification_loss = torch.nn.functional.cross_entropy(out1, source_label)
coral_loss = models.CORAL(out1, out2)
sum_loss = _lambda*coral_loss + classification_loss
sum_loss.backward()
optimizer.step()
result.append({
'epoch': epoch,
'step': batch_idx + 1,
'total_steps': train_steps,
'lambda': _lambda,
'coral_loss': coral_loss.data[0],
'classification_loss': classification_loss.data[0],
'total_loss': sum_loss.data[0]
})
print('Train Epoch: {:2d} [{:2d}/{:2d}]\t'
'Lambda: {:.4f}, Class: {:.6f}, CORAL: {:.6f}, Total_Loss: {:.6f}'.format(
epoch,
batch_idx + 1,
train_steps,
_lambda,
classification_loss.data[0],
coral_loss.data[0],
sum_loss.data[0]
))
return result
def test(model, dataset_loader, e):
model.eval()
test_loss = 0
correct = 0
for data, target in dataset_loader:
if CUDA:
data, target = data.cuda(), target.cuda()
data, target = Variable(data, volatile=True), Variable(target)
out, _ = model(data, data)
# sum up batch loss
test_loss += torch.nn.functional.cross_entropy(out, target, size_average=False).data[0]
# get the index of the max log-probability
pred = out.data.max(1, keepdim=True)[1]
correct += pred.eq(target.data.view_as(pred)).cpu().sum()
test_loss /= len(dataset_loader.dataset)
return {
'epoch': e,
'average_loss': test_loss,
'correct': correct,
'total': len(dataset_loader.dataset),
'accuracy': 100. * correct / len(dataset_loader.dataset)
}
# load AlexNet pre-trained model
def load_pretrained(model):
url = 'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth'
pretrained_dict = model_zoo.load_url(url)
model_dict = model.state_dict()
# filter out unmatch dict and delete last fc bias, weight
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
# del pretrained_dict['classifier.6.bias']
# del pretrained_dict['classifier.6.weight']
model_dict.update(pretrained_dict)
model.load_state_dict(model_dict)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--load', help='Resume from checkpoint file')
args = parser.parse_args()
model = models.DeepCORAL(31)
# support different learning rate according to CORAL paper
# i.e. 10 times learning rate for the last two fc layers.
optimizer = torch.optim.SGD([
{'params': model.sharedNet.parameters()},
{'params': model.fc.parameters(), 'lr': 10*LEARNING_RATE},
], lr=LEARNING_RATE, momentum=MOMENTUM)
if CUDA:
model = model.cuda()
if args.load is not None:
utils.load_net(model, args.load)
else:
load_pretrained(model.sharedNet)
training_statistic = []
testing_s_statistic = []
testing_t_statistic = []
for e in range(0, EPOCHS):
_lambda = (e+1)/EPOCHS
# _lambda = 0.0
res = train(model, optimizer, e+1, _lambda)
print('###EPOCH {}: Class: {:.6f}, CORAL: {:.6f}, Total_Loss: {:.6f}'.format(
e+1,
sum(row['classification_loss'] / row['total_steps'] for row in res),
sum(row['coral_loss'] / row['total_steps'] for row in res),
sum(row['total_loss'] / row['total_steps'] for row in res),
))
training_statistic.append(res)
test_source = test(model, source_loader, e)
test_target = test(model, target_loader, e)
testing_s_statistic.append(test_source)
testing_t_statistic.append(test_target)
print('###Test Source: Epoch: {}, avg_loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)'.format(
e+1,
test_source['average_loss'],
test_source['correct'],
test_source['total'],
test_source['accuracy'],
))
print('###Test Target: Epoch: {}, avg_loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)'.format(
e+1,
test_target['average_loss'],
test_target['correct'],
test_target['total'],
test_target['accuracy'],
))
utils.save(training_statistic, 'training_statistic.pkl')
utils.save(testing_s_statistic, 'testing_s_statistic.pkl')
utils.save(testing_t_statistic, 'testing_t_statistic.pkl')
utils.save_net(model, 'checkpoint.tar')