-
Notifications
You must be signed in to change notification settings - Fork 13
/
train_x3d_charades.py
236 lines (187 loc) · 9.52 KB
/
train_x3d_charades.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
import os
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.autograd import Variable
import torchvision
from torchvision import datasets, transforms
from torchsummary import summary
import numpy as np
from barbar import Bar
import pkbar
from apmeter import APMeter
import x3d as resnet_x3d
from charades import Charades
from transforms.spatial_transforms import Compose, Normalize, RandomHorizontalFlip, MultiScaleRandomCrop, MultiScaleRandomCropMultigrid, ToTensor, CenterCrop, CenterCropScaled
from transforms.temporal_transforms import TemporalRandomCrop
from transforms.target_transforms import ClassLabel
import warnings
warnings.filterwarnings("ignore")
parser = argparse.ArgumentParser()
parser.add_argument('-gpu', default='0', type=str)
args = parser.parse_args()
os.environ["CUDA_VISIBLE_DEVICES"]=args.gpu
BS = 16
BS_UPSCALE = 2
INIT_LR = 0.02 * BS_UPSCALE
GPUS = 2
X3D_VERSION = 'M'
CHARADES_ROOT = '/nfs/bigneuron/add_disk0/kumarak/Charades_v1_rgb'
CHARADES_ANNO = 'data/charades.json'
CHARADES_DATASET_SIZE = {'train':7900, 'val':1850}
CHARADES_MEAN = [0.413, 0.368, 0.338]
CHARADES_STD = [0.131, 0.125, 0.132] # CALCULATED ON CHARADES TRAINING SET FOR FRAME-WISE MEANS
# ON VAL SET MEAN:[0.415 0.384 0.366], STD:[0.146 0.140 0.137]
# warmup_steps=0
def run(init_lr=INIT_LR, max_epochs=100, root=CHARADES_ROOT, anno=CHARADES_ANNO, batch_size=BS*BS_UPSCALE):
frames=80 # DOUBLED INSIDE DATASET, AS LONGER CLIPS
crop_size = {'S':160, 'M':224, 'XL':312}[X3D_VERSION]
resize_size = {'S':[180.,225.], 'M':[256.,256.], 'XL':[360.,450.]}[X3D_VERSION] # 'M':[256.,320.] FOR LONGER SCHEDULE
gamma_tau = {'S':6, 'M':5, 'XL':5}[X3D_VERSION] # DOUBLED INSIDE DATASET, AS LONGER CLIPS
st_steps = 0 # FOR LR WARM-UP
load_steps = 0 # FOR LOADING AND PRINT SCHEDULE
steps = 0
epochs = 0
num_steps_per_update = 1 # ACCUMULATE GRADIENT IF NEEDED
cur_iterations = steps * num_steps_per_update
iterations_per_epoch = CHARADES_DATASET_SIZE['train']//batch_size
val_iterations_per_epoch = CHARADES_DATASET_SIZE['val']//(batch_size//2)
max_steps = iterations_per_epoch * max_epochs
train_spatial_transforms = Compose([MultiScaleRandomCropMultigrid([crop_size/i for i in resize_size], crop_size),
RandomHorizontalFlip(),
ToTensor(255),
Normalize(CHARADES_MEAN, CHARADES_STD)])
val_spatial_transforms = Compose([CenterCropScaled(crop_size),
ToTensor(255),
Normalize(CHARADES_MEAN, CHARADES_STD)])
dataset = Charades(anno, 'training', root, train_spatial_transforms,
frames=80, gamma_tau=gamma_tau, crops=1)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True,
num_workers=8, pin_memory=True)
val_dataset = Charades(anno, 'testing', root, val_spatial_transforms,
frames=80, gamma_tau=gamma_tau, crops=10)
val_dataloader = torch.utils.data.DataLoader(val_dataset, batch_size=batch_size//2, shuffle=False,
num_workers=8, pin_memory=True)
dataloaders = {'train': dataloader, 'val': val_dataloader}
datasets = {'train': dataset, 'val': val_dataset}
print('train',len(datasets['train']),'val',len(datasets['val']))
print('Total iterations:', max_steps, 'Total epochs:', max_epochs)
print('datasets created')
x3d = resnet_x3d.generate_model(x3d_version=X3D_VERSION, n_classes=400, n_input_channels=3,
dropout=0.5, base_bn_splits=1)
load_ckpt = torch.load('models/x3d_multigrid_kinetics_fb_pretrained.pt')
x3d.load_state_dict(load_ckpt['model_state_dict'])
save_model = 'models/x3d_charades_rgb_sgd_'
x3d.replace_logits(157)
if steps>0:
load_ckpt = torch.load('models/x3d_charades_rgb_sgd_'+str(load_steps).zfill(6)+'.pt')
x3d.load_state_dict(load_ckpt['model_state_dict'])
x3d.cuda()
x3d = nn.DataParallel(x3d)
print('model loaded')
lr = init_lr
print ('INIT LR: %f'%lr)
optimizer = optim.SGD(x3d.parameters(), lr=lr, momentum=0.9, weight_decay=1e-5)
lr_sched = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', patience=2, factor=0.1, verbose=True)
if steps>0:
optimizer.load_state_dict(load_ckpt['optimizer_state_dict'])
lr_sched.load_state_dict(load_ckpt['scheduler_state_dict'])
criterion = nn.BCEWithLogitsLoss()
val_apm = APMeter()
tr_apm = APMeter()
while epochs < max_epochs:
print ('Step {} Epoch {}'.format(steps, epochs))
print ('-' * 10)
# Each epoch has a training and validation phase
for phase in 2*['train']+['val']:
bar_st = iterations_per_epoch if phase == 'train' else val_iterations_per_epoch
bar = pkbar.Pbar(name='update: ', target=bar_st)
if phase == 'train':
x3d.train(True)
epochs += 1
torch.autograd.set_grad_enabled(True)
else:
x3d.train(False) # Set model to evaluate mode
_ = x3d.module.aggregate_sub_bn_stats() # FOR EVAL AGGREGATE BN STATS
torch.autograd.set_grad_enabled(False)
tot_loss = 0.0
tot_cls_loss = 0.0
num_iter = 0
optimizer.zero_grad()
# Iterate over data.
print(phase)
for i,data in enumerate(dataloaders[phase]):
num_iter += 1
bar.update(i)
if phase == 'train':
inputs, labels = data
else:
inputs, labels = data
b,n,c,t,h,w = inputs.shape # FOR MULTIPLE TEMPORAL CROPS
inputs = inputs.view(b*n,c,t,h,w)
inputs = inputs.cuda() # B 3 T W H
labels = labels.cuda() # B C
logits = x3d(inputs).squeeze(2) # B C
if phase == 'train':
probs = F.sigmoid(logits)
else:
logits = logits.view(b,n,logits.shape[1]) # FOR MULTIPLE TEMPORAL CROPS
probs = F.sigmoid(logits)
#probs = torch.mean(probs, 1)
#logits = torch.mean(logits, 1)
probs = torch.max(probs, dim=1)[0]
logits = torch.max(logits, dim=1)[0]
cls_loss = criterion(logits, labels)
tot_cls_loss += cls_loss.item()
if phase == 'train':
tr_apm.add(probs.detach().cpu().numpy(), labels.cpu().numpy())
else:
val_apm.add(probs.detach().cpu().numpy(), labels.cpu().numpy())
loss = cls_loss/num_steps_per_update
tot_loss += loss.item()
if phase == 'train':
loss.backward()
if num_iter == num_steps_per_update and phase == 'train':
#lr_warmup(lr, steps-st_steps, warmup_steps, optimizer)
steps += 1
num_iter = 0
optimizer.step()
optimizer.zero_grad()
s_times = iterations_per_epoch//2
if (steps-load_steps) % s_times == 0:
tr_map = tr_apm.value().mean()
tr_apm.reset()
print (' Epoch:{} {} steps: {} Cls Loss: {:.4f} Tot Loss: {:.4f} mAP: {:.4f}'.format(epochs, phase,
steps, tot_cls_loss/(s_times*num_steps_per_update), tot_loss/s_times, tr_map))#, tot_acc/(s_times*num_steps_per_update)))
tot_loss = tot_cls_loss = 0.
if steps % (1000) == 0:
ckpt = {'model_state_dict': x3d.module.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'scheduler_state_dict': lr_sched.state_dict()}
torch.save(ckpt, save_model+str(steps).zfill(6)+'.pt')
if phase == 'val':
val_map = val_apm.value().mean()
lr_sched.step(tot_loss)
val_apm.reset()
print (' Epoch:{} {} Loc Cls Loss: {:.4f} Tot Loss: {:.4f} mAP: {:.4f}'.format(epochs, phase,
tot_cls_loss/num_iter, (tot_loss*num_steps_per_update)/num_iter, val_map))
tot_loss = tot_cls_loss = 0.
def lr_warmup(init_lr, cur_steps, warmup_steps, opt):
start_after = 1
if cur_steps < warmup_steps and cur_steps > start_after:
lr_scale = min(1., float(cur_steps + 1) / warmup_steps)
for pg in opt.param_groups:
pg['lr'] = lr_scale * init_lr
def print_stats(long_ind, batch_size, stats, gamma_tau, bn_splits, lr):
bs = batch_size * LONG_CYCLE[long_ind]
if long_ind in [0,1]:
bs = [bs*j for j in [2,1]]
print(' ***** LR {} Frames {}/{} BS ({},{}) W/H ({},{}) BN_splits {} long_ind {} *****'.format(lr, stats[0][0], gamma_tau, bs[0], bs[1], stats[2][0], stats[3][0], bn_splits, long_ind))
else:
bs = [bs*j for j in [4,2,1]]
print(' ***** LR {} Frames {}/{} BS ({},{},{}) W/H ({},{},{}) BN_splits {} long_ind {} *****'.format(lr, stats[0][0], gamma_tau, bs[0], bs[1], bs[2], stats[1][0], stats[2][0], stats[3][0], bn_splits, long_ind))
if __name__ == '__main__':
run()