Home > Mobile >  TypeError: Can't convert vector element for 'scores', index=0
TypeError: Can't convert vector element for 'scores', index=0

Time:04-06

I'm trying to take the output of a yolov5s.onnx model and and run NMSBoxes on it. But I keep getting this error:

Traceback (most recent call last):
  File "python_detection.py", line 132, in <module>
    class_ids, confidences, boxes = wrap_detection(inputImage, outs[0])
  File "python_detection.py", line 88, in wrap_detection
    indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.25, 0.45)
TypeError: Can't convert vector element for 'scores', index=0

Everywhere I look, people are using the exact same code as me. Which makes sense, since this code was mostly copied from a tutorial. So I don't know what I'm doing so wrong that keeps giving me this error.

Here's the full function:

def wrap_detection(input_image, output_data):
    class_ids = []
    confidences = []
    boxes = []

    rows = output_data.shape[0]

    image_width, image_height, _ = input_image.shape

    x_factor = image_width / INPUT_WIDTH
    y_factor =  image_height / INPUT_HEIGHT

    for r in range(rows):
        row = output_data[r]
        confidence = row[4]
        if confidence >= 0.4:

            classes_scores = row[5:]
            _, _, _, max_indx = cv2.minMaxLoc(classes_scores)
            class_id = max_indx[1]
            if (classes_scores[class_id] > .25):

                confidences.append(confidence)

                class_ids.append(class_id)

                x, y, w, h = row[0].item(), row[1].item(), row[2].item(), row[3].item()
                left = int((x - 0.5 * w) * x_factor)
                top = int((y - 0.5 * h) * y_factor)
                width = int(w * x_factor)
                height = int(h * y_factor)
                box = np.array([left, top, width, height])
                boxes.append(box)

    '''
    Print the raw output
    '''
    # Save output
    np.set_printoptions(threshold=sys.maxsize)
    file = open("python_raw_model_output.txt", "w ")
    for i in range(len(boxes)):
        file.write(str(boxes[i])   " "   str(confidences[i])   " "   str(class_ids[i]))
        file.write("\n")
    file.close()

    # NMS on the lists
    indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.25, 0.45)

    result_class_ids = []
    result_confidences = []
    result_boxes = []

    for i in indexes:
        result_confidences.append(confidences[i])
        result_class_ids.append(class_ids[i])
        result_boxes.append(boxes[i])

    return result_class_ids, result_confidences, result_boxes

CodePudding user response:

I had the same issue. It seemed to be related to the cuda configuration as it works fine on the cpu. I did not figure out exactly what was wrong but I worked around the issue by using fastNMS: enter link description here

  • Related