MIScnn icon indicating copy to clipboard operation
MIScnn copied to clipboard

How to visualise my training dataset to confirm it is being correctly loaded?

Open emmanuel-nwogu opened this issue 1 year ago • 3 comments

ct_scans/
   scan_a/
      segmentation.nii.gz
      imaging.nii.gz
   scan_b/
      segmentation.nii.gz
      imaging.nii.gz
   scan_c/
      segmentation.nii.gz
      imaging.nii.gz

Above is the file structure of my dataset where segmentation.nii.gz and imaging.nii.gz are 3D NIFTI files with multiple slices. Also, I'm using code resembling the piece below to load the dataset.

interface = NIFTI_interface(channels=1, classes=2)
data_path = r".\ct_scans"
data_io = Data_IO(interface, data_path)

I'm especially worried about my training data loading because I had to change the slice_axis in the NIFTIslicer_interface init for the sample_data size to be correct while following this tutorial. When I run sample = data_io.sample_loader("scan_c", load_seg=True), I get a Sample of shape (H, W, N, C). N is my number of slices and my C = 1.

I found visualize_samples but I think it may be bugged. At this line, it tries to convert the sample to grayscale, even though my num of channels is 1, which leads to an IndexError. Here are the code and error respectively.

sample = data_io.sample_loader("scan_c", load_seg=True)
visualize_samples([sample], mask_seg=True, mask_pred=False)

Error:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2021.3.2\plugins\python-ce\helpers\pydev\pydevd.py", line 1483, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2021.3.2\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/nwogu/OneDrive/Documents/4mLab/MIScnn-UNet/data_io.py", line 39, in <module>
    visualize_samples([sample], mask_seg=True, mask_pred=False)
  File "C:\Users\nwogu\AppData\Local\Programs\Python\Python39\lib\site-packages\miscnn\utils\visualizer.py", line 154, in visualize_samples
    sample.img_data = normalize(sample.img_data, to_greyscale=True, normalize=True)
  File "C:\Users\nwogu\AppData\Local\Programs\Python\Python39\lib\site-packages\miscnn\utils\visualizer.py", line 99, in normalize
    return normalize_volume(sample, to_greyscale, normalize)
  File "C:\Users\nwogu\AppData\Local\Programs\Python\Python39\lib\site-packages\miscnn\utils\visualizer.py", line 48, in normalize_volume
    img[:, :, :] = vec_luminosity(img[:, :, :, 0], img[:, :, :, 1], img[:, :, :, 2])
IndexError: index 1 is out of bounds for axis 3 with size 1

I may be misusing it though, I am not sure. Please, let me know. :)

As an aside but kinda related, I tried writing my own visualize_samples but I could not find the definition of overlay_segmentation as used here.

emmanuel-nwogu avatar Apr 16 '23 12:04 emmanuel-nwogu

visualize_samples() is bugged as I came to find out from https://github.com/frankkramer-lab/MIScnn/issues/155. It does seem you are fixing it (https://github.com/frankkramer-lab/MIScnn/pull/156). Thanks :)

I believe there's another bug that may be missed though. I found this because my data is grayscaled so only one channel. This line in visualize_samples() has the to_greyscale parameter hardcoded as True. Unfortunately, this causes an IndexError here when the img_data is grayscale. Here's an example error dump where my img_data has the shape (370, 512, 256, 1) following the (N, H, W, C) format:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2021.3.2\plugins\python-ce\helpers\pydev\pydevd.py", line 1483, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2021.3.2\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/nwogu/OneDrive/Documents/4mLab/MIScnn-UNet/visualize_results.py", line 109, in <module>
    visualize_samples([sample], out_dir=evaluation_path, mask_seg=True, mask_pred=True,
  File "C:\Users\nwogu\AppData\Local\Programs\Python\Python39\lib\site-packages\miscnn\utils\visualizer.py", line 154, in visualize_samples
    sample.img_data = normalize(sample.img_data, to_greyscale=True, normalize=True)
  File "C:\Users\nwogu\AppData\Local\Programs\Python\Python39\lib\site-packages\miscnn\utils\visualizer.py", line 99, in normalize
    return normalize_volume(sample, to_greyscale, normalize)
  File "C:\Users\nwogu\AppData\Local\Programs\Python\Python39\lib\site-packages\miscnn\utils\visualizer.py", line 48, in normalize_volume
    img[:, :, :] = vec_luminosity(img[:, :, :, 0], img[:, :, :, 1], img[:, :, :, 2])
IndexError: index 1 is out of bounds for axis 3 with size 1
python-BaseException

cc @muellerdo

emmanuel-nwogu avatar May 10 '23 10:05 emmanuel-nwogu

Hey @emmanuel-nwogu,

good spot! Thanks!!

Philip (@Deathlymad) implemented the visualization and will have a look on it :)

Best Regards, Dominik

muellerdo avatar May 10 '23 14:05 muellerdo

afaik this should be fixed on the dev branch @muellerdo

Deathlymad avatar May 11 '23 19:05 Deathlymad