forked from robin-maillot/yolov5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mojo_test.py
277 lines (241 loc) · 9.6 KB
/
mojo_test.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
import argparse
import sys
from pathlib import Path
import cv2
import tqdm
import wandb
import numpy as np
import torch
from aisa_utils.dl_utils.utils import (
plot_object_count_difference_ridgeline,
make_video_results,
plot_object_count_difference_line,
)
from utils.general import xyxy2xywhn, scale_coords
from mojo_val import compute_predictions_and_labels, log_plot_as_wandb_artifact
from aisa_utils.dl_utils.plots import plot_predictions_and_labels_crops, compute_predictions_and_labels_false_pos, plot_dynamic_and_static_preds
FILE = Path(__file__).absolute()
sys.path.append(FILE.parents[0].as_posix()) # add yolov5/ to path
from models.experimental import attempt_load
from utils.general import (
check_dataset,
check_file,
check_img_size,
non_max_suppression,
set_logging,
increment_path,
colorstr,
)
from utils.torch_utils import select_device
import val
def get_images_and_labels(data):
image_paths = list(Path(data).glob("*.jpg")) + list(Path(data).glob("*.png"))
images = []
labels = []
for image_path in image_paths:
image = cv2.imread(str(image_path), 0)
images.append(image)
label_path = (
image_path.parents[2]
/ "labels"
/ image_path.parent.stem
/ f"{image_path.stem}.txt"
)
if label_path.is_file():
label = []
with label_path.open("r") as f:
for line in f.readlines():
line = line.split()
line = [int(line[0])] + list(map(float, line[1:]))
label.append(line)
labels.append(label)
else:
raise Exception(f"Missing label {label_path}")
return images, labels
@torch.no_grad()
def mojo_test(
data,
weights=None, # model.pt path(s)
batch_size=32, # batch size
imgsz=640, # inference size (pixels)
device="", # cuda device, i.e. 0 or 0,1,2,3 or cpu
save_txt=False, # save results to *.txt
project="runs/val", # save to project/name
name="exp", # save to project/name
exist_ok=False, # existing project/name ok, do not increment
entity=None,
test_video_root=None,
):
device = select_device(device, batch_size=batch_size)
print(f"data:{data}")
data_dict = check_dataset(data)
print(f"data_dict:{data_dict}")
# Trainloader
images, labels = get_images_and_labels(data_dict["test"])
# Directories
save_dir = increment_path(Path(project) / name, exist_ok=exist_ok) # increment run
(save_dir / "labels" if save_txt else save_dir).mkdir(
parents=True, exist_ok=True
) # make dir
run_id = torch.load(weights[0]).get("wandb_id")
wandb_run = wandb.init(
id=run_id, project=project, entity=entity, resume="allow", allow_val_change=True,
)
results, maps, t, extra_stats = val.run(
data,
weights=weights, # model.pt path(s)
batch_size=batch_size, # batch size
imgsz=imgsz, # inference size (pixels)
conf_thres=0.001, # confidence threshold
iou_thres=0.6, # NMS IoU threshold
task="test", # train, val, test, speed or study
)
total_inference_time = np.sum(t)
print(f"total_inference_time={total_inference_time:.1f}ms")
wandb_run.log({f"mojo_test/test_metrics/mp": results[0]})
wandb_run.log({f"mojo_test/test_metrics/mr": results[1]})
wandb_run.log({f"mojo_test/test_metrics/map50": results[2]})
wandb_run.log({f"mojo_test/test_metrics/map": results[3]})
wandb_run.log({f"mojo_test/test_metrics/inference_time": total_inference_time})
# Load model
model = attempt_load(weights, map_location=device) # load FP32 model
gs = max(int(model.stride.max()), 32) # grid size (max stride)
imgsz = check_img_size(imgsz, s=gs) # check image size
# Half
half = device.type != "cpu" # half precision only supported on CUDA
if half:
model.half()
def video_prediction_function(
frame_array, iou_thres_nms=0.45, conf_thres_nms=0.001
):
n_frames = len(frame_array)
preds = []
for i in range(0, n_frames, batch_size):
frames = []
for frame in frame_array[i : min(i + batch_size, n_frames)]:
from utils.datasets import letterbox
img = letterbox(frame, new_shape=(imgsz, imgsz))[0]
img = np.array([img, img, img])
img = np.ascontiguousarray(img)
frames.append(img)
frames = np.array(frames)
# Convert img to torch
img = torch.from_numpy(frames).to(device)
img = (
img.half() if device.type != "cpu" else img.float()
) # uint8 to fp16/32
img /= 255.0 # 0 - 255 to 0.0 - 1.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
# Inference
pred = model(img, augment=False)[0]
# Apply NMS
pred = non_max_suppression(
pred, iou_thres=iou_thres_nms, conf_thres=conf_thres_nms
)
_ = []
for j, det in enumerate(pred):
det[:, :4] = scale_coords(
img.shape[2:], det[:, :4], frame_array[i + j].shape
)
det = det.cpu().numpy()
det[:, :4] = xyxy2xywhn(
det[:, :4],
w=frame_array[i + j].shape[1],
h=frame_array[i + j].shape[0],
)
_.append(det)
preds += _
return preds
workspace_plots, artifacts_plots = dict(), dict()
preds_iou_thres = dict()
for iout in [0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65]:
preds_iou_thres[iout] = video_prediction_function(images, iou_thres_nms=iout)
fig_line = plot_object_count_difference_line(labels, preds_iou_thres)
fig, suggested_threshold = plot_object_count_difference_ridgeline(labels, preds_iou_thres[0.45])
print(f"suggested_threshold={suggested_threshold}")
workspace_plots["object_count_difference"] = fig
workspace_plots["object_count_difference_continuous"] = fig_line
# Extract prediction & labels match from validation extra stats
predn, preds_matched, labelsn, labels_matched, images_paths = compute_predictions_and_labels(
extra_stats,
threshold=suggested_threshold
)
static_plotly_dict, dynamic_wandb_list = plot_dynamic_and_static_preds(
predn,
preds_matched,
labelsn,
labels_matched,
images_paths,
threshold=suggested_threshold,
names={k: v for k, v in enumerate(model.names if hasattr(model, 'names') else model.module.names)}
)
artifacts_plots.update(static_plotly_dict)
for plot_idx, plot in enumerate(dynamic_wandb_list):
workspace_plots[f"Targets and predictions/{images_paths[plot_idx].name}"] = [plot]
# Compute TP, TN, FP, FN and draw crops of lowest and confidence threshold preds
true_pos, true_neg, false_pos, false_neg = compute_predictions_and_labels_false_pos(
predn, preds_matched, labelsn, labels_matched, images_paths, threshold=suggested_threshold
)
artifacts_plots.update(plot_predictions_and_labels_crops(
true_pos, true_neg, false_pos, false_neg, images_paths
))
for fig_name, fig in tqdm.tqdm(workspace_plots.items(), desc="Uploading workspace plots"):
wandb_run.log({f"mojo_test/workspace_plots/{fig_name}": fig})
for fig_name, fig in tqdm.tqdm(artifacts_plots.items(), desc="Uploading artifacts plots"):
log_plot_as_wandb_artifact(wandb_run, fig, fig_name)
if test_video_root is not None:
for video_path in Path(test_video_root).rglob("*.avi"):
output_video_path, jitter_plot = make_video_results(
video_path, lambda x: video_prediction_function(x, suggested_threshold)
)
wandb_run.log(
{
f"mojo_test/extra_videos/{output_video_path.name}": wandb.Video(
str(output_video_path), fps=60, format="mp4"
)
}
)
wandb_run.log(
{f"mojo_test/workspace_plots/{output_video_path.name}_jitter": jitter_plot}
)
return None
def parse_opt():
parser = argparse.ArgumentParser(prog="mojo_test.py")
parser.add_argument(
"--data", type=str, default="data/coco128.yaml", help="dataset.yaml path"
)
parser.add_argument(
"--weights", nargs="+", type=str, default="yolov5s.pt", help="model.pt path(s)"
)
parser.add_argument("--batch-size", type=int, default=32, help="batch size")
parser.add_argument(
"--imgsz",
"--img",
"--img-size",
type=int,
default=640,
help="inference size (pixels)",
)
parser.add_argument(
"--device", default="", help="cuda device, i.e. 0 or 0,1,2,3 or cpu"
)
parser.add_argument("--project", default="runs_test", help="save to project/name")
parser.add_argument("--name", default="exp", help="save to project/name")
parser.add_argument(
"--exist-ok",
action="store_true",
help="existing project/name ok, do not increment",
)
parser.add_argument("--entity", default=None, help="W&B entity")
opt = parser.parse_args()
opt.data = check_file(opt.data) # check file
return opt
def main(opt):
set_logging()
print(colorstr("mojo test: ") + ", ".join(f"{k}={v}" for k, v in vars(opt).items()))
mojo_test(**vars(opt))
if __name__ == "__main__":
# weights = Path(r"D:\Nanovare\dev\yolov5\wandb_mod_tests\exp\weights\best.pt")
opt = parse_opt()
main(opt)