Semantic Segmentation - How many layers to replace in transfer learning?
Show older comments
Im doing semantic segmentation using Resnet-18 with Deeplab v3+ (https://www.mathworks.com/help/vision/examples/semantic-segmentation-using-deep-learning.html)
However, I want to retain on progressively harder tasks and want to use transfer learning. How many of the final layers should I be replacing? How do I figure out how many layers I have?
On the analyzeNetwork it says I have 101 but I am using Resnet - 18 which I thought had much less?
4 Comments
Tran Thanh
on 6 May 2019
I can not find see helperDeeplabv3PlusResnet18.m file. Where can I find this file? Would you mind to send this file for me?
Philip Li
on 8 May 2019
Hi, I also can not find this file anywhere. Were you able to get it?
Tohru Kikawada
on 11 May 2019
Edited: Tohru Kikawada
on 11 May 2019
Philip, you'll need to try this on R2019a since the example has been revised to use DeepLab v3 instead of SegNet in the latest vresion.
Guy Reading
on 27 Sep 2019
I've been doing a bit of digging on the resnet18() <-> DeepLab V3+ connection, in this link MATLAB writes:
"This example creates the Deeplab v3+ network with weights initialized from a pre-trained Resnet-18 network"
But then we might ask, "how do we use the weights trained on one network to be used in another? Won't they be meaningless relative to Deeplab?
"The latest implementation of DeepLab supports multiple network backbones, like MobileNetv2, Xception, ResNet-v1, PNASNET and Auto-DeepLab."
So I guess we can treat Deeplab V3+ as some form of extension of resnet18 and thus can use the weights.
Answers (2)
Tohru Kikawada
on 2 May 2019
0 votes
Did you see helperDeeplabv3PlusResnet18.m which is attached to the example as a supporting file? The supporting function might be helpful to create your own transfer learning network.
4 Comments
awezmm
on 6 May 2019
Tohru Kikawada
on 6 May 2019
Edited: Tohru Kikawada
on 6 May 2019
Type the following command
openExample('deeplearning_shared/SemanticSegmentationUsingDeepLearningExample')
then the helperDeeplabv3PlusResnet18.m can be found in your current folder.
awezmm
on 7 May 2019
Tohru Kikawada
on 7 May 2019
Sorry for the confusion. Someone asked me where the file can be found.
Guy Reading
on 23 Sep 2019
Did you make any progress on this, @awezmm? I'm looking to do the same thing as you, with Resnet-18, too, and I got stuck at the same point as you so Googled & found your question here!
So far I've followed the (adapted for resnet) instructions of this tutorial:
So for resnet that'd be:
%% load a pre-trained CNN
pretrainedFolder = fullfile(tempdir,'pretrainedNetwork');
pretrainedNetwork = fullfile(pretrainedFolder,'deeplabv3plusResnet18CamVid.mat');
data = load(pretrainedNetwork);
net = data.net;
layers = net.Layers
This shows me all 101 layers for resnet. For me, personally, I'd like to classify 2 things (background or object) so I've edited the final layer to give me 2 things, but I'm pretty sure I need to do more layers and unsure which ones:
%% Modify the network to use 2 categories
layers(101) = pixelClassificationLayer; % note, in the example he uses classificationLayer as it's not semantic seg
& now I'm stuck! I'll comment r.e. letting you know any progress I've made... I'm going to look into the structure of resnet more, now, to get a better understanding of what I need to change and how...
5 Comments
Guy Reading
on 23 Sep 2019
Update:
I think these are the layers you need to change:
%% Modify the network to use 2 categories
numClasses = 2;
layers(97) = convolution2dLayer(1,numClasses);
layers(98) = transposedConv2dLayer(2, 8, 'Stride', 4, 'Cropping', 2);
layers(99) = crop2dLayer('Name', 'dec_crop2'); % crop layer requires connections
layers(100) = softmaxLayer();
layers(101) = pixelClassificationLayer;
If you stop here and then look at what we've made using the following function, we can see that we've broken a lot of connections of the CNN:
analyzeNetwork(layers);
So right now I'm looking to try and re-build those. I think we can do that with the following functions but I'm not sure...
%% creating connections
% Create a layerGraph. The first input of crop2dLayer is automatically
% connected to the first output of the image input layer.
lgraph = layerGraph(layers);
% Connect the second input to the image layer output.
lgraph = connectLayers(lgraph, 'image', 'crop/ref')
Examples of the above functions taken from here: https://uk.mathworks.com/help/deeplearning/examples/train-deep-learning-network-to-classify-new-images.html
Guy Reading
on 23 Sep 2019
Okay I've got a network with no errors showing when analysing it with analyzeNetwork(), I'll see if this has actually adapted the network correctly tomorrow, though, when I feed it some data. I couldn't use the function noted in the previous comment, connectLayers, as it doesn't work with a DAGNetwork:
%% load a pre-trained CNN
pretrainedFolder = fullfile(tempdir,'pretrainedNetwork');
pretrainedNetwork = fullfile(pretrainedFolder,'deeplabv3plusResnet18CamVid.mat');
data = load(pretrainedNetwork);
net = data.net;
layers = net.Layers;
lgraph = layerGraph(net);
%% Modify the network to use 2 categories
numClasses = 2;
objectCategories = categorical({'Lump','Background'});
newScorerLayer = convolution2dLayer(1,numClasses, ...
'Name', 'NewScorer',...
'NumChannels', 256,...
'WeightLearnRateFactor', 1,...
'BiasLearnRateFactor', 1);
newdec_ups2Layer = transposedConv2dLayer(8, 2, ...
'Stride', 4, ...
'Cropping', 2, ...
'Name', 'newdec_upsample2',...
'NumChannels', 2);
newDec_crop2Layer = crop2dLayer('centercrop', ...
'Name', 'dec_crop2'); % crop layer requires connections
newSoftmaxLayer = softmaxLayer('Name', 'newSoftmax-out');
newLabelsLayer = pixelClassificationLayer('Name', 'newLabels',...
'Classes', objectCategories);
% taken from: https://uk.mathworks.com/help/deeplearning/examples/train-deep-learning-network-to-classify-new-images.html
lgraph = replaceLayer(lgraph, 'scorer', newScorerLayer);
lgraph = replaceLayer(lgraph, 'dec_upsample2', newdec_ups2Layer);
lgraph = replaceLayer(lgraph, 'dec_crop2', newDec_crop2Layer);
lgraph = replaceLayer(lgraph, 'softmax-out', newSoftmaxLayer);
lgraph = replaceLayer(lgraph, 'labels', newLabelsLayer);
% check we have something that looks okay
analyzeNetwork(lgraph);
awezmm
on 23 Sep 2019
Guy Reading
on 24 Sep 2019
Oh right! So I looked to replace the layers which are specific to the amount of classes we want to categorise into. So layers 97:101 refer to layers which have a dimension set to 11, which was the original amount of classes:

Which is why I chose those. But I'm not 100% certain! Have you got this working for you at all? / what's the intuition behind picking the last layer, only?
Guy Reading
on 26 Sep 2019
To all that are reading: the above method worked for me & I'm starting to get labelled images from my model. I'm still not sure about which layers to freeze, if there are any suggestions with that I'd be interested to hear!
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!