Convert Image Classification Network into Regression Network using ResNet18
20 views (last 30 days)
Show older comments
Kenneth Afebu
on 1 Sep 2020
Commented: Kenneth Afebu
on 8 Sep 2020
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.
0 Comments
Accepted Answer
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).
For more information, see https://www.mathworks.com/help/deeplearning/ug/convert-classification-network-into-regression-network.html
Hope this helps.
More Answers (0)
See Also
Categories
Find more on Image Data Workflows 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!