MMDetection: OpenMMLab Detection Toolbox and Benchmark

3.3.0 · active · verified Thu Apr 16

MMDetection is an open-source object detection toolbox based on PyTorch, part of the OpenMMLab project. It provides a comprehensive collection of detection, instance segmentation, and object grounding algorithms, along with a benchmark for various computer vision tasks. Currently at version 3.3.0, it features frequent updates, including state-of-the-art models like Grounding DINO and various Transformer-based detectors, and integrates closely with other OpenMMLab libraries like MMEngine and MMCV.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to perform inference using MMDetection 3.x's `DetInferencer` class. It downloads a pre-trained `rtmdet_tiny` model, runs inference on a dummy image, prints the detected objects, and saves the visualization to a specified output directory.

import os
import torch
from mmdet.apis import DetInferencer
import mmcv # For image loading if not using DetInferencer's internal loading

# Ensure an image file exists for the demo
# For simplicity, create a dummy image or download one
img_path = 'demo_image.jpg'
if not os.path.exists(img_path):
    # Using a common utility to create a simple dummy image
    from PIL import Image
    img = Image.new('RGB', (640, 480), color = 'red')
    img.save(img_path)
    print(f"Created dummy image: {img_path}")

# Initialize the DetInferencer with a pre-trained model.
# The model weights will be automatically downloaded.
# Using device='cpu' for broader compatibility.
# rtmdet_tiny is a lightweight model for quick demo.
inferencer = DetInferencer(model='rtmdet_tiny_8xb32-300e_coco', device='cpu')

# Perform inference on the image
# The result contains detections, bounding boxes, labels, and scores.
result = inferencer(img_path, show=False) # show=False to prevent immediate display

# Print detected objects (example for the first image if batched)
if isinstance(result['predictions'], list) and len(result['predictions']) > 0:
    first_image_predictions = result['predictions'][0]
    print(f"Detected objects in {img_path}:")
    for box, label, score in zip(first_image_predictions['bboxes'], first_image_predictions['labels'], first_image_predictions['scores']):
        print(f"  Label: {label}, Score: {score:.2f}, BBox: {list(map(int, box))}")

# To save the visualization, specify an output directory
output_dir = 'mmdet_output'
os.makedirs(output_dir, exist_ok=True)
inferencer(img_path, out_dir=output_dir)
print(f"Inference results saved to {output_dir}")

# Clean up the dummy image and output directory (optional)
os.remove(img_path)
# For a full cleanup, you might also remove output_dir recursively
# import shutil; shutil.rmtree(output_dir)

view raw JSON →