Resize images in ImageDataStore on GPU

13 Ansichten (letzte 30 Tage)
RP
RP am 3 Okt. 2017
Kommentiert: ENVER AKBACAK am 21 Nov. 2018
I have trained a neural net (based on AlexNet) and want to classify new images by means of this neural net. First I read the images into an ImageDataStore but for AlexNet the dimension of the images have to be 227 x 227. Can I resize all images in the ImageDataStore on GPU? I currently use below code for the resizing but am wondering if this is the "fastest" way.
newSample.ReadFcn = @(newSample)imresize(imread(newSample),[227 227])
Thanks for your help,
Raf

Antworten (4)

Joss Knight
Joss Knight am 16 Okt. 2017
A ReadFcn is fine for classifying one image at a time. For classifying large batches of images it will slow things down considerably. You should resize all your data in batches before calling classify:
batchData = imresize(imds.read(), [227 227]);
If you have a large dataset to classify, it may be best simply to resize all the images and save them to new files.
For training, in R2017b you can use the augmentedImageSource.
  2 Kommentare
RP
RP am 19 Okt. 2017
Thanks for your suggestion Joss. I'm fairly new to Matlab and noticed that only my first image ends up in batchData. Do I have to create a for loop around the batchData resizing?
Joss Knight
Joss Knight am 24 Okt. 2017
Sorry. Set the ReadSize to the size of the batch and concatenate:
imds.ReadSize = 128;
batch = imds.read();
batch = cat(4, batch{:});

Melden Sie sich an, um zu kommentieren.


ENVER AKBACAK
ENVER AKBACAK am 27 Feb. 2018
Bearbeitet: ENVER AKBACAK am 27 Feb. 2018
Hi,
Set the number of images to be read in imds by executing:
imds.ReadSize = numpartitions(imds)
Then resize all images by:
imds.ReadFcn = @(loc)imresize(imread(loc),[227,227]);
thats it, all your images in imds is now [227,227]
check some of them randomly:
img = readimage(imds,5); imshow(img); size(img)
img = readimage(imds,222); imshow(img); size(img)
  1 Kommentar
Anup Gurung
Anup Gurung am 19 Mär. 2018
Hi I am trying to do the same thing, i want to reduce the size of the images i have in my folders. But its not working for me. Can you help me?
imds = imageDatastore('Shapes','IncludeSubfolders',true,'Labelsource','foldernames');
% % Count the label and how many images are there in each of the label % images
labelCount = countEachLabel(imds); % % Resize images in an image datastore
imds.ReadSize = (imds)25; imds.ReadFcn = @(loc)imresize(imread(loc),[32,32,1]);

Melden Sie sich an, um zu kommentieren.


ENVER AKBACAK
ENVER AKBACAK am 27 Apr. 2018
Try this
imds = imageDatastore('Shapes','IncludeSubfolders',true,'Labelsource','foldernames');
imds.ReadSize = numpartitions(imds)
imds.ReadFcn = @(loc)imresize(imread(loc),[32,32]);
  2 Kommentare
Rituraj Kushwaha
Rituraj Kushwaha am 21 Nov. 2018
what is loc?
ENVER AKBACAK
ENVER AKBACAK am 21 Nov. 2018
It is the parameter (input) of anonymous function. You can use any proper name instead. Each images in imds passed the function over this parameter. For example sqr= @y y.^2;
c=sqr(5);
c=25
here, 5 passed the function through y, like loc.

Melden Sie sich an, um zu kommentieren.


benhadid ahmed
benhadid ahmed am 17 Jul. 2018
try this you make a function to read a picture and resize it like this function img = CrerDataStore( file ) % Import image img = imread(file); % resize image img = imresize(img,[227 227]); % Convert grayscale to color (RGB) if you need img = repmat(img,[1 1 3]); end
after this you make your call function in your program like this imds= imageDatastore(pathToImages,'ReadFcn',@CrerDataStore); img=readimage(imds,1); size(img)

Community Treasure Hunt

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

Start Hunting!

Translated by