From d3470e99540c634cbe37aaf5af5882f446bbdf5d Mon Sep 17 00:00:00 2001 From: Somdip Dey Date: Wed, 27 Jun 2018 18:10:27 +0100 Subject: [PATCH] updated matlab code --- source/matlab/resnet50_custom.m | 188 ++++++++++++++++++++++++++++++ source/matlab/resnet50_layers.txt | 180 ++++++++++++++++++++++++++++ source/matlab/vgg16_custom.m | 11 +- 3 files changed, 373 insertions(+), 6 deletions(-) create mode 100644 source/matlab/resnet50_custom.m create mode 100644 source/matlab/resnet50_layers.txt diff --git a/source/matlab/resnet50_custom.m b/source/matlab/resnet50_custom.m new file mode 100644 index 0000000..9d2d92d --- /dev/null +++ b/source/matlab/resnet50_custom.m @@ -0,0 +1,188 @@ +function resnet50_custom() + %%Load pre-trained network model + net = resnet50; + lgraph = layerGraph(net); + + %%Load DataSet + imds = imageDatastore('/Users/somdipdey/Downloads/Motorway-Traffic-Categorised', ... + 'IncludeSubfolders',true, ... + 'LabelSource','foldernames'); + + %Split DataSet into two sets: Training & Validation. Here Split is done on + %9:1 ratio. + [imdsTrain,imdsValidation] = splitEachLabel(imds,0.75,'randomized'); + + %Select 9 random image numbers to show + numTrainImages = numel(imdsTrain.Labels); + idx = randperm(numTrainImages,9); + figure + + %Show images of 9 randoms + for i = 1:9 + subplot(3,3,i) + I = readimage(imdsTrain,idx(i)); + imshow(I); + title(char(imdsTrain.Labels(idx(i)))); + end + + %Get inputSize of images + inputSize = net.Layers(1).InputSize; + + %Remove last layer (3 layers from array) + lgraph = removeLayers(lgraph, {'fc1000','fc1000_softmax','ClassificationLayer_fc1000'}); + + %Fetch num of classes + numClasses = numel(categories(imdsTrain.Labels)); + + %Add a fully connected layer, a dropout layer, a softmax layer + newlayers = [ + fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20,'Name','lastFCCustom') + dropoutLayer(0.5,'Name','DROPOUT') + softmaxLayer('Name','softmax') + classificationLayer('Name','classoutput')]; + + lgraph = addLayers(lgraph,newlayers); + lgraph = connectLayers(lgraph,'avg_pool','lastFCCustom'); + + layers = lgraph.Layers; + + connections = lgraph.Connections; + + layers(1:111) = freezeWeights(layers(1:111)); + lgraph = createLgraphUsingConnections(layers,connections); + + %Display extracted feature of the last fully connected layer, which is + %custom built on 4 categories --> + %last_layer = 39; + %last_layer_name = net.Layers(last_layer).Name; + %visual_channels = 1:6; + %thisI = deepDreamImage(net,last_layer,visual_channels, ... + % 'PyramidLevels',1, ... + % 'NumIterations',28); + %figure; + %montage(thisI); + %title(['Layer: ',last_layer_name,' Features']) + + %%Train network + % Augment dataset to prevent network from overfitting and memorizing + % training images + pixelRange = [-30 30]; + imageAugmenter = imageDataAugmenter( ... + 'RandXReflection',true, ... + 'RandXTranslation',pixelRange, ... + 'RandYTranslation',pixelRange); + + %Augment training dataset images along with resizing + augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ... + 'DataAugmentation',imageAugmenter); + + %Just resize the validation dataset + augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation); + + %Specify training options + options = trainingOptions('sgdm', ... + 'MiniBatchSize',10, ... + 'MaxEpochs',10, ... + 'InitialLearnRate',1e-4, ... + 'ValidationData',augimdsValidation, ... + 'ValidationFrequency',3, ... + 'ValidationPatience',Inf, ... + 'Verbose',false, ... + 'ExecutionEnvironment','cpu', ... + 'Plots','training-progress'); + + %Finally start training + netTransfer = trainNetwork(augimdsTrain,lgraph,options); + + %%Classify Validation Images + [YPred,scores] = classify(netTransfer,augimdsValidation); + %Display Predication and it's score + fprintf('Prediction: %s' , YPred , ', Scores: '); + disp(scores); + fprintf('\n'); + + %%Display nine sample validation images with their predicted labels + idx = randperm(numel(imdsValidation.Files),9); + figure + for i = 1:9 + subplot(3,3,i) + I = readimage(imdsValidation,idx(i)); + imshow(I) + label = YPred(idx(i)); + title(string(label)); + end + + %%Calculate the classification accuracy on the validation set. Accuracy is the fraction of labels that the network predicts correctly + YValidation = imdsValidation.Labels; + accuracy = mean(YPred == YValidation); + %Display Accuracy + fprintf('Accuracy: %s' , accuracy); + + %Test classification on a completely different set + %Just resize the validation dataset + %%Load DataSet + test_imds = imageDatastore('/Users/somdipdey/Documents/MATLAB/Add-Ons/Collections/Deep Learning Tutorial Series/code/AHS/test_dataset', ... + 'IncludeSubfolders',true, ... + 'LabelSource','foldernames'); + test_augimds = augmentedImageDatastore(inputSize(1:2),test_imds); + %testing dataset if not needed + %%Classify Test Images + %[YPred2,scores2] = classify(netTransfer,test_augimdsValidation);% use if augmented + %test dataset is used for testing + [YPred2,scores2] = classify(netTransfer,test_augimds); + %Display Predication and it's score + fprintf('\n Testing on a completely new dataset \n'); + fprintf('Prediction: %s' , YPred2 , ', Scores: '); + disp(scores2); + fprintf('\n'); + %%Calculate the classification accuracy on the validation set. Accuracy is the fraction of labels that the network predicts correctly + test_YValidation = test_imds.Labels; + accuracy2 = mean(YPred2 == test_YValidation); + %Display Accuracy + fprintf('\n Accuracy: %s \n' , accuracy2); + + %%Display nine sample validation images with their predicted labels + idx2 = randperm(numel(test_imds.Files),9); + figure + for i = 1:9 + subplot(3,3,i) + I = readimage(test_imds,idx2(i)); + imshow(I) + label = YPred2(idx2(i)); + title(string(label)); + end +end + +% layers = freezeWeights(layers) sets the learning rates of all the +% parameters of the layers in the layer array |layers| to zero. + +function layers = freezeWeights(layers) + +for ii = 1:size(layers,1) + props = properties(layers(ii)); + for p = 1:numel(props) + propName = props{p}; + if ~isempty(regexp(propName, 'LearnRateFactor$', 'once')) + layers(ii).(propName) = 0; + end + end +end + +end + +% lgraph = createLgraphUsingConnections(layers,connections) creates a layer +% graph with the layers in the layer array |layers| connected by the +% connections in |connections|. + +function lgraph = createLgraphUsingConnections(layers,connections) + +lgraph = layerGraph(); +for i = 1:numel(layers) + lgraph = addLayers(lgraph,layers(i)); +end + +for c = 1:size(connections,1) + lgraph = connectLayers(lgraph,connections.Source{c},connections.Destination{c}); +end + +end \ No newline at end of file diff --git a/source/matlab/resnet50_layers.txt b/source/matlab/resnet50_layers.txt new file mode 100644 index 0000000..27e0688 --- /dev/null +++ b/source/matlab/resnet50_layers.txt @@ -0,0 +1,180 @@ +177x1 Layer array with layers: + + 1 'input_1' Image Input 224x224x3 images with 'zerocenter' normalization + 2 'conv1' Convolution 64 7x7x3 convolutions with stride [2 2] and padding [3 3 3 3] + 3 'bn_conv1' Batch Normalization Batch normalization with 64 channels + 4 'activation_1_relu' ReLU ReLU + 5 'max_pooling2d_1' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0] + 6 'res2a_branch2a' Convolution 64 1x1x64 convolutions with stride [1 1] and padding [0 0 0 0] + 7 'bn2a_branch2a' Batch Normalization Batch normalization with 64 channels + 8 'activation_2_relu' ReLU ReLU + 9 'res2a_branch2b' Convolution 64 3x3x64 convolutions with stride [1 1] and padding 'same' + 10 'bn2a_branch2b' Batch Normalization Batch normalization with 64 channels + 11 'activation_3_relu' ReLU ReLU + 12 'res2a_branch2c' Convolution 256 1x1x64 convolutions with stride [1 1] and padding [0 0 0 0] + 13 'res2a_branch1' Convolution 256 1x1x64 convolutions with stride [1 1] and padding [0 0 0 0] + 14 'bn2a_branch2c' Batch Normalization Batch normalization with 256 channels + 15 'bn2a_branch1' Batch Normalization Batch normalization with 256 channels + 16 'add_1' Addition Element-wise addition of 2 inputs + 17 'activation_4_relu' ReLU ReLU + 18 'res2b_branch2a' Convolution 64 1x1x256 convolutions with stride [1 1] and padding [0 0 0 0] + 19 'bn2b_branch2a' Batch Normalization Batch normalization with 64 channels + 20 'activation_5_relu' ReLU ReLU + 21 'res2b_branch2b' Convolution 64 3x3x64 convolutions with stride [1 1] and padding 'same' + 22 'bn2b_branch2b' Batch Normalization Batch normalization with 64 channels + 23 'activation_6_relu' ReLU ReLU + 24 'res2b_branch2c' Convolution 256 1x1x64 convolutions with stride [1 1] and padding [0 0 0 0] + 25 'bn2b_branch2c' Batch Normalization Batch normalization with 256 channels + 26 'add_2' Addition Element-wise addition of 2 inputs + 27 'activation_7_relu' ReLU ReLU + 28 'res2c_branch2a' Convolution 64 1x1x256 convolutions with stride [1 1] and padding [0 0 0 0] + 29 'bn2c_branch2a' Batch Normalization Batch normalization with 64 channels + 30 'activation_8_relu' ReLU ReLU + 31 'res2c_branch2b' Convolution 64 3x3x64 convolutions with stride [1 1] and padding 'same' + 32 'bn2c_branch2b' Batch Normalization Batch normalization with 64 channels + 33 'activation_9_relu' ReLU ReLU + 34 'res2c_branch2c' Convolution 256 1x1x64 convolutions with stride [1 1] and padding [0 0 0 0] + 35 'bn2c_branch2c' Batch Normalization Batch normalization with 256 channels + 36 'add_3' Addition Element-wise addition of 2 inputs + 37 'activation_10_relu' ReLU ReLU + 38 'res3a_branch2a' Convolution 128 1x1x256 convolutions with stride [2 2] and padding [0 0 0 0] + 39 'bn3a_branch2a' Batch Normalization Batch normalization with 128 channels + 40 'activation_11_relu' ReLU ReLU + 41 'res3a_branch2b' Convolution 128 3x3x128 convolutions with stride [1 1] and padding 'same' + 42 'bn3a_branch2b' Batch Normalization Batch normalization with 128 channels + 43 'activation_12_relu' ReLU ReLU + 44 'res3a_branch2c' Convolution 512 1x1x128 convolutions with stride [1 1] and padding [0 0 0 0] + 45 'res3a_branch1' Convolution 512 1x1x256 convolutions with stride [2 2] and padding [0 0 0 0] + 46 'bn3a_branch2c' Batch Normalization Batch normalization with 512 channels + 47 'bn3a_branch1' Batch Normalization Batch normalization with 512 channels + 48 'add_4' Addition Element-wise addition of 2 inputs + 49 'activation_13_relu' ReLU ReLU + 50 'res3b_branch2a' Convolution 128 1x1x512 convolutions with stride [1 1] and padding [0 0 0 0] + 51 'bn3b_branch2a' Batch Normalization Batch normalization with 128 channels + 52 'activation_14_relu' ReLU ReLU + 53 'res3b_branch2b' Convolution 128 3x3x128 convolutions with stride [1 1] and padding 'same' + 54 'bn3b_branch2b' Batch Normalization Batch normalization with 128 channels + 55 'activation_15_relu' ReLU ReLU + 56 'res3b_branch2c' Convolution 512 1x1x128 convolutions with stride [1 1] and padding [0 0 0 0] + 57 'bn3b_branch2c' Batch Normalization Batch normalization with 512 channels + 58 'add_5' Addition Element-wise addition of 2 inputs + 59 'activation_16_relu' ReLU ReLU + 60 'res3c_branch2a' Convolution 128 1x1x512 convolutions with stride [1 1] and padding [0 0 0 0] + 61 'bn3c_branch2a' Batch Normalization Batch normalization with 128 channels + 62 'activation_17_relu' ReLU ReLU + 63 'res3c_branch2b' Convolution 128 3x3x128 convolutions with stride [1 1] and padding 'same' + 64 'bn3c_branch2b' Batch Normalization Batch normalization with 128 channels + 65 'activation_18_relu' ReLU ReLU + 66 'res3c_branch2c' Convolution 512 1x1x128 convolutions with stride [1 1] and padding [0 0 0 0] + 67 'bn3c_branch2c' Batch Normalization Batch normalization with 512 channels + 68 'add_6' Addition Element-wise addition of 2 inputs + 69 'activation_19_relu' ReLU ReLU + 70 'res3d_branch2a' Convolution 128 1x1x512 convolutions with stride [1 1] and padding [0 0 0 0] + 71 'bn3d_branch2a' Batch Normalization Batch normalization with 128 channels + 72 'activation_20_relu' ReLU ReLU + 73 'res3d_branch2b' Convolution 128 3x3x128 convolutions with stride [1 1] and padding 'same' + 74 'bn3d_branch2b' Batch Normalization Batch normalization with 128 channels + 75 'activation_21_relu' ReLU ReLU + 76 'res3d_branch2c' Convolution 512 1x1x128 convolutions with stride [1 1] and padding [0 0 0 0] + 77 'bn3d_branch2c' Batch Normalization Batch normalization with 512 channels + 78 'add_7' Addition Element-wise addition of 2 inputs + 79 'activation_22_relu' ReLU ReLU + 80 'res4a_branch2a' Convolution 256 1x1x512 convolutions with stride [2 2] and padding [0 0 0 0] + 81 'bn4a_branch2a' Batch Normalization Batch normalization with 256 channels + 82 'activation_23_relu' ReLU ReLU + 83 'res4a_branch2b' Convolution 256 3x3x256 convolutions with stride [1 1] and padding 'same' + 84 'bn4a_branch2b' Batch Normalization Batch normalization with 256 channels + 85 'activation_24_relu' ReLU ReLU + 86 'res4a_branch2c' Convolution 1024 1x1x256 convolutions with stride [1 1] and padding [0 0 0 0] + 87 'res4a_branch1' Convolution 1024 1x1x512 convolutions with stride [2 2] and padding [0 0 0 0] + 88 'bn4a_branch2c' Batch Normalization Batch normalization with 1024 channels + 89 'bn4a_branch1' Batch Normalization Batch normalization with 1024 channels + 90 'add_8' Addition Element-wise addition of 2 inputs + 91 'activation_25_relu' ReLU ReLU + 92 'res4b_branch2a' Convolution 256 1x1x1024 convolutions with stride [1 1] and padding [0 0 0 0] + 93 'bn4b_branch2a' Batch Normalization Batch normalization with 256 channels + 94 'activation_26_relu' ReLU ReLU + 95 'res4b_branch2b' Convolution 256 3x3x256 convolutions with stride [1 1] and padding 'same' + 96 'bn4b_branch2b' Batch Normalization Batch normalization with 256 channels + 97 'activation_27_relu' ReLU ReLU + 98 'res4b_branch2c' Convolution 1024 1x1x256 convolutions with stride [1 1] and padding [0 0 0 0] + 99 'bn4b_branch2c' Batch Normalization Batch normalization with 1024 channels + 100 'add_9' Addition Element-wise addition of 2 inputs + 101 'activation_28_relu' ReLU ReLU + 102 'res4c_branch2a' Convolution 256 1x1x1024 convolutions with stride [1 1] and padding [0 0 0 0] + 103 'bn4c_branch2a' Batch Normalization Batch normalization with 256 channels + 104 'activation_29_relu' ReLU ReLU + 105 'res4c_branch2b' Convolution 256 3x3x256 convolutions with stride [1 1] and padding 'same' + 106 'bn4c_branch2b' Batch Normalization Batch normalization with 256 channels + 107 'activation_30_relu' ReLU ReLU + 108 'res4c_branch2c' Convolution 1024 1x1x256 convolutions with stride [1 1] and padding [0 0 0 0] + 109 'bn4c_branch2c' Batch Normalization Batch normalization with 1024 channels + 110 'add_10' Addition Element-wise addition of 2 inputs + 111 'activation_31_relu' ReLU ReLU + 112 'res4d_branch2a' Convolution 256 1x1x1024 convolutions with stride [1 1] and padding [0 0 0 0] + 113 'bn4d_branch2a' Batch Normalization Batch normalization with 256 channels + 114 'activation_32_relu' ReLU ReLU + 115 'res4d_branch2b' Convolution 256 3x3x256 convolutions with stride [1 1] and padding 'same' + 116 'bn4d_branch2b' Batch Normalization Batch normalization with 256 channels + 117 'activation_33_relu' ReLU ReLU + 118 'res4d_branch2c' Convolution 1024 1x1x256 convolutions with stride [1 1] and padding [0 0 0 0] + 119 'bn4d_branch2c' Batch Normalization Batch normalization with 1024 channels + 120 'add_11' Addition Element-wise addition of 2 inputs + 121 'activation_34_relu' ReLU ReLU + 122 'res4e_branch2a' Convolution 256 1x1x1024 convolutions with stride [1 1] and padding [0 0 0 0] + 123 'bn4e_branch2a' Batch Normalization Batch normalization with 256 channels + 124 'activation_35_relu' ReLU ReLU + 125 'res4e_branch2b' Convolution 256 3x3x256 convolutions with stride [1 1] and padding 'same' + 126 'bn4e_branch2b' Batch Normalization Batch normalization with 256 channels + 127 'activation_36_relu' ReLU ReLU + 128 'res4e_branch2c' Convolution 1024 1x1x256 convolutions with stride [1 1] and padding [0 0 0 0] + 129 'bn4e_branch2c' Batch Normalization Batch normalization with 1024 channels + 130 'add_12' Addition Element-wise addition of 2 inputs + 131 'activation_37_relu' ReLU ReLU + 132 'res4f_branch2a' Convolution 256 1x1x1024 convolutions with stride [1 1] and padding [0 0 0 0] + 133 'bn4f_branch2a' Batch Normalization Batch normalization with 256 channels + 134 'activation_38_relu' ReLU ReLU + 135 'res4f_branch2b' Convolution 256 3x3x256 convolutions with stride [1 1] and padding 'same' + 136 'bn4f_branch2b' Batch Normalization Batch normalization with 256 channels + 137 'activation_39_relu' ReLU ReLU + 138 'res4f_branch2c' Convolution 1024 1x1x256 convolutions with stride [1 1] and padding [0 0 0 0] + 139 'bn4f_branch2c' Batch Normalization Batch normalization with 1024 channels + 140 'add_13' Addition Element-wise addition of 2 inputs + 141 'activation_40_relu' ReLU ReLU + 142 'res5a_branch2a' Convolution 512 1x1x1024 convolutions with stride [2 2] and padding [0 0 0 0] + 143 'bn5a_branch2a' Batch Normalization Batch normalization with 512 channels + 144 'activation_41_relu' ReLU ReLU + 145 'res5a_branch2b' Convolution 512 3x3x512 convolutions with stride [1 1] and padding 'same' + 146 'bn5a_branch2b' Batch Normalization Batch normalization with 512 channels + 147 'activation_42_relu' ReLU ReLU + 148 'res5a_branch2c' Convolution 2048 1x1x512 convolutions with stride [1 1] and padding [0 0 0 0] + 149 'res5a_branch1' Convolution 2048 1x1x1024 convolutions with stride [2 2] and padding [0 0 0 0] + 150 'bn5a_branch2c' Batch Normalization Batch normalization with 2048 channels + 151 'bn5a_branch1' Batch Normalization Batch normalization with 2048 channels + 152 'add_14' Addition Element-wise addition of 2 inputs + 153 'activation_43_relu' ReLU ReLU + 154 'res5b_branch2a' Convolution 512 1x1x2048 convolutions with stride [1 1] and padding [0 0 0 0] + 155 'bn5b_branch2a' Batch Normalization Batch normalization with 512 channels + 156 'activation_44_relu' ReLU ReLU + 157 'res5b_branch2b' Convolution 512 3x3x512 convolutions with stride [1 1] and padding 'same' + 158 'bn5b_branch2b' Batch Normalization Batch normalization with 512 channels + 159 'activation_45_relu' ReLU ReLU + 160 'res5b_branch2c' Convolution 2048 1x1x512 convolutions with stride [1 1] and padding [0 0 0 0] + 161 'bn5b_branch2c' Batch Normalization Batch normalization with 2048 channels + 162 'add_15' Addition Element-wise addition of 2 inputs + 163 'activation_46_relu' ReLU ReLU + 164 'res5c_branch2a' Convolution 512 1x1x2048 convolutions with stride [1 1] and padding [0 0 0 0] + 165 'bn5c_branch2a' Batch Normalization Batch normalization with 512 channels + 166 'activation_47_relu' ReLU ReLU + 167 'res5c_branch2b' Convolution 512 3x3x512 convolutions with stride [1 1] and padding 'same' + 168 'bn5c_branch2b' Batch Normalization Batch normalization with 512 channels + 169 'activation_48_relu' ReLU ReLU + 170 'res5c_branch2c' Convolution 2048 1x1x512 convolutions with stride [1 1] and padding [0 0 0 0] + 171 'bn5c_branch2c' Batch Normalization Batch normalization with 2048 channels + 172 'add_16' Addition Element-wise addition of 2 inputs + 173 'activation_49_relu' ReLU ReLU + 174 'avg_pool' Average Pooling 7x7 average pooling with stride [7 7] and padding [0 0 0 0] + 175 'lastFCCustom' Fully Connected 4 fully connected layer + 176 'DROPOUT' Dropout layer 50% dropout + 177 'fc1000_softmax' Softmax softmax + 178 'ClassificationLayer_fc1000' Classification Output crossentropyex with 'tench' and 999 other classes diff --git a/source/matlab/vgg16_custom.m b/source/matlab/vgg16_custom.m index bd99513..6007529 100644 --- a/source/matlab/vgg16_custom.m +++ b/source/matlab/vgg16_custom.m @@ -9,7 +9,7 @@ function vgg16_custom() %Split DataSet into two sets: Training & Validation. Here Split is done on %9:1 ratio. - [imdsTrain,imdsValidation] = splitEachLabel(imds,0.75,'randomized'); + [imdsTrain,imdsValidation] = splitEachLabel(imds,0.9,'randomized'); %Select 9 random image numbers to show numTrainImages = numel(imdsTrain.Labels); @@ -110,17 +110,16 @@ function vgg16_custom() %Test classification on a completely different set %Just resize the validation dataset - %%Load DataSet + %%Load DataSet test_imds = imageDatastore('/Users/somdipdey/Documents/MATLAB/Add-Ons/Collections/Deep Learning Tutorial Series/code/AHS/test_dataset', ... 'IncludeSubfolders',true, ... 'LabelSource','foldernames'); - %test_augimdsValidation = - %augmentedImageDatastore(inputSize(1:2),test_imds);%no need to augment + test_augimds = augmentedImageDatastore(inputSize(1:2),test_imds);%no need to augment %testing dataset if not needed %%Classify Test Images %[YPred2,scores2] = classify(netTransfer,test_augimdsValidation);% use if augmented %test dataset is used for testing - [YPred2,scores2] = classify(netTransfer,test_imds); + [YPred2,scores2] = classify(netTransfer,test_augimds); %Display Predication and it's score fprintf('\n Testing on a completely new dataset \n'); fprintf('Prediction: %s' , YPred2 , ', Scores: '); @@ -130,7 +129,7 @@ function vgg16_custom() test_YValidation = test_imds.Labels; accuracy2 = mean(YPred2 == test_YValidation); %Display Accuracy - fprintf('\n Accuracy: %s' , accuracy2); + fprintf('\n Accuracy: %s \n' , accuracy2); %%Display nine sample validation images with their predicted labels idx2 = randperm(numel(test_imds.Files),9);