-
Notifications
You must be signed in to change notification settings - Fork 59
/
train_model.py
203 lines (160 loc) · 7.75 KB
/
train_model.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
import os
import sys
import argparse
import inspect
import datetime
import json
import time
parser = argparse.ArgumentParser()
parser.add_argument('-mode', type=str, help='rgb or flow')
parser.add_argument('-exp_name', type=str)
parser.add_argument('-batch_size', type=int, default=24)
parser.add_argument('-length', type=int, default=16)
parser.add_argument('-learnable', type=str, default='[0,0,0,0,0]')
parser.add_argument('-niter', type=int)
args = parser.parse_args()
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.optim import lr_scheduler
#import models
import flow_2p1d_resnets
device = torch.device('cuda')
##################
#
# Create model, dataset, and training setup
#
##################
model = flow_2p1d_resnets.resnet50(pretrained=False, mode=args.mode, n_iter=args.niter, learnable=eval(args.learnable), num_classes=400)
model = nn.DataParallel(model).to(device)
batch_size = args.batch_size
if args.system == 'hmdb':
from hmdb_dataset import HMDB as DS
dataseta = DS('data/hmdb/split1_train.txt', '/ssd/hmdb/', model=args.model, mode=args.mode, length=args.length)
dl = torch.utils.data.DataLoader(dataseta, batch_size=batch_size, shuffle=True, num_workers=8, pin_memory=True)
dataset = DS('data/hmdb/split1_test.txt', '/ssd/hmdb/', model=args.model, mode=args.mode, length=args.length, c2i=dataseta.class_to_id)
vdl = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True, num_workers=8, pin_memory=True)
dataloader = {'train':dl, 'val':vdl}
if args.system == 'minikinetics':
train = 'data/kinetics/minikinetics_train.json'
val = 'data/kinetics/minikinetics_val.json'
root = '/ssd/kinetics/'
from minikinetics_dataset import MK
dataset_tr = MK(train, root, length=args.length, model=args.model, mode=args.mode)
dl = torch.utils.data.DataLoader(dataset_tr, batch_size=batch_size, shuffle=True, num_workers=8, pin_memory=True)
dataset = MK(val, root, length=args.length, model=args.model, mode=args.mode)
vdl = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True, num_workers=8, pin_memory=True)
dataloader = {'train':dl, 'val':vdl}
if args.system == 'kinetics':
train = 'data/kinetics/kinetics_train.json'
val = 'data/kinetics/kinetics_val.json'
root = '/ssd/kinetics/'
from minikinetics_dataset import MK
dataset_tr = MK(train, root, length=args.length, model=args.model, mode=args.mode)
dl = torch.utils.data.DataLoader(dataset_tr, batch_size=batch_size, shuffle=True, num_workers=8, pin_memory=True)
dataset = MK(val, root, length=args.length, model=args.model, mode=args.mode)
vdl = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True, num_workers=8, pin_memory=True)
dataloader = {'train':dl, 'val':vdl}
# scale lr for flow layer
params = model.parameters()
params = [p for p in params]
other = []
print(len(params))
ln = eval(args.learnable)
if ln[0] == 1:
other += [p for p in params if (p.sum() == model.module.flow_layer.img_grad.sum()).all() and p.size() == model.module.flow_layer.img_grad.size()]
other += [p for p in params if (p.sum() == model.module.flow_layer.img_grad2.sum()).all() and p.size() == model.module.flow_layer.img_grad2.size()]
params = [p for p in params if (p.sum() != model.module.flow_layer.img_grad.sum()).all() or p.size() != model.module.flow_layer.img_grad.size()]
params = [p for p in params if (p.sum() != model.module.flow_layer.img_grad2.sum()).all() or p.size() != model.module.flow_layer.img_grad2.size()]
if ln[1] == 1:
other += [p for p in params if (p.sum() == model.module.flow_layer.f_grad.sum()).all() and p.size() == model.module.flow_layer.f_grad.size()]
other += [p for p in params if (p.sum() == model.module.flow_layer.f_grad2.sum()).all() and p.size() == model.module.flow_layer.f_grad2.size()]
params = [p for p in params if (p.sum() != model.module.flow_layer.f_grad.sum()).all() or p.size() != model.module.flow_layer.f_grad.size()]
params = [p for p in params if (p.sum() != model.module.flow_layer.f_grad2.sum()).all() or p.size() != model.module.flow_layer.f_grad2.size()]
if ln[2] == 1:
other += [p for p in params if (p.sum() == model.module.flow_layer.t.sum()).all() and p.size() == model.module.flow_layer.t.size()]
params = [p for p in params if (p.sum() != model.module.flow_layer.t.sum()).all() or p.size() != model.module.flow_layer.t.size()]
if ln[3] == 1:
other += [p for p in params if (p.sum() == model.module.flow_layer.l.sum()).all() and p.size() == model.module.flow_layer.l.size()]
params = [p for p in params if (p.sum() != model.module.flow_layer.l.sum()).all() or p.size() != model.module.flow_layer.l.size()]
if ln[4] == 1:
other += [p for p in params if (p.sum() == model.module.flow_layer.a.sum()).all() and p.size() == model.module.flow_layer.a.size()]
params = [p for p in params if (p.sum() != model.module.flow_layer.a.sum()).all() or p.size() != model.module.flow_layer.a.size()]
#print([p for p in model.parameters() if (p == model.module.flow_layer.t).all()])
#print(other)
print(len(params), len(other))
#exit()
lr = 0.01
solver = optim.SGD([{'params':params}, {'params':other, 'lr':0.01*lr}], lr=lr, weight_decay=1e-6, momentum=0.9)
lr_sched = optim.lr_scheduler.ReduceLROnPlateau(solver, patience=7)
#################
#
# Setup logs, store model code
# hyper-parameters, etc...
#
#################
log_name = datetime.datetime.today().strftime('%m-%d-%H%M')+'-'+args.exp_name
log_path = os.path.join('logs/',log_name)
os.mkdir(log_path)
os.system('cp * logs/'+log_name+'/')
# deal with hyper-params...
with open(os.path.join(log_path,'params.json'), 'w') as out:
hyper = vars(args)
json.dump(hyper, out)
log = {'iterations':[], 'epoch':[], 'validation':[], 'train_acc':[], 'val_acc':[]}
###############
#
# Train the model and save everything
#
###############
num_epochs = 60
for epoch in range(num_epochs):
for phase in ['train', 'val']:
train = (phase=='train')
if phase == 'train':
model.train()
else:
model.eval()
tloss = 0.
acc = 0.
tot = 0
c = 0
e=s=0
with torch.set_grad_enabled(train):
for vid, cls in dataloader[phase]:
if c%200 == 0:
print('epoch',epoch,'iter',c)
#s=time.time()
#print('btw batch', (s-e)*1000)
vid = vid.to(device)
cls = cls.to(device)
outputs = model(vid)
pred = torch.max(outputs, dim=1)[1]
corr = torch.sum((pred == cls).int())
acc += corr.item()
tot += vid.size(0)
loss = F.cross_entropy(outputs, cls)
#print(loss)
if phase == 'train':
solver.zero_grad()
loss.backward()
solver.step()
log['iterations'].append(loss.item())
tloss += loss.item()
c += 1
#e=time.time()
#print('batch',batch_size,'time',(e-s)*1000)
if phase == 'train':
log['epoch'].append(tloss/c)
log['train_acc'].append(acc/tot)
print('train loss',tloss/c, 'acc', acc/tot)
else:
log['validation'].append(tloss/c)
log['val_acc'].append(acc/tot)
print('val loss', tloss/c, 'acc', acc/tot)
lr_sched.step(tloss/c)
with open(os.path.join(log_path,'log.json'), 'w') as out:
json.dump(log, out)
torch.save(model.state_dict(), os.path.join(log_path, 'hmdb_flow-of-flow_2p1d.pt'))
#lr_sched.step()