-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathres_block.py
More file actions
executable file
·108 lines (98 loc) · 4.08 KB
/
Copy pathres_block.py
File metadata and controls
executable file
·108 lines (98 loc) · 4.08 KB
1
2
3
4
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import torch.nn as nn
import torch
from .nn_module import conv2d_block, fc_block
class ResBlock(nn.Module):
r'''
Overview:
Residual Block with 2D convolution layers, including 2 types:
basic block:
input channel: C
x -> 3*3*C -> norm -> act -> 3*3*C -> norm -> act -> out
\__________________________________________/+
bottleneck block:
x -> 1*1*(1/4*C) -> norm -> act -> 3*3*(1/4*C) -> norm -> act -> 1*1*C -> norm -> act -> out
\_____________________________________________________________________________/+
Interfaces:
forward
'''
def __init__(
self,
in_channels: int,
activation: nn.Module = nn.ReLU(),
norm_type: str = 'BN',
res_type: str = 'basic'
) -> None:
r"""
Overview:
Init the Residual Block
Arguments:
- in_channels (:obj:`int`): Number of channels in the input tensor
- activation (:obj:`nn.Module`): the optional activation function
- norm_type (:obj:`str`): type of the normalization, defalut set to 'BN'(Batch Normalization), \
supports ['BN', 'IN', 'SyncBN', None].
- res_type (:obj:`str`): type of residual block, supports ['basic', 'bottleneck']
"""
super(ResBlock, self).__init__()
self.act = activation
assert res_type in ['basic',
'bottleneck'], 'residual type only support basic and bottleneck, not:{}'.format(res_type)
self.res_type = res_type
if self.res_type == 'basic':
self.conv1 = conv2d_block(in_channels, in_channels, 3, 1, 1, activation=self.act, norm_type=norm_type)
self.conv2 = conv2d_block(in_channels, in_channels, 3, 1, 1, activation=None, norm_type=norm_type)
elif self.res_type == 'bottleneck':
self.conv1 = conv2d_block(in_channels, in_channels, 1, 1, 0, activation=self.act, norm_type=norm_type)
self.conv2 = conv2d_block(in_channels, in_channels, 3, 1, 1, activation=self.act, norm_type=norm_type)
self.conv3 = conv2d_block(in_channels, in_channels, 1, 1, 0, activation=None, norm_type=norm_type)
def forward(self, x: torch.Tensor) -> torch.Tensor:
r"""
Overview:
Return the redisual block output
Arguments:
- x (:obj:`torch.Tensor`): the input tensor
Returns:
- x(:obj:`torch.Tensor`): the resblock output tensor
"""
residual = x
x = self.conv1(x)
x = self.conv2(x)
if self.res_type == 'bottleneck':
x = self.conv3(x)
x = self.act(x + residual)
return x
class ResFCBlock(nn.Module):
r'''
Overview:
Residual Block with 2 fully connected block
x -> fc1 -> norm -> act -> fc2 -> norm -> act -> out
\_____________________________________/+
Interfaces:
forward
'''
def __init__(self, in_channels: int, activation: nn.Module = nn.ReLU(), norm_type: str = 'BN'):
r"""
Overview:
Init the Residual Block
Arguments:
- in_channels (:obj:`int`): Number of channels in the input tensor
- activation (:obj:`nn.Module`): the optional activation function
- norm_type (:obj:`str`): type of the normalization, defalut set to 'BN'
"""
super(ResFCBlock, self).__init__()
self.act = activation
self.fc1 = fc_block(in_channels, in_channels, activation=self.act, norm_type=norm_type)
self.fc2 = fc_block(in_channels, in_channels, activation=None, norm_type=norm_type)
def forward(self, x: torch.Tensor) -> torch.Tensor:
r"""
Overview:
Return the redisual block output
Arguments:
- x (:obj:`torch.Tensor`): the input tensor
Returns:
- x(:obj:`torch.Tensor`): the resblock output tensor
"""
residual = x
x = self.fc1(x)
x = self.fc2(x)
x = self.act(x + residual)
return x