Module Expr Str Import alias identifier ImportFrom alias ImportFrom alias identifier Import alias identifier ImportFrom alias ImportFrom alias ImportFrom alias ImportFrom alias Expr Call AttributeLoad AttributeLoad NameLoad attr attr Num Assign NameStore TupleLoad Num Num Assign NameStore Num Assign NameStore Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr Assign NameStore Call NameLoad NameLoad NameLoad Assign NameStore SubscriptLoad Call AttributeLoad NameLoad attr Num BinOpMult SubscriptLoad NameLoad Index Num SubscriptLoad NameLoad Index Num Slice Num Assign TupleStore NameStore NameStore Call NameLoad NameLoad NameLoad keyword Str Assign NameStore Call AttributeLoad NameLoad attr BinOpAdd TupleLoad Num NameLoad Assign NameStore Call NameLoad NameLoad keyword NameLoad Assign NameStore BinOpSub Call AttributeLoad NameLoad attr NameLoad Num Call AttributeLoad NameLoad attr NameLoad Num Assign NameStore Call NameLoad NameLoad NameLoad Assign NameStore SubscriptLoad Call AttributeLoad NameLoad attr Num BinOpMult SubscriptLoad NameLoad Index Num SubscriptLoad NameLoad Index Num Slice Num Assign TupleStore NameStore NameStore Call NameLoad NameLoad NameLoad keyword Str Assign NameStore Call AttributeLoad NameLoad attr BinOpAdd TupleLoad Num NameLoad Assign NameStore Call NameLoad NameLoad keyword NameLoad Assign TupleStore NameStore NameStore Call AttributeLoad NameLoad attr Num Num keyword TupleLoad Num Num Assign TupleStore NameStore NameStore NameStore NameStore Call AttributeLoad NameLoad attr Expr Call AttributeLoad NameLoad attr NameLoad keyword AttributeLoad AttributeLoad NameLoad attr attr Expr Call AttributeLoad NameLoad attr Str Expr Call AttributeLoad NameLoad attr NameLoad keyword AttributeLoad AttributeLoad NameLoad attr attr keyword Str Expr Call AttributeLoad NameLoad attr Str Expr Call AttributeLoad NameLoad attr NameLoad keyword AttributeLoad AttributeLoad NameLoad attr attr Expr Call AttributeLoad NameLoad attr Str Expr Call AttributeLoad NameLoad attr NameLoad keyword AttributeLoad AttributeLoad NameLoad attr attr keyword Str Expr Call AttributeLoad NameLoad attr Str For NameStore Call AttributeLoad NameLoad attr body Expr Call AttributeLoad NameLoad attr Str Expr Call AttributeLoad NameLoad attr Expr Call AttributeLoad NameLoad attr
N/A N/A 
============================================================
Gabors / Primary Visual Cortex "Simple Cells" from an Image
============================================================

How to build a (bio-plausible) "sparse" dictionary (or 'codebook', or
'filterbank') for e.g. image classification without any fancy math and
with just standard python scientific libraries?

Please find below a short answer ;-)

This simple example shows how to get Gabor-like filters [1]_ using just
a simple image. In our example, we use a photograph of the astronaut Eileen
Collins. Gabor filters are good approximations of the "Simple Cells" [2]_
receptive fields [3]_ found in the mammalian primary visual cortex (V1)
(for details, see e.g. the Nobel-prize winning work of Hubel & Wiesel done
in the 60s [4]_ [5]_).

Here we use McQueen's 'kmeans' algorithm [6]_, as a simple biologically
plausible hebbian-like learning rule and we apply it (a) to patches of
the original image (retinal projection), and (b) to patches of an
LGN-like [7]_ image using a simple difference of gaussians (DoG)
approximation.

Enjoy ;-) And keep in mind that getting Gabors on natural image patches
is not rocket science.

.. [1] http://en.wikipedia.org/wiki/Gabor_filter
.. [2] http://en.wikipedia.org/wiki/Simple_cell
.. [3] http://en.wikipedia.org/wiki/Receptive_field
.. [4] http://en.wikipedia.org/wiki/K-means_clustering
.. [5] http://en.wikipedia.org/wiki/Lateral_geniculate_nucleus
.. [6] D. H. Hubel and T. N., Wiesel Receptive Fields of Single Neurones
       in the Cat's Striate Cortex, J. Physiol. pp. 574-591 (148) 1959
.. [7] D. H. Hubel and T. N., Wiesel Receptive Fields, Binocular
       Interaction, and Functional Architecture in the Cat's Visual Cortex,
       J. Physiol. 160 pp.  106-154 1962
 N/A numpy np scipy.cluster.vq kmeans2 scipy ndimage ndi N/A matplotlib.pyplot plt skimage data skimage color skimage.util.shape view_as_windows skimage.util.montage montage2d N/A N/A N/A N/A np random seed 42 N/A patch_shape N/A 8 8 N/A n_filters 49 N/A astro N/A N/A color rgb2gray N/A N/A data astronaut N/A patches1 N/A view_as_windows astro patch_shape N/A patches1 N/A N/A N/A patches1 reshape -1 N/A N/A patch_shape N/A 0 N/A patch_shape N/A 1 N/A 8 N/A N/A fb1 _ N/A kmeans2 patches1 n_filters minit points N/A fb1 N/A N/A fb1 reshape N/A N/A -1 patch_shape N/A fb1_montage N/A montage2d fb1 rescale_intensity True N/A astro_dog N/A N/A N/A ndi gaussian_filter astro 0.5 N/A N/A ndi gaussian_filter astro 1 N/A patches2 N/A view_as_windows astro_dog patch_shape N/A patches2 N/A N/A N/A patches2 reshape -1 N/A N/A patch_shape N/A 0 N/A patch_shape N/A 1 N/A 8 N/A N/A fb2 _ N/A kmeans2 patches2 n_filters minit points N/A fb2 N/A N/A fb2 reshape N/A N/A -1 patch_shape N/A fb2_montage N/A montage2d fb2 rescale_intensity True N/A N/A fig axes N/A N/A plt subplots 2 2 figsize N/A 7 6 N/A N/A ax0 ax1 ax2 ax3 N/A N/A axes ravel N/A N/A N/A ax0 imshow astro cmap N/A N/A plt cm gray N/A N/A N/A ax0 set_title Image (original) N/A N/A N/A ax1 imshow fb1_montage cmap N/A N/A plt cm gray interpolation nearest N/A N/A N/A ax1 set_title K-means filterbank (codebook)
on original image N/A N/A N/A ax2 imshow astro_dog cmap N/A N/A plt cm gray N/A N/A N/A ax2 set_title Image (LGN-like DoG) N/A N/A N/A ax3 imshow fb2_montage cmap N/A N/A plt cm gray interpolation nearest N/A N/A N/A ax3 set_title K-means filterbank (codebook)
on LGN-like DoG image N/A ax N/A N/A axes ravel N/A N/A N/A N/A ax axis off N/A N/A N/A fig tight_layout N/A N/A N/A plt show