Error in DeepLearning using googlenet

조회 수: 16 (최근 30일)
Sushma TV
Sushma TV 2021년 11월 13일
댓글: Sushma TV 2021년 11월 19일
I am using transfer learning using Googlenet for binary classification of images. The following is the code.
imds = imageDatastore('RGBNewFrames_sameSz', ...
'IncludeSubfolders',true, ...
'LabelSource','foldernames');
%% Divide the data into training and validation data sets.
% Use 70% of the images for training and 30% for validation
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
%% Load Pretrained Network
net = googlenet;
analyzeNetwork(net)
inputSize = net.Layers(1).InputSize
%% Replace Final Layers
% The last three layers of the pretrained network net are configured for 1000 classes.
% These three layers must be fine-tuned for the new classification problem.
% Extract all layers, except the last three, from the pretrained network.
layersTransfer = net.Layers(1:end-3);
% transfer the layers to the new classification task by replacing the last three layers with a fully connected layer, a softmax layer, and a classification output layer.
numClasses = numel(categories(imdsTrain.Labels))
layers = [
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',10,'BiasLearnRateFactor',10,'Name', 'FC_Layer')
softmaxLayer('Name', 'soft_mxLayer')
classificationLayer('Name', 'OutputLayer')];
%% Train Network
pixelRange = [-30 30];
imageAugmenter = imageDataAugmenter( ...
'RandXReflection',true, ...
'RandXTranslation',pixelRange, ...
'RandYTranslation',pixelRange);
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ...
'DataAugmentation',imageAugmenter);
augimdsValidation = augmentedImageDatastore(inputSize(1:3),imdsValidation);
options = trainingOptions('sgdm', ...
'MiniBatchSize',10, ...
'MaxEpochs',2, ...%6
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',3, ...
'Verbose',false, ...
'Plots','training-progress');
netTransfer = trainNetwork(augimdsTrain,layers,options);
%% Classify Validation Images
[YPred,scores] = classify(netTransfer,augimdsValidation);
YValidation = imdsValidation.Labels;
accuracy = mean(YPred == YValidation)
Was Getting the error
Error using trainNetwork (line 170)
Invalid network.
Error in Ex2_Transfer_learning_googlenet (line 73)
netTransfer = trainNetwork(augimdsTrain,lgraph,options);
Caused by:
Layer 'inception_3a-3x3_reduce': Input size mismatch. Size of input to this layer is different from the expected input size.
Inputs to this layer:
from layer 'inception_3a-relu_1x1' (28×28×64 output)
Layer 'inception_3a-output': Unused input. Each layer input must be connected to the output of another layer.
Detected unused inputs:
input 'in2'
input 'in3'
input 'in4'
Layer 'inception_3b-output': Unused input. Each layer input must be connected to the output of another layer.
Detected unused inputs:
input 'in2'
Based on this made the following changes to the code
graph = layerGraph(layers);
netTransfer = trainNetwork(augimdsTrain,lgraph,options);
This resulted in the error,
Layer names in layer array must be non- empty.
So checked and found fcn, softmax did not have names and Explicity assigned names for these layers and executed the code.
The error on Layer names got sorted but now getting the old error again as below.(Error has also been mentioned above for reference)
Input size mismatch. Size of input to this layer is different from the expected input size.
Kindly help me sort this error. I tried all possible things I could think of to debug the error but could not solve it.
Getting the same error with resnet also. Presently it is working only with alexnet.
Any help on this will be greatly appreciated.
Thanks

채택된 답변

Srivardhan Gadila
Srivardhan Gadila 2021년 11월 15일
As David suggested you can use analyzeNetwork function to analyze your new network and debug the issue.
The issue with the workflow you are following is that, GoogleNet is a dagnetwork and when you are just collecting all the required layers excluding the last 3 layers in the "layersTransfer" array, you are only collecting the layers and information of the individual connections (Connections) is lost here.
layersTransfer = net.Layers(1:end-3);
So when you pass this new layers array to the trainNetwork function, it assumes that the network is a seriesNetwork and all the layers are connected serially. You can check the same using the analyzeNetwork function.
layers = [
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',10,'BiasLearnRateFactor',10,'Name', 'FC_Layer')
softmaxLayer('Name', 'soft_mxLayer')
classificationLayer('Name', 'OutputLayer')];
analyzeNetwork(layers)
Hence the correct workflow would be as follows:
Get the layerGraph instead of layers from the pretrained network:
lgraph = layerGraph(net);
Replace the fullyConnectedLayer and the classificationLayer with your newly defined layers:
fcLayer = fullyConnectedLayer(numClasses,'WeightLearnRateFactor',10,'BiasLearnRateFactor',10,'Name', 'FC_Layer');
clsLayer = classificationLayer('Name', 'OutputLayer');
lgraphNew = replaceLayer(lgraph,"loss3-classifier",fcLayer);
lgraphNew = replaceLayer(lgraphNew,"output",clsLayer)
analyzeNetwork(lgraphNew)
Now train this newly created layerGraph "lgraphNew" with the trainNetwork function.
  댓글 수: 10
Sushma TV
Sushma TV 2021년 11월 19일
Thank you sir..Actually found the command for finding the accuracy only..But for an unbalanced dataset that i have accuracy is not a good metric for evaluating model. So was looking for commands for other obtaining other metrics. Confusion matrix can be used for computing them. But wanted to check if I there are any inbuilt commands for it. Will check out in detail again. Thank you sir

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Willingham
David Willingham 2021년 11월 15일
Hi,
I'd recommend running the network analyser to see where the issues are with the modifications that have been made to the network. Here's the doc page for it:
Note: You can also visualize the network in the Deep Network Designer (the network analyser can also be launched from within it too): https://www.mathworks.com/help/deeplearning/ref/deepnetworkdesigner-app.html
What information does the network analyser provide you?
  댓글 수: 2
Sushma TV
Sushma TV 2021년 11월 16일
I have included the following lines of code
net = googlenet;
analyzeNetwork(net);
The analyzeNetwork(net) opens up the Deep learning Network Analyzer with the visual structure of the network and details of the network. It is showing the no. of layers, 144 layers, 0 errors, 0 warnings.
No details are displayed regarding the issue in the Deep learning Network Analyzer window.
The error is displayed in the Command window, with the error message as reported above. PArt of it shown below.
Error using trainNetwork (line 183)
Invalid network.
Error in Ex2_Transfer_learning_googlenet (line 99)
netTransfer = trainNetwork(augimdsTrain,lgraph,options);
Caused by:
Layer 'inception_3a-3x3_reduce': Input size mismatch. Size of input to this layer is different from
the expected input size.
Inputs to this layer:
from layer 'inception_3a-relu_1x1' (output size 28×28×64)
Layer 'inception_3a-output': Unconnected input. Each layer input must be connected to the output of
another layer.
Srivardhan Gadila
Srivardhan Gadila 2021년 11월 16일
@Sushma TV like I said in the answer below, to check the issues with your newly created network, you should use the analyzeNetwork function for it as well.
analyzeNetwork(layers)
As the pretrained network does not have any issues, using analyzeNetwork function on the pretrained network would not display any errors.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by