MobileNet V2 Trained on ImageNet Competition Data

Identify the main object in an image

Released in 2018 by researchers at Google, these models improve upon the performance of previous MobileNet models. The models introduce new inverted residual structures featuring shortcut connections between the thin bottleneck layers. Like their predecessors, the expansion layers use lightweight depthwise convolutions.

Number of models: 22

Training Set Information

Performance

Examples

Resource retrieval

Get the pre-trained net:

In[1]:=
NetModel["MobileNet V2 Trained on ImageNet Competition Data"]
Out[1]=

NetModel parameters

This model consists of a family of individual nets, each identified by a specific parameter combination. Inspect the available parameters:

In[2]:=
NetModel["MobileNet V2 Trained on ImageNet Competition Data", "ParametersInformation"]
Out[2]=

Pick a non-default net by specifying the parameters:

In[3]:=
NetModel[{"MobileNet V2 Trained on ImageNet Competition Data", "Depth" -> 1., "Width" -> 224}]
Out[3]=

Pick a non-default uninitialized net:

In[4]:=
NetModel[{"MobileNet V2 Trained on ImageNet Competition Data", "Depth" -> 0.75, "Width" -> 224}, "UninitializedEvaluationNet"]
Out[4]=

Basic usage

Classify an image:

In[5]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/9ad83e1a-fb2f-4e86-9941-87f3da1fb5c7"]
Out[5]=

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

In[6]:=
pred["Definition"]
Out[6]=

Get a list of available properties of the predicted Entity:

In[7]:=
pred["Properties"]
Out[7]=

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

In[8]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/da5b5dba-4895-4e4b-8a25-527b554e1c5d"]
Out[8]=

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

In[9]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/e2c7d20d-b2cd-411d-b12b-f11eebe710b3"]
Out[9]=

Obtain the list of names of all available classes:

In[10]:=
EntityValue[
 NetExtract[
   NetModel["MobileNet V2 Trained on ImageNet Competition Data"], "Output"][["Labels"]], "Name"]
Out[10]=

Feature extraction

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

In[11]:=
extractor = NetTake[NetModel[
   "MobileNet V2 Trained on ImageNet Competition Data"], "fc7"]
Out[11]=

Get a set of images:

In[12]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/50baa58c-0677-4b08-ba99-5506105e21a9"]

Visualize the features of a set of images:

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

Visualize convolutional weights

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

In[14]:=
weights = NetExtract[
  NetModel["MobileNet V2 Trained on ImageNet Competition Data"], {"1",
    "conv1", "Weights"}]
Out[14]=

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

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

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[16]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/01ec674d-be15-4b55-94d1-897cbe0ea5f0"]
In[17]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/a0bfae48-7d1a-4955-87c2-f77253642017"]

Remove the linear layer from the pre-trained net:

In[18]:=
tempNet = NetTake[NetModel[
   "MobileNet V2 Trained on ImageNet Competition Data"], "fc7"]
Out[18]=

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

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

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

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

Perfect accuracy is obtained on the test set:

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

Net information

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

In[22]:=
NetInformation[
 NetModel["MobileNet V2 Trained on ImageNet Competition Data"], "ArraysElementCounts"]
Out[22]=

Obtain the total number of parameters:

In[23]:=
NetInformation[
 NetModel["MobileNet V2 Trained on ImageNet Competition Data"], "ArraysTotalElementCount"]
Out[23]=

Obtain the layer type counts:

In[24]:=
NetInformation[
 NetModel["MobileNet V2 Trained on ImageNet Competition Data"], "LayerTypeCounts"]
Out[24]=

Display the summary graphic:

In[25]:=
NetInformation[
 NetModel["MobileNet V2 Trained on ImageNet Competition Data"], "SummaryGraphic"]
Out[25]=

Export to MXNet

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

In[26]:=
jsonPath = Export[FileNameJoin[{$TemporaryDirectory, "net.json"}], NetModel["MobileNet V2 Trained on ImageNet Competition Data"], "MXNet"]
Out[26]=

Export also creates a net.params file containing parameters:

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

Get the size of the parameter file:

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

Requirements

Wolfram Language 12.0 (April 2019) or above

Resource History

Reference