@@ -8,20 +8,24 @@ Here is an example of how to convolve an image with oriented
88filters. 2D convolution is notoriously a slow operation; we'll show
99how to improve the filtering speed using the caching extension.
1010
11- For our example, we'll use the famous image of Lena:
11+ For our example, we'll use the famous image of Lena
1212
1313 >>> import mdp
1414 >>> import numpy
15+ >>> import os
16+ >>> import matplotlib
17+ >>> matplotlib.rcParams[' examples.directory' ] = os.path.join(os.path.dirname(matplotlib.rcParams[' datapath' ]), ' sample_data' )
1518 >>> import pylab
16- >>> im = pylab.imread(' lena.png' )
19+ >>> from matplotlib.cbook import get_sample_data
20+ >>> im = pylab.imread(get_sample_data(" ada.png" ))
1721 >>> # transform to grayscale
1822 >>> im = numpy.sqrt((im[:,:,:3 ]** 2 .).mean(2 ))
1923
2024.. image :: lena_gray.png
2125 :width: 400
2226 :alt: Lena's famous photograph
2327
24- First, we create a bank of Gabor filters at different orientations:
28+ First, we create a bank of Gabor filters at different orientations
2529
2630 >>> # create Gabor filters bank
2731 >>> pi = numpy.pi
@@ -41,36 +45,36 @@ First, we create a bank of Gabor filters at different orientations:
4145 :width: 500
4246 :alt: The four Gabor filters
4347
44- To convolve the image, we use the ``Convolution2DNode `` as follows:
48+ To convolve the image, we use the ``Convolution2DNode `` as follows
4549
4650 >>> node = mdp.nodes.Convolution2DNode(gabors, mode = ' valid' , boundary = ' fill' ,
4751 ... fillvalue= 0 , output_2d= False )
4852 >>> cim = node.execute(im[numpy.newaxis,:,:])
4953
50- obtaining these filtered images:
54+ obtaining these filtered images
5155
5256.. image :: filtered_lena.png
5357 :width: 600
5458 :alt: Lena filtered by Gabors
5559
5660To demonstrate how to use the caching extension, we'll pretend we have
5761several images by copying Lena several times, and measure the
58- filtering performance with and without cache:
62+ filtering performance with and without cache
5963
6064 >>> x = mdp.utils.lrep(im, 3 )
6165 >>> # set up a Timer object to measure performance
62- >>> from timeit import Timer
63- >>> timer = Timer(" node.execute(x)" , " from __main__ import node, x" )
66+ >>> from timeit import Timer # doctest: +SKIP
67+ >>> timer = Timer(" node.execute(x)" , " from __main__ import node, x" ) # doctest: +SKIP
6468 >>> # first uncached execution
65- >>> print timer.repeat(1 , 1 ), ' sec'
69+ >>> print timer.repeat(1 , 1 ), ' sec' # doctest: +SKIP
6670 6.91 sec
6771 >>>
6872 >>> # now activating the cache on the Convolution2DNode class:
69- >>> with mdp.caching.cache(cache_classes = [mdp.nodes.Convolution2DNode]):
73+ >>> with mdp.caching.cache(cache_classes = [mdp.nodes.Convolution2DNode]): # doctest: +SKIP
7074 >>> # second execution, uncached if it's the first time the script is run
71- >>> print timer.repeat(1 , 1 ), ' sec'
75+ >>> print timer.repeat(1 , 1 ), ' sec' # doctest: +SKIP
7276 >>> # third execution, this time cached
73- >>> print timer.repeat(1 , 1 ), ' sec'
77+ >>> print timer.repeat(1 , 1 ), ' sec' # doctest: +SKIP
7478 7.05 sec
7579 39.6 msec
7680
0 commit comments