【问题标题】:Save output image of CNN model保存CNN模型的输出图像
【发布时间】:2021-07-01 10:54:42
【问题描述】:

我为边缘检测任务制作了一个 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)。如果有人可以提供帮助,将不胜感激。

【问题讨论】:

  • 对这个任务使用自动编码器或者改变模型输出3个通道或者这个分割成(13, 13, 1)的4个图像。
  • 您可以将其保存为具有 4 个通道的 RGBA 图像。

标签: image-processing pytorch tensor


【解决方案1】:

我建议以下步骤:

  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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    相关资源
    最近更新 更多