Fine-tuning resnet18 (transfer learning) - layer connections error

29 views (last 30 days)
Nour Mohamed
Nour Mohamed on 25 Jan 2021
Commented: Mahesh Taparia on 4 Feb 2021
I want to fine-tune the pretrained resnet18 model, but when I follow the steps on the website: https://www.mathworks.com/help/deeplearning/ug/transfer-learning-using-alexnet.html
I get an error about the layer connections in the netwrok and I can not retrain it. How can I preserve all the layer connections and not just the layers from resnet18?
the error:
Error using trainNetwork (line 170)
Invalid network.
Error in fine_tune (line 67)
netTransfer = trainNetwork(imdsTrain,lgraph,options);
Caused by:
Layer 'res2a': Unconnected input. Each layer input must be connected to the output of another layer.
Detected unconnected inputs:
input 'in2'
Layer 'res2b': Unconnected input. Each layer input must be connected to the output of another layer.
Detected unconnected inputs:
input 'in2'
Layer 'res3a': Unconnected input. Each layer input must be connected to the output of another layer.
Detected unconnected inputs:
input 'in2'
Layer 'res3b': Unconnected input. Each layer input must be connected to the output of another layer.
Detected unconnected inputs:
input 'in2'
Layer 'res4a': Unconnected input. Each layer input must be connected to the output of another layer.
Detected unconnected inputs:
input 'in2'
Layer 'res4b': Unconnected input. Each layer input must be connected to the output of another layer.
Detected unconnected inputs:
input 'in2'
Layer 'res5a': Unconnected input. Each layer input must be connected to the output of another layer.
Detected unconnected inputs:
input 'in2'
Layer 'res5b': Unconnected input. Each layer input must be connected to the output of another layer.
Detected unconnected inputs:
input 'in2'

Answers (1)

Mahesh Taparia
Mahesh Taparia on 28 Jan 2021
Hi
It seems that the network input layer is not connected to the next layer or the lgraph is having some unconnected layers. Use analyzeNetwork to check the network connection, it will show the unconnected layers. Use connectLayers to connect two layers.
If it does n't work then, can you share your code? It will give more clarity.
Hope it will help!
  2 Comments
Mahesh Taparia
Mahesh Taparia on 4 Feb 2021
Hi
It seems you want to remove last few layers of the dlnetwork. Taking only the layer part of the network will lose the layerGraph property. For example, in your case you want to remove last 3 layers and want to add some layer. You can remove these layers by using removeLayers as given below:
net=resnet18;
lgraph=layerGraph(net);
lgraph = removeLayers(lgraph,{'ClassificationLayer_predictions','prob','fc1000'});
analyzeNetwork(lgraph)
After this you use connectLayers to connect the other layers to the layergraph. You can also check the documentation of replaceLayer here. Hope it will help!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!