Wolfram Language

Image and Signal Processing

Image Recognition Using Deep Learning

Deep learning can be applied to many image processing and computer vision problems with great success. Using NetChain and NetTrain, you can define and train a neural network that categorizes a handwritten digit given an image.

Obtain training and validation data from the MNIST database of handwritten digits.

Copy to clipboard.
In[1]:=
Click for copyable input
resource = ResourceObject["MNIST"]; trainingData = ResourceData[resource, "TrainingData"]; testData = ResourceData[resource, "TestData"];
Copy to clipboard.
In[2]:=
Click for copyable input
RandomSample[trainingData, 5]
Out[2]=

Design a convolutional neural network architected for recognizing 28×28 grayscale images.

Copy to clipboard.
In[3]:=
Click for copyable input
lenet = NetChain[ {ConvolutionLayer[20, 5], Ramp, PoolingLayer[2, 2], ConvolutionLayer[50, 5], Ramp, PoolingLayer[2, 2], FlattenLayer[], 500, Ramp, 10, SoftmaxLayer[]}, "Output" -> NetDecoder[{"Class", Range[0, 9]}], "Input" -> NetEncoder[{"Image", {28, 28}, "Grayscale"}] ]
Out[3]=

Train the network for three training rounds.

Copy to clipboard.
In[4]:=
Click for copyable input
lenet = NetTrain[lenet, trainingData, ValidationSet -> testData, MaxTrainingRounds -> 3];
Out[4]=

Evaluate the trained network directly on images randomly sampled from the validation set.

Copy to clipboard.
In[5]:=
Click for copyable input
imgs = Keys @ RandomSample[testData, 5]; Thread[imgs -> lenet[imgs]]
Out[5]=

Related Examples

Find out if you already have access to Wolfram tech through your organization
×