Using GA and NN to find a cat face inside an image, problem in custom population and fitness function

Hello, I am currently working on a project to identify a cat face inside an image using GA, I had a trained neural network that can classify whether the input image is cat or not. My problem is in developing the GA to find the best window that can correspond to the cat face, I have developed a custom popoulation func & custom fitness func but I had this error Error in Genetic_Algorithm_NN>PopFunction (line 32) I = imread('C:\Users\Mohamed\Desktop\Neural Networks\Assignment 3\100cats\00000016_018.jpg');
Error using feval
Output argument "pop" (and maybe others) not assigned during call to
"C:\Users\Mohamed\Desktop\Neural Networks\Assignment
3\Genetic_Algorithm_NN.m>PopFunction".
Error in makeState (line 30)
state.Population =
feval(options.CreationFcn,GenomeLength,FitnessFcn,options,options.CreationFcnArgs{:});
Error in gaunc (line 41)
state =
makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 348)
[x,fval,exitFlag,output,population,scores] =
gaunc(FitnessFcn,nvars, ...
Error in Genetic_Algorithm_NN (line 24)
[chromosome,~,~,~,~,~] = ga(@FitFunc,nvars,options);
I am really stuck and I don't know what should be fixed, you kindly help is highly appreciated I will post the code in the next comment thanks

1 Comment

function cat_face = Genetic_Algorithm_NN
clear all
GenomeLength =3; % This is the number of features in the dataset
options = gaoptimset('CreationFcn', {@PopFunction},...
'PopulationSize',30,...
'Generations',100,...
'PopulationType', 'doubleVector',...
'SelectionFcn',{@selectionroulette},...
'MutationFcn',{@mutationuniform, 0.1},...
'CrossoverFcn', {@crossoverscattered,0.8},...
'EliteCount',2,...
'Vectorized','on',...
'StallGenLimit',100,...
'PlotFcns',{@gaplotbestf},...
'Display', 'iter');
nvars = 3;
[chromosome,~,~,~,~,~] = ga(@FitFunc,nvars,options);
Best_chromosome = chromosome;
cat_face = find(Best_chromosome==1);
end
%%%POPULATION FUNCTION
function [pop] = PopFunction(GenomeLength,FitnessFcn,options)
I = imread('C:\Users\Mohamed\Desktop\Neural Networks\Assignment 3\100cats\00000016_018.jpg');
[l,w,z]=size(I);
x = randi(w,[30,1]);
y = randi(l,[30,1]);
s = randi(min(l,w),[30,1]);
for i =1:30
if (x(i) + s(i) >w)
s(i) = w-x(i)-1;
return;
end
if (y(i) + s(i) >l)
s(i) = l-x(i)-1;
return;
end
end
temp = cat(2,x,y);
pop = cat(2,temp,s);
end
%%%FITNESS FUNCTION
function [FitVal] = FitFunc(pop)
load net.mat;
I = imread('C:\Users\Mohamed\Desktop\Neural Networks\Assignment 3\100cats\00000016_018.jpg');
results=zeros(30,1);
for i=1:30
crop = imcrop(I,[pop(i,1) pop(i,2) pop(i,3) pop(i,3)] );
%rescale it to 20x20 and change it to grayscale
I1= im2double(rgb2gray( imresize(crop,[20 20])));
%change it to a vector
IV(:,1)=I1(:);
%pass it to the neural network
output = net(IV);
%if output is high then it is cat, else it is not
results(i,1)=output(1);
end
FitVal =results ;
end

Sign in to comment.

 Accepted Answer

Mohamed - the error message is telling you that there is something wrong with your function to generate the populations and so the output parameter pop, the initial population is not being assigned. Put a breakpoint in the first line of this function and then run the code. What happens? Note that you have two return calls in your code that fire if certain conditions are met. If these fire before pop has been created, then the error makes sense.

1 Comment

Thanks alot GEOFF,
I removed the two return and it worked perfectly, now I have another error regarding crossoverfnc
below is the error Error using crossoversinglepoint Too many input arguments.
Error in stepGA (line 35) xoverKids = feval(options.CrossoverFcn, parents(1:(2 * nXoverKids)),options,GenomeLength,FitnessFcn,thisScore,thisPopulation,options.CrossoverFcnArgs{:});
Error in gaunc (line 85) [score,population,state] = stepGA(score,population,options,state,GenomeLength,FitnessFcn);
Error in ga (line 348) [x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in Genetic_Algorithm_NN (line 24) [chromosome,~,~,~,~,~] = ga(@FitFunc,nvars,options);

Sign in to comment.

More Answers (1)

It is now working , the crossover and mutation function does not work with integer problems
thanks again GEOFF, your advice was very helpful :)

2 Comments

hello sir ,please tell me how to write a code on fitness function. My fitness funtion = Accuracy + (no of absent attributes* balancing factor ) i.e balancing factor =0.1 please help me ,iam basic learner in matlab .

Sign in to comment.

Categories

Tags

Asked:

on 17 Dec 2014

Commented:

on 15 Mar 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!