How to train an object detector on your own data

Your classes, your images, one command. Here is the whole path from a folder of photos to a model you can ship.

8 min read

1. Collect images

Use photos from the place the model will work: the same camera, angle, light and background. A few hundred images per class is a good start; more variety beats more of the same. Include some images with none of your objects, so the model learns what to ignore.

2. Label them

Draw a box around every object of every class you want to detect, in every image. Missing a box teaches the model that the object is background. See how to label images for the details that matter.

3. Write the dataset file

fotonet reads a small YAML file that says where the images are and what the classes are called. Labels use the common YOLO text format: one .txt file per image in a parallel labels/ folder, one line per box: class_id x_center y_center width height, all from 0 to 1.

# data.yaml
path: datasets/parts
train: images/train
val: images/val
nc: 3
names:
  0: bolt
  1: nut
  2: washer

Keep about one image in ten for validation, and never train on it.

4. Train

fotonet train model=fotonetn data=data.yaml epochs=100 imgsz=640

By default training starts from fotonet's released COCO weights, which is what you want for your own data: the model already knows edges, shapes and objects, and only has to learn yours. Stop whenever you like and continue later with resume=runs/.../fotonetn_last.pt.

5. Check the result

fotonet val model=runs/fotonetn/fotonetn_best.pt data=data.yaml

Look at mAP, precision and recall on your validation images, then look at the predictions themselves. Mistakes usually point to missing or inconsistent labels. See mAP explained.

6. Export and ship

fotonet export model=runs/fotonetn/fotonetn_best.pt format=onnx

See exporting a detector for TensorRT, CoreML and the rest.

fotonet Studio, our desktop app, will do all of this in one window, from labeling to export. Coming soon.