【问题标题】:How to handle samples with multiple images in a pytorch image processing model?如何在 pytorch 图像处理模型中处理具有多个图像的样本?
【发布时间】:2020-11-15 11:36:56
【问题描述】:

我的模型训练涉及对同一图像的多个变体进行编码,然后将生成的表示对图像的所有变体求和。

数据加载器生成形状为:[batch_size,num_variants,1,height,width] 的张量批次。 1 对应图像颜色通道。

如何在 pytorch 中使用 minibatches 训练我的模型? 我正在寻找一种通过网络转发所有 batch_size×num_variant 图像并将所有变体组的结果相加的正确方法。

我目前的解决方案涉及展平前两个维度并执行 for 循环来对表示进行求和,但我觉得应该有更好的方法,而且我不确定渐变是否会记住所有内容。

【问题讨论】:

  • 您考虑过 3D 卷积模型吗?它们专为此类方法而设计。你可以考虑否。在 3D 卷积中的变体为depthnn.Conv3d 的输入格式为 batch_size*channels*depth*height*width
  • @planet_pluto 我不认为 3D 卷积是我正在寻找的。对图像的操作通过变体共享。它是在图像的所有变体上运行的相同模型。

标签: pytorch tensor mini-batch


【解决方案1】:

不确定我是否理解正确,但我想这就是你想要的(比如批量图像张量称为image):

Nb, Nv, inC, inH, inW = image.shape

# treat each variant as if it's an ordinary image in the batch
image = image.reshape(Nb*Nv, inC, inH, inW)

output = model(image)
_, outC, outH, outW = output.shape[1]

# reshapes the output such that dim==1 indicates variants
output = output.reshape(Nb, Nv, outC, outH, outW)

# summing over the variants and lose the dimension of summation, [Nb, outC, outH, outW]
output = output.sum(dim=1, keepdim=False)

我使用inCoutCinH等,以防输入和输出通道/大小不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多