lee-zq / 3DUNet-Pytorch

3DUNet implemented with pytorch
484 stars 108 forks source link

Mismatched Dimensions of input and output (couldn't visualize the result) #8

Closed ytliang97 closed 3 years ago

ytliang97 commented 3 years ago

Hi, thank you for sharing.

I follow your tutorial to train a model with two .nii samples.

It trained success and could be tested, but I noticed one thing that the shape of the model output didn't match with the input's whether I use raw data or fixed data as input data.

I use LiTS dataset number 28 data for testing.

Here is the code: checkDimension.py


import SimpleITK as sitk
from scipy import ndimage

def sitk_read_raw(img_path, resize_scale=1): # 读取3D图像并resale(因为一般医学图像并不是标准的[1,1,1]scale)
    nda = sitk.ReadImage(img_path)
    if nda is None:
        raise TypeError("input img is None!!!")
    nda = sitk.GetArrayFromImage(nda)  # channel first
    nda=ndimage.zoom(nda,[resize_scale,resize_scale,resize_scale],order=0) #rescale

    return nda

if __name__ == '__main__':
    input_name = './fixed_data/data/volume-28.nii'
    data_np = sitk_read_raw(input_name)
    print('model input shape: {:}'.format(data_np.shape))

    output_name = './output/model2/result/result-28.nii'
    data_np = sitk_read_raw(output_name)
    print('model output shape: {:}'.format(data_np.shape))

    raw_data = './raw_dataset/LiTS_batch2/data/volume-28.nii'
    data_np = sitk_read_raw(raw_data)
    print('raw data shape: {:}'.format(data_np.shape))

    raw_label = './raw_dataset/LiTS_batch2/label/segmentation-28.nii'
    data_np = sitk_read_raw(raw_label)
    print('raw data shape: {:}'.format(data_np.shape))

The result is:

model input shape: (122, 512, 512)
model output shape: (64, 256, 256)
raw data shape: (129, 512, 512)
raw label shape: (129, 512, 512)

I want to visualize it with ITK-SNAP.

The original data could be visualized like this:

image

But currently, the output shape of the model can't be visualized like above:

image

I still can't find out where the code I should edit to meet my needs. Could you give me some advice? Thank you!

lee-zq commented 3 years ago

@ytliang97

  1. When making inferences, original data should be used instead of fixed_data (please refer to test.py).
  2. The shape of the data is inconsistent because the original data was resized (default=0.5) before being input to the network. Therefore, you can set resize_scale to 1 in config.py, or use nearest neighbor interpolation to magnify the prediction result twice (recommended, much less calculation).
ytliang97 commented 3 years ago

Thank you for replying, I'll check it.

ytliang97 commented 3 years ago

The problem has been solved. Thank you!