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

78 ビュー (過去 30 日間)
Muhammad
Muhammad 2023 年 3 月 29 日
コメント済み: Matt J 2023 年 4 月 4 日
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 Center および File ExchangeImage Data Workflows についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by