File: tutorial_parallelization.rst

package info (click to toggle)
skimage 0.25.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,720 kB
  • sloc: python: 60,007; cpp: 2,592; ansic: 1,591; xml: 1,342; javascript: 1,267; makefile: 168; sh: 20
file content (69 lines) | stat: -rw-r--r-- 1,907 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
========================
How to parallelize loops
========================

In image processing, we frequently apply the same algorithm
on a large batch of images. In this paragraph, we propose to
use `joblib <https://joblib.readthedocs.io>`_ to parallelize
loops. Here is an example of such repetitive tasks:

.. code-block:: python

    import skimage as ski

    def task(image):
        """
        Apply some functions and return an image.
        """
        image = ski.restoration.denoise_tv_chambolle(
            image[0][0], weight=0.1, channel_axis=-1
        )
        fd, hog_image = ski.feature.hog(
            ski.color.rgb2gray(image),
            orientations=8,
            pixels_per_cell=(16, 16),
            cells_per_block=(1, 1),
            visualize=True
        )
        return hog_image


    # Prepare images
    hubble = ski.data.hubble_deep_field()
    width = 10
    pics = ski.util.view_as_windows(
        hubble, (width, hubble.shape[1], hubble.shape[2]), step=width
    )

To call the function ``task`` on each element of the list ``pics``, it is
usual to write a for loop. To measure the execution time of this loop, you can
use ipython and measure the execution time with ``%timeit``.

.. code-block:: python

    def classic_loop():
        for image in pics:
            task(image)


    %timeit classic_loop()

Another equivalent way to code this loop is to use a comprehension list which has the same efficiency.

.. code-block:: python

    def comprehension_loop():
        [task(image) for image in pics]

    %timeit comprehension_loop()

``joblib`` is a library providing an easy way to parallelize for loops once we have a comprehension list.
The number of jobs can be specified.

.. code-block:: python

    from joblib import Parallel, delayed
    def joblib_loop():
        Parallel(n_jobs=4)(delayed(task)(i) for i in pics)

    %timeit joblib_loop()