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