Message Boards Message Boards


resize large number of images to required size?

Posted 9 months ago
8 Replies
8 Total Likes

Greetings, I am trying to implement the VGG19 neural network. I have downloaded the imagenet dataset and am testing a few classes. I have figured out that NetEncoder can be used to resize images for training but am struck with resizing the images for testing. I am using the following line of code to import the images into testData variable.

testData = Import["C:\\Users\\cnn\\Desktop\\images\\test", "*.jpg"];

Now my problem is that the images in the folder are of various sizes and i want to resize all of them to 224 x 224 so that i can use it to test my network.

I have tried using


but it has resulted in error.

EDIT : Forgot to mention that there are two sub folders in the path. Problem has been resolved, Thank you.

8 Replies

Use the following code:

testData = Import["C:\\Users\\cnn\\Desktop\\images\\test\\*.jpg", ImageSize -> {224, 224}]
Posted 9 months ago

Thank you for your reply but I am getting the following error when I use the code you suggested:

Import::nffil: File not found during Import.

I have also tried to do the following:

testData = 
 Import["C:\\Users\\cnn\\Desktop\\images\\test\\", "*.jpg",  ImageSize -> {224, 224}]

I am getting the following error:

Import::elemstx: ImageSize->{224,224} is not a valid format or element specification. Expecting a string, list, or integer.
General::stop: Further output of Import::elemstx will be suppressed during this calculation.

can you please tell me what I am doing wrong?



Import["C:\\Users\\cnn\\Desktop\\images\\test\\", "*.jpg",  ImageSize -> {224, 224}]

This is not going to work.

The code the Piotr suggested should work (if you are up-to-date with Mathematica version (I am sure this works at least on 11.1)). To be on the safe side, you could also try doing following:

files = FileNames["*.JPG" | "*.JPEG", "C:\\Users\\cnn\\Desktop\\images\\test"]
testData = Import[#, ImageSize -> {224, 224}] & /@ files;

This solution can be useful, say, when your files formats are spelled differently (i.e. JPEG vs JPG)

Posted 9 months ago

I am using MMA version 11.2. I have tried implementing your code but was hit by the following error:


I have tried to get RandomSample

RandomSample[testData, 3]

and got the following error:

RandomSample::smplen: RandomSample cannot generate a sample of length 3, which is greater than the length of the sample set {}. If you want a choice of possibly repeated elements from the set, use RandomChoice.

Thanks for your solution but it seems it is not working to my problem. There are two sub folders in that folder namely dogs and cars, maybe that is causing some problem?



What do you want to achieve using RandomSample ? Please tell me what is the output of the following evaluation for you:

FileNames["*.JPG" | "*.JPEG", "C:\\Users\\cnn\\Desktop\\images\\test"] // Length


There are two sub folders in that folder namely dogs and cars

Really :) ? You should have told about that in your question. Now I don't understand how your initial code worked?

Just do following and it will work:

files = FileNames["*.JPG" | "*.JPEG", "C:\\Users\\cnn\\Desktop\\images\\test", Infinity]
testData = Import[#, ImageSize -> {224, 224}] & /@ files;
Posted 9 months ago

I just wanted to check whether the images have been imported or not using RandomSample. When I run the code you suggested it gave the following output:


I am new to MMA so I apologize for any inconvenience caused.

Please see the updated answer above.

Posted 9 months ago

Thanks a lot @Mikayel Egibyan, this time it worked flawlessly. Thank you so much for your time and patience.



Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract