加载训练好的反射去除模型报错:Cannot handle this data type: (1,1,1), |u1
解决反射去除模型运行时的KeyError: ((1,1,1), '|u1')问题
错误原因
PIL的Image.fromarray不支持形状为(H, W, 1)的单通道数组格式,它要求灰度图的输入形状为(H, W)(二维数组)。你的代码中,模型输出的reflection_free_image保留了最后一个通道维度(形状为(128,128,1)),转换为uint8后传给Image.fromarray就触发了这个KeyError。
修复方案
直接去掉数组的最后一个通道维度即可,有两种简单实现方式:
方式一:使用np.squeeze()压缩维度
修改标注图像的代码行:
annotated_image = annotate_image_with_metrics(Image.fromarray(np.squeeze((reflection_free_image * 255).astype(np.uint8))))
方式二:通过数组切片提取通道数据
修改标注图像的代码行:
annotated_image = annotate_image_with_metrics(Image.fromarray((reflection_free_image * 255).astype(np.uint8)[..., 0]))
另外注意:原代码最后一行的括号不完整,需要确保annotate_image_with_metrics和Image.fromarray的括号都正确闭合,避免语法错误。
内容的提问来源于stack exchange,提问作者Krish
相关产品推荐
相关产品推荐

