decoder.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import paddle
  15. import paddle.nn as nn
  16. from arch.base_module import SNConv, SNConvTranspose, ResBlock
  17. class Decoder(nn.Layer):
  18. def __init__(self, name, encode_dim, out_channels, use_bias, norm_layer,
  19. act, act_attr, conv_block_dropout, conv_block_num,
  20. conv_block_dilation, out_conv_act, out_conv_act_attr):
  21. super(Decoder, self).__init__()
  22. conv_blocks = []
  23. for i in range(conv_block_num):
  24. conv_blocks.append(
  25. ResBlock(
  26. name="{}_conv_block_{}".format(name, i),
  27. channels=encode_dim * 8,
  28. norm_layer=norm_layer,
  29. use_dropout=conv_block_dropout,
  30. use_dilation=conv_block_dilation,
  31. use_bias=use_bias))
  32. self.conv_blocks = nn.Sequential(*conv_blocks)
  33. self._up1 = SNConvTranspose(
  34. name=name + "_up1",
  35. in_channels=encode_dim * 8,
  36. out_channels=encode_dim * 4,
  37. kernel_size=3,
  38. stride=2,
  39. padding=1,
  40. output_padding=1,
  41. use_bias=use_bias,
  42. norm_layer=norm_layer,
  43. act=act,
  44. act_attr=act_attr)
  45. self._up2 = SNConvTranspose(
  46. name=name + "_up2",
  47. in_channels=encode_dim * 4,
  48. out_channels=encode_dim * 2,
  49. kernel_size=3,
  50. stride=2,
  51. padding=1,
  52. output_padding=1,
  53. use_bias=use_bias,
  54. norm_layer=norm_layer,
  55. act=act,
  56. act_attr=act_attr)
  57. self._up3 = SNConvTranspose(
  58. name=name + "_up3",
  59. in_channels=encode_dim * 2,
  60. out_channels=encode_dim,
  61. kernel_size=3,
  62. stride=2,
  63. padding=1,
  64. output_padding=1,
  65. use_bias=use_bias,
  66. norm_layer=norm_layer,
  67. act=act,
  68. act_attr=act_attr)
  69. self._pad2d = paddle.nn.Pad2D([1, 1, 1, 1], mode="replicate")
  70. self._out_conv = SNConv(
  71. name=name + "_out_conv",
  72. in_channels=encode_dim,
  73. out_channels=out_channels,
  74. kernel_size=3,
  75. use_bias=use_bias,
  76. norm_layer=None,
  77. act=out_conv_act,
  78. act_attr=out_conv_act_attr)
  79. def forward(self, x):
  80. if isinstance(x, (list, tuple)):
  81. x = paddle.concat(x, axis=1)
  82. output_dict = dict()
  83. output_dict["conv_blocks"] = self.conv_blocks.forward(x)
  84. output_dict["up1"] = self._up1.forward(output_dict["conv_blocks"])
  85. output_dict["up2"] = self._up2.forward(output_dict["up1"])
  86. output_dict["up3"] = self._up3.forward(output_dict["up2"])
  87. output_dict["pad2d"] = self._pad2d.forward(output_dict["up3"])
  88. output_dict["out_conv"] = self._out_conv.forward(output_dict["pad2d"])
  89. return output_dict
  90. class DecoderUnet(nn.Layer):
  91. def __init__(self, name, encode_dim, out_channels, use_bias, norm_layer,
  92. act, act_attr, conv_block_dropout, conv_block_num,
  93. conv_block_dilation, out_conv_act, out_conv_act_attr):
  94. super(DecoderUnet, self).__init__()
  95. conv_blocks = []
  96. for i in range(conv_block_num):
  97. conv_blocks.append(
  98. ResBlock(
  99. name="{}_conv_block_{}".format(name, i),
  100. channels=encode_dim * 8,
  101. norm_layer=norm_layer,
  102. use_dropout=conv_block_dropout,
  103. use_dilation=conv_block_dilation,
  104. use_bias=use_bias))
  105. self._conv_blocks = nn.Sequential(*conv_blocks)
  106. self._up1 = SNConvTranspose(
  107. name=name + "_up1",
  108. in_channels=encode_dim * 8,
  109. out_channels=encode_dim * 4,
  110. kernel_size=3,
  111. stride=2,
  112. padding=1,
  113. output_padding=1,
  114. use_bias=use_bias,
  115. norm_layer=norm_layer,
  116. act=act,
  117. act_attr=act_attr)
  118. self._up2 = SNConvTranspose(
  119. name=name + "_up2",
  120. in_channels=encode_dim * 8,
  121. out_channels=encode_dim * 2,
  122. kernel_size=3,
  123. stride=2,
  124. padding=1,
  125. output_padding=1,
  126. use_bias=use_bias,
  127. norm_layer=norm_layer,
  128. act=act,
  129. act_attr=act_attr)
  130. self._up3 = SNConvTranspose(
  131. name=name + "_up3",
  132. in_channels=encode_dim * 4,
  133. out_channels=encode_dim,
  134. kernel_size=3,
  135. stride=2,
  136. padding=1,
  137. output_padding=1,
  138. use_bias=use_bias,
  139. norm_layer=norm_layer,
  140. act=act,
  141. act_attr=act_attr)
  142. self._pad2d = paddle.nn.Pad2D([1, 1, 1, 1], mode="replicate")
  143. self._out_conv = SNConv(
  144. name=name + "_out_conv",
  145. in_channels=encode_dim,
  146. out_channels=out_channels,
  147. kernel_size=3,
  148. use_bias=use_bias,
  149. norm_layer=None,
  150. act=out_conv_act,
  151. act_attr=out_conv_act_attr)
  152. def forward(self, x, y, feature2, feature1):
  153. output_dict = dict()
  154. output_dict["conv_blocks"] = self._conv_blocks(
  155. paddle.concat(
  156. (x, y), axis=1))
  157. output_dict["up1"] = self._up1.forward(output_dict["conv_blocks"])
  158. output_dict["up2"] = self._up2.forward(
  159. paddle.concat(
  160. (output_dict["up1"], feature2), axis=1))
  161. output_dict["up3"] = self._up3.forward(
  162. paddle.concat(
  163. (output_dict["up2"], feature1), axis=1))
  164. output_dict["pad2d"] = self._pad2d.forward(output_dict["up3"])
  165. output_dict["out_conv"] = self._out_conv.forward(output_dict["pad2d"])
  166. return output_dict
  167. class SingleDecoder(nn.Layer):
  168. def __init__(self, name, encode_dim, out_channels, use_bias, norm_layer,
  169. act, act_attr, conv_block_dropout, conv_block_num,
  170. conv_block_dilation, out_conv_act, out_conv_act_attr):
  171. super(SingleDecoder, self).__init__()
  172. conv_blocks = []
  173. for i in range(conv_block_num):
  174. conv_blocks.append(
  175. ResBlock(
  176. name="{}_conv_block_{}".format(name, i),
  177. channels=encode_dim * 4,
  178. norm_layer=norm_layer,
  179. use_dropout=conv_block_dropout,
  180. use_dilation=conv_block_dilation,
  181. use_bias=use_bias))
  182. self._conv_blocks = nn.Sequential(*conv_blocks)
  183. self._up1 = SNConvTranspose(
  184. name=name + "_up1",
  185. in_channels=encode_dim * 4,
  186. out_channels=encode_dim * 4,
  187. kernel_size=3,
  188. stride=2,
  189. padding=1,
  190. output_padding=1,
  191. use_bias=use_bias,
  192. norm_layer=norm_layer,
  193. act=act,
  194. act_attr=act_attr)
  195. self._up2 = SNConvTranspose(
  196. name=name + "_up2",
  197. in_channels=encode_dim * 8,
  198. out_channels=encode_dim * 2,
  199. kernel_size=3,
  200. stride=2,
  201. padding=1,
  202. output_padding=1,
  203. use_bias=use_bias,
  204. norm_layer=norm_layer,
  205. act=act,
  206. act_attr=act_attr)
  207. self._up3 = SNConvTranspose(
  208. name=name + "_up3",
  209. in_channels=encode_dim * 4,
  210. out_channels=encode_dim,
  211. kernel_size=3,
  212. stride=2,
  213. padding=1,
  214. output_padding=1,
  215. use_bias=use_bias,
  216. norm_layer=norm_layer,
  217. act=act,
  218. act_attr=act_attr)
  219. self._pad2d = paddle.nn.Pad2D([1, 1, 1, 1], mode="replicate")
  220. self._out_conv = SNConv(
  221. name=name + "_out_conv",
  222. in_channels=encode_dim,
  223. out_channels=out_channels,
  224. kernel_size=3,
  225. use_bias=use_bias,
  226. norm_layer=None,
  227. act=out_conv_act,
  228. act_attr=out_conv_act_attr)
  229. def forward(self, x, feature2, feature1):
  230. output_dict = dict()
  231. output_dict["conv_blocks"] = self._conv_blocks.forward(x)
  232. output_dict["up1"] = self._up1.forward(output_dict["conv_blocks"])
  233. output_dict["up2"] = self._up2.forward(
  234. paddle.concat(
  235. (output_dict["up1"], feature2), axis=1))
  236. output_dict["up3"] = self._up3.forward(
  237. paddle.concat(
  238. (output_dict["up2"], feature1), axis=1))
  239. output_dict["pad2d"] = self._pad2d.forward(output_dict["up3"])
  240. output_dict["out_conv"] = self._out_conv.forward(output_dict["pad2d"])
  241. return output_dict