Invalid input data. Invalid size of channel dimension. Layer expects input with channel dimension size 3 but received input with size 1.
3 views (last 30 days)
Show older comments
i used alexnet model for custom training. I loaded rgb images as input but i recieve an error that the input channel size is 1. please help me to resolve this error. i have attached the code snippet.
imds = imageDatastore('N:\oraldata',...
'IncludeSubfolders',true,...
'labelSource','foldernames',...
'FileExtensions','.jpg');
imdrgb=imds.read;
XTrain = imdrgb;
YTrain = imds.Labels;
classes = categories(YTrain);
numClasses = 2;
net=alexnet;
dlnet = dlnetwork(lgraph)
velocity = [];
numEpochs = 2;
miniBatchSize = 30;
numObservations = numel(YTrain);
numIterationsPerEpoch = floor(numObservations./miniBatchSize);
initialLearnRate = 1e-4;
momentum = 0.9;
decay = 0.01;
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
iteration = 0;
start = tic;
% Loop over epochs.
for epoch = 1:numEpochs
% Shuffle data.
idx = randperm(numel(YTrain));
XTrain = XTrain(idx);
YTrain = YTrain(idx);
% Loop over mini-batches.
for i = 1:numIterationsPerEpoch
iteration = iteration + 1;
% Read mini-batch of data and convert the labels to dummy
% variables.
idx = (i-1)*miniBatchSize+1:i*miniBatchSize;
X = XTrain(idx);
A=char(X);
Y = zeros(numClasses, miniBatchSize, 'single');
for c = 1:numClasses
Y(c,YTrain(idx)==classes(c)) = 1;
end
% Convert mini-batch of data to dlarray.
dlX = dlarray(single(A),'SSCB');
% Evaluate the model gradients and loss using dlfeval and the
% modelGradients function.
[gradients,loss] = dlfeval(@modelGradients,dlnet,dlX,Y);
% Determine learning rate for time-based decay learning rate schedule.
% learnRate = initialLearnRate/(1 + decay*iteration);
% Update the network parameters using the SGDM optimizer.
[dlnet.Learnables, velocity] = sgdmupdate(dlnet.Learnables, gradients, velocity, learnRate, momentum);
% Display the training progress.
if plots == "training-progress"
D = duration(0,0,toc(start),'Format','hh:mm:ss');
addpoints(lineLossTrain,iteration,double(gather(extractdata(loss))))
title("Epoch: " + epoch + ", Elapsed: " + string(D))
drawnow
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [gradients,loss] = modelGradients(dlnet,dlX,Y)
dlYPred = forward(dlnet,dlX);
dlYPred = softmax(dlYPred);
loss = crossentropy(dlYPred,Y);
gradients = dlgradient(loss,dlnet.Learnables);
end
and i am gettin error as,
Layer 'data': Invalid input data. Invalid size of channel dimension. Layer expects input with channel dimension size 3
but received input with size 1.
2 Comments
M J
on 28 Oct 2020
Hey, did you find a solution to your problem? I'm having a similar issue.
Cheers.
Answers (1)
Snehal
on 27 Mar 2025
The error message indicates that the model ‘alexnet’ expects the input with 3 channels, but your code is providing data with only 1 channel. A possible reason for this could be that the input image you’re using is in grayscale format instead of RGB.
You can check the format of the input image and convert it to RGB (if they’re not already) using the following approach:
function image_rgb = convertToRGB(image)
% Check if the image is grayscale
if size(image, 3) == 1
% Duplicate the single channel to create a 3-channel image
image_rgb = cat(3, image, image, image);
else
% If already RGB, no conversion needed
image_rgb = image;
end
end
% Invoking the above defined method
img = convertToRGB(img);
Below are some documentation links for your reference:
- https://www.mathworks.com/help/matlab/ref/double.cat.html
- https://www.mathworks.com/help/matlab/ref/double.size.html#d126e1653855:~:text=szdim%20%3D%20size(A%2Cdim)%20returns,the%201%2Dby%2D2%20row%20vector%20szdim.
Hope this helps!
0 Comments
See Also
Categories
Find more on Define Shallow Neural Network Architectures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!