-
Notifications
You must be signed in to change notification settings - Fork 32
/
eval.py
183 lines (153 loc) · 5.61 KB
/
eval.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
import argparse
import torch
import os
from evaluator.ucf_jhmdb_evaluator import UCF_JHMDB_Evaluator
from evaluator.ava_evaluator import AVA_Evaluator
from dataset.transforms import BaseTransform
from utils.misc import load_weight, CollateFunc
from config import build_dataset_config, build_model_config
from models import build_model
def parse_args():
parser = argparse.ArgumentParser(description='YOWOv2')
# basic
parser.add_argument('-bs', '--batch_size', default=8, type=int,
help='test batch size')
parser.add_argument('-size', '--img_size', default=224, type=int,
help='the size of input frame')
parser.add_argument('--cuda', action='store_true', default=False,
help='use cuda.')
parser.add_argument('--save_path', default='./evaluator/eval_results/',
type=str, help='Trained state_dict file path to open')
# dataset
parser.add_argument('-d', '--dataset', default='ucf24',
help='ucf24, jhmdb, ava_v2.2.')
parser.add_argument('--root', default='/mnt/share/ssd2/dataset/STAD/',
help='data root')
# eval
parser.add_argument('--cal_frame_mAP', action='store_true', default=False,
help='calculate frame mAP.')
parser.add_argument('--cal_video_mAP', action='store_true', default=False,
help='calculate video mAP.')
# model
parser.add_argument('-v', '--version', default='yowo_v2_large', type=str,
help='build YOWOv2')
parser.add_argument('--weight', default=None,
type=str, help='Trained state_dict file path to open')
parser.add_argument('-ct', '--conf_thresh', default=0.1, type=float,
help='confidence threshold. We suggest 0.005 for UCF24 and 0.1 for AVA.')
parser.add_argument('-nt', '--nms_thresh', default=0.5, type=float,
help='NMS threshold. We suggest 0.5 for UCF24 and AVA.')
parser.add_argument('--topk', default=40, type=int,
help='topk prediction candidates.')
parser.add_argument('-K', '--len_clip', default=16, type=int,
help='video clip length.')
parser.add_argument('-m', '--memory', action="store_true", default=False,
help="memory propagate.")
return parser.parse_args()
def ucf_jhmdb_eval(args, d_cfg, model, transform, collate_fn):
data_dir = os.path.join(args.root, 'ucf24')
if args.cal_frame_mAP:
# Frame mAP evaluator
evaluator = UCF_JHMDB_Evaluator(
data_root=data_dir,
dataset=args.dataset,
model_name=args.version,
metric='fmap',
img_size=args.img_size,
len_clip=args.len_clip,
batch_size=args.batch_size,
conf_thresh=0.01,
iou_thresh=0.5,
transform=transform,
collate_fn=collate_fn,
gt_folder=d_cfg['gt_folder'],
save_path=args.save_path
)
# evaluate
evaluator.evaluate_frame_map(model, show_pr_curve=True)
elif args.cal_video_mAP:
# Video mAP evaluator
evaluator = UCF_JHMDB_Evaluator(
data_root=data_dir,
dataset=args.dataset,
model_name=args.version,
metric='vmap',
img_size=args.img_size,
len_clip=args.len_clip,
batch_size=args.batch_size,
conf_thresh=0.01,
iou_thresh=0.5,
transform=transform,
collate_fn=collate_fn,
gt_folder=d_cfg['gt_folder'],
save_path=args.save_path
)
# evaluate
evaluator.evaluate_video_map(model)
def ava_eval(args, d_cfg, model, transform, collate_fn):
data_dir = os.path.join(args.root, 'AVA_Dataset')
evaluator = AVA_Evaluator(
d_cfg=d_cfg,
data_root=data_dir,
img_size=args.img_size,
len_clip=args.len_clip,
sampling_rate=d_cfg['sampling_rate'],
batch_size=args.batch_size,
transform=transform,
collate_fn=collate_fn,
full_test_on_val=False,
version='v2.2')
mAP = evaluator.evaluate_frame_map(model)
if __name__ == '__main__':
args = parse_args()
# dataset
if args.dataset == 'ucf24':
num_classes = 24
elif args.dataset == 'jhmdb':
num_classes = 21
elif args.dataset == 'ava_v2.2':
num_classes = 80
else:
print('unknow dataset.')
exit(0)
# cuda
if args.cuda:
print('use cuda')
device = torch.device("cuda")
else:
device = torch.device("cpu")
# config
d_cfg = build_dataset_config(args)
m_cfg = build_model_config(args)
# build model
model, _ = build_model(
args=args,
d_cfg=d_cfg,
m_cfg=m_cfg,
device=device,
num_classes=num_classes,
trainable=False
)
# load trained weight
model = load_weight(model=model, path_to_ckpt=args.weight)
# to eval
model = model.to(device).eval()
# transform
basetransform = BaseTransform(img_size=args.img_size)
# run
if args.dataset in ['ucf24', 'jhmdb21']:
ucf_jhmdb_eval(
args=args,
d_cfg=d_cfg,
model=model,
transform=basetransform,
collate_fn=CollateFunc()
)
elif args.dataset == 'ava_v2.2':
ava_eval(
args=args,
d_cfg=d_cfg,
model=model,
transform=basetransform,
collate_fn=CollateFunc()
)