frankkramer-lab / MIScnn

A framework for Medical Image Segmentation with Convolutional Neural Networks and Deep Learning
GNU General Public License v3.0
402 stars 116 forks source link

ValueError on predicting after model is trained with analysis "patchwise-crop/grid" regime #65

Closed jumutc closed 3 years ago

jumutc commented 3 years ago

The pipeline looks like this:

# Create a pixel value normalization Subfunction for z-score scaling
sf_zscore = Normalization(mode="z-score")
sf_grayscale = Convert2Grayscale()

# Assemble Subfunction classes into a list
sf = [sf_grayscale, sf_zscore]
# Configure data augmentation part
aug = Data_Augmentation(cycles=2, scaling=True, rotations=True,
                        elastic_deform=True, mirror=True, brightness=True,
                        contrast=True, gamma=True, gaussian_noise=True)
aug.config_p_per_sample = 0.5
aug.config_contrast_range = (0.1, 10)
aug.config_brightness_range = (0.1, 10)
aug.config_gamma_range = (0.1, 10)
aug.config_gaussian_noise_range = (0.1, 1)
pp = Preprocessor(data_io, batch_size=14, subfunctions=sf,
                  prepare_subfunctions=True, prepare_batches=True,
                  data_aug=aug, analysis="patchwise-crop", patch_shape=(320, 320))

Original images are of much bigger size of course but when I do predict model.predict(sample_list[0:4]):

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-cae07e0f8070> in <module>
----> 1 model.predict(sample_list[0:4])

~\anaconda3\envs\bacteria_cfu\lib\site-packages\miscnn\neural_network\model.py in predict(self, sample_list, return_output, activation_output)
    165                 pred_batch = self.model.predict_on_batch(batch)
    166                 pred_list.append(pred_batch)
--> 167             pred_seg = np.concatenate(pred_list, axis=0)
    168             # Postprocess prediction
    169             pred_seg = self.preprocessor.postprocessing(sample, pred_seg,

<__array_function__ internals> in concatenate(*args, **kwargs)

ValueError: need at least one array to concatenate

I have checked both analysis regimes and the ValueError appears in both.

muellerdo commented 3 years ago

Hey @jumutc,

MIScnn does not automatically resizes the images if they are smaller than the desired patch shape.

Problem:

Side node:
If running patchwise-crop training, there could be no exceptions thrown due to the random cropping of batchgenerators will automatically pad an image if it is smaller than patch size. https://github.com/MIC-DKFZ/batchgenerators/blob/master/batchgenerators/augmentations/crop_and_pad_augmentations.py

Debugging:
Please try to output your image sizes (after preprocessing) and verify if they are all bigger or equal than your patch size.

Fast Solution:

Cheers, Dominik

jumutc commented 3 years ago

@muellerdo all images are much larger than 320x320 and the sizes are >1k for each side. Can it be that because of different sizes of different images we have a problem in batchgenerators? I also noticed that in batches folder everything is generated for predictions for being used so something is wrong in data_generator.py most probably.

jumutc commented 3 years ago

I tried to debug this case and the problem indeed is in that class:

from miscnn.neural_network.data_generator import DataGenerator

dataGen = DataGenerator(sample_list[:1], model.preprocessor,
                        training=False, validation=False,
                        shuffle=False, iterations=None)
print(len(dataGen))
print(dataGen[0])

with output being:

0
[[[[-2.0725093  -2.0371544  -2.0371544 ]
   [-2.0017996  -1.9310896  -2.0017996 ]
   [-2.1432192  -2.0017996  -2.3907037 ]
   ...
   [-0.7643772  -0.870442   -1.1532813 ]
   [-0.9765067  -0.870442   -1.1179265 ]
   [-0.9411518  -1.0118617  -1.0825715 ]]]]