-
Notifications
You must be signed in to change notification settings - Fork 6
/
examine_reducto.py
151 lines (129 loc) · 3.89 KB
/
examine_reducto.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
import argparse
import json
import logging
import os
import pickle
import subprocess
from pathlib import Path
from pdb import set_trace
import coloredlogs
import enlighten
import networkx as nx
import torch
import torchvision.transforms as T
import yaml
from PIL import Image
from torchvision import io
from dnn.dnn_factory import DNN_Factory
from utils.bbox_utils import jaccard
from utils.results_utils import read_results, write_results
from utils.video_utils import read_bandwidth
def main(args):
logger = logging.getLogger("examine")
handler = logging.NullHandler()
logger.addHandler(handler)
app = DNN_Factory().get_model(args.app)
fids = json.load(open(args.json, "r"))
# inference first
Path(f"{args.input}.source.pngs").mkdir(exist_ok=True)
Path(f"{args.input}.pngs").mkdir(exist_ok=True)
new_fid = 0
for new_fid, fid in enumerate(fids):
print(fid)
subprocess.run(
[
"cp",
f"{args.source}/%010d.png" % fid,
f"{args.input}.source.pngs/%010d.png" % new_fid,
]
)
subprocess.run(
[
"ffmpeg",
"-y",
"-i",
f"{args.input}.source.pngs/%010d.png",
"-start_number",
"0",
"-qp",
"28",
f"{args.input}",
]
)
subprocess.run(
[
"ffmpeg",
"-y",
"-i",
f"{args.input}",
"-start_number",
"0",
f"{args.input}.pngs/%010d.png",
]
)
for _ in range(1):
ground_truth_results = read_results(args.ground_truth, app.name, logger)
video_results = {}
for new_fid, fid in enumerate(fids):
print(fid)
image = Image.open(f"{args.input}.pngs/%010d.png" % new_fid)
image = T.ToTensor()(image)[None, :, :, :].cuda()
video_results[fid] = app.inference(image, detach=True)
last_fid = 0
for fid in ground_truth_results.keys():
if fid in video_results:
last_fid = fid
print(last_fid)
else:
video_results[fid] = video_results[last_fid]
metrics = app.calc_accuracy(video_results, ground_truth_results, args)
res = {
"application": app.name,
"video_name": args.input,
"bw": os.path.getsize(args.input),
"ground_truth_name": args.ground_truth,
}
res.update(metrics)
with open(args.stats, "a") as f:
f.write(yaml.dump([res]))
if __name__ == "__main__":
# set the format of the logger
coloredlogs.install(
fmt="%(asctime)s [%(levelname)s] %(name)s:%(funcName)s[%(lineno)s] -- %(message)s",
level="INFO",
)
parser = argparse.ArgumentParser()
parser.add_argument("--stats", type=str, default="stats")
parser.add_argument("--json", type=str, required=True)
parser.add_argument(
"-g",
"--ground_truth",
type=str,
help="The ground-truth video name.",
required=True,
)
parser.add_argument("--source", type=str, required=True)
parser.add_argument(
"--confidence_threshold",
type=float,
help="The confidence score threshold for calculating accuracy.",
default=0.7,
)
parser.add_argument(
"--app", type=str, help="The name of the model.", required=True,
)
parser.add_argument(
"--gt_confidence_threshold",
type=float,
help="The confidence score threshold for calculating accuracy.",
default=0.7,
)
parser.add_argument(
"--iou_threshold",
type=float,
help="The IoU threshold for calculating accuracy in object detection.",
default=0.5,
)
parser.add_argument("--input", type=str, required=True)
args = parser.parse_args()
main(args)