Clear Filters
Clear Filters

function checkImage​AndNetwork​ChannelSiz​es

2 views (last 30 days)
Nicha
Nicha on 17 Nov 2019
Commented: Jyothis Gireesh on 20 Nov 2019
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));
end

Answers (1)

Jyothis Gireesh
Jyothis Gireesh on 20 Nov 2019
Here are a few pointers which may be of help:
  • The function ndims() ignores all the trailing singleton dimensions of the input matrix. Hence it ignores the third singleton dimension and returns 2 when the input is a 2-D matrix.
  • One possible workaround is to use the permute() function as follows:
if ndims(permute(I, [3 1 2])) ~= 3 || Isize ~= networkChannelSize
error(message('vision:rcnn:invalidInputImageChannelSize', Isize, networkChannelSize));
end
If “I" is of size m x n, the permute operation changes the size of the matrix to 1 x m x n and ndims()” returns 3.
This method may create an error if “I” is a 4-D or higher dimensional matrix.
  • Else you may include another “if” condition which checks whether ndims(I)” is equal to 2 or not.
Hope this helps!!
  2 Comments
Nicha
Nicha on 20 Nov 2019
Those are great answers! May I ask how and if I can change this internal function? MATLAB didn't allow me to change and save it when I tried.
Jyothis Gireesh
Jyothis Gireesh on 20 Nov 2019
It is not a recommended pratice to edit the contents of a MATLAB internal function.
The problem may be due to wrong arguments provided in the "imageInputLayer" or any other layers of the model. Could you attach the code so that I can take a look at it?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!