Want to add regularization (L2) in Resnet50 code
7 views (last 30 days)
Show older comments
Muhammad Zubair
on 29 Apr 2020
Commented: Muhammad Zubair
on 2 May 2020
Hi guys,
I am training my data using Resnet50 in CNN but data is overfit. I want to reduce overfitting. So I want to add Regularizatin L2. Can anybody tell me how to add L2 in my code. You can see my code below.
clear all
close all
imds = imageDatastore("E:\test\data", ...
'IncludeSubfolders',true,'LabelSource','foldernames');
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomize'); %70% for train 30% for test
net=resnet50; % for the first time,you have to download the package from Add-on explorer
%Replace Final Layers
numClasses = numel(categories(imdsTrain.Labels));
lgraph = layerGraph(net);
newFCLayer = fullyConnectedLayer(numClasses,'Name','new_fc','WeightLearnRateFactor',10,'BiasLearnRateFactor',10);
lgraph = replaceLayer(lgraph,'fc1000' ,newFCLayer);
newClassLayer = classificationLayer('Name','new_classoutput');
lgraph = replaceLayer(lgraph,'ClassificationLayer_predictions',newClassLayer);
%Train Network
inputSize = net.Layers(1).InputSize;
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain);
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation);
options = trainingOptions('sgdm', ...
'MiniBatchSize',10, ...
'MaxEpochs',20, ...
'InitialLearnRate',1e-3, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',5, ...
'Verbose',false, ...
'Plots','training-progress');
trainedNet = trainNetwork(augimdsTrain,lgraph,options);
YPred = classify(trainedNet,augimdsValidation);
accuracy = mean(YPred == imdsValidation.Labels)
C = confusionmat(imdsValidation.Labels,YPred)
cm = confusionchart(imdsValidation.Labels,YPred);
cm.Title = 'Confusion Matrix for Validation Data';
cm.ColumnSummary = 'column-normalized';
cm.RowSummary = 'row-normalized';
0 Comments
Accepted Answer
Srivardhan Gadila
on 2 May 2020
You can specify the L2 regularization factors for the weights and biases in Convolutional and Fully Connected Layers by specifying the BiasL2Factor and WeightL2Factor properties, respectively. trainNetwork then multiplies the L2 regularization factors that you specify by using trainingOptions with these factors.
For more informtion refer to setL2Factor, Solver Options, L2 Regularization, Set Up Parameters in Convolutional and Fully Connected Layers.
More Answers (0)
See Also
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!