<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
Hello,
<div class=""><br class="">
</div>
<div class="">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.</div>
<div class=""><br class="">
</div>
<div class="">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.</div>
<div class=""><br class="">
</div>
<div class="">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.</div>
<div class=""><br class="">
</div>
<div class="">Brad</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
<div>
<blockquote type="cite" class="">
<div class="">On May 22, 2017, at 11:05 AM, Jonas Teuwen <<a href="mailto:jonasteuwen@gmail.com" class="">jonasteuwen@gmail.com</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div dir="auto" class="">Hello Brad,
<div dir="auto" class=""><br class="">
</div>
<div dir="auto" class="">That is a very nice example indeed. I will try it and see if that is sufficient for my purposes.</div>
<div dir="auto" class="">As far as I understood the python multithreading, spawning new threads can create quite a bit over overhead compared to say, OpenMP.<br class="">
</div>
<div dir="auto" class=""><br class="">
</div>
<div dir="auto" class="">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?</div>
<div dir="auto" class=""><br class="">
</div>
Jonas<br class="">
<div class="gmail_extra" dir="auto"><br class="">
<div class="gmail_quote">On May 22, 2017 16:46, "Lowekamp, Bradley (NIH/NLM/LHC) [C]" <<a href="mailto:blowekamp@mail.nih.gov" class="">blowekamp@mail.nih.gov</a>> wrote:<br type="attribution" class="">
<blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word" class="">Hello Jonas,
<div class=""><br class="">
</div>
<div class="">So ITK ( and therefore SimpleITK ), by default multi-thread each filter by default. This enables efficient processing of larger images.</div>
<div class=""><br class="">
</div>
<div class="">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.</div>
<div class=""><br class="">
</div>
<div class="">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:</div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class="">import SimpleITK as sitk</span></div>
</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class="">
<div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">from multiprocessing.pool import ThreadPool</span></div>
<div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class="">
</span></div>
<div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">p = ThreadPool()</span></div>
<div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class="">
</span></div>
<div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""># <a href="https://github.com/blowekamp/itkOBBLabelMap/tree/master/test/data" target="_blank" class="">https://github.com/<wbr class="">blowekamp/itkOBBLabelMap/tree/<wbr class="">master/test/data</a></span></div>
</span></div>
<div class="">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class="">img = sitk.ReadImage(“~/Downloads/<wbr class="">jelly_beans.png")</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class="">seg = sitk.ReadImage(“~/Downloads/<wbr class="">jelly_beans_seg.png”)</span></div>
</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class=""><br class="">
</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class="">shapeStats = sitk.<wbr class="">LabelShapeStatisticsImageFilte<wbr class="">r()</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class="">shapeStats.Execute(seg)</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class=""><br class="">
</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class="">
<div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">def extract_bb(img, shape_stats_filter, label):</span></div>
<div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">    [x,y,xsize,ysize]=shape_stats_<wbr class="">filter.GetBoundingBox(label)</span></div>
<div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">    return sitk.RegionOfInterest(img,<wbr class="">size=[xsize,ysize],index=[x,y]<wbr class="">)</span></div>
</span></div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="font-variant-ligatures:no-common-ligatures" class="">bbimg_list = p.map(lambda label: extract_bb(img, shapeStats, label), shapeStats.GetLabels())</span></div>
</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">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!</div>
<div class=""><br class="">
</div>
<div class="">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.</div>
<div class=""><br class="">
</div>
<div class="">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.</div>
<div class=""><br class="">
</div>
<div class="">Enjoy!</div>
<div class="">Brad</div>
<div class=""><br class="">
<div class="">
<blockquote type="cite" class="">
<div class="elided-text">
<div class="">On May 22, 2017, at 5:59 AM, Jonas Teuwen <<a href="mailto:jonasteuwen@gmail.com" target="_blank" class="">jonasteuwen@gmail.com</a>> wrote:</div>
<br class="m_5204697272368616392Apple-interchange-newline">
</div>
<div class="">
<div class="elided-text">
<div dir="ltr" class="">
<div class="">
<div class="">
<div class="">
<div class="">Dear all,<br class="">
<br class="">
</div>
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.<br class="">
<br class="">
</div>
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.<br class="">
<br class="">
</div>
Best,<br class="">
</div>
Jonas Teuwen<br class="">
<div class="">
<div class=""><br class="">
</div>
</div>
</div>
</div>
<div class="quoted-text">______________________________<wbr class="">_______<br class="">
Powered by <a href="http://www.kitware.com/" target="_blank" class="">www.kitware.com</a><br class="">
<br class="">
Visit other Kitware open-source projects at<br class="">
<a href="http://www.kitware.com/opensource/opensource.html" target="_blank" class="">http://www.kitware.com/<wbr class="">opensource/opensource.html</a><br class="">
<br class="">
Kitware offers ITK Training Courses, for more information visit:<br class="">
<a href="http://www.kitware.com/products/protraining.php" target="_blank" class="">http://www.kitware.com/<wbr class="">products/protraining.php</a><br class="">
<br class="">
Please keep messages on-topic and check the ITK FAQ at:<br class="">
<a href="http://www.itk.org/Wiki/ITK_FAQ" target="_blank" class="">http://www.itk.org/Wiki/ITK_<wbr class="">FAQ</a><br class="">
<br class="">
Follow this link to subscribe/unsubscribe:<br class="">
<a href="http://public.kitware.com/mailman/listinfo/insight-users" target="_blank" class="">http://public.kitware.com/<wbr class="">mailman/listinfo/insight-users</a><br class="">
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>