我正在尝试实施和训练original U-Net model,但是在尝试使用ISBI Challenge Dataset训练模型时,我陷入了困境。

根据原始的U-Net模型,网络输出具有2个通道且大小为388 x 388的图像。因此,我用于训练的数据加载器会生成张量,其张量为[batch,channel = 1,width = 572,height = 572]用于输入图像,[批处理,通道= 2,宽度= 388,宽度= 388]用于目标/输出图像。

我的问题实际上是,当我尝试使用nn.CrossEntropyLoss()时,出现以下错误:


  RuntimeError:无效参数3:仅支持成批的空间目标(3D张量),但尺寸目标为:4在/opt/conda/conda-bld/pytorch_1556653099582/work/aten/src/THNN/generic/SpatialClassNLLCriterion.c:59


我只是从PyTorch(这里是新手)开始……所以,如果有人可以帮助我克服这个问题,我将不胜感激。

源代码在GitHub上可用:

https://github.com/dalifreire/cnn_unet_pytorch
https://github.com/dalifreire/cnn_unet_pytorch/blob/master/unet_pytorch.ipynb

最好的祝福!

更新

我只是从蒙版中删除了通道尺寸,一切正常…现在,我正在生成形状为1 [width = 388,height = 388]的蒙版。

之后,我将使用输入图像(X),目标蒙版(y)和预测的输出蒙版(y_hat),如下所示:

X     --> torch.Size([10, 1, 572, 572])
y     --> torch.Size([10, 388, 388])
y_hat --> torch.Size([10, 2, 388, 388])


但是,我不明白为什么目标蒙版(y)和预测蒙版(y_hat)必须具有不同的形状?对我来说很奇怪

最佳答案

来自PyTorch的CrossEntropyLoss文档字符串:

Shape:

    - Input: :math:`(N, C)` where `C = number of classes`, or
      :math:`(N, C, d_1, d_2, ..., d_K)` with :math:`K \geq 1`
      in the case of `K`-dimensional loss.

    - Target: :math:`(N)` where each value is :math:`0 \leq \text{targets}[i] \leq C-1`, or
      :math:`(N, d_1, d_2, ..., d_K)` with :math:`K \geq 1` in the case of
      K-dimensional loss.

    - Output: scalar.
      If :attr:`reduction` is ``'none'``, then the same size as the target:
      :math:`(N)`, or
      :math:`(N, d_1, d_2, ..., d_K)` with :math:`K \geq 1` in the case
      of K-dimensional loss.


如果目标已包含类索引,则应删除通道维。

Source

08-26 20:26