Convert Image Classification Network into Regression Network using ResNet18

3 次查看(过去 30 天)
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.

采纳的回答

Madhav Thakker
Madhav Thakker 2020-9-8
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by