Wide ResNet-50-2 Trained on ImageNet Competition Data

Identify the main object in an image

Released in 2017 by Sergey Zagoruyko and Nikos Komodakis, this model provides improvement on existing residual networks. By decreasing the depth of the architecture and increasing the width of the network, state-of-the-art accuracies and much faster training were achieved. ImageNet classes are mapped to Wolfram Language Entities through their unique WordNet IDs.

Number of layers: 188 | Parameter count: 68,951,464 | Trained size: 277 MB |

Training Set Information

Performance

Examples

Resource retrieval

Get the pre-trained net:

In[1]:=
NetModel["Wide ResNet-50-2 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/5e9b34fa-ac6d-4660-8c65-5b420be1f03a"]
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/330fcc3f-7b49-472a-81b3-963a25de3ac0"]
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/466f967f-6f6f-44ea-927a-3ca9ebd65002"]
Out[6]=

Obtain the list of names of all available classes:

In[7]:=
EntityValue[
 NetExtract[
   NetModel["Wide ResNet-50-2 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[
   "Wide ResNet-50-2 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/dae097ce-a2a1-4910-b7e2-a552897a0378"]

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[
    "Wide ResNet-50-2 Trained on ImageNet Competition Data"], {1, "Weights"}];

Show the dimensions of the weights:

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

Visualize the weights as a list of 64 images of size 7x7:

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

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[14]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/3d67e1d3-ccab-4dac-97ab-41ea40d9fef0"]
In[15]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/5aa44ec3-cb88-43a8-b5a3-b8a002b61486"]

Remove the linear layer from the pre-trained net:

In[16]:=
tempNet = NetTake[NetModel[
   "Wide ResNet-50-2 Trained on ImageNet Competition Data"], {1, -4}]
Out[16]=

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

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

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

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

Perfect accuracy is obtained on the test set:

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

Net information

Inspect the sizes of all arrays in the net:

In[20]:=
NetInformation[
 NetModel["Wide ResNet-50-2 Trained on ImageNet Competition Data"], \
"ArraysElementCounts"]
Out[20]=

Obtain the total number of parameters:

In[21]:=
NetInformation[
 NetModel["Wide ResNet-50-2 Trained on ImageNet Competition Data"], \
"ArraysTotalElementCount"]
Out[21]=

Obtain the layer type counts:

In[22]:=
NetInformation[
 NetModel["Wide ResNet-50-2 Trained on ImageNet Competition Data"], \
"LayerTypeCounts"]
Out[22]=

Display the summary graphic:

In[23]:=
NetInformation[
 NetModel["Wide ResNet-50-2 Trained on ImageNet Competition Data"], \
"FullSummaryGraphic"]
Out[23]=

Export to MXNet

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

In[24]:=
jsonPath = Export[FileNameJoin[{$TemporaryDirectory, "net.json"}], NetModel["Wide ResNet-50-2 Trained on ImageNet Competition Data"], "MXNet"]
Out[24]=

Export also creates a net.params file containing parameters:

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

Get the size of the parameter file:

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

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

In[27]:=
ResourceObject[
  "Wide ResNet-50-2 Trained on ImageNet Competition \
Data"]["ByteCount"]
Out[27]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

Reference