-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathres_block.py
More file actions
81 lines (71 loc) · 2.9 KB
/
res_block.py
File metadata and controls
81 lines (71 loc) · 2.9 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
__author__="xu hongtao"
__email__="xxxmy@foxmail.com"
import torch.nn as nn
import torch.nn.functional as F
class basic_2d(nn.Module):
def __init__(self,in_channel,filters,stride=None,kernel_size=3,stage=0,block=0):
super(basic_2d,self).__init__()
if stride is None:
if block!=0 or stage==0:
stride=1
else:
stride=2
self.residual=nn.Sequential(
nn.Conv2d(in_channel,filters,kernel_size,stride=stride,bias=False,padding=1,padding_mode='zeros'),
nn.BatchNorm2d(filters,eps=1e-5),
nn.ReLU(inplace=True),
nn.Conv2d(filters,filters,kernel_size,bias=False,padding=1,padding_mode='zeros'),
nn.BatchNorm2d(filters,eps=1e-5)
)
if block ==0:
self.shortcut=nn.Sequential(
nn.Conv2d(in_channel,filters,(1,1),stride=stride,bias=False),
nn.BatchNorm2d(filters,eps=1e-5)
)
else:
self.shortcut=None
def forward(self,x):
residual=self.residual(x)
if self.shortcut is None:
shortcut=x
else:
shortcut=self.shortcut(x)
y=residual+shortcut
y=F.relu(y,inplace=True)
# print(y.size())
return y
class bottlneck_2d(nn.Module):
def __init__(self,in_channel,filters,stride=None,kernel_size=3,stage=0,block=0):
super(bottlneck_2d,self).__init__()
if stride is None:
if block!=0 or stage==0:
stride=1
else:
stride=2
self.residual=nn.Sequential(
nn.Conv2d(in_channel,filters,(1,1),stride=stride,bias=False),
nn.BatchNorm2d(filters,eps=1e-5),
nn.ReLU(inplace=True),
nn.Conv2d(filters,filters,kernel_size,bias=False,padding=1,padding_mode='zeros'),
nn.BatchNorm2d(filters,eps=1e-5),
nn.ReLU(inplace=True),
nn.Conv2d(filters,filters*4,(1,1),bias=False),
nn.BatchNorm2d(filters*4,eps=1e-5)
)
if block ==0:
self.shortcut=nn.Sequential(
nn.Conv2d(in_channel,filters*4,(1,1),stride=stride,bias=False),
nn.BatchNorm2d(filters*4,eps=1e-5)
)
else:
self.shortcut=None
def forward(self,x):
residual=self.residual(x)
if self.shortcut is None:
shortcut=x
else:
shortcut=self.shortcut(x)
y=residual+shortcut
y=F.relu(y,inplace=True)
# print(y.size())
return y