MMDetection: OpenMMLab Detection Toolbox and Benchmark
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
-
ModuleNotFoundError: No module named 'mmcv._ext' OR AssertionError: MMCV==xxx is used but incompatible. Please install mmcv>=xxx, <=xxx.
cause Incorrect or incompatible installation of MMCV. In MMDetection 2.x, it was `mmcv-full`, but for 3.x, it's `mmcv` (or `mmcv-lite` for CPU-only). Version mismatches are common.fixUninstall any existing `mmcv` or `mmcv-full`: `pip uninstall mmcv mmcv-full`. Then, use `mim install mmcv>=2.0.0` (for MMDetection 3.x) or consult the specific compatibility table for your MMDetection version. -
train.py: error: the following arguments are required: config
cause When running training or testing scripts (e.g., `tools/train.py`), the configuration file specifying the model, dataset, and training parameters was not provided.fixAlways provide the path to a valid configuration file. Example: `python tools/train.py configs/rtmdet/rtmdet_s_8xb32-300e_coco.py`. -
ImportError: cannot import name 'init_detector' from 'mmdet.apis'
cause This usually indicates trying to use an MMDetection 2.x API (`init_detector`, `inference_detector`) in a MMDetection 3.x environment, or general issues with `mmdet` installation.fixFor MMDetection 3.x, use `from mmdet.apis import DetInferencer` for high-level inference. If it's a general import error, ensure MMDetection is correctly installed (`pip install -e .` from source or `mim install mmdet`). -
GPU out of memory (OOM)
cause Training or inference with high-resolution images, large batch sizes, or complex models exceeds the available GPU memory.fixReduce batch size, decrease image resolution, use gradient accumulation, or enable automatic mixed precision (AMP) training if your model and PyTorch version support it. Check `gpu_assign_thr` or `find_unused_parameters=True` in config for specific scenarios.
Warnings
- breaking MMDetection 3.x introduced significant breaking changes compared to 2.x, including a completely revamped config system, data pipelines, API, and a new dependency on MMEngine.
- deprecated Python 3.6 support has been deprecated since MMDetection 2.28.0. MMDetection 3.x officially requires Python 3.7 or newer.
- gotcha MMCV version compatibility is crucial. Mismatches between MMDetection, PyTorch, and MMCV versions (especially `mmcv-full` vs `mmcv` in v2.x) are a very common source of installation and runtime errors.
- gotcha Runtime errors like 'undefined symbol', 'DLL load failed', 'invalid device function', or 'no kernel image is available for execution' often stem from CUDA, PyTorch, and MMCV compilation mismatches.
Install
-
pip install openmim mim install 'mmcv>=2.0.0' # or 'mmcv-lite>=2.0.0' mim install mmdet -
pip install 'torch>=1.8.0,<2.3.0' 'torchvision>=0.9.0,<0.16.0' # Adjust for your CUDA version pip install -U openmim mim install 'mmengine>=0.7.1' mim install 'mmcv>=2.0.0' # or 'mmcv-lite>=2.0.0' # Clone and install mmdet from source for editable mode or latest features git clone https://github.com/open-mmlab/mmdetection.git cd mmdetection pip install -v -e .
Imports
- DetInferencer
from mmdet.apis import init_detector, inference_detector
from mmdet.apis import DetInferencer
- BaseDetector
from mmdet.models import BaseDetector
Quickstart
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)