Error using nnet.cnn.LayerGraph>iThrowErrorIfLayerHasMultipleInputs Layer 'inception_3a-output' has multiple inputs. Specify which input of layer 'inception_3a-output' to use.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1388049/image.png)
clear all;
close all;
%Open file in MATLAB
outputFolder=fullfile('recycle101');
rootFolder=fullfile(outputFolder,'project');
categories={'aluminiumcan','petbottle','drinkcartonbox'};
%Separate dataset into training and validation in the ratio of 70:30
imds=imageDatastore(fullfile(rootFolder,categories),'LabelSource','foldernames');
tbl=countEachLabel(imds)
minSetCount=min(tbl{:,2});
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
%Count the total datasets in each file
countEachLabel(imds);
%Randomly choose file for aluminium can, PET bottles, and drink carton box
AluminiumCan=find(imds.Labels=='aluminiumcan',1);
PETBottles=find(imds.Labels=='petbottle',1);
DrinkCartonBox=find(imds.Labels=='drinkcartonbox',1);
%Plot image that was pick randomly
figure
subplot(2,2,1);
imshow(readimage(imds,AluminiumCan));
subplot(2,2,2);
imshow(readimage(imds,PETBottles));
subplot(2,2,3);
imshow(readimage(imds,DrinkCartonBox));
%Load pre-trained network
net=googlenet;
analyzeNetwork(net)
lys = net.Layers;
lys(end-3:end)
numClasses = numel(categories(imdsTrain.Labels));
lgraph = layerGraph(lys);
%Replace the classification layers for new task
newFCLayer = fullyConnectedLayer(3,'Name','new_fc','WeightLearnRateFactor',10,'BiasLearnRateFactor',10);
lgraph = replaceLayer(lgraph,'loss3-classifier',newFCLayer);
newClassLayer = classificationLayer('Name','new_classoutput');
lgraph = replaceLayer(lgraph,'output',newClassLayer);
analyzeNetwork(lgraph)
lgraph = connectLayers(lgraph,'inception_3a-relu_5x5','inception_3a-output');
lgraph = connectLayers(lgraph,'inception_3b-output','pool3-3x3_s2');
%Resize the image and train the network
imageSize=net.Layers(1).InputSize;
augmentedTrainingSet=augmentedImageDatastore(imageSize,...
imdsTrain,'ColorPreprocessing','gray2rgb');
augmentedValidateSet=augmentedImageDatastore(imageSize,...
imdsValidation,'ColorPreprocessing','gray2rgb');
options = trainingOptions('sgdm', ...
'MiniBatchSize',4, ...
'MaxEpochs',8, ...
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',augmentedValidateSet, ...
'ValidationFrequency',3, ...
'Verbose',false, ...
'ExecutionEnvironment','cpu', ...
'Plots','training-progress');
trainedNet = trainNetwork(augmentedTrainingSet,lgraph,options);
0 Kommentare
Antworten (1)
Ranjeet
am 2 Jun. 2023
Hi Tan,
You are extracting layers from ‘GoogleNet’ in the following code and then using the extracted layer to form a ‘layerGraph’ and then further add/replace other layers like fully connection layer and classification layer.
%Load pre-trained network
net=googlenet;
analyzeNetwork(net)
lys = net.Layers;
lys(end-3:end)
numClasses = numel(categories(imdsTrain.Labels));
lgraph = layerGraph(lys);
However, ‘Googlenet’ has multiple parallel connections from a single layer throughout the network. When you are create layerGraph from net.Layers, connection information in ‘net.Connections’ is not being used. ‘layerGraph’ can directly be created from network by ‘lgraph = layerGraph(net)’ so that connection information is also there. You may use the following code:
net=googlenet;
analyzeNetwork(net)
numClasses = 5;
lgraph = layerGraph(net);
% check the layer connected to fully connected layer]
inputSizeFc = lgraph.Layers(142).InputSize;
%Replace the classification layers for new task
newFCLayer = fullyConnectedLayer(numClasses,'Name','new_fc','WeightLearnRateFactor',10,'BiasLearnRateFactor',10, ...
'Bias',ones(numClasses, 1), 'Weights',ones(numClasses, inputSizeFc));
lgraph = replaceLayer(lgraph,'loss3-classifier',newFCLayer);
newClassLayer = classificationLayer('Name','new_classoutput');
lgraph = replaceLayer(lgraph,'output',newClassLayer);
analyzeNetwork(lgraph);
0 Kommentare
Siehe auch
Kategorien
Mehr zu Image Data Workflows finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!