### Build mmcv Docker Image with PyTorch and CUDA Source: https://mmcv.readthedocs.io/en/latest/get_started/installation This example shows how to build a Docker image with specific versions of PyTorch and CUDA. You can adjust PYTORCH, CUDA, CUDNN, and MMCV variables to your needs. ```bash PYTORCH=1.11.0 CUDA=11.3 CUDNN=8 MMCV=2.0.0 ``` -------------------------------- ### Install MMCV with mim (Recommended) Source: https://mmcv.readthedocs.io/en/latest/get_started/installation This command uses the 'mim' package manager to install MMCV. It's the recommended method and often utilizes pre-built packages for faster installation. ```bash mim install mmcv ``` -------------------------------- ### Install mmcv-lite Source: https://mmcv.readthedocs.io/en/latest/get_started/installation This command installs the mmcv-lite package using pip. Ensure PyTorch is successfully installed in your environment before running this command. ```bash pip install mmcv-lite ``` -------------------------------- ### Install mmcv-full on MLU Source: https://mmcv.readthedocs.io/en/latest/get_started/build These commands are used to build and install the mmcv-full package with MLU support. It involves setting build-related environment variables and running the Python setup script. ```Shell cd export MMCV_WITH_OPS=1 export FORCE_MLU=1 python setup.py install ``` -------------------------------- ### Build MMCV from source Source: https://mmcv.readthedocs.io/en/latest/index Guides on building MMCV from its source code. This is often necessary for custom installations, specific hardware support (like MLU devices), or when contributing to the project. It involves cloning the repository and running build commands. ```bash git clone https://github.com/open-mmlab/mmcv.git cd mmcv pip install -e . ``` -------------------------------- ### Install mmcv-lite Source: https://mmcv.readthedocs.io/en/latest/index Instructions for installing the 'lite' version of MMCV, which is a smaller, dependency-light version suitable for environments where full MMCV features are not required. Installation is also done via pip. ```bash pip install mmcv-lite ``` -------------------------------- ### Install Specific MMCV Version with mim Source: https://mmcv.readthedocs.io/en/latest/get_started/installation This command installs a specific version of MMCV (e.g., 2.0.0) using 'mim'. This is useful when a particular version is required. ```bash mim install mmcv==2.0.0 ``` -------------------------------- ### Install mmcv Source: https://mmcv.readthedocs.io/en/latest/index Instructions for installing the MMCV library. This typically involves using pip, a package installer for Python. Ensure you have a compatible Python version and necessary build tools if installing from source. ```bash pip install mmcv ``` -------------------------------- ### Verify PyTorch Installation Source: https://mmcv.readthedocs.io/en/latest/get_started/installation This code snippet checks if PyTorch is successfully installed in the current environment by printing its version. It's a prerequisite before installing MMCV. ```python import torch;print(torch.__version__) ``` -------------------------------- ### Install Optional Dependencies Source: https://mmcv.readthedocs.io/en/latest/get_started/build Installs optional Python packages required for speeding up MMCV compilation, such as ninja and psutil. ```bash (mmcv) PS C:\Users\xxx\mmcv> pip install -r requirements/optional.txt ``` -------------------------------- ### KeyMapper Initialization and Usage Example Source: https://mmcv.readthedocs.io/en/latest/_modules/mmcv/transforms/wrappers Demonstrates how to initialize and use the KeyMapper with a list of transforms and a mapping dictionary. It shows how to map input keys like 'img_src' to 'img1' and 'img_tar' to 'img2'. The example also illustrates manually setting ignored keys using '...'. ```Python >>> pipeline = [ >>> dict(type='KeyMapper', >>> mapping={ >>> 'img_src': 'img1', >>> 'img_tar': 'img2' >>> }, >>> transforms=[ >>> dict(type='RandomFlip'), >>> ]) >>> ] >>> # Example 3: Manually set ignored keys by "..." >>> pipeline = [ >>> ... >>> dict(type='KeyMapper', >>> mapping={ >>> # map outer key "gt_img" to inner key "img" >>> 'img': 'gt_img', >>> # ignore outer key "mask" >>> 'mask': ..., >>> }, >>> transforms=[ >>> dict(type='RandomFlip'), >>> ]) >>> ... >>> ] ``` -------------------------------- ### Install MMCV (with ops) Source: https://mmcv.readthedocs.io/en/latest/compatibility This command installs the MMCV package, which includes all necessary operations. It's recommended for users who need the full functionality of MMCV. ```bash pip install mmcv ``` -------------------------------- ### Install MMCV with pip (Specific Version and CUDA) Source: https://mmcv.readthedocs.io/en/latest/get_started/installation This command installs a specific version of MMCV (e.g., 2.2.0) using pip, targeting a specific CUDA version (e.g., cu121) and PyTorch version (e.g., torch2.4). It uses a custom index URL for finding the correct pre-built package. ```bash pip install mmcv==2.2.0 -f https://download.openmmlab.com/mmcv/dist/cu121/torch2.4/index.html ``` -------------------------------- ### Build mmcv with Docker (Local Repo) Source: https://mmcv.readthedocs.io/en/latest/get_started/installation This command builds the mmcv Docker image using the local repository. It assumes you are in the root directory of the mmcv project. ```bash cd ``` -------------------------------- ### Build mmcv with Docker (Remote Repo) Source: https://mmcv.readthedocs.io/en/latest/get_started/installation This command builds the mmcv Docker image using a remote repository. The Dockerfile installs the latest released version of mmcv-full by default. ```bash MMCV=2.0.0 ``` -------------------------------- ### Install MMCV (CPU Version) Source: https://mmcv.readthedocs.io/en/latest/get_started/build Installs the MMCV library in development mode after building the CPU version. This makes the library available for import. ```bash (mmcv) PS C:\Users\xxx\mmcv> python setup.py develop ``` -------------------------------- ### Verify PyTorch Installation Source: https://mmcv.readthedocs.io/en/latest/get_started/build Checks if PyTorch is installed by printing its version. This is a prerequisite for building MMCV. ```Python import torch;print(torch.__version__) ``` -------------------------------- ### Install and Run Pre-commit Hooks Source: https://mmcv.readthedocs.io/en/latest/community/contributing This command installs the pre-commit hooks defined in the project's configuration file. These hooks ensure code style consistency and can automatically fix some style issues before committing. ```bash pre-commit install pre-commit run --all-files ``` -------------------------------- ### Install mmcv-full with Docker Source: https://mmcv.readthedocs.io/en/latest/get_started/build This command installs the mmcv-full package by pulling and running a Cambricon Docker image. Ensure you have the correct Docker image provided by Cambricon. ```Shell ${docker image} ``` -------------------------------- ### Check Prerequisites and Packages Source: https://mmcv.readthedocs.io/en/latest/get_started/api_reference Functions to check if required Python packages or executables are installed and available in the environment. This helps in ensuring the correct setup for running the library. ```Python from mmcv.utils.misc import check_prerequisites, requires_package, requires_executable # Check if 'numpy' is installed check_prerequisites(['numpy']) # Ensure 'torch' is available, raise error if not # requires_package('torch') # Ensure 'gcc' executable is available, raise error if not # requires_executable('gcc') ``` ```Python from mmengine.utils.misc import check_prerequisites, requires_package, requires_executable # Check if 'pandas' is installed check_prerequisites(['pandas']) # Ensure 'tensorflow' is available, raise error if not # requires_package('tensorflow') # Ensure 'git' executable is available, raise error if not # requires_executable('git') ``` -------------------------------- ### Check MMCV and PyTorch Installation Source: https://mmcv.readthedocs.io/en/latest/faq Confirms that both MMCV and its necessary operations module are installed correctly. This helps in ruling out basic installation problems. ```python import mmcv import mmcv.ops ``` -------------------------------- ### Validate mmcv-lite Installation Source: https://mmcv.readthedocs.io/en/latest/get_started/build Checks the installation of mmcv-lite by importing the library and printing its version. ```python 'import mmcv;print(mmcv.__version__)' ``` -------------------------------- ### Test mmcv-full installation on MLU Source: https://mmcv.readthedocs.io/en/latest/get_started/build This Python code snippet verifies the successful installation of mmcv-full on a Cambricon MLU device. It imports necessary libraries, creates tensors on the MLU, and uses the `sigmoid_focal_loss` function from `mmcv.ops`. ```Python import torch import torch_mlu from mmcv.ops import sigmoid_focal_loss x = torch.randn(3, 10).mlu() x.requires_grad = True y = torch.tensor([1, 5, 3]).mlu() w = torch.ones(10).float().mlu() output = sigmoid_focal_loss(x, y, 2.0, 0.25, w, 'none') print(output) ``` -------------------------------- ### Install MMCV (version < 2.0.0) Source: https://mmcv.readthedocs.io/en/latest/compatibility This command installs a version of MMCV prior to 2.0.0. This is useful for users who need to maintain compatibility with older projects or specific features available only in older versions. ```bash pip install "mmcv < 2.0.0" ``` -------------------------------- ### Install PyTorch (CPU) Source: https://mmcv.readthedocs.io/en/latest/get_started/build Installs the CPU-only version of PyTorch using Conda. This is a prerequisite for building the CPU version of MMCV. ```bash (mmcv) PS C:\Users\xxx> conda install install pytorch torchvision cpuonly -c pytorch ``` -------------------------------- ### Validate MMCV Installation Source: https://mmcv.readthedocs.io/en/latest/get_started/build Runs a Python script to check if MMCV has been installed correctly and all its components are functional. ```bash (mmcv) PS C:\Users\xxx\mmcv> python .dev_scripts/check_installation.py ``` -------------------------------- ### Install Unit Test Dependencies (Linux) Source: https://mmcv.readthedocs.io/en/latest/community/contributing Installs necessary dependencies for running unit tests on Linux systems, particularly for modules like video that might have external requirements. This ensures a complete testing environment. ```shell # Linux sudo ``` -------------------------------- ### Install MMCV (without ops) Source: https://mmcv.readthedocs.io/en/latest/compatibility This command installs the mmcv-lite package, which does not include all operations. This is suitable for users who only need a subset of MMCV's functionalities or are working with specific versions. ```bash pip install mmcv-lite ``` -------------------------------- ### Install MMCV (GPU Version) Source: https://mmcv.readthedocs.io/en/latest/get_started/build Installs the MMCV library in development mode after building the GPU version. This makes the library available for import and use with CUDA. ```bash # install python setup.py develop ``` -------------------------------- ### Build MMCV (CPU Version) Source: https://mmcv.readthedocs.io/en/latest/get_started/build Builds the CPU version of MMCV by compiling the C++ extensions. This command prepares the library for installation. ```bash (mmcv) PS C:\Users\xxx\mmcv> python setup.py build_ext ``` -------------------------------- ### Check PyTorch and CUDA Versions Source: https://mmcv.readthedocs.io/en/latest/get_started/installation This code snippet verifies both the PyTorch version and the associated CUDA version installed in the environment. This information is crucial for selecting the correct MMCV installation command. ```python import torch;print(torch.__version__);print(torch.version.cuda) ``` -------------------------------- ### Build MMCV with GPU Support (Linux) Source: https://mmcv.readthedocs.io/en/latest/get_started/build Initiates the build process for MMCV, which can take over 10 minutes. Assumes necessary dependencies like ninja and psutil are installed, and CUDA is configured. ```Shell python setup.py install ``` -------------------------------- ### Get PyTorch Version Source: https://mmcv.readthedocs.io/en/latest/get_started/api_reference Retrieves the currently installed PyTorch version as a string. This is essential for compatibility checks and feature utilization. ```Python from mmcv.utils.parrots_wrapper import TORCH_VERSION print(f'PyTorch version: {TORCH_VERSION}') ``` ```Python from mmengine.utils.dl_utils.parrots_wrapper import TORCH_VERSION print(f'Installed PyTorch version: {TORCH_VERSION}') ``` -------------------------------- ### Get CUDA Home Directory Source: https://mmcv.readthedocs.io/en/latest/get_started/api_reference Retrieves the installation path for the CUDA toolkit. This path is often required by libraries that interface with CUDA. ```Python from mmcv.utils.parrots_wrapper import _get_cuda_home cuda_path = _get_cuda_home() print(f'CUDA home directory: {cuda_path}') ``` ```Python from mmengine.utils.dl_utils.parrots_wrapper import _get_cuda_home cuda_installation_path = _get_cuda_home() print(f'CUDA installation path: {cuda_installation_path}') ``` -------------------------------- ### Query GPU Device Information Source: https://mmcv.readthedocs.io/en/latest/get_started/build Executes the deviceQuery CUDA sample application to retrieve information about the installed GPU, including its CUDA compute capability. ```bash (mmcv) PS C:\Users\xxx\mmcv> &"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\extras\demo_suite\deviceQuery.exe" Device 0: "NVIDIA GeForce GTX 1660 SUPER" CUDA Driver Version / Runtime Version 11.7 / 11.1 CUDA Capability Major/Minor version number: 7.5 ``` -------------------------------- ### TransformBroadcaster Example Usage (Python) Source: https://mmcv.readthedocs.io/en/latest/_modules/mmcv/transforms/wrappers Illustrates how to configure the TransformBroadcaster with different scenarios, including broadcasting to keys containing data sequences and setting ignored keys during broadcasting. It shows the dictionary-based configuration for transformations like Crop and Normalize. ```python # Example 2: Broadcast to keys that contains data sequences pipeline = [ dict(type='LoadImageFromFile', key='lq'), # low-quality img dict(type='LoadImageFromFile', key='gt'), # ground-truth img # TransformBroadcaster maps multiple outer fields to standard # the inner field and process them with wrapped transforms # respectively dict(type='TransformBroadcaster', # case 2: from one outer field that contains multiple # data elements (e.g. a list) # mapping={'img': 'images'}, auto_remap=True, share_random_param=True, transforms=[ dict(type='Crop', crop_size=(384, 384)), dict(type='Normalize'), ]) ] # Example 3: Set ignored keys in broadcasting pipeline = [ dict(type='TransformBroadcaster', # Broadcast the wrapped transforms to multiple images # 'lq' and 'gt, but only update 'img_shape' once mapping={ 'img': ['lq', 'gt'], 'img_shape': ['img_shape', ...], }, auto_remap=True, share_random_params=True, transforms=[ # `RandomCrop` will modify the field "img", # and optionally update "img_shape" if it exists dict(type='RandomCrop'), ]) ] ``` -------------------------------- ### Clone MMCV Repository Source: https://mmcv.readthedocs.io/en/latest/get_started/build Clones the official MMCV repository from GitHub. This is the first step in building MMCV from source. ```bash (mmcv) PS C:\Users\xxx> git clone https://github.com/open-mmlab/mmcv.git (mmcv) PS C:\Users\xxx\mmcv> cd mmcv ``` -------------------------------- ### SimpleRoIAlign Module Initialization Source: https://mmcv.readthedocs.io/en/latest/_modules/mmcv/ops/point_sample Initializes a SimpleRoIAlign module, a faster alternative to standard RoIAlign. It requires an output size (height, width) and a spatial scale factor, with an option to enable alignment. ```Python class SimpleRoIAlign(nn.Module): def __init__(self, output_size: Tuple[int], spatial_scale: float, aligned: bool = True) -> None: """Simple RoI align in PointRend, faster than standard RoIAlign. Args: output_size (tuple[int]): h, w ``` -------------------------------- ### Build MMCV-full on Cambricon MLU Devices Source: https://mmcv.readthedocs.io/en/latest/index Specific instructions for building the full version of MMCV tailored for Cambricon MLU (Machine Learning Unit) devices. This process requires specific environment setup and compilation flags relevant to the Cambricon hardware. ```bash MMCV_WITH_CAMBRICON=1 pip install -e . ``` -------------------------------- ### Install Unit Test Dependencies (Windows) Source: https://mmcv.readthedocs.io/en/latest/community/contributing Installs necessary dependencies for running unit tests on Windows systems using Conda. This is essential for modules with specific environment requirements. ```shell # Windows conda ``` -------------------------------- ### Get Convolution Layer (Python) Source: https://mmcv.readthedocs.io/en/latest/get_started/api_reference A utility function to get or construct a convolution layer. This is a common operation in deep learning for building convolutional neural networks. ```Python from mmcv.utils.parrots_wrapper import _get_conv # Example usage: # conv_layer = _get_conv(in_channels=3, out_channels=64, kernel_size=3) ``` ```Python from mmengine.utils.dl_utils.parrots_wrapper import _get_conv # Example usage: # conv_layer = _get_conv(in_channels=3, out_channels=64, kernel_size=3) ``` -------------------------------- ### TransformBroadcaster Initialization Source: https://mmcv.readthedocs.io/en/latest/_modules/mmcv/transforms/wrappers Initializes the TransformBroadcaster with a list of transformations, optional mappings for inputs and outputs, and configuration for automatic remapping and sharing random parameters. It inherits from a base class and sets up parameters for controlling transformation behavior. ```python def __init__(self, transforms: List[Union[Dict, Callable[[Dict], Dict]]], mapping: Optional[Dict] = None, remapping: Optional[Dict] = None, auto_remap: Optional[bool] = None, allow_nonexist_keys: bool = False, share_random_params: bool = False): super().__init__(transforms, mapping, remapping, auto_remap, allow_nonexist_keys) self.share_random_params = share_random_params ``` -------------------------------- ### Get Device Utility in MMCV and MMEngine Source: https://mmcv.readthedocs.io/en/latest/get_started/api_reference A utility function to get the current device in MMCV and MMEngine. It helps in managing device assignments for tensors and models. ```Python from mmcv.device.utils import get_device from mmengine.device.utils import get_device ``` -------------------------------- ### MMSyncBN Initialization and Parameters Source: https://mmcv.readthedocs.io/en/latest/_modules/mmcv/ops/sync_bn Initializes the SyncBatchNorm module with specified features, epsilon, momentum, and affine parameters. It configures running statistics tracking and synchronization groups, supporting different statistical modes ('default' and 'N'). ```Python class SyncBatchNorm(Module): """Synchronized Batch Normalization. Args: num_features (int): number of features/chennels in input tensor eps (float, optional): a value added to the denominator for numerical stability. Defaults to 1e-5. momentum (float, optional): the value used for the running_mean and running_var computation. Defaults to 0.1. affine (bool, optional): whether to use learnable affine parameters. Defaults to True. track_running_stats (bool, optional): whether to track the running mean and variance during training. When set to False, this module does not track such statistics, and initializes statistics buffers ``running_mean`` and ``running_var`` as ``None``. When these buffers are ``None``, this module always uses batch statistics in both training and eval modes. Defaults to True. group (int, optional): synchronization of stats happen within each process group individually. By default it is synchronization across the whole world. Defaults to None. stats_mode (str, optional): The statistical mode. Available options includes ``'default'`` and ``'N'``. Defaults to 'default'. When ``stats_mode=='default'``, it computes the overall statistics using those from each worker with equal weight, i.e., the statistics are synchronized and simply divied by ``group``. This mode will produce inaccurate statistics when empty tensors occur. When ``stats_mode=='N'``, it compute the overall statistics using the total number of batches in each worker ignoring the number of group, i.e., the statistics are synchronized and then divied by the total batch ``N``. This mode is beneficial when empty tensors occur during training, as it average the total mean by the real number of batch. """ def __init__(self, num_features: int, eps: float = 1e-5, momentum: float = 0.1, affine: bool = True, track_running_stats: bool = True, group: Optional[int] = None, stats_mode: str = 'default'): super().__init__() self.num_features = num_features self.eps = eps self.momentum = momentum self.affine = affine self.track_running_stats = track_running_stats group = dist.group.WORLD if group is None else group self.group = group self.group_size = dist.get_world_size(group) assert stats_mode in ['default', 'N'], \ f'"stats_mode" only accepts "default" and "N", got "{stats_mode}"' self.stats_mode = stats_mode if self.affine: self.weight = Parameter(torch.Tensor(num_features)) self.bias = Parameter(torch.Tensor(num_features)) else: self.register_parameter('weight', None) self.register_parameter('bias', None) if self.track_running_stats: self.register_buffer('running_mean', torch.zeros(num_features)) self.register_buffer('running_var', torch.ones(num_features)) self.register_buffer('num_batches_tracked', torch.tensor(0, dtype=torch.long)) else: self.register_buffer('running_mean', None) self.register_buffer('running_var', None) self.register_buffer('num_batches_tracked', None) self.reset_parameters() ``` -------------------------------- ### Using Custom MyFlip Transform Source: https://mmcv.readthedocs.io/en/latest/understand_mmcv/data_transform Shows how to instantiate and use the custom `MyFlip` transformation class with a sample image dictionary. It also illustrates how to integrate the custom transform into the MMCV pipeline configuration. ```Python import numpy as np transform = MyFlip(direction='horizontal') data_dict = {'img': np.random.rand(224, 224, 3)} data_dict = transform(data_dict) processed_img = data_dict['img'] ``` ```Python pipeline = [ ... dict(type='MyFlip', direction='horizontal'), ... ] ``` -------------------------------- ### Install PyTorch with CUDA (Windows) Source: https://mmcv.readthedocs.io/en/latest/get_started/build Installs PyTorch, torchvision, and cudatoolkit version 10.2 within the activated conda environment on Windows. This command specifies the channel as 'pytorch'. ```Shell # CUDA version (mmcv) PS C:\Users\xxx> conda install pytorch torchvision cudatoolkit=10.2 -c pytorch ``` -------------------------------- ### Validate MMCV Installation (Linux/macOS) Source: https://mmcv.readthedocs.io/en/latest/get_started/build Confirms that MMCV has been installed correctly by running a validation command. If errors occur, users are directed to check FAQs or open an issue. ```Shell python -m mmcv.utils.collect_env ```