我正在尝试执行遮挡分析,以了解我的输入图像中的哪些补丁与模型的输出具有最大相关性(最后一层是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)发布于 2020-03-03 08:45:30
我认为您应该将赋值表达式width, height = image.size, image.size更改为width, height = image.size,因为原始表达式将使width和height的值成为image.size形式的元组,而表达式width, height = image.size将获取image.size元组中的2个元素,并将每个值赋给width和height
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 heatmaphttps://stackoverflow.com/questions/60498517
复制相似问题