[ITK] [ITK-users] CropImageFilter, multithreading

Lowekamp, Bradley (NIH/NLM/LHC) [C] blowekamp at mail.nih.gov
Mon May 22 11:40:37 EDT 2017


Hello,

The below code should be efficient if most of the time is spent in the ITK C++ filters. If most of the time is spent in Python code, or Python libraries which don’t unlock the GIL it may not see a benefit.

There are many type of multi-threading in Python, some spawn expensive full processes and duplicate data, like the multiprocessing module. The approach here uses ( and reuses ) light weight threads which share memory, and take advantage of SimpleITK unlocking of the GIL to allow concurrent threads running multiple ITK filters. As I said before this is an uncommon feature in Python libraries, if you did the same operation with numpy, or scikit-image it would not scale at all.

In the link in the comment in the sample code below you and can find an ITK module I worked on which was implement these algorithms as ITK filters, and examples which do about the same thing. These methods are mostly composing existing filters in ITK. Composing filters into complex operation and performing parallel computing on small images are areas that I find SimpleITK excels in.

Brad


On May 22, 2017, at 11:05 AM, Jonas Teuwen <jonasteuwen at gmail.com<mailto:jonasteuwen at gmail.com>> wrote:

Hello Brad,

That is a very nice example indeed. I will try it and see if that is sufficient for my purposes.
As far as I understood the python multithreading, spawning new threads can create quite a bit over overhead compared to say, OpenMP.

If I wanted to translate your example to ITK/C++ would a direct translation be the most sensible thing to do, or does ITK have smarter methods for that?

Jonas

On May 22, 2017 16:46, "Lowekamp, Bradley (NIH/NLM/LHC) [C]" <blowekamp at mail.nih.gov<mailto:blowekamp at mail.nih.gov>> wrote:
Hello Jonas,

So ITK ( and therefore SimpleITK ), by default multi-thread each filter by default. This enables efficient processing of larger images.

However, for your task you can run a large number of filters concurrently. Fortunately, SimpleITK does support concurrent execution of ITK filters with light weight python Threads! This is an uncommon feature for Python libraries and is a distinguishing feature of SimpleITK.

I have been planning on writing an example or notebook on this.  Here is an efficient and compact code to accomplish your task as I understand it:

import SimpleITK as sitk
from multiprocessing.pool import ThreadPool

p = ThreadPool()

# https://github.com/blowekamp/itkOBBLabelMap/tree/master/test/data
img = sitk.ReadImage(“~/Downloads/jelly_beans.png")
seg = sitk.ReadImage(“~/Downloads/jelly_beans_seg.png”)

shapeStats = sitk.LabelShapeStatisticsImageFilter()
shapeStats.Execute(seg)

def extract_bb(img, shape_stats_filter, label):
    [x,y,xsize,ysize]=shape_stats_filter.GetBoundingBox(label)
    return sitk.RegionOfInterest(img,size=[xsize,ysize],index=[x,y])

bbimg_list = p.map(lambda label: extract_bb(img, shapeStats, label), shapeStats.GetLabels())


This uses advance concepts of multi-threading, closures, mapping, and thread pools. I think it is the integration of SimpleITK any Python at its bests!

One tweak which could be made to this code is to create a RegionOfInterestImageFilter object, and explicitly set it’s number of threads to 1, so that it is not multi-threaded.

A related not is the recently Oriented Bounding Box computation has been added to ITK’s LabelShape objects and filters, this is starting to get propagated into SimpleITK now. This can be used for a similar purpose but with a resample image filter to change the orientation of the separate object.

Enjoy!
Brad

On May 22, 2017, at 5:59 AM, Jonas Teuwen <jonasteuwen at gmail.com<mailto:jonasteuwen at gmail.com>> wrote:

Dear all,

Currently I have SimpleITK code to extract patches from a 3D medical image to train a neural network with. I do this with CropImageFilter and check if they are on the edge or not, and pad if necessary.

Currently this is done offline, so speed is not really an issue, however, I would like to do this online now, so load the image and mask, and return the patches. If I want to extract many small patches, about 1000 out of a large image (~3000x3000x50 or so) is there any reason why I would not use OpenMP instead of ITK's possibilities? I do not have a good understanding of the multithreading capabilities yet, so any pointers would be great.

Best,
Jonas Teuwen

_____________________________________
Powered by www.kitware.com<http://www.kitware.com/>

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php

Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/insight-users



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/community/attachments/20170522/dedcb130/attachment.html>
-------------- next part --------------
_____________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php

Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/insight-users


More information about the Community mailing list