-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add GPU-accelerated dcm image decoding #8954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
f7a384e
9c4d813
7eb485e
1aebb73
77ac541
423574f
6fbac5a
7fbef1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # Copyright (c) MONAI Consortium | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """ | ||
| MONAI integration helpers for the nvImageCodec pydicom decoder plugin. | ||
|
|
||
| The decoder implementation lives in ``nvidia.nvimgcodec.tools.dicom.pydicom_plugin`` | ||
| (shipped with ``nvidia-nvimgcodec-cuXX``). This module provides MONAI-facing helpers | ||
| and stable aliases for registration and availability checks. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
|
|
||
| from monai.utils import optional_import | ||
|
|
||
| cp, has_cp = optional_import("cupy") | ||
| pydicom_plugin, has_pydicom_plugin = optional_import("nvidia.nvimgcodec.tools.dicom.pydicom_plugin") | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
| if has_pydicom_plugin: | ||
| DECODER_DEPENDENCIES = pydicom_plugin.DECODER_DEPENDENCIES | ||
| NVIMGCODEC_MIN_VERSION = pydicom_plugin.NVIMGCODEC_MIN_VERSION | ||
| NVIMGCODEC_MIN_VERSION_TUPLE = pydicom_plugin.NVIMGCODEC_MIN_VERSION_TUPLE | ||
| NVIMGCODEC_PLUGIN_LABEL = pydicom_plugin.NVIMGCODEC_PLUGIN_LABEL | ||
| SUPPORTED_DECODER_CLASSES = pydicom_plugin.SUPPORTED_DECODER_CLASSES | ||
| SUPPORTED_TRANSFER_SYNTAXES = pydicom_plugin.SUPPORTED_TRANSFER_SYNTAXES | ||
| is_available = pydicom_plugin.is_available | ||
| else: # pragma: no cover - optional dependency not installed | ||
| DECODER_DEPENDENCIES = {} | ||
| NVIMGCODEC_MIN_VERSION = "0.8.0" | ||
| NVIMGCODEC_MIN_VERSION_TUPLE = (0, 8, 0) | ||
| NVIMGCODEC_PLUGIN_LABEL = "0.8.0+nvimgcodec" | ||
| SUPPORTED_DECODER_CLASSES = [] | ||
| SUPPORTED_TRANSFER_SYNTAXES = [] | ||
|
|
||
| def is_available(uid) -> bool: # type: ignore[no-redef] | ||
| return False | ||
|
|
||
|
|
||
| def is_nvimgcodec_available() -> bool: | ||
| """Return ``True`` if nvImageCodec with CUDA support is available.""" | ||
| if not has_pydicom_plugin or getattr(pydicom_plugin, "nvimgcodec", None) is None or not has_cp: | ||
| _logger.debug("nvimgcodec pydicom plugin, nvimgcodec module, or CuPy missing.") | ||
| return False | ||
| try: | ||
| if not cp.cuda.is_available(): | ||
| _logger.debug("CUDA device not found.") | ||
| return False | ||
| except Exception as exc: # pragma: no cover - environment specific | ||
| _logger.debug(f"CUDA availability check failed: {exc}") | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| def register_as_decoder_plugin(module_path: str | None = None) -> bool: | ||
| """Register the nvImageCodec pydicom decoder plugin.""" | ||
| if not is_nvimgcodec_available(): | ||
| _logger.warning("nvImageCodec is not available; skipping pydicom decoder plugin registration.") | ||
| return False | ||
| if not has_pydicom_plugin: | ||
| return False | ||
| return bool(pydicom_plugin.register(module_path)) | ||
|
|
||
|
|
||
| def unregister_as_decoder_plugin() -> bool: | ||
| """Unregister the nvImageCodec pydicom decoder plugin.""" | ||
| if not has_pydicom_plugin: | ||
| return False | ||
| return bool(pydicom_plugin.unregister()) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,8 +36,10 @@ | |
| NibabelReader, | ||
| NrrdReader, | ||
| NumpyReader, | ||
| NvImgCodecPydicomReader, | ||
| PILReader, | ||
| PydicomReader, | ||
| get_default_reader_registration_order, | ||
| ) | ||
| from monai.data.meta_tensor import MetaTensor | ||
| from monai.data.utils import is_no_channel | ||
|
|
@@ -63,6 +65,7 @@ | |
|
|
||
| SUPPORTED_READERS = { | ||
| "pydicomreader": PydicomReader, | ||
| "nvimgcodecpydicomreader": NvImgCodecPydicomReader, | ||
| "itkreader": ITKReader, | ||
| "nrrdreader": NrrdReader, | ||
| "numpyreader": NumpyReader, | ||
|
|
@@ -116,7 +119,9 @@ class LoadImage(Transform): | |
| - User-specified reader in the constructor of `LoadImage`. | ||
| - Readers from the last to the first in the registered list. | ||
| - Current default readers: (nii, nii.gz -> NibabelReader), (png, jpg, bmp -> PILReader), | ||
| (npz, npy -> NumpyReader), (nrrd -> NrrdReader), (DICOM file -> ITKReader). | ||
| (npz, npy -> NumpyReader), (nrrd -> NrrdReader), (DICOM file -> ITKReader by default). | ||
| - The default DICOM reader can be changed with the ``MONAI_DICOM_READER`` environment variable. | ||
| Supported values are ``itk`` (default), ``pydicom``, and ``nvimgcodec`` (GPU-accelerated decoding). | ||
|
|
||
| Please note that for png, jpg, bmp, and other 2D formats, readers by default swap axis 0 and 1 after | ||
| loading the array with ``reverse_indexing`` set to ``True`` because the spatial axes definition | ||
|
|
@@ -185,7 +190,7 @@ def __init__( | |
| self.expanduser = expanduser | ||
|
|
||
| self.readers: list[ImageReader] = [] | ||
| for r in SUPPORTED_READERS: # set predefined readers as default | ||
| for r in get_default_reader_registration_order(): # set predefined readers as default | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this change |
||
| try: | ||
| self.register(SUPPORTED_READERS[r](*args, **kwargs)) | ||
| except OptionalImportError: | ||
|
|
@@ -258,22 +263,21 @@ def __call__(self, filename: Sequence[PathLike] | PathLike, reader: ImageReader | |
| img = reader.read(filename) # runtime specified reader | ||
| else: | ||
| for reader in self.readers[::-1]: | ||
| if self.auto_select: # rely on the filename extension to choose the reader | ||
| if reader.verify_suffix(filename): | ||
| img = reader.read(filename) | ||
| break | ||
| else: # try the user designated readers | ||
| try: | ||
| img = reader.read(filename) | ||
| except Exception as e: | ||
| err.append(traceback.format_exc()) | ||
| logging.getLogger(self.__class__.__name__).debug(e, exc_info=True) | ||
| logging.getLogger(self.__class__.__name__).info( | ||
| f"{reader.__class__.__name__}: unable to load {filename}.\n" | ||
| ) | ||
| else: | ||
| err = [] | ||
| break | ||
| # Unified reader selection so auto-select also catches read failures and tries the next reader | ||
| # (same as the explicit-reader path) | ||
| if self.auto_select and not reader.verify_suffix(filename): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the original intent of the code this replaced was to fallback to the |
||
| continue | ||
| try: | ||
| img = reader.read(filename) | ||
| except Exception as e: | ||
| err.append(traceback.format_exc()) | ||
| logging.getLogger(self.__class__.__name__).debug(e, exc_info=True) | ||
| logging.getLogger(self.__class__.__name__).info( | ||
| f"{reader.__class__.__name__}: unable to load {filename}.\n" | ||
| ) | ||
| else: | ||
| err = [] | ||
| break | ||
|
|
||
| if img is None or reader is None: | ||
| if isinstance(filename, Sequence) and len(filename) == 1: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.