Debian Codegraph @

demo/batch_processing.py

raw symbols SCIP
1 """
2 Demo: Parallel processing across images
3
4 Multithreading can be used to run transforms on a set of images in parallel.
5 This will give a net performance benefit if the images to be transformed are
6 sufficiently large.
7
8 This demo runs a multilevel wavelet decomposition on a list of 32 images,
9 each of size (512, 512). Computations are repeated sequentially and in
10 parallel and the runtimes compared.
11
12 In general, multithreading will be more beneficial for larger images and for
13 wavelets with a larger filter size.
14
15 One can also change ``ndim`` to 3 in the code below to use a set of 3D volumes
16 instead.
17 """
18
19 import time
20 from functools import partial
21 from multiprocessing import cpu_count
22
23 try:
24 from concurrent import futures
25 except ImportError:
26 raise ImportError(
27 "This demo requires concurrent.futures. It can be installed for "
28 "for python 2.x via: pip install futures")
29
30 import numpy as np
31 from numpy.testing import assert_array_equal
32
33 import pywt
34
35 # the test image
36 cam = pywt.data.camera().astype(float)
37
38 ndim = 2 # dimension of images to transform (2 or 3)
39 num_images = 32 # number of images to transform
40 max_workers = cpu_count() # max number of available threads
41 nrepeat = 5 # averages used in the benchmark
42
43 # create a list of num_images images
44 if ndim == 2:
45 imgs = [cam, ] * num_images
46 wavelet = 'db8'
47 elif ndim == 3:
48 # stack image along 3rd dimension to create a [512 x 512 x 16] 3D volume
49 im3 = np.concatenate([cam[:, :, np.newaxis], ]*16, axis=-1)
50 # create multiple copies of the volume
51 imgs = [im3, ] * num_images
52 wavelet = 'db1'
53 else:
54 ValueError("Only 2D and 3D test cases implemented")
55
56 # define a function to apply to each image
57 wavedecn_func = partial(pywt.wavedecn, wavelet=wavelet, mode='periodization',
58 level=3)
59
60
61 def concurrent_transforms(func, imgs, max_workers=None):
62 """Call func on each img in imgs using a ThreadPoolExecutor."""
63 executor = futures.ThreadPoolExecutor
64 if max_workers is None:
65 # default to as many workers as available cpus
66 max_workers = cpu_count()
67 results = []
68 with executor(max_workers=max_workers) as execute:
69 for result in execute.map(func, imgs):
70 results.append(result)
71 return results
72
73
74 print("Processing {} images of shape {}".format(len(imgs), imgs[0].shape))
75
76 # Sequential computation via a list comprehension
77 tstart = time.time()
78 for n in range(nrepeat):
79 results = [wavedecn_func(img) for img in imgs]
80 t = (time.time()-tstart)/nrepeat
81 print("\nSequential Case")
82 print("\tElapsed time: {:0.2f} ms".format(1000*t))
83
84
85 # Concurrent computation via concurrent.futures
86 tstart = time.time()
87 for n in range(nrepeat):
88 results_concurrent = concurrent_transforms(wavedecn_func, imgs,
89 max_workers=max_workers)
90 t2 = (time.time()-tstart)/nrepeat
91 print("\nMultithreaded Case")
92 print("\tNumber of concurrent workers: {}".format(max_workers))
93 print("\tElapsed time: {:0.2f} ms".format(1000*t2))
94 print("\nRelative speedup with concurrent = {}".format(t/t2))
95
96 # check a couple of the coefficient arrays to verify matching results for
97 # sequential and multithreaded computation
98 assert_array_equal(results[-1][0],
99 results_concurrent[-1][0])
100 assert_array_equal(results[-1][1]['d' + 'a'*(ndim-1)],
101 results_concurrent[-1][1]['d' + 'a'*(ndim-1)])