| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | # copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  | # you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | # You may obtain a copy of the License at | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #    http://www.apache.org/licenses/LICENSE-2.0 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | # distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | # See the License for the specific language governing permissions and | 
					
						
							|  |  |  | # limitations under the License. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from __future__ import absolute_import | 
					
						
							|  |  |  | from __future__ import division | 
					
						
							|  |  |  | from __future__ import print_function | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import paddle | 
					
						
							|  |  |  | from paddle import nn | 
					
						
							|  |  |  | import paddle.nn.functional as F | 
					
						
							|  |  |  | from paddle import ParamAttr | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-04 20:43:27 +08:00
										 |  |  | class DBFPN(nn.Layer): | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |     def __init__(self, in_channels, out_channels, **kwargs): | 
					
						
							| 
									
										
										
										
											2020-11-04 20:43:27 +08:00
										 |  |  |         super(DBFPN, self).__init__() | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |         self.out_channels = out_channels | 
					
						
							| 
									
										
										
										
											2020-11-05 20:45:19 +08:00
										 |  |  |         weight_attr = paddle.nn.initializer.KaimingUniform() | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 15:13:36 +08:00
										 |  |  |         self.in2_conv = nn.Conv2D( | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             in_channels=in_channels[0], | 
					
						
							|  |  |  |             out_channels=self.out_channels, | 
					
						
							|  |  |  |             kernel_size=1, | 
					
						
							| 
									
										
										
										
											2021-06-06 10:39:17 +00:00
										 |  |  |             weight_attr=ParamAttr(initializer=weight_attr), | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             bias_attr=False) | 
					
						
							| 
									
										
										
										
											2020-11-05 15:13:36 +08:00
										 |  |  |         self.in3_conv = nn.Conv2D( | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             in_channels=in_channels[1], | 
					
						
							|  |  |  |             out_channels=self.out_channels, | 
					
						
							|  |  |  |             kernel_size=1, | 
					
						
							| 
									
										
										
										
											2021-06-06 10:39:17 +00:00
										 |  |  |             weight_attr=ParamAttr(initializer=weight_attr), | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             bias_attr=False) | 
					
						
							| 
									
										
										
										
											2020-11-05 15:13:36 +08:00
										 |  |  |         self.in4_conv = nn.Conv2D( | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             in_channels=in_channels[2], | 
					
						
							|  |  |  |             out_channels=self.out_channels, | 
					
						
							|  |  |  |             kernel_size=1, | 
					
						
							| 
									
										
										
										
											2021-06-06 10:39:17 +00:00
										 |  |  |             weight_attr=ParamAttr(initializer=weight_attr), | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             bias_attr=False) | 
					
						
							| 
									
										
										
										
											2020-11-05 15:13:36 +08:00
										 |  |  |         self.in5_conv = nn.Conv2D( | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             in_channels=in_channels[3], | 
					
						
							|  |  |  |             out_channels=self.out_channels, | 
					
						
							|  |  |  |             kernel_size=1, | 
					
						
							| 
									
										
										
										
											2021-06-06 10:39:17 +00:00
										 |  |  |             weight_attr=ParamAttr(initializer=weight_attr), | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             bias_attr=False) | 
					
						
							| 
									
										
										
										
											2020-11-05 15:13:36 +08:00
										 |  |  |         self.p5_conv = nn.Conv2D( | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             in_channels=self.out_channels, | 
					
						
							|  |  |  |             out_channels=self.out_channels // 4, | 
					
						
							|  |  |  |             kernel_size=3, | 
					
						
							|  |  |  |             padding=1, | 
					
						
							| 
									
										
										
										
											2021-06-06 10:39:17 +00:00
										 |  |  |             weight_attr=ParamAttr(initializer=weight_attr), | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             bias_attr=False) | 
					
						
							| 
									
										
										
										
											2020-11-05 15:13:36 +08:00
										 |  |  |         self.p4_conv = nn.Conv2D( | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             in_channels=self.out_channels, | 
					
						
							|  |  |  |             out_channels=self.out_channels // 4, | 
					
						
							|  |  |  |             kernel_size=3, | 
					
						
							|  |  |  |             padding=1, | 
					
						
							| 
									
										
										
										
											2021-06-06 10:39:17 +00:00
										 |  |  |             weight_attr=ParamAttr(initializer=weight_attr), | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             bias_attr=False) | 
					
						
							| 
									
										
										
										
											2020-11-05 15:13:36 +08:00
										 |  |  |         self.p3_conv = nn.Conv2D( | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             in_channels=self.out_channels, | 
					
						
							|  |  |  |             out_channels=self.out_channels // 4, | 
					
						
							|  |  |  |             kernel_size=3, | 
					
						
							|  |  |  |             padding=1, | 
					
						
							| 
									
										
										
										
											2021-06-06 10:39:17 +00:00
										 |  |  |             weight_attr=ParamAttr(initializer=weight_attr), | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             bias_attr=False) | 
					
						
							| 
									
										
										
										
											2020-11-05 15:13:36 +08:00
										 |  |  |         self.p2_conv = nn.Conv2D( | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             in_channels=self.out_channels, | 
					
						
							|  |  |  |             out_channels=self.out_channels // 4, | 
					
						
							|  |  |  |             kernel_size=3, | 
					
						
							|  |  |  |             padding=1, | 
					
						
							| 
									
										
										
										
											2021-06-06 10:39:17 +00:00
										 |  |  |             weight_attr=ParamAttr(initializer=weight_attr), | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  |             bias_attr=False) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def forward(self, x): | 
					
						
							|  |  |  |         c2, c3, c4, c5 = x | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         in5 = self.in5_conv(c5) | 
					
						
							|  |  |  |         in4 = self.in4_conv(c4) | 
					
						
							|  |  |  |         in3 = self.in3_conv(c3) | 
					
						
							|  |  |  |         in2 = self.in2_conv(c2) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 18:15:44 +08:00
										 |  |  |         out4 = in4 + F.upsample( | 
					
						
							|  |  |  |             in5, scale_factor=2, mode="nearest", align_mode=1)  # 1/16 | 
					
						
							|  |  |  |         out3 = in3 + F.upsample( | 
					
						
							|  |  |  |             out4, scale_factor=2, mode="nearest", align_mode=1)  # 1/8 | 
					
						
							|  |  |  |         out2 = in2 + F.upsample( | 
					
						
							|  |  |  |             out3, scale_factor=2, mode="nearest", align_mode=1)  # 1/4 | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         p5 = self.p5_conv(in5) | 
					
						
							|  |  |  |         p4 = self.p4_conv(out4) | 
					
						
							|  |  |  |         p3 = self.p3_conv(out3) | 
					
						
							|  |  |  |         p2 = self.p2_conv(out2) | 
					
						
							| 
									
										
										
										
											2020-11-06 18:15:44 +08:00
										 |  |  |         p5 = F.upsample(p5, scale_factor=8, mode="nearest", align_mode=1) | 
					
						
							|  |  |  |         p4 = F.upsample(p4, scale_factor=4, mode="nearest", align_mode=1) | 
					
						
							|  |  |  |         p3 = F.upsample(p3, scale_factor=2, mode="nearest", align_mode=1) | 
					
						
							| 
									
										
										
										
											2020-10-13 17:13:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         fuse = paddle.concat([p5, p4, p3, p2], axis=1) | 
					
						
							|  |  |  |         return fuse |