Deep Network Designer Training Images are size 224x224x1 but input layer expects images of size 224x224x3 (GoogLeNet Network Trained on Color Images to Gray)
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am working on a CNN using transfer learning in GoogLeNet. The script is below:
clc; close all;
animal_image_dataset = imageDatastore('***filename' );
%img = readimage(animal_image_dataset,n);
[train, test] = splitEachLabel(animal_image_dataset,0.01); %use test bc test has labels
%use test when calling bc test has labels
%test has the class labels
%%
%imageSize = [224 224 3]; %image size
% preprocessing
augmenter = imageDataAugmenter('RandRotation',[-180,180],...
'RandXReflection',(1),'RandYReflection',(1));
trainds = augmentedImageDatastore([224 224],train, ...
'DataAugmentation',augmenter,'ColorPreprocessing','gray2rgb' );
testds = augmentedImageDatastore([224 224],test,...
'DataAugmentation',augmenter,'ColorPreprocessing','gray2rgb'); %testds does not have labels
%testds does not have the class labels
%%
net = googlenet;
% open deepnet designer and create lgraph
%I = readimage(animal_image_dataset,4000);
%size(I)
%I = imresize(I,[224 224]); %resize images
%imshow(I)
lgraph = layerGraph(net);
layer = imageInputLayer([224 224 3],'Name','Value');
newFc = fullyConnectedLayer(4,"Name","new_fc");
lgraph = replaceLayer(lgraph,"loss3-classifier",newFc);
newOut = classificationLayer("Name","new_out");
lgraph = replaceLayer(lgraph,"output",newOut);
%%
options = trainingOptions("sgdm","InitialLearnRate", 0.001);
[animalnet,info] = trainNetwork(trainds,lgraph,options);
%%
% testing
plot(info.TrainingLoss)
figure(3)
Ypred = classify(animalnet,testds);
%%
truetest = test.Labels; %animalActual = test.Labels
%%
numCorrect = nnz(Ypred == truetest) %count num correct
fracCorrect = numCorrect/numel(Ypred) %percentage correct
accuracy = mean(Ypred == truetest); %avg correct
%%
confusionchart(test.Labels,Ypred);
idxWrong = find(Ypred ~= truetest);
idx2 = idxWrong(2);
figure(4)
imshow(readimage(test,idx2))
title(test.Labels(idx2))
This becomes the pretrained network that then goes into the Deep Network Designer App. It originally takes in RGB images and classifies them. It then is supposed to classify greyscale images via transfer learning. However, in the Deep Network Designer App, I get the error message "Training Failed. The training images are of size 224x224x1 but the input layer expects images of size 224x224x3." If I use the grayscale images to train a different network (so they are the first images that network sees, but it uses the exact same code as above, just the file path is modified) and then go into the Deep Network Designer app and use the RGB images for transfer learning, I do not have this error.
How do I fix this error? Do I modify something in my script? Is there something I do in the Deep Network Designer App? Thank you so much!
0 commentaires
Réponses (1)
yanqi liu
le 7 Fév 2022
clc; close all;
animal_image_dataset = imageDatastore('***filename' );
%img = readimage(animal_image_dataset,n);
[traino, testo] = splitEachLabel(animal_image_dataset,0.1); %use test bc test has labels
%use test when calling bc test has labels
%test has the class labels
%%
%imageSize = [224 224 3]; %image size
% preprocessing
% augmenter = imageDataAugmenter('RandRotation',[-180,180],...
% 'RandXReflection',(1),'RandYReflection',(1));
% trainds = augmentedImageDatastore([224 224],train, ...
% 'DataAugmentation',augmenter,'ColorPreprocessing','gray2rgb' );
% testds = augmentedImageDatastore([224 224],test,...
% 'DataAugmentation',augmenter,'ColorPreprocessing','gray2rgb'); %testds does not have labels
%testds does not have the class labels
%%
net = googlenet;
inputSize = net.Layers(1).InputSize;
% open deepnet designer and create lgraph
%I = readimage(animal_image_dataset,4000);
%size(I)
%I = imresize(I,[224 224]); %resize images
%imshow(I)
lgraph = layerGraph(net);
% layer = imageInputLayer([224 224 3],'Name','Value');
newFc = fullyConnectedLayer(4,"Name","new_fc");
lgraph = replaceLayer(lgraph,"loss3-classifier",newFc);
newOut = classificationLayer("Name","new_out");
lgraph = replaceLayer(lgraph,"output",newOut);
trainds = augmentedImageDatastore(inputSize(1:2),traino,'ColorPreprocessing','gray2rgb');
testds = augmentedImageDatastore(inputSize(1:2),testo,'ColorPreprocessing','gray2rgb');
%%
options = trainingOptions("sgdm","InitialLearnRate", 0.001);
[animalnet,info] = trainNetwork(trainds,lgraph,options);
%%
% testing
plot(info.TrainingLoss)
figure(3)
Ypred = classify(animalnet,testds);
%%
truetest = testo.Labels; %animalActual = test.Labels
%%
numCorrect = nnz(Ypred == truetest) %count num correct
fracCorrect = numCorrect/numel(Ypred) %percentage correct
accuracy = mean(Ypred == truetest); %avg correct
%%
confusionchart(testo.Labels,Ypred);
idxWrong = find(Ypred ~= truetest);
idx2 = idxWrong(2);
figure(4)
imshow(readimage(testo,idx2))
title(testo.Labels(idx2))
4 commentaires
Voir également
Catégories
En savoir plus sur Get Started with Deep Learning Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
