主要内容

Preprocess Images for Deep Learning

To train a network and make predictions on new data, your images must match the input size of the network. If you need to adjust the size of your images to match the network, then you can rescale or crop your data to the required size.

您可以通过应用随机来有效地增加培训数据的量augmentationto your data. Augmentation also 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 in input images. AnaugmentedImageDatastore提供了一种方便的方法,将有限的增强量应用于2D图像,以解决分类问题。

要进行更高级的预处理操作,用于回归问题的预处理图像或预处理3-D体积图像,您可以从内置数据存储开始。您还可以使用自己的管道预处理图像转换结合功能。

使用缩放和裁剪调整图像大小

You can store image data as a numeric array, anImageDatastoreobject, or a table. AnImageDatastore使您能够批量导入数据从图像collections that are too large to fit in memory. You can use an augmented image datastore or a resized 4-D array for training, prediction, and classification. You can use a resized 3-D array for prediction and classification only.

这re are two ways to resize image data to match the input size of a network.

  • Rescaling multiplies the height and width of the image by a scaling factor. If the scaling factor is not identical in the vertical and horizontal directions, then rescaling changes the spatial extents of the pixels and the aspect ratio.

  • 种植提取图像的子区域并保留每个像素的空间范围。您可以从中心或图像中的随机位置裁剪图像。

Resizing Option 数据格式 调整功能 Sample Code
Rescaling
  • 3-D array representing a single color or multispectral image

  • 3-D array representing a stack of grayscale images

  • 4-D array representing a stack of images

精加工

im = imresize(I,outputSize);

outputSize指定重新缩放图像的尺寸。

  • 4-D array representing a stack of images

  • ImageDatastore

  • 桌子

augmentedImageDatastore

auimds = augmentedimagedatastore(outputsize,i);

outputSize指定重新缩放图像的尺寸。

Cropping
  • 3-D array representing a single color or multispectral image

imcrop(图像处理工具箱)

im = imcrop(I,rect);

矩形指定2D裁剪窗口的大小和位置。

  • 3-D array representing a stack of grayscale images

  • 4-D array representing a stack of color or multispectral images

imcrop3(图像处理工具箱)

im = imcrop3(I,cuboid);

cuboid指定3-D裁剪窗口的大小和位置。

  • 4-D array representing a stack of images

  • ImageDatastore

  • 桌子

augmentedImageDatastore

auimds = augmentedimagedatastore(输出尺寸,i,“ outputsizemode”,m);

Specifymas'centercrop'to crop from the center of the input image.

Specifymas'randcrop'从输入图像中的随机位置裁剪。

Augment Images for Training with Random Geometric Transformations

For image classification problems, you can use anaugmentedImageDatastore以调整大小,旋转,反射,剪切和翻译转换的随机组合增强图像。

该图显示了如何火车网uses an augmented image datastore to transform training data for each epoch. When you use data augmentation, one randomly augmented version of each image is used during each epoch of training. For an example of the workflow, see带有增强图像的火车网络

  1. 指定培训图像。

  2. 配置图像转换选项,例如旋转角度范围以及是否通过创建一个随机应用反射imageDataAugmenter

    Tip

    要预览应用于示例图像的转换,请使用augmentfunction.

  3. Create anaugmentedImageDatastore。指定the training images, the size of output images, and theimageDataAugmenter。这size of output images must be compatible with the size of theimageInputLayerof the network.

  4. 训练网络,将增强图像数据存储指定为数据源火车网。对于每次训练的迭代,增强图像数据存储都将转换的随机组合应用于小型培训数据中的图像。

    当您将增强图像数据存储用作训练图像的来源时,数据存储会随机捕获每个时期的训练数据,以便每个时代使用略有不同的数据集。每个时期的实际训练图像数量不会改变。转换的图像未存储在内存中。

Perform Additional Image Processing Operations Using Built-In Datastores

一些数据存储在阅读一批数据时执行特定且有限的图像预处理操作。这些特定于应用程序的数据存储在表中列出。您可以将这些数据存储用作使用深度学习Toolbox™的深度学习应用程序的培训,验证和测试数据集的来源。所有这些数据存储都以支持的格式返回数据万博1manbetx火车网

数据存储 描述
augmentedImageDatastore 应用随机的仿射几何变换,包括调整大小,旋转,反射,剪切和翻译,以训练深层神经网络。例如,请参阅使用预验证的网络进行转移学习
pixelLabelImageDatastore(计算机视觉工具箱) Apply identical affine geometric transformations to images and corresponding ground truth labels for training semantic segmentation networks (requires Computer Vision Toolbox™). For an example, seeSemantic Segmentation Using Deep Learning
randomPatchExtractionDatastore(图像处理工具箱) 从图像或像素标签图像中提取多对随机补丁(需要图像处理工具箱™)。您可以选择地将相同的随机仿射几何变换应用于斑块对。例如,请参阅使用深度学习增加图像分辨率
DeNoisingImagedAtatastore(图像处理工具箱) Apply randomly generated Gaussian noise for training denoising networks (requires Image Processing Toolbox).

Apply Custom Image Processing Pipelines Using Combine and Transform

To perform more general and complex image preprocessing operations than offered by the application-specific datastores, you can use the转换结合功能。For more information, see数据存储s for Deep Learning

使用图像数据转换数据存储

转换函数创建了一个变更形式的数据存储,称为underlying datastore, by transforming the data read by the underlying datastore according to a transformation function that you define.

自定义转换功能必须接受以下格式的数据基础数据存储的功能。对于图像数据ImageDatastore,格式取决于ReadSizeproperty .

  • 什么时候ReadSizeis 1, the transformation function must accept an integer array. The size of the array is consistent with the type of images in theImageDatastore。For example, a grayscale image has dimensionsm-经过-n,TrueColor图像具有尺寸m-经过-n-by-3,以及带有多光谱图像cchannels has dimensionsm-经过-n-经过-c

  • 什么时候ReadSizeis greater than 1, the transformation function must accept a cell array of image data. Each element corresponds to an image in the batch.

转换函数必须返回与网络输入大小相匹配的数据。这转换功能不支持一对多观察映射。万博1manbetx

Tip

转换函数支持基础时的预万博1manbetx取ImageDatastore读s a batch of JPG or PNG image files. For these image types, do not use thereadfcn争论ImageDatastoreto apply image preprocessing, as this option is usually significantly slower. If you use a custom read function, thenImageDatastoredoes not prefetch.

将数据存储与图像数据相结合

结合function concatenates the data read from multiple datastores and maintains parity between the datastores.

  • 将数据连接到两列表或两列单元阵列中,用于具有单个输入的训练网络,例如图像到图像回归网络。

  • Concatenate data to a (numInputs+1)-column cell array for training networks with multiple inputs.

See Also

||||

Related Examples

More About