Convert Image Classification Network into Regression Network using ResNet18

조회 수: 5 (최근 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에서 Deep Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by