# ADMM Reconstruction

**URL:** <https://discourse.itk.org/t/admm-reconstruction/6490>\
**Category:** Algorithms\
**Tags:** rtk, python\
**Created:** [March 4, 2024, 9:05am UTC](https://discourse.itk.org/t/admm-reconstruction/6490 "2024-03-04T09:05:15Z")\
**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:** [March 4, 2024, 9:05am UTC](https://discourse.itk.org/t/admm-reconstruction/6490/1 "2024-03-04T09:05:15Z")

</div>

I successfully ran the FirstCudaReconstruction.py file on my project. But sometimes the scanning angle may only be 90 °, and the reconstruction effect using FDK is not good. I checked the [rtk](http://www.openrtk.org/Doxygen/classrtk_1_1ADMMTotalVariationConeBeamReconstructionFilter.html) and found that ADMM reconstruction may be used for non full week scan reconstruction.

I referred to rtkadmmtotalvariationtest.cxx, but encountered an error.

```auto
ImageType = itk.CudaImage[itk.F, 3]
GradientOutputImageType = itk.CudaImage[itk.CovariantVector[itk.F, 3], 3]
ADMMTotalVariationType = itk.RTK.ADMMTotalVariationConeBeamReconstructionFilter[ImageType, GradientOutputImageType]

```

And the error prompt looks like this.

```auto
itk.support.extras.TemplateTypeError: itk.ADMMTotalVariationConeBeamReconstructionFilter is not wrapped for input type `itk.CudaImage[itk.F,3], itk.CudaImage[itk.CovariantVector[itk.F,3],3]`.

```

How can I create an correct object about ADMMTotalVariationConeBeamReconstructionFilter? 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:** [March 4, 2024, 3:29pm UTC](https://discourse.itk.org/t/admm-reconstruction/6490/2 "2024-03-04T15:29:57Z")

</div>

The wrapping deduces some of the templates from default values:

> <https://github.com/RTKConsortium/RTK/blob/master/wrapping/rtkADMMTotalVariationConeBeamReconstructionFilter.wrap>

It should work with `itk.RTK.ADMMTotalVariationConeBeamReconstructionFilter[ImageType]`.  
Don’t you have the following full error message to indicate the reason for the problem?

```auto
Traceback (most recent call last):
  File "/home/srit/src/itk/lin64-PythonWrapping-bindings/itk/support/template_class.py", line 525, in __getitem__
    this_item = self. __template__ [key]
KeyError: (<class 'itk.itkCudaImagePython.itkCudaImageF3'>, <class 'itk.itkCudaImagePython.itkCudaImageCVF33'>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/srit/src/itk/lin64-PythonWrapping-bindings/itk/support/template_class.py", line 529, in __getitem__
    raise itk.TemplateTypeError(self, key)
itk.support.extras.TemplateTypeError: itk.ADMMTotalVariationConeBeamReconstructionFilter is not wrapped for input type `itk.CudaImage[itk.F,3], itk.CudaImage[itk.CovariantVector[itk.F,3],3]`.

To limit the size of the package, only a limited number of
types are available in ITK Python. To print the supported
types, run the following command in your python environment:

    itk.ADMMTotalVariationConeBeamReconstructionFilter.GetTypes()

Possible solutions:
* If you are an application user:
** Convert your input image into a supported format (see below).
** Contact developer to report the issue.
* If you are an application developer, force input images to be
loaded in a supported pixel type.

    e.g.: instance = itk.ADMMTotalVariationConeBeamReconstructionFilter[itk.CudaImage[itk.F,3]].New(my_input)

* (Advanced) If you are an application developer, build ITK Python yourself and
turned to `ON` the corresponding CMake option to wrap the pixel type or image
dimension you need. When configuring ITK with CMake, you can set
`ITK_WRAP_${type}` (replace ${type} with appropriate pixel type such as
`double`). If you need to support images with 4 or 5 dimensions, you can add
these dimensions to the list of dimensions in the CMake variable
`ITK_WRAP_IMAGE_DIMS`.

Supported input types:

itk.CudaImage[itk.F,3]
itk.Image[itk.F,3]
itk.Image[itk.D,3]

```

---

<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:** [March 5, 2024, 1:15am UTC](https://discourse.itk.org/t/admm-reconstruction/6490/3 "2024-03-05T01:15:20Z")

</div>

> [@simon.rit](#):
>
> Don’t you have the following full error message to indicate the reason for the problem?

Thank you for your reply. I have tried itk.RTK.ADMMTotalVariationConeBeamReconstructionFilter[ImageType]. But I’m not sure is there need pip other wrapping like use itk.CudaImage, so I asked this question there for some suggestions. It’s really help for me.  
By the way, does RTK support reconstructing partial data first, so that scanned data can be reconstructed simultaneously with CT scans, achieving the goal of accelerating reconstruction speed.

---

<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:** [March 5, 2024, 10:42am UTC](https://discourse.itk.org/t/admm-reconstruction/6490/4 "2024-03-05T10:42:41Z")

</div>

On the rtkadmmtotalvariationtest.cxx, it’s seems that the calculation method of origins is not formula `origin = spacing*(size-1)*(-0.5)` like FDK reconstruction. How can I set the value of origin?  
On the other word, I want run ADMM on GPU, so I use SetPixelContainer to transfer my data to GPU. There is an error happened, and that error didn’t happen on CPU.

```auto
    admmtotalvariation.Update()
RuntimeError: C:\P\IPP\ITK-source\ITK\Modules\Core\Common\include\itkImageConstIterator.h:211:
ITK ERROR: Region ImageRegion (000000CDBE4B2330)
  Dimension: 3
  Index: [0, 468, 111]
  Size: [752, 156, 1]
 is outside of buffered region ImageRegion (000002B7B213EEA0)
  Dimension: 3
  Index: [0, 0, 0]
  Size: [0, 0, 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:** [March 5, 2024, 11:02am UTC](https://discourse.itk.org/t/admm-reconstruction/6490/5 "2024-03-05T11:02:33Z")

</div>

> [@zhangm](#):
>
> By the way, does RTK support reconstructing partial data first, so that scanned data can be reconstructed simultaneously with CT scans, achieving the goal of accelerating reconstruction speed.

Yes, you can start the reconstruction before the end of the acquisition but it is a bit tricky and I removed the example showing it with [this commit](https://github.com/SimonRit/RTK/commits/095323c9f4eaa1ab3a3de8fd6cc1201d3a83bde0) (to reduce the maintenance burden).

> [@zhangm](#):
>
> On the rtkadmmtotalvariationtest.cxx, it’s seems that the calculation method of origins is not formula `origin = spacing*(size-1)*(-0.5)` like FDK reconstruction. How can I set the value of origin?

The formula is used to center the detector around the origin (0,0) but it does not have to be perfectly centered as long as the simulation is consistent with the reconstruction.

> [@zhangm](#):
>
> On the other word, I want run ADMM on GPU, so I use SetPixelContainer to transfer my data to GPU. There is an error happened, and that error didn’t happen on CPU.

There is not enough context to understand what is going here. Did you update the reader beforehand?

---

<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:** [March 5, 2024, 11:15am UTC](https://discourse.itk.org/t/admm-reconstruction/6490/6 "2024-03-05T11:15:50Z")

</div>

> [@simon.rit](#):
>
> There is not enough context to understand what is going here. Did you update the reader beforehand?

Yes, I have update the reader. There is my code about this section:

```auto
def loaImage(rawImages, width, height, spacing):
    itk_images = itk.GetImageFromArray(rawImages)
    itk_images.SetSpacing([spacing, spacing, spacing])
    itk_images.SetOrigin([spacing*(width-1)*(-0.5), spacing*(height-1)*(-0.5), 0])
    itk_images.Update()
    print(itk_images)
    return itk_images
reader = loaImage(images, width, height, spacing_origin)
newReader = itk.CastImageFilter[itk.Image[itk.US, 3], itk.Image[itk.F, 3]].New()
newReader.SetInput(reader)
newReader.Update()
projections = CudaImage.New()
projections.SetPixelContainer(newReader.GetOutput().GetPixelContainer())
projections.CopyInformation(newReader.GetOutput())
projections.SetBufferedRegion(newReader.GetOutput().GetBufferedRegion())
projections.SetRequestedRegion(newReader.GetOutput().GetRequestedRegion())

ConstantImageSourceType = itk.RTK.ConstantImageSource[CudaImage]
# Create reconstructed image
constantImageSource2 = ConstantImageSourceType.New()
sizeOutput = [outX, outY, outZ] # [128, 128, 128]
scale = [images.shape[2] / outX, images.shape[2] / outY, images.shape[1] / outZ]
spacing = [spacing_origin / (sdd / sid) * scale[0], spacing_origin / (sdd / sid) * scale[1],
               spacing_origin / (sdd / sid) * scale[2]]
origin = [-spacing[0] * (outX - 1) * 0.5, -spacing[1] * (outY - 1) * 0.5,
              -spacing[2] * (outZ - 1) * 0.5] # VolspaingX*(VolX-1)*-0.5
constantImageSource2.SetOrigin(origin)
constantImageSource2.SetSpacing(spacing)
constantImageSource2.SetSize(sizeOutput)
constantImageSource2.SetConstant(0.)

ADMMTotalVariationType = itk.RTK.ADMMTotalVariationConeBeamReconstructionFilter[CudaImage]
admmtotalvariation = ADMMTotalVariationType.New()
admmtotalvariation.SetInput(0, constantImageSource2.GetOutput())
admmtotalvariation.SetInput(1, projections)
admmtotalvariation.SetGeometry(geometry)
admmtotalvariation.SetAlpha(100)
admmtotalvariation.SetBeta(1000)
admmtotalvariation.SetAL_iterations(3)
admmtotalvariation.SetCG_iterations(2)
admmtotalvariation.SetForwardProjectionFilter(ADMMTotalVariationType.ForwardProjectionType_FP_CUDARAYCAST)
admmtotalvariation.SetBackProjectionFilter(ADMMTotalVariationType.BackProjectionType_BP_CUDAVOXELBASED)
admmtotalvariation.Update()

```

---

<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:** [March 5, 2024, 2:39pm UTC](https://discourse.itk.org/t/admm-reconstruction/6490/7 "2024-03-05T14:39:40Z")

</div>

I don’t really know… First comment, in

> [@zhangm](#):
>
> ```Python
> itk_images = itk.GetImageFromArray(rawImages)
> itk_images.SetSpacing([spacing, spacing, spacing])
> itk_images.SetOrigin([spacing*(width-1)*(-0.5), spacing*(height-1)*(-0.5), 0])
> itk_images.Update()
> 
> ```

I don’t think this `Update()` is required. Second, can you try if disconnecting the output of `newReader` from the pipeline helps? Something like:

```Python
newReader.Update()
img = newReader.GetOutput()
img.DisconnectPipeline()
projections = CudaImage.New()
projections.SetPixelContainer(img.GetPixelContainer())
projections.CopyInformation(img)
projections.SetBufferedRegion(img.GetBufferedRegion())
projections.SetRequestedRegion(img.GetRequestedRegion())

```

---

<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:** [March 6, 2024, 1:12am UTC](https://discourse.itk.org/t/admm-reconstruction/6490/8 "2024-03-06T01:12:55Z")

</div>

I tried this method but it didn’t solve the problem.  
I use this pipeline on ADMM CPU reconstruction, there’s no problem here either.

```auto
ADMMTotalVariationType = itk.RTK.ADMMTotalVariationConeBeamReconstructionFilter[ImageType]
admmtotalvariation = ADMMTotalVariationType.New()
admmtotalvariation.SetInput(constantImageSource2.GetOutput())
admmtotalvariation.SetInput(1, newReader.GetOutput())
admmtotalvariation.SetGeometry(geometry)
admmtotalvariation.SetAlpha(100)
admmtotalvariation.SetBeta(1000)
admmtotalvariation.SetAL_iterations(3)
admmtotalvariation.SetCG_iterations(2)
admmtotalvariation.SetForwardProjectionFilter(ADMMTotalVariationType.ForwardProjectionType_FP_JOSEPH)
admmtotalvariation.SetBackProjectionFilter(ADMMTotalVariationType.BackProjectionType_BP_VOXELBASED)
admmtotalvariation.Update()

```

So is there a problem with the way I transfer data to the GPU? I don’t know where the problem is because this pipepline was useful on FDK GPU reconstruction.  
By monitoring the system memory, there was a momentary high GPU usage, and it is unclear if this is the problem. My data size is about 920KB\*180, GPU size is 15GB.  
 ![20240306152409](https://discourse.itk.org/uploads/default/original/2X/8/885195d7e64c509be4d22d7651ce4c17bf5dd800.png)

---

<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:** [March 6, 2024, 2:41am UTC](https://discourse.itk.org/t/admm-reconstruction/6490/9 "2024-03-06T02:41:05Z")

</div>

> [@simon.rit](#):
>
> Yes, you can start the reconstruction before the end of the acquisition but it is a bit tricky and I removed the example showing it with [this commit](https://github.com/SimonRit/RTK/commits/095323c9f4eaa1ab3a3de8fd6cc1201d3a83bde0) (to reduce the maintenance burden).

May I ask if it is convenient to send me a copy of this file? My email is [1533168486@qq.com](mailto:1533168486@qq.com), very appriciate for you.

---

<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:** [March 6, 2024, 8:57am UTC](https://discourse.itk.org/t/admm-reconstruction/6490/10 "2024-03-06T08:57:20Z")

</div>

> [@zhangm](#):
>
> May I ask if it is convenient to send me a copy of this file? My email is [1533168486@qq.com](mailto:1533168486@qq.com), very appriciate for you.

You can download the original file in the commit 🙂 [https://raw.githubusercontent.com/SimonRit/RTK/38a9f44e2c8a3311735f627e1150b3ff7a94d169/applications/rtkinlinefdk/rtkinlinefdk.cxx](https://raw.githubusercontent.com/SimonRit/RTK/38a9f44e2c8a3311735f627e1150b3ff7a94d169/applications/rtkinlinefdk/rtkinlinefdk.cxx)

---

<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:** [March 6, 2024, 9:18am UTC](https://discourse.itk.org/t/admm-reconstruction/6490/11 "2024-03-06T09:18:30Z")

</div>

I’m sorry that I can’t open the link as you provided. It will be helpful if you can send the file by email.

---

<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:** [March 6, 2024, 9:49am UTC](https://discourse.itk.org/t/admm-reconstruction/6490/12 "2024-03-06T09:49:01Z")

</div>

The link can be opened by this [method](https://blog.csdn.net/weixin_39535752/article/details/110642981)

---

<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:** [March 7, 2024, 5:41pm UTC](https://discourse.itk.org/t/admm-reconstruction/6490/13 "2024-03-07T17:41:37Z")

</div>

> [@zhangm](#):
>
> So is there a problem with the way I transfer data to the GPU? I don’t know where the problem is because this pipepline was useful on FDK GPU reconstruction.  
> By monitoring the system memory, there was a momentary high GPU usage, and it is unclear if this is the problem. My data size is about 920KB\*180, GPU size is 15GB.

I just checked your data and code that you sent privately. The volume you’re trying to reconstruct has a sizeOutput of [752, 752, 624], i.e., 10 GB. Make it [128, 128, 128] and your code runs fine. Don’t forget that advanced reconstruction algorithms require multiple copies of the volume.

---

<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:** [March 8, 2024, 12:46am UTC](https://discourse.itk.org/t/admm-reconstruction/6490/14 "2024-03-08T00:46:01Z")

</div>

Thank you very much for your help.
