conv_module.py 2.67 KB
Newer Older
Kai Chen's avatar
Kai Chen committed
1
2
3
import warnings

import torch.nn as nn
Kai Chen's avatar
Kai Chen committed
4
from mmcv.cnn import kaiming_init, constant_init
Kai Chen's avatar
Kai Chen committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

from .norm import build_norm_layer


class ConvModule(nn.Module):

    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 padding=0,
                 dilation=1,
                 groups=1,
                 bias=True,
                 normalize=None,
                 activation='relu',
                 inplace=True,
                 activate_last=True):
        super(ConvModule, self).__init__()
        self.with_norm = normalize is not None
        self.with_activatation = activation is not None
        self.with_bias = bias
        self.activation = activation
        self.activate_last = activate_last

        if self.with_norm and self.with_bias:
            warnings.warn('ConvModule has norm and bias at the same time')

        self.conv = nn.Conv2d(
            in_channels,
            out_channels,
            kernel_size,
            stride,
            padding,
            dilation,
            groups,
            bias=bias)

        self.in_channels = self.conv.in_channels
        self.out_channels = self.conv.out_channels
        self.kernel_size = self.conv.kernel_size
        self.stride = self.conv.stride
        self.padding = self.conv.padding
        self.dilation = self.conv.dilation
        self.transposed = self.conv.transposed
        self.output_padding = self.conv.output_padding
        self.groups = self.conv.groups

        if self.with_norm:
Kai Chen's avatar
Kai Chen committed
55
56
            norm_channels = out_channels if self.activate_last else in_channels
            self.norm = build_norm_layer(normalize, norm_channels)
Kai Chen's avatar
Kai Chen committed
57
58
59
60
61
62
63
64
65
66
67

        if self.with_activatation:
            assert activation in ['relu'], 'Only ReLU supported.'
            if self.activation == 'relu':
                self.activate = nn.ReLU(inplace=inplace)

        # Default using msra init
        self.init_weights()

    def init_weights(self):
        nonlinearity = 'relu' if self.activation is None else self.activation
Kai Chen's avatar
Kai Chen committed
68
        kaiming_init(self.conv, nonlinearity=nonlinearity)
Kai Chen's avatar
Kai Chen committed
69
        if self.with_norm:
Kai Chen's avatar
Kai Chen committed
70
            constant_init(self.norm, 1, bias=0)
Kai Chen's avatar
Kai Chen committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

    def forward(self, x, activate=True, norm=True):
        if self.activate_last:
            x = self.conv(x)
            if norm and self.with_norm:
                x = self.norm(x)
            if activate and self.with_activatation:
                x = self.activate(x)
        else:
            if norm and self.with_norm:
                x = self.norm(x)
            if activate and self.with_activatation:
                x = self.activate(x)
            x = self.conv(x)
        return x