From 5036dc59dfaeea75932c656525b600861b34786a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Haian=20Huang=28=E6=B7=B1=E5=BA=A6=E7=9C=B8=29?= <1286304229@qq.com> Date: Wed, 28 Jun 2023 13:53:54 +0800 Subject: [PATCH] replace mmcls with mmpretrain (#10545) --- .circleci/test.yml | 18 ++++---- .dev_scripts/benchmark_options.py | 2 +- .github/pull_request_template.md | 2 +- configs/convnext/README.md | 4 +- ...7_fpn_4conv1fc-giou_amp-ms-crop-3x_coco.py | 10 ++--- ...7_fpn_4conv1fc-giou_amp-ms-crop-3x_coco.py | 10 ++--- ...onvnext-t-p4-w7_fpn_amp-ms-crop-3x_coco.py | 10 ++--- ...xb2-4e_mot17halftrain_test-mot17halfval.py | 6 +-- configs/reid/README.md | 2 +- ...0_8xb32-6e_mot17train80_test-mot17val20.py | 4 +- configs/rtmdet/classification/README.md | 8 ++-- .../cspnext-s_8xb256-rsb-a1-600e_in1k.py | 6 +-- ...dhuman-mot17halftrain_test-mot17halfval.py | 6 +-- configs/timm_example/README.md | 10 ++--- ...inanet_timm-efficientnet-b1_fpn_1x_coco.py | 10 ++--- .../retinanet_timm-tv-resnet50_fpn_1x_coco.py | 10 ++--- docs/en/advanced_guides/how_to.md | 32 +++++++------- docs/en/user_guides/deploy.md | 2 +- docs/zh_cn/advanced_guides/how_to.md | 32 +++++++------- docs/zh_cn/user_guides/deploy.md | 2 +- mmdet/models/backbones/efficientnet.py | 6 +-- mmdet/models/backbones/pvt.py | 2 +- .../reid_data_preprocessor.py | 43 ++++++++++++++----- mmdet/models/reid/base_reid.py | 12 +++--- mmdet/models/reid/linear_reid_head.py | 12 +++--- projects/ConvNeXt-V2/README.md | 6 +-- ...cnn_convnext-v2-b_fpn_lsj-3x-fcmae_coco.py | 9 ++-- requirements/tracking.txt | 4 +- .../test_reid/test_linear_reid_head.py | 2 +- 29 files changed, 152 insertions(+), 130 deletions(-) diff --git a/.circleci/test.yml b/.circleci/test.yml index 1b11955459c..b20f63ab28e 100644 --- a/.circleci/test.yml +++ b/.circleci/test.yml @@ -74,6 +74,7 @@ jobs: pip install -r requirements/tests.txt -r requirements/optional.txt pip install --force-reinstall pycocotools pip install albumentations>=0.3.2 --no-binary imgaug,albumentations + pip install -r requirements/tracking.txt pip install git+https://github.com/cocodataset/panopticapi.git pip install git+https://github.com/JonathonLuiten/TrackEval.git - run: @@ -93,10 +94,10 @@ jobs: type: string cuda: type: enum - enum: ["10.1", "10.2", "11.1", "11.7"] + enum: ["11.1", "11.7"] cudnn: type: integer - default: 7 + default: 8 machine: image: ubuntu-2004-cuda-11.4:202110-01 # docker_layer_caching: true @@ -123,6 +124,7 @@ jobs: docker exec mmdetection pip install -r requirements/tests.txt -r requirements/optional.txt docker exec mmdetection pip install pycocotools docker exec mmdetection pip install albumentations>=0.3.2 --no-binary imgaug,albumentations + docker exec mmdetection pip install -r requirements/tracking.txt docker exec mmdetection pip install git+https://github.com/cocodataset/panopticapi.git docker exec mmdetection pip install git+https://github.com/JonathonLuiten/TrackEval.git docker exec mmdetection python -c 'import mmcv; print(mmcv.__version__)' @@ -157,9 +159,9 @@ workflows: - dev-3.x - build_cpu: name: minimum_version_cpu - torch: 1.7.1 - torchvision: 0.8.2 - python: 3.7.4 # The lowest python 3.7.x version available on CircleCI images + torch: 1.8.0 + torchvision: 0.9.0 + python: 3.7.16 requires: - lint - build_cpu: @@ -178,7 +180,7 @@ workflows: torch: 1.8.1 # Use double quotation mark to explicitly specify its type # as string instead of number - cuda: "10.2" + cuda: "11.1" requires: - hold - build_cuda: @@ -194,8 +196,8 @@ workflows: jobs: - build_cuda: name: minimum_version_gpu - torch: 1.6.0 - cuda: "10.1" + torch: 1.8.0 + cuda: "11.1" filters: branches: only: diff --git a/.dev_scripts/benchmark_options.py b/.dev_scripts/benchmark_options.py index ee81a9c08f0..cdb1f87d792 100644 --- a/.dev_scripts/benchmark_options.py +++ b/.dev_scripts/benchmark_options.py @@ -5,7 +5,7 @@ 'pip install instaboostfast', 'pip install git+https://github.com/cocodataset/panopticapi.git', 'pip install timm', - 'pip install mmcls>=1.0.0rc0', + 'pip install mmpretrain', 'pip install git+https://github.com/lvis-dataset/lvis-api.git', 'pip install -r ../requirements/multimodal.txt', 'pip install -r ../requirements/tracking.txt', diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 8f8e28983ff..7a9f0d901f4 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -21,5 +21,5 @@ If this PR introduces a new feature, it is better to list some use cases here, a 1. Pre-commit or other linting tools are used to fix the potential lint issues. 2. The modification is covered by complete unit tests. If not, please add more unit test to ensure the correctness. -3. If the modification has potential influence on downstream projects, this PR should be tested with downstream projects, like MMDet or MMCls. +3. If the modification has potential influence on downstream projects, this PR should be tested with downstream projects, like MMDet or MMPreTrain. 4. The documentation has been modified accordingly, like docstring or example tutorials. diff --git a/configs/convnext/README.md b/configs/convnext/README.md index 44b14205c31..8764327dc69 100644 --- a/configs/convnext/README.md +++ b/configs/convnext/README.md @@ -20,10 +20,10 @@ The "Roaring 20s" of visual recognition began with the introduction of Vision Tr **Note**: -- ConvNeXt backbone needs to install [MMClassification](https://github.com/open-mmlab/mmclassification) first, which has abundant backbones for downstream tasks. +- ConvNeXt backbone needs to install [MMPreTrain](https://github.com/open-mmlab/mmpretrain) first, which has abundant backbones for downstream tasks. ```shell -pip install mmcls>=1.0 +pip install mmpretrain ``` - The performance is unstable. `Cascade Mask R-CNN` may fluctuate about 0.2 mAP. diff --git a/configs/convnext/cascade-mask-rcnn_convnext-s-p4-w7_fpn_4conv1fc-giou_amp-ms-crop-3x_coco.py b/configs/convnext/cascade-mask-rcnn_convnext-s-p4-w7_fpn_4conv1fc-giou_amp-ms-crop-3x_coco.py index 465209d3863..9a5fbedcaa7 100644 --- a/configs/convnext/cascade-mask-rcnn_convnext-s-p4-w7_fpn_4conv1fc-giou_amp-ms-crop-3x_coco.py +++ b/configs/convnext/cascade-mask-rcnn_convnext-s-p4-w7_fpn_4conv1fc-giou_amp-ms-crop-3x_coco.py @@ -1,15 +1,15 @@ _base_ = './cascade-mask-rcnn_convnext-t-p4-w7_fpn_4conv1fc-giou_amp-ms-crop-3x_coco.py' # noqa -# TODO: delete custom_imports after mmcls supports auto import -# please install mmcls>=1.0 -# import mmcls.models to trigger register_module in mmcls -custom_imports = dict(imports=['mmcls.models'], allow_failed_imports=False) +# please install mmpretrain +# import mmpretrain.models to trigger register_module in mmpretrain +custom_imports = dict( + imports=['mmpretrain.models'], allow_failed_imports=False) checkpoint_file = 'https://download.openmmlab.com/mmclassification/v0/convnext/downstream/convnext-small_3rdparty_32xb128-noema_in1k_20220301-303e75e3.pth' # noqa model = dict( backbone=dict( _delete_=True, - type='mmcls.ConvNeXt', + type='mmpretrain.ConvNeXt', arch='small', out_indices=[0, 1, 2, 3], drop_path_rate=0.6, diff --git a/configs/convnext/cascade-mask-rcnn_convnext-t-p4-w7_fpn_4conv1fc-giou_amp-ms-crop-3x_coco.py b/configs/convnext/cascade-mask-rcnn_convnext-t-p4-w7_fpn_4conv1fc-giou_amp-ms-crop-3x_coco.py index 1e031e90d52..c92f86838c3 100644 --- a/configs/convnext/cascade-mask-rcnn_convnext-t-p4-w7_fpn_4conv1fc-giou_amp-ms-crop-3x_coco.py +++ b/configs/convnext/cascade-mask-rcnn_convnext-t-p4-w7_fpn_4conv1fc-giou_amp-ms-crop-3x_coco.py @@ -4,16 +4,16 @@ '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] -# TODO: delete custom_imports after mmcls supports auto import -# please install mmcls>=1.0 -# import mmcls.models to trigger register_module in mmcls -custom_imports = dict(imports=['mmcls.models'], allow_failed_imports=False) +# please install mmpretrain +# import mmpretrain.models to trigger register_module in mmpretrain +custom_imports = dict( + imports=['mmpretrain.models'], allow_failed_imports=False) checkpoint_file = 'https://download.openmmlab.com/mmclassification/v0/convnext/downstream/convnext-tiny_3rdparty_32xb128-noema_in1k_20220301-795e9634.pth' # noqa model = dict( backbone=dict( _delete_=True, - type='mmcls.ConvNeXt', + type='mmpretrain.ConvNeXt', arch='tiny', out_indices=[0, 1, 2, 3], drop_path_rate=0.4, diff --git a/configs/convnext/mask-rcnn_convnext-t-p4-w7_fpn_amp-ms-crop-3x_coco.py b/configs/convnext/mask-rcnn_convnext-t-p4-w7_fpn_amp-ms-crop-3x_coco.py index 23d46e289eb..5792b5b5c5a 100644 --- a/configs/convnext/mask-rcnn_convnext-t-p4-w7_fpn_amp-ms-crop-3x_coco.py +++ b/configs/convnext/mask-rcnn_convnext-t-p4-w7_fpn_amp-ms-crop-3x_coco.py @@ -4,16 +4,16 @@ '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] -# TODO: delete custom_imports after mmcls supports auto import -# please install mmcls>=1.0 -# import mmcls.models to trigger register_module in mmcls -custom_imports = dict(imports=['mmcls.models'], allow_failed_imports=False) +# please install mmpretrain +# import mmpretrain.models to trigger register_module in mmpretrain +custom_imports = dict( + imports=['mmpretrain.models'], allow_failed_imports=False) checkpoint_file = 'https://download.openmmlab.com/mmclassification/v0/convnext/downstream/convnext-tiny_3rdparty_32xb128-noema_in1k_20220301-795e9634.pth' # noqa model = dict( backbone=dict( _delete_=True, - type='mmcls.ConvNeXt', + type='mmpretrain.ConvNeXt', arch='tiny', out_indices=[0, 1, 2, 3], drop_path_rate=0.4, diff --git a/configs/deepsort/deepsort_faster-rcnn_r50_fpn_8xb2-4e_mot17halftrain_test-mot17halfval.py b/configs/deepsort/deepsort_faster-rcnn_r50_fpn_8xb2-4e_mot17halftrain_test-mot17halfval.py index fb0f7cb9f28..70d3393829b 100644 --- a/configs/deepsort/deepsort_faster-rcnn_r50_fpn_8xb2-4e_mot17halftrain_test-mot17halfval.py +++ b/configs/deepsort/deepsort_faster-rcnn_r50_fpn_8xb2-4e_mot17halftrain_test-mot17halfval.py @@ -40,9 +40,9 @@ detector=detector, reid=dict( type='BaseReID', - data_preprocessor=None, + data_preprocessor=dict(type='mmpretrain.ClsDataPreprocessor'), backbone=dict( - type='mmcls.ResNet', + type='mmpretrain.ResNet', depth=50, num_stages=4, out_indices=(3, ), @@ -55,7 +55,7 @@ fc_channels=1024, out_channels=128, num_classes=380, - loss_cls=dict(type='mmcls.CrossEntropyLoss', loss_weight=1.0), + loss_cls=dict(type='mmpretrain.CrossEntropyLoss', loss_weight=1.0), loss_triplet=dict(type='TripletLoss', margin=0.3, loss_weight=1.0), norm_cfg=dict(type='BN1d'), act_cfg=dict(type='ReLU')), diff --git a/configs/reid/README.md b/configs/reid/README.md index f033b8d51b0..a5bfe5ec499 100644 --- a/configs/reid/README.md +++ b/configs/reid/README.md @@ -1,6 +1,6 @@ # Training a ReID Model -You may want to train a ReID model for multiple object tracking or other applications. We support ReID model training in MMDetection, which is built upon [MMClassification](https://github.com/open-mmlab/mmclassification). +You may want to train a ReID model for multiple object tracking or other applications. We support ReID model training in MMDetection, which is built upon [MMPretrain](https://github.com/open-mmlab/mmpretrain). ### 1. Development Environment Setup diff --git a/configs/reid/reid_r50_8xb32-6e_mot17train80_test-mot17val20.py b/configs/reid/reid_r50_8xb32-6e_mot17train80_test-mot17val20.py index 7e315d8a2de..83669de7c17 100644 --- a/configs/reid/reid_r50_8xb32-6e_mot17train80_test-mot17val20.py +++ b/configs/reid/reid_r50_8xb32-6e_mot17train80_test-mot17val20.py @@ -9,7 +9,7 @@ std=[58.395, 57.12, 57.375], to_rgb=True), backbone=dict( - type='mmcls.ResNet', + type='mmpretrain.ResNet', depth=50, num_stages=4, out_indices=(3, ), @@ -22,7 +22,7 @@ fc_channels=1024, out_channels=128, num_classes=380, - loss_cls=dict(type='mmcls.CrossEntropyLoss', loss_weight=1.0), + loss_cls=dict(type='mmpretrain.CrossEntropyLoss', loss_weight=1.0), loss_triplet=dict(type='TripletLoss', margin=0.3, loss_weight=1.0), norm_cfg=dict(type='BN1d'), act_cfg=dict(type='ReLU')), diff --git a/configs/rtmdet/classification/README.md b/configs/rtmdet/classification/README.md index 6aee2c61794..acc127db2ca 100644 --- a/configs/rtmdet/classification/README.md +++ b/configs/rtmdet/classification/README.md @@ -4,23 +4,23 @@ In this folder, we provide the imagenet pre-training config of RTMDet's backbone ## Requirements -To train with these configs, please install [MMClassification 1.x](https://github.com/open-mmlab/mmclassification/tree/1.x) first. +To train with these configs, please install [MMPreTrain](https://github.com/open-mmlab/mmpretrain) first. Install by MIM: ```shell -mim install mmcls>=1.0.0rc0 +mim install mmpretrain ``` or install by pip: ```shell -pip install mmcls>=1.0.0rc0 +pip install mmpretrain ``` ## Prepare Dataset -To pre-train on ImageNet, you need to prepare the dataset first. Please refer to the [guide](https://mmclassification.readthedocs.io/en/1.x/user_guides/dataset_prepare.html#imagenet). +To pre-train on ImageNet, you need to prepare the dataset first. Please refer to the [guide](https://mmpretrain.readthedocs.io/en/latest/user_guides/dataset_prepare.html#imagenet). ## How to Train diff --git a/configs/rtmdet/classification/cspnext-s_8xb256-rsb-a1-600e_in1k.py b/configs/rtmdet/classification/cspnext-s_8xb256-rsb-a1-600e_in1k.py index 5708a45e632..dcfd2ea47d5 100644 --- a/configs/rtmdet/classification/cspnext-s_8xb256-rsb-a1-600e_in1k.py +++ b/configs/rtmdet/classification/cspnext-s_8xb256-rsb-a1-600e_in1k.py @@ -1,7 +1,7 @@ _base_ = [ - 'mmcls::_base_/datasets/imagenet_bs256_rsb_a12.py', - 'mmcls::_base_/schedules/imagenet_bs2048_rsb.py', - 'mmcls::_base_/default_runtime.py' + 'mmpretrain::_base_/datasets/imagenet_bs256_rsb_a12.py', + 'mmpretrain::_base_/schedules/imagenet_bs2048_rsb.py', + 'mmpretrain::_base_/default_runtime.py' ] model = dict( diff --git a/configs/strongsort/strongsort_yolox_x_8xb4-80e_crowdhuman-mot17halftrain_test-mot17halfval.py b/configs/strongsort/strongsort_yolox_x_8xb4-80e_crowdhuman-mot17halftrain_test-mot17halfval.py index e37c1f9fcb5..532e2aee718 100644 --- a/configs/strongsort/strongsort_yolox_x_8xb4-80e_crowdhuman-mot17halftrain_test-mot17halfval.py +++ b/configs/strongsort/strongsort_yolox_x_8xb4-80e_crowdhuman-mot17halftrain_test-mot17halfval.py @@ -22,9 +22,9 @@ detector=detector, reid=dict( type='BaseReID', - data_preprocessor=None, + data_preprocessor=dict(type='mmpretrain.ClsDataPreprocessor'), backbone=dict( - type='mmcls.ResNet', + type='mmpretrain.ResNet', depth=50, num_stages=4, out_indices=(3, ), @@ -37,7 +37,7 @@ fc_channels=1024, out_channels=128, num_classes=380, - loss_cls=dict(type='mmcls.CrossEntropyLoss', loss_weight=1.0), + loss_cls=dict(type='mmpretrain.CrossEntropyLoss', loss_weight=1.0), loss_triplet=dict(type='TripletLoss', margin=0.3, loss_weight=1.0), norm_cfg=dict(type='BN1d'), act_cfg=dict(type='ReLU'))), diff --git a/configs/timm_example/README.md b/configs/timm_example/README.md index b4e45b0a6b0..848f8d3c269 100644 --- a/configs/timm_example/README.md +++ b/configs/timm_example/README.md @@ -27,22 +27,22 @@ Py**T**orch **Im**age **M**odels (`timm`) is a collection of image models, layer ### Install additional requirements -MMDetection supports timm backbones via `TIMMBackbone`, a wrapper class in MMClassification. -Thus, you need to install `mmcls` in addition to timm. +MMDetection supports timm backbones via `TIMMBackbone`, a wrapper class in MMPretrain. +Thus, you need to install `mmpretrain` in addition to timm. If you have already installed requirements for mmdet, run ```shell pip install 'dataclasses; python_version<"3.7"' pip install timm -pip install 'mmcls>=0.20.0' +pip install mmpretrain ``` -See [this document](https://mmclassification.readthedocs.io/en/latest/install.html) for the details of MMClassification installation. +See [this document](https://mmpretrain.readthedocs.io/en/latest/get_started.html#installation) for the details of MMPretrain installation. ### Edit config - See example configs for basic usage. -- See the documents of [timm feature extraction](https://rwightman.github.io/pytorch-image-models/feature_extraction/#multi-scale-feature-maps-feature-pyramid) and [TIMMBackbone](https://mmclassification.readthedocs.io/en/latest/api.html#mmcls.models.backbones.TIMMBackbone) for details. +- See the documents of [timm feature extraction](https://rwightman.github.io/pytorch-image-models/feature_extraction/#multi-scale-feature-maps-feature-pyramid) and [TIMMBackbone](https://mmpretrain.readthedocs.io/en/latest/api/generated/mmpretrain.models.backbones.TIMMBackbone.html#mmpretrain.models.backbones.TIMMBackbone) for details. - Which feature map is output depends on the backbone. Please check `backbone out_channels` and `backbone out_strides` in your log, and modify `model.neck.in_channels` and `model.backbone.out_indices` if necessary. - If you use Vision Transformer models that do not support `features_only=True`, add `custom_hooks = []` to your config to disable `NumClassCheckHook`. diff --git a/configs/timm_example/retinanet_timm-efficientnet-b1_fpn_1x_coco.py b/configs/timm_example/retinanet_timm-efficientnet-b1_fpn_1x_coco.py index 433cf5c7bda..b87dddf50f7 100644 --- a/configs/timm_example/retinanet_timm-efficientnet-b1_fpn_1x_coco.py +++ b/configs/timm_example/retinanet_timm-efficientnet-b1_fpn_1x_coco.py @@ -4,15 +4,15 @@ '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] -# TODO: delete custom_imports after mmcls supports auto import -# please install mmcls>=1.0 -# import mmcls.models to trigger register_module in mmcls -custom_imports = dict(imports=['mmcls.models'], allow_failed_imports=False) +# please install mmpretrain +# import mmpretrain.models to trigger register_module in mmpretrain +custom_imports = dict( + imports=['mmpretrain.models'], allow_failed_imports=False) model = dict( backbone=dict( _delete_=True, - type='mmcls.TIMMBackbone', + type='mmpretrain.TIMMBackbone', model_name='efficientnet_b1', features_only=True, pretrained=True, diff --git a/configs/timm_example/retinanet_timm-tv-resnet50_fpn_1x_coco.py b/configs/timm_example/retinanet_timm-tv-resnet50_fpn_1x_coco.py index 315284b5074..74e43506959 100644 --- a/configs/timm_example/retinanet_timm-tv-resnet50_fpn_1x_coco.py +++ b/configs/timm_example/retinanet_timm-tv-resnet50_fpn_1x_coco.py @@ -4,15 +4,15 @@ '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] -# TODO: delete custom_imports after mmcls supports auto import -# please install mmcls>=1.0 -# import mmcls.models to trigger register_module in mmcls -custom_imports = dict(imports=['mmcls.models'], allow_failed_imports=False) +# please install mmpretrain +# import mmpretrain.models to trigger register_module in mmpretrain +custom_imports = dict( + imports=['mmpretrain.models'], allow_failed_imports=False) model = dict( backbone=dict( _delete_=True, - type='mmcls.TIMMBackbone', + type='mmpretrain.TIMMBackbone', model_name='tv_resnet50', # ResNet-50 with torchvision weights features_only=True, pretrained=True, diff --git a/docs/en/advanced_guides/how_to.md b/docs/en/advanced_guides/how_to.md index 8b19fc9db5b..7eb41ceeb7a 100644 --- a/docs/en/advanced_guides/how_to.md +++ b/docs/en/advanced_guides/how_to.md @@ -1,10 +1,10 @@ This tutorial collects answers to any `How to xxx with MMDetection`. Feel free to update this doc if you meet new questions about `How to` and find the answers! -# Use backbone network through MMClassification +# Use backbone network through MMPretrain -The model registry in MMDet, MMCls, MMSeg all inherit from the root registry in MMEngine. This allows these repositories to directly use the modules already implemented by each other. Therefore, users can use backbone networks from MMClassification in MMDetection without implementing a network that already exists in MMClassification. +The model registry in MMDet, MMPreTrain, MMSeg all inherit from the root registry in MMEngine. This allows these repositories to directly use the modules already implemented by each other. Therefore, users can use backbone networks from MMPretrain in MMDetection without implementing a network that already exists in MMPretrain. -## Use backbone network implemented in MMClassification +## Use backbone network implemented in MMPretrain Suppose you want to use `MobileNetV3-small` as the backbone network of `RetinaNet`, the example config is as the following. @@ -14,27 +14,27 @@ _base_ = [ '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] -# please install mmcls>=1.0.0rc0 -# import mmcls.models to trigger register_module in mmcls -custom_imports = dict(imports=['mmcls.models'], allow_failed_imports=False) +# please install mmpretrain +# import mmpretrain.models to trigger register_module in mmpretrain +custom_imports = dict(imports=['mmpretrain.models'], allow_failed_imports=False) pretrained = 'https://download.openmmlab.com/mmclassification/v0/mobilenet_v3/convert/mobilenet_v3_small-8427ecf0.pth' model = dict( backbone=dict( _delete_=True, # Delete the backbone field in _base_ - type='mmcls.MobileNetV3', # Using MobileNetV3 from mmcls + type='mmpretrain.MobileNetV3', # Using MobileNetV3 from mmpretrain arch='small', out_indices=(3, 8, 11), # Modify out_indices init_cfg=dict( type='Pretrained', checkpoint=pretrained, - prefix='backbone.')), # The pre-trained weights of backbone network in MMCls have prefix='backbone.'. The prefix in the keys will be removed so that these weights can be normally loaded. + prefix='backbone.')), # The pre-trained weights of backbone network in mmpretrain have prefix='backbone.'. The prefix in the keys will be removed so that these weights can be normally loaded. # Modify in_channels neck=dict(in_channels=[24, 48, 96], start_level=0)) ``` -## Use backbone network in TIMM through MMClassification +## Use backbone network in TIMM through MMPretrain -MMClassification also provides a wrapper for the PyTorch Image Models (timm) backbone network, users can directly use the backbone network in timm through MMClassification. Suppose you want to use [EfficientNet-B1](../../../configs/timm_example/retinanet_timm-efficientnet-b1_fpn_1x_coco.py) as the backbone network of RetinaNet, the example config is as the following. +MMPretrain also provides a wrapper for the PyTorch Image Models (timm) backbone network, users can directly use the backbone network in timm through MMPretrain. Suppose you want to use [EfficientNet-B1](../../../configs/timm_example/retinanet_timm-efficientnet-b1_fpn_1x_coco.py) as the backbone network of RetinaNet, the example config is as the following. ```python # https://github.com/open-mmlab/mmdetection/blob/main/configs/timm_example/retinanet_timm-efficientnet-b1_fpn_1x_coco.py @@ -45,13 +45,13 @@ _base_ = [ '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] -# please install mmcls>=1.0.0rc0 -# import mmcls.models to trigger register_module in mmcls -custom_imports = dict(imports=['mmcls.models'], allow_failed_imports=False) +# please install mmpretrain +# import mmpretrain.models to trigger register_module in mmpretrain +custom_imports = dict(imports=['mmpretrain.models'], allow_failed_imports=False) model = dict( backbone=dict( _delete_=True, # Delete the backbone field in _base_ - type='mmcls.TIMMBackbone', # Using timm from mmcls + type='mmpretrain.TIMMBackbone', # Using timm from mmpretrain model_name='efficientnet_b1', features_only=True, pretrained=True, @@ -61,9 +61,9 @@ model = dict( optimizer = dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0001) ``` -`type='mmcls.TIMMBackbone'` means use the `TIMMBackbone` class from MMClassification in MMDetection, and the model used is `EfficientNet-B1`, where `mmcls` means the MMClassification repo and `TIMMBackbone` means the TIMMBackbone wrapper implemented in MMClassification. +`type='mmpretrain.TIMMBackbone'` means use the `TIMMBackbone` class from MMPretrain in MMDetection, and the model used is `EfficientNet-B1`, where `mmpretrain` means the MMPretrain repo and `TIMMBackbone` means the TIMMBackbone wrapper implemented in MMPretrain. -For the principle of the Hierarchy Registry, please refer to the [MMEngine document](https://github.com/open-mmlab/mmengine/blob/main/docs/en/tutorials/config.md). For how to use other backbones in MMClassification, you can refer to the [MMClassification document](https://github.com/open-mmlab/mmclassification/blob/dev-1.x/docs/en/tutorials/config.md). +For the principle of the Hierarchy Registry, please refer to the [MMEngine document](https://github.com/open-mmlab/mmengine/blob/main/docs/en/tutorials/config.md). For how to use other backbones in MMPretrain, you can refer to the [MMPretrain document](https://mmpretrain.readthedocs.io/en/latest/user_guides/config.html). # Use Mosaic augmentation diff --git a/docs/en/user_guides/deploy.md b/docs/en/user_guides/deploy.md index 94c078882e3..db320d1409e 100644 --- a/docs/en/user_guides/deploy.md +++ b/docs/en/user_guides/deploy.md @@ -1,6 +1,6 @@ # Model Deployment -The deployment of OpenMMLab codebases, including MMDetection, MMClassification and so on are supported by [MMDeploy](https://github.com/open-mmlab/mmdeploy). +The deployment of OpenMMLab codebases, including MMDetection, MMPretrain and so on are supported by [MMDeploy](https://github.com/open-mmlab/mmdeploy). The latest deployment guide for MMDetection can be found from [here](https://mmdeploy.readthedocs.io/en/dev-1.x/04-supported-codebases/mmdet.html). This tutorial is organized as follows: diff --git a/docs/zh_cn/advanced_guides/how_to.md b/docs/zh_cn/advanced_guides/how_to.md index 8fede40cfd3..6705dafdeab 100644 --- a/docs/zh_cn/advanced_guides/how_to.md +++ b/docs/zh_cn/advanced_guides/how_to.md @@ -1,10 +1,10 @@ 本教程收集了任何如何使用 MMDetection 进行 xxx 的答案。 如果您遇到有关`如何做`的问题及答案,请随时更新此文档! -## 使用 MMClassification 的骨干网络 +## 使用 MMPretrain 的骨干网络 -MMDet、MMCls、MMSeg 中的模型注册表都继承自 MMEngine 中的根注册表,允许这些存储库直接使用彼此已经实现的模块。 因此用户可以在 MMDetection 中使用来自 MMClassification 的骨干网络,而无需实现MMClassification 中已经存在的网络。 +MMDet、MMPretrain、MMSeg 中的模型注册表都继承自 MMEngine 中的根注册表,允许这些存储库直接使用彼此已经实现的模块。 因此用户可以在 MMDetection 中使用来自 MMPretrain 的骨干网络,而无需实现MMPretrain 中已经存在的网络。 -### 使用在 MMClassification 中实现的骨干网络 +### 使用在 MMPretrain 中实现的骨干网络 假设想将 `MobileNetV3-small` 作为 `RetinaNet` 的骨干网络,则配置文件如下。 @@ -14,27 +14,27 @@ _base_ = [ '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] -# please install mmcls>=1.0.0rc0 -# import mmcls.models to trigger register_module in mmcls -custom_imports = dict(imports=['mmcls.models'], allow_failed_imports=False) +# please install mmpretrain +# import mmpretrain.models to trigger register_module in mmpretrain +custom_imports = dict(imports=['mmpretrain.models'], allow_failed_imports=False) pretrained = 'https://download.openmmlab.com/mmclassification/v0/mobilenet_v3/convert/mobilenet_v3_small-8427ecf0.pth' model = dict( backbone=dict( _delete_=True, # 将 _base_ 中关于 backbone 的字段删除 - type='mmcls.MobileNetV3', # 使用 mmcls 中的 MobileNetV3 + type='mmpretrain.MobileNetV3', # 使用 mmpretrain 中的 MobileNetV3 arch='small', out_indices=(3, 8, 11), # 修改 out_indices init_cfg=dict( type='Pretrained', checkpoint=pretrained, - prefix='backbone.')), # MMCls 中骨干网络的预训练权重含义 prefix='backbone.',为了正常加载权重,需要把这个 prefix 去掉。 + prefix='backbone.')), # mmpretrain 中骨干网络的预训练权重含义 prefix='backbone.',为了正常加载权重,需要把这个 prefix 去掉。 # 修改 in_channels neck=dict(in_channels=[24, 48, 96], start_level=0)) ``` -### 通过 MMClassification 使用 TIMM 中实现的骨干网络 +### 通过 MMPretrain 使用 TIMM 中实现的骨干网络 -由于 MMClassification 提供了 Py**T**orch **Im**age **M**odels (`timm`) 骨干网络的封装,用户也可以通过 MMClassification 直接使用 `timm` 中的骨干网络。假设想将 [`EfficientNet-B1`](../../../configs/timm_example/retinanet_timm-efficientnet-b1_fpn_1x_coco.py) 作为 `RetinaNet` 的骨干网络,则配置文件如下。 +由于 MMPretrain 提供了 Py**T**orch **Im**age **M**odels (`timm`) 骨干网络的封装,用户也可以通过 MMPretrain 直接使用 `timm` 中的骨干网络。假设想将 [`EfficientNet-B1`](../../../configs/timm_example/retinanet_timm-efficientnet-b1_fpn_1x_coco.py) 作为 `RetinaNet` 的骨干网络,则配置文件如下。 ```python # https://github.com/open-mmlab/mmdetection/blob/main/configs/timm_example/retinanet_timm_efficientnet_b1_fpn_1x_coco.py @@ -44,13 +44,13 @@ _base_ = [ '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] -# please install mmcls>=1.0.0rc0 -# import mmcls.models to trigger register_module in mmcls -custom_imports = dict(imports=['mmcls.models'], allow_failed_imports=False) +# please install mmpretrain +# import mmpretrain.models to trigger register_module in mmpretrain +custom_imports = dict(imports=['mmpretrain.models'], allow_failed_imports=False) model = dict( backbone=dict( _delete_=True, # 将 _base_ 中关于 backbone 的字段删除 - type='mmcls.TIMMBackbone', # 使用 mmcls 中 timm 骨干网络 + type='mmpretrain.TIMMBackbone', # 使用 mmpretrain 中 timm 骨干网络 model_name='efficientnet_b1', features_only=True, pretrained=True, @@ -60,9 +60,9 @@ model = dict( optimizer = dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0001) ``` -`type='mmcls.TIMMBackbone'` 表示在 MMDetection 中使用 MMClassification 中的 `TIMMBackbone` 类,并且使用的模型为` EfficientNet-B1`,其中 `mmcls` 表示 MMClassification 库,而 `TIMMBackbone ` 表示 MMClassification 中实现的 TIMMBackbone 包装器。 +`type='mmpretrain.TIMMBackbone'` 表示在 MMDetection 中使用 MMPretrain 中的 `TIMMBackbone` 类,并且使用的模型为` EfficientNet-B1`,其中 `mmpretrain` 表示 MMPretrain 库,而 `TIMMBackbone ` 表示 MMPretrain 中实现的 TIMMBackbone 包装器。 -关于层次注册器的具体原理可以参考 [MMEngine 文档](https://mmengine.readthedocs.io/zh_cn/latest/tutorials/config.md#跨项目继承配置文件),关于如何使用 MMClassification 中的其他 backbone,可以参考 [MMClassification 文档](https://github.com/open-mmlab/mmclassification/blob/dev-1.x/docs/en/tutorials/config.md)。 +关于层次注册器的具体原理可以参考 [MMEngine 文档](https://mmengine.readthedocs.io/zh_cn/latest/tutorials/config.md#跨项目继承配置文件),关于如何使用 MMPretrain 中的其他 backbone,可以参考 [MMPretrain 文档](https://mmpretrain.readthedocs.io/en/latest/user_guides/config.html)。 ## 使用马赛克数据增强 diff --git a/docs/zh_cn/user_guides/deploy.md b/docs/zh_cn/user_guides/deploy.md index da2e7f68241..f796b004f0b 100644 --- a/docs/zh_cn/user_guides/deploy.md +++ b/docs/zh_cn/user_guides/deploy.md @@ -1,6 +1,6 @@ # 模型部署 -[MMDeploy](https://github.com/open-mmlab/mmdeploy) 是 OpenMMLab 的部署仓库,负责包括 MMClassification、MMDetection 等在内的各算法库的部署工作。 +[MMDeploy](https://github.com/open-mmlab/mmdeploy) 是 OpenMMLab 的部署仓库,负责包括 MMPretrain、MMDetection 等在内的各算法库的部署工作。 你可以从[这里](https://mmdeploy.readthedocs.io/zh_CN/1.x/04-supported-codebases/mmdet.html)获取 MMDeploy 对 MMDetection 部署支持的最新文档。 本文的结构如下: diff --git a/mmdet/models/backbones/efficientnet.py b/mmdet/models/backbones/efficientnet.py index 5d3e35b0934..8484afe2e34 100644 --- a/mmdet/models/backbones/efficientnet.py +++ b/mmdet/models/backbones/efficientnet.py @@ -351,7 +351,7 @@ def make_layer(self): se_cfg = None else: # In mmdetection, the `divisor` is deleted to align - # the logic of SELayer with mmcls. + # the logic of SELayer with mmpretrain. se_cfg = dict( channels=mid_channels, ratio=expand_ratio * se_ratio, @@ -365,7 +365,7 @@ def make_layer(self): mid_channels = int(self.in_channels * expand_ratio) if se_cfg is not None: # In mmdetection, the `divisor` is deleted to align - # the logic of SELayer with mmcls. + # the logic of SELayer with mmpretrain. se_cfg = dict( channels=mid_channels, ratio=se_ratio * expand_ratio, @@ -387,7 +387,7 @@ def make_layer(self): drop_path_rate=dpr[block_idx], with_cp=self.with_cp, # In mmdetection, `with_expand_conv` is set to align - # the logic of InvertedResidual with mmcls. + # the logic of InvertedResidual with mmpretrain. with_expand_conv=(mid_channels != self.in_channels))) self.in_channels = out_channels block_idx += 1 diff --git a/mmdet/models/backbones/pvt.py b/mmdet/models/backbones/pvt.py index 9d16c48178f..8b250f63c1b 100644 --- a/mmdet/models/backbones/pvt.py +++ b/mmdet/models/backbones/pvt.py @@ -555,7 +555,7 @@ def init_weights(self): else: state_dict = checkpoint if self.convert_weights: - # Because pvt backbones are not supported by mmcls, + # Because pvt backbones are not supported by mmpretrain, # so we need to convert pre-trained weights to match this # implementation. state_dict = pvt_convert(state_dict) diff --git a/mmdet/models/data_preprocessors/reid_data_preprocessor.py b/mmdet/models/data_preprocessors/reid_data_preprocessor.py index 25162a22bb6..3d0a1d45d97 100644 --- a/mmdet/models/data_preprocessors/reid_data_preprocessor.py +++ b/mmdet/models/data_preprocessors/reid_data_preprocessor.py @@ -10,12 +10,32 @@ from mmdet.registry import MODELS try: - import mmcls - from mmcls.models.utils.batch_augments import RandomBatchAugment - from mmcls.structures import (batch_label_to_onehot, cat_batch_labels, - stack_batch_scores, tensor_split) + import mmpretrain + from mmpretrain.models.utils.batch_augments import RandomBatchAugment + from mmpretrain.structures import (batch_label_to_onehot, cat_batch_labels, + tensor_split) except ImportError: - mmcls = None + mmpretrain = None + + +def stack_batch_scores(elements, device=None): + """Stack the ``score`` of a batch of :obj:`LabelData` to a tensor. + + Args: + elements (List[LabelData]): A batch of :obj`LabelData`. + device (torch.device, optional): The output device of the batch label. + Defaults to None. + Returns: + torch.Tensor: The stacked score tensor. + """ + item = elements[0] + if 'score' not in item._data_fields: + return None + + batch_score = torch.stack([element.score for element in elements]) + if device is not None: + batch_score = batch_score.to(device) + return batch_score @MODELS.register_module() @@ -54,7 +74,7 @@ class ReIDDataPreprocessor(BaseDataPreprocessor): num_classes (int, optional): The number of classes. Defaults to None. batch_augments (dict, optional): The batch augmentations settings, including "augments" and "probs". For more details, see - :class:`mmcls.models.RandomBatchAugment`. + :class:`mmpretrain.models.RandomBatchAugment`. """ def __init__(self, @@ -66,10 +86,10 @@ def __init__(self, to_onehot: bool = False, num_classes: Optional[int] = None, batch_augments: Optional[dict] = None): - if mmcls is None: + if mmpretrain is None: raise RuntimeError('Please run "pip install openmim" and ' - 'run "mim install mmcls>=1.0.0rc0" tp ' - 'install mmcls first.') + 'run "mim install mmpretrain" to ' + 'install mmpretrain first.') super().__init__() self.pad_size_divisor = pad_size_divisor self.pad_value = pad_value @@ -163,8 +183,9 @@ def forward(self, data: dict, training: bool = False) -> dict: sample_item = data_samples[0] if data_samples is not None else None if 'gt_label' in sample_item: gt_labels = [sample.gt_label for sample in data_samples] - batch_label, label_indices = cat_batch_labels( - gt_labels, device=self.device) + gt_labels_tensor = [gt_label.label for gt_label in gt_labels] + batch_label, label_indices = cat_batch_labels(gt_labels_tensor) + batch_label = batch_label.to(self.device) batch_score = stack_batch_scores(gt_labels, device=self.device) if batch_score is None and self.to_onehot: diff --git a/mmdet/models/reid/base_reid.py b/mmdet/models/reid/base_reid.py index 519fbc1a9b5..4c45964394a 100644 --- a/mmdet/models/reid/base_reid.py +++ b/mmdet/models/reid/base_reid.py @@ -4,10 +4,10 @@ import torch try: - import mmcls - from mmcls.models.classifiers import ImageClassifier + import mmpretrain + from mmpretrain.models.classifiers import ImageClassifier except ImportError: - mmcls = None + mmpretrain = None ImageClassifier = object from mmdet.registry import MODELS @@ -19,10 +19,10 @@ class BaseReID(ImageClassifier): """Base model for re-identification.""" def __init__(self, *args, **kwargs): - if mmcls is None: + if mmpretrain is None: raise RuntimeError('Please run "pip install openmim" and ' - 'run "mim install mmcls>=1.0.0rc0" tp ' - 'install mmcls first.') + 'run "mim install mmpretrain" to ' + 'install mmpretrain first.') super().__init__(*args, **kwargs) def forward(self, diff --git a/mmdet/models/reid/linear_reid_head.py b/mmdet/models/reid/linear_reid_head.py index 3f1fdf6d894..f35aaf6c2fc 100644 --- a/mmdet/models/reid/linear_reid_head.py +++ b/mmdet/models/reid/linear_reid_head.py @@ -6,10 +6,10 @@ import torch.nn as nn try: - import mmcls - from mmcls.evaluation.metrics import Accuracy + import mmpretrain + from mmpretrain.evaluation.metrics import Accuracy except ImportError: - mmcls = None + mmpretrain = None from mmengine.model import BaseModule @@ -55,10 +55,10 @@ def __init__(self, topk: Union[int, Tuple[int]] = (1, ), init_cfg: Union[dict, List[dict]] = dict( type='Normal', layer='Linear', mean=0, std=0.01, bias=0)): - if mmcls is None: + if mmpretrain is None: raise RuntimeError('Please run "pip install openmim" and ' - 'run "mim install mmcls>=1.0.0rc0" tp ' - 'install mmcls first.') + 'run "mim install mmpretrain" to ' + 'install mmpretrain first.') super(LinearReIDHead, self).__init__(init_cfg=init_cfg) assert isinstance(topk, (int, tuple)) diff --git a/projects/ConvNeXt-V2/README.md b/projects/ConvNeXt-V2/README.md index a0de226d44e..7a9f56cd247 100644 --- a/projects/ConvNeXt-V2/README.md +++ b/projects/ConvNeXt-V2/README.md @@ -19,12 +19,10 @@ Driven by improved architectures and better representation learning frameworks, **Note**: - This is a pre-release version of ConvNeXt-V2 object detection. The official finetuning setting of ConvNeXt-V2 has not been released yet. -- ConvNeXt backbone needs to install [MMClassification dev-1.x branch](https://github.com/open-mmlab/mmclassification/tree/dev-1.x) first, which has abundant backbones for downstream tasks. +- ConvNeXt backbone needs to install [MMPretrain](https://github.com/open-mmlab/mmpretrain/) first, which has abundant backbones for downstream tasks. ```shell -git clone -b dev-1.x https://github.com/open-mmlab/mmclassification.git -cd mmclassification -pip install -U openmim && mim install -e . +pip install mmpretrain ``` ## Citation diff --git a/projects/ConvNeXt-V2/configs/mask-rcnn_convnext-v2-b_fpn_lsj-3x-fcmae_coco.py b/projects/ConvNeXt-V2/configs/mask-rcnn_convnext-v2-b_fpn_lsj-3x-fcmae_coco.py index 95b960df92f..59e89550459 100644 --- a/projects/ConvNeXt-V2/configs/mask-rcnn_convnext-v2-b_fpn_lsj-3x-fcmae_coco.py +++ b/projects/ConvNeXt-V2/configs/mask-rcnn_convnext-v2-b_fpn_lsj-3x-fcmae_coco.py @@ -5,16 +5,17 @@ 'mmdet::_base_/default_runtime.py' ] -# please install the mmclassification dev-1.x branch -# import mmcls.models to trigger register_module in mmcls -custom_imports = dict(imports=['mmcls.models'], allow_failed_imports=False) +# please install the mmpretrain +# import mmpretrain.models to trigger register_module in mmpretrain +custom_imports = dict( + imports=['mmpretrain.models'], allow_failed_imports=False) checkpoint_file = 'https://download.openmmlab.com/mmclassification/v0/convnext-v2/convnext-v2-base_3rdparty-fcmae_in1k_20230104-8a798eaf.pth' # noqa image_size = (1024, 1024) model = dict( backbone=dict( _delete_=True, - type='mmcls.ConvNeXt', + type='mmpretrain.ConvNeXt', arch='base', out_indices=[0, 1, 2, 3], # TODO: verify stochastic depth rate {0.1, 0.2, 0.3, 0.4} diff --git a/requirements/tracking.txt b/requirements/tracking.txt index 823c8c33794..b338d09a185 100644 --- a/requirements/tracking.txt +++ b/requirements/tracking.txt @@ -1,5 +1,5 @@ -mmcls>=1.0.0rc0 +mmpretrain motmetrics -numpy==1.23.5 +numpy<1.24.0 scikit-learn seaborn diff --git a/tests/test_models/test_reid/test_linear_reid_head.py b/tests/test_models/test_reid/test_linear_reid_head.py index ffca01d7c19..7fe58275687 100644 --- a/tests/test_models/test_reid/test_linear_reid_head.py +++ b/tests/test_models/test_reid/test_linear_reid_head.py @@ -20,7 +20,7 @@ def setUpClass(cls) -> None: fc_channels=64, out_channels=32, num_classes=2, - loss_cls=dict(type='mmcls.CrossEntropyLoss', loss_weight=1.0), + loss_cls=dict(type='mmpretrain.CrossEntropyLoss', loss_weight=1.0), loss_triplet=dict(type='TripletLoss', margin=0.3, loss_weight=1.0), norm_cfg=dict(type='BN1d'), act_cfg=dict(type='ReLU'))