Resize images in ImageDataStore on GPU

4 次查看(过去 30 天)
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

回答(4 个)

Joss Knight
Joss Knight 2017-10-16
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 个评论
RP
RP 2017-10-19
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 2017-10-24
Sorry. Set the ReadSize to the size of the batch and concatenate:
imds.ReadSize = 128;
batch = imds.read();
batch = cat(4, batch{:});

请先登录,再进行评论。


ENVER AKBACAK
ENVER AKBACAK 2018-2-27
编辑:ENVER AKBACAK 2018-2-27
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 个评论
Anup Gurung
Anup Gurung 2018-3-19
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]);

请先登录,再进行评论。


ENVER AKBACAK
ENVER AKBACAK 2018-4-27
Try this
imds = imageDatastore('Shapes','IncludeSubfolders',true,'Labelsource','foldernames');
imds.ReadSize = numpartitions(imds)
imds.ReadFcn = @(loc)imresize(imread(loc),[32,32]);
  2 个评论
ENVER AKBACAK
ENVER AKBACAK 2018-11-21
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.

请先登录,再进行评论。


benhadid ahmed
benhadid ahmed 2018-7-17
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)

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by