-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluating_model.py
291 lines (209 loc) · 8.38 KB
/
evaluating_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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import os
import cv2
import sys
import importlib
import torch
import torchvision
import numpy as np
import av
sys.path.insert(0, "../")
import io
import base64
from IPython.display import HTML
# In[2]:
from pytorch_implementation_new_way import WebMDataset
from pytorch_implementation_new_way import MultiColumn, Model
from pytorch_implementation_new_way import ComposeMix, Scale, Augmentor
from pytorch_implementation_new_way import remove_module_from_checkpoint_state_dict
# In[3]:
config = {
"model_name": "3D_model",
"output_dir": "D:/capstone-project-2-webapp/flask-server/data/trained_models/",
"input_mode": "av",
"data_folder": "D:/capstone-project-2-webapp/flask-server/data/videos/",
"json_data_val": "D:/capstone-project-2-webapp/flask-server/data/files/validation_data.json",
"json_file_labels": "D:/capstone-project-2-webapp/flask-server/data/files/labels.json",
"num_workers": 5,
"num_classes": 4,
"batch_size": 30,
"clip_size": 72,
"nclips_train": 1,
"nclips_val": 1,
"upscale_factor_train": 1.4,
"upscale_factor_eval": 1.0,
"step_size_train": 1,
"step_size_val": 1,
"lr": 0.008,
"last_lr": 0.00001,
"momentum": 0.9,
"weight_decay": 0.00001,
"num_epochs": -1,
"print_freq": 100,
"conv_model": "D:/something-something-project/smth-smth-v2-baseline-with-models/trained_models/pretrained.model3D_1",
"input_spatial_size": 84,
"column_units": 512,
"save_features": True
}
class VideoFolder(torch.utils.data.Dataset):
def __init__(self, root, json_file_input, json_file_labels, clip_size,
nclips, step_size, is_val, transform_pre=None, transform_post=None,
augmentation_mappings_json=None, augmentation_types_todo=None,
get_item_id=False, is_test=False):
self.dataset_object = WebMDataset(json_file_input, json_file_labels,
root, is_test=is_test)
self.dataset_object = WebMDataset(json_file_input, json_file_labels,
root, is_test=is_test)
self.json_data = self.dataset_object.json_data
self.classes = self.dataset_object.classes
self.classes_dict = self.dataset_object.classes_dict
self.root = root
self.transform_pre = transform_pre
self.transform_post = transform_post
self.augmentor = Augmentor(augmentation_mappings_json,
augmentation_types_todo)
self.clip_size = clip_size
self.nclips = nclips
self.step_size = step_size
self.is_val = is_val
self.get_item_id = get_item_id
def get_video_id_data(self, video_id):
for data in self.json_data:
if str(video_id) == data.id:
return data
def __getitem__(self, video_id):
"""
[!] FPS jittering doesn't work with AV dataloader as of now
"""
item = self.get_video_id_data(video_id)
# Open video file
reader = av.open(item.path)
try:
imgs = []
imgs = [f.to_rgb().to_nd_array() for f in reader.decode(video=0)]
except (RuntimeError, ZeroDivisionError) as exception:
print('{}: WEBM reader cannot open {}. Empty '
'list returned.'.format(type(exception).__name__, item.path))
imgs = self.transform_pre(imgs)
imgs, label = self.augmentor(imgs, item.label)
imgs = self.transform_post(imgs)
num_frames = len(imgs)
target_idx = self.classes_dict[label]
if self.nclips > -1:
num_frames_necessary = self.clip_size * self.nclips * self.step_size
else:
num_frames_necessary = num_frames
offset = 0
if num_frames_necessary < num_frames:
# If there are more frames, then sample starting offset.
diff = (num_frames - num_frames_necessary)
# temporal augmentation
if not self.is_val:
offset = np.random.randint(0, diff)
imgs = imgs[offset: num_frames_necessary + offset: self.step_size]
if len(imgs) < (self.clip_size * self.nclips):
imgs.extend([imgs[-1]] *
((self.clip_size * self.nclips) - len(imgs)))
# format data to torch
data = torch.stack(imgs)
data = data.permute(1, 0, 2, 3)
if self.get_item_id:
return (data, target_idx, item.id)
else:
return (data, target_idx)
def __len__(self):
return len(self.json_data)
# In[40]:
# path_to_vid = os.path.join(config["data_folder"], item_id + ".webm")
# video = io.open(path_to_vid, 'r+b').read()
# encoded = base64.b64encode(video)
# HTML(data='''<video alt="test" controls>
# <source src="data:video/mp4;base64,{0}" type="video/mp4" />
# </video>'''.format(encoded.decode('ascii')))
def predict(vid_id):
# set column model
# column_cnn_def = importlib.import_module("{}".format(config['conv_model']))
model_name = config["model_name"]
print("=> Name of the model -- {}".format(model_name))
# checkpoint path to a trained model
checkpoint_path = os.path.join("../", config["output_dir"], config["model_name"], "model_best.pth.tar")
print("=> Checkpoint path --> {}".format(checkpoint_path))
# In[5]:
model = MultiColumn(config['num_classes'], Model, int(config["column_units"]))
model.eval();
# In[6]:
print("=> loading checkpoint")
checkpoint = torch.load(checkpoint_path)
checkpoint['state_dict'] = remove_module_from_checkpoint_state_dict(
checkpoint['state_dict'])
model.load_state_dict(checkpoint['state_dict'])
print("=> loaded checkpoint '{}' (epoch {})"
.format(checkpoint_path, checkpoint['epoch']))
# In[7]:
import json
# Center crop videos during evaluation
transform_eval_pre = ComposeMix([
[Scale(config['input_spatial_size']), "img"],
[torchvision.transforms.ToPILImage(), "img"],
[torchvision.transforms.CenterCrop(config['input_spatial_size']), "img"]
])
transform_post = ComposeMix([
[torchvision.transforms.ToTensor(), "img"],
[torchvision.transforms.Normalize(
mean=[0.485, 0.456, 0.406], # default values for imagenet
std=[0.229, 0.224, 0.225]), "img"]
])
val_data = VideoFolder(root=config['data_folder'],
json_file_input=config['json_data_val'],
json_file_labels=config['json_file_labels'],
clip_size=config['clip_size'],
nclips=config['nclips_val'],
step_size=config['step_size_val'],
is_val=True,
transform_pre=transform_eval_pre,
transform_post=transform_post,
get_item_id=True,
)
dict_two_way = val_data.classes_dict
# In[8]:
len(val_data)
# In[9]:
dict_two_way
# In[10]:
type(val_data)
# In[11]:
selected_indx = np.random.randint(len(val_data))
selected_indx = 48
print(selected_indx)
input_data, target, item_id = val_data[vid_id]
input_data = input_data.unsqueeze(0)
print("Id of the video sample = {}".format(item_id))
print("True label --> {} ({})".format(target, dict_two_way[target]))
# In[13]:
if config['nclips_val'] > 1:
input_var = list(input_data.split(config['clip_size'], 2))
for idx, inp in enumerate(input_var):
input_var[idx] = torch.autograd.Variable(inp)
else:
input_var = [torch.autograd.Variable(input_data)]
# In[14]:
output = model(input_var).squeeze(0)
output = torch.nn.functional.softmax(output, dim=0)
print(output)
# In[37]:
# compute top5 predictions
pred_prob, pred_top5 = output.data.topk(4)
pred_prob = pred_prob.numpy()
pred_top5 = pred_top5.numpy()
# In[38]:
print(pred_prob)
print(pred_top5)
# In[39]:
print("Id of the video sample = {}".format(item_id))
print("True label --> {} ({})".format(target, dict_two_way[target]))
print("\nTop-4 Predictions:")
for i, pred in enumerate(pred_top5):
print("Top {} :== {}. Prob := {:.2f}%".format(i + 1, dict_two_way[pred], pred_prob[i] * 100))
predict(217421)