作者你好,首先很感谢您在github上提供的代码。
下面是我在训练3D CT数据出现的问题:
训练时在运行到main.py里的260行,也就是loss=criterion_ce(outputs,y.argmax(dim=1)) +criterion_dice(outputs,y.argmax(dim=1))出错,我进去看后,发现是predict和target维度不一致,以图片大小为646496,batchs=4,channel=2为例,predict为(4,2,64,64,96),target为(4,128,64,96),缺少channel维度,使得dice损失函数无法运行,同时self._one_hot_encoder函数使得第二个数翻倍,也就是叠加了,我加入target = torch.unsqueeze(target,1)这行代码后,成功进行训练,但tensorboard中显示loss在缓慢下降,但dice直接下降到0。
D:\ProgramData\miniconda3\envs\pytorch\python.exe E:/python_learning/Pytorch-Medical-Segmentation-master/main.py
epoch:1
Batch: 0/5 epoch 1
Traceback (most recent call last):
File "E:\python_learning\Pytorch-Medical-Segmentation-master\main.py", line 582, in
train()
File "E:\python_learning\Pytorch-Medical-Segmentation-master\main.py", line 265, in train
loss = criterion_ce(outputs, y.argmax(dim=1)) + criterion_dice(outputs, y.argmax(dim=1))
File "D:\ProgramData\miniconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 1194, in _call_impl
return forward_call(*input, **kwargs)
File "E:\python_learning\Pytorch-Medical-Segmentation-master\loss_function.py", line 141, in forward
assert inputs.size() == target.size(), 'predict & target shape do not match'
AssertionError: predict & target shape do not match
Process finished with exit code 1
非常感谢您的回复