Unexpected connection and input errors after modifying neural networks

CODE:
model4 = squeezenet;
numClasses = 2;
layersTransfer = [
net.Layers(1:end-3)
fullyConnectedLayer(numClasses, 'Name', 'fc')
softmaxLayer('Name', 'softmax')
classificationLayer('Name', 'output')
];
dataFolder = './larger_data/larger_PetImages';
categories = {'cat', 'dog'};
imds2 = imageDatastore(fullfile(dataFolder, categories), 'LabelSource', 'foldernames');
% Another program converts images in imageDatastore to 224 x 224 x 3
[larger_trainingSet, larger_validationSet] = splitEachLabel(imds, 0.75, 'randomized');
new_options = trainingOptions('adam', ...
'MiniBatchSize',10, ...
'MaxEpochs',10, ...
'InitialLearnRate',1e-3, ...
'Shuffle','every-epoch', ...
'ValidationData',larger_validationSet, ...
'ValidationFrequency',3, ...
'Verbose',false, ...
'Plots','training-progress');
[model4_predictor, info] = trainNetwork(larger_trainingSet, layersTransfer, new_options);
ERROR:
Error using trainNetwork
Invalid network.
Caused by:
Layer 'fire2-concat': Unconnected input. Each layer input must be connected to the output of another layer.
Layer 'fire2-expand3x3': Invalid input data. The number of channels of the input data (64) must match the layer's expected number of channels (16).
Layer 'fire3-concat': Unconnected input. Each layer input must be connected to the output of another layer.
Layer 'fire4-concat': Unconnected input. Each layer input must be connected to the output of another layer.
Layer 'fire5-concat': Unconnected input. Each layer input must be connected to the output of another layer.
Layer 'fire6-concat': Unconnected input. Each layer input must be connected to the output of another layer.
Layer 'fire7-concat': Unconnected input. Each layer input must be connected to the output of another layer.
Layer 'fire8-concat': Unconnected input. Each layer input must be connected to the output of another layer.
Layer 'fire9-concat': Unconnected input. Each layer input must be connected to the output of another layer.
COMMENT:
Similar errors were experienced when making any modifications to the final three layers of the GoogleNet CNN.
Any help would be greatly appreciated

 Réponse acceptée

Taylor
Taylor le 21 Mar 2024
The Deep Network Designer app has a useful Network Analyzer tool for just this type of issue. Open the app using deepNetworkDesigner, load layersTransfer from your workspace, and select "Analyze" on the top toolstrip. You should see a report like the one below. Looks like your approach to modifying the network for transfer learning reconfigured the connections in the network unexpectedly. I would recommend following the approach outlined here.

6 commentaires

Thanks for the feedback. I followed the link as you suggested. My concern is that I wanted to understand how to fix and prevent this problem from a purely code (no toolboxes or apps) point of view. Is that possible? Thanks again.
It is possible, but currently it is a massive headache. R2024a (due out soon) has many upgrades in this regard. In short the issue is that squeeznet loads a DAGNetwork object which is not as flexible as a LayerGraph object. This is how you will get around this issue in R2024a.
Can you point me towards the "massive headache" solution that can be used now in R2023b? My instructor wants us to do this by code. Thanks.
I tried to remove as much of the headache as possible for you:
net = squeezenet;
lgraph = layerGraph(net);
% Assuming the names of the last three layers are known
layersToRemove = {'pool10', 'prob', 'ClassificationLayer_predictions'};
lgraph = removeLayers(lgraph, layersToRemove);
numClasses = 2;
newLayers = [
fullyConnectedLayer(numClasses, 'Name', 'fc')
softmaxLayer('Name', 'softmax')
classificationLayer('Name', 'output')
];
% Find the layer just before the removed layers
layerBeforeRemoved = 'relu_conv10';
% Add the new layers
lgraph = addLayers(lgraph, newLayers);
% Connect the new fully connected layer to the layer before the removed ones
lgraph = connectLayers(lgraph, layerBeforeRemoved, 'fc');
netFinal = assembleNetwork(lgraph);
Warning: Network issues detected.

Caused by:
Layer 'output': Empty Classes property. Classes will be set to categorical(1:N), where N is the number of classes.
Error using assembleNetwork
Invalid network.

Caused by:
Layer 'fc': Empty Weights property. Specify a nonempty value for the Weights property.
Layer 'fc': Empty Bias property. Specify a nonempty value for the Bias property.
As you see, the final line yields an error asking you to declare the Weights and Bias values for the added fullyConnectedLayer.
Once you're no longer constrained by your instructor, I highly recommend utilizing the built-in apps that come with MATLAB. They are quite intuitive and help circumvent these sorts of issues.
Thanks a lot. I really appreciate the help. Hopefully my instructor will allow use of the built-in apps in the future.
You're welcome! Best of luck with your work going forward!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Deep Learning Toolbox dans Centre d'aide et File Exchange

Produits

Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by