Error using nnet.cnn.L​ayerGraph>​iThrowErro​rIfStartLa​yerIsOutpu​tLayer Unable to connect to the layer 'Classific​ationLayer​_fc1000'. This layer is an output layer and has no outp

15 views (last 30 days)
Hi everyone,
I am trying to learn deep learning but I am getting the error. here is the code.
gTruth(1:4,:)
rng(0);
shuffledIndices = randperm(height(gTruth));
idx = floor(0.6 * length(shuffledIndices) );
trainingData = gTruth(shuffledIndices(1:idx),:);
testData = gTruth(shuffledIndices(idx+1:end),:);
imdsTrain = imageDatastore(trainingData{:,'imageFilename'});
bldsTrain = boxLabelDatastore(trainingData(:,2:end));
imdsTest = imageDatastore(testData{:,'imageFilename'});
bldsTest = boxLabelDatastore(testData(:,2:end));
trainingData = combine(imdsTrain,bldsTrain);
testData = combine(imdsTest, bldsTest);
data = read(trainingData);
I = data{1};
bbox = data{2};
annotatedImage = insertShape(I,'rectangle',bbox);
annotatedImage = imresize(annotatedImage,2);
figure
imshow(annotatedImage)
net = resnet50();
lgraph = layerGraph(net);
inputSize = [2872 920 3];
classNames = {'orangecone','bluecone', 'yellowcone'};
ssdLayerGraph = removeLayers(lgraph,'activation_40_relu');
weightsInitializerValue = 'glorot';
biasInitializerValue = 'zeros';
% Append Extra layers on top of a base network.
extraLayers = [];
% Add conv6_1 and corresponding reLU
filterSize = 1;
numFilters = 256;
numChannels = 1024;
conv6_1 = convolution2dLayer(filterSize, numFilters, NumChannels = numChannels, ...
Name = 'conv6_1', ...
WeightsInitializer = weightsInitializerValue, ...
BiasInitializer = biasInitializerValue);
relu6_1 = reluLayer(Name = 'relu6_1');
extraLayers = [extraLayers; conv6_1; relu6_1];
% Add conv6_2 and corresponding reLU
filterSize = 3;
numFilters = 512;
numChannels = 256;
conv6_2 = convolution2dLayer(filterSize, numFilters, NumChannels = numChannels, ...
Padding = "same", ...
Stride = [2, 2], ...
Name = 'conv6_2', ...
WeightsInitializer = weightsInitializerValue, ...
BiasInitializer = biasInitializerValue);
relu6_2 = reluLayer(Name = 'relu6_2');
extraLayers = [extraLayers; conv6_2; relu6_2];
% Add conv7_1 and corresponding reLU
filterSize = 1;
numFilters = 128;
numChannels = 512;
conv7_1 = convolution2dLayer(filterSize, numFilters, NumChannels = numChannels, ...
Name = 'conv7_1', ...
WeightsInitializer = weightsInitializerValue, ...
BiasInitializer = biasInitializerValue);
relu7_1 = reluLayer(Name = 'relu7_1');
extraLayers = [extraLayers; conv7_1; relu7_1];
% Add conv7_2 and corresponding reLU
filterSize = 3;
numFilters = 256;
numChannels = 128;
conv7_2 = convolution2dLayer(filterSize, numFilters, NumChannels = numChannels, ...
Padding = "same", ...
Stride = [2, 2], ...
Name = 'conv7_2', ...
WeightsInitializer = weightsInitializerValue, ...
BiasInitializer = biasInitializerValue);
relu7_2 = reluLayer(Name = 'relu7_2');
extraLayers = [extraLayers; conv7_2; relu7_2];
% Add conv8_1 and corresponding reLU
filterSize = 1;
numFilters = 128;
numChannels = 256;
conv8_1 = convolution2dLayer(filterSize, numFilters, NumChannels = numChannels, ...
Name = 'conv8_1', ...
WeightsInitializer = weightsInitializerValue, ...
BiasInitializer = biasInitializerValue);
relu8_1 = reluLayer(Name = 'relu8_1');
extraLayers = [extraLayers; conv8_1; relu8_1];
% Add conv8_2 and corresponding reLU
filterSize = 3;
numFilters = 256;
numChannels = 128;
conv8_2 = convolution2dLayer(filterSize, numFilters, NumChannels = numChannels, ...
Name = 'conv8_2', ...
WeightsInitializer = weightsInitializerValue, ...
BiasInitializer = biasInitializerValue);
relu8_2 = reluLayer(Name ='relu8_2');
extraLayers = [extraLayers; conv8_2; relu8_2];
% Add conv9_1 and corresponding reLU
filterSize = 1;
numFilters = 128;
numChannels = 256;
conv9_1 = convolution2dLayer(filterSize, numFilters, NumChannels = numChannels, ...
Padding = "same", ...
Name = 'conv9_1', ...
WeightsInitializer = weightsInitializerValue, ...
BiasInitializer = biasInitializerValue);
relu9_1 = reluLayer('Name', 'relu9_1');
extraLayers = [extraLayers; conv9_1; relu9_1];
if ~isempty(extraLayers)
lastLayerName = ssdLayerGraph.Layers(end).Name;
ssdLayerGraph = addLayers(ssdLayerGraph, extraLayers);
ssdLayerGraph = connectLayers(ssdLayerGraph, lastLayerName, extraLayers(1).Name);
end
error :
Error using nnet.cnn.LayerGraph>iThrowErrorIfStartLayerIsOutputLayer
Unable to connect to the layer 'ClassificationLayer_fc1000'. This layer is an output layer and has no
outputs to other layers.
Error in nnet.cnn.LayerGraph>iGetSourceInformation (line 629)
iThrowErrorIfStartLayerIsOutputLayer( layers(startLayerIndex) );
Error in nnet.cnn.LayerGraph/connectLayers (line 295)
[startLayerName,startLayerIndex,~, layerOutputIndex] = iGetSourceInformation(s, layers);
>>
  1 Comment
Rahul
Rahul on 5 Jan 2023
I have analyzed the code shared by you. Please make the changes in the code as shown below. You will see the layered graph.
if ~isempty(extraLayers)
lastLayerName = ssdLayerGraph.Layers(end).Name;
ssdLayerGraph = addLayers(ssdLayerGraph, extraLayers);
%ssdLayerGraph = connectLayers(ssdLayerGraph, lastLayerName, extraLayers(1).Name);
end
analyzeNetwork(ssdLayerGraph)
In this, you will find issues in the attached image.
Please confirm the connections, its inputs and outputs. Based on that, you can connect your layers as per that information. Please refer to the documentation page of "addLayers", "connectLayers" for your reference.

Sign in to comment.

Answers (1)

Rahul
Rahul on 9 Jan 2023
I have analyzed the code shared by you. Please make the changes in the code as shown below. You will see the layered graph.
if ~isempty(extraLayers)
lastLayerName = ssdLayerGraph.Layers(end).Name;
ssdLayerGraph = addLayers(ssdLayerGraph, extraLayers);
%ssdLayerGraph = connectLayers(ssdLayerGraph, lastLayerName, extraLayers(1).Name);
end
analyzeNetwork(ssdLayerGraph)
Please confirm the connections, its inputs and outputs. Based on that, you can connect your layers as per that information. Please refer to the documentation page of "addLayers", "connectLayers" for your reference.

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!