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)
댓글 수: 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!