[itk-python] Create an image with GPU

I used pip to install itk5.4.0, and running some demo about RTK. There was normal when I running FirstReconstruction.py, but there was an error when running FirstCudaReconstruction.py. Both using itk.GPUImage and itk.RTK.CudaImage will result in errors.
AttributeError: module ‘itk’ has no attribute ‘GPUImage’
AttributeError: module ‘RTK’ has no attribute ‘CudaImage’
So I want to know how can I create an image with GPU.
Any help would be appreciated.

CudaImage is part of CudaCommon

pip install itk-cudacommon-cuda116

RTK is released in a non-cuda version (itk-rtk) and in a Cuda 11.6 version (itk-rtk-cuda116).

The current releases of the two cuda packages (RTK v2.5 and CudaCommon v1.0.1) are compatible with ITK v5.4rc01. There is no version compatible with ITK v5.4rc02 because the gcc compiler has been upgraded and the new version requires Cuda v12.

CudaCommon v1.0.0 and RTK v2.4.1 is compatible with ITK v5.3.0.

1 Like

Thank you for your reply.
Does This means only the version of cuda is greater than 11.6 can be used on Python? There seems to be no requirement for the cuda version on C++.

This means that we provide compiled binaries for Cuda 11.6 only. But other versions (except for Cuda 12 currently) can be used if you compile it yourself.

Thank you for your reply. It’s really helpful for me.

As your suggest, I pip itk-rtk-cuda116-1.01 and itk-cudacomman-cuda116-2.5.0 on my project. And run FirstCudaReconstruction.py, new error is happended :sob:.

Traceback (most recent call last):
  File "C:\Users\zhangming\Desktop\reconstructionSoft\preProcess\FirstCudaReconstruction.py", line 17, in <module>
    GPUImageType = itk.RTK.CudaImage[itk.F,3]
  File "D:\anaconda\envs\CTr\lib\site-packages\itk\support\lazy.py", line 138, in __getattribute__
    base.itk_load_swig_module(module, namespace)
  File "D:\anaconda\envs\CTr\lib\site-packages\itk\support\base.py", line 96, in itk_load_swig_module
    itk_load_swig_module(dep, namespace)
  File "D:\anaconda\envs\CTr\lib\site-packages\itk\support\base.py", line 96, in itk_load_swig_module
    itk_load_swig_module(dep, namespace)
  File "D:\anaconda\envs\CTr\lib\site-packages\itk\support\base.py", line 241, in itk_load_swig_module
    load_module_needed_factories(name)
  File "D:\anaconda\envs\CTr\lib\site-packages\itk\support\base.py", line 392, in load_module_needed_factories
    load_factories(needed_factories[name])
  File "D:\anaconda\envs\CTr\lib\site-packages\itk\support\base.py", line 369, in load_factories
    itk_load_swig_module(module_name, namespace)
  File "D:\anaconda\envs\CTr\lib\site-packages\itk\support\base.py", line 96, in itk_load_swig_module
    itk_load_swig_module(dep, namespace)
  File "D:\anaconda\envs\CTr\lib\site-packages\itk\support\base.py", line 132, in itk_load_swig_module
    l_module = loader.load(swig_module_name)
  File "D:\anaconda\envs\CTr\lib\site-packages\itk\support\base.py", line 291, in load
    l_spec.loader.exec_module(l_module)  # pytype: disable=attribute-error
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "D:\anaconda\envs\CTr\lib\site-packages\itk\support\..\CudaCommonPython.py", line 13, in <module>
    from . import _CudaCommonPython
ImportError: DLL load failed while importing _CudaCommonPython: The specified module could not be found。

I tried uninstall itk-cudacommon-cuda116 and install it again as suggest on the Internet. But the problem has not been solved.

Can you provide the list of installed packages with python -m pip list? To check that you have ITK 5.4rc1, CudaCommon 1.0.1 and RTK 2.5.0.

I would also check for DLL dependencies of _CudaCommonPython.

This is my list of installed package. It seems that I have the correct verison of itk.

Package Version


certifi 2021.10.8
click 8.1.7
colorama 0.4.6
contourpy 1.2.0
cycler 0.12.1
fonttools 4.47.2
importlib-resources 6.1.1
itk 5.4rc1
itk-core 5.4rc1
itk-cudacommon-cuda116 1.0.1
itk-filtering 5.4rc1
itk-io 5.4rc1
itk-numerics 5.4rc1
itk-registration 5.4rc1
itk-rtk-cuda116 2.5.0
itk-segmentation 5.4rc1
kiwisolver 1.4.5
matplotlib 3.8.2
numpy 1.26.3
opencv-python 4.9.0.80
packaging 23.2
pillow 10.2.0
pip 21.2.4
pyparsing 3.1.1
PyQt5 5.15.9
pyqt5-plugins 5.15.9.2.3
PyQt5-Qt5 5.15.2
PyQt5-sip 12.13.0
pyqt5-tools 5.15.9.3.3
python-dateutil 2.8.2
python-dotenv 1.0.1
qt5-applications 5.15.2.2.3
qt5-tools 5.15.2.1.3
scipy 1.12.0
setuptools 58.0.4
six 1.16.0
vtk 9.3.0
wheel 0.37.0
wincertstore 0.2
zipp 3.17.0

Yes indeed. Have you installed Cuda 11.6? There are a number of Cuda DLL that are striped out of the Python package because they are part of Cuda 11.6, see here. A DLL dependency tool would allow you to verify that all depencies of the CudaCommon Python DLL are available.

Yes I have installed cuda with version 11.6.

C:\Users\zhangming>nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Fri_Dec_17_18:28:54_Pacific_Standard_Time_2021
Cuda compilation tools, release 11.6, V11.6.55
Build cuda_11.6.r11.6/compiler.30794723_0

Is the CUDA_PATH env variable set to this Cuda? Because FirstCudaReconstruction uses this to set the dll path

I installed cuda11.0 on my computer before, but I uninstalled and reinstalled 11.6. I’m not sure if it’s a configuration issue. The path returned by OS is still 11.0. I tried again after adding the new path, but still got the same error message.

if sys.platform == 'win32':
  # os.add_dll_directory(os.path.join(os.environ['CUDA_PATH'], 'bin'))
  os.add_dll_directory(os.path.join(r'C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6', 'bin'))

Have you checked the DLL dependencies of the CudaCommon Python DLL?

The problem was solved by setting the path returned by os.environ[‘CUDA_PATH’] to be the path of CUDA11.6. Thank you very much for your answer.

2 Likes