Skip to content

Commit e1e7fd3

Browse files
author
Tomasz Stanislawski
committed
make YOLO work with pyAIO
1 parent 0071634 commit e1e7fd3

2 files changed

Lines changed: 30 additions & 11 deletions

File tree

computer_vision/object_detection/yolo_v8/run.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,30 +97,40 @@ def run_pytorch_fp(model_path, batch_size, num_runs, timeout, images_path, anno_
9797
# Ultralytics sets it to True by default. This way we suppress the logging by default while still allowing the user
9898
# to set it to True if needed
9999
from utils.pytorch import PyTorchRunner
100-
from ultralytics.utils.nms import non_max_suppression
101100

102101
def run_single_pass(pytorch_runner, coco):
103102
shape = (640, 640)
104-
inp = torch.stack(coco.get_input_array(shape))
105-
output = pytorch_runner.run(batch_size, inp)
106-
output = non_max_suppression(output)
103+
dset = coco.get_input_array(shape)
104+
outputs = []
105+
for inp in dset:
106+
output, *_ = pytorch_runner.run(1, inp)
107+
outputs.append(output)
108+
assert len(outputs) == batch_size
107109

108110
for i in range(batch_size):
109-
for d in range(output[i].shape[0]):
111+
for b in range(len(outputs[i].boxes)):
112+
bbox = outputs[i].boxes.xywh[b]
113+
cls = int(outputs[i].boxes.cls[b])
110114
coco.submit_bbox_prediction(
111115
i,
112-
coco.convert_bbox_to_coco_order(output[i][d][:4].tolist()),
113-
output[i][d][4].item(),
114-
coco.translate_cat_id_to_coco(output[i][d][5].item())
116+
coco.convert_bbox_to_coco_order(bbox),
117+
cls,
118+
coco.translate_cat_id_to_coco(cls)
115119
)
116120

117121
dataset = COCODataset(batch_size, "RGB", "COCO_val2014_000000000000", images_path,
118122
anno_path, pre_processing="PyTorch_objdet", sort_ascending=True, order="NCHW")
119123

120124
from ultralytics import YOLO
125+
import numpy as np
121126
model = YOLO(model_path)
122127

123-
runner = PyTorchRunner(model.model,
128+
# make sure that model.predictor exists
129+
dummy_input = np.zeros((640, 640, 3), dtype=np.uint8)
130+
model.predict(dummy_input)
131+
assert model.predictor is not None
132+
133+
runner = PyTorchRunner(model,
124134
disable_jit_freeze=disable_jit_freeze,
125135
example_inputs=torch.stack(dataset.get_input_array((640, 640))))
126136

utils/pytorch.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,17 @@ def __init__(self,
7272
# More natural comparison to version.parse("2.0") returns False for 2.0.0a0+git07156c4.dev, which is
7373
# wrong. There was never a PyTorch 1.14, so this comparison acts like comparing to 2.0, but works
7474
# correctly for such edge cases.
75-
self._frozen_script = torch.compile(self._model, backend="aio-2" if AIO else "inductor",
76-
options={"modelname": self._model._get_name()} if AIO else {})
75+
backend = "aio-2" if AIO else "inductor"
76+
options = {"modelname": self._model._get_name()} if AIO else {}
77+
78+
if self._model._get_name() == "YOLO":
79+
# use compiled model for a prodictor model
80+
frozen_script = torch.compile(self._model.model, backend=backend, options=options)
81+
self._model.predictor.model.model = frozen_script
82+
self._frozen_script = self._model
83+
else:
84+
self._frozen_script = torch.compile(self._model, backend=backend, options=options)
85+
7786
elif os.environ.get("TORCH_COMPILE") == "1" and not version.parse(
7887
pkg_resources.get_distribution("torch").version) >= version.parse("1.14"):
7988
utils.print_goodbye_message_and_die(

0 commit comments

Comments
 (0)