记录解决RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 27 but got size

Problem Description

In the process of target detection service, the yolov7 model is packaged into a prediction service API through flask. The image input size for this training is 1280. If the input prediction image is larger than 1280, the prediction is successful, and if it is smaller than 1280, it will report RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 27 but got size.
Since only small images are predicted to report an error, it is guessed that it is a resize problem during image processing, and the following code line is prompted to be wrong

pred = self.model(img, augment=self.augment)[0]

The complete error message is as follows:
insert image description here

Cause Analysis:

Tip: Fill in the analysis of the question here:

After analyzing it for a long time, I finally found out that the padding of the small picture was not handled well. In the following code, a parameter stride was missing, which caused the pixel error of the small picture during the padding process, resulting in a dimension error.

img = letterbox(img0, new_shape=self.img_size)[0]

solution:

Finally, by referring to the image processing process in the original utils.datasets code, the code is modified. The reference code is as follows
insert image description here

Transform your own base64_to_image function code as follows:

    def base64_to_image(self,imagebase64):
        """
        输入base64图片,输出图片
        """
        try:
            imgbase64= base64.b64decode(imagebase64)
            buf_str = BytesIO(imgbase64).getvalue()
            nparr = np.fromstring(buf_str, np.uint8)
            img0 = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
            
#             img = letterbox(img0, new_shape=self.img_size)[0]
            img = letterbox(img0, self.img_size, stride=self.stride)[0]
            img = img[:, :, ::-1].transpose(2, 0, 1)  # BGR to RGB, to 3x416x416
            img = np.ascontiguousarray(img)
            
            return img,img0
        except:
            print("输入图片必须是BASE64格式...")

Because the first version of the code was not written by myself, I spent an afternoon and night to check the code line by line, and finally solved it, so I should record it to prevent forgetting it next time.

Guess you like

Origin blog.csdn.net/h363924219/article/details/127778425