-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
229 lines (175 loc) · 9.44 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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from metric import compress, calculate_map, calculate_top_map
import datasets
import settings
from models import ImgNet, TxtNet
import os.path as osp
import numpy as np
import scipy.io as sio
from numpy import dot, cross, kron
class Session:
def __init__(self):
self.logger = settings.logger
torch.manual_seed(1)
torch.cuda.manual_seed_all(1)
torch.cuda.set_device(settings.GPU_ID)
if settings.DATASET == "WIKI":
self.train_dataset = datasets.WIKI(root=settings.DATA_DIR, train=True,
transform=datasets.wiki_train_transform)
self.test_dataset = datasets.WIKI(root=settings.DATA_DIR, train=False,
transform=datasets.wiki_test_transform)
self.database_dataset = datasets.WIKI(root=settings.DATA_DIR, train=True,
transform=datasets.wiki_test_transform)
if settings.DATASET == "MIRFlickr":
self.train_dataset = datasets.MIRFlickr(train=True, transform=datasets.mir_train_transform)
self.test_dataset = datasets.MIRFlickr(train=False, database=False, transform=datasets.mir_test_transform)
self.database_dataset = datasets.MIRFlickr(train=False, database=True,
transform=datasets.mir_test_transform)
if settings.DATASET == "NUSWIDE":
self.train_dataset = datasets.NUSWIDE(train=True, transform=datasets.nus_train_transform)
self.test_dataset = datasets.NUSWIDE(train=False, database=False, transform=datasets.nus_test_transform)
self.database_dataset = datasets.NUSWIDE(train=False, database=True, transform=datasets.nus_test_transform)
# Data Loader (Input Pipeline)
self.train_loader = torch.utils.data.DataLoader(dataset=self.train_dataset,
batch_size=settings.BATCH_SIZE,
shuffle=True,
num_workers=settings.NUM_WORKERS,
drop_last=True)
self.test_loader = torch.utils.data.DataLoader(dataset=self.test_dataset,
batch_size=settings.BATCH_SIZE,
shuffle=False,
num_workers=settings.NUM_WORKERS)
self.database_loader = torch.utils.data.DataLoader(dataset=self.database_dataset,
batch_size=settings.BATCH_SIZE,
shuffle=False,
num_workers=settings.NUM_WORKERS)
self.CodeNet_I = ImgNet(code_len=settings.CODE_LEN)
self.FeatNet_I = ImgNet(code_len=settings.CODE_LEN)
self.txt_feat_len = datasets.txt_feat_len
self.CodeNet_T = TxtNet(code_len=settings.CODE_LEN, txt_feat_len=self.txt_feat_len)
if settings.DATASET == "WIKI":
self.opt_I = torch.optim.SGD([{'params': self.CodeNet_I.fc_encode.parameters(), 'lr': settings.LR_IMG},
{'params': self.CodeNet_I.alexnet.classifier.parameters(),
'lr': settings.LR_IMG}],
momentum=settings.MOMENTUM, weight_decay=settings.WEIGHT_DECAY)
if settings.DATASET == "MIRFlickr" or settings.DATASET == "NUSWIDE":
self.opt_I = torch.optim.SGD(self.CodeNet_I.parameters(), lr=settings.LR_IMG, momentum=settings.MOMENTUM,
weight_decay=settings.WEIGHT_DECAY)
self.opt_T = torch.optim.SGD(self.CodeNet_T.parameters(), lr=settings.LR_TXT, momentum=settings.MOMENTUM,
weight_decay=settings.WEIGHT_DECAY)
def train(self, epoch):
self.CodeNet_I.cuda().train()
self.FeatNet_I.cuda().eval()
self.CodeNet_T.cuda().train()
self.CodeNet_I.set_alpha(epoch)
self.CodeNet_T.set_alpha(epoch)
self.logger.info('Epoch [%d/%d], alpha for ImgNet: %.3f, alpha for TxtNet: %.3f' % (
epoch + 1, settings.NUM_EPOCH, self.CodeNet_I.alpha, self.CodeNet_T.alpha))
for idx, (img, F_T, labels, _) in enumerate(self.train_loader):
img = Variable(img.cuda())
F_T = Variable(torch.FloatTensor(F_T.numpy()).cuda())
labels = Variable(labels.cuda())
self.opt_I.zero_grad()
self.opt_T.zero_grad()
F_I, _, _ = self.FeatNet_I(img)
_, hid_I, code_I = self.CodeNet_I(img)
_, hid_T, code_T = self.CodeNet_T(F_T)
F_I = F.normalize(F_I)
S_I1 = F_I.mm(F_I.t())
C_I = F_I.detach().clone()
D_I = F_I.detach().clone()
C_I[C_I > 0] = 1
D_I[D_I == 0] = -1
D_I[D_I > 0] = 0
C_I1 = C_I.mm(C_I.t())
D_I1 = D_I.mm(D_I.t())
E_I1 = 4096 - (C_I1 + D_I1)
F_I1 = (3*abs(C_I1-E_I1))/(C_I1+E_I1+0.01)
S_I = S_I1 * (F_I1)
S_I = S_I * 2 - 1
F_T = F.normalize(F_T)
S_T1 = F_T.mm(F_T.t())
C_T = F_T.detach().clone()
D_T = F_T.detach().clone()
C_T[C_T > 0] = 1
C_T[C_T <= 0] = 0
D_T[D_T == 0] = -1
D_T[D_T > 0] = 0
C_T1 = C_T.mm(C_T.t())
D_T1 = D_T.mm(D_T.t())
E_T1 = self.txt_feat_len - (C_T1 + D_T1)
F_T1 = (2*abs(C_T1-E_T1))/(C_T1+E_T1+0.01)
S_T = S_T1 * (F_T1)
S_T = S_T * 2 - 1
B_I = F.normalize(code_I)
B_T = F.normalize(code_T)
BI_BI = B_I.mm(B_I.t())
BT_BT = B_T.mm(B_T.t())
BI_BT = B_I.mm(B_T.t())
S = settings.BETA * S_I + (1 - settings.BETA) * S_T
loss1 = F.mse_loss(BI_BI, S)
loss2 = F.mse_loss(BI_BT, S)
loss3 = F.mse_loss(BT_BT, S)
loss4 = F.mse_loss(B_I, B_T)
loss = settings.LAMBDA1 * loss1 + settings.LAMBDA3 * loss2 + settings.LAMBDA2 * loss3 + loss4
loss.backward()
self.opt_I.step()
self.opt_T.step()
if (idx + 1) % (len(self.train_dataset) // settings.BATCH_SIZE / settings.EPOCH_INTERVAL) == 0:
self.logger.info('Epoch [%d/%d], Iter [%d/%d] Loss1: %.4f Loss2: %.4f Loss3: %.4f Loss4: %.4f Total Loss: %.4f'
% (
epoch + 1, settings.NUM_EPOCH, idx + 1, len(self.train_dataset) // settings.BATCH_SIZE,
loss1.item(), loss2.item(), loss3.item(), loss4.item(), loss.item()))
def eval(self, epoch):
self.logger.info('--------------------Evaluation: Calculate top MAP-------------------')
# Change model to 'eval' mode (BN uses moving mean/var).
self.CodeNet_I.eval().cuda()
self.CodeNet_T.eval().cuda()
if settings.DATASET == "MIRFlickr" :
re, re_BI, re_BT, re_L, qu_BI, qu_BT, qu_L = compress(self.database_loader, self.test_loader, self.CodeNet_I,
self.CodeNet_T, self.database_dataset, self.test_dataset)
MAP_I2T = calculate_top_map(qu_B=qu_BI, re_B=re, qu_L=qu_L, re_L=re_L, topk=50)
MAP_T2I = calculate_top_map(qu_B=qu_BT, re_B=re, qu_L=qu_L, re_L=re_L, topk=50)
self.logger.info('MAP of Image to Text: %.3f, MAP of Text to Image: %.3f' % (MAP_I2T, MAP_T2I))
self.logger.info('--------------------------------------------------------------------')
def save_checkpoints(self, step, file_name='latest.pth'):
ckp_path = osp.join(settings.MODEL_DIR, file_name)
obj = {
'ImgNet': self.CodeNet_I.state_dict(),
'TxtNet': self.CodeNet_T.state_dict(),
'step': step,
}
torch.save(obj, ckp_path)
self.logger.info('**********Save the trained model successfully.**********')
def load_checkpoints(self, file_name='latest.pth'):
ckp_path = osp.join(settings.MODEL_DIR, file_name)
try:
obj = torch.load(ckp_path, map_location=lambda storage, loc: storage.cuda())
self.logger.info('**************** Load checkpoint %s ****************' % ckp_path)
except IOError:
self.logger.error('********** No checkpoint %s!*********' % ckp_path)
return
self.CodeNet_I.load_state_dict(obj['ImgNet'])
self.CodeNet_T.load_state_dict(obj['TxtNet'])
self.logger.info('********** The loaded model has been trained for %d epochs.*********' % obj['step'])
def main():
sess = Session()
torch.set_num_threads(3)
if settings.EVAL == True:
sess.load_checkpoints()
sess.eval()
else:
for epoch in range(settings.NUM_EPOCH):
# train the Model
sess.train(epoch)
# eval the Model
if (epoch + 1) % settings.EVAL_INTERVAL == 0:
sess.eval(epoch)
# save the model
if epoch + 1 == settings.NUM_EPOCH:
sess.save_checkpoints(step=epoch + 1)
if __name__ == '__main__':
main()