Filter löschen
Filter löschen

How to use removeLayers without layerNames (How to remove layers if the layers don't have names)?

10 Ansichten (letzte 30 Tage)
When I am using transfer learning with ResNet50 I am removing the last 3 layers of ResNet as follows:
net = resnet50;
lgraph = layerGraph(net);
lgraph = removeLayers(lgraph, {'fc1000','fc1000_softmax','ClassificationLayer_fc1000'});
However, syntax for removeLayers is
newlgraph = removeLayers(lgraph,layerNames)
Right now I am trying to use transfer learning with MobileNetv2, but the last 3 layers are not named and doesn't have layerNames specified after checking the layers with the following code:
net = mobilenetv2;
lgraph = layerGraph(net);
alllayers = lgraph.Layers
How can I remove the last 3 layers or n number of layers of MobileNetv2 in order to use transfer learning with my own classifier?
  2 Kommentare
Somdip Dey
Somdip Dey am 24 Okt. 2019
The output of 'ver' command as follows:
MATLAB Version: (R2019b) Update 1
MATLAB License Number: 955201
Operating System: Mac OS X Version: 10.14.4 Build: 18E226
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
MATLAB Version 9.7 (R2019b)
Simulink Version 10.0 (R2019b)
Computer Vision Toolbox Version 9.1 (R2019b)
Deep Learning Toolbox Version 13.0 (R2019b)
Image Acquisition Toolbox Version 6.1 (R2019b)
Image Processing Toolbox Version 11.0 (R2019b)
Statistics and Machine Learning Toolbox Version 11.6 (R2019b)
Symbolic Math Toolbox Version 8.4 (R2019b)
My code is as follows:
%%Load pre-trained network model
net = mobilenetv2;
lgraph = layerGraph(net);
%%Load DataSet
imds = imageDatastore('/SOMEPATH/DS', ...
'IncludeSubfolders',true, ...
%Split DataSet into two sets: Training & Validation. Here Split is done on
%9:1 ratio.
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.75,'randomized');
%Get inputSize of images
inputSize = net.Layers(1).InputSize;
%Remove last layer (3 layers from array)
lgraph = removeLayers(lgraph, {'fc1000','fc1000_softmax','ClassificationLayer_fc1000'});
%Fetch num of classes
numClasses = numel(categories(imdsTrain.Labels));
%Add a fully connected layer, a dropout layer, a softmax layer
newlayers = [
lgraph = addLayers(lgraph,newlayers);
lgraph = connectLayers(lgraph,'avg_pool','lastFCCustom');
layers = lgraph.Layers;
connections = lgraph.Connections;
layers(1:111) = freezeWeights(layers(1:111));
lgraph = createLgraphUsingConnections(layers,connections);
The output in the console is as follow:
Error using nnet.cnn.LayerGraph>iValidateLayerName (line 663)
Layer 'ClassificationLayer_fc1000' does not exist.
Error in nnet.cnn.LayerGraph/removeLayers (line 234)
iValidateLayerName( ...
Error in mobilenetv2_custom (line 32)
lgraph = removeLayers(lgraph,

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jalaj Gambhir
Jalaj Gambhir am 18 Okt. 2019
I cannot reproduce the issue at my end. The same snippet of code that you mentioned, gives the layer details including the Layer Names of all 155 Layers of MobileNetv2. And, I am able to remove layers with the procedure you mentioned with resnet50.
Another method of creating a new layer graph with the required layer is to use addLayers as mentioned below:
newlgraph = layerGraph;
net = mobilenetv2;
lgraph = layerGraph(net);
alllayers = lgraph.Layers;
newlgraph = addLayers(newlgraph, alllayers(1:end-3))

Community Treasure Hunt

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

Start Hunting!

Translated by