-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sd.py
332 lines (281 loc) · 11.2 KB
/
test_sd.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import argparse
import logging
import os
import warnings
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import torch
import torch.backends.cudnn as cudnn
import torch.nn.functional as F
import torchvision
import wandb
from PIL import ImageFile
from sklearn.metrics import confusion_matrix
from torch.utils.data import DataLoader
from tqdm import tqdm
import utils as utils
from data import dataset_EgoGesture, dataset_NvGesture
from models.models_SD_actionnet import TSN
from models.spatial_transforms import *
from models.temporal_transforms import *
ImageFile.LOAD_TRUNCATED_IMAGES = True
warnings.filterwarnings("ignore")
os.environ['WANDB_MODE'] = 'disabled'
def parse_opts():
parser = argparse.ArgumentParser()
parser.add_argument('--cuda_id', type=str, default='0')
parser.add_argument('--checkpoint_path', type=str, default='')
parser.add_argument('--note', type=str, default='')
parser.add_argument('--single_clip_test', action='store_true')
parser.add_argument('--multiple_clip_test', action='store_true')
# args for dataloader
parser.add_argument('--batch_size', type=int, default=1)
parser.add_argument('--num_workers', type=int, default=4)
parser.add_argument('--clip_len', type=int, default=8)
# args for preprocessing
parser.add_argument('--shift_div', default=8, type=int)
parser.add_argument('--is_shift', default=True, type=bool)
parser.add_argument('--base_model', default='resnet50', type=str)
parser.add_argument('--dataset', default='EgoGesture', type=str)
# args for testing
parser.add_argument('--test_crops', type=int, default=1)
parser.add_argument('--scale_size', type=int, default=256)
parser.add_argument('--crop_size', type=int, default=224)
parser.add_argument('--clip_num', type=int, default=10)
args = parser.parse_args()
return args
args = parse_opts()
params = dict()
if args.dataset == 'EgoGesture':
params['num_classes'] = 83
# params['num_classes'] = 10
elif args.dataset == 'NvGesture':
params['num_classes'] = 25
annot_path = 'data/{}_annotation'.format(args.dataset)
os.environ['CUDA_VISIBLE_DEVICES'] = args.cuda_id
device = 'cuda:0'
y_true, c1_pred, c2_pred, c3_pred, c4_pred = [], [], [], [], []
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].reshape(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
@torch.no_grad()
def inference(model, val_dataloader):
top1 = AverageMeter()
middle1_top1 = AverageMeter()
middle2_top1 = AverageMeter()
middle3_top1 = AverageMeter()
model.eval()
for step, inputs in enumerate(tqdm(val_dataloader)):
rgb, labels = inputs[0], inputs[1]
rgb = rgb.to(device, non_blocking=True).float()
labels = labels.to(device, non_blocking=True).long()
nb, n_clip, nt, nc, h, w = rgb.size()
# n_clip * nb (1) * crops, T, C, H, W
rgb = rgb.view(-1, nt//args.test_crops, nc, h, w)
outputs, middle_output1, middle_output2, middle_output3, \
_, _, _, _ = model(rgb)
outputs = outputs.view(nb, n_clip*args.test_crops, -1)
outputs = F.softmax(outputs, 2)
outputs = outputs.data.mean(1)
middle_output1 = middle_output1.view(nb, n_clip*args.test_crops, -1)
middle_output1 = F.softmax(middle_output1, 2)
middle_output1 = middle_output1.data.mean(1)
middle_output2 = middle_output2.view(nb, n_clip*args.test_crops, -1)
middle_output2 = F.softmax(middle_output2, 2)
middle_output2 = middle_output2.data.mean(1)
middle_output3 = middle_output3.view(nb, n_clip*args.test_crops, -1)
middle_output3 = F.softmax(middle_output3, 2)
middle_output3 = middle_output3.data.mean(1)
# measure accuracy and record loss
prec1 = accuracy(outputs, labels, topk=(1, ))
top1.update(prec1[0].item(), labels.size(0))
middle1_prec1 = accuracy(middle_output1, labels, topk=(1,))
middle1_top1.update(middle1_prec1[0].item(), labels.size(0))
middle2_prec1 = accuracy(middle_output2, labels, topk=(1,))
middle2_top1.update(middle2_prec1[0].item(), labels.size(0))
middle3_prec1 = accuracy(middle_output3, labels, topk=(1,))
middle3_top1.update(middle3_prec1[0].item(), labels.size(0))
_, pred = outputs.topk(1, 1, True, True)
_, middle1_pred = middle_output1.topk(1, 1, True, True)
_, middle2_pred = middle_output2.topk(1, 1, True, True)
_, middle3_pred = middle_output3.topk(1, 1, True, True)
c1_pred.extend(middle1_pred.view(-1).detach().cpu().numpy())
c2_pred.extend(middle2_pred.view(-1).detach().cpu().numpy())
c3_pred.extend(middle3_pred.view(-1).detach().cpu().numpy())
c4_pred.extend(pred.view(-1).detach().cpu().numpy())
y_true.extend(labels.view(-1).detach().cpu().numpy())
logging.info('Top-1: {top1_acc:.2f}, '
'Middle1@1: {middle1_top1:.2f}, '
'Middle2@1: {middle2_top1:.2f}, '
'Middle3@1: {middle3_top1:.2f} '
.format(top1_acc=top1.avg,
middle1_top1=middle1_top1.avg,
middle2_top1=middle2_top1.avg,
middle3_top1=middle3_top1.avg
)
)
wandb.log(
{
'Top-1': top1.avg,
'Middle1_top1': middle1_top1.avg,
'Middle2_top1': middle2_top1.avg,
'Middle3_top1': middle3_top1.avg,
}
)
def plot_cm(y_true, y_pred, name):
cf_matrix = confusion_matrix(y_true, y_pred)
per_cls_acc = cf_matrix.diagonal()/cf_matrix.sum(axis=0)
class_names = [
'Scroll_right',
'Scroll_left',
'Scroll_down',
'Scroll_up',
'Zoom_in',
'Zoom_out',
'Rotate_clockwise',
'Rotate_counterclockwise',
'Pull',
'Push'
]
print(class_names)
print(per_cls_acc)
print("Plot confusion matrix")
df_cm = pd.DataFrame(cf_matrix, class_names, class_names)
plt.figure(figsize=(6.4, 4.8), layout='tight')
ax = sns.heatmap(df_cm, annot=True, fmt="d")
ax.set_xticks(np.arange(len(class_names)) + 0.5, labels=class_names)
ax.set_yticks(np.arange(len(class_names)) + 0.5, labels=class_names)
ax.set_xlabel('Predicted')
ax.set_ylabel('Ground-Truth')
plt.setp(ax.get_xticklabels(), rotation=45,
ha="right", rotation_mode="anchor")
plt.savefig(exp_path + f"/{name}_cm.png")
if __name__ == '__main__':
seed = 1
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
random.seed(seed)
np.random.seed(seed)
exp_path = '/'.join(args.checkpoint_path.split('/')[:-1])
if not os.path.exists(exp_path):
os.makedirs(exp_path)
logger_file = os.path.join(exp_path, 'test.log')
handlers = [logging.FileHandler(logger_file, mode='a'), # ! mode='w' will overwrite the log file
logging.StreamHandler()]
logging.basicConfig(level=logging.INFO,
datefmt='%m-%d-%y %H:%M',
format='%(asctime)s:%(message)s',
handlers=handlers)
logging.info(args.note + '_test_crops' + str(args.test_crops) + '_clip_num' + str(
args.clip_num) + '_scale_size' + str(args.scale_size) + '_crop_size' + str(args.crop_size))
wandb.init(
project=args.dataset + '_Inference',
name=args.checkpoint_path.split('/')[-2],
notes=args.note,
config=args
)
if args.dataset == 'EgoGesture':
cropping = torchvision.transforms.Compose([
GroupScale([args.crop_size, args.crop_size])
])
else:
if args.test_crops == 1:
cropping = torchvision.transforms.Compose([
GroupScale(args.scale_size),
GroupCenterCrop(args.crop_size)
])
elif args.test_crops == 3:
cropping = torchvision.transforms.Compose([
GroupFullResSample(args.crop_size, args.scale_size, flip=False)
])
elif args.test_crops == 5:
cropping = torchvision.transforms.Compose([
GroupOverSample(args.crop_size, args.scale_size, flip=False)
])
input_mean = [.485, .456, .406]
input_std = [.229, .224, .225]
normalize = GroupNormalize(input_mean, input_std)
spatial_transform = torchvision.transforms.Compose([
cropping,
Stack(roll=(args.base_model in ['BNInception', 'InceptionV3'])),
ToTorchFormatTensor(div=(args.base_model not in [
'BNInception', 'InceptionV3'])),
normalize
])
# for mulitple clip test, use random sampling;
# for single clip test, use middle sampling
if args.single_clip_test:
temporal_transform = torchvision.transforms.Compose([
TemporalUniformCrop_val(args.clip_len)
])
if args.multiple_clip_test:
temporal_transform = torchvision.transforms.Compose([
TemporalUniformCrop_train(args.clip_len)
])
cudnn.benchmark = True
model = TSN(params['num_classes'], args.clip_len, 'RGB',
is_shift=args.is_shift,
base_model=args.base_model,
shift_div=args.shift_div,
img_feature_dim=args.crop_size,
consensus_type='avg',
fc_lr5=True)
checkpoint = torch.load(args.checkpoint_path)
model.load_state_dict(checkpoint['state_dict'], strict=False)
model = model.to(device)
if args.dataset == 'EgoGesture':
val_dataset = dataset_EgoGesture.dataset_video_inference(
annot_path,
'test',
# 'test_10cls',
clip_num=args.clip_num,
spatial_transform=spatial_transform,
temporal_transform=temporal_transform,
)
val_dataloader = DataLoader(
val_dataset,
batch_size=args.batch_size,
num_workers=args.num_workers
)
elif args.dataset == 'NvGesture':
val_dataset = dataset_NvGesture.dataset_video_inference(
annot_path,
'test',
clip_num=args.clip_num,
spatial_transform=spatial_transform,
temporal_transform=temporal_transform,
)
val_dataloader = DataLoader(
val_dataset,
batch_size=args.batch_size,
num_workers=args.num_workers
)
inference(model, val_dataloader)
plot_cm(y_true, c1_pred, 'c1')
plot_cm(y_true, c2_pred, 'c2')
plot_cm(y_true, c3_pred, 'c3')
plot_cm(y_true, c4_pred, 'c4')