The training images are of size 224×224×3 but the input layer expects images of size 224×224×1.

81 次查看(过去 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)

回答(3 个)

Matt J
Matt J 2023-3-29
imageInputLayer([224 224 3])
  17 个评论
Muhammad
Muhammad 2023-4-4
I run this code now but getting this error
Error: File: testfetalMri.m Line: 16 Column: 1
Function definitions in a script must appear at the end of the file.
Move all statements after the "customreader" function definition to before the first local function definition.
clear all
close all
%faceDatasetPath = fullfile('c:','FaceDataset');
%imds = imageDatastore("D:\test", ...
% 'IncludeSubfolders',true,'LabelSource','foldernames');
imds = imageDatastore("D:\test", ...
'IncludeSubfolders',true,'LabelSource','foldernames','ReadFcn',@customreader);
function img=customreader(filename)
img=imread(filename);
img=img(:,:,1);
end
%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)

请先登录,再进行评论。


Image Analyst
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.

Walter Roberson
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.

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by