forked from antoine77340/howto100m
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
229 lines (210 loc) · 7.35 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
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from __future__ import print_function
import torch as th
from torch.utils.data import DataLoader
import numpy as np
import torch.optim as optim
from args import get_args
import random
import os
from youtube_dataloader import Youtube_DataLoader
from youcook_dataloader import Youcook_DataLoader
from model import Net
from metrics import compute_metrics, print_computed_metrics
from loss import MaxMarginRankingLoss
from gensim.models.keyedvectors import KeyedVectors
import pickle
from msrvtt_dataloader import MSRVTT_DataLoader, MSRVTT_TrainDataLoader
from lsmdc_dataloader import LSMDC_DataLoader
def Eval_retrieval(model, eval_dataloader, dataset_name):
model.eval()
print('Evaluating Text-Video retrieval on {} data'.format(dataset_name))
with th.no_grad():
for i_batch, data in enumerate(eval_dataloader):
# text = data['text'].cuda()
# video = data['video'].cuda()
text = data['text']
video = data['video']
m = model(video, text)
m = m.cpu().detach().numpy()
metrics = compute_metrics(m)
print_computed_metrics(metrics)
def TrainOneBatch(model, opt, data, loss_fun):
# text = data['text'].cuda()
# video = data['video'].cuda()
text = data['text']
video = data['video']
video = video.view(-1, video.shape[-1])
text = text.view(-1, text.shape[-2], text.shape[-1])
opt.zero_grad()
with th.set_grad_enabled(True):
sim_matrix = model(video, text)
loss = loss_fun(sim_matrix)
loss.backward()
opt.step()
return loss.item()
if __name__ == "__main__":
args = get_args()
if args.verbose:
print(args)
# predefining random initial seeds
th.manual_seed(args.seed)
np.random.seed(args.seed)
random.seed(args.seed)
if args.checkpoint_dir != '' and not(os.path.isdir(args.checkpoint_dir)):
os.mkdir(args.checkpoint_dir)
if not(args.youcook) and not(args.msrvtt) and not(args.lsmdc):
print('Loading captions: {}'.format(args.caption_path))
caption = pickle.load(open(args.caption_path, 'rb'))
print('done')
print('Loading word vectors: {}'.format(args.word2vec_path))
we = KeyedVectors.load_word2vec_format(args.word2vec_path, binary=True)
print('done')
if args.youcook:
dataset = Youcook_DataLoader(
data=args.youcook_train_path,
we=we,
max_words=args.max_words,
we_dim=args.we_dim,
)
elif args.msrvtt:
dataset = MSRVTT_TrainDataLoader(
csv_path=args.msrvtt_train_csv_path,
json_path=args.msrvtt_train_json_path,
features_path=args.msrvtt_train_features_path,
we=we,
max_words=args.max_words,
we_dim=args.we_dim,
)
elif args.lsmdc:
dataset = LSMDC_DataLoader(
csv_path=args.lsmdc_train_csv_path,
features_path=args.lsmdc_train_features_path,
we=we,
max_words=args.max_words,
we_dim=args.we_dim,
)
else:
dataset = Youtube_DataLoader(
csv=args.train_csv,
features_path=args.features_path_2D,
features_path_3D=args.features_path_3D,
caption=caption,
min_time=args.min_time,
max_words=args.max_words,
min_words=args.min_words,
feature_framerate=args.feature_framerate,
we=we,
we_dim=args.we_dim,
n_pair=args.n_pair,
)
dataset_size = len(dataset)
dataloader = DataLoader(
dataset,
batch_size=args.batch_size,
num_workers=args.num_thread_reader,
shuffle=True,
batch_sampler=None,
drop_last=True,
)
if args.eval_youcook:
dataset_val = Youcook_DataLoader(
data=args.youcook_val_path,
we=we,
max_words=args.max_words,
we_dim=args.we_dim,
)
dataloader_val = DataLoader(
dataset_val,
batch_size=args.batch_size_val,
num_workers=args.num_thread_reader,
shuffle=False,
)
if args.eval_lsmdc:
dataset_lsmdc = LSMDC_DataLoader(
csv_path=args.lsmdc_test_csv_path,
features_path=args.lsmdc_test_features_path,
we=we,
max_words=args.max_words,
we_dim=args.we_dim,
subsample_csv=1000,
)
dataloader_lsmdc = DataLoader(
dataset_lsmdc,
batch_size=1000,
num_workers=args.num_thread_reader,
shuffle=False,
)
if args.eval_msrvtt:
msrvtt_testset = MSRVTT_DataLoader(
csv_path=args.msrvtt_test_csv_path,
features_path=args.msrvtt_test_features_path,
we=we,
max_words=args.max_words,
we_dim=args.we_dim,
)
dataloader_msrvtt = DataLoader(
msrvtt_testset,
batch_size=1000,
num_workers=args.num_thread_reader,
shuffle=False,
drop_last=False,
)
net = Net(
video_dim=args.feature_dim,
embd_dim=args.embd_dim,
we_dim=args.we_dim,
n_pair=args.n_pair,
max_words=args.max_words,
sentence_dim=args.sentence_dim,
)
net.train()
# Optimizers + Loss
loss_op = MaxMarginRankingLoss(
margin=args.margin,
negative_weighting=args.negative_weighting,
batch_size=args.batch_size,
n_pair=args.n_pair,
hard_negative_rate=args.hard_negative_rate,
)
# net.cuda()
# loss_op.cuda()
if args.pretrain_path != '':
if th.cuda.is_available():
net.load_checkpoint(args.pretrain_path)
else:
net.load_checkpoint(args.pretrain_path, cpu=True)
optimizer = optim.Adam(net.parameters(), lr=args.lr)
if args.verbose:
print('Starting training loop ...')
for epoch in range(args.epochs):
running_loss = 0.0
# if args.eval_youcook:
# Eval_retrieval(net, dataloader_val, 'YouCook2')
# if args.eval_msrvtt:
# Eval_retrieval(net, dataloader_msrvtt, 'MSR-VTT')
# if args.eval_lsmdc:
# Eval_retrieval(net, dataloader_lsmdc, 'LSMDC')
if args.verbose:
print('Epoch: %d' % epoch)
for i_batch, sample_batch in enumerate(dataloader):
batch_loss = TrainOneBatch(net, optimizer, sample_batch, loss_op)
running_loss += batch_loss
if (i_batch + 1) % args.n_display == 0 and args.verbose:
print('Epoch %d, Epoch status: %.4f, Training loss: %.4f' %
(epoch + 1, args.batch_size * float(i_batch) / dataset_size,
running_loss / args.n_display))
running_loss = 0.0
for param_group in optimizer.param_groups:
param_group['lr'] *= args.lr_decay
if args.checkpoint_dir != '':
path = os.path.join(args.checkpoint_dir, 'e{}.pth'.format(epoch + 1))
net.save_checkpoint(path)
if args.eval_youcook:
Eval_retrieval(net, dataloader_val, 'YouCook2')
if args.eval_msrvtt:
Eval_retrieval(net, dataloader_msrvtt, 'MSR-VTT')
if args.eval_lsmdc:
Eval_retrieval(net, dataloader_lsmdc, 'LSMDC')