Wolfram Research

Squeeze-and-Excitation Net Trained on ImageNet Competition Data

Identify the main object in an image

Released in 2017, these models focus on exploring the influence of the channel encoding. The new Squeeze-and-Excitation (SE) blocks adaptively recalibrate the channel-wise feature responses by explicitly modeling interdependencies between channels. These blocks are added to the existing state-of-the-art deep architectures to improve their performance.

Training Set Information

Performance

Examples

Resource retrieval

Get the pre-trained net:

In[1]:=
NetModel["Squeeze-and-Excitation Net Trained on ImageNet Competition \
Data"]
Out[1]=

NetModel parameters

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

In[2]:=
NetModel["Squeeze-and-Excitation Net Trained on ImageNet Competition \
Data", "ParametersInformation"]
Out[2]=

Pick a non-default net by specifying the parameters:

In[3]:=
NetModel[{"Squeeze-and-Excitation Net Trained on ImageNet Competition \
Data", "Type" -> "ResNeXt-101"}]
Out[3]=

Pick a non-default uninitialized net:

In[4]:=
NetModel[{"Squeeze-and-Excitation Net Trained on ImageNet Competition \
Data", "Type" -> "ResNeXt-101"}, "UninitializedEvaluationNet"]
Out[4]=

Basic usage

Classify an image:

In[5]:=
CloudGet["https://www.wolframcloud.com/objects/23dc65dd-d946-4b69-b972-a78cf8c3a935"] (* Evaluate this cell to copy the example input from a cloud object *)
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]:=
CloudGet["https://www.wolframcloud.com/objects/332306db-8643-48bc-a7f9-9829dde17ff9"] (* Evaluate this cell to copy the example input from a cloud object *)
Out[8]=

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

In[9]:=
CloudGet["https://www.wolframcloud.com/objects/0ee398f5-a1af-4efa-a2c8-3ef6c6e9c2b1"] (* Evaluate this cell to copy the example input from a cloud object *)
Out[9]=

Obtain the list of names of all available classes:

In[10]:=
EntityValue[
 NetExtract[
   NetModel[
    "Squeeze-and-Excitation Net Trained on ImageNet Competition \
Data"], "Output"][["Labels"]], "Name"]
Out[10]=

Feature extraction

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

In[11]:=
extractor = 
 Take[NetModel[
   "Squeeze-and-Excitation Net Trained on ImageNet Competition \
Data"], {1, -3}]
Out[11]=

Get a set of images:

In[12]:=
CloudGet["https://www.wolframcloud.com/objects/6ad72a1c-21dc-4bf9-8876-5111c92e5e2f"] (* Evaluate this cell to copy the example input from a cloud object *)

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[
    "Squeeze-and-Excitation Net Trained on ImageNet Competition \
Data"], {"conv1_1_3x3_s2", "Weights"}];

Show the dimensions of the weights:

In[15]:=
Dimensions[weights]
Out[15]=

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

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

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[17]:=
CloudGet["https://www.wolframcloud.com/objects/222ff1c7-e332-446e-94d4-c509f58c9268"] (* Evaluate this cell to copy the example input from a cloud object *)
In[18]:=
CloudGet["https://www.wolframcloud.com/objects/de2faf3a-bd22-4c2c-9f4f-2a4bf5493c67"] (* Evaluate this cell to copy the example input from a cloud object *)

Remove the linear layer from the pre-trained net:

In[19]:=
tempNet = 
 Take[NetModel[
   "Squeeze-and-Excitation Net Trained on ImageNet Competition \
Data"], {1, -3}]
Out[19]=

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

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

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

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

Perfect accuracy is obtained on the test set:

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

Net information

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

In[23]:=
NetInformation[
 NetModel["Squeeze-and-Excitation Net Trained on ImageNet Competition \
Data"], "ArraysElementCounts"]
Out[23]=

Obtain the total number of parameters:

In[24]:=
NetInformation[
 NetModel["Squeeze-and-Excitation Net Trained on ImageNet Competition \
Data"], "ArraysTotalElementCount"]
Out[24]=

Obtain the layer type counts:

In[25]:=
NetInformation[
 NetModel["Squeeze-and-Excitation Net Trained on ImageNet Competition \
Data"], "LayerTypeCounts"]
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["Squeeze-and-Excitation Net 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