主要内容

Create and Explore Datastore for Image Classification

此示例显示了如何创建,读取和增强图像数据存储,以用于培训深度学习网络。特别是,此示例显示了如何创建ImageDatastoreobject from a collection of images, read and extract the properties of the datastore, and create anaugmentedImageDatastore在培训期间使用。

创建图像数据存储

Use an成像对象管理大量无法完全适合内存的图像。大量图像集合在深度学习应用中很常见,这些应用程序定期涉及数千个标记图像的培训。这些图像通常存储在文件夹中,每个类别包含图像的子文件夹。

Download Data Set

该示例使用示例食品图像数据集,其中包含978张九类食品照片,大约为77 MB。下载example foodimagedataset.zipfile from the MathWorks website, then unzip the file.

zipFile = matlab.internal.examples.downloadSupportFile('nnet','data/ExampleFoodImageDataset.zip');filepath = fileparts(zipFile); dataFolder = fullfile(filepath,'ExampleFoodImageDataset');UNZIP(Zipfile,DataFolder);

此数据集中的图像分为每个类的子文件夹。

Create an image datastore from the images in the path and their subfolders. Use the folder names as label names.

foodimds = imagedatastore(datafolter,。。。“包括橡皮folders”,true,。。。“ Labelsource”,'foldernames');

Properties of Datastore

Extract the properties of the datastore.

查找观察总数。该数据集有978个观测值分为9个类。

numobs =长度(foodimds.Labels)
numObs = 978

查找每个课程的观察次数。您可以看到此数据集在每个类中不包含相等数量的观察值。

numObsPerClass = countEachLabel(foodImds)
numObsPerClass=9×2桌Label Count _____________ _____ caesar_salad 26 caprese_salad 15 french_fries 181 greek_salad 24 hamburger 238 hot_dog 31 pizza 299 sashimi 40 sushi 124

You can also visualize the distribution of the class labels using a histogram.

histogram(foodImds.Labels) set(gca,'TickLabelInterpreter','none')

探索Datastore

通过查看数据存储的随机选择图像,检查数据是否如预期。

numobstoshow = 8;idx = randperm(numobs,numobstoshow);imshow(imtile(foodimds.files(idx),,'GridSize',[2 4],“缩略图”,[100 100]))

You can also view images that belong to a specific class.

class ="pizza"; idxClass = find(foodImds.Labels == class); idx = randsample(idxClass,numObsToShow); imshow(imtile(foodImds.Files(idx),'GridSize',[2 4],“缩略图”,[100 100]));

To take a closer look at individual images in your datastore or folder, use theImage Browser(图像处理工具箱)应用程序。

Image Augmentation

Augmentation enables you to train networks to be invariant to distortions in image data. For example, you can add randomized rotations to input images so that a network is invariant to the presence of rotation. AnaugmentedImageDatastore对象提供了一种方便的方法,将有限的增强集应用于2-D图像以解决分类问题。

Define an augmentation scheme. This scheme applies a random rotation between [–90,90] degrees and a random scaling between [1,2]. The augmented datastore automatically resizes the images to the输入培训期间的价值。

imageAugmenter = imageDataAugmenter(。。。'randrotation',[-90 90],。。。'RandScale',[1 2]); inputSize = [100 100];

Using the augmentation scheme, define the augmented image datastore.

augfoodimds = augmentedimagedatastore(inputsize,foodimds,。。。'DataAugmentation',imageAugmenter);

增强数据存储包含与原始图像数据存储相同的图像数量。

augFoodImds.NumObservations
ANS = 978

When you use an augmented image datastore as a source of training images, the datastore randomly perturbs the training data for each epoch, where an epoch is a full pass of the training algorithm over the entire training data set. Therefore, each epoch uses a slightly different data set, but the actual number of training images in each epoch does not change.

Visualize Augmented Data

Visualize the augmented image data that you want to use to train the network.

Shuffle the datastore.

augfoodimds =洗牌(augfoodimds);

TheaugmentedImageDatastoreobject applies the transformations when reading the datastore and does not store the transformed images in memory. Consequently, each time you read the same images, you see a random combination of the augmentations defined.

Use thefunction to read a subset of the augmented datastore.

subset1 = read(augFoodImds);

Reset the datastore to its state before calling read and read a subset of the datastore again.

重置(augfoodimds)subset2 = read(augfoodimds);

显示增强图像的两个子集。

imshow (imtile (subset1.input,'GridSize',[2 4]))

imshow(imtile(subset2.input,'GridSize',[2 4]))

您可以看到两个实例都显示出具有不同转换的相同图像。将转换应用于图像对于深度学习应用程序很有用,因为您可以在图像的随机更改版本上训练网络。这样做会使网络暴露于该类别的不同图像变化,并使IT可以学会将图像分类,即使它们具有不同的视觉属性。

创建数据存储对象后,请使用深网设计师应用程序或trainNetwork训练图像分类网络的功能。例如,请参阅Transfer Learning Using Pretrained Network

For more information on preprocessing images for deep learning applications, seePreprocess Images for Deep Learning。您也可以使用使用该效果应用更高级的增强量,例如不同的亮度或饱和度。转换结合functions. For more information, seeDatastores for Deep Learning

See Also

|||

相关话题