def test_resize_nearest(self): program = Program() with program_guard(program): x = layers.data(name='x', shape=[3, 9, 6], dtype="float32") output = layers.resize_nearest(x, out_shape=[12, 12]) self.assertIsNotNone(output) output = layers.resize_nearest(x, scale=3) self.assertIsNotNone(output) print(str(program))
def forward(self, inputs): assert len(inputs) == len(self.in_channels) outs = [] out = inputs[-1] outs.append(out) for i in range(self.num_ins): out = L.resize_nearest(out, scale=2, align_corners=False) out = L.pad2d(out, [0, 1, 0, 1]) out = self.conv2x2[i](out) if i < 4: out = L.concat([out, inputs[-i - 2]], axis=1) identity = self.conv1x1[i](out) out = self.deres_layers[i](out) + identity outs.append(out) outs[-1] = L.tanh(outs[-1]) return tuple(outs)
def forward(self, input): y = layers.resize_nearest(input, scale=self.scale_factor) return y
def YOLOv3(inputs, initial_filters, num_classes, is_test, trainable=True): i32 = initial_filters i64 = i32 * 2 i128 = i32 * 4 i256 = i32 * 8 i512 = i32 * 16 i1024 = i32 * 32 ''' darknet53部分,所有卷积层都没有偏移bias_attr=False ''' x = conv2d_unit(inputs, i32, (3, 3), stride=1, padding=1, name='conv01', is_test=is_test, trainable=trainable) x = conv2d_unit(x, i64, (3, 3), stride=2, padding=1, name='conv02', is_test=is_test, trainable=trainable) x = stack_residual_block(x, i32, n=1, conv_start_idx=3, is_test=is_test, trainable=trainable) x = conv2d_unit(x, i128, (3, 3), stride=2, padding=1, name='conv05', is_test=is_test, trainable=trainable) x = stack_residual_block(x, i64, n=2, conv_start_idx=6, is_test=is_test, trainable=trainable) x = conv2d_unit(x, i256, (3, 3), stride=2, padding=1, name='conv10', is_test=is_test, trainable=trainable) act11 = stack_residual_block(x, i128, n=8, conv_start_idx=11, is_test=is_test, trainable=trainable) x = conv2d_unit(act11, i512, (3, 3), stride=2, padding=1, name='conv27', is_test=is_test, trainable=trainable) act19 = stack_residual_block(x, i256, n=8, conv_start_idx=28, is_test=is_test, trainable=trainable) x = conv2d_unit(act19, i1024, (3, 3), stride=2, padding=1, name='conv44', is_test=is_test, trainable=trainable) act23 = stack_residual_block(x, i512, n=4, conv_start_idx=45, is_test=is_test, trainable=trainable) ''' darknet53部分结束,余下部分不再有残差块stack_residual_block() ''' x = conv2d_unit(act23, i512, (1, 1), stride=1, padding=0, name='conv53', is_test=is_test, trainable=trainable) x = conv2d_unit(x, i1024, (3, 3), stride=1, padding=1, name='conv54', is_test=is_test, trainable=trainable) x = conv2d_unit(x, i512, (1, 1), stride=1, padding=0, name='conv55', is_test=is_test, trainable=trainable) x = conv2d_unit(x, i1024, (3, 3), stride=1, padding=1, name='conv56', is_test=is_test, trainable=trainable) lkrelu57 = conv2d_unit(x, i512, (1, 1), stride=1, padding=0, name='conv57', is_test=is_test, trainable=trainable) x = conv2d_unit(lkrelu57, i1024, (3, 3), stride=1, padding=1, name='conv58', is_test=is_test, trainable=trainable) y1 = P.conv2d( x, 3 * (num_classes + 5), filter_size=(1, 1), param_attr=ParamAttr(initializer=fluid.initializer.Normal(0.0, 0.01), name="conv59.conv.weights"), bias_attr=ParamAttr(initializer=fluid.initializer.Constant(0.0), name="conv59.conv.bias")) x = conv2d_unit(lkrelu57, i256, (1, 1), stride=1, padding=0, name='conv60', is_test=is_test, trainable=trainable) x = P.resize_nearest(x, scale=float(2)) x = P.concat([x, act19], axis=1) x = conv2d_unit(x, i256, (1, 1), stride=1, padding=0, name='conv61', is_test=is_test, trainable=trainable) x = conv2d_unit(x, i512, (3, 3), stride=1, padding=1, name='conv62', is_test=is_test, trainable=trainable) x = conv2d_unit(x, i256, (1, 1), stride=1, padding=0, name='conv63', is_test=is_test, trainable=trainable) x = conv2d_unit(x, i512, (3, 3), stride=1, padding=1, name='conv64', is_test=is_test, trainable=trainable) lkrelu64 = conv2d_unit(x, i256, (1, 1), stride=1, padding=0, name='conv65', is_test=is_test, trainable=trainable) x = conv2d_unit(lkrelu64, i512, (3, 3), stride=1, padding=1, name='conv66', is_test=is_test, trainable=trainable) y2 = P.conv2d( x, 3 * (num_classes + 5), filter_size=(1, 1), param_attr=ParamAttr(initializer=fluid.initializer.Normal(0.0, 0.01), name="conv67.conv.weights"), bias_attr=ParamAttr(initializer=fluid.initializer.Constant(0.0), name="conv67.conv.bias")) x = conv2d_unit(lkrelu64, i128, (1, 1), stride=1, padding=0, name='conv68', is_test=is_test, trainable=trainable) x = P.resize_nearest(x, scale=float(2)) x = P.concat([x, act11], axis=1) x = conv2d_unit(x, i128, (1, 1), stride=1, padding=0, name='conv69', is_test=is_test, trainable=trainable) x = conv2d_unit(x, i256, (3, 3), stride=1, padding=1, name='conv70', is_test=is_test, trainable=trainable) x = conv2d_unit(x, i128, (1, 1), stride=1, padding=0, name='conv71', is_test=is_test, trainable=trainable) x = conv2d_unit(x, i256, (3, 3), stride=1, padding=1, name='conv72', is_test=is_test, trainable=trainable) x = conv2d_unit(x, i128, (1, 1), stride=1, padding=0, name='conv73', is_test=is_test, trainable=trainable) x = conv2d_unit(x, i256, (3, 3), stride=1, padding=1, name='conv74', is_test=is_test, trainable=trainable) y3 = P.conv2d( x, 3 * (num_classes + 5), filter_size=(1, 1), param_attr=ParamAttr(initializer=fluid.initializer.Normal(0.0, 0.01), name="conv75.conv.weights"), bias_attr=ParamAttr(initializer=fluid.initializer.Constant(0.0), name="conv75.conv.bias")) # 相当于numpy的transpose(),交换下标 y1 = P.transpose(y1, perm=[0, 2, 3, 1]) y2 = P.transpose(y2, perm=[0, 2, 3, 1]) y3 = P.transpose(y3, perm=[0, 2, 3, 1]) return y1, y2, y3
def __call__(self, body_feats): ''' :param body_feats: [s8, s16, s32] :return: bs32 | 卷积 | bs16 [fs32] | | 卷积 上采样 | | lateral topdown \ / 相加 | bs8 [fs16] | | 卷积 上采样 | | lateral topdown \ / 相加 | [fs8] fpn_inner_output = [fs32, fs16, fs8] 然后 fs32, fs16, fs8 分别再接一个卷积得到 p5, p4, p3 ; p5 接一个卷积得到 p6, p6 接一个卷积得到 p7。 ''' reverse_body_feats = body_feats[::-1] # [s32, s16, s8] num_backbone_stages = len(reverse_body_feats) # 3 # fpn内部的输出 fpn_inner_output = [None for _ in range(num_backbone_stages)] body_input = reverse_body_feats[0] # 骨干网络的s32。先接一个卷积 fpn_inner_output[0] = self.convs[0](body_input) # fpn的s32 for i in range(1, num_backbone_stages): body_input = reverse_body_feats[i] # 骨干网络的s16 top_output = fpn_inner_output[i - 1] # fpn的s32 # 骨干网络的s16卷积,fpn的s32上采样,再融合,融合方式为逐元素相加 lateral = self.convs[i](body_input) # 骨干网络的s16卷积,stride=16 topdown = L.resize_nearest(top_output, scale=float(2)) # fpn的s32上采样,stride=16 fpn_inner_single = lateral + topdown # fpn的s16 fpn_inner_output[i] = fpn_inner_single # fpn的s16 # 第二次卷积 fpn_outputs = [None for _ in range(num_backbone_stages)] for i in range(num_backbone_stages): fpn_input = fpn_inner_output[i] # fpn的s32 fpn_output = self.second_convs[i](fpn_input) # fpn的s32 fpn_outputs[i] = fpn_output # p6p7 if self.use_p6p7: p6_input = fpn_outputs[0] # p5 p6 = self.p6_conv(p6_input) p7 = self.p7_conv(p6) outs = [p7, p6] + fpn_outputs # [p7, p6, p5, p4, p3] spatial_scale = [1. / 128., 1. / 64., 1. / 32., 1. / 16., 1. / 8.] return outs, spatial_scale else: outs = fpn_outputs # [p5, p4, p3] spatial_scale = [1. / 32., 1. / 16., 1. / 8.] return outs, spatial_scale
def forward(self, x): return L.resize_nearest(x, scale=self.scale)