-
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
03ed82c
0235afe
edca43f
ee9f5cd
65c1161
7ffd22e
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_preferred_dicom_reader_key, | ||
| ) | ||
| from monai.data.meta_tensor import MetaTensor | ||
| from monai.data.utils import is_no_channel | ||
|
|
@@ -59,10 +61,15 @@ | |
| nrrd, _ = optional_import("nrrd") | ||
| FileLock, has_filelock = optional_import("filelock", name="FileLock") | ||
|
|
||
| __all__ = ["LoadImage", "SaveImage", "SUPPORTED_READERS"] | ||
| __all__ = ["LoadImage", "SaveImage", "SUPPORTED_READERS", "get_default_reader_registration_order"] | ||
|
|
||
| # Default readers for :py:class:`LoadImage`. Dict insertion order is the registration order | ||
| # (auto-select tries registered readers from last to first). Users may add custom readers here. | ||
| # DICOM readers are listed first so that, by default, ``itkreader`` is tried before the other | ||
| # DICOM readers; ``MONAI_DICOM_READER`` can promote a preferred DICOM reader to last (tried first). | ||
| SUPPORTED_READERS = { | ||
| "pydicomreader": PydicomReader, | ||
| "nvimgcodecpydicomreader": NvImgCodecPydicomReader, | ||
| "itkreader": ITKReader, | ||
| "nrrdreader": NrrdReader, | ||
| "numpyreader": NumpyReader, | ||
|
|
@@ -71,6 +78,22 @@ | |
| } | ||
|
|
||
|
|
||
| def get_default_reader_registration_order() -> list[str]: | ||
| """ | ||
| Return the default reader registration order for :py:class:`LoadImage`. | ||
|
|
||
| Uses :py:data:`SUPPORTED_READERS` insertion order so user-added entries are included. | ||
| If ``MONAI_DICOM_READER`` resolves to a non-empty preferred key present in | ||
| ``SUPPORTED_READERS``, that key is moved to the end of the list so auto-selection | ||
| tries it first. | ||
| """ | ||
| order = list(SUPPORTED_READERS) | ||
| preferred = get_preferred_dicom_reader_key() | ||
| if preferred and preferred in order: | ||
| order = [key for key in order if key != preferred] + [preferred] | ||
| return order | ||
|
|
||
|
|
||
| def switch_endianness(data, new="<"): | ||
| """ | ||
| Convert the input `data` endianness to `new`. | ||
|
|
@@ -116,7 +139,10 @@ 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 first among DICOM readers by default). | ||
| - Optionally set ``MONAI_DICOM_READER`` to ``itk``, ``pydicom``, or ``nvimgcodec`` | ||
| (GPU-accelerated decoding) to try that DICOM reader first. | ||
|
|
||
| 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 +211,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
Author
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. Made the changes per review comments: preserve the SUPPORTED_READERS, and added the function in this module to only shuffle the explicitly specified (via env var) DICOM reader to the end of the list. |
||
| try: | ||
| self.register(SUPPORTED_READERS[r](*args, **kwargs)) | ||
| except OptionalImportError: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.