From 7c42e5de1a4db1662db19a1b62c63ce98de713a6 Mon Sep 17 00:00:00 2001 From: fengjiayi Date: Wed, 30 May 2018 13:37:41 +0800 Subject: [PATCH] Polish RandomCropOp --- paddle/fluid/operators/random_crop_op.cc | 2 +- paddle/fluid/operators/random_crop_op.cu | 20 +++++++++++++++++++ python/paddle/fluid/layers/nn.py | 5 +++-- .../paddle/fluid/tests/unittests/op_test.py | 2 -- 4 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 paddle/fluid/operators/random_crop_op.cu diff --git a/paddle/fluid/operators/random_crop_op.cc b/paddle/fluid/operators/random_crop_op.cc index d92b8bbbb58..b14b559e31d 100644 --- a/paddle/fluid/operators/random_crop_op.cc +++ b/paddle/fluid/operators/random_crop_op.cc @@ -74,8 +74,8 @@ namespace ops = paddle::operators; namespace f = paddle::framework; REGISTER_OPERATOR(random_crop, ops::RandomCropOp, ops::RandomCropOpMaker, ops::RandomCropOpInferShape, f::EmptyGradOpMaker); + template using Kernel = ops::RandomCropKernel; - REGISTER_OP_CPU_KERNEL(random_crop, Kernel, Kernel, Kernel, Kernel, Kernel); diff --git a/paddle/fluid/operators/random_crop_op.cu b/paddle/fluid/operators/random_crop_op.cu new file mode 100644 index 00000000000..2782911b4fe --- /dev/null +++ b/paddle/fluid/operators/random_crop_op.cu @@ -0,0 +1,20 @@ +// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "paddle/fluid/operators/random_crop_op.h" + +template +using Kernel = ops::RandomCropKernel; +REGISTER_OP_CUDA_KERNEL(random_crop, Kernel, Kernel, Kernel, + Kernel, Kernel); diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index c337e0f4f20..3f04dcccd6a 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -3990,15 +3990,16 @@ def upsampling_bilinear2d(input, out_shape=None, scale=None, name=None): return out -def random_crop(input, shape, seed=0): +def random_crop(input, shape, seed=1): helper = LayerHelper("random_crop", **locals()) dtype = helper.input_dtype() out = helper.create_tmp_variable(dtype) if isinstance(seed, int): + seed_value = seed seed = helper.create_global_variable( persistable=True, shape=[1], dtype="int32") helper.set_variable_initializer( - var=seed, initializer=Constant(value=seed)) + var=seed, initializer=Constant(value=seed_value)) elif not isinstance(seed, Variable): raise ValueError("'seed' must be a Variable or an int.") seed_out = helper.create_tmp_variable(dtype="int32") diff --git a/python/paddle/fluid/tests/unittests/op_test.py b/python/paddle/fluid/tests/unittests/op_test.py index b7e62533b33..b611470fa1f 100644 --- a/python/paddle/fluid/tests/unittests/op_test.py +++ b/python/paddle/fluid/tests/unittests/op_test.py @@ -336,8 +336,6 @@ class OpTest(unittest.TestCase): actual_t = np.array(actual) expect = self.outputs[out_name] expect_t = expect[0] if isinstance(expect, tuple) else expect - import pdb - pdb.set_trace() self.assertTrue( np.allclose( actual_t, expect_t, atol=atol), -- GitLab