保存CNN模型的输出图像

时间:2021-04-05 15:23:38

标签: image-processing pytorch tensor

我为边缘检测任务制作了一个 CNN 模型。它运行良好,我可以通过功能图看到良好的结果。问题是我在每个卷积层中使用了 4 个过滤器,因此输出将是具有 4 个通道的图像,但我需要将图像保存为 3 个通道以供稍后使用,这里是代码示例我用过:

#Feed image to model:
output = model(img) #input size (1, 3, 224, 224)
#convert output tensor to numpy
fimg = output.detach().cpu().numpy()   
#get rid of the single batch dimension 
f2img = fimg.squeeze(0)
#swap axes to (13, 13, 4)
f2img = fimg.transpose(1, 2, 0)

它会被保存为 (13,13,4) 但我需要它作为 (13, 13, 3)。如果有人可以提供帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

我建议采取以下步骤:

  1. 将图像数组RGBA转换为PIL图像对象

    来自 PIL 导入图像

    img_rgba = Image.fromarray(img_rgba)

  2. RGBA 图像对象转 RGB

    img_rgb = img_rgba.convert('RGB')

  3. 回到 np.ndarray

    img_rgb = np.array(img_rgb)

注意:如果您的图像是 RBGA 格式而不是 RGBA,您将需要一个额外的步骤将其从 RBG 转换为 RGB。

img_rgb = cv2.cvtColor(bgr_image_array, cv2.COLOR_BGR2RGB)

相关问题