Python图像大小不匹配导致索引器错误:布尔索引与维度0上的索引数组不匹配
我有以下代码,加载数据集,将图像大小调整为1200*800,加载其注释,然后报告准确性和预测:Python图像大小不匹配导致索引器错误:布尔索引与维度0上的索引数组不匹配,python,Python,我有以下代码,加载数据集,将图像大小调整为1200*800,加载其注释,然后报告准确性和预测: # resize images resized_images = [] file_names = glob(os.path.join(IMAGE_DIR, "*.jpg")) for i in range(len(file_names)): print("Resizing: " + str(i)) image = skimage.io.imread(file_names[i])
# resize images
resized_images = []
file_names = glob(os.path.join(IMAGE_DIR, "*.jpg"))
for i in range(len(file_names)):
print("Resizing: " + str(i))
image = skimage.io.imread(file_names[i])
image_resized = resize(image, (1200, 800),anti_aliasing=True)
resized_images.append(image_resized)
masks_prediction = np.zeros((1200, 800, len(file_names)))
for i in range(len(resized_images)):
print(i)
image = resized_images[i]
predictions = model.detect([image], verbose=1)
p = predictions[0]
masks = p['masks']
merged_mask = np.zeros((masks.shape[0], masks.shape[1]))
for j in range(masks.shape[2]):
merged_mask[masks[:,:,j]==True] = True
masks_prediction[:,:,i] = merged_mask
print(masks_prediction.shape)
#load annotations
dataset = components.ComponentsDataset()
dataset.load_components(ANNOTATION_DIR, "predict")
resized_images = []
file_names = glob(os.path.join(IMAGE_DIR, "*.jpg"))
for i in range(len(file_names)):
print("Resizing: " + str(i))
image = skimage.io.imread(file_names[i])
image_resized = resize(image, (1200, 800),anti_aliasing=True)
resized_images.append(image_resized)
# report the accuracy and prediction
accuracy = 0
precision = 0
for image_id in range(len(dataset.image_info)):
name = dataset.image_info[image_id]['id']
file_name = os.path.join(IMAGE_DIR, name)
image_id_pred = file_names.index(file_name)
merged_mask = masks_prediction[:, :, image_id_pred]
annotated_mask = dataset.load_mask(image_id)[0]
merged_annotated_mask = np.zeros((1200, 800))
for i in range(annotated_mask.shape[2]):
merged_annotated_mask[annotated_mask[:,:,i]==True] = True
accuracy += np.sum(merged_mask==merged_annotated_mask) / (1200 * 800)
all_correct = np.sum(merged_annotated_mask[merged_mask == 1])
precision += all_correct / (np.sum(merged_mask))
print('accuracy:{}'.format(accuracy / len(file_names)))
print('precision:{}'.format(precision / len(file_names)))
但是,我发现以下错误,这使我认为尺寸完全有问题:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-38-a652e79112fb> in <module>()
10 merged_annotated_mask = np.zeros((1200, 800))
11 for i in range(annotated_mask.shape[2]):
---> 12 merged_annotated_mask[annotated_mask[:,:,i]==True] = True
13 accuracy += np.sum(merged_mask==merged_annotated_mask) / (1200 * 800)
14 all_correct = np.sum(merged_annotated_mask[merged_mask == 1])
IndexError: boolean index did not match indexed array along dimension 0; dimension is 1200 but corresponding boolean dimension is 1572
列表中的图像大小都正确调整为(1200800)。但带注释的遮罩是从数据集加载的,不会动态调整大小:
annotated_mask = dataset.load_mask(image_id)[0]
方法load\u mask
生成图像原始高度
和宽度
的掩模图像,而不是调整大小的掩模图像。遮罩大小和图像大小需要匹配
您可以在加载遮罩图像后调整其大小(类似于调整输入图像的大小),以使此方法起作用
另一个选择是进行批处理预处理——将所有图像调整为通用大小,再次保存为.jpg,并将其用作新输入,而不在此程序中进行任何类型的调整。但是,您还必须仔细调整其他数据(如多边形)以匹配新坐标。我已经更新了答案。面具的原始图像大小确实是个问题。我不能做任何事情来弄乱多边形,所以我认为为我调整面具的大小是最好的方法。你能告诉我应该在哪里调整它们的大小吗?在
load_mask
方法中,在绘制多边形之后,在将数据类型转换为np.bool
之前,我将mask=resize(mask,(1200,800),anti_aliasing=True)
添加到带有np.bool
的返回语句之前,但是我仍然得到同样的错误。也许load\u mask
正在采取另一条路径,委托给父类?那么也需要调整那里的大小。
annotated_mask = dataset.load_mask(image_id)[0]