Thanks for sharing all this info, what threw me a bit off was the CUDA error. I’ve went through it, and it seems a bit outdated (we’re almost finished rebuilding all YOLO examples). In the mean time, I’ll share here a simpler version of running YOLOX, that is not dependant on the model-zoo (thus removing heavey packages like pytorch etc.)
Start by importing some relevant packages:
#!/usr/bin/env python3
import numpy as np
import wget
from hailo_platform import (HEF, VDevice, HailoStreamInterface, InferVStreams, ConfigureParams, InputVStreamParams, OutputVStreamParams, FormatType)
from PIL import Image, ImageDraw, ImageFont
import os
import random
In this case, we’re using a pre-compiled HEF for Hailo-8 from the model-zoo, and a sample image from COCO.
hef_path = 'yolox_s_leaky.hef'
input_dir = '/data/coco/images/val2017/'
if not os.path.isfile(hef_path):
wget.download(f'https://hailo-model-zoo.s3.eu-west-2.amazonaws.com/ModelZoo/Compiled/v2.11.0/hailo8/{hef_path}')
Define some supporting methods:
def get_label(class_id):
with open('/data/coco/labels.json','r') as f:
labels = eval(f.read())
return labels[str(class_id)]
def draw_detection(draw, d, c, color, scale_factor_x, scale_factor_y):
"""Draw box and label for 1 detection."""
label = get_label(c)
ymin, xmin, ymax, xmax = d
font = ImageFont.truetype('LiberationSans-Regular.ttf', size=15)
draw.rectangle([(xmin * scale_factor_x, ymin * scale_factor_y), (xmax * scale_factor_x, ymax * scale_factor_y)], outline=color, width=2)
draw.text((xmin * scale_factor_x + 4, ymin * scale_factor_y + 4), label, fill=color, font=font)
return label
def annotate_image(image, results, thr=0.45, dim=640, offset_background=True):
COLORS = np.random.randint(0, 255, size=(90, 3), dtype=np.uint8)
draw = ImageDraw.Draw(image)
oh, ow, _ = np.asarray(img).shape
rh, rw = oh/dim, ow/dim
for idx, class_detections in enumerate(results[list(infer_results.keys())[0]][0]):
if class_detections.shape[0]>0:
color = tuple(int(c) for c in COLORS[idx])
for det in class_detections:
if det[4] > thr:
if offset_background:
label = draw_detection(draw, det[0:4] * dim , idx+1, color, rw, rh)
else:
label = draw_detection(draw, det[0:4] * dim , idx, color, rw, rh)
img = Image.open(input_dir + random.choice(os.listdir(input_dir)))
resized_image = np.asarray(img.resize((640, 640)))
if len(resized_image.shape)==1:
print('Please rerun this cell to get a color image')
This is the main block for executing on the Hailo device (using pyhailort):
hef = HEF(hef_path)
with VDevice() as target:
configure_params = ConfigureParams.create_from_hef(hef, interface=HailoStreamInterface.PCIe)
network_group = target.configure(hef, configure_params)[0]
network_group_params = network_group.create_params()
input_vstream_info = hef.get_input_vstream_infos()[0]
input_vstreams_params = InputVStreamParams.make_from_network_group(network_group, quantized=False, format_type=FormatType.UINT8)
output_vstreams_params = OutputVStreamParams.make_from_network_group(network_group, quantized=False, format_type=FormatType.FLOAT32)
with InferVStreams(network_group, input_vstreams_params, output_vstreams_params) as infer_pipeline:
input_data = {input_vstream_info.name: np.expand_dims(resized_image, axis=0).astype(np.uint8)}
with network_group.activate(network_group_params):
infer_results = infer_pipeline.infer(input_data)
Finally, you can annotate the image:
annotate_image(img, infer_results)