ivadomed / model_seg_mouse-sc_wm-gm_t1

White and grey matter segmentation on T1-weighted exvivo mouse spinal cord
MIT License
2 stars 1 forks source link

Do proper 2D slice training from 3D volumes #3

Closed jcohenadad closed 1 year ago

jcohenadad commented 1 year ago

See this tutorial, notably the 'squeeze dim', which I am currently not doing.

Useful links:

With the current code, I am able to get 2D patches, only those that contain a non-empty label slice: image

jcohenadad commented 1 year ago

However, when trying , it does not select slices with labels anymore. So, I'm trying to figure out how patches are organized.

When removing intensity scaling, and batch_size=1, I can figure out that each patch corresponds to a z-slice, and the [0,0] voxel is identified:

image

jcohenadad commented 1 year ago

Working prototype at 66bdb42332e4cda5b10996ce96e838c40a9fc4db using PatchDataset and custom patch_func

jcohenadad commented 1 year ago

Alternative approach in 375bdf53ddbc8c5ef7b7544403b64122946d7565

Code ```python # Iterate across image/label 3D volume, fetch non-empty slice and output a single list of image/label pair patch_data = [] for data_dict in data_dicts: # i=1 nii_image = load(data_dict['image']) nii_label = load(data_dict['label']) for i_z in range(nii_label.shape[2]): image_z = nii_image.get_fdata()[:, :, i_z] label_z = nii_label.get_fdata()[:, :, i_z] if label_z.sum() > 0: patch_data.append({'image': image_z, 'label': label_z}) def patch_func(dataset): """Dummy function to output a sequence of dataset of length 1""" return [dataset] ds = PatchDataset(data=patch_data, patch_func=patch_func, samples_per_image=1, transform=None) check_loader = DataLoader(ds, batch_size=1) check_data = first(check_loader) # TODO: split train/validation ```

Working code in https://github.com/ivadomed/model_seg_mouse-sc_wm-gm_t1/pull/1 🎉

jcohenadad commented 1 year ago

Alternative approach suggested in https://github.com/Project-MONAI/MONAI/discussions/5948#discussioncomment-5330042

Code: https://github.com/ivadomed/model_seg_mouse-sc_wm-gm_t1/blob/9d7ba1bf32704061c252ae5b5abf074b4c780b6d/train.py#L140-L176

Error message ```console Traceback (most recent call last): File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/transforms/transform.py", line 103, in apply_transform return _apply_transform(transform, data, unpack_items) File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/transforms/transform.py", line 67, in _apply_transform return transform(parameters) File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/transforms/croppad/dictionary.py", line 880, in __call__ self.randomize(d.get(self.label_key), fg_indices, bg_indices, d.get(self.image_key)) File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/transforms/croppad/dictionary.py", line 868, in randomize self.cropper.randomize(label=label, fg_indices=fg_indices, bg_indices=bg_indices, image=image) File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/transforms/croppad/array.py", line 1027, in randomize self.centers = generate_pos_neg_label_crop_centers( File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/transforms/utils.py", line 525, in generate_pos_neg_label_crop_centers centers.append(correct_crop_centers(center, spatial_size, label_spatial_shape, allow_smaller)) File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/transforms/utils.py", line 446, in correct_crop_centers spatial_size = fall_back_tuple(spatial_size, default=label_spatial_shape) File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/utils/misc.py", line 234, in fall_back_tuple user = ensure_tuple_rep(user_provided, ndim) File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/utils/misc.py", line 191, in ensure_tuple_rep raise ValueError(f"Sequence must have length {dim}, got {len(tup)}.") ValueError: Sequence must have length 2, got 3. The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 628, in __next__ data = self._next_data() File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 671, in _next_data data = self._dataset_fetcher.fetch(index) # may raise StopIteration File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py", line 58, in fetch data = [self.dataset[idx] for idx in possibly_batched_index] File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py", line 58, in data = [self.dataset[idx] for idx in possibly_batched_index] File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/data/dataset.py", line 110, in __getitem__ return self._transform(index) File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/data/grid_dataset.py", line 270, in _transform patches = self.patch_func(image) File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/transforms/compose.py", line 177, in __call__ input_ = apply_transform(_transform, input_, self.map_items, self.unpack_items, self.log_stats) File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/transforms/transform.py", line 130, in apply_transform raise RuntimeError(f"applying transform {transform}") from e RuntimeError: applying transform ```

While debugging, I can see the error is related to a mismatch between the extracted label and the input size: image

jcohenadad commented 1 year ago

Trying https://github.com/Project-MONAI/MONAI/discussions/5948#discussioncomment-5346854 in 5107b4181a018358e43b34f1189819a6ce042b88, getting:

Traceback (most recent call last):
  File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/transforms/transform.py", line 103, in apply_transform
    return _apply_transform(transform, data, unpack_items)
  File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/transforms/transform.py", line 67, in _apply_transform
    return transform(parameters)
  File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/transforms/utility/array.py", line 262, in __call__
    raise ValueError(msg)
ValueError: Metadata not available and channel_dim=None, EnsureChannelFirst is not in use.

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
  File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/utils/misc.py", line 105, in first
    for i in iterable:
  File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 628, in __next__
    data = self._next_data()
  File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 671, in _next_data
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
  File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py", line 58, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py", line 58, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/data/dataset.py", line 110, in __getitem__
    return self._transform(index)
  File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/data/grid_dataset.py", line 270, in _transform
    patches = self.patch_func(image)
  File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/transforms/compose.py", line 177, in __call__
    input_ = apply_transform(_transform, input_, self.map_items, self.unpack_items, self.log_stats)
  File "/Users/julien/code/model_seg_mouse-sc_wm-gm_t1/venv/lib/python3.10/site-packages/monai/transforms/transform.py", line 130, in apply_transform
    raise RuntimeError(f"applying transform {transform}") from e
RuntimeError: applying transform <monai.transforms.utility.array.EnsureChannelFirst object at 0x7f9355f669e0>
jcohenadad commented 1 year ago

Issue was fixed in 4042dc744c74c08b0e05976b513b1bb8c1b1de50 (thank you @KumoLiu):

jcohenadad commented 1 year ago

Can close