The training images are of size 224×224×3 but the input layer expects images of size 224×224×1.
45 次查看(过去 30 天)
显示 更早的评论
I want to train a D.L network (googlenet) with images with "224×224×1" of size (they are panchromatic). I follow the steps described in the "mathworks" as described bellow but I get this error: " The training images are of size 224×224×3 but the input layer expects images of size 224×224×1." So what I should do in order to train my network ? Many thanks.
Below is the code.
clear all
close all
%faceDatasetPath = fullfile('c:','FaceDataset');
imds = imageDatastore("D:\test", ...
'IncludeSubfolders',true,'LabelSource','foldernames');
%display some samples from the dataset
figure;
perm = randperm(213,20);
for i = 1:20
subplot(4,5,i);
imshow(imds.Files{perm(i)});
title(imds.Labels(perm(i)));
end
numTrainFiles = 6;
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomize');
[no_of_TrainImages ~]=size(imdsTrain.Files);
[no_of_TestImages ~]=size(imdsValidation.Files);
layers = [
imageInputLayer([224 224 1])
convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,64,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
fullyConnectedLayer(7)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',10, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',10, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(imdsTrain,layers,options);
YPred = classify(net,imdsValidation);
YValidation = imdsValidation.Labels;
accuracy = sum(YPred == YValidation)/numel(YValidation)
0 个评论
回答(3 个)
Image Analyst
2023-3-29
You should write a little script to convert all of your images to gray scale and save them in a different folder so you don't overwrite your originals. Or else change your input layer to accept color images, as Matt showed you.
0 个评论
Walter Roberson
2023-3-30
Use an augmentedImageDataStore with size 224 224 1 and 'ColorPreprocessing', 'rgb2gray' . This will automatically resize any image to the right size and will convert to grayscale if needed.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!