Unrecognized function or variable 'invertResidualLayer'

Illustration
NoYeah - 2023-03-23T14:55:22+00:00
Question: Unrecognized function or variable 'invertResidualLayer'

I have installed add-on   'Deep Learning Toolbox' and 'Deep Learning Toolbox model for mobilenetv2'   and used the below code   net = mobilenetv2; % Load pretrained MobileNet model numClasses = numel(classNames); layers = [ net.Layers(1:end-3) fullyConnectedLayer(numClasses,'Name','fc') softmaxLayer('Name','softmax') classificationLayer('Name','classoutput')]; options = trainingOptions('adam', ... 'MaxEpochs',10, ... 'MiniBatchSize',32, ... 'ValidationData',imdsValidation, ... 'ValidationFrequency',5, ... 'InitialLearnRate',1e-4, ... 'LearnRateSchedule','piecewise', ... 'LearnRateDropFactor',0.1, ... 'LearnRateDropPeriod',5, ... 'L2Regularization',0.01, ... 'Plots','training-progress'); % Train the network net = trainNetwork(imdsTrain,layers,options); and I got the error message below Error using trainNetwork invalid network. caused by: Layer 'block_11_add' Unconnected input. Each layer input must be connected to the output of another layer. Layer 'block_12_add' Unconnected input. Each layer input must be connected to the output of another layer. Layer 'block_14_add' Unconnected input. Each layer input must be connected to the output of another layer. Layer 'block_15_add' Unconnected input. Each layer input must be connected to the output of another layer. Layer 'block_2_add' Unconnected input. Each layer input must be connected to the output of another layer. Layer 'block_4_add' Unconnected input. Each layer input must be connected to the output of another layer. Layer 'block_5_add' Unconnected input. Each layer input must be connected to the output of another layer. Layer 'block_7_add' Unconnected input. Each layer input must be connected to the output of another layer. Layer 'block_8_add' Unconnected input. Each layer input must be connected to the output of another layer. Layer 'block_9_add' Unconnected input. Each layer input must be connected to the output of another layer. I don`t know why this error happens...   maybe using pretrained model intrigue error depends on the data?

Expert Answer

Profile picture of John Williams John Williams answered . 2025-11-20

Hi,
 
I have struggled with this issue a lot and I know the reason behind it.
 
The problem is while you are calling 'net.Layers(1:end-3)' it will only have information of layers, not the connections and the problem is occuring in addition layer as it requires 2 inputs but the layer graph is skipping one connection in net.Layer.
 
I don't have any permenent solution to that but you can try 2 things:
  1. Import the 'net' in the Network Designer Application and export the 'lgraph' after replacing last 3 layers with your desired layer.
  2. You can use 'connectLayer' manually for all the unconnected layers like this,
lgraph = layerGraph(layers);
lgraph = connectLayers(lgraph,'input','add/in2');
but this process will take lot of time.
 
But by looking at you code I think you can just use the 'replaceLayer' in last layer(to match the number of classes) because mobilenetv2 already have fc and softmax function.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!