Wolfram Computation Meets Knowledge

LeNet Trained on MNIST Data

Identify the handwritten digit in an image

This pioneer work for image classification with convolutional neural nets was released in 1998. It was developed by Yann LeCun and his collaborators at AT&T Labs while they experimented with a large range of machine learning solutions for classification on the MNIST dataset. This model achieves 98.5% accuracy on the MNIST test set.

Number of layers: 147 | Parameter count: 5,978,677 | Trained size: 1,750 KB

Training Set Information

Training Set Data

Examples

Resource retrieval

Retrieve the resource object:

In[1]:=
ResourceObject["LeNet Trained on MNIST Data"]
Out[1]=

Retrieve the pre-trained net:

In[2]:=
NetModel["LeNet Trained on MNIST Data"]
Out[2]=

Basic usage

Apply the trained net to a set of inputs:

In[3]:=
NetModel["LeNet Trained on MNIST Data"][{\!\(\*
GraphicsBox[
TagBox[RasterBox[CompressedData["
1:eJxTTMoPSmNiYGAo5gASQYnljkVFiZXBAkBOaF5xZnpeaopnXklqemqRRRJI
mQwU/6cK2MHQhEtqiTTTJVxyQUy633BIneZSeolLWwSTBS6pP1ZM+bjkzjEJ
4ZL6X81kjkvqKydTHS65L0y45c4wMS3FJdfOxHRtcW3tkY/Y5cBA+T5WOaPk
5GgmJkNMOQWwW/6FMMliyqWB5X46MlliNTPo//+JTExbMOU+Ad0h683ElP8P
U+73fCGQM4XuYPHD//97LZmYLPdhlRrUAABgHMjK
"], {{0, 28}, {28, 0}}, {0, 
       255},
ColorFunction->GrayLevel],
BoxForm`ImageTag[
      "Byte", ColorSpace -> Automatic, Interleaving -> None],
Selectable->False],
DefaultBaseStyle->"ImageGraphics",
ImageSizeRaw->{28, 28},
PlotRange->{{0, 28}, {0, 28}}]\), \!\(\*
GraphicsBox[
TagBox[RasterBox[CompressedData["
1:eJxTTMoPSmNiYGAo5gASQYnljkVFiZXBAkBOaF5xZnpeaopnXklqemqRRRJI
mQwU/x9IsJWBKew2dql3okzMzH6/sMr5M7tPNGWeglXOQPf//yPiZdjlgMI7
mbHKVTOVQQkM8FCaef+v3dIih7HIfQphnnKCmdntBzYzdzKLizEzM+/DJvdD
hxnoP+xueafNxCvJwWSG3S0SV/+7M/tikbvAzHzyP1AOi5lvNJj9fwPlVJdj
ytUzMx8HUu7xWIw0ZdJ49f/PJUWTL5hyTMwT//+/xMxcgkUfE/Ou/92KzKGf
sMqpGLBzTb6MRer/Sh1gmGDzG/0AACEauS8=
"], {{0, 28}, {28, 0}}, {0, 255},
       
ColorFunction->GrayLevel],
BoxForm`ImageTag[
      "Byte", ColorSpace -> Automatic, Interleaving -> None],
Selectable->False],
DefaultBaseStyle->"ImageGraphics",
ImageSizeRaw->{28, 28},
PlotRange->{{0, 28}, {0, 28}}]\), \!\(\*
GraphicsBox[
TagBox[RasterBox[CompressedData["
1:eJxTTMoPSmNiYGAo5gASQYnljkVFiZXBAkBOaF5xZnpeaopnXklqemqRRRJI
mQwU/x9IsN/BdW1Vstx8LFK7eZgYmYBAcepvNJkvG/mZIHJMTLfR5OaBBAtX
r44CKqn4hSK1TRgoZQNipQEZp5ClPpoCRSr+gJiXgarykeWWAqUqoSalocrd
52ViCnwJMx5VLpeJyQVufyOK3Fo2JqYWOM+WiWkFkjZGRjs4p4mRkXE5nPcp
GegQGOeXM9BZ1vth3EAgT3I+lHMOyLE7Adf3y4uJqeY1hP3MFShXhbDuTwgT
0ywIa5s9Wnh+AnIFErYBgSVIRrz4G0LumxoTFIDiQaztPzLIQ5JzPogi9f+Z
NkzOof77fzTwY1EcSKps5y90GToCAMITbxU=
"], {{0, 28}, {28, 0}}, {0, 255},
       
ColorFunction->GrayLevel],
BoxForm`ImageTag[
      "Byte", ColorSpace -> Automatic, Interleaving -> None],
Selectable->False],
DefaultBaseStyle->"ImageGraphics",
ImageSizeRaw->{28, 28},
PlotRange->{{0, 28}, {0, 28}}]\)}]
Out[3]=

Give class probabilities for a single input:

In[4]:=
NetModel["LeNet Trained on MNIST Data"][\!\(\*
GraphicsBox[
TagBox[RasterBox[CompressedData["
1:eJxTTMoPSmNiYGAo5gASQYnljkVFiZXBAkBOaF5xZnpeaopnXklqemqRRRJI
mQwU/x9IsJWBKew2dql3okzMzH6/sMr5M7tPNGWeglXOQPf//yPiZdjlgMI7
mbHKVTOVQQkM8FCaef+v3dIih7HIfQphnnKCmdntBzYzdzKLizEzM+/DJvdD
hxnoP+xueafNxCvJwWSG3S0SV/+7M/tikbvAzHzyP1AOi5lvNJj9fwPlVJdj
ytUzMx8HUu7xWIw0ZdJ49f/PJUWTL5hyTMwT//+/xMxcgkUfE/Ou/92KzKGf
sMqpGLBzTb6MRer/Sh1gmGDzG/0AACEauS8=
"], {{0, 28}, {28, 0}}, {0, 255},
      
ColorFunction->GrayLevel],
BoxForm`ImageTag[
     "Byte", ColorSpace -> Automatic, Interleaving -> None],
Selectable->False],
DefaultBaseStyle->"ImageGraphics",
ImageSizeRaw->{28, 28},
PlotRange->{{0, 28}, {0, 28}}]\), "Probabilities"]
Out[4]=

Feature extraction

Create a subset of the MNIST dataset:

In[5]:=
sample = Keys[RandomSample[ResourceData["MNIST"], 150]]
Out[5]=

Remove the last linear layer of the net, which will be used as a feature extractor:

In[6]:=
extractor = Take[NetModel["LeNet Trained on MNIST Data"], {1, -3}]
Out[6]=

Visualize the features of a subset of the MNIST dataset:

In[7]:=
FeatureSpacePlot[sample, FeatureExtractor -> extractor]
Out[7]=

Visualization of net operation

Extract the convolutional features from the first layer:

In[8]:=
convFeatures = NetModel["LeNet Trained on MNIST Data"][\!\(\*
GraphicsBox[
TagBox[RasterBox[CompressedData["
1:eJxTTMoPSmNiYGAo5gASQYnljkVFiZXBAkBOaF5xZnpeaopnXklqemqRRRJI
mQwU/x+sYDHDDlxSN7jtoKw9McdRpf6EMC6HsLZxFn9GlVvEGP4PzHjM6/AX
Veo6p+oXMOOnifA7NCk5ZoiJv+MYJ6FK/YtgnANhlTC6/kSR+lbKGPkb4lhW
6Zeo2rIYGbNqWq8AWa6sB1Gl+lgYGRkNGNniZqWz5KNKXRBkYCoD+mi9Lzsj
46bnv5Hl1nCHzwYzPjgwhtfWXkHRCFM6hzEcRRMSuKXEdxuH1P+ljKm4pN4p
ilzAJbeGMQOX1E8Ribu45JYzeuCS+l/KfQCnHNUAAFQtzfI=
"], {{0, 28}, {28, 
       0}}, {0, 255},
ColorFunction->GrayLevel],
BoxForm`ImageTag[
      "Byte", ColorSpace -> Automatic, Interleaving -> None],
Selectable->False],
DefaultBaseStyle->"ImageGraphics",
ImageSizeRaw->{28, 28},
PlotRange->{{0, 28}, {0, 28}}]\), NetPort[1, "Output"]]
Out[8]=

Visualize the features:

In[9]:=
ImageCollage[Map[ImageAdjust@*Image, convFeatures], 
 ImageSize -> Medium]
Out[9]=

Training the uninitialized architecture

Retrieve the uninitialized architecture:

In[10]:=
net = NetModel["LeNet Trained on MNIST Data", 
  "UninitializedEvaluationNet"]
Out[10]=

Retrieve the MNIST dataset:

In[11]:=
mnist = ResourceObject["MNIST"]
Out[11]=

Use the training dataset provided:

In[12]:=
trainSet = ResourceData[mnist, "TrainingData"]
Out[12]=

Use the test dataset provided:

In[13]:=
valSet = ResourceData[mnist, "TestData"]
Out[13]=

Train the net:

In[14]:=
trained = NetTrain[net, trainSet, ValidationSet -> valSet]
Out[14]=

Generate a ClassifierMeasurementsObject of the net with the test set:

In[15]:=
cm = ClassifierMeasurements[trained, valSet]
Out[15]=

Evaluate the accuracy on the validation set:

In[16]:=
cm["Accuracy"]
Out[16]=

Visualize the confusion matrix:

In[17]:=
cm["ConfusionMatrixPlot"]
Out[17]=

Export to MXNet

Export the net into a format that can be opened in MXNet:

In[18]:=
jsonPath = 
 Export[FileNameJoin[{$TemporaryDirectory, "net.json"}], 
  NetModel["LeNet Trained on MNIST Data"], "MXNet"]
Out[18]=

Export also creates a net.params file containing parameters:

In[19]:=
paramPath = FileNameJoin[{DirectoryName[jsonPath], "net.params"}]
Out[19]=

Get the size of the parameter file:

In[20]:=
FileByteCount[paramPath]
Out[20]=

The size is similar to the byte count of the resource object:

In[21]:=
ResourceObject["LeNet Trained on MNIST Data"]["ByteCount"]
Out[21]=

Represent the MXNet net as a graph:

In[22]:=
Import[jsonPath, {"MXNet", "NodeGraphPlot"}]
Out[22]=

Requirements

Wolfram Language 11.1 (March 2017) or above

Reference

  • Y. LeCun, L. Bottou, Y. Bengio, P. Haffner, "Gradient-Based Learning Applied to Document Recognition," Proceedings of the IEEE, 86, 2278-2324 (1998)
  • (available from http://yann.lecun.com/exdb/lenet)