CNN Code
CNN Code
clear removes all variables from the current workspace, releasing them from system memory.
clc;
clear all;
close all;
fullfile(filepart1,...,filepartN) builds a full file specification from the specified folder and file
names.
imageFolder = fullfile("../br/br/Testing/");
imageDatastore creates a datastore from the collection of image data specified by location.
countEachLabel counts the number of times each unique label occurs in the datastore.
tb1 = countEachLabel(imds)
1 glioma 300
2 meningioma 306
3 notumor 405
4 pituitary 300
minSetCount = min(tb1{:,2});
maxNumImages = 300;
minSetCount = min(maxNumImages,minSetCount);
splitEachLabel randomly assigns the specified proportion of files from each label to the new datastores.
imds = splitEachLabel(imds,minSetCount,'randomized');
tb2 = countEachLabel(imds)
1 glioma 300
2 meningioma 300
3 notumor 300
1
Label Count
4 pituitary 300
Spliting the imds datastore into imdstrain datastore with 70% of imds datastore and imdtest datastore with
30% of imds datastore.
[imdstrain,imdstest] = splitEachLabel(imds,0.7,'randomized');
imageSize=[256 256 1];
An augmented image datastore transforms training dataset with optional preprocessing such as resizing,
grayconversion.
datastore = augmentedImageDatastore(imageSize,imdstrain,"ColorPreprocessing","rgb2gray"
A batch normalization layer normalizes a mini-batch of data across all observations for each channel
independently.
A ReLU layer performs a threshold operation to each element of the input, where any value less than zero is set
to zero.
A 2-D max pooling layer performs downsampling by dividing the input into rectangular pooling regions, then
computing the maximum of each region.
A fully connected layer multiplies the input by a weight matrix and then adds a bias vector.
A classification layer computes the cross-entropy loss for classification and weighted classification tasks with
mutually exclusive classes.
layers = [ ...
imageInputLayer(imageSize,'Name','input')
convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
2
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,64,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(32)
reluLayer
fullyConnectedLayer(16)
reluLayer
fullyConnectedLayer(8)
fullyConnectedLayer(4)
softmaxLayer
classificationLayer ];
lgraph = layerGraph(layers);
figure
plot(lgraph)
3
trainingOptions returns training options with additional options specified by one or more name-value
arguments.
net = trainNetwork(datastore,layers,options);
save('net',"net")
An augmented image datastore transforms test dataset with optional preprocessing such as resizing,
grayconversion.
imdsTest_rsz = augmentedImageDatastore(imageSize,imdstest,'ColorPreprocessing','rgb2gra
imdsTest_rsz =
augmentedImageDatastore with properties:
NumObservations: 360
Files: {360×1 cell}
AlternateFileSystemRoots: {}
MiniBatchSize: 128
DataAugmentation: 'none'
ColorPreprocessing: 'rgb2gray'
OutputSize: [256 256]
4
OutputSizeMode: 'resize'
DispatchInBackground: 0
load net
YPred = classify(net,imdsTest_rsz);
cm = confusionchart(imdstest.Labels,YPred);
cm.Title = 'Brain Tumor Classification Using CNN';
Accuracy:
accuracy = 0.7944
5
6