2018-10-12 13:07:35 +08:00
## CNN
2020-08-06 16:51:58 +08:00
We provide some building bricks for CNNs, including layer building, module bundles and weight initialization.
2020-05-05 12:02:33 +08:00
### Layer building
We may need to try different layers of the same type when running experiments,
but do not want to modify the code from time to time.
Here we provide some layer building methods to construct layers from a dict,
which can be written in configs or specified via command line arguments.
#### Usage
A simplest example is
2020-07-21 14:48:16 +08:00
2020-05-05 12:02:33 +08:00
```python
2022-08-19 11:18:47 +08:00
from mmcv.cnn import build_conv_layer
2020-05-05 12:02:33 +08:00
cfg = dict(type='Conv3d')
2021-02-21 11:13:39 +08:00
layer = build_conv_layer(cfg, in_channels=3, out_channels=8, kernel_size=3)
2020-05-05 12:02:33 +08:00
```
- `build_conv_layer` : Supported types are Conv1d, Conv2d, Conv3d, Conv (alias for Conv2d).
- `build_norm_layer` : Supported types are BN1d, BN2d, BN3d, BN (alias for BN2d), SyncBN, GN, LN, IN1d, IN2d, IN3d, IN (alias for IN2d).
2021-02-22 10:55:42 +08:00
- `build_activation_layer` : Supported types are ReLU, LeakyReLU, PReLU, RReLU, ReLU6, ELU, Sigmoid, Tanh, GELU.
2020-05-05 12:02:33 +08:00
- `build_upsample_layer` : Supported types are nearest, bilinear, deconv, pixel_shuffle.
- `build_padding_layer` : Supported types are zero, reflect, replicate.
#### Extension
We also allow extending the building methods with custom layers and operators.
1. Write and register your own module.
2022-05-16 20:47:56 +08:00
```python
2022-08-19 11:18:47 +08:00
from mmengine.registry import MODELS
2020-05-05 12:02:33 +08:00
2022-08-19 11:18:47 +08:00
@MODELS .register_module()
2022-05-16 20:47:56 +08:00
class MyUpsample:
2020-05-05 12:02:33 +08:00
2022-05-16 20:47:56 +08:00
def __init__ (self, scale_factor):
pass
2020-05-05 12:02:33 +08:00
2022-05-16 20:47:56 +08:00
def forward(self, x):
pass
```
2020-05-05 12:02:33 +08:00
2. Import `MyUpsample` somewhere (e.g., in `__init__.py` ) and then use it.
2022-05-16 20:47:56 +08:00
```python
2022-08-19 11:18:47 +08:00
from mmcv.cnn import build_upsample_layer
2022-05-16 20:47:56 +08:00
cfg = dict(type='MyUpsample', scale_factor=2)
layer = build_upsample_layer(cfg)
```
2020-05-05 12:02:33 +08:00
### Module bundles
We also provide common module bundles to facilitate the network construction.
`ConvModule` is a bundle of convolution, normalization and activation layers,
please refer to the [api ](api.html#mmcv.cnn.ConvModule ) for details.
```python
2022-08-19 11:18:47 +08:00
from mmcv.cnn import ConvModule
2020-05-05 12:02:33 +08:00
# conv + bn + relu
conv = ConvModule(3, 8, 2, norm_cfg=dict(type='BN'))
# conv + gn + relu
conv = ConvModule(3, 8, 2, norm_cfg=dict(type='GN', num_groups=2))
# conv + relu
conv = ConvModule(3, 8, 2)
# conv
conv = ConvModule(3, 8, 2, act_cfg=None)
# conv + leaky relu
conv = ConvModule(3, 8, 3, padding=1, act_cfg=dict(type='LeakyReLU'))
# bn + conv + relu
conv = ConvModule(
3, 8, 2, norm_cfg=dict(type='BN'), order=('norm', 'conv', 'act'))
```
2020-06-28 23:15:47 +08:00
### Model Zoo
Besides torchvision pre-trained models, we also provide pre-trained models of following CNN:
- VGG Caffe
- ResNet Caffe
- ResNeXt
- ResNet with Group Normalization
- ResNet with Group Normalization and Weight Standardization
- HRNetV2
- Res2Net
- RegNet
#### Model URLs in JSON
The model zoo links in MMCV are managed by JSON files.
The json file consists of key-value pair of model name and its url or path.
An example json file could be like:
```json
{
"model_a": "https://example.com/models/model_a_9e5bac.pth",
"model_b": "pretrain/model_b_ab3ef2c.pth"
}
```
2020-07-21 14:48:16 +08:00
The default links of the pre-trained models hosted on OpenMMLab AWS could be found [here ](https://github.com/open-mmlab/mmcv/blob/master/mmcv/model_zoo/open_mmlab.json ).
2020-06-28 23:15:47 +08:00
2022-10-20 14:41:41 +08:00
You may override default links by putting `open-mmlab.json` under `MMCV_HOME` . If `MMCV_HOME` is not found in your environment, `~/.cache/mmcv` will be used by default. You may use your own path with `export MMCV_HOME=/your/path` .
2020-06-28 23:15:47 +08:00
The external json files will be merged into default one. If the same key presents in both external json and default json, the external one will be used.
#### Load Checkpoint
2022-10-20 14:41:41 +08:00
The following types are supported for `filename` of `mmcv.load_checkpoint()` .
2020-06-28 23:15:47 +08:00
- filepath: The filepath of the checkpoint.
- `http://xxx` and `https://xxx` : The link to download the checkpoint. The `SHA256` postfix should be contained in the filename.
2022-10-20 14:41:41 +08:00
- `torchvision://xxx` : The model links in `torchvision.models` . Please refer to [torchvision ](https://pytorch.org/docs/stable/torchvision/models.html ) for details.
2020-06-28 23:15:47 +08:00
- `open-mmlab://xxx` : The model links or filepath provided in default and additional json files.