我想在Matlab Resnet50中添加正则化(L2)

时间:2020-05-02 19:36:37

标签: matlab machine-learning image-processing deep-learning conv-neural-network

我正在使用CNN中的Resnet50训练数据,但是数据过拟合。我想减少过度拟合。所以我想添加正则化L2。有人可以告诉我如何在代码中添加L2吗?您可以在下面看到我的代码。

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';

1 个答案:

答案 0 :(得分:0)

在ResNet50 CNN模型上实施L2正则化

resnet_base = ResNet50(weights='imagenet', include_top=False, input_shape=(224,224,3))
alpha = 0.00002
for layer in resnet_base.layers:
  if isinstance(layer, keras.layers.Conv2D) or isinstance(layer, keras.layers.Dense):
    layer.add_loss(keras.regularizers.l2(alpha)(layer.kernel))
  if hasattr(layer, 'bias_regularizer') and layer.use_bias:
    layer.add_loss(keras.regularizers.l2(alpha)(layer.bias))

希望这会有所帮助!

相关问题