# \[itk-python\] Create an image with GPU

**URL:** https://discourse.itk.org/t/itk-python-create-an-image-with-gpu/6414
**Category:** Algorithms
**Tags:** itk, python
**Created:** [January 26, 2024, 12:55am UTC](https://discourse.itk.org/t/itk-python-create-an-image-with-gpu/6414 "2024-01-26T00:55:34Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![zhangm](https://discourse.itk.org/letter_avatar_proxy/v4/letter/z/34f0e0/32.png) [@zhangm](https://discourse.itk.org/u/zhangm)
#### Post date: [January 26, 2024, 12:55am UTC](https://discourse.itk.org/t/itk-python-create-an-image-with-gpu/6414/1 "2024-01-26T00:55:34Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)
#### Post date: [January 26, 2024, 10:12am UTC](https://discourse.itk.org/t/itk-python-create-an-image-with-gpu/6414/2 "2024-01-26T10:12:14Z")

</div>

CudaImage is part of CudaCommon

```auto
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.

---

<div class="post-metadata">

### Author: ![zhangm](https://discourse.itk.org/letter_avatar_proxy/v4/letter/z/34f0e0/32.png) [@zhangm](https://discourse.itk.org/u/zhangm)
#### Post date: [January 27, 2024, 12:42am UTC](https://discourse.itk.org/t/itk-python-create-an-image-with-gpu/6414/3 "2024-01-27T00:42:27Z")

</div>

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++.

---

<div class="post-metadata">

### Author: ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)
#### Post date: [January 28, 2024, 9:00pm UTC](https://discourse.itk.org/t/itk-python-create-an-image-with-gpu/6414/4 "2024-01-28T21:00:31Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![zhangm](https://discourse.itk.org/letter_avatar_proxy/v4/letter/z/34f0e0/32.png) [@zhangm](https://discourse.itk.org/u/zhangm)
#### Post date: [January 29, 2024, 12:37am UTC](https://discourse.itk.org/t/itk-python-create-an-image-with-gpu/6414/5 "2024-01-29T00:37:08Z")

</div>

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

---

<div class="post-metadata">

### Author: ![zhangm](https://discourse.itk.org/letter_avatar_proxy/v4/letter/z/34f0e0/32.png) [@zhangm](https://discourse.itk.org/u/zhangm)
#### Post date: [January 29, 2024, 3:07am UTC](https://discourse.itk.org/t/itk-python-create-an-image-with-gpu/6414/6 "2024-01-29T03:07:45Z")

</div>

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 😭.

```auto
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.

---

<div class="post-metadata">

### Author: ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)
#### Post date: [January 29, 2024, 6:02am UTC](https://discourse.itk.org/t/itk-python-create-an-image-with-gpu/6414/7 "2024-01-29T06:02:16Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![zhangm](https://discourse.itk.org/letter_avatar_proxy/v4/letter/z/34f0e0/32.png) [@zhangm](https://discourse.itk.org/u/zhangm)
#### Post date: [January 29, 2024, 6:08am UTC](https://discourse.itk.org/t/itk-python-create-an-image-with-gpu/6414/8 "2024-01-29T06:08:40Z")

</div>

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

---

<div class="post-metadata">

### Author: ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)
#### Post date: [January 29, 2024, 6:29am UTC](https://discourse.itk.org/t/itk-python-create-an-image-with-gpu/6414/9 "2024-01-29T06:29:09Z")

</div>

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](https://github.com/RTKConsortium/ITKCudaCommon/blob/master/.github/workflows/build-test-package-python-cuda.yml#L52). A DLL dependency tool would allow you to verify that all depencies of the CudaCommon Python DLL are available.

---

<div class="post-metadata">

### Author: ![zhangm](https://discourse.itk.org/letter_avatar_proxy/v4/letter/z/34f0e0/32.png) [@zhangm](https://discourse.itk.org/u/zhangm)
#### Post date: [January 29, 2024, 6:35am UTC](https://discourse.itk.org/t/itk-python-create-an-image-with-gpu/6414/10 "2024-01-29T06:35:28Z")

</div>

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

---

<div class="post-metadata">

### Author: ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)
#### Post date: [January 29, 2024, 6:41am UTC](https://discourse.itk.org/t/itk-python-create-an-image-with-gpu/6414/11 "2024-01-29T06:41:14Z")

</div>

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

> <https://github.com/RTKConsortium/RTK/blob/master/examples/FirstReconstruction/FirstCudaReconstruction.py#L14>

---

<div class="post-metadata">

### Author: ![zhangm](https://discourse.itk.org/letter_avatar_proxy/v4/letter/z/34f0e0/32.png) [@zhangm](https://discourse.itk.org/u/zhangm)
#### Post date: [January 29, 2024, 7:01am UTC](https://discourse.itk.org/t/itk-python-create-an-image-with-gpu/6414/12 "2024-01-29T07:01:07Z")

</div>

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.

```auto
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'))

```

---

<div class="post-metadata">

### Author: ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)
#### Post date: [January 29, 2024, 7:02am UTC](https://discourse.itk.org/t/itk-python-create-an-image-with-gpu/6414/13 "2024-01-29T07:02:27Z")

</div>

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

---

<div class="post-metadata">

### Author: ![zhangm](https://discourse.itk.org/letter_avatar_proxy/v4/letter/z/34f0e0/32.png) [@zhangm](https://discourse.itk.org/u/zhangm)
#### Post date: [January 29, 2024, 10:04am UTC](https://discourse.itk.org/t/itk-python-create-an-image-with-gpu/6414/14 "2024-01-29T10:04:53Z")

</div>

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.
