-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathNeck.py
More file actions
151 lines (130 loc) · 4.57 KB
/
Neck.py
File metadata and controls
151 lines (130 loc) · 4.57 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import torch.nn as nn
import torch
from wama_modules.BaseModule import ConvNormActive
from wama_modules.utils import resizeTensor
class FPN(nn.Module):
def __init__(self,
in_channels_list=[16, 32, 64, 128],
c1=128,
c2=256,
active='relu',
norm='bn',
gn_c=8,
mode='AddSmall2Big', # AddSmall2Big or AddBig2Small(much better or classification tasks)
dim=2,
):
super().__init__()
self.mode = mode
self.conv1_list = nn.ModuleList([
ConvNormActive(in_channels, c1, kernel_size=1, norm=norm, active=active, gn_c=gn_c, dim=dim, padding=0)
for in_channels in in_channels_list
])
self.conv2_list = nn.ModuleList([
ConvNormActive(c1, c2, kernel_size=3, norm=norm, active=active, gn_c=gn_c, dim=dim, padding=1)
for _ in range(len(in_channels_list))
])
def forward(self, x_list):
"""
:param x_list: multi scale feature maps, from shallow(big size) to deep(small size)
:return:
# demo
# 1D
x_list = [
torch.ones([3,16,32]),
torch.ones([3,32,24]),
torch.ones([3,64,16]),
torch.ones([3,128,8]),
]
fpn = FPN(in_channels_list=[16, 32, 64, 128],
c1=128,
c2=256,
active='relu',
norm='bn',
gn_c=8,
mode='AddSmall2Big',
dim=1,)
fpn = FPN(in_channels_list=[16, 32, 64, 128],
c1=128,
c2=256,
active='relu',
norm='bn',
gn_c=8,
mode='AddBig2Small', # revserse, for classification
dim=1,)
f_list = fpn(x_list)
_ = [print(i.shape) for i in x_list]
_ = [print(i.shape) for i in f_list]
# 2D
x_list = [
torch.ones([3,16,32,32]),
torch.ones([3,32,24,24]),
torch.ones([3,64,16,16]),
torch.ones([3,128,8,8]),
]
fpn = FPN(in_channels_list=[16, 32, 64, 128],
c1=128,
c2=256,
active='relu',
norm='bn',
gn_c=8,
mode='AddSmall2Big',
dim=2,)
fpn = FPN(in_channels_list=[16, 32, 64, 128],
c1=128,
c2=256,
active='relu',
norm='bn',
gn_c=8,
mode='AddBig2Small', # revserse, for classification
dim=2,)
f_list = fpn(x_list)
_ = [print(i.shape) for i in x_list]
_ = [print(i.shape) for i in f_list]
# 3D
x_list = [
torch.ones([3,16,32,32,32]),
torch.ones([3,32,24,24,24]),
torch.ones([3,64,16,16,16]),
torch.ones([3,128,8,8,8]),
]
fpn = FPN(in_channels_list=[16, 32, 64, 128],
c1=128,
c2=256,
active='relu',
norm='bn',
gn_c=8,
mode='AddSmall2Big',
dim=3,)
fpn = FPN(in_channels_list=[16, 32, 64, 128],
c1=128,
c2=256,
active='relu',
norm='bn',
gn_c=8,
mode='AddBig2Small', # revserse, for classification
dim=3,)
f_list = fpn(x_list)
_ = [print(i.shape) for i in x_list]
_ = [print(i.shape) for i in f_list]
"""
f_list = [self.conv1_list[index](f) for index, f in enumerate(x_list)]
if self.mode == 'AddSmall2Big':
f_list_2 = []
x = f_list[-1]
f_list_2.append(x)
for index in range(len(f_list)-1):
# print(f_list[-(index+2)].shape[2:])
x = f_list[-(index + 2)] + resizeTensor(x, size=f_list[-(index+2)].shape[2:])
f_list_2.append(x)
f_list_2 = f_list_2[::-1]
elif self.mode == 'AddBig2Small':
f_list_2 = []
x = f_list[0]
f_list_2.append(x)
for index in range(len(f_list) - 1):
# print(f_list[index + 1].shape[2:])
x = f_list[index + 1] + resizeTensor(x, size=f_list[index + 1].shape[2:])
f_list_2.append(x)
return_list = [self.conv2_list[index](f) for index, f in enumerate(f_list_2)]
return return_list
# UCtrans的