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

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 mention that itk support image streaming mode.

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. With 5.0 refactoring, MultiThreader is now called MultiThreaderBase.

Here is how to use streaming:

1 Like

to limit the cpu usage, this works for python

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


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

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

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'

Try SetNumberOfWorkUnits: 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().

1 Like

Maybe this line should have been MultiThreaderBase::Pointer mt = this->GetMultiThreader(); for resampleF.SetNumberOfWorkUnits(10) to have the desired effect. But

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

should work.

1 Like

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

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

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

here I found a python version link

1 Like

replace

resampleF.Update()

by

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)
and the total process time is really quick, 160 tiles for 30.11 seconds
image

1 Like

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

1 Like