From 61f2e9d5e13d7f8fc7b19b4de3d1eb059cb2f54d Mon Sep 17 00:00:00 2001 From: Liangliang He Date: Tue, 5 Dec 2017 18:00:42 +0800 Subject: [PATCH] Change resize bilinear output size from tensor to attributes --- mace/kernels/opencl/resize_bilinear_opencl.cc | 7 ++-- mace/kernels/resize_bilinear.h | 37 ++++++------------- mace/ops/resize_bilinear.h | 5 +-- mace/ops/resize_bilinear_test.cc | 9 +---- 4 files changed, 17 insertions(+), 41 deletions(-) diff --git a/mace/kernels/opencl/resize_bilinear_opencl.cc b/mace/kernels/opencl/resize_bilinear_opencl.cc index 27dd8e62..1ebc21f8 100644 --- a/mace/kernels/opencl/resize_bilinear_opencl.cc +++ b/mace/kernels/opencl/resize_bilinear_opencl.cc @@ -13,7 +13,7 @@ namespace kernels { template void ResizeBilinearFunctor::operator()( - const Tensor *input, const Tensor *resize_dims, Tensor *output) { + const Tensor *input, Tensor *output) { const index_t batch = input->dim(0); const index_t in_height = input->dim(1); const index_t in_width = input->dim(2); @@ -21,9 +21,8 @@ void ResizeBilinearFunctor::operator()( const index_t channel_blocks = RoundUpDiv4(channels); - index_t out_height; - index_t out_width; - GetOutputSize(resize_dims, &out_height, &out_width); + index_t out_height = out_height_; + index_t out_width = out_width_; MACE_CHECK(out_height > 0 && out_width > 0); std::vector output_shape {batch, out_height, out_width, channels}; if (input->is_image()) { diff --git a/mace/kernels/resize_bilinear.h b/mace/kernels/resize_bilinear.h index 27415ebd..c365243a 100644 --- a/mace/kernels/resize_bilinear.h +++ b/mace/kernels/resize_bilinear.h @@ -105,26 +105,16 @@ void ResizeImage(const T *images, struct ResizeBilinearFunctorBase { ResizeBilinearFunctorBase(const std::vector &size, bool align_corners) - : align_corners_(align_corners), size_(size) {} + : align_corners_(align_corners) { + MACE_CHECK(size.size() == 2); + out_height_ = size[0]; + out_width_ = size[1]; + } protected: - void GetOutputSize(const Tensor *resize_dims, - index_t *out_height, - index_t *out_width) { - if (size_[0] < 0 || size_[1] < 0) { - MACE_CHECK(resize_dims != nullptr && resize_dims->dim_size() == 1); - Tensor::MappingGuard resize_dims_mapper(resize_dims); - auto dims_data = resize_dims->data(); - *out_height = dims_data[0]; - *out_width = dims_data[1]; - } else { - *out_height = size_[0]; - *out_width = size_[1]; - } - } - bool align_corners_; - std::vector size_; + index_t out_height_; + index_t out_width_; }; template @@ -132,17 +122,14 @@ struct ResizeBilinearFunctor : ResizeBilinearFunctorBase { ResizeBilinearFunctor(const std::vector &size, bool align_corners) : ResizeBilinearFunctorBase(size, align_corners) {} - void operator()(const Tensor *input, - const Tensor *resize_dims, - Tensor *output) { + void operator()(const Tensor *input, Tensor *output) { const index_t batch = input->dim(0); const index_t in_height = input->dim(1); const index_t in_width = input->dim(2); const index_t channels = input->dim(3); - index_t out_height; - index_t out_width; - GetOutputSize(resize_dims, &out_height, &out_width); + index_t out_height = out_height_; + index_t out_width = out_width_; MACE_CHECK(out_height > 0 && out_width > 0); std::vector out_shape{batch, out_height, out_width, channels}; output->Resize(out_shape); @@ -180,9 +167,7 @@ struct ResizeBilinearFunctor : ResizeBilinearFunctorBase ResizeBilinearFunctor(const std::vector &size, bool align_corners) : ResizeBilinearFunctorBase(size, align_corners) {} - void operator()(const Tensor *input, - const Tensor *resize_dims, - Tensor *output); + void operator()(const Tensor *input, Tensor *output); }; } // namespace kernels diff --git a/mace/ops/resize_bilinear.h b/mace/ops/resize_bilinear.h index 23c5edab..0e814f53 100644 --- a/mace/ops/resize_bilinear.h +++ b/mace/ops/resize_bilinear.h @@ -21,15 +21,12 @@ class ResizeBilinearOp : public Operator { bool Run() override { const Tensor *input = this->Input(0); - const Tensor *resize_dims = this->Input(1); Tensor *output = this->Output(0); MACE_CHECK(input->dim_size() == 4, "input must be 4-dimensional.", input->dim_size()); - MACE_CHECK(resize_dims->dim_size() == 1, - "resize dim must be 2-dimensional.", resize_dims->dim_size()); - functor_(input, resize_dims, output); + functor_(input, output); return true; } diff --git a/mace/ops/resize_bilinear_test.cc b/mace/ops/resize_bilinear_test.cc index 3e50c3b4..fedb12e9 100644 --- a/mace/ops/resize_bilinear_test.cc +++ b/mace/ops/resize_bilinear_test.cc @@ -16,15 +16,14 @@ TEST_F(ResizeBilinearTest, CPUResizeBilinearWOAlignCorners) { auto &net = test_net(); OpDefBuilder("ResizeBilinear", "ResizeBilinearTest") .Input("Input") - .Input("OutSize") .Output("Output") + .AddIntsArg("size", {1, 2}) .Finalize(net.NewOperatorDef()); // Add input data vector input(24); std::iota(begin(input), end(input), 0); net.AddInputFromArray("Input", {1, 2, 4, 3}, input); - net.AddInputFromArray("OutSize", {2}, {1, 2}); // Run net.RunOp(); @@ -41,16 +40,15 @@ TEST_F(ResizeBilinearTest, ResizeBilinearWAlignCorners) { auto &net = test_net(); OpDefBuilder("ResizeBilinear", "ResizeBilinearTest") .Input("Input") - .Input("OutSize") .Output("Output") .AddIntArg("align_corners", 1) + .AddIntsArg("size", {1, 2}) .Finalize(net.NewOperatorDef()); // Add input data vector input(24); std::iota(begin(input), end(input), 0); net.AddInputFromArray("Input", {1, 2, 4, 3}, input); - net.AddInputFromArray("OutSize", {2}, {1, 2}); // Run net.RunOp(); @@ -80,11 +78,9 @@ void TestRandomResizeBilinear() { // Add input data net.AddRandomInput("Input", {batch, in_height, in_width, channels}); - net.AddInputFromArray("OutSize", {2}, {height, width}); OpDefBuilder("ResizeBilinear", "ResizeBilinearTest") .Input("Input") - .Input("OutSize") .Output("Output") .AddIntArg("align_corners", align_corners) .AddIntsArg("size", {height, width}) @@ -99,7 +95,6 @@ void TestRandomResizeBilinear() { OpDefBuilder("ResizeBilinear", "ResizeBilinearTest") .Input("InputImage") - .Input("OutSize") .Output("OutputImage") .AddIntArg("align_corners", align_corners) .AddIntsArg("size", {height, width}) -- GitLab