├── BEEFCN.m ├── Bees CNN.jpg ├── BeesCNNAlgorithm.m ├── CNNDat.rar ├── FuzzyCost.m ├── FuzzyParameters.m ├── GenerateFuzzy.m ├── GettingFuzzyParameters.m ├── JustLoad.m ├── LICENSE ├── PerformBeeDance.m ├── README.md └── ReadMe.txt /BEEFCN.m: -------------------------------------------------------------------------------- 1 | function bestfis=BEEFCN(fis,data) 2 | % Variables 3 | p0=GettingFuzzyParameters(fis); 4 | Problem.CostFunction=@(x) FuzzyCost(x,fis,data); 5 | Problem.nVar=numel(p0); 6 | alpha=1; 7 | Problem.VarMin=-(10^alpha); 8 | Problem.VarMax=10^alpha; 9 | % Bees Algorithm Parameters 10 | Params.MaxIt=15; 11 | Params.nScoutBee = 10; % Number of Scout Bees 12 | Params.nSelectedSite = round(0.5*Params.nScoutBee); % Number of Selected Sites 13 | Params.nEliteSite = round(0.4*Params.nSelectedSite); % Number of Selected Elite Sites 14 | Params.nSelectedSiteBee = round(0.5*Params.nScoutBee); % Number of Recruited Bees for Selected Sites 15 | Params.nEliteSiteBee = 2*Params.nSelectedSiteBee; % Number of Recruited Bees for Elite Sites 16 | Params.r = 0.1*(Problem.VarMax-Problem.VarMin); % Neighborhood Radius 17 | Params.rdamp = 0.95; % Neighborhood Radius Damp Rate 18 | % Starting Bees Algorithm 19 | results=Runbee(Problem,Params); 20 | % Getting the Results 21 | p=results.BestSol.Position.*p0; 22 | bestfis=FuzzyParameters(fis,p); 23 | end 24 | %% Bees 25 | function results=Runbee(Problem,Params) 26 | disp('Starting Bees Algorithm Training :)'); 27 | % Cost Function 28 | CostFunction=Problem.CostFunction; 29 | % Number of Decision Variables 30 | nVar=Problem.nVar; 31 | % Size of Decision Variables Matrixv 32 | VarSize=[1 nVar]; 33 | % Lower Bound of Variables 34 | VarMin=Problem.VarMin; 35 | % Upper Bound of Variables 36 | VarMax=Problem.VarMax; 37 | % Some Change 38 | if isscalar(VarMin) && isscalar(VarMax) 39 | dmax = (VarMax-VarMin)*sqrt(nVar); 40 | else 41 | dmax = norm(VarMax-VarMin); 42 | end 43 | %% Bees Algorithm Parameters 44 | MaxIt=Params.MaxIt; 45 | nScoutBee = Params.nScoutBee; % Number of Scout Bees 46 | nSelectedSite = Params.nSelectedSite; % Number of Selected Sites 47 | nEliteSite = Params.nEliteSite; % Number of Selected Elite Sites 48 | nSelectedSiteBee = Params.nSelectedSiteBee; % Number of Recruited Bees for Selected Sites 49 | nEliteSiteBee = Params.nEliteSiteBee; % Number of Recruited Bees for Elite Sites 50 | r = Params.r; % Neighborhood Radius 51 | rdamp = Params.rdamp; % Neighborhood Radius Damp Rate 52 | %% Second Stage 53 | % Empty Bee Structure 54 | empty_bee.Position = []; 55 | empty_bee.Cost = []; 56 | % Initialize Bees Array 57 | bee = repmat(empty_bee, nScoutBee, 1); 58 | % Create New Solutions 59 | for i = 1:nScoutBee 60 | bee(i).Position = unifrnd(VarMin, VarMax, VarSize); 61 | bee(i).Cost = CostFunction(bee(i).Position); 62 | end 63 | % Sort 64 | [~, SortOrder] = sort([bee.Cost]); 65 | bee = bee(SortOrder); 66 | % Update Best Solution Ever Found 67 | BestSol = bee(1); 68 | % Array to Hold Best Cost Values 69 | BestCost = zeros(MaxIt, 1); 70 | %% Bees Algorithm Main Body 71 | for it = 1:MaxIt 72 | % Elite Sites 73 | for i = 1:nEliteSite 74 | bestnewbee.Cost = inf; 75 | for j = 1:nEliteSiteBee 76 | newbee.Position = PerformBeeDance(bee(i).Position, r); 77 | newbee.Cost = CostFunction(newbee.Position); 78 | if newbee.Cost 7 | 8 | - 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: 9 | 10 | 11 | 12 | ![Bees CNN](https://user-images.githubusercontent.com/11339420/150426815-417019d7-f7af-4de2-890e-582411724840.jpg) 13 | 14 | - 1. 15 | - '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. 16 | - 2. 17 | - 'imageInputLayer' = it is size of your image data like [64 64 1] 18 | - 3. 19 | - 'fullyConnectedLayer' = it is number of your classes like (8) 20 | - 4. 21 | - 'MaxEpochs' = the more the better and more computation run time like 40 22 | - 5. 23 | - 'ClusNum' = Fuzzy C Means (FCM) Cluster Number like 3 or 4 is nice 24 | - 6. 25 | - These two are from "BEEFCN.m" function : 26 | - 'Params.MaxIt' = it is iteration number in Bees algorithm. 20 is good 'Params.nScoutBee' = it is population number in Bees algorithm. Like 10. 27 | 28 | 29 | - Feel free to contact me if you find any problem using the code: 30 | - Author: SeyedMuhammadHosseinMousavi 31 | - My Email: mosavi.a.i.buali@gmail.com 32 | - Hope it help you, enjoy the code and wish me luck :) 33 | 34 | 35 | -------------------------------------------------------------------------------- /ReadMe.txt: -------------------------------------------------------------------------------- 1 | %% Bees CNN Algorithm (A Fuzzy Evolutionary Deep Leaning) - Created in 20 Jan 2022 by Seyed Muhammad Hossein Mousavi 2 | % It is possible to fit deep learning weights and bias using evolutionary 3 | % algorithm, right after training stage. Here, CNN is used to classify 8 4 | % face classes. After CNN train, initial fuzzy model is created to aid the 5 | % learning process. Finally, CNN network weights (from Fully Connected Layer) 6 | % trains using Bees algorithm 7 | % to be fitted in a nature inspired manner (here behavior of Bees). You can 8 | % used your data with any number of samples and classes. Remember, code's 9 | % parameters are adjusted for this data and if you want to replace your 10 | % data you may have to change the parameters. Image data is in 64*64 size and 11 | % in 2 dimensions and stored in 'CNNDat' folder. So, important parameters 12 | % are as below: 13 | % 1. 14 | % 'numTrainFiles' = you have to change this based on number of your samples 15 | % in each class. for example if each class has 120 sample, 90 is good 16 | % enough as 90 samples considered for train and others for test. 17 | % 2. 18 | % 'imageInputLayer' = it is size of your image data like [64 64 1] 19 | % 3. 20 | % 'fullyConnectedLayer' = it is number of your classes like (8) 21 | % 4. 22 | % 'MaxEpochs' = the more the better and more computation run time like 40 23 | % 5. 24 | % 'ClusNum' = Fuzzy C Means (FCM) Cluster Number like 3 or 4 is nice 25 | % 6. 26 | % These two are from "BEEFCN.m" function : 27 | % 'Params.MaxIt' = it is iteration number in Bees algorithm. 20 is good 28 | % 'Params.nScoutBee' = it is population number in Bees algorithm. Like 10. 29 | % ------------------------------------------------ 30 | % Feel free to contact me if you find any problem using the code: 31 | % Author: SeyedMuhammadHosseinMousavi 32 | % My Email: mosavi.a.i.buali@gmail.com 33 | % My Google Scholar: https://scholar.google.com/citations?user=PtvQvAQAAAAJ&hl=en 34 | % My GitHub: https://github.com/SeyedMuhammadHosseinMousavi?tab=repositories 35 | % My ORCID: https://orcid.org/0000-0001-6906-2152 36 | % My Scopus: https://www.scopus.com/authid/detail.uri?authorId=57193122985 37 | % My MathWorks: https://www.mathworks.com/matlabcentral/profile/authors/9763916# 38 | % my RG: https://www.researchgate.net/profile/Seyed-Mousavi-17 39 | % ------------------------------------------------ 40 | % Hope it help you, enjoy the code and wish me luck :) 41 | --------------------------------------------------------------------------------