EfficientNet Trained on ImageNet with AutoAugment

Identify the main object in an image

Released in 2019, this model utilizes the techniques of AutoAugment data augmentation on the EfficientNet architectures to effectively perform image classification.

Number of models: 8

Training Set Information

Performance

Examples

Resource retrieval

Get the pre-trained net:

In[1]:=
NetModel["EfficientNet Trained on ImageNet with AutoAugment"]
Out[1]=
Image

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["EfficientNet Trained on ImageNet with AutoAugment", \
"ParametersInformation"]
Out[2]=
Image

Pick a non-default net by specifying the parameters:

In[3]:=
NetModel[{"EfficientNet Trained on ImageNet with AutoAugment", "Architecture" -> "B5"}]
Out[3]=
Image

Pick a non-default uninitialized net:

In[4]:=
NetModel[{"EfficientNet Trained on ImageNet with AutoAugment", "Architecture" -> "B7"}, "UninitializedEvaluationNet"]
Out[4]=
Image

Basic usage

Classify an image:

In[5]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/753ee0bc-1c92-48fd-a932-5344450b1d35"]
Out[5]=
Image

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

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

Get a list of available properties of the predicted Entity:

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

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

In[8]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/c36e9527-1b86-478f-834a-6317d7c453f9"]
Out[8]=
Image

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/1b300312-5948-4048-99ac-3c6cd3d8a09e"]
Out[9]=
Image

Obtain the list of names of all available classes:

In[10]:=
EntityValue[
 NetExtract[
   NetModel["EfficientNet Trained on ImageNet with AutoAugment"], "Output"][["Labels"]], "Name"]
Out[10]=
Image

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[
   "EfficientNet Trained on ImageNet with AutoAugment"], {1, -3}]
Out[11]=
Image

Get a set of images:

In[12]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/f0b9acfe-bd56-40da-b455-12e2806bf6cd"]

Visualize the features of a set of images:

In[13]:=
FeatureSpacePlot[imgs, FeatureExtractor -> extractor, LabelingSize -> 200, ImageSize -> Full, AspectRatio -> 1/2]
Out[13]=
Image

Visualize convolutional weights

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

In[14]:=
NetModel["EfficientNet Trained on ImageNet with AutoAugment"]
Out[14]=
Image
In[15]:=
weights = NetExtract[
   NetModel[
    "EfficientNet Trained on ImageNet with AutoAugment"], \
{"stem_conv", "Weights"}];

Show the dimensions of the weights:

In[16]:=
Dimensions[weights]
Out[16]=
Image

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

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

Transfer learning

Use the pre-trained model to build a classifier for telling apart images of motorcycles and bicycles. Create a test set and a training set:

In[18]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/77638936-f3cb-4f6e-b58d-7faa086594e1"]
In[19]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/332638ab-acfd-43de-8955-17e26c8eb2cd"]

Remove the linear layer from the pre-trained net:

In[20]:=
tempNet = Take[NetModel[
   "EfficientNet Trained on ImageNet with AutoAugment"], {1, -3}]
Out[20]=
Image

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

In[21]:=
newNet = NetChain[<|"pretrainedNet" -> tempNet, "linearNew" -> LinearLayer[], "softmax" -> SoftmaxLayer[]|>, "Output" -> NetDecoder[{"Class", {"bicycle", "motorcycle"}}]]
Out[21]=
Image

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

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

Perfect accuracy is obtained on the test set:

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

Net information

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

In[24]:=
Information[
 NetModel["EfficientNet Trained on ImageNet with AutoAugment"], \
"ArraysElementCounts"]
Out[24]=
Image

Obtain the total number of parameters:

In[25]:=
Information[
 NetModel["EfficientNet Trained on ImageNet with AutoAugment"], \
"ArraysTotalElementCount"]
Out[25]=
Image

Obtain the layer type counts:

In[26]:=
Information[
 NetModel["EfficientNet Trained on ImageNet with AutoAugment"], \
"LayerTypeCounts"]
Out[26]=
Image

Export to MXNet

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

In[27]:=
jsonPath = Export[FileNameJoin[{$TemporaryDirectory, "net.json"}], NetModel["EfficientNet Trained on ImageNet with AutoAugment"], "MXNet"]
Out[27]=
Image

Export also creates a net.params file containing parameters:

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

Get the size of the parameter file:

In[29]:=
FileByteCount[paramPath]
Out[29]=
Image

Requirements

Wolfram Language 12.1 (March 2020) or above

Resource History

Reference