# how to use image streaming to reduce memory usage and cpu usage in image stitching

**URL:** https://discourse.itk.org/t/how-to-use-image-streaming-to-reduce-memory-usage-and-cpu-usage-in-image-stitching/4795
**Category:** Beginner Questions
**Tags:** registration, python
**Created:** [February 8, 2022, 10:40am UTC](https://discourse.itk.org/t/how-to-use-image-streaming-to-reduce-memory-usage-and-cpu-usage-in-image-stitching/4795 "2022-02-08T10:40:48Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![allen](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@allen](https://discourse.itk.org/u/allen)
#### Post date: [February 8, 2022, 10:40am UTC](https://discourse.itk.org/t/how-to-use-image-streaming-to-reduce-memory-usage-and-cpu-usage-in-image-stitching/4795/1 "2022-02-08T10:40:49Z")

</div>

The cpu usage and memory usage is pretty high when stitching tiles into a single one. 140  
tiles(2592\*1944) stitching occupy nearly 100% cpu usage for more than 60s. i wonder if the usage won’t be so high in the image streaming mode as they are computed one by one. if so, is there a demo code or guidance how to use image streaming in itk, thanks.  
this [article](https://link.springer.com/article/10.1007/s40192-021-00202-x#:~:text=Support%20for%20image%20streaming%20(processing%20an%20image%20piece%2Dby%2Dpiece%20instead%20of%20all%20at%20once)) mention that itk support image streaming mode.

---

<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: [February 8, 2022, 7:22pm UTC](https://discourse.itk.org/t/how-to-use-image-streaming-to-reduce-memory-usage-and-cpu-usage-in-image-stitching/4795/2 "2022-02-08T19:22:35Z")

</div>

Using streaming will reduce memory use, but not CPU use. To reduce CPU use, you should limit number of threads ITK uses. There are a [few ways](https://itk.org/pipermail/insight-users/2008-July/026850.html). With 5.0 refactoring, `MultiThreader` is now called `MultiThreaderBase`.

Here is how to use streaming:

> <https://github.com/InsightSoftwareConsortium/ITKMontage/blob/v0.7.3/test/itkMontageTestHelper.hxx#L403-L414>

---

<div class="post-metadata">

### Author: ![allen](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@allen](https://discourse.itk.org/u/allen)
#### Post date: [February 9, 2022, 11:46am UTC](https://discourse.itk.org/t/how-to-use-image-streaming-to-reduce-memory-usage-and-cpu-usage-in-image-stitching/4795/3 "2022-02-09T11:46:40Z")

</div>

to limit the cpu usage, this works for python

```auto
itk.MultiThreaderBase.SetGlobalMaximumNumberOfThreads(5)
itk.MultiThreaderBase.SetGlobalDefaultNumberOfThreads(5)

```

but even if the thread number setting is effective, the cpu usage still get to 100% for a few seconds while resampling

 ![8d03a1f1eaf477a553038a0d745479b](https://discourse.itk.org/uploads/default/original/2X/6/61ede2e94b77fddd43ab2e6f6725899c951c8c1b.png)  
is there any way to limit the cpu usage in TileMergeImageFilter, or bind cpu core also a acceptable solution that spare other cpu core to be idle. now i add time.sleep() to limit the resample cpu usage like this

```auto
resampleF = itk.TileMergeImageFilter[type(color_images[0])].New()
resampleF.SetMontageSize(stage_tiles.GetAxisSizes())
# resampleF.SetNumberOfThreads(10)

for t in range(stage_tiles.LinearSize()):
  resampleF.SetInputTile(t, color_images[t])
  index = stage_tiles.LinearIndexToNDIndex(t)
  transform = montage.GetOutputTransform(index)
  resampleF.SetTileTransform(index, transform)
  time.sleep(0.1)
resampleF.Update()

```

yet i think that’s not the best way to do it and will increase processing time significantly  
by the way the TileMergeImageFilter does not support SetNumberOfThreads

```auto
Traceback (most recent call last):
  File "E:\projects\ITKMontage\examples\SimpleMontage.py", line 122, in <module>
    resampleF.SetNumberOfThreads(10)
AttributeError: 'itkTileMergeImageFilterIRGBUS2' object has no attribute 'SetNumberOfThreads'

```

---

<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: [February 9, 2022, 2:19pm UTC](https://discourse.itk.org/t/how-to-use-image-streaming-to-reduce-memory-usage-and-cpu-usage-in-image-stitching/4795/4 "2022-02-09T14:19:08Z")

</div>

Try [SetNumberOfWorkUnits](https://itk.org/Doxygen/html/classitk_1_1ProcessObject.html#a07e1d0d435747ae65bfb213bd12053e8): `resampleF.SetNumberOfWorkUnits(10)`.

I wouldn’t expect that adding sleep in the for loop would reduce CPU use, as the computations happen when you invoke `resampleF.Update()`.

---

<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: [February 9, 2022, 2:26pm UTC](https://discourse.itk.org/t/how-to-use-image-streaming-to-reduce-memory-usage-and-cpu-usage-in-image-stitching/4795/5 "2022-02-09T14:26:06Z")

</div>

Maybe [this line](https://github.com/InsightSoftwareConsortium/ITKMontage/blob/v0.7.3/include/itkTileMergeImageFilter.hxx#L409) should have been `MultiThreaderBase::Pointer mt = this->GetMultiThreader();` for `resampleF.SetNumberOfWorkUnits(10)` to have the desired effect. But

```auto
itk.MultiThreaderBase.SetGlobalMaximumNumberOfThreads(5)
itk.MultiThreaderBase.SetGlobalDefaultNumberOfThreads(5)

```

should work.

---

<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: [February 9, 2022, 2:29pm UTC](https://discourse.itk.org/t/how-to-use-image-streaming-to-reduce-memory-usage-and-cpu-usage-in-image-stitching/4795/6 "2022-02-09T14:29:39Z")

</div>

If you don’t want your computer to “lock up”, you could give the process “itk\_test” priority lower than “normal”. Also, how many physical cores (not logical, hyper-threaded cores) does your CPU have? You need to set number of threads below that number to ensure CPU use does not reach 100%.

---

<div class="post-metadata">

### Author: ![allen](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@allen](https://discourse.itk.org/u/allen)
#### Post date: [February 10, 2022, 3:57pm UTC](https://discourse.itk.org/t/how-to-use-image-streaming-to-reduce-memory-usage-and-cpu-usage-in-image-stitching/4795/7 "2022-02-10T15:57:49Z")

</div>

> [@dzenanz](#):
>
> resampleF.SetNumberOfWorkUnits(10)

my computer has 6 physical cores so i set the number 5, bu t it seems still reach 100% usage, yet resampleF.SetNumberOfWorkUnits(10) worked i think. i will update my results after test more times

---

<div class="post-metadata">

### Author: ![allen](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@allen](https://discourse.itk.org/u/allen)
#### Post date: [February 16, 2022, 4:11pm UTC](https://discourse.itk.org/t/how-to-use-image-streaming-to-reduce-memory-usage-and-cpu-usage-in-image-stitching/4795/8 "2022-02-16T16:11:03Z")

</div>

the max cpu usage is under 70% now after hundreds times test these days

---

<div class="post-metadata">

### Author: ![allen](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@allen](https://discourse.itk.org/u/allen)
#### Post date: [February 16, 2022, 4:24pm UTC](https://discourse.itk.org/t/how-to-use-image-streaming-to-reduce-memory-usage-and-cpu-usage-in-image-stitching/4795/9 "2022-02-16T16:24:12Z")

</div>

here I found a python version [link](https://github.com/InsightSoftwareConsortium/ScientificImageAnalysisVisualizationAndArtificialIntelligenceCourse/blob/34368360527a9f927106a7cfe9f8769c651ab8b9/solutions/2_Image_Filtering_Exercise4.py)

---

<div class="post-metadata">

### Author: ![allen](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@allen](https://discourse.itk.org/u/allen)
#### Post date: [February 17, 2022, 11:12am UTC](https://discourse.itk.org/t/how-to-use-image-streaming-to-reduce-memory-usage-and-cpu-usage-in-image-stitching/4795/10 "2022-02-17T11:12:06Z")

</div>

replace

```auto
resampleF.Update()

```

by

```auto
writer = itk.ImageFileWriter.New(resampleF.GetOutput().astype(itk.RGBPixel[itk.UC]))
writer.SetFileName(out_file)
writer.SetNumberOfStreamDivisions(5)
writer.Update()

```

it works great for me, the memory usage line no waves like nothing happened  
 ![1645096260(1)](https://discourse.itk.org/uploads/default/original/2X/4/46ed5b7ba60963da21f352ff005c50ca722d94f0.png)  
and the total process time is really quick, 160 tiles for 30.11 seconds  
 ![image](https://discourse.itk.org/uploads/default/original/2X/0/0322f01021028370e7d5ae0f30122189ccc13f8d.png)

---

<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: [February 17, 2022, 2:52pm UTC](https://discourse.itk.org/t/how-to-use-image-streaming-to-reduce-memory-usage-and-cpu-usage-in-image-stitching/4795/11 "2022-02-17T14:52:15Z")

</div>

I guess that lower memory use leads to no use of swap file on hard disk, hence the overall processing is faster.
