Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

vgg.py 12 KB

You have to be logged in to leave a comment. Sign In
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
  1. import torch.nn as nn
  2. import math
  3. # import torch.utils.model_zoo as model_zoo
  4. import torch
  5. import numpy as np
  6. import torch.nn.functional as F
  7. # vgg16
  8. def vgg(cfg, i, batch_norm=False):
  9. layers = []
  10. in_channels = i
  11. stage = 1
  12. for v in cfg:
  13. if v == 'M':
  14. stage += 1
  15. if stage == 6:
  16. layers += [nn.MaxPool2d(kernel_size=3, stride=2, padding=1)]
  17. else:
  18. layers += [nn.MaxPool2d(kernel_size=3, stride=2, padding=1)]
  19. else:
  20. if stage == 6:
  21. # conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=4, dilation=4, bias=False)
  22. conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
  23. else:
  24. conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
  25. if batch_norm:
  26. layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]
  27. else:
  28. layers += [conv2d, nn.ReLU(inplace=True)]
  29. in_channels = v
  30. return layers
  31. class vgg16(nn.Module):
  32. def __init__(self):
  33. super(vgg16, self).__init__()
  34. self.cfg = {'tun': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'], 'tun_ex': [512, 512, 512]}
  35. self.extract = [8, 15, 22, 29] # [3, 8, 15, 22, 29]
  36. self.extract_ex = [5]
  37. self.base = nn.ModuleList(vgg(self.cfg['tun'], 3))
  38. self.base_ex = vgg_ex(self.cfg['tun_ex'], 512)
  39. for m in self.modules():
  40. if isinstance(m, nn.Conv2d):
  41. n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
  42. m.weight.data.normal_(0, 0.01)
  43. elif isinstance(m, nn.BatchNorm2d):
  44. m.weight.data.fill_(1)
  45. m.bias.data.zero_()
  46. def load_pretrained_model(self, model):
  47. self.base.load_state_dict(model)
  48. def forward(self, x, multi=0):
  49. tmp_x = []
  50. for k in range(len(self.base)):
  51. x = self.base[k](x)
  52. if k in self.extract:
  53. tmp_x.append(x)
  54. x = self.base_ex(x)
  55. tmp_x.append(x)
  56. if multi == 1:
  57. tmp_y = []
  58. tmp_y.append(tmp_x[0])
  59. return tmp_y
  60. else:
  61. return tmp_x
  62. class vgg_ex(nn.Module):
  63. def __init__(self, cfg, incs=512, padding=1, dilation=1):
  64. super(vgg_ex, self).__init__()
  65. self.cfg = cfg
  66. layers = []
  67. for v in self.cfg:
  68. # conv2d = nn.Conv2d(incs, v, kernel_size=3, padding=4, dilation=4, bias=False)
  69. conv2d = nn.Conv2d(incs, v, kernel_size=3, padding=padding, dilation=dilation, bias=False)
  70. layers += [conv2d, nn.ReLU(inplace=True)]
  71. incs = v
  72. self.ex = nn.Sequential(*layers)
  73. for m in self.modules():
  74. if isinstance(m, nn.Conv2d):
  75. n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
  76. m.weight.data.normal_(0, 0.01)
  77. elif isinstance(m, nn.BatchNorm2d):
  78. m.weight.data.fill_(1)
  79. m.bias.data.zero_()
  80. def forward(self, x):
  81. x = self.ex(x)
  82. return x
  83. # class vgg16_locate(nn.Module):
  84. # def __init__(self):
  85. # super(vgg16_locate,self).__init__()
  86. # self.cfg = [512, 512, 512]
  87. # self.vgg16 = vgg16()
  88. # # self.maxpool2 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  89. # # self.maxpool3 = nn.MaxPool2d(kernel_size=3, stride=3, padding=1)
  90. # self.layer61 = vgg_ex(self.cfg, 512, 3, 3)
  91. # self.layer62 = vgg_ex(self.cfg, 512, 6, 6)
  92. # self.layer63 = vgg_ex(self.cfg, 512, 9, 9)
  93. # self.layer64 = vgg_ex(self.cfg, 512, 12, 12)
  94. #
  95. #
  96. # # self.layer6_convert, self.layer6_trans, self.layer6_score = [],[],[]
  97. # # for ii in range(3):
  98. # # self.layer6_convert.append(nn.Conv2d(1024, 512, 3, 1, 1, bias=False))
  99. # # self.layer6_trans.append(nn.Conv2d(512, 512, 1, 1, bias=False))
  100. # # self.layer6_score.append(nn.Conv2d(512, 1, 1, 1))
  101. # # self.layer6_convert, self.layer6_trans, self.layer6_score = nn.ModuleList(self.layer6_convert), nn.ModuleList(self.layer6_trans), nn.ModuleList(self.layer6_score)
  102. # self.trans = nn.Conv2d(512*5, 512, 3, 1, 1, bias=False)
  103. # # self.score = nn.Conv2d(3, 1, 1, 1)
  104. # # self.score = nn.Conv2d(1, 1, 1, 1)
  105. # self.relu = nn.ReLU(inplace=True)
  106. #
  107. # for m in self.modules():
  108. # if isinstance(m, nn.Conv2d):
  109. # n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
  110. # m.weight.data.normal_(0, 0.01)
  111. # elif isinstance(m, nn.BatchNorm2d):
  112. # m.weight.data.fill_(1)
  113. # m.bias.data.zero_()
  114. #
  115. # def load_pretrained_model(self, model):
  116. # self.vgg16.load_pretrained_model(model)
  117. #
  118. # def forward(self, x):
  119. # x_size = x.size()[2:]
  120. # xs = self.vgg16(x)
  121. #
  122. # xls = [xs[-1]]
  123. # xls.append(self.layer61(xs[-2]))
  124. # xls.append(self.layer62(xs[-2]))
  125. # xls.append(self.layer63(xs[-2]))
  126. # xls.append(self.layer64(xs[-2]))
  127. #
  128. # # xls_tmp = [self.layer6_convert[0](xls[0])]
  129. # # for ii in range(1, 3):
  130. # # xls_tmp.append(F.interpolate(self.layer6_convert[ii](xls[ii]), xls_tmp[0].size()[2:], mode='bilinear', align_corners=True))
  131. # #
  132. # # xls_trans = self.layer6_trans[0](xls_tmp[0])
  133. # # for ii in range(1, 3):
  134. # # xls_trans = torch.add(xls_trans, self.layer6_trans[ii](xls_tmp[ii]))
  135. # score, score_fuse = [], None
  136. # # for ii in range(3):
  137. # # score.append(self.layer6_score[ii](xls_tmp[ii]))
  138. #
  139. # xls_trans = self.trans(self.relu(torch.cat(xls, dim=1)))
  140. # xs[-1] = xls_trans
  141. # # score_fuse = F.interpolate(self.score(torch.cat(score, dim=1)), x_size, mode='bilinear', align_corners=True)
  142. # # score_fuse = F.interpolate(self.score(torch.add(torch.add(score[0], score[1]), score[2])), x_size, mode='bilinear', align_corners=True)
  143. #
  144. # # score = [F.interpolate(ss, x_size, mode='bilinear', align_corners=True) for ss in score]
  145. #
  146. # return xs, score_fuse, score
  147. class vgg16_locate(nn.Module):
  148. def __init__(self):
  149. super(vgg16_locate,self).__init__()
  150. self.vgg16 = vgg16()
  151. self.in_planes = 512
  152. # self.out_planes = [512, 256, 128, 64] # with convert layer, with conv6
  153. # self.out_planes = [512, 512, 256, 128] # no convert layer, with conv6
  154. self.out_planes = [512, 256, 128] # no convert layer, no conv6
  155. ppms, infos = [], []
  156. # for ii in [3, 6, 12]:
  157. # if ii <= 8:
  158. # ppms.append(nn.Sequential(nn.AvgPool2d(kernel_size=ii, stride=ii), nn.Conv2d(self.in_planes, self.in_planes, 1, 1, bias=False), nn.ReLU(inplace=True)))
  159. # else:
  160. # ppms.append(nn.Sequential(nn.AdaptiveAvgPool2d(1), nn.Conv2d(self.in_planes, self.in_planes, 1, 1, bias=False), nn.ReLU(inplace=True)))
  161. for ii in [1, 3, 5]:
  162. ppms.append(nn.Sequential(nn.AdaptiveAvgPool2d(ii), nn.Conv2d(self.in_planes, self.in_planes, 1, 1, bias=False), nn.ReLU(inplace=True)))
  163. self.ppms = nn.ModuleList(ppms)
  164. self.ppm_cat = nn.Sequential(nn.Conv2d(self.in_planes * 4, self.in_planes, 3, 1, 1, bias=False), nn.ReLU(inplace=True))
  165. #self.ppm_cat = nn.Sequential(nn.Conv2d(self.in_planes, self.in_planes, 3, 1, 1, bias=False), nn.ReLU(inplace=True))
  166. # self.ppm_score = nn.Conv2d(self.in_planes, 1, 1, 1)
  167. for ii in self.out_planes:
  168. infos.append(nn.Sequential(nn.Conv2d(self.in_planes, ii, 3, 1, 1, bias=False), nn.ReLU(inplace=True)))
  169. self.infos = nn.ModuleList(infos)
  170. for m in self.modules():
  171. if isinstance(m, nn.Conv2d):
  172. n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
  173. m.weight.data.normal_(0, 0.01)
  174. elif isinstance(m, nn.BatchNorm2d):
  175. m.weight.data.fill_(1)
  176. m.bias.data.zero_()
  177. def load_pretrained_model(self, model):
  178. self.vgg16.load_pretrained_model(model)
  179. def forward(self, x):
  180. x_size = x.size()[2:]
  181. xs = self.vgg16(x)
  182. xls = [xs[-1]]
  183. #xls = xs[-1]
  184. for k in range(len(self.ppms)):
  185. xls.append(F.interpolate(self.ppms[k](xs[-1]), xs[-1].size()[2:], mode='bilinear', align_corners=True))
  186. #xls = torch.add(xls, F.interpolate(self.ppms[k](xs[-1]), xs[-1].size()[2:], mode='bilinear', align_corners=True))
  187. xls = self.ppm_cat(torch.cat(xls, dim=1))
  188. #xls = self.ppm_cat(xls)
  189. top_score = None
  190. # top_score = F.interpolate(self.ppm_score(xls), x_size, mode='bilinear', align_corners=True)
  191. infos = []
  192. for k in range(len(self.infos)):
  193. infos.append(self.infos[k](F.interpolate(xls, xs[len(self.infos) - 1 - k].size()[2:], mode='bilinear', align_corners=True)))
  194. return xs, top_score, infos
  195. # class vgg16_locate(nn.Module):
  196. # def __init__(self):
  197. # super(vgg16_locate,self).__init__()
  198. # self.cfg = [1024, 1024, 1024]
  199. # self.vgg16 = vgg16()
  200. # self.maxpool4 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  201. # self.maxpool5 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  202. # self.maxpool6 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  203. #
  204. # self.layer5 = vgg_ex(self.cfg, 1024)
  205. # self.layer6 = vgg_ex(self.cfg, 1024)
  206. # self.layer7 = vgg_ex(self.cfg, 1024)
  207. #
  208. # self.layer71 = nn.Conv2d(1024, 512, 1, 1, bias=False)
  209. # self.layer61 = nn.Conv2d(1024, 512, 1, 1, bias=False)
  210. # self.layer51 = nn.Conv2d(1024, 512, 1, 1, bias=False)
  211. # self.layer41 = nn.Conv2d(1024, 512, 1, 1, bias=False)
  212. #
  213. # self.layer76 = nn.Conv2d(512, 512, 3, 1, 1, bias=False)
  214. # self.layer65 = nn.Conv2d(512, 512, 3, 1, 1, bias=False)
  215. # self.layer54 = nn.Sequential(nn.Conv2d(512, 512, 3, 1, 1, bias=False), nn.ReLU(inplace=True), nn.Conv2d(512, 512, 1, 1, bias=False))
  216. # # self.layer54 = nn.Conv2d(512, 512, 3, 1, 1, bias=False)
  217. # # self.layer54_ = nn.Sequential(nn.ReLU(inplace=True), nn.Conv2d(512, 512, 1, 1, bias=False))
  218. # # self.score = nn.Conv2d(512, 1, 1, 1)
  219. #
  220. # self.relu = nn.ReLU(inplace=True)
  221. #
  222. # for m in self.modules():
  223. # if isinstance(m, nn.Conv2d):
  224. # n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
  225. # m.weight.data.normal_(0, 0.01)
  226. # elif isinstance(m, nn.BatchNorm2d):
  227. # m.weight.data.fill_(1)
  228. # m.bias.data.zero_()
  229. #
  230. # def load_pretrained_model(self, model):
  231. # self.vgg16.load_pretrained_model(model)
  232. #
  233. # def forward(self, x):
  234. # x_size = x.size()[2:]
  235. # score_fuse, score = None, None
  236. # xs = self.vgg16(x)
  237. #
  238. # x5 = self.layer5(self.maxpool4(xs[-1]))
  239. # x6 = self.layer6(self.maxpool5(x5))
  240. # x7 = self.layer7(self.maxpool6(x6))
  241. #
  242. # x8 = self.layer76(self.relu(torch.add(F.interpolate(self.layer71(x7) , x6.size()[2:], mode='bilinear', align_corners=True), self.layer61(x6))))
  243. # x8 = self.layer65(self.relu(torch.add(F.interpolate(x8 , x5.size()[2:], mode='bilinear', align_corners=True), self.layer51(x5))))
  244. # x8 = self.layer54(self.relu(torch.add(F.interpolate(x8 , xs[-1].size()[2:], mode='bilinear', align_corners=True), self.layer41(xs[-1]))))
  245. # xs[-1] = x8
  246. #
  247. # # x8 = self.layer76(self.relu(torch.add(F.interpolate(self.layer71(x7) , x6.size()[2:], mode='bilinear', align_corners=True), self.layer61(x6))))
  248. # # x9 = self.layer65(self.relu(torch.add(F.interpolate(x8 , x5.size()[2:], mode='bilinear', align_corners=True), self.layer51(x5))))
  249. # # x10 = self.layer54(self.relu(torch.add(F.interpolate(x9 , xs[-1].size()[2:], mode='bilinear', align_corners=True), self.layer41(xs[-1]))))
  250. # # score_fuse = F.interpolate(self.score(self.relu(torch.add(torch.add(F.interpolate(x8 , x10.size()[2:], mode='bilinear', align_corners=True),
  251. # # F.interpolate(x9 , x10.size()[2:], mode='bilinear', align_corners=True)), x10))), x_size, mode='bilinear', align_corners=True)
  252. # # xs[-1] = self.layer54_(x10)
  253. #
  254. # return xs, score_fuse, score
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...