---
title: "TrainImageContentDetector"
language: "en"
type: "Symbol"
summary: "TrainImageContentDetector[{img1 -> {bbox1 -> class1, ...}, ...}] trains a ContentDetectorFunction[...] based on the examples given."
keywords: 
- object detection
- object detector
- image cases
- image content
- object recognition
- object classification
- computer vision
- YOLO
- SSD
canonical_url: "https://reference.wolfram.com/language/ref/TrainImageContentDetector.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Supervised Machine Learning"
    link: "https://reference.wolfram.com/language/guide/SupervisedMachineLearning.en.md"
  - 
    title: "Computer Vision"
    link: "https://reference.wolfram.com/language/guide/ComputerVision.en.md"
related_functions: 
  - 
    title: "ContentDetectorFunction"
    link: "https://reference.wolfram.com/language/ref/ContentDetectorFunction.en.md"
  - 
    title: "TrainTextContentDetector"
    link: "https://reference.wolfram.com/language/ref/TrainTextContentDetector.en.md"
  - 
    title: "ImageCases"
    link: "https://reference.wolfram.com/language/ref/ImageCases.en.md"
  - 
    title: "Classify"
    link: "https://reference.wolfram.com/language/ref/Classify.en.md"
  - 
    title: "NetTrain"
    link: "https://reference.wolfram.com/language/ref/NetTrain.en.md"
  - 
    title: "FindFaces"
    link: "https://reference.wolfram.com/language/ref/FindFaces.en.md"
  - 
    title: "FindImageShapes"
    link: "https://reference.wolfram.com/language/ref/FindImageShapes.en.md"
  - 
    title: "ImageIdentify"
    link: "https://reference.wolfram.com/language/ref/ImageIdentify.en.md"
---
[EXPERIMENTAL]

# TrainImageContentDetector

TrainImageContentDetector[{img1 -> {bbox1 -> class1, …}, …}] trains a ContentDetectorFunction[…] based on the examples given.

## Details and Options

* ``TrainImageContentDetector`` attempts to train an object detector using the provided training data.

* Object detection is a computer vision technique used for recognizing subimages in an image.

[image]

* ``TrainImageContentDetector`` returns a ``ContentDetectorFunction[…]`` that can then be applied to new input data.

* The bounding boxes ``bboxi`` should be given as ``Rectangle`` objects.

* The classes ``classi`` can be arbitrary expressions.

* ``TrainImageContentDetector[…][image]`` can be used to detect objects in ``image`` and returns the result as a list of associations of the form ``<|"Image" -> img, "Class" -> class|>``.

* ``TrainImageContentDetector`` supports the following options:

|                    |           |                                                                |
| ------------------ | --------- | -------------------------------------------------------------- |
| PerformanceGoal    | Automatic | favor settings with specific advantages                        |
| ProgressReporting  | Automatic | how to report progress during training                         |
| RandomSeeding      | 1234      | how to seed pseudorandom generators internally                 |
| TargetDevice       | "CPU"     | the target device on which to perform training                 |
| TimeGoal           | Automatic | number of seconds to train for                                 |
| ValidationSet      | None      | the set of data on which to evaluate the model during training |

---

## Examples (6)

### Basic Examples (1)

Train a basic object detector:

```wl
In[1]:= df = TrainImageContentDetector[{[image] -> {Rectangle[{89, 26}, {317, 217}] -> "heart"}, [image] -> {Rectangle[{228, 99}, {289, 158}] -> "heart"}, [image] -> {Rectangle[{86, 133}, {176, 207}] -> "heart", Rectangle[{170, 83}, {225, 130}] -> "heart"}, [image] -> {Rectangle[{147, 86}, {239, 186}] -> "heart"}}]

Out[1]= ContentDetectorFunction[<|Classes -> {heart}, GridSize -> {13, 13}, Anchors -> {{0.0440562, 0.0521065}, {0.144189, 0.158656}, {0.256802, 0.421103}, {0.606371, 0.271368}, {0.751578, 0.705252}}, Net -> NetChain[<2>], DataRange -> Full, Fitting -> Fit, Version -> 1., <<2>>, Function -> NeuralFunctions`Private`file19YOLO`YOLOEvaluator, InputType -> Image, Architecture -> YOLO|>]
```

Apply the detector on a new image:

```wl
In[2]:=
testImage = [image];
df[testImage]

Out[2]= {<|"Image" -> [image], "Class" -> "heart"|>, <|"Image" -> [image], "Class" -> "heart"|>, <|"Image" -> [image], "Class" -> "heart"|>}
```

Highlight the detection on the input image:

```wl
In[3]:= HighlightImage[testImage, Legended[#BoundingBox, #Class]& /@ df[testImage, {"BoundingBox", "Class"}]]

Out[3]= [image]
```

### Options (5)

#### PerformanceGoal (1)

Use ``PerformanceGoal -> "Quality"`` to emphasize the quality of the result:

```wl
In[1]:=
training = {[image] -> {Rectangle[{48, 25}, {160, 144}] -> "apple", Rectangle[{144, 24}, {279, 144}] -> "apple"}, [image] -> {Rectangle[{84, 43}, {227, 155}] -> "strawberry"}, [image] -> {Rectangle[{60, 70}, {180, 212}] -> "strawberry"}};
TrainImageContentDetector[training, PerformanceGoal -> "Quality"]

During evaluation of In[1]:= [image]
```

Use ``PerformanceGoal -> "Speed"`` to emphasize the speed of computation:

```wl
In[2]:= TrainImageContentDetector[training, PerformanceGoal -> "Speed"]

During evaluation of In[3]:= [image]
```

#### ProgressReporting (1)

By default, progress is reported in a dynamic panel:

```wl
In[1]:= training = {[image] -> {Rectangle[{48, 25}, {160, 144}] -> "apple", Rectangle[{144, 24}, {279, 144}] -> "apple"}, [image] -> {Rectangle[{84, 43}, {227, 155}] -> "strawberry"}, [image] -> {Rectangle[{60, 70}, {180, 212}] -> "strawberry"}};

In[2]:= TrainImageContentDetector[training]

During evaluation of In[2]:= [image]

During evaluation of In[2]:= [image]
```

Use ``ProgressReporting -> False`` to avoid displaying the progress panel:

```wl
In[3]:= TrainImageContentDetector[training, ProgressReporting -> False]
```

#### TargetDevice (1)

Train a detector using the default system GPU, if available:

```wl
In[1]:=
training = {[image] -> {Rectangle[{48, 25}, {160, 144}] -> "apple", Rectangle[{144, 24}, {279, 144}] -> "apple"}, [image] -> {Rectangle[{84, 43}, {227, 155}] -> "strawberry"}, [image] -> {Rectangle[{60, 70}, {180, 212}] -> "strawberry"}};
TrainImageContentDetector[training, TargetDevice -> "GPU"]

Out[1]= ContentDetectorFunction[<|Classes -> {apple, strawberry}, GridSize -> {13, 13}, Anchors -> {{0.0440562, 0.0521065}, {0.144189, 0.158656}, {0.256802, 0.421103}, {0.606371, 0.271368}, {0.751578, 0.705252}}, Net -> NetChain[<2>], DataRange -> Full, Fitting -> Fit, <<3>>, Function -> NeuralFunctions`Private`file19YOLO`YOLOEvaluator, InputType -> Image, Architecture -> YOLO|>]
```

If a compatible GPU is not available, a message is issued:

```wl
In[2]:= TrainImageContentDetector[training, TargetDevice -> "GPU"]
```

TrainImageContentDetector::trgdevmac: TargetDevice -> "GPU" is not supported on Mac OS platforms.

```wl
Out[2]= [image]
```

#### TimeGoal (1)

The training time can be influenced by several factors, such as the number of examples and classes:

```wl
In[1]:= training = {[image] -> {Rectangle[{48, 25}, {160, 144}] -> "apple", Rectangle[{144, 24}, {279, 144}] -> "apple"}, [image] -> {Rectangle[{84, 43}, {227, 155}] -> "strawberry"}, [image] -> {Rectangle[{60, 70}, {180, 212}] -> "strawberry"}};

In[2]:= TrainImageContentDetector[training]

During evaluation of In[2]:= [image]
```

Use ``TimeGoal`` to specify a target time for the training:

```wl
In[3]:= TrainImageContentDetector[training, TimeGoal -> Quantity[10, "Minutes"]]

During evaluation of In[4]:= [image]
```

#### ValidationSet (1)

By default, only cross-validation is performed on the detector:

```wl
In[1]:= training = {[image] -> {Rectangle[{48, 25}, {160, 144}] -> "apple", Rectangle[{144, 24}, {279, 144}] -> "apple"}, [image] -> {Rectangle[{84, 43}, {227, 155}] -> "strawberry"}, [image] -> {Rectangle[{60, 70}, {180, 212}] -> "strawberry"}};

In[2]:= TrainImageContentDetector[training]

Out[2]= ContentDetectorFunction[<|Classes -> {apple, strawberry}, GridSize -> {13, 13}, Anchors -> {{0.0440562, 0.0521065}, {0.144189, 0.158656}, {0.256802, 0.421103}, {0.606371, 0.271368}, {0.751578, 0.705252}}, Net -> NetChain[<2>], DataRange -> Full, Fitting -> Fit, <<3>>, Function -> NeuralFunctions`Private`file19YOLO`YOLOEvaluator, InputType -> Image, Architecture -> YOLO|>]
```

Use ``ValidationSet`` to provide separate validation examples:

```wl
In[3]:= validation = {[image] -> {Rectangle[{50, 74}, {139, 177}] -> "strawberry"}, [image] -> {Rectangle[{82, 44}, {132, 94}] -> "apple", Rectangle[{102, 94}, {159, 144}] -> "apple", Rectangle[{115, 8}, {170, 62}] -> "apple"}};

In[4]:= TrainImageContentDetector[training, ValidationSet -> validation]

Out[4]= ContentDetectorFunction[<|Classes -> {apple, strawberry}, GridSize -> {13, 13}, Anchors -> {{0.0440562, 0.0521065}, {0.144189, 0.158656}, {0.256802, 0.421103}, {0.606371, 0.271368}, {0.751578, 0.705252}}, Net -> NetChain[<2>], DataRange -> Full, Fitting -> Fit, <<3>>, Function -> NeuralFunctions`Private`file19YOLO`YOLOEvaluator, InputType -> Image, Architecture -> YOLO|>]
```

## See Also

* [`ContentDetectorFunction`](https://reference.wolfram.com/language/ref/ContentDetectorFunction.en.md)
* [`TrainTextContentDetector`](https://reference.wolfram.com/language/ref/TrainTextContentDetector.en.md)
* [`ImageCases`](https://reference.wolfram.com/language/ref/ImageCases.en.md)
* [`Classify`](https://reference.wolfram.com/language/ref/Classify.en.md)
* [`NetTrain`](https://reference.wolfram.com/language/ref/NetTrain.en.md)
* [`FindFaces`](https://reference.wolfram.com/language/ref/FindFaces.en.md)
* [`FindImageShapes`](https://reference.wolfram.com/language/ref/FindImageShapes.en.md)
* [`ImageIdentify`](https://reference.wolfram.com/language/ref/ImageIdentify.en.md)

## Related Guides

* [Supervised Machine Learning](https://reference.wolfram.com/language/guide/SupervisedMachineLearning.en.md)
* [Computer Vision](https://reference.wolfram.com/language/guide/ComputerVision.en.md)

## History

* [Introduced in 2021 (13.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn130.en.md)