"Image" (Net Encoder)

NetEncoder["Image"]

represents an encoder that converts a 2D image to a rank-3 tensor of pixel values.

NetEncoder[{"Image",size}]

represents an encoder that resizes the input image to size.

NetEncoder[{"Image",{width,height}}]

represents an encoder that resizes the input image to the specified dimensions.

NetEncoder[{"Image",size,"param"val,}]

represents an encoder with specific parameters for preprocessing.

Details

  • NetEncoder[][input] applies the encoder to an input to produce an output.
  • NetEncoder[][{input1,input2,}] applies the encoder to a list of inputs to produce a list of outputs.
  • The input to the encoder can be an Image[] object or a File[] expression.
  • NetEncoder["Image"] is equivalent to NetEncoder[{"Image",{128,128}}].
  • An encoder can be attached to an input port of a net by specifying "port"->NetEncoder[] when constructing the net.
  • Parameters
  • The following parameters are supported:
  • ColorSpace"RGB"color space to use for encoding
    "DataTransposed"Falsewhether width is the first dimension
    InterleavingFalsewhether channels are interleaved
    "MeanImage"Nonemean image to subtract
    "VarianceImage"Nonevariance image to normalize by
    Method"Stretch"how to conform the size
    ResamplingAutomaticresampling method
    AlignmentCenterhow to align the image for Method"Fit" or "Fill"
    PaddingBlackpadding scheme for Method"Fit"
  • Possible values for Method are:
  • "Stretch"stretch the image to fit by resampling
    "Fit"fit the whole image; keep the aspect ratio; pad if necessary
    "Fill"fit the smaller dimension; crop the other if necessary
  • The Interleaving and "DataTransposed" options affect the output shape in the following way:
  • The following settings can be used for "MeanImage" and "VarianceImage":
  • Nonedo not subtract anything
    mvalue to subtract from each pixel
    {m1,m2,}values to be subtracted from different channels
    Image[]overall image to subtract
  • Pixels are normalized to lie between 0 and 1 before doing subtraction.

Examples

open allclose all

Basic Examples  (1)

Create an image encoder of a specified size:

Encode an image:

Visualize the encoded values as an image:

Scope  (1)

NetEncoder["Image"] can encode either File or Image objects. Create an image encoder:

Apply the encoder to a File object:

Apply the encoder to an Image object:

Apply the encoder to a list of images:

Parameters  (12)

ColorSpace  (1)

Set the ColorSpace of the output image to "Grayscale":

The output only has one channel dimension, appropriate for grayscale images.

"DataTransposed"  (2)

With "DataTransposed"False, the height is the first dimension in the output:

With "DataTransposed"True, the width is the first dimension in the output:

With "DataTransposed"True and InterleavingTrue, the image can be scaled to a fixed height and a varying width:

Interleaving  (2)

With InterleavingFalse, the channel dimension of the output is the first dimension in the dimensions list:

With InterleavingTrue, the channel dimension of the output is the last dimension in the dimensions list:

With InterleavingTrue, the image can be scaled to a fixed width and a varying height:

"MeanImage"  (1)

Create an image encoder that subtracts a list of mean values from the encoded image:

Create an image with every pixel value being 0.8:

Apply the encoder to the image:

This is equivalent to:

"VarianceImage"  (1)

Create an image encoder that normalizes the encoded image by a list of variances:

Create an image with every pixel value being 0.8:

Apply the encoder to the image:

This is equivalent to:

Method  (2)

Create an image encoder that conforms the dimensions by fitting the whole image and padding if necessary:

Encode an image:

Visualize the encoded values as an image:

Create an image encoder that conforms the dimensions by fitting the smaller dimension and cropping if necessary:

Encode an image:

Visualize the encoded values as an image:

Resampling  (1)

Create an image encoder that conforms the dimensions by scaling using a piecewise linear interpolation:

Encode an image:

Visualize the encoded values as an image:

Padding  (1)

Create an image encoder that conforms the dimensions by fitting the whole image and padding with gray if necessary:

Encode an image:

Visualize the encoded values as an image:

Alignment  (1)

Create an image encoder that conforms the dimensions by fitting the whole image and padding on the right side or at the bottom if necessary:

Encode images:

Visualize the encoded values as images:

Properties & Relations  (2)

When applying an image encoder to a list of File objects, NetEncoder will attempt to parallelize the computation on multicore processors. Create a list of 64 images:

Create an image encoder:

Show the number of available processors:

Map the encoder over the images:

To exploit parallelism, apply the encoder to a list of File objects directly rather than with Map:

NetTrain will automatically try to attach an encoder when a net is not fully specified. Automatic attachment of an image encoder: