Applies a 2D deconvolution (optionally with batch normalization and relu activation) over an input signal composed of several input planes. Attributes: conv (nn.Module): convolution module bn (nn.Module): batch normalization module relu (bool): whether
| 70 | |
| 71 | |
| 72 | class Deconv2d(nn.Module): |
| 73 | """Applies a 2D deconvolution (optionally with batch normalization and relu activation) |
| 74 | over an input signal composed of several input planes. |
| 75 | |
| 76 | Attributes: |
| 77 | conv (nn.Module): convolution module |
| 78 | bn (nn.Module): batch normalization module |
| 79 | relu (bool): whether to activate by relu |
| 80 | |
| 81 | Notes: |
| 82 | Default momentum for batch normalization is set to be 0.01, |
| 83 | |
| 84 | """ |
| 85 | |
| 86 | def __init__(self, in_channels, out_channels, kernel_size, stride=1, |
| 87 | relu=True, bn=True, bn_momentum=0.1, init_method="xavier", **kwargs): |
| 88 | super(Deconv2d, self).__init__() |
| 89 | self.out_channels = out_channels |
| 90 | assert stride in [1, 2] |
| 91 | self.stride = stride |
| 92 | |
| 93 | self.conv = nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride=stride, |
| 94 | bias=(not bn), **kwargs) |
| 95 | self.bn = nn.BatchNorm2d(out_channels, momentum=bn_momentum) if bn else None |
| 96 | # self.bn = nn.GroupNorm(8, out_channels) if bn else None |
| 97 | self.relu = relu |
| 98 | |
| 99 | # assert init_method in ["kaiming", "xavier"] |
| 100 | # self.init_weights(init_method) |
| 101 | |
| 102 | def forward(self, x): |
| 103 | y = self.conv(x) |
| 104 | if self.stride == 2: |
| 105 | h, w = list(x.size())[2:] |
| 106 | y = y[:, :, :2 * h, :2 * w].contiguous() |
| 107 | if self.bn is not None: |
| 108 | x = self.bn(y) |
| 109 | if self.relu: |
| 110 | x = F.relu(x, inplace=True) |
| 111 | return x |
| 112 | |
| 113 | def init_weights(self, init_method): |
| 114 | """default initialization""" |
| 115 | init_uniform(self.conv, init_method) |
| 116 | if self.bn is not None: |
| 117 | init_bn(self.bn) |
| 118 | |
| 119 | |
| 120 | class Conv3d(nn.Module): |