获取类型错误:“tuple”和“int”的操作数类型不受支持

2024-04-18 23:24:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试执行遮挡分析,以了解输入图像中哪些面片与模型的输出最大相关(最后一层是softmax的输出)。然而,我一直得到相同的错误,即类型不匹配,我猜。有人能解释一下我做错了什么以及如何防止这个问题吗

Traceback (most recent call last):
  File "occlusion.py", line 70, in <module>
    occlusion(attribute_extractor, jpegfile, mgn_output_for_original_img)
  File "occlusion.py", line 29, in occlusion
    output_height = int(np.ceil((height - int(occ_size)) / int(occ_stride)))
TypeError: unsupported operand type(s) for -: 'tuple' and 'int'

# model -> MGN - deep learning model
# image -> b_box cropped image of the person
# label -> MGN output label for the image


def occlusion(model, image, label, occ_size = 50, occ_stride = 50, occ_pixel = 0.5):

    #get the width and height of the img
    width, height = image.size, image.size
    print(width)
    print(height)

    #set the output img width and height
    output_height = int(np.ceil((height - int(occ_size)) / int(occ_stride)))
    output_width = int(np.ceil((width - int(occ_size)) / int(occ_stride)))

    #create a white image with the sizes defined above
    heatmap = torch.zeros((output_height, output_width))

    #iterate all the pixels in each column
    for h in range(0, height):
        for w in range(0, width):

            h_start = h*occ_stride
            w_start = w*occ_stride
            h_end = min(height, h_start + occ_size)
            w_end = min(width, w_start + occ_size)

            if (w_end) >= width or (h_end) >= height:
                continue

            input_image = image.clone().detach()

            #replacing all the pixel information in the image with occ_pixel(grey) in the specified location
            input_image[:, :, w_start:w_end, h_start:h_end] = occ_pixel

            #run inference on modified image
            output = model(input_image)
            output = nn.functional.softmax(output, dim=1)
            prob = output.tolist()[0][label]

            #setting the heatmap location to probability value
            heatmap[h, w] = prob

    return heatmap

attribute_extractor = MgnWrapper("./model.pt")
jpegfile = Image.open("tmpgal/    1.jpg")

width, height = jpegfile.size
print(type(width))
print(type(height))

mgn_output_for_original_img = attribute_extractor(jpegfile)
occlusion(attribute_extractor, jpegfile, mgn_output_for_original_img)


Tags: theinimageforoutputsizewidthstart
1条回答
网友
1楼 · 发布于 2024-04-18 23:24:28

我认为您应该将赋值表达式width, height = image.size, image.size更改为width, height = image.size,因为原始表达式将widthheight的值作为image.size的元组,而表达式width, height = image.size将获取image.size元组中的2个元素,并将每个值分配给widthheight

def occlusion(model, image, label, occ_size = 50, occ_stride = 50, occ_pixel = 0.5):

    #get the width and height of the img
    width, height = image.size
    print(width)
    print(height)

    #set the output img width and height
    output_height = int(np.ceil((height - int(occ_size)) / int(occ_stride)))
    output_width = int(np.ceil((width - int(occ_size)) / int(occ_stride)))

    #create a white image with the sizes defined above
    heatmap = torch.zeros((output_height, output_width))

    #iterate all the pixels in each column
    for h in range(0, height):
        for w in range(0, width):

            h_start = h*occ_stride
            w_start = w*occ_stride
            h_end = min(height, h_start + occ_size)
            w_end = min(width, w_start + occ_size)

            if (w_end) >= width or (h_end) >= height:
                continue

            input_image = image.clone().detach()

            #replacing all the pixel information in the image with occ_pixel(grey) in the specified location
            input_image[:, :, w_start:w_end, h_start:h_end] = occ_pixel

            #run inference on modified image
            output = model(input_image)
            output = nn.functional.softmax(output, dim=1)
            prob = output.tolist()[0][label]

            #setting the heatmap location to probability value
            heatmap[h, w] = prob

    return heatmap

相关问题 更多 >