项目场景:
最近在用UNETR做医学图像分割,在用官方的tutorial跑自己的数据集时出现了报错不能训练
问题描述
例如:在前面的transform设置,dataset和dataloader都能正常运行,在训练的时候下面这句代码
global_step, dice_val_best, global_step_best = train(
global_step, train_loader, dice_val_best, global_step_best
)
报错如下:
RuntimeError: Caught RuntimeError in DataLoader worker process 0.
Original Traceback (most recent call last):
File "/opt/conda/lib/python3.8/site-packages/monai/transforms/transform.py", line 89, in apply_transform
return _apply_transform(transform, data, unpack_items)
File "/opt/conda/lib/python3.8/site-packages/monai/transforms/transform.py", line 53, in _apply_transform
return transform(parameters)
File "/opt/conda/lib/python3.8/site-packages/monai/transforms/croppad/dictionary.py", line 1171, in __call__
self.randomize(label, fg_indices, bg_indices, image)
File "/opt/conda/lib/python3.8/site-packages/monai/transforms/croppad/dictionary.py", line 1153, in randomize
self.centers = generate_pos_neg_label_crop_centers(
File "/opt/conda/lib/python3.8/site-packages/monai/transforms/utils.py", line 512, in generate_pos_neg_label_crop_centers
centers.append(correct_crop_centers(center, spatial_size, label_spatial_shape, allow_smaller))
File "/opt/conda/lib/python3.8/site-packages/monai/transforms/utils.py", line 439, in correct_crop_centers
raise ValueError("The size of the proposed random crop ROI is larger than the image size.")
ValueError: The size of the proposed random crop ROI is larger than the image size.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/conda/lib/python3.8/site-packages/torch/utils/data/_utils/worker.py", line 198, in _worker_loop
data = fetcher.fetch(index)
File "/opt/conda/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/opt/conda/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/opt/conda/lib/python3.8/site-packages/monai/data/dataset.py", line 97, in __getitem__
return self._transform(index)
File "/opt/conda/lib/python3.8/site-packages/monai/data/dataset.py", line 807, in _transform
data = apply_transform(_transform, data)
File "/opt/conda/lib/python3.8/site-packages/monai/transforms/transform.py", line 113, in apply_transform
raise RuntimeError(f"applying transform {transform}") from e
RuntimeError: applying transform <monai.transforms.croppad.dictionary.RandCropByPosNegLabeld object at 0x7f3c221e9340>
- 不知道怎么解决把num_works改为了0依然报错。
- 查了这句报错:ValueError: The size of the proposed random crop ROI is larger than the image size参考.说是该错误是由于ROI裁剪过大而无法覆盖整个输入图像体积造成的。它与网络架构无关,但与数据加载器相关。于是找到transform设置中把
spatial_size=(96, 96, 96)
改成spatial_size=(48, 48, 48)
train_transforms = Compose(
[
LoadImaged(keys=["image", "label"]),
AddChanneld(keys=["image", "label"]),
Orientationd(keys=["image", "label"], axcodes="RAS"),
Spacingd(
keys=["image", "label"],
pixdim=(1.5, 1.5, 2.0),
mode=("bilinear", "nearest"),
),
ScaleIntensityRanged(
keys=["image"],
a_min=-175,
a_max=250,
b_min=0.0,
b_max=1.0,
clip=True,
),
CropForegroundd(keys=["image", "label"], source_key="image"),
RandCropByPosNegLabeld(
keys=["image", "label"],
label_key="label",
# spatial_size=(96, 96, 96),
spatial_size=(48, 48, 48),
pos=1,
neg=1,
num_samples=4,
image_key="image",
image_threshold=0,
),
RandFlipd(
keys=["image", "label"],
spatial_axis=[0],
prob=0.10,
),
RandFlipd(
keys=["image", "label"],
spatial_axis=[1],
prob=0.10,
),
RandFlipd(
keys=["image", "label"],
spatial_axis=[2],
prob=0.10,
),
RandRotate90d(
keys=["image", "label"],
prob=0.10,
max_k=3,
),
RandShiftIntensityd(
keys=["image"],
offsets=0.10,
prob=0.50,
),
ToTensord(keys=["image", "label"]),
]
)
但是训练模型时又报了新的错误,报错信息如下:
RuntimeError: The size of tensor a (27) must match the size of tensor b (216) at non-singleton dimension 1
原因分析:
ROI裁剪尺寸大于图片本身了,
解决方案:
在调用UNETR模型时,参数img_size=(96, 96, 96)改为img_size=(32, 32, 32)