Computer Vision

Image Classification

MNIST Digit Classification

Train a digit recognizer on the MNIST database of handwritten digits using a convolutional neural network.
First, obtain the training and validation data:
Define a convolutional neural network that takes in 28×28 grayscale images as input:
Train the network for three training rounds. NetTrain will automatically attach a CrossEntropyLossLayer using the same classes that were provided to the decoder:
Evaluate the trained network directly on images randomly sampled from the validation set:
Obtain probability estimates of all classes for a specific input:
Use NetMeasurements to test the classification performance of the trained net on the test set:

CIFAR-10 Object Classification

Using the CIFAR-10 database of labeled images, train a convolutional net to predict the class of each object. First, obtain the training data:
Extract the unique classes:
Create a convolutional net that predicts the class, given an image:
Train the net on the training data:
Predict the most likely classes for a set of images:
For a specific example, give the probabilities of the most likely label assignments:
From a random sample, select the images for which the net produces highest and lowest entropy predictions. High-entropy inputs can be interpreted as those for which the net is most uncertain about the correct class:
Learned Embedding
Learn an embedding of the digits in the MNIST dataset.
First, import the data and take only those examples with labels between 0 and 4:
Create a training set by sampling pairs of images and associating them with True if their labels are different and False if their labels are the same:
Define a convolutional net to use as an embedding network:
Construct the embedding net:
Train the network:
Apply the network to a list of pairs of digits to compute their distances under the embedding. Digits with the same label have small distances:
Extract the embedding network:
Compute the embedding of a digit:
Sample 500 digits and group them by their labels:
Compute their embeddings and plot them. Digits with the same label are clustered under the learned embedding:
Style Transfer
Create a new image with the content of one image and in the style of another image. This implementation follows the method described in Gatys et al., "A Neural Algorithm of Artistic Style".
An example content and style image:
To create the image that is a mix of both of these images, start by obtaining a pre-trained image classification network:
Take a layer that will be used as a feature extractor for the style and content images:
There are three loss functions used. The first loss ensures that the content of the synthesized image is similar to that of the content image:
The second loss ensures that the style of the synthesized image is similar to that of the style image. Style similarity is defined as the mean-squared difference between the Gram matrices of the input and target:
The third loss ensures that the magnitude of intensity changes across adjacent pixels in the synthesized image is small. This helps the synthesized image look more natural:
Define a function that creates the final training net for any content and style image. This function also creates a random initial image:
Define a NetDecoder for visualizing the predicted image:
The training data consists of features extracted from the content and style images. Define a feature extraction function:
Create a training set consisting of a single example of a content and style feature:
Create the training net whose input dimensions correspond to the content and style image dimensions:
When training, the three losses are weighted differently to set the relative importance of the content and style. These values might need to be changed with different content and style images. Create a loss specification that defines the final loss as a combination of the three losses:
Optimize the image using NetTrain. LearningRateMultipliers are used to freeze all parameters in the net except for the NetArrayLayer. The training is best done on a GPU, as it will take up to an hour to get good results with CPU training. The training can be stopped at any time via Evaluation Abort Evaluation:
Extract the final image from the NetArrayLayer of the trained net:
Semantic Segmentation (Pixel Classification)

Semantic Segmentation on a Toy Text Dataset

Train a net that classifies every pixel in an image of a word as being part of the background or part of one of the letters a through z.
First, generate training and test data, which consists of images of words and the corresponding "mask" integer matrices that label each pixel:
Obtain the number of classes:
Assign a color to each class index:
Obtain a random sample of input images in the training set:
Visualize the mask array of a single example, where each color represents a particular class:
Define a convolutional net that takes an image and returns a probability vector for every pixel in the image:
GPU training is recommended, as CPU training could take up to an hour. Train the net:
Create a prediction function that gives the index of the most likely class at each pixel:
Predict the pixel classes of an image from the test set:
Plot the actual target mask of the example:
Obtain a sorted list of probabilities for a specific pixel to be a certain class:
Define a function that rasterizes a string and then applies the trained net to give a matrix of indices corresponding to the most likely class of each pixel:
Some characters are easily distinguished from other characters:
Other characters are harder:
Obtain the pixel-level accuracy of the trained net on the test set: