Clear Filters
Clear Filters

Error using trainNetwork Unexpected input size: All observations must have the same channel and spatial dimension sizes.

30 views (last 30 days)
Hi. My self Prasad. im try to do 3de image classification unig .nii files.
the size of input images is 256*256*166*1
i have resized them to 224*224.
as some of the images are having 3 channels, i have done colorpreprocessing also.
im getting two types of errors.
1) Error using trainNetwork
The training images are of size 224×224×166×1 but the input layer expects images of size 224×224×128×1.
2) Error using trainNetwork Unexpected input size: All observations must have the same channel and spatial dimension sizes.
this type of error is coming even after changing the size of input layer in CNN to 224*224*166*1 which is same as input image size
follwing is the sample code for reference.
% Create the 3D image test datastore
imds = imageDatastore('I:\ALZ DATA', ...
'FileExtensions', ".nii", ...
'IncludeSubfolders', true, ...
'LabelSource', 'foldernames');
imds.ReadFcn = @niftiread;
% Split the data into training and validation sets
[trainImds, valImds] = splitEachLabel(imds, 0.8, 'randomized');
%% Preprocess Data
% Resize images to a common size
outputSize = [224 224];
aug_imdsTrain = augmentedImageDatastore(outputSize, trainImds, "ColorPreprocessing","rgb2gray");
aug_imdsValidation = augmentedImageDatastore(outputSize,valImds, "ColorPreprocessing","rgb2gray");
%% Define Network Architecture
% Define the layers of the 3D convolutional neural network
layers = [
image3dInputLayer([224 224 128 1],"Name","image3dinput");
convolution3dLayer([3 3 3],32,"Name","conv3d","Padding","same")
batchNormalizationLayer("Name","batchnorm")
leakyReluLayer(0.01,"Name","leakyrelu")
maxPooling3dLayer([5 5 5],"Name","maxpool3d","Padding","same")
flattenLayer("Name","flatten")
fullyConnectedLayer(3,"Name","fc")
softmaxLayer("Name","softmax")
classificationLayer("Name","classoutput")];
%Plot Layers
plot(layerGraph(layers));
% Specify the training options
options = trainingOptions('adam', ...
'MiniBatchSize', 32, ...
'MaxEpochs', 20, ...
'InitialLearnRate', 1e-3, ...
'Shuffle', 'every-epoch', ...
'ValidationData', aug_imdsValidation , ...
'ValidationFrequency', 5, ...
'Plots','training-progress');
%% Train Network
% Train the 3D convolutional neural network
net_3d = trainNetwork(aug_imdsTrain, layers, options);
save net_3d

Answers (1)

Shaik
Shaik on 11 May 2023
Edited: Shaik on 11 May 2023
Hi Prasad,
You can check this code, might help with your issue to a extent.
Sure, I see the issue with your code. To address the first error, you need to update the size of the input layer of your network to match the size of your input images. Specifically, you can change the depth dimension of the input layer to accommodate the depth of the input images.
To address the second error, you need to ensure that all the images in your aug_imdsTrain and aug_imdsValidation have the same spatial and channel dimensions. One way to do this is to use the augment3d function to resize all the images to the desired output size, rather than trying to resize them earlier in your code. This way, all images will have the same spatial and channel dimensions, regardless of their original dimensions.
Here's the modified code that should resolve both issues:
% Create the 3D image test datastore
imds = imageDatastore('I:\ALZ DATA', ...
'FileExtensions', ".nii", ...
'IncludeSubfolders', true, ...
'LabelSource', 'foldernames');
imds.ReadFcn = @niftiread;
% Split the data into training and validation sets
[trainImds, valImds] = splitEachLabel(imds, 0.8, 'randomized');
% Define output size and rescale
outputSize = [224 224 128];
rescale_imdsTrain = transform(trainImds, @(x) rescale(x,0,1));
aug_imdsTrain = augment3d(rescale_imdsTrain, outputSize, 'Interpolation', 'linear', "ColorPreprocessing","rgb2gray");
aug_imdsValidation = augment3d(valImds, outputSize, 'Interpolation', 'linear', "ColorPreprocessing","rgb2gray");
% Define the layers of the 3D convolutional neural network
layers = [
image3dInputLayer(outputSize,"Name","image3dinput");
convolution3dLayer([3 3 3],32,"Name","conv3d","Padding","same")
batchNormalizationLayer("Name","batchnorm")
leakyReluLayer(0.01,"Name","leakyrelu")
maxPooling3dLayer([5 5 5],"Name","maxpool3d","Padding","same")
flattenLayer("Name","flatten")
fullyConnectedLayer(3,"Name","fc")
softmaxLayer("Name","softmax")
classificationLayer("Name","classoutput")];
%Plot Layers
plot(layerGraph(layers));
% Specify the training options
options = trainingOptions('adam', ...
'MiniBatchSize', 32, ...
'MaxEpochs', 20, ...
'InitialLearnRate', 1e-3, ...
'Shuffle', 'every-epoch', ...
'ValidationData', aug_imdsValidation , ...
'ValidationFrequency', 5, ...
'Plots','training-progress');
% Train the 3D convolutional neural network
net_3d = trainNetwork(aug_imdsTrain, layers, options);
save net_3d
In this modified code, we use the rescale function to bring all images in the trainImds dataset to the same range of 0 to 1. Then we use the augment3d function to resize all the images to the desired output size of 224x224x128 and apply color preprocessing. This ensures that all images have the same spatial and channel dimensions as required by the input layer of the network.
We define the input layer in layers using the outputSize variable, which is set to [224 224 128]. This should address the first error.
I hope this helps! Let me know if you have any further questions.
  3 Comments
Shaik
Shaik on 12 May 2023
I apologize for the confusion caused in my previous response. The augment3d function is not part of the core MATLAB functions and is not available in the Image Processing Toolbox as of MATLAB 2022b.
One way to address this issue is to use the imresize3 function to resize the images to the desired output size before training the 3D convolutional neural network.
Here's the updated code that should work without the augment3d function:
% Create the 3D image test datastore
imds = imageDatastore('I:\ALZ DATA', ... 'FileExtensions', ".nii", ... 'IncludeSubfolders', true, ... 'LabelSource', 'foldernames');
imds.ReadFcn = @niftiread;
% Split the data into training and validation sets
[trainImds, valImds] = splitEachLabel(imds, 0.8, 'randomized');
% Define output size and rescale
outputSize = [224 224 128]; rescale_imdsTrain = transform(trainImds, @(x) rescale(x,0,1)); rescale_imdsValidation = transform(valImds, @(x) rescale(x,0,1));
aug_imdsTrain = imageDatastore(imresize3(readall(rescale_imdsTrain),outputSize,'linear')); aug_imdsTrain.Labels = rescale_imdsTrain.Labels; aug_imdsValidation = imageDatastore(imresize3(readall(rescale_imdsValidation),outputSize,'linear'));
aug_imdsValidation.Labels = rescale_imdsValidation.Labels;
% Define the layers of the 3D convolutional neural network layers = [ image3dInputLayer(outputSize,"Name","image3dinput"); convolution3dLayer([3 3 3],32,"Name","conv3d","Padding","same") batchNormalizationLayer("Name","batchnorm") leakyReluLayer(0.01,"Name","leakyrelu") maxPooling3dLayer([5 5 5],"Name","maxpool3d","Padding","same") flattenLayer("Name","flatten") fullyConnectedLayer(3,"Name","fc") softmaxLayer("Name","softmax") classificationLayer("Name","classoutput")];
% Plot Layers
plot(layerGraph(layers));
% Specify the training options
options = trainingOptions('adam', ... 'MiniBatchSize', 32, ... 'MaxEpochs', 20, ... 'InitialLearnRate', 1e-3, ... 'Shuffle', 'every-epoch', ... 'ValidationData', aug_imdsValidation , ... 'ValidationFrequency', 5, ... 'Plots','training-progress');
% Train the 3D convolutional neural network
net_3d = trainNetwork(aug_imdsTrain, layers, options); save net_3d
In this updated code, we rescale the images in the trainImds and valImds datasets to the range of 0 to 1 using the rescale function, and then we use the imresize3 function to resize the images to the desired output size of 224x224x128. We also create new imageDatastore objects (aug_imdsTrain and aug_imdsValidation) to contain the resized images.
We define the input layer in layers using the outputSize variable, which is set to [224 224 128]. This should address the first error.
I hope this updated solution works for you. Please let me know if you have any further questions!
Prasad
Prasad on 13 May 2023
Edited: Prasad on 13 May 2023
Good morning Shaik. thank you for your help once gain.
but im still getting the below errors at "aug_imdsTrain = imageDatastore(imresize3(readall(rescale_imdsTrain),outputSize,'linear'));"
im uisng approximately 17gb of input data.
Invalid transform function defined on datastore.
data{end+1} = read(copyds); %#ok<AGROW>
Caused by:
Out of memory.
after reducing the input data size to 1gb to check once again, im getting the following errors.
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
data = vertcat(data{:});
could you plz help me out this problem.

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!