-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
259 lines (192 loc) · 9.19 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
from __future__ import unicode_literals, print_function, division
import argparse
import timeit
from datetime import datetime
import socket
import os
import glob
from read_data import *
import torch
from tensorboardX import SummaryWriter
from torch import nn, optim
from torch.autograd import Variable
import torch.nn as nn
from C3D import *
#os.environ["CUDA_VISIBLE_DEVICES"]="1"
def train_C3D(args):
device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu")
print("Device being used:", device)
save_dir_root = "./"
if args.resume_epoch != 0:
runs = sorted(glob.glob(os.path.join(save_dir_root, 'run', 'run_*')))
run_id = int(runs[-1].split('_')[-1]) if runs else 0
else:
runs = sorted(glob.glob(os.path.join(save_dir_root, 'run', 'run_**')))
run_id = int(runs[-1].split('_')[-1]) + 1 if runs else 0
print(run_id)
#run_id = 48
save_dir = os.path.join(save_dir_root, 'run', 'run_' + str(run_id))
saveName = args.modelName + '-' + args.dataset
model = C3D_model(num_classes=2).to(device)
train_params = [{'params': get_1x_lr_params(model), 'lr': args.lr},
{'params': get_10x_lr_params(model), 'lr': args.lr * 10}]
criterion = nn.CrossEntropyLoss().to(device)
optimizer = optim.SGD(train_params, lr=args.lr, momentum=0.9, weight_decay=5e-4)
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10,
gamma=0.1)
if args.resume_epoch == 0:
print("Training {} from scratch...".format('C3D'))
else:
checkpoint = torch.load(os.path.join(save_dir, 'models', saveName + '_epoch-' + str(args.resume_epoch - 1) + '.pth.tar'),
map_location=lambda storage, loc: storage) # Load all tensors onto the CPU
print("Initializing weights from: {}...".format(
os.path.join(save_dir, 'models', saveName + '_epoch-' + str(args.resume_epoch - 1) + '.pth.tar')))
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['opt_dict'])
print('Total params: %.2fM' % (sum(p.numel() for p in model.parameters()) / 1000000.0))
criterion.to(device)
log_dir = os.path.join(save_dir, 'models', datetime.now().strftime('%b%d_%H-%M-%S') + '_' + socket.gethostname())
writer = SummaryWriter(log_dir=log_dir)
print('Training model on {} dataset...'.format(args.dataset))
train = Dashcam_data(train='train')
step = 0
for epoch in range(args.resume_epoch, args.epochs):
# each epoch has a training and validation step
for phase in ['train']:
start_time = timeit.default_timer()
# reset the running loss and corrects
running_loss = 0.0
running_corrects = 0.0
# scheduler.step() is to be called once every epoch during training
dataset = train
scheduler.step()
model.train()
im_names = (dataset.total_folders)
tot_batches = int(im_names/args.batch_size)
for i in range(tot_batches):
inputs, labels = dataset.get_next_batch(args.batch_size,args.clip_len)
# move inputs and labels to the device the training is taking place on
inputs = Variable(inputs).to(device)
labels = Variable(labels).to(device)
optimizer.zero_grad()
k = inputs
for j in range(args.clip_len-1):
k[:,:,j,:,:] = inputs[:,:,j+1,:,:]-inputs[:,:,j,:,:]
loss1 = torch.mean(torch.mean(torch.mean(torch.mean(torch.mean(k,dim = 1),dim = 1),dim= 1),dim= 1),dim=0)
outputs = model(inputs)
probs = nn.Softmax(dim=1)(outputs)
preds = torch.max(probs, 1)[1]
loss = criterion(outputs, labels)
loss = loss+loss1
print(loss.item())
if (i % 10)==0:
print("Epoch", epoch, "Batch done ", i, "out of", tot_batches)
print("loss is ", loss.item())
writer.add_scalar('data/train_loss_batch', loss.item(), step)
step += 1
loss.backward()
optimizer.step()
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
epoch_loss = (running_loss / im_names)
epoch_acc = (running_corrects.item() / im_names)*100
if phase == 'train':
writer.add_scalar('data/train_acc_epoch', epoch_acc, epoch)
writer.add_scalar('data/train_loss_epoch', epoch_loss, epoch)
print("[{}] Epoch: {}/{} Loss: {} Acc: {}".format(phase, epoch + 1, args.epochs, epoch_loss, epoch_acc))
stop_time = timeit.default_timer()
print("Execution time: " + str(stop_time - start_time) + "\n")
if epoch % args.snapshot == (args.snapshot - 1):
torch.save({
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'opt_dict': optimizer.state_dict(),
}, os.path.join('./models', saveName + '_epoch-' + str(epoch) + '.pth.tar'))
print("Save model at {}\n".format(
os.path.join('./models', saveName + '_epoch-' + str(epoch) + '.pth.tar')))
torch.save({
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'opt_dict': optimizer.state_dict(),
}, os.path.join('./models', saveName + '_epoch-' + str(epoch) + '.pth.tar'))
print("Save model at {}\n".format(
os.path.join('./models', saveName + '_epoch-' + str(epoch) + '.pth.tar')))
writer.close()
def test_C3D(args):
TP=0
FN=0
FP=0
device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu")
model = C3D_model(num_classes=2).to(device)
checkpoint = torch.load("./models/C3D-dashcam_epoch-24.pth.tar")
model.load_state_dict(checkpoint['state_dict'])
model.eval()
start_time = timeit.default_timer()
criterion = nn.CrossEntropyLoss().to(device)
running_loss = 0.0
running_corrects = 0.0
dataset = Dashcam_data(train='test')
im_names = (dataset.total_folders)
# for inputs, labels in tqdm(test_dataloader):
print(im_names)
tot_batches = int(im_names / args.batch_size)
for i in range(tot_batches):
print(i, "out of", tot_batches)
inputs, labels = dataset.get_next_batch(args.batch_size, args.clip_len)
inputs = inputs.to(device)
labels = labels.to(device)
k = inputs
for j in range(args.clip_len - 1):
k[:, :, j, :, :] = inputs[:, :, j + 1, :, :] - inputs[:, :, j, :, :]
loss1 = torch.mean(torch.mean(torch.mean(torch.mean(torch.mean(k, dim=1), dim=1), dim=1), dim=1), dim=0)
with torch.no_grad():
outputs = model(inputs)
probs = nn.Softmax(dim=1)(outputs)
preds = torch.max(probs, 1)[1]
loss = criterion(outputs, labels)
loss = loss+loss1
running_loss += loss.item() * inputs.size(0)
print("preds",preds)
print("labels.data",labels.data)
for i in range(args.batch_size):
if (preds[i]==1 and labels[i] ==1):
TP+=1
elif (preds[i]==0 and labels[i] ==1):
FN+=1
elif(preds[i]==1 and labels[i]==0):
FP+=1
running_corrects += torch.sum(preds == labels.data)
Recall = TP/(TP+FN)
print("Recall is ", Recall)
Precision = TP / (TP + FP)
print("Precision is ", Precision)
epoch_loss = running_loss / im_names
epoch_acc = (running_corrects.item() / im_names)*100
print("[test] Loss: {} Acc: {}".format(epoch_loss, epoch_acc))
stop_time = timeit.default_timer()
print("Execution time: " + str(stop_time - start_time) + "\n")
def main():
argparser = argparse.ArgumentParser()
argparser.add_argument('--epochs', type=int, help='epoch number', default=60)
argparser.add_argument('--Train', type=bool, default=True)
argparser.add_argument('--continue_training', type=bool, default=True)
argparser.add_argument('--model', type=str, default="checkpoint")
argparser.add_argument('--lr', type=float, default=1e-3)
argparser.add_argument('--batch_size', type=int, default=2)
argparser.add_argument('--clip_len', type=int, default=16)
argparser.add_argument('--resume_epoch', type=int, default=0)
argparser.add_argument('--dataset', type=str, default="dashcam")
argparser.add_argument('--save_dir', type=str, default="logs")
argparser.add_argument('--save_epoch', type=int, default=10)
argparser.add_argument('--snapshot', type=int, default=1)
argparser.add_argument('--num_classes', type=int, default=2)
argparser.add_argument('--modelName', type=str, default="C3D")
argparser.add_argument('--useTest', type=bool, default=False)
argparser.add_argument('--nTestInterval', type=int, default=2)
args = argparser.parse_args()
if args.Train:
train_C3D(args)
else:
test_C3D(args)
if __name__=='__main__':
main()