MMCV: OpenMMLab Computer Vision Foundation
MMCV (OpenMMLab Computer Vision Foundation) is a foundational library for computer vision research and development, providing a comprehensive set of building blocks including rich data augmentation and transformation operations, various deep learning utilities, and support for hardware-specific optimizations. It is a core component of the OpenMMLab ecosystem. The current version is 2.2.0, with frequent releases to add features, bug fixes, and support for new PyTorch versions and hardware platforms.
Common errors
-
ModuleNotFoundError: No module named 'mmcv.runner'
cause Attempting to import training-related modules (like Runner) from MMCV in versions >= 2.0.0, where they have been moved to the MMEngine library.fixChange the import statement to `from mmengine.runner import Runner`. Ensure `mmengine` is installed. -
RuntimeError: CUDA error: no kernel image is available for execution on the device
cause This typically means `mmcv-full` was installed incorrectly, or the installed `mmcv-full` version's CUDA extensions do not match your current PyTorch or NVIDIA driver/CUDA toolkit environment.fixUninstall any existing `mmcv` and `mmcv-full`. Install `openmim` (`pip install -U openmim`) and then use `mim install mmcv-full` after carefully checking PyTorch and CUDA compatibility in the official documentation. -
ImportError: cannot import name 'build_from_cfg' from 'mmcv.cnn'
cause The utility function `build_from_cfg` was moved to a different module path in MMCV v2.x.fixUpdate the import statement to `from mmcv.registry import build_from_cfg`.
Warnings
- breaking MMCV v2.0.0 introduced significant breaking changes by moving training-related modules (like `Runner`, `Hook`, `Optimizer`) to a new library, `MMEngine`. Direct imports from `mmcv.runner` or similar paths will fail.
- gotcha Installing `mmcv-full` (the version with CUDA/hardware extensions) via `pip install mmcv-full` can be problematic and may result in a CPU-only build or version mismatches. The `mim` tool is highly recommended for managing `mmcv-full` installations.
- gotcha MMCV-full has strict compatibility requirements with PyTorch and CUDA versions. Mismatched versions can lead to runtime errors (e.g., CUDA kernel errors) or unexpected behavior.
- breaking The paths for data transformation pipelines and utilities (e.g., `Compose`) changed in v2.x. Old imports like `from mmcv.datasets.pipelines import Compose` are no longer correct.
Install
-
pip install -U openmim mim install mmcv-full -
pip install mmcv
Imports
- Config
from mmcv import Config
- Runner
from mmcv.runner import Runner
from mmengine.runner import Runner
- build_from_cfg
from mmcv.cnn import build_from_cfg
from mmcv.registry import build_from_cfg
- Compose
from mmcv.datasets.pipelines import Compose
from mmcv.transforms import Compose
Quickstart
from mmcv import Config
from mmcv.transforms import Compose, Resize, RandomFlip
import numpy as np
# 1. Load a config (can be a dummy dict or a path to a .py config file)
cfg_dict = dict(
model=dict(
type='ResNet',
depth=50,
num_classes=1000
),
data=dict(
train_pipeline=[
dict(type='Resize', scale=(224, 224)),
dict(type='RandomFlip', prob=0.5)
]
)
)
cfg = Config(cfg_dict)
print(f"Loaded config model type: {cfg.model.type}")
# 2. Use data transforms
pipeline = Compose([
Resize(scale=(256, 256)),
RandomFlip(prob=0.5)
])
# Dummy image data
dummy_img = np.random.rand(512, 512, 3).astype(np.uint8)
data_sample = dict(img=dummy_img, img_shape=dummy_img.shape[:2], ori_shape=dummy_img.shape[:2])
transformed_data = pipeline(data_sample)
print(f"Original image shape: {data_sample['img'].shape}")
print(f"Transformed image shape: {transformed_data['img'].shape}")