Demo of competitive learning applied to image fragments

David Young

Competitive learning can be used to find an approximation to the principal components of image fragments. This is demonstrated here.

Contents

Setup

You will need to set your Matlab path to run this demo yourself, using the two commands

 addpath H:\teach\ComputerVision\matlab
 addpath H:\teach\AdvancedComputerVision\matlab

See also http://www.cogs.susx.ac.uk/users/davidy/compvis/matlab_demos/intro_demo.html

Read in an image, and display it. You should change the filename below to try different images from the vision collection.

image = teachimage('edin_lib.bmp');
figure(1);
clf;
imshow(image);

Create a centre-surround mask and convolve it with the image

This removes large-scale grey-level variation

filt = fspecial('log', 10, 1.5);
cc = convolve2(image, filt, 'valid');
imshow(cc, []);

Choose parameters for sampling the image

Sample the image. Each column of p contains the pixel values from one patch of image, windowed with a Gaussian

hsize = 10;   % the patches sampled are 21 x 21 pixels
sigma = 3;    % gaussian window width
nsamp = 200;  % extract 200 samples for training

p = imsample(cc, hsize, nsamp, sigma);
[len, nsamp] = size(p);         % get size of p conveniently

Display some of the fragments

The first 16 fragments are displayed

figure(1);
nshow = 16;
nsubx = ceil(sqrt(nshow));
nsuby = ceil(nshow/nsubx);
sz = 2*hsize+1;
samp = zeros(sz,sz);
for i = 1:nshow
    samp(:) = p(:, i);
    subplot(nsubx, nsuby, i);
    imshow(samp, []);
end;

Set up to do competitive learning on the samples.

See the NN toolkit manual for details.

pr = repmat([-0.5 0.5], len, 1);    % each row of pr has the range of a sample
nunits = 16;                        % 16 units, arbitrarily
wtrate = 0.001;                     % learning rate for weights
biasrate = wtrate/10;               % learning rate for biases
net = newc(pr, nunits, wtrate, biasrate);         % make the net
net.trainParam.epochs = 5;          % no. of epochs to train

Train the network using the image samples

net = train(net, p);
TRAINR, Epoch 0/5
TRAINR, Epoch 5/5
TRAINR, Maximum epoch reached.

Extract and view the weight arrays

The weight arrays below are not presented in any particular order. Note that many of them reflect the dominant orientation of edges in the image used.

wts = net.IW{1,1};          % get the overall weight array
sz = 2*hsize+1;
samp = zeros(sz, sz);       % for making them 2D again
figure(2);
nsubx = ceil(sqrt(nunits));
nsuby = ceil(nunits/nsubx);
for i = 1:nunits
    samp(:) = wts(i, :);
    subplot(nsubx, nsuby, i);
    imshow(samp, []);
end;

See which parts of the image one of the units has learnt to respond to

We arbitrarily choose one weight array and convolve it with the image to see what it is responding to.

unit = 1;         % select unit to study here
samp(:) = wts(unit, :);
r = convolve2(cc, rot90(samp,2), 'valid');
figure(3);
imshow(r, []);

Compare these results with the principal components analysis

Perform a singular value decomposition on the image samples, and look at the first few eigenvectors, to see how they compare with the learnt weight arrays. These are ranked - the first principal component is in the top left.

[u, s, v] = svd(p, 0);

figure(4);
for i = 1:nunits
    samp(:) = u(:, i);
    subplot(nsubx, nsuby, i);
    imshow(samp, []);
end;

Experimenting

You can download this document and then extract the original M-file with Matlab's grabcode function. You can then edit it for experimentation. (Functions from the Sussex vision library are only available to Sussex students and staff.)

Copyright University of Sussex, 2006.