When debugging the code of densenet for classification task, the following errors are encountered in the process of image preprocessing:
runtimeerror: stack expectations each tensor to be equal size, but got [640, 640] at entry 0 and [560, 560] at entry 2
it means that the size of the loaded sheets is inconsistent
after searching, it is found that there should be a problem in the preprocessing process when I load the image
the following is the instantiation part of training data preprocessing.
train_transform = Compose(
[
LoadImaged(keys=keys),
AddChanneld(keys=keys),
CropForegroundd(keys=keys[:-1], source_key="tumor"),
ScaleIntensityd(keys=keys[:-1]),
# # Orientationd(keys=keys[:-1], axcodes="RAI"),
Resized(keys=keys[:-1], spatial_size=(64, 64), mode='bilinear'),
ConcatItemsd(keys=keys[:-1], name="image"),
RandGaussianNoised(keys=["image"], std=0.01, prob=0.15),
RandFlipd(keys=["image"], prob=0.5), # , spatial_axis=[0, 1]
RandAffined(keys=["image"], mode='bilinear', prob=1.0, spatial_size=[64, 64], # The 3 here is because we don't know what the size of the three modal images will be after stitching, so we first use
rotate_range=(0, 0, np.pi/15), scale_range=(0.1, 0.1)),
ToTensord(keys=keys),
]
)
My keys are [“t2_img”, “dwi_img”, “adc_img”, “tumor”]
the error shows that the loaded tensor has dimensions [640, 640] and [560, 560], which are the dimensions of my original image, which indicates that there may be a problem in my clipping step or resize step. Finally, after screening, it is found that there is a problem in my resize step. In the resize step, I selected keys = keys [: – 1], that is, it does not contain “tumor”. Therefore, when resizing, my tumor image will still maintain the size of the original image, and the data contained in this dictionary will still be a whole when loaded, The dimensions of each dimension of the whole will automatically expand to the largest of the corresponding dimensions of all objects, so the data I loaded will still be the size of the original drawing. Make the following corrections:
train_transform = Compose(
[
LoadImaged(keys=keys),
AddChanneld(keys=keys),
CropForegroundd(keys=keys[:-1], source_key="tumor"),
ScaleIntensityd(keys=keys[:-1]),
# # Orientationd(keys=keys[:-1], axcodes="RAI"),
Resized(keys=keys, spatial_size=(64, 64), mode='bilinear'), # remove [:-1]
ConcatItemsd(keys=keys[:-1], name="image"),
RandGaussianNoised(keys=["image"], std=0.01, prob=0.15),
RandFlipd(keys=["image"], prob=0.5), # , spatial_axis=[0, 1]
RandAffined(keys=["image"], mode='bilinear', prob=1.0, spatial_size=[64, 64], # The 3 here is because we don't know what the size of the three modal images will be after stitching, so we first use
rotate_range=(0, 0, np.pi/15), scale_range=(0.1, 0.1)),
ToTensord(keys=keys),
]
)
Run successfully!
Read More:
- [Solved] RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dim
- [Solved] ValueError: Error when checking input: expected conv2d_input to have 4 dimensions
- Opencv: How to Draw Palette
- [Solved] ValueError: row index was 65536, not allowed by .xls format
- Here is the difference and connection of Torch. View (), Transpose (), and Permute ()
- Error:output with shape [1, 224, 224] doesn‘t match the broadcast shape [3, 224, 224]
- [Solved] Python Error: An attempt has been made to start a new process before the current process has finished …
- How to Solve cv2.applyColorMap Error
- Python: How to get the size of the picture (byte/kb/mb)
- [Solved] YOLOv4 Error: Layer before convolutional layer must output image.: No error
- [Solved] Error occurred when finalizing GeneratorDataset iterator: Failed precondition: Python interpreter st
- Python RuntimeError: Expected 4-dimensional input for 4-dimensional weight [32, 1, 5, 5]
- RuntimeWarning: overflow encountered in ubyte_Scalars pixel addition and subtraction overflow exception
- How to Solve RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu
- CV: How to extracts the part of the picture with the specified color
- [Solved] AttributeError: ‘NoneType‘ object has no attribute ‘astype‘
- Python+ Pandas + Evaluation of Music Equipment over the years (Notes)
- Panda error in modifying line name index does not support mutable operations
- [Solved] PyTorch Caught RuntimeError in DataLoader worker process 0和invalid argument 0: Sizes of tensors mus
- from keras.preprocessing.text import Tokenizer error: AttributeError: module ‘tensorflow.compat.v2‘ has..