首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取元组:-:‘TypeError’和'int‘不支持的操作数类型

获取元组:-:‘TypeError’和'int‘不支持的操作数类型
EN

Stack Overflow用户
提问于 2020-03-03 08:37:15
回答 1查看 101关注 0票数 0

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

代码语言:javascript
复制
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'
代码语言:javascript
复制
# 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)
EN

回答 1

Stack Overflow用户

发布于 2020-03-03 08:45:30

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

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60498517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档