From 8ef1bf870c890afe989130cce976efca11d94a07 Mon Sep 17 00:00:00 2001 From: shangliang Xu Date: Tue, 17 Aug 2021 14:14:00 +0800 Subject: [PATCH] [bug fix] fix unfold negative_size_param (#34943) * [bug fix] fix unfold negative_size_param --- paddle/fluid/operators/unfold_op.cc | 19 +++++++++++++++ .../fluid/tests/unittests/test_layers.py | 24 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/paddle/fluid/operators/unfold_op.cc b/paddle/fluid/operators/unfold_op.cc index 5c0eb64993b..d4155960beb 100644 --- a/paddle/fluid/operators/unfold_op.cc +++ b/paddle/fluid/operators/unfold_op.cc @@ -154,6 +154,25 @@ class UnfoldOp : public framework::OperatorWithKernel { paddings[2], strides[0]); int output_width = CalcOutputSize(in_dims[3], kernel_sizes[1], dilations[1], paddings[1], paddings[3], strides[1]); + // check output height and width + PADDLE_ENFORCE_GT( + output_height, 0, + platform::errors::InvalidArgument( + "The sliding blocks calculated from input spatial size (%d, %d), " + "kernel_sizes (%d, %d), strides (%d, %d), dilations (%d, %d), " + "is (%d, %d), which should be a positive integer.", + in_dims[2], in_dims[3], kernel_sizes[0], kernel_sizes[1], + strides[0], strides[1], dilations[0], dilations[1], output_height, + output_width)); + PADDLE_ENFORCE_GT( + output_width, 0, + platform::errors::InvalidArgument( + "The sliding blocks calculated from input spatial size (%d, %d), " + "kernel_sizes (%d, %d), strides (%d, %d), dilations (%d, %d), " + "is (%d, %d), which should be a positive integer.", + in_dims[2], in_dims[3], kernel_sizes[0], kernel_sizes[1], + strides[0], strides[1], dilations[0], dilations[1], output_height, + output_width)); int output_col_length = output_height * output_width; out_dims.push_back(output_col_length); diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index ad53c815cd1..1bd5e08e28e 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -3320,6 +3320,30 @@ class TestBook(LayerTest): dy_res_value = dy_res.numpy() self.assertTrue(np.array_equal(static_res, dy_res_value)) + def test_dice_loss(self): + num_classes = 4 + eps = 1e-6 + input_np = np.random.rand(2, 3, num_classes).astype('float32') + label_np = np.random.randint(0, num_classes, [2, 3, 1], dtype=np.int64) + + with self.static_graph(): + input_ = layers.data( + name="input", shape=[None, 3, num_classes], dtype="float32") + label_ = layers.data( + name="label", shape=[None, 3, 1], dtype="int64") + output = layers.dice_loss(input_, label_, eps) + static_res = self.get_static_graph_result( + feed={'input': input_np, + 'label': label_np}, + fetch_list=[output])[0] + + with self.dynamic_graph(): + input_ = base.to_variable(input_np) + label_ = base.to_variable(label_np) + dy_res = layers.dice_loss(input_, label_, eps) + dy_res_value = dy_res.numpy() + self.assertTrue(np.array_equal(static_res, dy_res_value)) + def test_roi_perspective_transform(self): # TODO(minqiyang): dygraph do not support lod now with self.static_graph(): -- GitLab