Replies: 3 comments 1 reply
-
I'm not sure I can duplicate the issue here. What size are your tensors just before import monai, torch
import monai.transforms as mt
img = torch.rand(1, 32, 32, 17)
lbl = torch.zeros_like(img)
lbl[0, 10:20, 10:20, 4:12] = 1 # one small label
data = {"image": img, "label": lbl}
keys = ("image", "label")
com = mt.Compose(
[
mt.CropForegroundd(keys, source_key=keys[1]),
mt.SpatialPadd(keys, spatial_size=(32, 32, 16), mode="constant"),
mt.RandCropByPosNegLabeld(keys, label_key=keys[1], spatial_size=(32, 32, 16), num_samples=2),
mt.RandGaussianNoised(keys[0], prob=0.15, std=0.01),
mt.RandFlipd(keys, spatial_axis=0, prob=0.5),
mt.RandFlipd(keys, spatial_axis=1, prob=0.5),
mt.RandFlipd(keys, spatial_axis=2, prob=0.5),
]
)
ds = monai.data.Dataset([data], com)
dl = monai.data.DataLoader(ds)
res = monai.utils.first(dl)
print(res["image"].shape, res["label"].shape) # prints torch.Size([2, 1, 32, 32, 16]) torch.Size([2, 1, 32, 32, 16]) |
Beta Was this translation helpful? Give feedback.
-
Hi, thanks for your response. Originally, each tensor had dimensions of (512, 512, z), but after applying CropForeground, each tensor has a different size due to variations in the background of each image. I opted not to use CropForeground with source_key=keys[1] because it creates a patch of the region I intend to segment, which is not my objective. Instead, I aim to eliminate the black areas surrounding the main body, ensuring that the input data to the network does not include a large black portion. To achieve this, I removed CropForeground and applied CenterSpatialCrop with a fixed size using roi_size=(460, 460, -1). This adjustment resolved the error, although it did not eliminate the black parts from all images. |
Beta Was this translation helpful? Give feedback.
-
I'm still not sure how this error is coming up for you. |
Beta Was this translation helpful? Give feedback.
-
Hi, I have the title error and I do not know how to solve it. This is MY code:
keys=("image", "label")
xforms = [
LoadImaged(keys, ensure_channel_first=True, image_only=True),
Orientationd(keys, axcodes="LPS"),
CropForegroundd(keys, source_key=keys[0]),
Spacingd(keys, pixdim=(1.0, 1.0, 3.0), mode=("bilinear", "nearest")[: len(keys)]),
ScaleIntensityRanged(keys[0], a_min=-1000.0, a_max=500.0, b_min=0.0, b_max=1.0, clip=True),
SpatialPadd(keys, spatial_size=(32, 32, 16), mode="constant"),
]
if mode == "train":
xforms.extend(
[
RandCropByPosNegLabeld(keys, label_key=keys[1], spatial_size=(32, 32, 16), num_samples=2),
RandGaussianNoised(keys[0], prob=0.15, std=0.01),
RandFlipd(keys, spatial_axis=0, prob=0.5),
RandFlipd(keys, spatial_axis=1, prob=0.5),
RandFlipd(keys, spatial_axis=2, prob=0.5),
]
Why do I get that error if I have added SpatialPadd with a spatial size of (32,32,16) before RandCropByPosNegLabeld? Thank you
Beta Was this translation helpful? Give feedback.
All reactions