Hi to all, i’m currently working with a custom unet model for semantic segmentation in a raspberry pi 5 with an AI HAT + (HAILO 8). The model has to segment 4 classes in an industrial image. I have the .onnx trained that works fine and also I managed to get de .har quantized working pretty well too (just with an acceptable error margin) using the hailo dataflow compiler. When I compile the .hef file and do the inference in my raspberry pi with the ai hat, the results are totally wrong and I can’t understand why.
I’m doing exactly the same preprocess method to prepare the image for .har quantized and for the .hef. In both cases i’m doing the normalization outside the .alls. I think that the .hef has something wrong or i’m not understanding correctly how to work with the output of the inference method.
This is the preprocess method i’m using:
img_pil = Image.open(image_path).convert("RGB")
img_original = np.array(img_pil)
img_resized = img_pil.resize(target_size, Image.BILINEAR)
img_resized_np = np.array(img_resized, dtype=np.float32) # (H, W, 3), 0-255
img_float = img_resized_np / 255.0 # (H, W, 3), [0,1]
mean = np.array([0.485, 0.456, 0.406], dtype=np.float32)
std = np.array([0.229, 0.224, 0.225], dtype=np.float32)
img_normalized = (img_float - mean) / std # (H, W, 3)
return img_normalized, img_original
This is the postprocess method i’m using:
output_dict = result.results[0]
tensor_uint8 = output_dict['data'] # Shape: (1, 1048576, 4)
scale = output_dict['quantization']['scale'][0]
zero = output_dict['quantization']['zero'][0]
tensor_float = scale * (tensor_uint8.astype(np.float32) - zero)
tensor_reshaped = tensor_float.reshape(1024, 1024, 4)
tensor_normalized = np.zeros_like(tensor_reshaped)
for c in range(4):
channel = tensor_reshaped[:, :, c]
mean = channel.mean()
std = channel.std()
if std > 0:
tensor_normalized[:, :, c] = (channel - mean) / std
else:
tensor_normalized[:, :, c] = channel - mean
mask_zscore = np.argmax(tensor_normalized, axis=-1)
This is the output of the inference method:
[{'data': array([[[112, 94, 86, 83],
[120, 97, 82, 71],
[121, 95, 81, 65],
...,
[133, 102, 82, 67],
[130, 101, 83, 69],
[119, 106, 90, 73]]], shape=(1, 1048576, 4), dtype=uint8), 'id': 0, 'name': 'unet/conv31', 'quantization': {'axis': -1, 'scale': [0.1700144112110138], 'zero': [99]}, 'shape': [1, 1048576, 4], 'size': 4194304, 'type': 'DG_UINT8'}]
Any help is appreciated.
Thanks in advance!