|
| 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 | + |
| 42 | +%% Cleaning |
| 43 | + clear; |
| 44 | + clc; |
| 45 | + warning('off'); |
| 46 | + |
| 47 | +%% CNN Deep Neural Network |
| 48 | +% Load the deep sample data as an image datastore. |
| 49 | +deepDatasetPath = fullfile('CNNDat'); |
| 50 | +imds = imageDatastore(deepDatasetPath, ... |
| 51 | + 'IncludeSubfolders',true, ... |
| 52 | + 'LabelSource','foldernames'); |
| 53 | +% Divide the data into training and validation data sets |
| 54 | +numTrainFiles = 90; |
| 55 | +[imdsTrain,imdsValidation] = splitEachLabel(imds,numTrainFiles,'randomize'); |
| 56 | +% Define the convolutional neural network architecture. |
| 57 | +layers = [ |
| 58 | +% Image Input Layer An imageInputLayer |
| 59 | + imageInputLayer([64 64 1]) |
| 60 | +% Convolutional Layer |
| 61 | +convolution2dLayer(3,8,'Padding','same') |
| 62 | +% Batch Normalization |
| 63 | + batchNormalizationLayer |
| 64 | +% ReLU Layer The batch |
| 65 | + reluLayer |
| 66 | +% Max Pooling Layer |
| 67 | + % More values means less weights |
| 68 | + maxPooling2dLayer(4,'Stride',4) |
| 69 | + %------------------------------ |
| 70 | + convolution2dLayer(3,8,'Padding','same') |
| 71 | + batchNormalizationLayer |
| 72 | + reluLayer |
| 73 | + maxPooling2dLayer(5,'Stride',5) |
| 74 | + convolution2dLayer(3,8,'Padding','same') |
| 75 | + batchNormalizationLayer |
| 76 | + reluLayer |
| 77 | +% Fully Connected Layer (Number of Classes) |
| 78 | + fullyConnectedLayer(8) |
| 79 | +% Softmax Layer |
| 80 | + softmaxLayer |
| 81 | +% Classification Layer The final layer |
| 82 | + classificationLayer]; |
| 83 | +% Specify the training options |
| 84 | +options = trainingOptions('sgdm', ... |
| 85 | + 'InitialLearnRate',0.001, ... |
| 86 | + 'MaxEpochs',20, ... |
| 87 | + 'Shuffle','every-epoch', ... |
| 88 | + 'ValidationData',imdsValidation, ... |
| 89 | + 'ValidationFrequency',8, ... |
| 90 | + 'Verbose',false, ... |
| 91 | + 'Plots','training-progress'); |
| 92 | +% Train the network |
| 93 | +[net,info]= trainNetwork(imdsTrain,layers,options); |
| 94 | + |
| 95 | +%% Bees Algorithm Weight Fitting |
| 96 | +% Converting Serial Network to an Object |
| 97 | +netobj = net.saveobj; |
| 98 | +% Extracting Fully Connected Layer's Weights To Evolve |
| 99 | +FullConn=netobj.Layers(13, 1).Weights; |
| 100 | +netbias=netobj.Layers(13, 1).Bias; |
| 101 | + |
| 102 | +%% Data for Each Weight |
| 103 | +sizefinal=size(FullConn); |
| 104 | +sizefinal=sizefinal(1,1); |
| 105 | +for i=1:sizefinal |
| 106 | +Inputs=FullConn(i,:); |
| 107 | +Targets=Inputs; |
| 108 | +data.Inputs=Inputs; |
| 109 | +data.Targets=Targets; |
| 110 | +datam{i}=JustLoad(data); |
| 111 | +end; |
| 112 | + |
| 113 | +%% Making Basic Fuzzy Model for Each Class Weight |
| 114 | +% Fuzzy C Means (FCM) Cluster Number |
| 115 | +ClusNum=3; |
| 116 | +% Creating Initial Fuzzy Model to Employ for Each Class Weight |
| 117 | +for i=1:sizefinal |
| 118 | +fism{i}=GenerateFuzzy(datam{i},ClusNum); |
| 119 | +end |
| 120 | + |
| 121 | +%% Tarining Bees Algorithm |
| 122 | +% Fitting Fully Connected Layer's Weights with Bees Algorithm |
| 123 | +for i=1:sizefinal |
| 124 | +disp(['Bees Are Working on Weights of Class # (' num2str(i) ')']); |
| 125 | +BeesFISm{i}=BEEFCN(fism{i},datam{i}); |
| 126 | +end; |
| 127 | + |
| 128 | +%% Train Output Extraction |
| 129 | +for i=1:sizefinal |
| 130 | +TrTar{i}=datam{i}.TrainTargets; |
| 131 | +TrInp{i}=datam{i}.TrainInputs; |
| 132 | +TrainOutputs{i}=evalfis(TrInp{i},BeesFISm{i}); |
| 133 | +end; |
| 134 | +% Train Errors |
| 135 | +for i=1:sizefinal |
| 136 | +tmp=datam{i}; |
| 137 | +tt=tmp.TrainTargets; |
| 138 | +tp=TrainOutputs{i}; |
| 139 | +Errors{i}=tt-tp; |
| 140 | +MSE{i}=mean(Errors{i}.^2); |
| 141 | +RMSE{i}=sqrt(MSE{i}); |
| 142 | +error_mean{i}=mean(Errors{i}); |
| 143 | +error_std{i}=std(Errors{i}); |
| 144 | +end; |
| 145 | +% Convereting Output Cell to Matrix |
| 146 | +for i=1:sizefinal |
| 147 | +EvolvedFullConn(i,:)=TrainOutputs{i}'; |
| 148 | +end; |
| 149 | + |
| 150 | +%% Replacing Evolved Weights |
| 151 | +netobj.Layers(13, 1).Weights=EvolvedFullConn; |
| 152 | +% New Network |
| 153 | +Newnet=netobj.Layers; |
| 154 | +% Converting Network to Serial Network |
| 155 | +BeesNet = assembleNetwork(Newnet); |
| 156 | + |
| 157 | +%% Predict The Labels |
| 158 | +% Common CNN Accuracy |
| 159 | +YPred = classify(net,imdsValidation); |
| 160 | +YValidation = imdsValidation.Labels; |
| 161 | +CNNaccuracy = sum(YPred == YValidation)/numel(YValidation); |
| 162 | +% Bees CNN Accuracy |
| 163 | +YPredbee = classify(BeesNet,imdsValidation); |
| 164 | +YValidationbee = imdsValidation.Labels; |
| 165 | +Beesaccuracy = sum(YPredbee == YValidationbee)/numel(YValidationbee); |
| 166 | + |
| 167 | +%% Confusion Matrix |
| 168 | +figure; |
| 169 | +plotconfusion(YPred,YValidation); |
| 170 | +title(['CNN Accuracy = ' num2str(CNNaccuracy)]); |
| 171 | +figure; |
| 172 | +plotconfusion(YPredbee,YValidationbee); |
| 173 | +title(['Bees-CNN Accuracy = ' num2str(Beesaccuracy)]); |
| 174 | + |
| 175 | +%% Statistics |
| 176 | +fprintf('The CNN Accuracy Is = %0.4f.\n',CNNaccuracy*100) |
| 177 | +fprintf('The Bees CNN Accuracy Is = %0.4f.\n',Beesaccuracy*100) |
| 178 | + |
0 commit comments