MATLAB Answers

Convert Image Classification Network into Regression Network using ResNet18

18 views (last 30 days)
Tried above task following this example but have error
I will appreciate it if anyone can help, and thanks in advance. The images are in augmented image datastore
My code:
net = resnet18;
layers = net.Layers;
numResponses = 1;
layers = [
layers(1:68)
fullyConnectedLayer(numResponses)
regressionLayer];
options = trainingOptions('sgdm',...
'InitialLearnRate',0.001, ...
'ValidationData',{augimdsImagesP11Test,P11Test_betaRepBSN8},...
'Plots','training-progress',...
'Verbose',false);
net2 = trainNetwork(augimdsImagesP11Train,P11Train2_beta,layers,options);
ERROR =
Error using trainNetwork (line 170)
Layers argument must be an array of layers or a layer graph.

Accepted Answer

Madhav Thakker
Madhav Thakker on 8 Sep 2020
Hi Kenneth,
I understand that you want to convert the pretrained resent-18 to regression network.
While training the network, we must pass the Layer graph or an array of layers to our network. I am attaching a code snippet that does the same with resnet-18 network and modifies the network to be used as a regression network.
net = resnet18;
lgraph = layerGraph(net);
numClasses = 1;
newLearnableLayer = fullyConnectedLayer(numClasses, ...
'Name','new_fc', ...
'WeightLearnRateFactor',10, ...
'BiasLearnRateFactor',10);
regress = regressionLayer('Name', 'new_reg');
lgraph = replaceLayer(lgraph,"fc1000",newLearnableLayer);
lgraph = removeLayers(lgraph, "ClassificationLayer_predictions");
lgraph = replaceLayer(lgraph,"prob",regress);
Note that, the input to the network is same as the pretrained resnet-18, i.e., images with shape (224, 224, 3).
Hope this helps.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!