From 50ac93f080c48e96693d9f69ee39c94f6cd2e03d Mon Sep 17 00:00:00 2001 From: nemonameless Date: Thu, 29 Sep 2022 07:41:06 +0000 Subject: [PATCH] fix UT in in_dygraph_mode --- .../tests/unittests/test_box_coder_op.py | 1 + .../tests/unittests/test_prior_box_op.py | 2 - python/paddle/vision/ops.py | 103 +++++++++--------- 3 files changed, 55 insertions(+), 51 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_box_coder_op.py b/python/paddle/fluid/tests/unittests/test_box_coder_op.py index 406f2c213e970..a1ed0115415cf 100644 --- a/python/paddle/fluid/tests/unittests/test_box_coder_op.py +++ b/python/paddle/fluid/tests/unittests/test_box_coder_op.py @@ -337,4 +337,5 @@ def test_dygraph_with_static(self): if __name__ == '__main__': + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_prior_box_op.py b/python/paddle/fluid/tests/unittests/test_prior_box_op.py index 7592079d63af9..0b05aef4f5e23 100644 --- a/python/paddle/fluid/tests/unittests/test_prior_box_op.py +++ b/python/paddle/fluid/tests/unittests/test_prior_box_op.py @@ -110,8 +110,6 @@ def init_test_params(self): self.flip = True self.set_min_max_aspect_ratios_order() self.real_aspect_ratios = [1, 2.0, 1.0 / 2.0, 3.0, 1.0 / 3.0] - self.aspect_ratios = np.array(self.aspect_ratios, - dtype=np.float64).flatten() self.variances = [0.1, 0.1, 0.2, 0.2] self.variances = np.array(self.variances, dtype=np.float64).flatten() diff --git a/python/paddle/vision/ops.py b/python/paddle/vision/ops.py index 038ed40becf41..3c6a0c3724ce9 100755 --- a/python/paddle/vision/ops.py +++ b/python/paddle/vision/ops.py @@ -710,25 +710,6 @@ def prior_box(input, clip=True, flip=True) """ - if in_dygraph_mode(): - step_w, step_h = steps - if max_sizes == None: - max_sizes = [] - box, var = _C_ops.prior_box(input, image, min_sizes, aspect_ratios, - variance, max_sizes, flip, clip, step_w, - step_h, offset, min_max_aspect_ratios_order) - return box, var - - if _non_static_mode(): - step_w, step_h = steps - if max_sizes == None: - max_sizes = [] - box, var = _legacy_C_ops.prior_box(input, image, min_sizes, - aspect_ratios, variance, max_sizes, - flip, clip, step_w, step_h, offset, - min_max_aspect_ratios_order) - return box, var - helper = LayerHelper("prior_box", **locals()) dtype = helper.input_dtype() check_variable_and_dtype(input, 'input', @@ -750,39 +731,63 @@ def _is_list_or_tuple_(data): aspect_ratios = list(map(float, aspect_ratios)) steps = list(map(float, steps)) - attrs = { - 'min_sizes': min_sizes, - 'aspect_ratios': aspect_ratios, - 'variances': variance, - 'flip': flip, - 'clip': clip, - 'step_w': steps[0], - 'step_h': steps[1], - 'offset': offset, - 'min_max_aspect_ratios_order': min_max_aspect_ratios_order - } + cur_max_sizes = None if max_sizes is not None and len(max_sizes) > 0 and max_sizes[0] > 0: if not _is_list_or_tuple_(max_sizes): max_sizes = [max_sizes] - attrs['max_sizes'] = max_sizes - - box = helper.create_variable_for_type_inference(dtype) - var = helper.create_variable_for_type_inference(dtype) - helper.append_op( - type="prior_box", - inputs={ - "Input": input, - "Image": image - }, - outputs={ - "Boxes": box, - "Variances": var - }, - attrs=attrs, - ) - box.stop_gradient = True - var.stop_gradient = True - return box, var + cur_max_sizes = max_sizes + + if in_dygraph_mode(): + step_w, step_h = steps + if max_sizes == None: + max_sizes = [] + box, var = _C_ops.prior_box(input, image, min_sizes, aspect_ratios, + variance, max_sizes, flip, clip, step_w, + step_h, offset, min_max_aspect_ratios_order) + return box, var + + if _non_static_mode(): + attrs = ('min_sizes', min_sizes, 'aspect_ratios', aspect_ratios, + 'variances', variance, 'flip', flip, 'clip', clip, 'step_w', + steps[0], 'step_h', steps[1], 'offset', offset, + 'min_max_aspect_ratios_order', min_max_aspect_ratios_order) + if cur_max_sizes is not None: + attrs += ('max_sizes', cur_max_sizes) + box, var = _legacy_C_ops.prior_box(input, image, *attrs) + return box, var + else: + attrs = { + 'min_sizes': min_sizes, + 'aspect_ratios': aspect_ratios, + 'variances': variance, + 'flip': flip, + 'clip': clip, + 'step_w': steps[0], + 'step_h': steps[1], + 'offset': offset, + 'min_max_aspect_ratios_order': min_max_aspect_ratios_order + } + + if cur_max_sizes is not None: + attrs['max_sizes'] = cur_max_sizes + + box = helper.create_variable_for_type_inference(dtype) + var = helper.create_variable_for_type_inference(dtype) + helper.append_op( + type="prior_box", + inputs={ + "Input": input, + "Image": image + }, + outputs={ + "Boxes": box, + "Variances": var + }, + attrs=attrs, + ) + box.stop_gradient = True + var.stop_gradient = True + return box, var def box_coder(prior_box,