next up previous
Next: rap_images.m: Generates all the Up: Matlab functions Previous: lin_sym.m: Computes the linear

neural.m: Generates the training data, trains and tests the neural network

%Create Network
% We have 1000 input pixels each has a range 0:10
% there are 9 neurones in the hidden layer
% There is 1 output neuron

pos_val = 0.9;
neg_val = 0.1;
size_x = 40;
size_y = 30;
inputs = size_x * size_y * 2;
max_input = 1;
hidden_neurones = 11;
Neg_exampl = 150;

% build now the matrix of activation of our image
load ssand3;
hor_remove;
I_hr = conv2( ssand3, filtre_hor, 'valid' );
I = medfilt2( I_hr, [5 3] );
I = I - min( I(:));
I = I / max( I(:));
I = I * max_input;
dspim( I );

clear ssand3;
clear I_hr;

I_mean = mean( I(:));

Pini = [ zeros(inputs,1) max_input *ones(inputs,1) ];
[W1,B1,W2,B2] = initff(Pini, hidden_neurones, 'logsig', 1, 'logsig');

% For the training, we generate 150 random vectors for the negative
% examples. We whish an output of 0.1 for a negative example and 0.85
% for a positive. We want the random examples to be in the interval
% [I_mean-1, I_mean+1]

Pos_exampl = round((1.25-0.85)/0.05 * (1.8-0.5)/0.05);
P = zeros( inputs, Neg_exampl+Pos_exampl);
for (a=1:Neg_exampl)
  temp = adjust( rand( size_y, size_x ));
  P( : , a ) = temp(:);
end;

% P = rand( inputs, 150 ) * 2 + I_mean - 1;
T = neg_val * ones( 1, 150 );

% Let's add some self made hyperbolas to our training set
index = Neg_exampl;
for (a=0.85:0.05:1.25)
  for (b=0.5:0.05:1.8)
    hy = hyperbola(a,b,0.5,2,-1,1,max_input,I_mean);
    % adjust it for the Neural Net
    hy = adjust( hy );
    index = index + 1;
    P( : , index ) = hy(:);
    T = [T pos_val];
	% Well also take the inverse image
	% hy = 10 - hy;
    hy = hyperbola(a,b,0.5,2,-1,1,0,I_mean);
    hy = adjust( hy );
    index = index + 1;
    P( : , index ) = hy(:);
    T = [T pos_val];
  end;
end;

save training P T;

figure;
tp = [10 500 0.3 0.01];
% [W1,B1,W2,B2, epochs, error] = 
%  trainbp( W1, B1, 'logsig', W2, B2, 'logsig', P, T, tp );
[W1,B1,W2,B2, epochs, error] = 
  trainbpx( W1, B1, 'logsig', W2, B2, 'logsig', P, T, tp );

% try it out

[m n] = size( I );
I_adj = adjust( I );
m = m - size_y;
n = n - size_x;
res = zeros( m, n );
for ( i = 1:m )
  for ( j = 1:n )
    testmat = I_adj(i:i+size_y-1, j*2:(j+size_x-1)*2-1 );
    [A1 A2] =
		simuff(testmat(:), W1, B1, 'logsig', W2, B2, 'logsig');
    res(i,j) = A2;
  end;
end;

dspim( res );

% try to find the highest activation
max_val = max( res(:));
[maxi, maxj] = find( res >= max_val );
% and draw a square around the spot in the first image
max_s = max( I(:));
I( maxi(1), maxj(1):maxj(1)+size_x-1 ) =
  max_s * ones( 1, size_x );
I( maxi(1)+size_y-1, maxj(1):maxj(1)+size_x-1 ) =
  max_s * ones( 1, size_x );
I( maxi(1):maxi(1)+size_y-1 , maxj(1)) =
  max_s * ones( size_y, 1 );
I( maxi(1):maxi(1)+size_y-1 , maxj(1)+size_x-1 ) =
  max_s * ones( size_y, 1 );

dspim( I );



Adrian Perrig
Wed Jun 5 22:28:55 MET DST 1996