Skip to content

Commit

Permalink
fix UT in in_dygraph_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
nemonameless committed Sep 29, 2022
1 parent 1a7ae77 commit 50ac93f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 51 deletions.
1 change: 1 addition & 0 deletions python/paddle/fluid/tests/unittests/test_box_coder_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,5 @@ def test_dygraph_with_static(self):


if __name__ == '__main__':
paddle.enable_static()
unittest.main()
2 changes: 0 additions & 2 deletions python/paddle/fluid/tests/unittests/test_prior_box_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
103 changes: 54 additions & 49 deletions python/paddle/vision/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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,
Expand Down

0 comments on commit 50ac93f

Please sign in to comment.