MATLAB Answers

Errors in transfer learning using resnet101

12 ビュー (過去 30 日間)
KEN SUEMATSU
KEN SUEMATSU 2021 年 3 月 15 日
回答済み: Akira Agata 2021 年 3 月 18 日
I would like to use resnet101 to do transfer learning.
When I build the network and use the trainNetwork function as shown below, I get the following error. What is the cause?
Layer 'res2a': unconnected input. The input of each layer must be coupled with the output of another layer.
An unconnected input was detected:
net = resnet101;
layers = net.Layers;
layers = [
layers(1:344)
fullyConnectedLayer(Numberofclasses)
layers(346)
classificationLayer];
options = trainingOptions('sgdm',...
'MiniBatchSize',16,...
'InitialLearnRate', 0.0001, ...
...)
trainNetwork(TrainImage,TrainData,layers,options);

採用された回答

Akira Agata
Akira Agata 2021 年 3 月 18 日
Since ResNet-101 is imported as a DAGNetwork object, the following steps will be needed (more details can be found in this Link)
  1. Convert DAGNetwork object to LayerGraph object
  2. Replace the last few layers
  3. Freeze bias/weight of initial layers (optional)
  4. Re-connect all the layers in the original order by using the support function createLgraphUsingConnections
So the MATLAB code will be like this.
net = resnet101;
% 1. Convert DAGNetwork object to LayerGraph object
lgraph = layerGraph(net);
% 2. Replace the last few layers
lgraph = replaceLayer(lgraph,'fc1000',...
fullyConnectedLayer(Numberofclasses,'Name','fcNew'));
lgraph = replaceLayer(lgraph,'ClassificationLayer_predictions',...
classificationLayer('Name','ClassificationNew'));
% 4. Re-connect all the layers in the original order
% by using the support function createLgraphUsingConnections
layers = lgraph.Layers;
connections = lgraph.Connections;
lgraph = createLgraphUsingConnections(layers,connections);
% Train the network
options = trainingOptions('sgdm',...
'MiniBatchSize',16,...
'InitialLearnRate', 0.0001, ...
...)
net = trainNetwork(imdsTrain,lgraph,options);

その他の回答 (0 件)

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by