# Speed up the reconstruction

**URL:** https://discourse.itk.org/t/speed-up-the-reconstruction/6599
**Category:** Algorithms
**Tags:** python, rtk
**Created:** [April 26, 2024, 7:53am UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599 "2024-04-26T07:53:15Z")
**Posts on this page:** 20
**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: [April 26, 2024, 7:53am UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/1 "2024-04-26T07:53:16Z")

</div>

I use CudaFDK to reconstruction my data, the speed is much faster than using CPU. But I still want to try and make this time shorter. I use Profile on pyCharm to print the time spent by the process like below:

 ![20240426170737](https://discourse.itk.org/uploads/default/original/2X/7/75c0dce496ea81ba2f8e95c558debf2327786117.png)

Is there any way to speed up the progress? I found here that `itk.MultiThreaderBase` can achieve multithreading, but I don’t know how to apply it here.  
Any reply will be appreciate.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 26, 2024, 12:59pm UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/2 "2024-04-26T12:59:58Z")

</div>

GPU classes are parallel by their very nature. The only other significant function is `create_module`, taking 32 seconds. This might be the loading and import of all the Python modules. If you have `itkConfig.LazyLoading = False` in your code, try removing it or setting it to `True`.

---

<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: [April 28, 2024, 1:05am UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/3 "2024-04-28T01:05:12Z")

</div>

Thank you for your reply.  
I didn’t use `itkConfig.LazyLoading = False` on my code. To prevent default settings, I set `itkConfig.LazyLoading = True` before the code starts. But the speed did not improve.  
The reason may be that I use `from itk import XXX` not `import itk` that have the same effect with LazyLoding to prevent loading all modules?

---

<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: [May 2, 2024, 5:17am UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/4 "2024-05-02T05:17:37Z")

</div>

Most of the time is spent in GPUrec. You may want to optimize [m\_ProjectionsSubsetSize](http://www.openrtk.org/Doxygen/classrtk_1_1FDKConeBeamReconstructionFilter.html#a6f7158a175fa856a302ef1790cf18c82). I don’t see many other solutions, except maybe the [block size in Cuda](https://github.com/RTKConsortium/RTK/blob/master/src/rtkCudaFDKBackProjectionImageFilter.cu#L110-L113) but this is currently hard-coded. Don’t hesitate to report positive or negative results, this is interesting to us too!

---

<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: [May 6, 2024, 7:33am UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/5 "2024-05-06T07:33:41Z")

</div>

Thank you for your reply, I’ll try to change the value of m\_ProjectionSubsetSize  
During these times, a lot of time was spent on the first call to RTK as rtk.CudaImage.

> [@Speed up the reconstruction](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/2):
>
> GPU classes are parallel by their very nature. The only other significant function is create\_module, taking 32 seconds. This might be the loading and import of all the Python modules. If you have itkConfig.LazyLoading = False in your code, try removing it or setting it to True.

According to the suggestion, I set itkConfig.LazyLoading = True, but it didn’t work. Is there any better way to reduce this time?

---

<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: [May 6, 2024, 9:38am UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/6 "2024-05-06T09:38:24Z")

</div>

ITK’s package loading time is notoriously long. I don’t have a practical solution. One solution is to reduce the number of types at compilation time with the CMake variables `ITK_WRAP_*`, e.g., no double but float only, 3D and 4D only, etc. But I don’t have a set of predefined variables which would work for RTK and (most likely) many configurations are not supported.

---

<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: [May 6, 2024, 9:50am UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/7 "2024-05-06T09:50:59Z")

</div>

Thank you for your advice.  
In this case, the second run time will be reduced. Because it is not necessary to find all modules during the second run.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [May 6, 2024, 12:34pm UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/8 "2024-05-06T12:34:22Z")

</div>

Usual ITK loading time is 3-8 seconds, so 30 seconds is way too excessive. I have no further suggestions about how to reduce it.

---

<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: [May 7, 2024, 3:36am UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/9 "2024-05-07T03:36:21Z")

</div>

When I use rtk there will appear the error like this:  
 ![20240507105910](https://discourse.itk.org/uploads/default/original/2X/1/12cff57300bd5eede8bd1f6128b79ad3297fe742.png)  
But this does not affect the subsequent code execution. Is this the reason for the long time to loading rtk?  
My pip list is like below:

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

```

---

<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: [May 7, 2024, 12:09pm UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/10 "2024-05-07T12:09:36Z")

</div>

> [@dzenanz](#):
>
> Usual ITK loading time is 3-8 seconds, so 30 seconds is way too excessive. I have no further suggestions about how to reduce it.

This is not my experience but it really depends on what you use. The following code on my (recent) laptop

```auto
import time

def custom_callback(name, progress):
    global mod_start_time
    if progress == 0:
        mod_start_time = time.time()
    if progress == 1:
        t = time.time() - mod_start_time
        print(f"Loaded {name} in {t:.2f} s.")

import itkConfig
itkConfig.ImportCallback = custom_callback
from itk import RTK as rtk
start_time = time.time()
rtk.ThreeDCircularProjectionGeometry.New()
print("--- %s seconds ---" % (time.time() - start_time))

```

gives

```auto
Loaded ITKPyBase in 0.24 s.
Loaded ITKCommon in 0.51 s.
Loaded ITKImageSources in 0.04 s.
Loaded ITKStatistics in 0.11 s.
Loaded ITKImageFilterBase in 0.68 s.
Loaded ITKTransform in 0.11 s.
Loaded ITKImageFunction in 0.09 s.
Loaded ITKImageGrid in 0.79 s.
Loaded ITKFFT in 0.40 s.
Loaded ITKMesh in 0.19 s.
Loaded ITKSpatialObjects in 0.09 s.
Loaded ITKImageCompose in 0.07 s.
Loaded ITKImageStatistics in 0.46 s.
Loaded ITKPath in 0.02 s.
Loaded ITKImageIntensity in 5.54 s.
Loaded ITKThresholding in 0.79 s.
Loaded ITKConvolution in 0.08 s.
Loaded ITKSmoothing in 0.19 s.
Loaded ITKOptimizers in 0.02 s.
Loaded ITKImageGradient in 0.16 s.
Loaded ITKImageFeature in 0.32 s.
Loaded ITKFiniteDifference in 0.12 s.
Loaded ITKDisplacementField in 0.06 s.
Loaded ITKRegistrationCommon in 0.27 s.
Loaded ITKImageNoise in 0.28 s.
Loaded ITKIOBMP in 0.00 s.
Loaded ITKIOBioRad in 0.00 s.
Loaded ITKIOBruker in 0.00 s.
Loaded ITKIOGDCM in 0.01 s.
Loaded ITKIOIPL in 0.00 s.
Loaded ITKIOGE in 0.00 s.
Loaded ITKIOGIPL in 0.00 s.
Loaded ITKIOHDF5 in 0.00 s.
Loaded ITKIOJPEG in 0.00 s.
Loaded ITKIOJPEG2000 in 0.00 s.
Loaded ITKIOTIFF in 0.00 s.
Loaded ITKIOLSM in 0.00 s.
Loaded ITKIOMINC in 0.00 s.
Loaded ITKIOMRC in 0.00 s.
Loaded ITKIOMeta in 0.00 s.
Loaded ITKIONIFTI in 0.00 s.
Loaded ITKIONRRD in 0.00 s.
Loaded ITKIOPNG in 0.00 s.
Loaded ITKIOStimulate in 0.00 s.
Loaded ITKIOVTK in 0.00 s.
Loaded ITKIORAW in 0.01 s.
Loaded ITKBridgeNumPy in 0.03 s.
Loaded RTK in 3.60 s.
Loaded ITKIOImageBase in 3.60 s.
--- 15.493510484695435 seconds ---

```

if the modules have already been read from disk (it’s longer when it’s not in cache). \>15 s is excessive, I agree!

---

<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: [May 7, 2024, 12:11pm UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/11 "2024-05-07T12:11:18Z")

</div>

> [@zhangm](#):
>
> When I use rtk there will appear the error like this:  
> ![20240507105910](https://discourse.itk.org/uploads/default/original/2X/1/12cff57300bd5eede8bd1f6128b79ad3297fe742.png)  
> But this does not affect the subsequent code execution. Is this the reason for the long time to loading rtk?

I have never encountered this issue. Does it occur with a fresh install in a separate python environment?

---

<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: [May 8, 2024, 3:08am UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/12 "2024-05-08T03:08:01Z")

</div>

I have run the code and the ouput like below, and I am using python 3.9.  
From the results, the corresponding time trend for loading each component is the same, but my loading time will be longer.

```auto
Loaded ITKPyBase in 0.43 s.
Loaded ITKCommon in 0.91 s.
Loaded ITKImageSources in 0.04 s.
Loaded ITKStatistics in 0.15 s.
Loaded ITKImageFilterBase in 1.34 s.
Loaded ITKTransform in 0.20 s.
Loaded ITKImageFunction in 0.10 s.
Loaded ITKImageGrid in 1.62 s.
Loaded ITKFFT in 0.84 s.
Loaded ITKMesh in 0.16 s.
Loaded ITKSpatialObjects in 0.11 s.
Loaded ITKImageCompose in 0.11 s.
Loaded ITKImageStatistics in 0.87 s.
Loaded ITKPath in 0.02 s.
Loaded ITKImageIntensity in 11.99 s.
Loaded ITKThresholding in 1.17 s.
Loaded ITKConvolution in 0.11 s.
Loaded ITKSmoothing in 0.36 s.
Loaded ITKOptimizers in 0.02 s.
Loaded ITKImageGradient in 0.32 s.
Loaded ITKImageFeature in 0.60 s.
Loaded ITKFiniteDifference in 0.23 s.
Loaded ITKDisplacementField in 0.09 s.
Loaded ITKRegistrationCommon in 0.31 s.
Loaded ITKImageNoise in 0.64 s.
Loaded ITKIOBMP in 0.00 s.
Loaded ITKIOBioRad in 0.00 s.
Loaded ITKIOBruker in 0.00 s.
Loaded ITKIOGDCM in 0.01 s.
Loaded ITKIOIPL in 0.00 s.
Loaded ITKIOGE in 0.00 s.
Loaded ITKIOGIPL in 0.00 s.
Loaded ITKIOHDF5 in 0.00 s.
Loaded ITKIOJPEG in 0.00 s.
Loaded ITKIOJPEG2000 in 0.00 s.
Loaded ITKIOTIFF in 0.00 s.
Loaded ITKIOLSM in 0.00 s.
Loaded ITKIOMINC in 0.00 s.
Loaded ITKIOMRC in 0.00 s.
Loaded ITKIOMeta in 0.00 s.
Loaded ITKIONIFTI in 0.00 s.
Loaded ITKIONRRD in 0.00 s.
Loaded ITKIOPNG in 0.00 s.
Loaded ITKIOStimulate in 0.00 s.
Loaded ITKIOVTK in 0.00 s.
Loaded ITKIORAW in 0.01 s.
Loaded ITKBridgeNumPy in 0.03 s.
Loaded CudaCommon in 0.08 s.
Loaded RTK in 9.38 s.
Loaded ITKIOImageBase in 9.38 s.
--- 32.5577392578125 seconds ---

```

> [@simon.rit](#):
>
> I have never encountered this issue. Does it occur with a fresh install in a separate python environment?

This problem has always existed even though I create a new python environment.

---

<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: [May 8, 2024, 7:27pm UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/13 "2024-05-08T19:27:26Z")

</div>

> [@zhangm](#):
>
> This problem has always existed even though I create a new python environment.

Which OS and Python version?

---

<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: [May 9, 2024, 12:46am UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/14 "2024-05-09T00:46:40Z")

</div>

My OS is Win10 Professional Edition and python version is 3.9.

---

<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: [May 15, 2024, 7:01am UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/15 "2024-05-15T07:01:15Z")

</div>

I create a new python environment and install itk by CMAKE instead of pip install. There’s a lot less time left. I am very confused about this result beacuse I didn’t reduce the modules numbers.  
Actually, using pip would be more convenient for me, but the loading rtk time is too long.

```auto
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8
Loaded ITKPyBase in 0.19 s.
Loaded ITKCommon in 0.44 s.
Loaded ITKImageSources in 0.03 s.
Loaded ITKStatistics in 0.11 s.
Loaded ITKImageFilterBase in 0.28 s.
Loaded ITKTransform in 0.11 s.
Loaded ITKImageFunction in 0.07 s.
Loaded ITKImageGrid in 0.58 s.
Loaded ITKFFT in 0.27 s.
Loaded ITKMesh in 0.11 s.
Loaded ITKSpatialObjects in 0.10 s.
Loaded ITKImageCompose in 0.04 s.
Loaded ITKImageStatistics in 0.28 s.
Loaded ITKPath in 0.02 s.
Loaded ITKImageIntensity in 2.43 s.
Loaded ITKThresholding in 0.53 s.
Loaded ITKConvolution in 0.05 s.
Loaded ITKSmoothing in 0.12 s.
Loaded ITKOptimizers in 0.02 s.
Loaded ITKImageGradient in 0.11 s.
Loaded ITKImageFeature in 0.23 s.
Loaded ITKFiniteDifference in 0.09 s.
Loaded ITKDisplacementField in 0.06 s.
Loaded ITKRegistrationCommon in 0.18 s.
Loaded ITKImageNoise in 0.17 s.
Loaded ITKIOBMP in 0.00 s.
Loaded ITKIOBioRad in 0.00 s.
Loaded ITKIOBruker in 0.00 s.
Loaded ITKIOGDCM in 0.02 s.
Loaded ITKIOIPL in 0.00 s.
Loaded ITKIOGE in 0.01 s.
Loaded ITKIOGIPL in 0.00 s.
Loaded ITKIOHDF5 in 0.01 s.
Loaded ITKIOJPEG in 0.00 s.
Loaded ITKIOJPEG2000 in 0.00 s.
Loaded ITKIOTIFF in 0.00 s.
Loaded ITKIOLSM in 0.00 s.
Loaded ITKIOMINC in 0.00 s.
Loaded ITKIOMRC in 0.00 s.
Loaded ITKIOMeta in 0.00 s.
Loaded ITKIONIFTI in 0.00 s.
Loaded ITKIONRRD in 0.00 s.
Loaded ITKIOPNG in 0.00 s.
Loaded ITKIOStimulate in 0.00 s.
Loaded ITKIOVTK in 0.00 s.
Loaded ITKIORAW in 0.01 s.
Loaded ITKBridgeNumPy in 0.03 s.
Loaded RTK in 2.43 s.
Loaded ITKIOImageBase in 2.43 s.
--- 9.409522771835327 seconds ---

```

---

<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: [May 15, 2024, 7:08am UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/16 "2024-05-15T07:08:57Z")

</div>

This is probably because you have activated less wrapping types in your compilation options. Can you provide the output of `grep ITK_WRAP CMakeCache.txt` in your binary directory?

---

<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: [May 16, 2024, 1:10am UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/17 "2024-05-16T01:10:29Z")

</div>

Sorry for taking so long to reply. The file is shown in the attachment  
[CMakeCache.txt](https://discourse.itk.org/uploads/short-url/rmLsW5V3X2szgTV2MUJy0lvyyYK.txt) (566.8 KB)  
I have some issues with the use of itk Python generated by cmake. I use itk by the WrapITK.pth, could I use itk by copy the file path on .pth to site-package. That I can use itk as I pip it.

---

<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: [May 16, 2024, 6:38am UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/18 "2024-05-16T06:38:48Z")

</div>

Unlike the pypi packages, your CMakeCache.txt indicates that you do not wrap 4D images (`ITK_WRAP_IMAGE_DIMS:STRING=2;3`), double (`ITK_WRAP_double:BOOL=OFF`) and unsigned\_short (`ITK_WRAP_unsigned_short:BOOL=OFF`).  
You cannot pip ITK compiled like this I think but you could generate wheels if that’s what you want using [ITKPythonPackage suite](https://github.com/InsightSoftwareConsortium/ITKPythonPackage).

---

<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: [May 21, 2024, 1:19am UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/19 "2024-05-21T01:19:22Z")

</div>

As your advice, I tried to use ITKPythonPackage to generate whheels. But I failed.  
Can you give me some advice on how can I generate itk5.4rc1 and rtk2.5 with GPU, and set `(ITK_WRAP_IMAGE_DIMS:STRING=2;3)`?

---

<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: [May 22, 2024, 7:44am UTC](https://discourse.itk.org/t/speed-up-the-reconstruction/6599/20 "2024-05-22T07:44:06Z")

</div>

Have you checked the [tutorial](https://itkpythonpackage.readthedocs.io/en/latest/Build_ITK_Module_Python_packages.html)? I don’t have a better solution than ITKPythonPackage.

[Next page](https://discourse.itk.org/t/speed-up-the-reconstruction/6599.md?page=2)
