Wolfram Research

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.

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/0e0b4462-106f-4fa8-b5cb-8a8bc44c4f0d"]
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/97c098a7-132e-4285-b760-b2e31d50e25a"]
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/804319fc-d629-4ff7-b212-2c336ca9ec1a"]
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/fa84a171-e0e6-4a52-86bc-22ce9bbb1660"]

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/35a0180f-dc7a-48d6-92e0-0dbc121ef3d2"]
In[17]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/5654e9ba-c62e-41d0-b79f-7cece61830cb"]

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