Clear Filters
Clear Filters

How can I use images which have different size for CNN?

14 views (last 30 days)
Hello everyone,
I have prepared a neural network and I need to test it now but my dataset has images that have different sizes. How can I use all of them even they have different size?

Accepted Answer

Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh on 20 Feb 2022
except some special cases, there is a limitation for forwarding different size arrays toward a CNN. after images pass the convolutional layers each one lead to an array with different size. and because of fully connected layers at end of network it is not possible to work with different size array. ( they should work with input with certain size)
so you should convert them to certain size for this network. and there is a fast method to do so:
input_size = Net.Layers(1).InputSize;
images_folder_path = 'E:\images'; % the folder contain all images
image_database = imageDatastore(images_folder_path);
image_database = transform(image_database,,@(x) (imresize(x,input_size(1:2))));
after this, you can pass this datastore to network all in once. for example with classification:
Output = classify(Net,image_database);
if not, select one by one and pass it through the network:
I =;
y = classify(Net,I);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!