Main Content

Getting Started with Object Detection Using Deep Learning

Object detection using deep learning provides a fast and accurate means to predict the location of an object in an image. Deep learning is a powerful machine learning technique in which the object detector automatically learns image features required for detection tasks. Several techniques for object detection using deep learning are available such as Faster R-CNN, you only look once (YOLO) v2, YOLO v3, YOLO v4, and single shot detection (SSD).

Applications for object detection include:

  • Image classification

  • Scene understanding

  • Self-driving vehicles

  • Surveillance

Create Training Data for Object Detection

Use a labeling app to interactively label ground truth data in a video, image sequence, image collection, or custom data source. You can label object detection ground truth using rectangle labels, which define the position and size of the object in the image.

Augment and Preprocess Data

Using data augmentation provides a way to use limited data sets for training. Minor changes, such as translation, cropping, or transforming an image, provide, new, distinct, and unique images that you can use to train a robust detector. Datastores are a convenient way to read and augment collections of data. Use imageDatastore and the boxLabelDatastore to create datastores for images and labeled bounding box data.

For more information about augmenting training data using datastores, see Datastores for Deep Learning (Deep Learning Toolbox), and Perform Additional Image Processing Operations Using Built-In Datastores (Deep Learning Toolbox).

Create Object Detection Network

Each object detector contains a unique network architecture. For example, the Faster R-CNN detector uses a two-stage network for detection, whereas the YOLO v2 detector uses a single stage. Use functions like fasterRCNNLayers or yolov2Layers to create a network. You can also design a network layer by layer using the Deep Network Designer (Deep Learning Toolbox).

Train Detector and Evaluate Results

Use the trainFasterRCNNObjectDetector, trainYOLOv2ObjectDetector, trainYOLOv4ObjectDetector, and trainSSDObjectDetector functions to train an object detector. Use the evaluateDetectionMissRate and evaluateDetectionPrecision functions to evaluate the training results.

Detect Objects Using Deep Learning Detectors

Detect objects in an image using the trained detector. For example, the partial code shown below uses the trained detector on an image I. Use the detect object function on fasterRCNNObjectDetector, yolov2ObjectDetector, yolov3ObjectDetector, yolov4ObjectDetector, or ssdObjectDetector objects to return bounding boxes, detection scores, and categorical labels assigned to the bounding boxes.

I = imread(input_image)
[bboxes,scores,labels] = detect(detector,I)

Detect Objects Using Pretrained Object Detection Models

Computer Vision Toolbox™ provides pretrained object detection models to use for performing out-of-the-box inference.

To use a pretrained object detection model

  1. Download a pretrained model. The pretrained models are shipped as support packages that you can download and install using either the visionSupportPackages function or the Add-On Explorer.

  2. Use an object detector in the Computer Vision Toolbox to load the pretrained model and detect objects in a test image.

The table lists the names of the pretrained models, names of the corresponding support packages, and the object detectors in Computer Vision Toolbox.

Name of Pretrained ModelsDescriptionName of Support PackageObject Detector
resnet50-coco A pretrained Mask R-CNN object detector trained on the COCO data set with a ResNet-50 network as the feature extractor.Computer Vision Toolbox Model for Mask R-CNN Instance Segmentationmaskrcnn
darknet19-cocoA pretrained YOLO v2 deep learning network created using DarkNet-19 as the base network and trained on COCO dataset.Computer Vision Toolbox Model for YOLO v2 Object Detectionyolov2ObjectDetector
tiny-yolov2-cocoA pretrained YOLO v2 deep learning network created using a small base network and trained on COCO dataset.
darknet53-cocoA pretrained YOLO v3 deep learning network created using DarkNet-53 as the base network and trained on COCO dataset.Computer Vision Toolbox Model for YOLO v3 Object Detectionyolov3ObjectDetector
tiny-yolov3-cocoA pretrained YOLO v3 deep learning network created using a small base network and trained on COCO dataset.
csp-darknet53-cocoA pretrained YOLO v4 deep learning network created using CSP-DarkNet-53 as the base network and trained on COCO dataset.Computer Vision Toolbox Model for YOLO v4 Object Detectionyolov4ObjectDetector
tiny-yolov4-cocoA pretrained YOLO v4 deep learning network created using a small base network and trained on COCO dataset.

For example, to use the darknet19-coco pretrained YOLO v2 model for object detection, load the model by using the yolov2ObjectDetector object. You can then use the detect function of yolov2ObjectDetector object to detect objects in an unknown image.

detector = yolov2ObjectDetector("darknet19-coco");
testImage = imread("highway.png");
[bboxes,scores,labels] = detect(detector,testImage);

MathWorks GitHub

MathWorks® GitHub repository provides implementations of the latest pretrained object detection deep learning networks to download and use for performing out-of-the-box inference. The pretrained object detection networks are already trained on standard data sets such as the COCO and Pascal VOC data sets. You can use these pretrained models directly to detect different objects in a test image.

For a list of all the latest MathWorks pretrained object detectors, see MATLAB Deep Learning (GitHub).

See Also

Apps

Related Topics