### Install MMPreTrain Source: https://context7.com/open-mmlab/mmpretrain/llms.txt Instructions for installing MMPreTrain using pip and mim. Supports installation from PyPI, from source for development, and with optional multi-modal support. ```bash pip install -U openmim && mim install "mmpretrain>=1.0.0rc8" ``` ```bash git clone https://github.com/open-mmlab/mmpretrain.git cd mmpretrain pip install -U openmim && mim install -e . ``` ```bash mim install -e ".[multimodal]" ``` -------------------------------- ### Verify MMPretrain Installation Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/get_started.md Demonstrates how to verify the installation by running an inference demo. Includes both command-line and Python API approaches. ```shell python demo/image_demo.py demo/demo.JPEG resnet18_8xb32_in1k --device cpu ``` ```python from mmpretrain import get_model, inference_model model = get_model('resnet18_8xb32_in1k', device='cpu') inference_model(model, 'demo/demo.JPEG') ``` -------------------------------- ### Execute Model Training and Distributed Training Source: https://context7.com/open-mmlab/mmpretrain/llms.txt Provides command-line examples for training models on single/multi-GPU setups, resuming from checkpoints, and utilizing Slurm clusters. ```bash # Single GPU python tools/train.py configs/resnet/resnet50_8xb32_in1k.py # Multi-GPU (4 GPUs) bash ./tools/dist_train.sh configs/resnet/resnet50_8xb32_in1k.py 4 # Resume training python tools/train.py configs/resnet/resnet50_8xb32_in1k.py --resume ./work_dirs/my_experiment/latest.pth # Slurm cluster ./tools/slurm_train.sh partition_name job_name configs/resnet/resnet50_8xb32_in1k.py ./work_dirs/resnet50 ``` -------------------------------- ### Install MMPretrain Framework Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/get_started.md Provides commands to install MMPretrain either from source for development or as a standard Python package using OpenMIM. ```shell # Install from source git clone https://github.com/open-mmlab/mmpretrain.git cd mmpretrain pip install -U openmim && mim install -e . # Install as a Python package pip install -U openmim && mim install "mmpretrain>=1.0.0rc8" ``` -------------------------------- ### Reparameterize Model Checkpoint (Detailed Bash Example) Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/zh_CN/papers/riformer.md A detailed bash example showing how to download a checkpoint and then reparameterize it using the conversion script, specifying configuration and file paths. ```bash # download the weight wget https://download.openmmlab.com/mmclassification/v1/riformer/riformer-s12_32xb128_in1k_20230406-6741ce71.pth # reparameterize unfused weight to fused weight python tools/model_converters/reparameterize_model.py configs/riformer/riformer-s12_8xb128_in1k.py riformer-s12_32xb128_in1k_20230406-6741ce71.pth riformer-s12_deploy.pth ``` -------------------------------- ### Install Multi-modality Support Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/get_started.md Adds optional dependencies for multi-modality models by including the [multimodal] flag during installation. ```shell # Install from source mim install -e ".[multimodal]" # Install as a Python package mim install "mmpretrain[multimodal]>=1.0.0rc8" ``` -------------------------------- ### Install PyTorch Grad-CAM Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/useful_tools/cam_visualization.md Installs the required 'grad-cam' library version 1.3.6 or higher for CAM visualization. This is a prerequisite for using the visualization tool. ```bash pip install "grad-cam>=1.3.6" ``` -------------------------------- ### Train and Test ConvNeXt Models Source: https://github.com/open-mmlab/mmpretrain/blob/main/configs/convnext/README.md Provides command-line interface examples for training a ConvNeXt model on a dataset and evaluating a pre-trained checkpoint. ```shell python tools/train.py configs/convnext/convnext-tiny_32xb128_in1k.py ``` ```shell python tools/test.py configs/convnext/convnext-tiny_32xb128_in1k.py https://download.openmmlab.com/mmclassification/v0/convnext/convnext-tiny_32xb128_in1k_20221207-998cf3e9.pth ``` -------------------------------- ### Test DeiT Model using Command Line Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/zh_CN/papers/deit.md This command-line example shows how to test a trained DeiT model. It requires the configuration file and the URL or path to the pre-trained weights (e.g., a .pth file). ```shell python tools/test.py configs/deit/deit-tiny_4xb256_in1k.py https://download.openmmlab.com/mmclassification/v0/deit/deit-tiny_pt-4xb256_in1k_20220218-13b382a0.pth ``` -------------------------------- ### Train and Test BYOL Models via CLI Source: https://github.com/open-mmlab/mmpretrain/blob/main/configs/byol/README.md Provides command-line interface examples for training a new model and testing an existing checkpoint using MMPretrain tools. ```shell python tools/train.py configs/byol/byol_resnet50_16xb256-coslr-200e_in1k.py ``` ```shell python tools/test.py configs/byol/benchmarks/resnet50_8xb512-linear-coslr-90e_in1k.py https://download.openmmlab.com/mmselfsup/1.x/byol/byol_resnet50_16xb256-coslr-200e_in1k/resnet50_linear-8xb512-coslr-90e_in1k/resnet50_linear-8xb512-coslr-90e_in1k_20220825-7596c6f5.pth ``` -------------------------------- ### Resume Training using Command Line Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/advanced_guides/runtime.md Provides command-line examples for resuming training using the 'tools/train.py' script. The '--resume' argument can be used to automatically resume from the latest checkpoint or to resume from a specific checkpoint file. ```bash # Automatically resume from the latest checkpoint. python tools/train.py configs/resnet/resnet50_8xb32_in1k.py --resume # Resume from the specified checkpoint. python tools/train.py configs/resnet/resnet50_8xb32_in1k.py --resume checkpoints/resnet.pth ``` -------------------------------- ### Configure Visualizer Backends Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/advanced_guides/runtime.md Configures the UniversalVisualizer to log data to different backends. Examples include local storage, TensorBoard, and WandB. ```python visualizer = dict( type='UniversalVisualizer', vis_backends=[ dict(type='LocalVisBackend'), dict(type='TensorboardVisBackend'), ] ) visualizer = dict( type='UniversalVisualizer', vis_backends=[ dict(type='LocalVisBackend'), dict(type='WandbVisBackend'), ] ) ``` -------------------------------- ### Train DeiT Model using Command Line Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/zh_CN/papers/deit.md This snippet provides the command-line instruction to start training a DeiT model. It requires a configuration file (e.g., `deit-tiny_4xb256_in1k.py`) and uses the `train.py` script from the `mmpretrain` project. ```shell python tools/train.py configs/deit/deit-tiny_4xb256_in1k.py ``` -------------------------------- ### Download and Preprocess ImageNet using MIM (Bash) Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/user_guides/dataset_prepare.md Command to download and preprocess the ImageNet dataset using the MIM tool. Requires OpenXlab CLI tools to be installed and logged in. ```bash # install OpenXlab CLI tools pip install -U openxlab # log in to OpenXLab openxlab login # download and preprocess by MIM, better to execute in $MMPreTrain directory. mim download mmpretrain --dataset imagenet1k ``` -------------------------------- ### Configure Environment Settings Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/advanced_guides/runtime.md Sets low-level parameters including cuDNN benchmarks, multi-processing start methods, and distributed communication backends. ```python env_cfg = dict( cudnn_benchmark=False, mp_cfg=dict(mp_start_method='fork', opencv_num_threads=0), dist_cfg=dict(backend='nccl'), ) ``` -------------------------------- ### Data Augmentation and Processing Example Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/advanced_guides/pipeline.md Illustrates a simple data augmentation recipe involving random resizing and cropping to a specified scale, followed by random horizontal flipping with a given probability. ```python train_pipeline = [ ... dict(type='RandomResizedCrop', scale=224), dict(type='RandomFlip', prob=0.5, direction='horizontal'), ... ] ``` -------------------------------- ### Using GLIP Model in Python Source: https://github.com/open-mmlab/mmpretrain/blob/main/configs/glip/README.md Demonstrates how to load a pre-trained GLIP model using mmpretrain and perform inference. It shows how to get the model, pass input tensors, and extract features. ```python import torch from mmpretrain import get_model model = get_model('swin-t_glip-pre_3rdparty', pretrained=True) inputs = torch.rand(1, 3, 224, 224) out = model(inputs) print(type(out)) # To extract features. feats = model.extract_feat(inputs) print(type(feats)) ``` -------------------------------- ### Switch to Deploy Mode Programmatically Source: https://github.com/open-mmlab/mmpretrain/blob/main/configs/repmlp/README.md Demonstrates how to instantiate a RepMLPNet backbone and manually trigger the switch to deployment mode within a Python script. ```python from mmpretrain.models import RepMLPNet backbone = RepMLPNet(arch='B', img_size=224, reparam_conv_kernels=(1, 3)) backbone.switch_to_deploy() ``` -------------------------------- ### Initialize Universal Visualizer Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/migration.md Sets up the UniversalVisualizer instance to handle result visualization and logging across multiple backends. ```python visualizer = dict( type='UniversalVisualizer', vis_backends=[ dict(type='LocalVisBackend'), ] ) ``` -------------------------------- ### Install PyTorch for MMPretrain Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/get_started.md Installs PyTorch and torchvision dependencies. Provides separate commands for GPU-enabled environments and CPU-only environments. ```shell # GPU platforms conda install pytorch torchvision -c pytorch # CPU platforms conda install pytorch torchvision cpuonly -c pytorch ``` -------------------------------- ### Train and Test VGG Models using MMPretrain Scripts Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/zh_CN/papers/vgg.md These commands illustrate how to initiate the training and testing processes for VGG models using MMPretrain's command-line tools. Ensure your dataset is prepared according to the documentation before running these commands. Training requires a configuration file, while testing requires both a configuration file and a model checkpoint path. ```shell python tools/train.py configs/vgg/vgg11_8xb32_in1k.py ``` ```shell python tools/test.py configs/vgg/vgg11_8xb32_in1k.py https://download.openmmlab.com/mmclassification/v0/vgg/vgg11_batch256_imagenet_20210208-4271cd6c.pth ``` -------------------------------- ### Install MMSegmentation for Downstream Tasks Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/user_guides/downstream.md Install the MMSegmentation library using OpenMMLab's MIM tool to enable downstream semantic segmentation workflows. ```shell pip install openmim mim install 'mmsegmentation>=1.0.0rc0' ``` -------------------------------- ### Install MMDetection for Downstream Tasks Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/user_guides/downstream.md Install the MMDetection library using OpenMMLab's MIM tool to enable downstream object detection workflows. ```shell pip install openmim mim install 'mmdet>=3.0.0rc0' ``` -------------------------------- ### Initialize Multi-modality Dataset in MMPretrain Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/user_guides/dataset_prepare.md Demonstrates how to instantiate a multi-modality dataset class, such as RefCOCO. These datasets often require additional parameters like annotation files and specific data prefixes. ```python from mmpretrain.datasets import RefCOCO # Initialize the RefCOCO dataset dataset = RefCOCO( data_root='data/refcoco', ann_file='annotations/refcoco.json', data_prefix='images/', split='train' ) ``` -------------------------------- ### Install and Initialize Pre-commit Hook Source: https://github.com/open-mmlab/mmpretrain/blob/main/CONTRIBUTING.md These commands install the pre-commit tool and then initialize the pre-commit hooks for the repository. This ensures that code is checked and formatted automatically before each commit, adhering to project standards. ```shell pip install -U pre-commit pre-commit install ``` -------------------------------- ### Load Checkpoint and Resume Training Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/advanced_guides/runtime.md Demonstrates how to specify a checkpoint file to load for initialization or resuming training. 'load_from' takes a path to a checkpoint, and 'resume=True' enables resuming from the loaded checkpoint. Auto-resuming from the latest checkpoint is also supported by setting 'load_from=None' and 'resume=True'. ```python # load from which checkpoint load_from = "Your checkpoint path" # whether to resume training from the loaded checkpoint resume = False ``` -------------------------------- ### Example Model Configuration with L1Loss Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/advanced_guides/modules.md This is a complete example of a model configuration dictionary that includes the `L1Loss` as the loss function for the `LinearClsHead`. It defines the backbone, neck, and head components of an image classifier. ```python model = dict( type='ImageClassifier', backbone=dict( type='ResNet_CIFAR', depth=18, num_stages=4, out_indices=(3, ), style='pytorch'), neck=dict(type='GlobalAveragePooling'), head=dict( type='LinearClsHead', num_classes=10, in_channels=512, loss=dict(type='L1Loss', loss_weight=1.0), topk=(1, 5), )) ``` -------------------------------- ### Inspect Configuration via CLI Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/user_guides/config.md Use the provided utility script to print the fully resolved configuration file, including all inherited values, to the console. ```bash python tools/misc/print_config.py /PATH/TO/CONFIG ``` -------------------------------- ### Train and Test RepVGG Models using Command Line Source: https://github.com/open-mmlab/mmpretrain/blob/main/configs/repvgg/README.md Provides command-line instructions for training and testing RepVGG models. It assumes the dataset is prepared according to the documentation. Testing can be done with a standard checkpoint or a reparameterized model. ```shell python tools/train.py configs/repvgg/repvgg-A0_8xb32_in1k.py ``` ```shell python tools/test.py configs/repvgg/repvgg-A0_8xb32_in1k.py https://download.openmmlab.com/mmclassification/v0/repvgg/repvgg-A0_8xb32_in1k_20221213-60ae8e23.pth ``` ```shell python tools/test.py configs/repvgg/repvgg-A0_8xb32_in1k.py repvgg_A0_deploy.pth --cfg-options model.backbone.deploy=True ``` -------------------------------- ### GET /model/load Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/zh_CN/papers/efficientnet_v2.md Initialize an EfficientNetV2 model instance for feature extraction or custom processing. ```APIDOC ## GET /model/load ### Description Loads a pre-trained EfficientNetV2 model into memory for further operations like feature extraction. ### Method GET ### Endpoint /model/load ### Parameters #### Query Parameters - **model_name** (string) - Required - The identifier of the model - **pretrained** (boolean) - Optional - Whether to load pre-trained weights ### Request Example { "model_name": "efficientnetv2-b0_3rdparty_in1k", "pretrained": true } ### Response #### Success Response (200) - **model_instance** (object) - The loaded PyTorch model object #### Response Example { "status": "success", "message": "Model loaded successfully" } ``` -------------------------------- ### GET /models/load Source: https://github.com/open-mmlab/mmpretrain/blob/main/configs/simmim/README.md Load a SimMIM model architecture for feature extraction or downstream tasks. ```APIDOC ## GET /models/load ### Description Initializes a SimMIM model with pre-trained weights and provides methods for feature extraction. ### Method GET ### Endpoint /models/load ### Parameters #### Query Parameters - **model_name** (string) - Required - The model configuration name. - **pretrained** (boolean) - Optional - Whether to load pre-trained weights. ### Request Example { "model_name": "simmim_swin-base-w6_8xb256-amp-coslr-100e_in1k-192px", "pretrained": true } ### Response #### Success Response (200) - **model_type** (string) - The type of the loaded model object. - **feature_dim** (integer) - The dimension of the extracted features. #### Response Example { "model_type": "torch.nn.Module", "feature_dim": 1024 } ``` -------------------------------- ### GET /models/features Source: https://github.com/open-mmlab/mmpretrain/blob/main/configs/cspnet/README.md Load a CSPNet model and extract feature maps from input tensors. ```APIDOC ## GET /models/features ### Description Loads a specific CSPNet model architecture and extracts high-level features from a provided tensor input. ### Method GET ### Endpoint /models/features ### Parameters #### Query Parameters - **model_name** (string) - Required - The name of the model to load - **pretrained** (boolean) - Optional - Whether to load pre-trained weights ### Request Example { "model_name": "cspdarknet50_3rdparty_8xb32_in1k", "pretrained": true } ### Response #### Success Response (200) - **features** (tensor) - The extracted feature map output from the model backbone #### Response Example { "features": "torch.Tensor(...)" } ``` -------------------------------- ### Initialize and Use LeViT Model Source: https://github.com/open-mmlab/mmpretrain/blob/main/configs/levit/README.md Demonstrates how to load a pre-trained LeViT model using mmpretrain, perform a forward pass with dummy input, and extract features from the model. ```python import torch from mmpretrain import get_model model = get_model('levit-128s_3rdparty_in1k', pretrained=True) inputs = torch.rand(1, 3, 224, 224) out = model(inputs) print(type(out)) # To extract features. feats = model.extract_feat(inputs) print(type(feats)) ``` -------------------------------- ### GET /model/load Source: https://github.com/open-mmlab/mmpretrain/blob/main/configs/conformer/README.md Instantiate a Conformer model architecture for feature extraction or custom processing. ```APIDOC ## GET /model/load ### Description Loads a pre-trained Conformer model into memory, allowing for direct tensor input processing or feature extraction. ### Method GET ### Endpoint /model/load ### Parameters #### Query Parameters - **model_name** (string) - Required - The identifier of the Conformer model - **pretrained** (boolean) - Optional - Whether to load pre-trained weights ### Request Example GET /model/load?model_name=conformer-tiny-p16_3rdparty_in1k&pretrained=true ### Response #### Success Response (200) - **model_type** (string) - The type of the loaded model - **status** (string) - Confirmation of successful loading #### Response Example { "model_type": "Conformer", "status": "loaded" } ``` -------------------------------- ### MobileNetV2 Inference Source: https://github.com/open-mmlab/mmpretrain/blob/main/configs/mobilenet_v2/README.md Perform inference using a pre-trained MobileNetV2 model to get predictions for an image. ```APIDOC ## Predict image ### Description Perform inference using a pre-trained MobileNetV2 model to get predictions for an image. ### Method `inference_model` ### Endpoint N/A (Python function) ### Parameters - **model_name** (str) - Required - The name of the pre-trained model (e.g., 'mobilenet-v2_8xb32_in1k'). - **image_path** (str) - Required - The path to the input image. ### Request Example ```python from mmpretrain import inference_model predict = inference_model('mobilenet-v2_8xb32_in1k', 'demo/bird.JPEG') print(predict['pred_class']) print(predict['pred_score']) ``` ### Response #### Success Response (200) - **pred_class** (list) - Predicted class labels. - **pred_score** (list) - Predicted class scores. #### Response Example ```json { "pred_class": ["Robin", ...], "pred_score": [0.98, ...] } ``` ``` -------------------------------- ### Switch to Deploy Mode Programmatically Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/zh_CN/papers/replknet.md Demonstrates how to manually switch a RepLKNet backbone to deploy mode within a Python script to enable optimized inference. ```python from mmpretrain.models import RepLKNet backbone = RepLKNet(arch='31B') backbone.switch_to_deploy() ``` -------------------------------- ### Register and Configure New Backbone Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/advanced_guides/modules.md Shows how to expose the new backbone module in the package initialization file and how to reference it within a model configuration dictionary. ```python from .resnet_cifar import ResNet_CIFAR __all__ = ['ResNet_CIFAR'] ``` ```python model = dict( backbone=dict( type='ResNet_CIFAR', depth=18 ) ) ``` -------------------------------- ### GET /models/load Source: https://github.com/open-mmlab/mmpretrain/blob/main/configs/convnext_v2/README.md Loads a ConvNeXt V2 model instance for custom inference or feature extraction. ```APIDOC ## GET /models/load ### Description Initializes a model object with pre-trained weights for advanced tasks like feature extraction. ### Method GET ### Endpoint /models/load ### Parameters #### Query Parameters - **model_name** (string) - Required - The identifier for the model architecture - **pretrained** (boolean) - Optional - Whether to load pre-trained weights ### Request Example { "model_name": "convnext-v2-atto_3rdparty-fcmae_in1k", "pretrained": true } ### Response #### Success Response (200) - **model_type** (string) - The class type of the loaded model - **status** (string) - Confirmation of successful loading #### Response Example { "model_type": "ConvNeXtV2", "status": "success" } ``` -------------------------------- ### JSON Annotation File Example for OpenMMLab 2.0 Dataset Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/user_guides/dataset_prepare.md An example of a JSON annotation file adhering to the OpenMMLab 2.0 Dataset Format Specification. It includes 'metainfo' for dataset metadata and 'data_list' containing sample information like image paths and ground truth labels. This format is designed for unified dataset interfaces in multi-task algorithm training. ```json { "metainfo": { "classes": ["cat", "dog"], "...": "..." }, "data_list": [ { "img_path": "xxx/xxx_0.jpg", "gt_label": 0, "...": "..." }, { "img_path": "xxx/xxx_1.jpg", "gt_label": 1, "...": "..." } ] } ``` -------------------------------- ### Using Built-in Metrics Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/advanced_guides/evaluation.md Examples of how to configure `val_evaluator` and `test_evaluator` in your config files to use pre-defined metrics. ```APIDOC ## Using Built-in Metrics ### Description Configure `val_evaluator` and `test_evaluator` to use MMPretrain's provided metrics for single-label and multi-label classification. ### Examples 1. **Top-k Accuracy**: ```python val_evaluator = dict(type='Accuracy', topk=(1, 5)) test_evaluator = val_evaluator ``` 2. **Accuracy with Precision and Recall**: ```python val_evaluator = [ dict(type='Accuracy', topk=(1, 5)), dict(type='SingleLabelMetric', items=['precision', 'recall']) ] test_evaluator = val_evaluator ``` 3. **Multi-label Metrics (mAP, Class-wise, Overall)**: ```python val_evaluator = [ dict(type='AveragePrecision'), dict(type='MultiLabelMetric', average='macro'), # class-wise mean dict(type='MultiLabelMetric', average='micro') # overall mean ] test_evaluator = val_evaluator ``` ``` -------------------------------- ### Execute Training and Testing Commands Source: https://github.com/open-mmlab/mmpretrain/blob/main/configs/maskfeat/README.md Provides the command-line interface instructions for training a model from a configuration file and testing a model using a specific checkpoint. ```shell python tools/train.py configs/maskfeat/maskfeat_vit-base-p16_8xb256-amp-coslr-300e_in1k.py ``` ```shell python tools/test.py configs/maskfeat/benchmarks/vit-base-p16_8xb256-coslr-100e_in1k.py https://download.openmmlab.com/mmselfsup/1.x/maskfeat/maskfeat_vit-base-p16_8xb256-amp-coslr-300e_in1k/vit-base-p16_ft-8xb256-coslr-100e_in1k/vit-base-p16_ft-8xb256-coslr-100e_in1k_20221028-5134431c.pth ``` -------------------------------- ### Train and Test MAE Models using MMPretrain CLI Source: https://github.com/open-mmlab/mmpretrain/blob/main/configs/mae/README.md These shell commands illustrate how to train and test MAE models using the MMPretrain framework. Training requires a configuration file and dataset preparation, while testing uses a specific configuration and a checkpoint (or None). ```shell python tools/train.py configs/mae/mae_vit-base-p16_8xb512-amp-coslr-300e_in1k.py ``` ```shell python tools/test.py configs/mae/benchmarks/vit-base-p16_8xb128-coslr-100e_in1k.py None ``` -------------------------------- ### GET /datasets/multi-modality Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/user_guides/dataset_prepare.md Retrieves the list of supported multi-modality datasets, such as RefCOCO, used for advanced pretraining tasks. ```APIDOC ## GET /datasets/multi-modality ### Description Returns a list of supported multi-modality datasets, including details on available splits and external download sources. ### Method GET ### Endpoint /datasets/multi-modality ### Parameters None ### Response #### Success Response (200) - **datasets** (array) - A list of multi-modality dataset objects. #### Response Example { "datasets": [ {"name": "RefCOCO", "splits": ["train", "val", "test"], "homepage": "https://bvisionweb1.cs.unc.edu/licheng/referit/data/refcoco.zip"} ] } ``` -------------------------------- ### GET /models/load Source: https://github.com/open-mmlab/mmpretrain/blob/main/configs/efficientnet_v2/README.md Loads a pre-trained EfficientNetV2 model into memory for custom inference or feature extraction tasks. ```APIDOC ## GET /models/load ### Description Initializes an EfficientNetV2 model instance with pre-trained weights for downstream tasks. ### Method GET ### Endpoint /models/load ### Parameters #### Query Parameters - **model_name** (string) - Required - The identifier for the model - **pretrained** (boolean) - Optional - Whether to load pre-trained weights (default: true) ### Request Example { "model_name": "efficientnetv2-b0_3rdparty_in1k", "pretrained": true } ### Response #### Success Response (200) - **status** (string) - Confirmation of model loading - **model_type** (string) - The architecture type loaded #### Response Example { "status": "success", "model_type": "EfficientNetV2" } ``` -------------------------------- ### Initialize Image Classification Dataset in MMPretrain Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/user_guides/dataset_prepare.md Demonstrates how to instantiate a standard image classification dataset class within the MMPretrain framework. The constructor typically requires the data_root path and optional parameters for data splits and processing pipelines. ```python from mmpretrain.datasets import CIFAR10 # Initialize the CIFAR10 dataset dataset = CIFAR10(data_root='data/cifar10', split='train') ``` -------------------------------- ### Implementing Custom Metrics Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/advanced_guides/evaluation.md Guide on creating and registering your own evaluation metrics by inheriting from `BaseMetric` and using the `METRICS` registry. ```APIDOC ## Implementing Custom Metrics ### Description Create custom evaluation metrics by defining a new class that inherits from `mmengine.evaluator.BaseMetric`, implementing `process` and `compute_metrics` methods, and registering it with the `METRICS` registry. ### Steps 1. **Create a new metric file** (e.g., `mmpretrain/evaluation/metrics/my_metric.py`) ```python from mmengine.evaluator import BaseMetric from mmpretrain.registry import METRICS from typing import Sequence, Dict, List @METRICS.register_module() class MyMetric(BaseMetric): def process(self, data_batch: Sequence[Dict], data_samples: Sequence[Dict]): """ The processed results should be stored in ``self.results``, which will be used to computed the metrics when all batches have been processed. `data_batch` stores the batch data from dataloader, and `data_samples` stores the batch outputs from model. """ # Implement your data processing logic here pass def compute_metrics(self, results: List): """ Compute the metrics from processed results and returns the evaluation results. """ # Implement your metric calculation logic here pass ``` 2. **Import the custom metric** in `mmpretrain/evaluation/metrics/__init__.py`: ```python # In mmpretrain/evaluation/metrics/__init__.py ... # other imports from .my_metric import MyMetric __all__ = [..., 'MyMetric'] ``` 3. **Use the custom metric** in your config file: ```python val_evaluator = dict(type='MyMetric', ...) test_evaluator = val_evaluator ``` ### Further Reading For more details, refer to the {external+mmengine:doc}`MMEngine Documentation: Evaluation `. ``` -------------------------------- ### Configure Modular Model and Data Pipelines Source: https://context7.com/open-mmlab/mmpretrain/llms.txt Shows how to create custom configurations using inheritance, override model parameters, and define data preprocessing pipelines. ```python _base_ = ['../_base_/models/resnet50.py', '../_base_/datasets/imagenet_bs32.py'] model = dict(train_cfg=dict(augments=dict(type='CutMix', alpha=1.0))) train_pipeline = [ dict(type='LoadImageFromFile'), dict(type='RandomResizedCrop', scale=224), dict(type='PackInputs'), ] train_dataloader = dict(batch_size=32, dataset=dict(type='ImageNet', pipeline=train_pipeline)) ``` -------------------------------- ### Create and Activate Conda Environment Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/get_started.md Initializes a new Conda environment named 'openmmlab' with Python 3.8 and activates it for subsequent installations. ```shell conda create --name openmmlab python=3.8 -y conda activate openmmlab ``` -------------------------------- ### Define Training Pipeline Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/migration.md Example of a standard training pipeline configuration using the 'PackInputs' transformation, which is consistent across MMPretrain 1.x. ```python train_pipeline = [ dict(type='LoadImageFromFile'), dict( type='RandomResizedCrop', scale=224, crop_ratio_range=(0.2, 1.0), backend='pillow', interpolation='bicubic'), dict(type='RandomFlip', prob=0.5), dict(type='PackInputs') ] ``` -------------------------------- ### Register and Configure New Model Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/advanced_guides/modules.md Expose the new algorithm in the package's __init__.py file and define it within the project configuration dictionary to integrate it into the training pipeline. ```python from .new_algorithm import NewAlgorithm __all__ = ['NewAlgorithm'] ``` ```python model = dict( type='NewAlgorithm', backbone=..., neck=..., head=... ) ``` -------------------------------- ### Batch Augmentations Configuration Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/api/data_process.rst Configuration examples for batch augmentations like Mixup and CutMix, which are applied during training to improve model generalization. ```APIDOC ## Batch Augmentations ### Description Batch augmentations are components of data preprocessors that involve mixing multiple samples within a batch. Common examples include Mixup and CutMix, typically used only during the training phase. ### Configuration Examples Batch augmentations are configured within the `model.train_cfg` field. #### Basic Configuration: ```python model = dict( backbone=..., neck=..., head=..., train_cfg=dict(augments=[ dict(type='Mixup', alpha=0.8), dict(type='CutMix', alpha=1.0), ]), ) ``` #### Configuration with Probabilities: Specify probabilities for each batch augmentation. ```python model = dict( backbone=..., neck=..., head=..., train_cfg=dict(augments=[ dict(type='Mixup', alpha=0.8), dict(type='CutMix', alpha=1.0), ], probs=[0.3, 0.7]) ) ``` ### Supported Batch Augmentations - `Mixup`: Blends images and their labels. - `CutMix`: Cuts a patch from one image and pastes it onto another, adjusting labels accordingly. - `ResizeMix`: A variation that resizes images before mixing. ``` -------------------------------- ### Configure Pre-training Environment Requirements Source: https://github.com/open-mmlab/mmpretrain/blob/main/projects/fgia_accv2022_1st/README.md Lists the essential dependencies including PyTorch, torchvision, CUDA, MMEngine, and MMCV required for the pre-training pipeline. ```shell PyTorch 1.11.0 torchvision 0.12.0 CUDA 11.3 MMEngine >= 0.1.0 MMCV >= 2.0.0rc0 ``` -------------------------------- ### Load Pre-trained Model via Command Line Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/notes/finetune_custom_dataset.md Applies a pre-trained model by specifying its checkpoint path directly in the training command using `--cfg-options`. This avoids modifying configuration files directly and allows for easy switching of pre-trained weights. ```shell bash tools/dist_train.sh configs/resnet/resnet50_8xb32-ft_custom.py 8 \ --cfg-options model.backbone.init_cfg.type='Pretrained' \ model.backbone.init_cfg.checkpoint='https://download.openmmlab.com/mmselfsup/1.x/mocov3/mocov3_resnet50_8xb512-amp-coslr-100e_in1k/mocov3_resnet50_8xb512-amp-coslr-100e_in1k_20220927-f1144efa.pth' \ model.backbone.init_cfg.prefix='backbone' \ ``` -------------------------------- ### Training and Testing SwAV Models Source: https://github.com/open-mmlab/mmpretrain/blob/main/configs/swav/README.md Provides command-line instructions for training and testing SwAV models using the mmpretrain framework. Requires dataset preparation and configuration files. ```shell python tools/train.py configs/swav/swav_resnet50_8xb32-mcrop-coslr-200e_in1k-224px-96px.py ``` ```shell python tools/test.py configs/swav/benchmarks/resnet50_8xb512-linear-coslr-90e_in1k.py https://download.openmmlab.com/mmselfsup/1.x/swav/swav_resnet50_8xb32-mcrop-2-6-coslr-200e_in1k-224-96/resnet50_linear-8xb32-coslr-100e_in1k/resnet50_linear-8xb32-coslr-100e_in1k_20220825-80341e08.pth ``` -------------------------------- ### Migrate Optimizer Configuration to optim_wrapper Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/migration.md Demonstrates how to consolidate optimizer settings and gradient clipping into the new optim_wrapper structure, replacing the deprecated optimizer_config. ```python # Original optimizer = dict( type='AdamW', lr=0.0015, weight_decay=0.3, paramwise_cfg = dict( norm_decay_mult=0.0, bias_decay_mult=0.0, )) optimizer_config = dict(grad_clip=dict(max_norm=1.0)) # New optim_wrapper = dict( optimizer=dict(type='AdamW', lr=0.0015, weight_decay=0.3), paramwise_cfg = dict( norm_decay_mult=0.0, bias_decay_mult=0.0, ), clip_grad=dict(max_norm=1.0), ) ``` -------------------------------- ### GET /datasets/image-classification Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/user_guides/dataset_prepare.md Retrieves the list of supported image classification datasets available in MMPretrain, including their specific data splits and source homepages. ```APIDOC ## GET /datasets/image-classification ### Description Returns a list of supported image classification datasets, such as Caltech101, CIFAR10, and MNIST, along with their supported data splits and reference links. ### Method GET ### Endpoint /datasets/image-classification ### Parameters None ### Response #### Success Response (200) - **datasets** (array) - A list of dataset objects containing name, supported splits, and homepage URL. #### Response Example { "datasets": [ {"name": "CIFAR10", "splits": ["train", "test"], "homepage": "https://www.cs.toronto.edu/~kriz/cifar.html"}, {"name": "MNIST", "splits": ["train", "test"], "homepage": "http://yann.lecun.com/exdb/mnist/"} ] } ``` -------------------------------- ### Data Preprocessor Configuration Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/api/data_process.rst Configuration examples for the data preprocessor, which handles tasks like normalization, padding, and device transfer before feeding data to the model. ```APIDOC ## Data Preprocessor Configuration ### Description The data preprocessor is a crucial component that processes data in batches before it's fed into the neural network. It can perform operations such as moving data to the target device, padding inputs, stacking inputs into a batch, converting color formats (e.g., BGR to RGB), normalizing images, and applying batch augmentations. ### Configuration Examples #### Using the `data_preprocessor` field: ```python data_preprocessor = dict( # RGB format normalization parameters mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True, # convert image from BGR to RGB ) ``` #### Using the `model.data_preprocessor` field (higher priority): ```python model = dict( backbone = ..., neck = ..., head = ..., data_preprocessor = dict( mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True) train_cfg=... ) ``` ### Key Components - **Normalization**: Uses specified `mean` and `std` values. - **Color Conversion**: `to_rgb` flag to convert between BGR and RGB formats. - **Batch Processing**: Operates on batches of data, allowing for GPU acceleration. ### Supported Data Preprocessor Types - `ClsDataPreprocessor` - `SelfSupDataPreprocessor` - `TwoNormDataPreprocessor` - `VideoDataPreprocessor` ``` -------------------------------- ### Train Model on Single Machine (Shell) Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/user_guides/train.md This script initiates model training on a single machine, supporting both CPU and GPU. It requires a configuration file and accepts various arguments to control the training process, such as working directory, resuming training, and automatic mixed precision. ```shell python tools/train.py ${CONFIG_FILE} [ARGS] ``` ```shell CUDA_VISIBLE_DEVICES=-1 python tools/train.py ${CONFIG_FILE} [ARGS] ``` -------------------------------- ### Training Swin Transformer Model Source: https://github.com/open-mmlab/mmpretrain/blob/main/configs/swin_transformer/README.md Command to start training a Swin Transformer model. Requires dataset preparation according to the provided documentation link. ```shell python tools/train.py configs/swin_transformer/swin-tiny_16xb64_in1k.py ``` -------------------------------- ### Inherit and Modify Configuration Files Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/user_guides/config.md Demonstrates how to inherit a base configuration file and override specific parameters such as training epochs, data augmentation, and dataset paths. ```python _base_ = './resnet50_8xb32_in1k.py' model = dict( train_cfg=dict( augments=dict(type='CutMix', alpha=1.0) ) ) train_cfg = dict(max_epochs=300, val_interval=10) param_scheduler = dict(step=[150, 200, 250]) train_dataloader = dict( dataset=dict(data_root='mydata/imagenet/train'), ) val_dataloader = dict( batch_size=64, dataset=dict(data_root='mydata/imagenet/val'), ) test_dataloader = dict( batch_size=64, dataset=dict(data_root='mydata/imagenet/val'), ) ``` -------------------------------- ### SimMIM Training and Testing CLI Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/zh_CN/papers/simmim.md Command line interface instructions for training and testing SimMIM models using configuration files. ```APIDOC ## Training and Testing CLI ### Description Use the provided training and testing scripts to fine-tune or evaluate SimMIM models on specific datasets. ### Method CLI (Shell) ### Parameters #### Command Arguments - **config_path** (string) - Required - Path to the model configuration file. - **checkpoint_url** (string) - Optional - URL to the pre-trained model weights for testing. ### Request Example ```shell # Training python tools/train.py configs/simmim/simmim_swin-base-w6_8xb256-amp-coslr-100e_in1k-192px.py # Testing python tools/test.py [config_path] [checkpoint_url] ``` ``` -------------------------------- ### Use Custom Transform in Config File (Python) Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/advanced_guides/pipeline.md This example illustrates how to incorporate a custom data transform into a training pipeline configuration file by specifying its type. ```python train_pipeline = [ ... dict(type='MyTransform'), ... ] ``` -------------------------------- ### Visualize Dataset with Browse Tool Source: https://github.com/open-mmlab/mmpretrain/blob/main/docs/en/useful_tools/dataset_visualization.md This script visualizes dataset samples using various modes like 'original', 'transformed', 'concat', and 'pipeline'. It takes a configuration file as input and allows customization of output directory, number of images, and display options. Dependencies include Python and the MMPretrain environment. ```bash python tools/visualization/browse_dataset.py \ ${CONFIG_FILE} \ [-o, --output-dir ${OUTPUT_DIR}] \ [-p, --phase ${DATASET_PHASE}] \ [-n, --show-number ${NUMBER_IMAGES_DISPLAY}] \ [-i, --show-interval ${SHOW_INTERRVAL}] \ [-m, --mode ${DISPLAY_MODE}] \ [-r, --rescale-factor ${RESCALE_FACTOR}] \ [-c, --channel-order ${CHANNEL_ORDER}] \ [--cfg-options ${CFG_OPTIONS}] ``` ```shell python ./tools/visualization/browse_dataset.py ./configs/resnet/resnet101_8xb16_cifar10.py --phase val --output-dir tmp --mode original --show-number 100 --rescale-factor 10 --channel-order RGB ``` ```shell python ./tools/visualization/browse_dataset.py ./configs/resnet/resnet50_8xb32_in1k.py -n 100 ``` ```shell python ./tools/visualization/browse_dataset.py configs/swin_transformer/swin-small_16xb64_in1k.py -n 10 -m concat ``` ```shell python ./tools/visualization/browse_dataset.py configs/swin_transformer/swin-small_16xb64_in1k.py -m pipeline ``` ```shell python ./tools/visualization/browse_dataset.py configs/beit/beit_beit-base-p16_8xb256-amp-coslr-300e_in1k.py -m pipeline ```