MASILab / tbi_ct_lesion_segmentation

MIT License
5 stars 5 forks source link

ValueError: cannot contain negative values #1

Closed muschellij2 closed 1 month ago

muschellij2 commented 5 years ago

Can't share the data right now, but running into ValueError: ((420, 420), (400, 400), (-60, -60)) cannot contain negative values., when trying on our data:

Traceback (most recent call last):
  File "train.py", line 119, in <module>
    num_channels=num_channels)
  File "/scratch/users/jmusche1@jhu.edu/pitch_run/tbi_ct_lesion_segmentation/utils/patch_ops.py", line 287, in CreatePatchesForTraining
    ct = pad_image(ct, target_dims)
  File "/scratch/users/jmusche1@jhu.edu/pitch_run/tbi_ct_lesion_segmentation/utils/pad.py", line 16, in pad_image
    new_img[:,:,:] = np.pad(img_data[:,:,:], pads, 'constant', constant_values=0)
  File "/software/apps/anaconda/5.2/python/3.6/lib/python3.6/site-packages/numpy/lib/arraypad.py", line 1301, in pad
    pad_width = _validate_lengths(narray, pad_width)
  File "/software/apps/anaconda/5.2/python/3.6/lib/python3.6/site-packages/numpy/lib/arraypad.py", line 1086, in _validate_lengths
    raise ValueError(fmt % (number_elements,))
ValueError: ((420, 420), (400, 400), (-60, -60)) cannot contain negative values.
muschellij2 commented 5 years ago

This error is due to:

        target_dims = (512*2, 512*2, 64)
        ct = pad_image(ct, target_dims)

from https://github.com/MASILab/tbi_ct_lesion_segmentation/blob/master/utils/patch_ops.py#L287, which calls https://github.com/MASILab/tbi_ct_lesion_segmentation/blob/master/utils/pad.py#L3.

We have registered our data to the MNI template:

>>> ct.shape
(184, 224, 184)

which now causes errors. But even if we had not, we have some thin-slice CT scans which have dimensions of 512x512x100 let's say, and this will fail as well, correct?

sremedios commented 5 years ago

Thank you for bringing this to our attention. The pad_image() function has now been generalized to pad appropriately for arbitrary image sizes, where new images are padded to dimensions of the nearest greatest multiple of 2^4 (required for utilization of the U-net).

https://github.com/MASILab/tbi_ct_lesion_segmentation/blob/master/utils/pad.py

muschellij2 commented 5 years ago

We have the data in a standard space (184, 224, 184). Trying the update, I get:

*** PREPROCESSSING ***
100%|███████████████████████████████████████████| 21/21 [00:02<00:00,  9.83it/s]
Total number of lesion patches = 361384.0
Allowed total number of patches = 10000
  0%|                                                    | 0/10 [00:00<?, ?it/s
Traceback (most recent call last):
  File "train.py", line 119, in <module>
    num_channels=num_channels)
  File "/scratch/users/jmusche1@jhu.edu/pitch_run/tbi_ct_lesion_segmentation/utils/patch_ops.py", line 297, in CreatePatchesForTraining
    CTPatches[indices[cur_idx], :, :, :] = ct_patch
ValueError: could not broadcast input array from shape (78,128,1) into shape (128,128,1)
sremedios commented 5 years ago

This appears to be an issue with the patch collection at edges of CT slices.

I've tried a quick fix, please let me know whether this addresses the problem.

For reference, the fix is at: https://github.com/MASILab/tbi_ct_lesion_segmentation/blob/master/utils/patch_ops.py#L225 where images are padded out further to allow for patches to be gathered from edges of slices.