Is it possible to change the kernel size of ResNet50 pretrained networks?

8 visualizzazioni (ultimi 30 giorni)
Below is my code to change the kernel size in the pretrained network ResNet50, however it turns out these errors
  • "Unable to set the 'FilterSize' property of class 'Convolution2DLayer' because it is read-only." and
  • "Error in (line 41) net.Layers(convLayerIndex).FilterSize = newKernelSize;"
May I know the problem of this code? and how can I solve this?
% Load the pre-trained ResNet50 model
net = resnet50;
% Display network layers
disp(net.Layers);
% Get input layer size
Input_Layer_Size = net.Layers(1).InputSize(1:2);
% Resize training and validation images
Resized_Training_Image = augmentedImageDatastore(Input_Layer_Size, DatasetTrain);
Resized_Validation_Image = augmentedImageDatastore(Input_Layer_Size, DatasetValidation);
% Define new layers
Number_of_Classes = numel(categories(DatasetTrain.Labels));
New_Feature_Learner = fullyConnectedLayer(Number_of_Classes, ...
'Name', 'Disease_Feature_Learner', ...
'WeightLearnRateFactor', 10, ...
'BiasLearnRateFactor', 10);
New_Classifier_Layer = classificationLayer('Name', 'Disease_Classifier');
% Replace layers in the network
net = layerGraph(net);
net = replaceLayer(net, 'fc1000', New_Feature_Learner);
net = replaceLayer(net, 'ClassificationLayer_fc1000', New_Classifier_Layer);
% Find the convolutional layer whose kernel size you want to modify
convLayerName = 'conv1'; % Change this to the name of the specific layer you want to modify
convLayerIndex = arrayfun(@(l) strcmp(l.Name, convLayerName), net.Layers);
if any(convLayerIndex)
% Modify the kernel size of the convolutional layer
newKernelSize = [5, 5]; % Change this to the desired new kernel size [height, width]
net.Layers(convLayerIndex).FilterSize = newKernelSize;
else
disp('Convolutional layer not found in the network.');
end
% Set up training options
Size_of_Minibatch = 4;
Validation_Frequency = floor(numel(Resized_Training_Image.Files) / Size_of_Minibatch);
Training_Options = trainingOptions('sgdm', ...
'MiniBatchSize', Size_of_Minibatch, ...
'MaxEpochs', 6, ...
'InitialLearnRate', 3e-4, ...
'Shuffle', 'every-epoch', ...
'ValidationData', Resized_Validation_Image, ...
'ValidationFrequency', Validation_Frequency, ...
'Verbose', false, ...
'Plots', 'training-progress');
% Train the network
net = trainNetwork(Resized_Training_Image, net, Training_Options);

Risposte (1)

Binaya
Binaya il 14 Gen 2024
Hi Amiera
I understand that you would like to change the filter size of one of the convolution layers of the "ResNet50" pre-trained model.
The task you are trying to accomplish is not possible for the following reasons:
  1. The "ResNet50" model is already pre-trained in MATLAB.
  2. When this model is loaded, the trained model weights are already assigned into the network.
  3. Trying to modify the filter size of a convolution layer of this network would lead to change in number of weights of this new model which does leads to non-existent weights.
  4. To avoid this, the convolution layer hyperparameters are "read-only" properties.
If you want the hyperparameters to be changed, you can try the following steps:
  1. Load the "net" into a layer graph(say "lgraph") using "layerGraph".
  2. Create a new convolution layer using "convolution2dLayer" function.
  3. Replace the layer corresponding to "net.Layers(convLayerIndex)" in "lgraph" with the new convolution layer using "replaceLayer" function.
  4. Convert the layer graph "lgraph" into a new network using "assembleNetwork" function.
Please refer to the following documentation for more details:
  1. "layerGraph": https://www.mathworks.com/help/deeplearning/ref/nnet.cnn.layergraph.html
  2. "replaceLayer": https://www.mathworks.com/help/deeplearning/ref/nnet.cnn.layergraph.replacelayer.html
  3. "convolution2dLayer": https://www.mathworks.com/help/deeplearning/ref/nnet.cnn.layer.convolution2dlayer.html
  4. "assembleNetwork": https://www.mathworks.com/help/deeplearning/ref/assemblenetwork.html
I hope this helps
Regards
Binaya

Categorie

Scopri di più su Image Data Workflows in Help Center e File Exchange

Prodotti


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by