Project-MONAI / tutorials

MONAI Tutorials
https://monai.io/started.html
Apache License 2.0
1.85k stars 682 forks source link

KeyError in "reconstruction/MRI_reconstruction/unet_demo/inference.ipynb" #1670

Closed KumoLiu closed 7 months ago

KumoLiu commented 7 months ago
{
    "name": "RuntimeError",
    "message": "applying transform <monai.transforms.compose.Compose object at 0x7f07003fabc0>",
    "stack": "---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File /usr/local/lib/python3.10/dist-packages/monai/transforms/transform.py:141, in apply_transform(transform, data, map_items, unpack_items, log_stats, lazy, overrides)
    140         return [_apply_transform(transform, item, unpack_items, lazy, overrides, log_stats) for item in data]
--> 141     return _apply_transform(transform, data, unpack_items, lazy, overrides, log_stats)
    142 except Exception as e:
    143     # if in debug mode, don't swallow exception so that the breakpoint
    144     # appears where the exception was raised.

File /usr/local/lib/python3.10/dist-packages/monai/transforms/transform.py:98, in _apply_transform(transform, data, unpack_parameters, lazy, overrides, logger_name)
     96     return transform(*data, lazy=lazy) if isinstance(transform, LazyTrait) else transform(*data)
---> 98 return transform(data, lazy=lazy) if isinstance(transform, LazyTrait) else transform(data)

File /usr/local/lib/python3.10/dist-packages/monai/apps/reconstruction/transforms/dictionary.py:62, in ExtractDataKeyFromMetaKeyd.__call__(self, data)
     61 for key in self.keys:
---> 62     if key in d[self.meta_key]:
     63         d[key] = d[self.meta_key][key]  # type: ignore

KeyError: 'kspace_meta_dict'

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
File /usr/local/lib/python3.10/dist-packages/monai/transforms/transform.py:141, in apply_transform(transform, data, map_items, unpack_items, log_stats, lazy, overrides)
    140         return [_apply_transform(transform, item, unpack_items, lazy, overrides, log_stats) for item in data]
--> 141     return _apply_transform(transform, data, unpack_items, lazy, overrides, log_stats)
    142 except Exception as e:
    143     # if in debug mode, don't swallow exception so that the breakpoint
    144     # appears where the exception was raised.

File /usr/local/lib/python3.10/dist-packages/monai/transforms/transform.py:98, in _apply_transform(transform, data, unpack_parameters, lazy, overrides, logger_name)
     96     return transform(*data, lazy=lazy) if isinstance(transform, LazyTrait) else transform(*data)
---> 98 return transform(data, lazy=lazy) if isinstance(transform, LazyTrait) else transform(data)

File /usr/local/lib/python3.10/dist-packages/monai/transforms/compose.py:335, in Compose.__call__(self, input_, start, end, threading, lazy)
    334 _lazy = self._lazy if lazy is None else lazy
--> 335 result = execute_compose(
    336     input_,
    337     transforms=self.transforms,
    338     start=start,
    339     end=end,
    340     map_items=self.map_items,
    341     unpack_items=self.unpack_items,
    342     lazy=_lazy,
    343     overrides=self.overrides,
    344     threading=threading,
    345     log_stats=self.log_stats,
    346 )
    348 return result

File /usr/local/lib/python3.10/dist-packages/monai/transforms/compose.py:111, in execute_compose(data, transforms, map_items, unpack_items, start, end, lazy, overrides, threading, log_stats)
    110         _transform = deepcopy(_transform) if isinstance(_transform, ThreadUnsafe) else _transform
--> 111     data = apply_transform(
    112         _transform, data, map_items, unpack_items, lazy=lazy, overrides=overrides, log_stats=log_stats
    113     )
    114 data = apply_pending_transforms(data, None, overrides, logger_name=log_stats)

File /usr/local/lib/python3.10/dist-packages/monai/transforms/transform.py:171, in apply_transform(transform, data, map_items, unpack_items, log_stats, lazy, overrides)
    170         _log_stats(data=data)
--> 171 raise RuntimeError(f\"applying transform {transform}\") from e

RuntimeError: applying transform <monai.apps.reconstruction.transforms.dictionary.ExtractDataKeyFromMetaKeyd object at 0x7f07003fbdf0>

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
Cell In[7], line 6
      4 val_ssim = []
      5 step = 1
----> 6 for test_data in test_loader:
      7     input, target, mean, std, fname = (
      8         test_data[\"kspace_masked_ifft\"],
      9         test_data[\"reconstruction_rss\"],
   (...)
     12         test_data[\"kspace_meta_dict\"][\"filename\"],
     13     )
     15     # iterate through all slices:

File /usr/local/lib/python3.10/dist-packages/torch/utils/data/dataloader.py:631, in _BaseDataLoaderIter.__next__(self)
    628 if self._sampler_iter is None:
    629     # TODO(https://github.com/pytorch/pytorch/issues/76750)
    630     self._reset()  # type: ignore[call-arg]
--> 631 data = self._next_data()
    632 self._num_yielded += 1
    633 if self._dataset_kind == _DatasetKind.Iterable and \\
    634         self._IterableDataset_len_called is not None and \\
    635         self._num_yielded > self._IterableDataset_len_called:

File /usr/local/lib/python3.10/dist-packages/torch/utils/data/dataloader.py:675, in _SingleProcessDataLoaderIter._next_data(self)
    673 def _next_data(self):
    674     index = self._next_index()  # may raise StopIteration
--> 675     data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    676     if self._pin_memory:
    677         data = _utils.pin_memory.pin_memory(data, self._pin_memory_device)

File /usr/local/lib/python3.10/dist-packages/torch/utils/data/_utils/fetch.py:51, in _MapDatasetFetcher.fetch(self, possibly_batched_index)
     49         data = self.dataset.__getitems__(possibly_batched_index)
     50     else:
---> 51         data = [self.dataset[idx] for idx in possibly_batched_index]
     52 else:
     53     data = self.dataset[possibly_batched_index]

File /usr/local/lib/python3.10/dist-packages/torch/utils/data/_utils/fetch.py:51, in <listcomp>(.0)
     49         data = self.dataset.__getitems__(possibly_batched_index)
     50     else:
---> 51         data = [self.dataset[idx] for idx in possibly_batched_index]
     52 else:
     53     data = self.dataset[possibly_batched_index]

File /usr/local/lib/python3.10/dist-packages/monai/data/dataset.py:112, in Dataset.__getitem__(self, index)
    109 if isinstance(index, collections.abc.Sequence):
    110     # dataset[[1, 3, 4]]
    111     return Subset(dataset=self, indices=index)
--> 112 return self._transform(index)

File /usr/local/lib/python3.10/dist-packages/monai/data/dataset.py:916, in CacheDataset._transform(self, index)
    912     cache_index = index
    914 if cache_index is None:
    915     # no cache for this index, execute all the transforms directly
--> 916     return super()._transform(index)
    918 if self._cache is None:
    919     raise RuntimeError(\"cache buffer is not initialized, please call `set_data()` first.\")

File /usr/local/lib/python3.10/dist-packages/monai/data/dataset.py:98, in Dataset._transform(self, index)
     94 \"\"\"
     95 Fetch single data item from `self.data`.
     96 \"\"\"
     97 data_i = self.data[index]
---> 98 return apply_transform(self.transform, data_i) if self.transform is not None else data_i

File /usr/local/lib/python3.10/dist-packages/monai/transforms/transform.py:171, in apply_transform(transform, data, map_items, unpack_items, log_stats, lazy, overrides)
    169     else:
    170         _log_stats(data=data)
--> 171 raise RuntimeError(f\"applying transform {transform}\") from e

RuntimeError: applying transform <monai.transforms.compose.Compose object at 0x7f07003fabc0>"
}