Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Refactor] Use MMCV Model Registry #843

Merged
merged 5 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ in [TSM: Temporal Shift Module for Efficient Video Understanding](https://arxiv.
1. create a new file in `mmaction/models/backbones/resnet_tsm.py`.

```python
from ..registry import BACKBONES
from ..builder import BACKBONES
from .resnet import ResNet

@BACKBONES.register_module()
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/3_new_dataset.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ import os.path as osp
import mmcv

from .base import BaseDataset
from .registry import DATASETS
from .builder import DATASETS


@DATASETS.register_module()
Expand Down
6 changes: 3 additions & 3 deletions docs/tutorials/5_new_modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Assume you want to add an optimizer named as `MyOptimizer`, which has arguments
You need to first implement the new optimizer in a file, e.g., in `mmaction/core/optimizer/my_optimizer.py`:

```python
from .registry import OPTIMIZERS
from mmcv.runner import OPTIMIZERS
from torch.optim import Optimizer

@OPTIMIZERS.register_module()
Expand Down Expand Up @@ -118,7 +118,7 @@ Here we show how to develop new components with an example of TSN.
```python
import torch.nn as nn

from ..registry import BACKBONES
from ..builder import BACKBONES

@BACKBONES.register_module()
class ResNet(nn.Module):
Expand Down Expand Up @@ -161,7 +161,7 @@ Here we show how to develop a new head with the example of TSNHead as the follow
and overwrite `init_weights(self)` and `forward(self, x)` method.

```python
from ..registry import HEADS
from ..builder import HEADS
from .base import BaseHead


Expand Down
8 changes: 2 additions & 6 deletions docs/tutorials/7_customize_runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ You need to create a new directory named `mmaction/core/optimizer`.
And then implement the new optimizer in a file, e.g., in `mmaction/core/optimizer/my_optimizer.py`:

```python
from .registry import OPTIMIZERS
from mmcv.runner import OPTIMIZERS
from torch.optim import Optimizer


Expand Down Expand Up @@ -113,11 +113,7 @@ Some models may have some parameter-specific settings for optimization, e.g. wei
The users can do those fine-grained parameter tuning through customizing optimizer constructor.

```python
from mmcv.utils import build_from_cfg

from mmcv.runner.optimizer import OPTIMIZER_BUILDERS, OPTIMIZERS
from mmaction.utils import get_root_logger
from .my_optimizer import MyOptimizer
from mmcv.runner.optimizer import OPTIMIZER_BUILDERS


@OPTIMIZER_BUILDERS.register_module()
Expand Down
2 changes: 1 addition & 1 deletion docs_zh_CN/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ MMAction2 将模型组件分为 4 种基础模型:
1. 创建 `mmaction/models/backbones/resnet_tsm.py` 文件

```python
from ..registry import BACKBONES
from ..builder import BACKBONES
from .resnet import ResNet

@BACKBONES.register_module()
Expand Down
2 changes: 1 addition & 1 deletion docs_zh_CN/tutorials/3_new_dataset.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ import os.path as osp
import mmcv

from .base import BaseDataset
from .registry import DATASETS
from .builder import DATASETS


@DATASETS.register_module()
Expand Down
6 changes: 3 additions & 3 deletions docs_zh_CN/tutorials/5_new_modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
用户需要首先实现一个新的优化器文件,如 `mmaction/core/optimizer/my_optimizer.py`:

```python
from .registry import OPTIMIZERS
from mmcv.runner import OPTIMIZERS
from torch.optim import Optimizer

@OPTIMIZERS.register_module()
Expand Down Expand Up @@ -105,7 +105,7 @@ MMAction2 将模型组件分为 4 种基础模型:
```python
import torch.nn as nn

from ..registry import BACKBONES
from ..builder import BACKBONES

@BACKBONES.register_module()
class ResNet(nn.Module):
Expand Down Expand Up @@ -148,7 +148,7 @@ MMAction2 将模型组件分为 4 种基础模型:
并重写 `init_weights(self)` 和 `forward(self, x)` 方法

```python
from ..registry import HEADS
from ..builder import HEADS
from .base import BaseHead


Expand Down
8 changes: 2 additions & 6 deletions docs_zh_CN/tutorials/7_customize_runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ optimizer = dict(type='Adam', lr=0.001, betas=(0.9, 0.999), eps=1e-08, weight_de
可以创建一个名为 `mmaction/core/optimizer` 的文件夹,并在目录下的文件进行构建,如 `mmaction/core/optimizer/my_optimizer.py`:

```python
from .registry import OPTIMIZERS
from mmcv.runner import OPTIMIZERS
from torch.optim import Optimizer


Expand Down Expand Up @@ -111,11 +111,7 @@ optimizer = dict(type='MyOptimizer', a=a_value, b=b_value, c=c_value)
用户可以通过自定义优化器构造函数来进行那些细粒度的参数调整。

```python
from mmcv.utils import build_from_cfg

from mmcv.runner.optimizer import OPTIMIZER_BUILDERS, OPTIMIZERS
from mmaction.utils import get_root_logger
from .my_optimizer import MyOptimizer
from mmcv.runner.optimizer import OPTIMIZER_BUILDERS


@OPTIMIZER_BUILDERS.register_module()
Expand Down
6 changes: 4 additions & 2 deletions mmaction/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from .base import BaseDataset
from .blending_utils import (BaseMiniBatchBlending, CutmixBlending,
MixupBlending)
from .builder import build_dataloader, build_dataset
from .builder import (BLENDINGS, DATASETS, PIPELINES, build_dataloader,
build_dataset)
from .dataset_wrappers import RepeatDataset
from .hvu_dataset import HVUDataset
from .image_dataset import ImageDataset
Expand All @@ -20,5 +21,6 @@
'RawframeDataset', 'BaseDataset', 'ActivityNetDataset', 'SSNDataset',
'HVUDataset', 'AudioDataset', 'AudioFeatureDataset', 'ImageDataset',
'RawVideoDataset', 'AVADataset', 'AudioVisualDataset',
'BaseMiniBatchBlending', 'CutmixBlending', 'MixupBlending'
'BaseMiniBatchBlending', 'CutmixBlending', 'MixupBlending', 'DATASETS',
'PIPELINES', 'BLENDINGS'
]
2 changes: 1 addition & 1 deletion mmaction/datasets/activitynet_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from ..core import average_recall_at_avg_proposals
from .base import BaseDataset
from .registry import DATASETS
from .builder import DATASETS


@DATASETS.register_module()
Expand Down
2 changes: 1 addition & 1 deletion mmaction/datasets/audio_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import torch

from .base import BaseDataset
from .registry import DATASETS
from .builder import DATASETS


@DATASETS.register_module()
Expand Down
2 changes: 1 addition & 1 deletion mmaction/datasets/audio_feature_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import torch

from .base import BaseDataset
from .registry import DATASETS
from .builder import DATASETS


@DATASETS.register_module()
Expand Down
2 changes: 1 addition & 1 deletion mmaction/datasets/audio_visual_dataset.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os.path as osp

from .builder import DATASETS
from .rawframe_dataset import RawframeDataset
from .registry import DATASETS


@DATASETS.register_module()
Expand Down
2 changes: 1 addition & 1 deletion mmaction/datasets/ava_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ..core.evaluation.ava_utils import ava_eval, read_labelmap, results2csv
from ..utils import get_root_logger
from .base import BaseDataset
from .registry import DATASETS
from .builder import DATASETS


@DATASETS.register_module()
Expand Down
2 changes: 1 addition & 1 deletion mmaction/datasets/blending_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import torch.nn.functional as F
from torch.distributions.beta import Beta

from .registry import BLENDINGS
from .builder import BLENDINGS

__all__ = ['BaseMiniBatchBlending', 'MixupBlending', 'CutmixBlending']

Expand Down
9 changes: 6 additions & 3 deletions mmaction/datasets/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import numpy as np
from mmcv.parallel import collate
from mmcv.runner import get_dist_info
from mmcv.utils import build_from_cfg
from mmcv.utils import Registry, build_from_cfg
from torch.utils.data import DataLoader

from .dataset_wrappers import RepeatDataset
from .registry import DATASETS
from .samplers import ClassSpecificDistributedSampler, DistributedSampler

if platform.system() != 'Windows':
Expand All @@ -20,6 +18,10 @@
soft_limit = min(4096, hard_limit)
resource.setrlimit(resource.RLIMIT_NOFILE, (soft_limit, hard_limit))

DATASETS = Registry('dataset')
PIPELINES = Registry('pipeline')
BLENDINGS = Registry('blending')


def build_dataset(cfg, default_args=None):
"""Build a dataset from config dict.
Expand All @@ -33,6 +35,7 @@ def build_dataset(cfg, default_args=None):
Dataset: The constructed dataset.
"""
if cfg['type'] == 'RepeatDataset':
from .dataset_wrappers import RepeatDataset
dataset = RepeatDataset(
build_dataset(cfg['dataset'], default_args), cfg['times'])
else:
Expand Down
2 changes: 1 addition & 1 deletion mmaction/datasets/dataset_wrappers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .registry import DATASETS
from .builder import DATASETS


@DATASETS.register_module()
Expand Down
2 changes: 1 addition & 1 deletion mmaction/datasets/hvu_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ..core import mean_average_precision
from .base import BaseDataset
from .registry import DATASETS
from .builder import DATASETS


@DATASETS.register_module()
Expand Down
2 changes: 1 addition & 1 deletion mmaction/datasets/image_dataset.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .registry import DATASETS
from .builder import DATASETS
from .video_dataset import VideoDataset


Expand Down
2 changes: 1 addition & 1 deletion mmaction/datasets/pipelines/augmentations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
from torch.nn.modules.utils import _pair

from ..registry import PIPELINES
from ..builder import PIPELINES


def _init_lazy_if_proper(results, lazy):
Expand Down
2 changes: 1 addition & 1 deletion mmaction/datasets/pipelines/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from mmcv.utils import build_from_cfg

from ..registry import PIPELINES
from ..builder import PIPELINES


@PIPELINES.register_module()
Expand Down
2 changes: 1 addition & 1 deletion mmaction/datasets/pipelines/formating.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import torch
from mmcv.parallel import DataContainer as DC

from ..registry import PIPELINES
from ..builder import PIPELINES


def to_tensor(data):
Expand Down
2 changes: 1 addition & 1 deletion mmaction/datasets/pipelines/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from torch.nn.modules.utils import _pair

from ...utils import get_random_string, get_shm_dir, get_thread_id
from ..registry import PIPELINES
from ..builder import PIPELINES


@PIPELINES.register_module()
Expand Down
2 changes: 1 addition & 1 deletion mmaction/datasets/rawframe_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import torch

from .base import BaseDataset
from .registry import DATASETS
from .builder import DATASETS


@DATASETS.register_module()
Expand Down
2 changes: 1 addition & 1 deletion mmaction/datasets/rawvideo_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import mmcv

from .base import BaseDataset
from .registry import DATASETS
from .builder import DATASETS


@DATASETS.register_module()
Expand Down
5 changes: 0 additions & 5 deletions mmaction/datasets/registry.py

This file was deleted.

2 changes: 1 addition & 1 deletion mmaction/datasets/ssn_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
perform_regression, temporal_iou, temporal_nms)
from ..utils import get_root_logger
from .base import BaseDataset
from .registry import DATASETS
from .builder import DATASETS


class SSNInstance:
Expand Down
2 changes: 1 addition & 1 deletion mmaction/datasets/video_dataset.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os.path as osp

from .base import BaseDataset
from .registry import DATASETS
from .builder import DATASETS


@DATASETS.register_module()
Expand Down
6 changes: 3 additions & 3 deletions mmaction/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
ResNet2Plus1d, ResNet3d, ResNet3dCSN, ResNet3dLayer,
ResNet3dSlowFast, ResNet3dSlowOnly, ResNetAudio,
ResNetTIN, ResNetTSM, TANet)
from .builder import (DETECTORS, build_backbone, build_detector, build_head,
from .builder import (BACKBONES, DETECTORS, HEADS, LOCALIZERS, LOSSES, NECKS,
RECOGNIZERS, build_backbone, build_detector, build_head,
build_localizer, build_loss, build_model, build_neck,
build_recognizer)
from .common import LFB, TAM, Conv2plus1d, ConvAudio
Expand All @@ -16,7 +17,6 @@
from .necks import TPN
from .recognizers import (AudioRecognizer, BaseRecognizer, recognizer2d,
recognizer3d)
from .registry import BACKBONES, HEADS, LOCALIZERS, LOSSES, RECOGNIZERS
from .roi_extractors import SingleRoIExtractor3D

__all__ = [
Expand All @@ -32,5 +32,5 @@
'AudioTSNHead', 'X3D', 'X3DHead', 'ResNet3dLayer', 'DETECTORS',
'SingleRoIExtractor3D', 'BBoxHeadAVA', 'ResNetAudio', 'build_detector',
'ConvAudio', 'AVARoIHead', 'MobileNetV2', 'MobileNetV2TSM', 'TANet', 'LFB',
'FBOHead', 'LFBInferHead', 'TRNHead'
'FBOHead', 'LFBInferHead', 'TRNHead', 'NECKS'
]
2 changes: 1 addition & 1 deletion mmaction/models/backbones/c3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from mmcv.utils import _BatchNorm

from ...utils import get_root_logger
from ..registry import BACKBONES
from ..builder import BACKBONES


@BACKBONES.register_module()
Expand Down
2 changes: 1 addition & 1 deletion mmaction/models/backbones/mobilenet_v2_tsm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ..registry import BACKBONES
from ..builder import BACKBONES
from .mobilenet_v2 import InvertedResidual, MobileNetV2
from .resnet_tsm import TemporalShift

Expand Down
2 changes: 1 addition & 1 deletion mmaction/models/backbones/resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from torch.utils import checkpoint as cp

from ...utils import get_root_logger
from ..registry import BACKBONES
from ..builder import BACKBONES


class BasicBlock(nn.Module):
Expand Down
2 changes: 1 addition & 1 deletion mmaction/models/backbones/resnet2plus1d.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ..registry import BACKBONES
from ..builder import BACKBONES
from .resnet3d import ResNet3d


Expand Down
Loading