Unable to obtain boxes for a TFLITE Yolov5 model

Issue

I trained a model allowing the detection of ‘+’ characters on an image thanks to Yolov5. I want to use this model in TFLITE. However, when I infer an image in the model, I have trouble interpreting the output.

Here is how I infer in my model :

interpreter = tf.lite.Interpreter("/Users/maximereder/Desktop/best-fp16.tflite")

interpreter.allocate_tensors()

IMAGE_PATH = "/Users/maximereder/Documents/ML/dataset/plus- 
1000x750/train/IMG_1492B2.jpg"
img = cv2.resize(cv2.imread(IMAGE_PATH), (640, 640))
features = img.copy()
np_features = np.array(features, dtype=np.float32)
np_features = np.expand_dims(np_features, axis=0)

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
interpreter.set_tensor(input_details[0]['index'], np_features)

interpreter.invoke()

out_details :

[{'name': 'Identity',
'index': 422,
'shape': array([    1, 25200,    85], dtype=int32),
'shape_signature': array([    1, 25200,    85], dtype=int32),
'dtype': numpy.float32,
'quantization': (0.0, 0),
'quantization_parameters': {'scales': array([], dtype=float32),
'zero_points': array([], dtype=int32),
'quantized_dimension': 0},
'sparsity_parameters': {}}]

When I want to interpret the output of my model:

detection_boxes = interpreter.get_tensor(output_details[0]['index'])
detection_classes = interpreter.get_tensor(output_details[1]['index'])
detection_scores = interpreter.get_tensor(output_details[2]['index'])
num_boxes = interpreter.get_tensor(output_details[3]['index'])

Output of interpreter.get_tensor(output_details[0][‘index’] :

array([[[-5.6185187e-03,  1.3539949e-02,  5.7405889e-02,  4.2354122e-02,
          1.3114554e-04,  9.9999905e-01],
        [-4.4679684e-03,  1.7201375e-02,  6.8576269e-02,  2.5891241e-02,
          3.4220223e-04,  9.9999964e-01],
        [-4.9383980e-03,  1.5453462e-02,  4.2031817e-02,  2.6558569e-02,
          2.5974249e-03,  9.9999815e-01],
        ...,
        [ 9.2678040e-01,  9.2856336e-01,  7.1995622e-01,  5.6132025e-01,
          1.4253161e-14,  9.9999440e-01],
        [ 9.2535079e-01,  9.2647862e-01,  9.4501650e-01,  1.1292257e+00,
          4.9554409e-09,  9.9999994e-01],
        [ 1.0224271e+00,  9.7982901e-01,  2.2890522e+00,  1.1467136e-02,
          1.4553191e-07,  9.9999893e-01]]], dtype=float32)

I get an error : IndexError: list index out of range

I understand the meaning of the error but why is there only one element in my output? How can I interpret it?
I want to get the boxes.
Thank you.

Solution

Please check https://github.com/ultralytics/yolov5/issues/1981

‘shape’: array([ 1, 25200, 85], dtype=int32)

[x ,y ,w ,h , conf, class0, class1, …] total 85 columns

col 0-3 is boxes, col 4 is conf, and the other 80 is the class

to obtain the real boxes, you need do some processing like e.g rescale xywh, NMS ( non max suppression)

You can check the detect.py and utils/general.py in Yolov5 source code

Answered By – Marcus Wong

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published