diff --git a/paddle/fluid/operators/unfold_op.cc b/paddle/fluid/operators/unfold_op.cc index 5c0eb64993b55646203a91e7bbc6d978105e8027..d4155960bebe5992b093a61f29b6b5edf48fea61 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 ad53c815cd1c818bbb43d1362fa3674fb8e6a4db..1bd5e08e28ef0f151b8c78b2537a08c66dd20e22 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():