From 66c3bfaf4790c783ada99c1c5f37315752a83ede Mon Sep 17 00:00:00 2001 From: ghostxsl <451323469@qq.com> Date: Mon, 16 Aug 2021 20:39:19 +0800 Subject: [PATCH 1/2] [bug fix] fix unfold negative_size_param --- paddle/fluid/operators/unfold_op.cc | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/paddle/fluid/operators/unfold_op.cc b/paddle/fluid/operators/unfold_op.cc index 5c0eb64993b556..d4155960bebe59 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); From f15a534047de75c4c2feb329bf3e3bea4f91a3cf Mon Sep 17 00:00:00 2001 From: ghostxsl <451323469@qq.com> Date: Mon, 16 Aug 2021 20:54:13 +0800 Subject: [PATCH 2/2] add dice_loss unittest --- .../fluid/tests/unittests/test_layers.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index ad53c815cd1c81..1bd5e08e28ef0f 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -3320,6 +3320,30 @@ def test_roi_align(self): 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():