Skip to content

Commit

Permalink
added threadpoolctl to deal with multiprocessing and threading issues…
Browse files Browse the repository at this point in the history
…, added missing requirement scikit-learn to requirements.txt
  • Loading branch information
FabianIsensee committed Sep 13, 2019
1 parent e191ef9 commit 2d23b58
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 40 deletions.
17 changes: 1 addition & 16 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,10 @@ if __name__ == '__main__':

This is not required on Linux.

## Important!
Starting from version 1.14.6 numpy has issues with multiprocessing (it's supposed to be a feature...).
Mutrix multiplications (which we are using to rotate coordinate systems for data augmentation) now run mutlithreaded on all available threads.
This can cause chaos if you are using a multiprocessing pipeline, beacause each background worker will spawn a lot of
threads to do the matrix multiplication (8 workers on a 16 Core machine = up to 8*16=256 threads. duh.). There is nothing we (dkfz devs) can do to
tackle that problem, but this will only be a real issue in very specific configurations of data augmentation. If you
notice unnecessarily high CPU load, there are two things you can do:

- (recommended) run all your experiments with `OMP_NUM_THREADS=1` or (even better) add this to your environment
variables (`export OMP_NUM_THREADS=1` in .bashrc on linux)
- downgrade numpy to 1.14.5 (pip install numpy==1.14.5) to solve the issue

Numpy devs are aware of this problem and trying to find a solution
(see https://github.com/numpy/numpy/issues/11826#issuecomment-425087772)


## Release Notes


- 0.19.5: fixed OMP_NUM_THREADS issue by using threadpoolctl package
- 0.19:
- There is now a complete example for BraTS2017/8 available for both 2D and 3D. Use this if you would like to get
some insights on how I (Fabian) do my experiments
Expand Down
44 changes: 23 additions & 21 deletions batchgenerators/dataloading/multi_threaded_augmenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,37 @@
from queue import Empty, Full
import traceback
from time import sleep, time
from threadpoolctl import threadpool_limits


def producer(queue, data_loader, transform, thread_id, seed, abort_event):
try:
np.random.seed(seed)
data_loader.set_thread_id(thread_id)
item = None
with threadpool_limits(limits=1):
np.random.seed(seed)
data_loader.set_thread_id(thread_id)
item = None

while True:
# check if abort event was set
if not abort_event.is_set():
while True:
# check if abort event was set
if not abort_event.is_set():

if item is None:
if item is None:

try:
item = next(data_loader)
if transform is not None:
item = transform(**item)
except StopIteration:
item = "end"

try:
item = next(data_loader)
if transform is not None:
item = transform(**item)
except StopIteration:
item = "end"

try:
queue.put(item, timeout=2)
item = None
except Full:
# queue was full because items in it were not consumed. Try again.
pass
else:
break
queue.put(item, timeout=2)
item = None
except Full:
# queue was full because items in it were not consumed. Try again.
pass
else:
break

except KeyboardInterrupt:
# drain queue, then give 'end', set abort flag and reraise KeyboardInterrupt
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
threadpoolctl
scikit-learn
numpy>=1.10.2
scipy
scikit-image

scikit-learn
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup

setup(name='batchgenerators',
version='0.19.4',
version='0.19.5',
description='Data augmentation toolkit',
url='https://github.com/MIC-DKFZ/batchgenerators',
author='Division of Medical Image Computing, German Cancer Research Center',
Expand All @@ -16,7 +16,8 @@
"scikit-image",
"scikit-learn",
"future",
"unittest2"
"unittest2",
"threadpoolctl"
],
keywords=['deep learning', 'image segmentation', 'image classification', 'medical image analysis',
'medical image segmentation', 'data augmentation'],
Expand Down

0 comments on commit 2d23b58

Please sign in to comment.