YOLO V8 Classify Trained on ImageNet Competition Data

Identify the main object in an image

YOLO (You Only Look Once) Version 8 by Ultralytics is the latest version of the YOLO models. Just like its predecessor, YOLO Version 5, YOLO Version 8 is an anchor-free model that was trained with mosaic augmentation. It features the use of new "C2f" blocks, which employ additional dense connections between bottleneck modules. Although YOLO models are historically object detection models, in this case, their heads were adapted for image classification and the models were trained on ImageNet.

Training Set Information

Model Information

Examples

Resource retrieval

Get the pre-trained net:

In[1]:=
NetModel["YOLO V8 Classify Trained on ImageNet Competition Data"]
Out[2]=

NetModel parameters

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

In[3]:=
NetModel["YOLO V8 Classify Trained on ImageNet Competition Data", "ParametersInformation"]
Out[4]=

Pick a non-default net by specifying the parameters:

In[5]:=
NetModel[{"YOLO V8 Classify Trained on ImageNet Competition Data", "Size" -> "X"}]
Out[6]=

Pick a non-default uninitialized net:

In[7]:=
NetModel[{"YOLO V8 Classify Trained on ImageNet Competition Data", "Size" -> "M"}, "UninitializedEvaluationNet"]
Out[8]=

Basic usage

Classify an image:

In[9]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/fb779ef2-8026-41a2-844d-f8b54ad9eedd"]
In[10]:=
pred = NetModel[
   "YOLO V8 Classify Trained on ImageNet Competition Data"][testImage]
Out[11]=

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

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

Get a list of available properties of the predicted Entity:

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

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

In[14]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/d3f275ad-e60d-46e8-a15c-198390779830"]
Out[15]=

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

In[16]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/6c04f2a3-c69d-4bca-82f5-a2ad1063394f"]
Out[17]=

Obtain the list of names of all available classes:

In[18]:=
EntityValue[
 NetExtract[
   NetModel["YOLO V8 Classify Trained on ImageNet Competition Data"], "Output"][["Labels"]], "Name"]
Out[19]=

Feature extraction

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

In[20]:=
extractor = Take[NetModel[
   "YOLO V8 Classify Trained on ImageNet Competition Data"], {1, -3}]
Out[21]=

Get a set of images:

In[22]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/db2a9da7-ed6c-4683-9ddf-d085510abc30"]
In[23]:=

Visualize the features of a set of images:

In[24]:=
FeatureSpacePlot[imgs, FeatureExtractor -> extractor, LabelingSize -> 70, AspectRatio -> 1/2]
Out[24]=

Transfer learning

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

In[25]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/a22e63e1-bd66-490c-9516-27f0fd2fddbf"]
In[26]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/19d1f9a6-a833-4daa-9740-c822cf5c5a14"]

Remove the last classification layers from the pre-trained net:

In[27]:=
tempNet = Take[NetModel[
   "YOLO V8 Classify Trained on ImageNet Competition Data"], {1, -3}]
Out[28]=

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

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

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

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

Perfect accuracy is obtained on the test set:

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

Net information

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

In[32]:=
Information[
 NetModel[
  "YOLO V8 Classify Trained on ImageNet Competition Data"], "ArraysElementCounts"]
Out[33]=

Obtain the total number of parameters:

In[34]:=
Information[
 NetModel[
  "YOLO V8 Classify Trained on ImageNet Competition Data"], "ArraysTotalElementCount"]
Out[35]=

Obtain the layer type counts:

In[36]:=
Information[
 NetModel[
  "YOLO V8 Classify Trained on ImageNet Competition Data"], "LayerTypeCounts"]
Out[37]=

Display the summary graphic:

In[38]:=
Information[
 NetModel[
  "YOLO V8 Classify Trained on ImageNet Competition Data"], "SummaryGraphic"]
Out[39]=

Export to ONNX

Export the net to the ONNX format:

In[40]:=
onnxFile = Export[FileNameJoin[{$TemporaryDirectory, "net.onnx"}], NetModel["YOLO V8 Classify Trained on ImageNet Competition Data"]]
Out[41]=

Get the size of the ONNX file:

In[42]:=
FileByteCount[onnxFile]
Out[42]=

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

In[43]:=
NetModel["YOLO V8 Classify Trained on ImageNet Competition Data", "ByteCount"]
Out[44]=

Check some metadata of the ONNX model:

In[45]:=
{OpsetVersion, IRVersion} = {Import[onnxFile, "OperatorSetVersion"], Import[onnxFile, "IRVersion"]}
Out[45]=

Import the model back into Wolfram Language. However, the NetEncoder and NetDecoder will be absent because they are not supported by ONNX:

In[46]:=
Import[onnxFile]
Out[46]=

Resource History

Reference