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

조회 수: 47 (최근 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)
Matt J
Matt J 2023년 4월 4일
Move customreader to the end of the file as the error message says.

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


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