-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
666980f
commit 6f41b9f
Showing
11 changed files
with
455 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
function bestfis=BEEFCN(fis,data) | ||
% Variables | ||
p0=GettingFuzzyParameters(fis); | ||
Problem.CostFunction=@(x) FuzzyCost(x,fis,data); | ||
Problem.nVar=numel(p0); | ||
alpha=1; | ||
Problem.VarMin=-(10^alpha); | ||
Problem.VarMax=10^alpha; | ||
% Bees Algorithm Parameters | ||
Params.MaxIt=15; | ||
Params.nScoutBee = 10; % Number of Scout Bees | ||
Params.nSelectedSite = round(0.5*Params.nScoutBee); % Number of Selected Sites | ||
Params.nEliteSite = round(0.4*Params.nSelectedSite); % Number of Selected Elite Sites | ||
Params.nSelectedSiteBee = round(0.5*Params.nScoutBee); % Number of Recruited Bees for Selected Sites | ||
Params.nEliteSiteBee = 2*Params.nSelectedSiteBee; % Number of Recruited Bees for Elite Sites | ||
Params.r = 0.1*(Problem.VarMax-Problem.VarMin); % Neighborhood Radius | ||
Params.rdamp = 0.95; % Neighborhood Radius Damp Rate | ||
% Starting Bees Algorithm | ||
results=Runbee(Problem,Params); | ||
% Getting the Results | ||
p=results.BestSol.Position.*p0; | ||
bestfis=FuzzyParameters(fis,p); | ||
end | ||
%% Bees | ||
function results=Runbee(Problem,Params) | ||
disp('Starting Bees Algorithm Training :)'); | ||
% Cost Function | ||
CostFunction=Problem.CostFunction; | ||
% Number of Decision Variables | ||
nVar=Problem.nVar; | ||
% Size of Decision Variables Matrixv | ||
VarSize=[1 nVar]; | ||
% Lower Bound of Variables | ||
VarMin=Problem.VarMin; | ||
% Upper Bound of Variables | ||
VarMax=Problem.VarMax; | ||
% Some Change | ||
if isscalar(VarMin) && isscalar(VarMax) | ||
dmax = (VarMax-VarMin)*sqrt(nVar); | ||
else | ||
dmax = norm(VarMax-VarMin); | ||
end | ||
%% Bees Algorithm Parameters | ||
MaxIt=Params.MaxIt; | ||
nScoutBee = Params.nScoutBee; % Number of Scout Bees | ||
nSelectedSite = Params.nSelectedSite; % Number of Selected Sites | ||
nEliteSite = Params.nEliteSite; % Number of Selected Elite Sites | ||
nSelectedSiteBee = Params.nSelectedSiteBee; % Number of Recruited Bees for Selected Sites | ||
nEliteSiteBee = Params.nEliteSiteBee; % Number of Recruited Bees for Elite Sites | ||
r = Params.r; % Neighborhood Radius | ||
rdamp = Params.rdamp; % Neighborhood Radius Damp Rate | ||
%% Second Stage | ||
% Empty Bee Structure | ||
empty_bee.Position = []; | ||
empty_bee.Cost = []; | ||
% Initialize Bees Array | ||
bee = repmat(empty_bee, nScoutBee, 1); | ||
% Create New Solutions | ||
for i = 1:nScoutBee | ||
bee(i).Position = unifrnd(VarMin, VarMax, VarSize); | ||
bee(i).Cost = CostFunction(bee(i).Position); | ||
end | ||
% Sort | ||
[~, SortOrder] = sort([bee.Cost]); | ||
bee = bee(SortOrder); | ||
% Update Best Solution Ever Found | ||
BestSol = bee(1); | ||
% Array to Hold Best Cost Values | ||
BestCost = zeros(MaxIt, 1); | ||
%% Bees Algorithm Main Body | ||
for it = 1:MaxIt | ||
% Elite Sites | ||
for i = 1:nEliteSite | ||
bestnewbee.Cost = inf; | ||
for j = 1:nEliteSiteBee | ||
newbee.Position = PerformBeeDance(bee(i).Position, r); | ||
newbee.Cost = CostFunction(newbee.Position); | ||
if newbee.Cost<bestnewbee.Cost | ||
bestnewbee = newbee; | ||
end | ||
end | ||
if bestnewbee.Cost<bee(i).Cost | ||
bee(i) = bestnewbee; | ||
end | ||
end | ||
% Selected Non-Elite Sites | ||
for i = nEliteSite+1:nSelectedSite | ||
bestnewbee.Cost = inf; | ||
for j = 1:nSelectedSiteBee | ||
newbee.Position = PerformBeeDance(bee(i).Position, r); | ||
newbee.Cost = CostFunction(newbee.Position); | ||
if newbee.Cost<bestnewbee.Cost | ||
bestnewbee = newbee; | ||
end | ||
end | ||
if bestnewbee.Cost<bee(i).Cost | ||
bee(i) = bestnewbee; | ||
end | ||
end | ||
% Non-Selected Sites | ||
for i = nSelectedSite+1:nScoutBee | ||
bee(i).Position = unifrnd(VarMin, VarMax, VarSize); | ||
bee(i).Cost = CostFunction(bee(i).Position); | ||
end | ||
% Sort | ||
[~, SortOrder] = sort([bee.Cost]); | ||
bee = bee(SortOrder); | ||
% Update Best Solution Ever Found | ||
BestSol = bee(1); | ||
% Store Best Cost Ever Found | ||
BestCost(it) = BestSol.Cost; | ||
% Display Iteration Information | ||
disp(['In Iteration Number ' num2str(it) ' Bees Algorithm Fittest Value Is = ' num2str(BestCost(it))]); | ||
% Damp Neighborhood Radius | ||
r = r*rdamp; | ||
end | ||
disp('Bees Algorithm Came To End :)'); | ||
% Store Res | ||
results.BestSol=BestSol; | ||
results.BestCost=BestCost; | ||
% Plot Bees Algorithm Training Stages | ||
set(gcf, 'Position', [600, 300, 500, 400]) | ||
plot(BestCost,':',... | ||
'LineWidth',2,... | ||
'MarkerSize',8,... | ||
'MarkerEdgeColor','g',... | ||
'Color',[0.1,0.9,0.1]); | ||
title('Bees Algorithm Training') | ||
xlabel('Bees Algorithm Iteration Number','FontSize',10,... | ||
'FontWeight','bold','Color','m'); | ||
ylabel('Bees Algorithm Fittest Value','FontSize',10,... | ||
'FontWeight','bold','Color','m'); | ||
legend({'Bees Algorithm Train'}); | ||
end | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
%% Bees CNN Algorithm (A Fuzzy Evolutionary Deep Leaning) - Created in 20 Jan 2022 by Seyed Muhammad Hossein Mousavi | ||
% It is possible to fit deep learning weights and bias using evolutionary | ||
% algorithm, right after training stage. Here, CNN is used to classify 8 | ||
% face classes. After CNN train, initial fuzzy model is created to aid the | ||
% learning process. Finally, CNN network weights (from Fully Connected Layer) | ||
% trains using Bees algorithm | ||
% to be fitted in a nature inspired manner (here behavior of Bees). You can | ||
% used your data with any number of samples and classes. Remember, code's | ||
% parameters are adjusted for this data and if you want to replace your | ||
% data you may have to change the parameters. Image data is in 64*64 size and | ||
% in 2 dimensions and stored in 'CNNDat' folder. So, important parameters | ||
% are as below: | ||
% 1. | ||
% 'numTrainFiles' = you have to change this based on number of your samples | ||
% in each class. for example if each class has 120 sample, 90 is good | ||
% enough as 90 samples considered for train and others for test. | ||
% 2. | ||
% 'imageInputLayer' = it is size of your image data like [64 64 1] | ||
% 3. | ||
% 'fullyConnectedLayer' = it is number of your classes like (8) | ||
% 4. | ||
% 'MaxEpochs' = the more the better and more computation run time like 40 | ||
% 5. | ||
% 'ClusNum' = Fuzzy C Means (FCM) Cluster Number like 3 or 4 is nice | ||
% 6. | ||
% These two are from "BEEFCN.m" function : | ||
% 'Params.MaxIt' = it is iteration number in Bees algorithm. 20 is good | ||
% 'Params.nScoutBee' = it is population number in Bees algorithm. Like 10. | ||
% ------------------------------------------------ | ||
% Feel free to contact me if you find any problem using the code: | ||
% Author: SeyedMuhammadHosseinMousavi | ||
% My Email: [email protected] | ||
% My Google Scholar: https://scholar.google.com/citations?user=PtvQvAQAAAAJ&hl=en | ||
% My GitHub: https://github.com/SeyedMuhammadHosseinMousavi?tab=repositories | ||
% My ORCID: https://orcid.org/0000-0001-6906-2152 | ||
% My Scopus: https://www.scopus.com/authid/detail.uri?authorId=57193122985 | ||
% My MathWorks: https://www.mathworks.com/matlabcentral/profile/authors/9763916# | ||
% my RG: https://www.researchgate.net/profile/Seyed-Mousavi-17 | ||
% ------------------------------------------------ | ||
% Hope it help you, enjoy the code and wish me luck :) | ||
|
||
%% Cleaning | ||
clear; | ||
clc; | ||
warning('off'); | ||
|
||
%% CNN Deep Neural Network | ||
% Load the deep sample data as an image datastore. | ||
deepDatasetPath = fullfile('CNNDat'); | ||
imds = imageDatastore(deepDatasetPath, ... | ||
'IncludeSubfolders',true, ... | ||
'LabelSource','foldernames'); | ||
% Divide the data into training and validation data sets | ||
numTrainFiles = 90; | ||
[imdsTrain,imdsValidation] = splitEachLabel(imds,numTrainFiles,'randomize'); | ||
% Define the convolutional neural network architecture. | ||
layers = [ | ||
% Image Input Layer An imageInputLayer | ||
imageInputLayer([64 64 1]) | ||
% Convolutional Layer | ||
convolution2dLayer(3,8,'Padding','same') | ||
% Batch Normalization | ||
batchNormalizationLayer | ||
% ReLU Layer The batch | ||
reluLayer | ||
% Max Pooling Layer | ||
% More values means less weights | ||
maxPooling2dLayer(4,'Stride',4) | ||
%------------------------------ | ||
convolution2dLayer(3,8,'Padding','same') | ||
batchNormalizationLayer | ||
reluLayer | ||
maxPooling2dLayer(5,'Stride',5) | ||
convolution2dLayer(3,8,'Padding','same') | ||
batchNormalizationLayer | ||
reluLayer | ||
% Fully Connected Layer (Number of Classes) | ||
fullyConnectedLayer(8) | ||
% Softmax Layer | ||
softmaxLayer | ||
% Classification Layer The final layer | ||
classificationLayer]; | ||
% Specify the training options | ||
options = trainingOptions('sgdm', ... | ||
'InitialLearnRate',0.001, ... | ||
'MaxEpochs',20, ... | ||
'Shuffle','every-epoch', ... | ||
'ValidationData',imdsValidation, ... | ||
'ValidationFrequency',8, ... | ||
'Verbose',false, ... | ||
'Plots','training-progress'); | ||
% Train the network | ||
[net,info]= trainNetwork(imdsTrain,layers,options); | ||
|
||
%% Bees Algorithm Weight Fitting | ||
% Converting Serial Network to an Object | ||
netobj = net.saveobj; | ||
% Extracting Fully Connected Layer's Weights To Evolve | ||
FullConn=netobj.Layers(13, 1).Weights; | ||
netbias=netobj.Layers(13, 1).Bias; | ||
|
||
%% Data for Each Weight | ||
sizefinal=size(FullConn); | ||
sizefinal=sizefinal(1,1); | ||
for i=1:sizefinal | ||
Inputs=FullConn(i,:); | ||
Targets=Inputs; | ||
data.Inputs=Inputs; | ||
data.Targets=Targets; | ||
datam{i}=JustLoad(data); | ||
end; | ||
|
||
%% Making Basic Fuzzy Model for Each Class Weight | ||
% Fuzzy C Means (FCM) Cluster Number | ||
ClusNum=3; | ||
% Creating Initial Fuzzy Model to Employ for Each Class Weight | ||
for i=1:sizefinal | ||
fism{i}=GenerateFuzzy(datam{i},ClusNum); | ||
end | ||
|
||
%% Tarining Bees Algorithm | ||
% Fitting Fully Connected Layer's Weights with Bees Algorithm | ||
for i=1:sizefinal | ||
disp(['Bees Are Working on Weights of Class # (' num2str(i) ')']); | ||
BeesFISm{i}=BEEFCN(fism{i},datam{i}); | ||
end; | ||
|
||
%% Train Output Extraction | ||
for i=1:sizefinal | ||
TrTar{i}=datam{i}.TrainTargets; | ||
TrInp{i}=datam{i}.TrainInputs; | ||
TrainOutputs{i}=evalfis(TrInp{i},BeesFISm{i}); | ||
end; | ||
% Train Errors | ||
for i=1:sizefinal | ||
tmp=datam{i}; | ||
tt=tmp.TrainTargets; | ||
tp=TrainOutputs{i}; | ||
Errors{i}=tt-tp; | ||
MSE{i}=mean(Errors{i}.^2); | ||
RMSE{i}=sqrt(MSE{i}); | ||
error_mean{i}=mean(Errors{i}); | ||
error_std{i}=std(Errors{i}); | ||
end; | ||
% Convereting Output Cell to Matrix | ||
for i=1:sizefinal | ||
EvolvedFullConn(i,:)=TrainOutputs{i}'; | ||
end; | ||
|
||
%% Replacing Evolved Weights | ||
netobj.Layers(13, 1).Weights=EvolvedFullConn; | ||
% New Network | ||
Newnet=netobj.Layers; | ||
% Converting Network to Serial Network | ||
BeesNet = assembleNetwork(Newnet); | ||
|
||
%% Predict The Labels | ||
% Common CNN Accuracy | ||
YPred = classify(net,imdsValidation); | ||
YValidation = imdsValidation.Labels; | ||
CNNaccuracy = sum(YPred == YValidation)/numel(YValidation); | ||
% Bees CNN Accuracy | ||
YPredbee = classify(BeesNet,imdsValidation); | ||
YValidationbee = imdsValidation.Labels; | ||
Beesaccuracy = sum(YPredbee == YValidationbee)/numel(YValidationbee); | ||
|
||
%% Confusion Matrix | ||
figure; | ||
plotconfusion(YPred,YValidation); | ||
title(['CNN Accuracy = ' num2str(CNNaccuracy)]); | ||
figure; | ||
plotconfusion(YPredbee,YValidationbee); | ||
title(['Bees-CNN Accuracy = ' num2str(Beesaccuracy)]); | ||
|
||
%% Statistics | ||
fprintf('The CNN Accuracy Is = %0.4f.\n',CNNaccuracy*100) | ||
fprintf('The Bees CNN Accuracy Is = %0.4f.\n',Beesaccuracy*100) | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function [z, out]=FuzzyCost(x,fis,data) | ||
MinAbs=1e-5; | ||
if any(abs(x)<MinAbs) | ||
S=(abs(x)<MinAbs); | ||
x(S)=MinAbs.*sign(x(S)); | ||
end | ||
p0=GettingFuzzyParameters(fis); | ||
p=x.*p0; | ||
fis=FuzzyParameters(fis,p); | ||
x=data.TrainInputs; | ||
t=data.TrainTargets; | ||
y=evalfis(x,fis); | ||
e=t-y; | ||
MSE=mean(e(:).^2); | ||
RMSE=sqrt(MSE); | ||
z=RMSE; | ||
out.fis=fis; | ||
out.MSE=MSE; | ||
out.RMSE=RMSE; | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function fis=FuzzyParameters(fis,p) | ||
nInput=numel(fis.input); | ||
for i=1:nInput | ||
nMF=numel(fis.input(i).mf); | ||
for j=1:nMF | ||
k=numel(fis.input(i).mf(j).params); | ||
fis.input(i).mf(j).params=p(1:k); | ||
p(1:k)=[]; | ||
end | ||
end | ||
nOutput=numel(fis.output); | ||
for i=1:nOutput | ||
nMF=numel(fis.output(i).mf); | ||
for j=1:nMF | ||
k=numel(fis.output(i).mf(j).params); | ||
fis.output(i).mf(j).params=p(1:k); | ||
p(1:k)=[]; | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
function fis=GenerateFuzzy(data,nCluster) | ||
if ~exist('nCluster','var') | ||
nCluster='auto'; | ||
end | ||
x=data.TrainInputs; | ||
t=data.TrainTargets; | ||
% Important Params | ||
fcm_U=2; | ||
fcm_MaxIter=100; | ||
fcm_MinImp=1e-5; | ||
% | ||
fcm_Display=false; | ||
fcm_options=[fcm_U fcm_MaxIter fcm_MinImp fcm_Display]; | ||
fis=genfis3(x,t,'sugeno',nCluster,fcm_options); | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function p=GettingFuzzyParameters(fis) | ||
p=[]; | ||
nInput=numel(fis.input); | ||
for i=1:nInput | ||
nMF=numel(fis.input(i).mf); | ||
for j=1:nMF | ||
p=[p fis.input(i).mf(j).params]; | ||
end | ||
end | ||
nOutput=numel(fis.output); | ||
for i=1:nOutput | ||
nMF=numel(fis.output(i).mf); | ||
for j=1:nMF | ||
p=[p fis.output(i).mf(j).params]; | ||
end | ||
end | ||
end |
Oops, something went wrong.