VGG-19 Trained on ImageNet Competition Data

Identify the main object in an image

Released in 2014 by the Visual Geometry Group at the University of Oxford, this family of architectures achieved second place for the 2014 ImageNet Classification competition. It is noteworthy for its extremely simple structure, being a simple linear chain of layers, with all the convolutional layers having a kernel size of 3x3. Despite this simple structure, it achieves competitive classification accuracy compared to more complicated nets (such as GoogLeNet), although at the cost of slower evaluation speed and much larger net size.

Number of layers: 46 | Parameter count: 143,667,240 | Trained size: 575 MB |

Training Set Information

Performance

Examples

Resource retrieval

Get the pre-trained net:

In[1]:=
NetModel["VGG-19 Trained on ImageNet Competition Data"]
Out[1]=
Image

Basic usage

Classify an image:

In[2]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/364521e5-2245-4bc4-a8b0-f19cb8b761b7"]
Out[2]=
Image

The prediction is an Entity object, which can be queried:

In[3]:=
pred["Definition"]
Out[3]=
Image

Get a list of available properties of the predicted Entity:

In[4]:=
pred["Properties"]
Out[4]=
Image

Obtain the probabilities of the ten most likely entities predicted by the net:

In[5]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/1a482f67-92cf-4679-8c6a-c111df95e5c8"]
Out[5]=
Image

An object outside the list of the ImageNet classes will be misidentified:

In[6]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/801c3fac-92ee-4a93-8abc-ed92cb92e888"]
Out[6]=
Image

Obtain the list of names of all available classes:

In[7]:=
EntityValue[
 NetExtract[NetModel["VGG-19 Trained on ImageNet Competition Data"], "Output"][["Labels"]], "Name"]
Out[7]=
Image

Feature extraction

Remove the last three layers of the trained net, so that the net produces a vector representation of an image:

In[8]:=
extractor = Take[NetModel[
   "VGG-19 Trained on ImageNet Competition Data"], {1, -4}]
Out[8]=
Image

Get a set of images:

In[9]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/0d05015e-a98d-4b9a-ad42-68b4645a2d71"]

Visualize the features of a set of images:

In[10]:=
FeatureSpacePlot[imgs, FeatureExtractor -> extractor, LabelingSize -> 100, ImageSize -> 800]
Out[10]=
Image

Visualize convolutional weights

Extract the weights of the first convolutional layer in the trained net:

In[11]:=
weights = NetExtract[
   NetModel[
    "VGG-19 Trained on ImageNet Competition Data"], {"conv1_1", "Weights"}];

Visualize the weights as a list of 64 images of size 3x3:

In[12]:=
ImageAdjust[Image[#, Interleaving -> False]] & /@ Normal[weights]
Out[12]=
Image

Transfer learning

Use the pre-trained model to build a classifier for telling apart images of dogs and cats. Create a test set and a training set:

In[13]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/80ac3f76-9b3c-4c1c-bb1f-f24e3c911197"]
In[14]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/5638a1be-ae09-40e8-8ddf-a22ff7cc8aaa"]

Remove the linear layer from the pre-trained net:

In[15]:=
tempNet = Take[NetModel[
   "VGG-19 Trained on ImageNet Competition Data"], {1, -4}]
Out[15]=
Image

Create a new net composed of the pre-trained net followed by a linear layer and a softmax layer:

In[16]:=
newNet = NetChain[<|"pretrainedNet" -> tempNet, "linearNew" -> LinearLayer[], "softmax" -> SoftmaxLayer[]|>, "Output" -> NetDecoder[{"Class", {"cat", "dog"}}]]
Out[16]=
Image

Train on the dataset, freezing all the weights except for those in the "linearNew" layer (use TargetDevice -> "GPU" for training on a GPU):

In[17]:=
trainedNet = NetTrain[newNet, trainSet, LearningRateMultipliers -> {"linearNew" -> 1, _ -> 0}]
Out[17]=
Image

Perfect accuracy is obtained on the test set:

In[18]:=
ClassifierMeasurements[trainedNet, testSet, "Accuracy"]
Out[18]=
Image

Net information

Inspect the number of parameters of all arrays in the net:

In[19]:=
NetInformation[
 NetModel["VGG-19 Trained on ImageNet Competition Data"], \
"ArraysElementCounts"]
Out[19]=
Image

Obtain the total number of parameters:

In[20]:=
NetInformation[
 NetModel["VGG-19 Trained on ImageNet Competition Data"], \
"ArraysTotalElementCount"]
Out[20]=
Image

Obtain the layer type counts:

In[21]:=
NetInformation[
 NetModel["VGG-19 Trained on ImageNet Competition Data"], \
"LayerTypeCounts"]
Out[21]=
Image

Display the summary graphic:

In[22]:=
NetInformation[
 NetModel["VGG-19 Trained on ImageNet Competition Data"], \
"SummaryGraphic"]
Out[22]=
Image

Export to MXNet

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

In[23]:=
jsonPath = Export[FileNameJoin[{$TemporaryDirectory, "net.json"}], NetModel["VGG-19 Trained on ImageNet Competition Data"], "MXNet"]
Out[23]=
Image

Export also creates a net.params file containing parameters:

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

Get the size of the parameter file:

In[25]:=
FileByteCount[paramPath]
Out[25]=
Image

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

In[26]:=
ResourceObject[
  "VGG-19 Trained on ImageNet Competition Data"]["ByteCount"]
Out[26]=
Image

Represent the MXNet net as a graph:

In[27]:=
Import[jsonPath, {"MXNet", "NodeGraphPlot"}]
Out[104]=
Image
Out[27]=
Image

Requirements

Wolfram Language 11.2 (September 2017) or above

Resource History

Reference