how to create a grayscale-image-input SSD_MobileNetV2 object detection network

7 次查看(过去 30 天)
Hi, I am trying to modify a pretrained MobileNet v2 network to create a SSD object detection network. But the training data of network need to be grayscale images, not RGB images anymore. The code is written reference to Create SSD Object Detection Network .
net = mobilenetv2();
lgraph = layerGraph(net);
%update network input size
imageInputSize = [300 300 1];
imgLayer = imageInputLayer(imageInputSize,"Name","input_1");
lgraph = replaceLayer(lgraph,"input_1",imgLayer);
%Select Feature Extraction Layers
featureExtractionLayer = "block_12_add";
%Remove Layers After Feature Extraction Layer
modified = load("mobilenetv2Block12Add.mat");
lgraph = modified.mobilenetv2Block12Add;
%Attach AnchorBoxLayer
numClasses = 1;
anchorBoxes = [204 184;152 134;141 191];
anchorBox = anchorBoxLayer(anchorBoxes,"Name","anchors");
lgraph = addLayers(lgraph,anchorBox);
lgraph = connectLayers(lgraph,"block_12_add","anchors");
%Create SSD Classifcation Branch
numAnchors = size(anchorBoxes,1);
numClassesPlusBackground = numClasses + 1;
numClsFilters = numAnchors * numClassesPlusBackground;
filterSize = 3;
conv = convolution2dLayer(filterSize,numClsFilters,...
"Name","convClassification",...
"Padding","same");
lgraph = addLayers(lgraph,conv);
lgraph = connectLayers(lgraph,"anchors","convClassification");
%Create SSD Regression Branch
numRegFilters = 4 * numAnchors;
conv = convolution2dLayer(filterSize,numRegFilters,...
"Name","convRegression",...
"Padding","same");
lgraph = addLayers(lgraph,conv);
lgraph = connectLayers(lgraph,"anchors","convRegression");
%Merge Classification Features
numFeatureExtractionLayers = numel(featureExtractionLayer);
mergeClassification = ssdMergeLayer(numClassesPlusBackground,numFeatureExtractionLayers,...
"Name","mergeClassification");
lgraph = addLayers(lgraph,mergeClassification);
lgraph = connectLayers(lgraph,"convClassification","mergeClassification/in1");
%Merge Regression Features
numCoordinates = 4;
mergeRegression = ssdMergeLayer(numCoordinates,numFeatureExtractionLayers,...
"Name","mergeRegression");
lgraph = addLayers(lgraph,mergeRegression);
lgraph = connectLayers(lgraph,"convRegression","mergeRegression/in1");
%Complete SSD Detection Network
clsLayers = [
softmaxLayer("Name","softmax")
focalLossLayer("Name","focalLoss")
];
lgraph = addLayers(lgraph,clsLayers);
lgraph = connectLayers(lgraph,"mergeClassification","softmax");
reg = rcnnBoxRegressionLayer("Name","boxRegression");
lgraph = addLayers(lgraph,reg);
lgraph = connectLayers(lgraph,"mergeRegression","boxRegression");
analyzeNetwork(lgraph)
But the result of command "analyzeNetwork(lgraph)" show that it finally creates a network with 300*300*3 three channel iuput,.It still create a network with RGB image input. How can I fix the issue? Thanks.

回答(1 个)

Nomit Jangid
Nomit Jangid 2020-11-30
Hi ceng,
You're changing layerGraph input in the 14th line as well. You can solve this issue by using your replaceLayer function after the 14th layer. Although, you might also have to change convolution layers as they are expecting three channels.
Hope this helps.

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by