I'm using MATLAB R2019b and I'm trying to create a CNN model that takes grayscale images as input. However, I got the error in checkImageAndNetworkChannelSizes function even though both image channel size and network channel size are 1 because ndims(I) of grayscale image is 2 and therefore the first part of the conditional statement is true. Is this a bug? Is there a way to get around this? I try to add singleton dimension to my image but it seems to be ignored/dropped.
function checkImageAndNetworkChannelSizes(I, networkChannelSize)
% If the input image size has a different channel size than that of
% the network input size, we need to error.
[~, ~, Isize] = size(I);
if ndims(I) ~= 3 || Isize ~= networkChannelSize
error(message('vision:rcnn:invalidInputImageChannelSize', Isize, networkChannelSize));