SqueezeNet V1.1 Trained on ImageNet Competition Data

Identify the main object in an image

Released in 2016, SqueezeNet is a successful attempt to produce a high-performance image classification model using as few parameters as possible. Aiming for all of the benefits of a computationally cheap algorithm, it achieves good accuracy, while having very few parameters (~5 MB) compared to similar nets. This is achieved by making heavy use of bottleneck (squeeze) 1x1 convolutions. ImageNet classes are mapped to Wolfram Language Entities through their unique WordNet IDs.

Number of layers: 69 | Parameter count: 1,235,496 | Trained size: 5 MB |

Training Set Information

Performance

Examples

Resource retrieval

Get the pre-trained net:

In[1]:=
NetModel["SqueezeNet V1.1 Trained on ImageNet Competition Data"]
Out[1]=

Basic usage

Classify an image:

In[2]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/0bf855f0-8710-4304-8d76-2557ec82b1f9"]
Out[2]=

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

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

Get a list of available properties of the predicted Entity:

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

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/359b8848-24c1-406f-9844-d27dd612343d"]
Out[5]=

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/9cdd7d89-11da-4db5-99d9-93969e7b1c24"]
Out[6]=

Obtain the list of names of all available classes:

In[7]:=
EntityValue[
 NetExtract[
   NetModel["SqueezeNet V1.1 Trained on ImageNet Competition Data"], "Output"][["Labels"]], "Name"]
Out[7]=

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[
   "SqueezeNet V1.1 Trained on ImageNet Competition Data"], {1, -4}]
Out[8]=

Get a set of images:

In[9]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/45b60883-1a11-4e51-abe5-52eece372e7c"]

Visualize the features of a set of images:

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

Visualize convolutional weights

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

In[11]:=
weights = NetExtract[
   NetModel[
    "SqueezeNet V1.1 Trained on ImageNet Competition Data"], {"conv1",
     "Weights"}];

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

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

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/a6cc20ee-d8c4-461e-9b76-7c89800cdbf1"]
In[14]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/9ee577a4-73db-4383-b3fc-b360c40bd26d"]

Remove the linear layer from the pre-trained net:

In[15]:=
tempNet = Take[NetModel[
   "SqueezeNet V1.1 Trained on ImageNet Competition Data"], {1, -4}]
Out[15]=

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]=

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]=

Perfect accuracy is obtained on the test set:

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

Net information

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

In[19]:=
NetInformation[
 NetModel["SqueezeNet V1.1 Trained on ImageNet Competition Data"], \
"ArraysElementCounts"]
Out[19]=

Obtain the total number of parameters:

In[20]:=
NetInformation[
 NetModel["SqueezeNet V1.1 Trained on ImageNet Competition Data"], \
"ArraysTotalElementCount"]
Out[20]=

Obtain the layer type counts:

In[21]:=
NetInformation[
 NetModel["SqueezeNet V1.1 Trained on ImageNet Competition Data"], \
"LayerTypeCounts"]
Out[21]=

Display the summary graphic:

In[22]:=
NetInformation[
 NetModel["SqueezeNet V1.1 Trained on ImageNet Competition Data"], \
"SummaryGraphic"]
Out[22]=

Export to MXNet

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

In[23]:=
jsonPath = Export[FileNameJoin[{$TemporaryDirectory, "net.json"}], NetModel["SqueezeNet V1.1 Trained on ImageNet Competition Data"], "MXNet"]
Out[23]=

Export also creates a net.params file containing parameters:

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

Get the size of the parameter file:

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

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

In[26]:=
ResourceObject[
  "SqueezeNet V1.1 Trained on ImageNet Competition Data"]["ByteCount"]
Out[26]=

Represent the MXNet net as a graph:

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

Requirements

Wolfram Language 11.1 (March 2017) or above

Resource History

Reference