Invalid training data. The output size (1000) of the last layer does not match the number of classes (5).

37 views (last 30 days)
Create Layer Graph
Create the layer graph variable to contain the network layers.
lgraph = layerGraph();
Add Layer Branches
Add the branches of the network to the layer graph. Each branch is a linear array of layers.
tempLayers = [
imageInputLayer([227 227 3],"Name","data","Mean",params.data.Mean)
convolution2dLayer([3 3],64,"Name","conv1","BiasLearnRateFactor",10,"Stride",[2 2],"WeightLearnRateFactor",10,"Bias",params.conv1.Bias,"Weights",params.conv1.Weights)
reluLayer("Name","relu_conv1")
maxPooling2dLayer([3 3],"Name","pool1","Stride",[2 2])
convolution2dLayer([1 1],16,"Name","fire2-squeeze1x1","BiasLearnRateFactor",10,"WeightLearnRateFactor",10,"Bias",params.fire2_squeeze1x1.Bias,"Weights",params.fire2_squeeze1x1.Weights)
reluLayer("Name","fire2-relu_squeeze1x1")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([1 1],64,"Name","fire2-expand1x1","BiasLearnRateFactor",10,"WeightLearnRateFactor",10,"Bias",params.fire2_expand1x1.Bias,"Weights",params.fire2_expand1x1.Weights)
reluLayer("Name","fire2-relu_expand1x1")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([3 3],64,"Name","fire2-expand3x3","BiasLearnRateFactor",10,"Padding",[1 1 1 1],"WeightLearnRateFactor",10,"Bias",params.fire2_expand3x3.Bias,"Weights",params.fire2_expand3x3.Weights)
reluLayer("Name","fire2-relu_expand3x3")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
depthConcatenationLayer(2,"Name","fire2-concat")
convolution2dLayer([1 1],16,"Name","fire3-squeeze1x1","BiasLearnRateFactor",10,"WeightLearnRateFactor",10,"Bias",params.fire3_squeeze1x1.Bias,"Weights",params.fire3_squeeze1x1.Weights)
reluLayer("Name","fire3-relu_squeeze1x1")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([1 1],64,"Name","fire3-expand1x1","BiasLearnRateFactor",10,"WeightLearnRateFactor",10,"Bias",params.fire3_expand1x1.Bias,"Weights",params.fire3_expand1x1.Weights)
reluLayer("Name","fire3-relu_expand1x1")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([3 3],64,"Name","fire3-expand3x3","BiasLearnRateFactor",10,"Padding",[1 1 1 1],"WeightLearnRateFactor",10,"Bias",params.fire3_expand3x3.Bias,"Weights",params.fire3_expand3x3.Weights)
reluLayer("Name","fire3-relu_expand3x3")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
depthConcatenationLayer(2,"Name","fire3-concat")
maxPooling2dLayer([3 3],"Name","pool3","Padding",[0 1 0 1],"Stride",[2 2])
convolution2dLayer([1 1],32,"Name","fire4-squeeze1x1","BiasLearnRateFactor",10,"WeightLearnRateFactor",10,"Bias",params.fire4_squeeze1x1.Bias,"Weights",params.fire4_squeeze1x1.Weights)
reluLayer("Name","fire4-relu_squeeze1x1")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([1 1],128,"Name","fire4-expand1x1","BiasLearnRateFactor",10,"WeightLearnRateFactor",10,"Bias",params.fire4_expand1x1.Bias,"Weights",params.fire4_expand1x1.Weights)
reluLayer("Name","fire4-relu_expand1x1")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([3 3],128,"Name","fire4-expand3x3","BiasLearnRateFactor",10,"Padding",[1 1 1 1],"WeightLearnRateFactor",10,"Bias",params.fire4_expand3x3.Bias,"Weights",params.fire4_expand3x3.Weights)
reluLayer("Name","fire4-relu_expand3x3")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
depthConcatenationLayer(2,"Name","fire4-concat")
convolution2dLayer([1 1],32,"Name","fire5-squeeze1x1","BiasLearnRateFactor",10,"WeightLearnRateFactor",10,"Bias",params.fire5_squeeze1x1.Bias,"Weights",params.fire5_squeeze1x1.Weights)
reluLayer("Name","fire5-relu_squeeze1x1")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([3 3],128,"Name","fire5-expand3x3","BiasLearnRateFactor",10,"Padding",[1 1 1 1],"WeightLearnRateFactor",10,"Bias",params.fire5_expand3x3.Bias,"Weights",params.fire5_expand3x3.Weights)
reluLayer("Name","fire5-relu_expand3x3")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([1 1],128,"Name","fire5-expand1x1","BiasLearnRateFactor",10,"WeightLearnRateFactor",10,"Bias",params.fire5_expand1x1.Bias,"Weights",params.fire5_expand1x1.Weights)
reluLayer("Name","fire5-relu_expand1x1")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
depthConcatenationLayer(2,"Name","fire5-concat")
maxPooling2dLayer([3 3],"Name","pool5","Padding",[0 1 0 1],"Stride",[2 2])
convolution2dLayer([1 1],48,"Name","fire6-squeeze1x1","BiasLearnRateFactor",10,"WeightLearnRateFactor",10,"Bias",params.fire6_squeeze1x1.Bias,"Weights",params.fire6_squeeze1x1.Weights)
reluLayer("Name","fire6-relu_squeeze1x1")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([3 3],192,"Name","fire6-expand3x3","BiasLearnRateFactor",10,"Padding",[1 1 1 1],"WeightLearnRateFactor",10,"Bias",params.fire6_expand3x3.Bias,"Weights",params.fire6_expand3x3.Weights)
reluLayer("Name","fire6-relu_expand3x3")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([1 1],192,"Name","fire6-expand1x1","BiasLearnRateFactor",10,"WeightLearnRateFactor",10,"Bias",params.fire6_expand1x1.Bias,"Weights",params.fire6_expand1x1.Weights)
reluLayer("Name","fire6-relu_expand1x1")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
depthConcatenationLayer(2,"Name","fire6-concat")
convolution2dLayer([1 1],48,"Name","fire7-squeeze1x1","BiasLearnRateFactor",10,"WeightLearnRateFactor",10,"Bias",params.fire7_squeeze1x1.Bias,"Weights",params.fire7_squeeze1x1.Weights)
reluLayer("Name","fire7-relu_squeeze1x1")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([1 1],192,"Name","fire7-expand1x1","BiasLearnRateFactor",10,"WeightLearnRateFactor",10,"Bias",params.fire7_expand1x1.Bias,"Weights",params.fire7_expand1x1.Weights)
reluLayer("Name","fire7-relu_expand1x1")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([3 3],192,"Name","fire7-expand3x3","BiasLearnRateFactor",10,"Padding",[1 1 1 1],"WeightLearnRateFactor",10,"Bias",params.fire7_expand3x3.Bias,"Weights",params.fire7_expand3x3.Weights)
reluLayer("Name","fire7-relu_expand3x3")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
depthConcatenationLayer(2,"Name","fire7-concat")
convolution2dLayer([1 1],64,"Name","fire8-squeeze1x1","BiasLearnRateFactor",10,"WeightLearnRateFactor",10,"Bias",params.fire8_squeeze1x1.Bias,"Weights",params.fire8_squeeze1x1.Weights)
reluLayer("Name","fire8-relu_squeeze1x1")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([1 1],256,"Name","fire8-expand1x1","BiasLearnRateFactor",10,"WeightLearnRateFactor",10,"Bias",params.fire8_expand1x1.Bias,"Weights",params.fire8_expand1x1.Weights)
reluLayer("Name","fire8-relu_expand1x1")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([3 3],256,"Name","fire8-expand3x3","BiasLearnRateFactor",10,"Padding",[1 1 1 1],"WeightLearnRateFactor",10,"Bias",params.fire8_expand3x3.Bias,"Weights",params.fire8_expand3x3.Weights)
reluLayer("Name","fire8-relu_expand3x3")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
depthConcatenationLayer(2,"Name","fire8-concat")
convolution2dLayer([1 1],64,"Name","fire9-squeeze1x1","BiasLearnRateFactor",10,"WeightLearnRateFactor",10,"Bias",params.fire9_squeeze1x1.Bias,"Weights",params.fire9_squeeze1x1.Weights)
reluLayer("Name","fire9-relu_squeeze1x1")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([3 3],256,"Name","fire9-expand3x3","BiasLearnRateFactor",10,"Padding",[1 1 1 1],"WeightLearnRateFactor",10,"Bias",params.fire9_expand3x3.Bias,"Weights",params.fire9_expand3x3.Weights)
reluLayer("Name","fire9-relu_expand3x3")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([1 1],256,"Name","fire9-expand1x1","BiasLearnRateFactor",10,"WeightLearnRateFactor",10,"Bias",params.fire9_expand1x1.Bias,"Weights",params.fire9_expand1x1.Weights)
reluLayer("Name","fire9-relu_expand1x1")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
depthConcatenationLayer(2,"Name","fire9-concat")
dropoutLayer(0.5,"Name","drop9")
convolution2dLayer([1 1],1000,"Name","conv10","BiasL2Factor",1,"BiasLearnRateFactor",10,"WeightLearnRateFactor",10,"Bias",params.conv10.Bias,"Weights",params.conv10.Weights)
reluLayer("Name","relu_conv10")
globalAveragePooling2dLayer("Name","pool10")
fullyConnectedLayer(1000,"Name","fc","BiasLearnRateFactor",10,"WeightLearnRateFactor",10)
softmaxLayer("Name","prob")
classificationLayer("Name","ClassificationLayer_predictions","Classes",params.ClassificationLayer_predictions.Classes)];
lgraph = addLayers(lgraph,tempLayers);
% clean up helper variable
clear tempLayers;
Connect Layer Branches
Connect all the branches of the network to create the network graph.
lgraph = connectLayers(lgraph,"fire2-relu_squeeze1x1","fire2-expand1x1");
lgraph = connectLayers(lgraph,"fire2-relu_squeeze1x1","fire2-expand3x3");
lgraph = connectLayers(lgraph,"fire2-relu_expand1x1","fire2-concat/in1");
lgraph = connectLayers(lgraph,"fire2-relu_expand3x3","fire2-concat/in2");
lgraph = connectLayers(lgraph,"fire3-relu_squeeze1x1","fire3-expand1x1");
lgraph = connectLayers(lgraph,"fire3-relu_squeeze1x1","fire3-expand3x3");
lgraph = connectLayers(lgraph,"fire3-relu_expand3x3","fire3-concat/in2");
lgraph = connectLayers(lgraph,"fire3-relu_expand1x1","fire3-concat/in1");
lgraph = connectLayers(lgraph,"fire4-relu_squeeze1x1","fire4-expand1x1");
lgraph = connectLayers(lgraph,"fire4-relu_squeeze1x1","fire4-expand3x3");
lgraph = connectLayers(lgraph,"fire4-relu_expand1x1","fire4-concat/in1");
lgraph = connectLayers(lgraph,"fire4-relu_expand3x3","fire4-concat/in2");
lgraph = connectLayers(lgraph,"fire5-relu_squeeze1x1","fire5-expand3x3");
lgraph = connectLayers(lgraph,"fire5-relu_squeeze1x1","fire5-expand1x1");
lgraph = connectLayers(lgraph,"fire5-relu_expand3x3","fire5-concat/in2");
lgraph = connectLayers(lgraph,"fire5-relu_expand1x1","fire5-concat/in1");
lgraph = connectLayers(lgraph,"fire6-relu_squeeze1x1","fire6-expand3x3");
lgraph = connectLayers(lgraph,"fire6-relu_squeeze1x1","fire6-expand1x1");
lgraph = connectLayers(lgraph,"fire6-relu_expand3x3","fire6-concat/in2");
lgraph = connectLayers(lgraph,"fire6-relu_expand1x1","fire6-concat/in1");
lgraph = connectLayers(lgraph,"fire7-relu_squeeze1x1","fire7-expand1x1");
lgraph = connectLayers(lgraph,"fire7-relu_squeeze1x1","fire7-expand3x3");
lgraph = connectLayers(lgraph,"fire7-relu_expand1x1","fire7-concat/in1");
lgraph = connectLayers(lgraph,"fire7-relu_expand3x3","fire7-concat/in2");
lgraph = connectLayers(lgraph,"fire8-relu_squeeze1x1","fire8-expand1x1");
lgraph = connectLayers(lgraph,"fire8-relu_squeeze1x1","fire8-expand3x3");
lgraph = connectLayers(lgraph,"fire8-relu_expand1x1","fire8-concat/in1");
lgraph = connectLayers(lgraph,"fire8-relu_expand3x3","fire8-concat/in2");
lgraph = connectLayers(lgraph,"fire9-relu_squeeze1x1","fire9-expand3x3");
lgraph = connectLayers(lgraph,"fire9-relu_squeeze1x1","fire9-expand1x1");
lgraph = connectLayers(lgraph,"fire9-relu_expand3x3","fire9-concat/in2");
lgraph = connectLayers(lgraph,"fire9-relu_expand1x1","fire9-concat/in1");
Plot Layers
plot(lgraph);

Accepted Answer

Philip Brown
Philip Brown on 25 Nov 2021
As in Yanqi Liu's comment, you probably need to modify the fully connected layer too:
fullyConnectedLayer(5,"Name","fc","BiasLearnRateFactor",10,"WeightLearnRateFactor",10)
When you do transfer learning (in Deep Network Designer or at the command line), there's 2 layers you need to change:
  1. Replace the old classificationLayer with a new one, which has no set classes. These will be learned during training.
  2. Replace the fully-connected layer which does classification. That needs to have an OutputSize equal to the number of classes you want to use.
In Deep Network Designer, you can delete the old blocks, drag new ones in from the palette, connect them up, and set their properties. You don't need to set the classificationLayer's classes manually; they will get set automatically when training.

More Answers (1)

yanqi liu
yanqi liu on 24 Nov 2021
yes,sir,may be modify the classify layer,such as
classificationLayer("Name","ClassificationLayer_predictions","Classes",params.ClassificationLayer_predictions.Classes)];
to
classificationLayer("Name","ClassificationLayer_predictions","Classes",5)];
  3 Comments
yanqi liu
yanqi liu on 24 Nov 2021
yes,sir,please use or upload the params.mat
tempLayers = [
depthConcatenationLayer(2,"Name","fire9-concat")
dropoutLayer(0.5,"Name","drop9")
convolution2dLayer([1 1],5,"Name","conv10","BiasL2Factor",1,"BiasLearnRateFactor",10,"WeightLearnRateFactor",10,"Bias",params.conv10.Bias,"Weights",params.conv10.Weights)
reluLayer("Name","relu_conv10")
globalAveragePooling2dLayer("Name","pool10")
fullyConnectedLayer(5,"Name","fc","BiasLearnRateFactor",10,"WeightLearnRateFactor",10)
softmaxLayer("Name","prob")
classificationLayer("Name","ClassificationLayer_predictions","Classes",params.ClassificationLayer_predictions.Classes)];

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!