cnn.md 2.38 KB
Newer Older
1
2
## CNN

ChaseMonsterAway's avatar
ChaseMonsterAway committed
3
We provide some building bricks for CNNs, including layer building, module bundles and weight initialization.
Kai Chen's avatar
Kai Chen committed
4
5
6
7
8
9
10
11
12
13
14

### 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
Kai Chen's avatar
Kai Chen committed
15

Kai Chen's avatar
Kai Chen committed
16
```python
17
18
from mmcv.cnn import build_conv_layer

Kai Chen's avatar
Kai Chen committed
19
cfg = dict(type='Conv3d')
ftbabi's avatar
ftbabi committed
20
layer = build_conv_layer(cfg, in_channels=3, out_channels=8, kernel_size=3)
Kai Chen's avatar
Kai Chen committed
21
22
23
24
```

- `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).
ftbabi's avatar
ftbabi committed
25
- `build_activation_layer`: Supported types are ReLU, LeakyReLU, PReLU, RReLU, ReLU6, ELU, Sigmoid, Tanh, GELU.
Kai Chen's avatar
Kai Chen committed
26
27
28
29
30
31
32
33
34
- `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.

35
   ```python
36
   from mmengine.registry import MODELS
Kai Chen's avatar
Kai Chen committed
37

38
   @MODELS.register_module()
39
   class MyUpsample:
Kai Chen's avatar
Kai Chen committed
40

41
42
       def __init__(self, scale_factor):
           pass
Kai Chen's avatar
Kai Chen committed
43

44
45
46
       def forward(self, x):
           pass
   ```
Kai Chen's avatar
Kai Chen committed
47
48
49

2. Import `MyUpsample` somewhere (e.g., in `__init__.py`) and then use it.

50
   ```python
51
52
   from mmcv.cnn import build_upsample_layer

53
54
55
   cfg = dict(type='MyUpsample', scale_factor=2)
   layer = build_upsample_layer(cfg)
   ```
Kai Chen's avatar
Kai Chen committed
56
57
58
59
60
61
62
63

### 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
64
65
from mmcv.cnn import ConvModule

Kai Chen's avatar
Kai Chen committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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'))
```