Python 获取类型错误:不支持的操作数类型为-:';元组';和';int';
我正在尝试执行遮挡分析,以了解输入图像中哪些面片与模型的输出最大相关(最后一层是softmax的输出)。然而,我一直得到相同的错误,即类型不匹配,我猜。有人能解释一下我做错了什么以及如何防止这个问题吗Python 获取类型错误:不支持的操作数类型为-:';元组';和';int';,python,deep-learning,Python,Deep Learning,我正在尝试执行遮挡分析,以了解输入图像中哪些面片与模型的输出最大相关(最后一层是softmax的输出)。然而,我一直得到相同的错误,即类型不匹配,我猜。有人能解释一下我做错了什么以及如何防止这个问题吗 Traceback (most recent call last): File "occlusion.py", line 70, in <module> occlusion(attribute_extractor, jpegfile, mgn_output_for_orig
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'
我认为您应该将赋值表达式
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 heatmap
既然image.size返回一个由两个元素组成的元组,分别分配给width和height?只需使用
print()
(和print(type(…)
)查看此行变量中的值即可打印(类型(高度))
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