From febd1a0ebcbbbfd9da72d0f94b6b121bb54e30f4 Mon Sep 17 00:00:00 2001 From: nihuini Date: Fri, 10 Jan 2020 15:21:20 +0800 Subject: [PATCH] add crop test --- src/layer/arm/crop_arm.cpp | 22 ++- tests/CMakeLists.txt | 1 + tests/test_crop.cpp | 268 +++++++++++++++++++++++++++++++++++++ 3 files changed, 289 insertions(+), 2 deletions(-) create mode 100644 tests/test_crop.cpp diff --git a/src/layer/arm/crop_arm.cpp b/src/layer/arm/crop_arm.cpp index 86f127fc8..3f82330b8 100644 --- a/src/layer/arm/crop_arm.cpp +++ b/src/layer/arm/crop_arm.cpp @@ -346,7 +346,16 @@ int Crop_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) } // opt.use_packing_layout #endif // __ARM_NEON - return Crop::forward(bottom_blob, top_blob, opt); + Mat bottom_blob_unpacked = bottom_blob; + if (elempack != 1) + { + Option opt_pack1 = opt; + opt_pack1.blob_allocator = opt.workspace_allocator; + + convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt); + } + + return Crop::forward(bottom_blob_unpacked, top_blob, opt); } int Crop_arm::forward(const std::vector& bottom_blobs, std::vector& top_blobs, const Option& opt) const @@ -558,7 +567,16 @@ int Crop_arm::forward(const std::vector& bottom_blobs, std::vector& to } // opt.use_packing_layout #endif // __ARM_NEON - return Crop::forward(bottom_blobs, top_blobs, opt); + Mat bottom_blob_unpacked = bottom_blob; + if (elempack != 1) + { + Option opt_pack1 = opt; + opt_pack1.blob_allocator = opt.workspace_allocator; + + convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt); + } + + return Crop::forward(bottom_blob_unpacked, top_blob, opt); } } // namespace ncnn diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 499d67eb9..c73d6331a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -18,6 +18,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src) ncnn_add_layer_test(Concat) ncnn_add_layer_test(Convolution) ncnn_add_layer_test(ConvolutionDepthWise) +ncnn_add_layer_test(Crop) ncnn_add_layer_test(Deconvolution) ncnn_add_layer_test(DeconvolutionDepthWise) ncnn_add_layer_test(InnerProduct) diff --git a/tests/test_crop.cpp b/tests/test_crop.cpp new file mode 100644 index 000000000..14fdcb22a --- /dev/null +++ b/tests/test_crop.cpp @@ -0,0 +1,268 @@ +// Tencent is pleased to support the open source community by making ncnn available. +// +// Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. +// +// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// https://opensource.org/licenses/BSD-3-Clause +// +// 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 "testutil.h" + +#include "layer/crop.h" + +static int test_crop(const ncnn::Mat& a, int woffset, int hoffset, int coffset, int outw, int outh, int outc, int woffset2, int hoffset2, int coffset2, bool use_packing_layout) +{ + ncnn::ParamDict pd; + pd.set(0, woffset);// woffset + pd.set(1, hoffset);// hoffset + pd.set(2, coffset);// coffset + pd.set(3, outw);// outw + pd.set(4, outh);// outh + pd.set(5, outc);// outc + pd.set(6, woffset2);// woffset2 + pd.set(7, hoffset2);// hoffset2 + pd.set(8, coffset2);// coffset2 + + std::vector weights(0); + ncnn::ModelBinFromMatArray mb(weights.data()); + + ncnn::Option opt; + opt.num_threads = 1; + opt.use_vulkan_compute = true; + opt.use_int8_inference = false; + opt.use_fp16_packed = false; + opt.use_fp16_storage = false; + opt.use_fp16_arithmetic = false; + opt.use_int8_storage = false; + opt.use_int8_arithmetic = false; + opt.use_packing_layout = use_packing_layout; + + int ret = test_layer("Crop", pd, mb, opt, a); + if (ret != 0) + { + fprintf(stderr, "test_crop failed a.dims=%d a=(%d %d %d) woffset=%d hoffset=%d coffset=%d outw=%d outh=%d outc=%d woffset2=%d hoffset2=%d coffset2=%d use_packing_layout=%d\n", a.dims, a.w, a.h, a.c, woffset, hoffset, coffset, outw, outh, outc, woffset2, hoffset2, coffset2, use_packing_layout); + } + + return ret; +} + +static ncnn::Mat IntArrayMat(int a0) +{ + ncnn::Mat m(1); + int* p = m; + p[0] = a0; + return m; +} + +static ncnn::Mat IntArrayMat(int a0, int a1) +{ + ncnn::Mat m(2); + int* p = m; + p[0] = a0; + p[1] = a1; + return m; +} + +static ncnn::Mat IntArrayMat(int a0, int a1, int a2) +{ + ncnn::Mat m(3); + int* p = m; + p[0] = a0; + p[1] = a1; + p[2] = a2; + return m; +} + +static void print_int_array(const ncnn::Mat& a) +{ + const int* pa = a; + + fprintf(stderr, "["); + for (int i=0; i weights(0); + ncnn::ModelBinFromMatArray mb(weights.data()); + + ncnn::Option opt; + opt.num_threads = 1; + opt.use_vulkan_compute = true; + opt.use_int8_inference = false; + opt.use_fp16_packed = false; + opt.use_fp16_storage = false; + opt.use_fp16_arithmetic = false; + opt.use_int8_storage = false; + opt.use_int8_arithmetic = false; + opt.use_packing_layout = use_packing_layout; + + int ret = test_layer("Crop", pd, mb, opt, a); + if (ret != 0) + { + fprintf(stderr, "test_crop failed a.dims=%d a=(%d %d %d)", a.dims, a.w, a.h, a.c); + fprintf(stderr, " starts="); print_int_array(starts); + fprintf(stderr, " ends="); print_int_array(ends); + fprintf(stderr, " axes="); print_int_array(axes); + fprintf(stderr, " use_packing_layout=%d\n", use_packing_layout); + } + + return ret; +} + +static int test_crop_0() +{ + ncnn::Mat a = RandomMat(13, 11, 16); + + return 0 + || test_crop(a, 0, 0, 0, -233, -233, -233, 0, 0, 0, false) + || test_crop(a, 0, 3, 0, 10, -233, -233, 0, 0, 0, false) + || test_crop(a, 5, 0, 0, -233, 6, -233, 0, 0, 0, false) + || test_crop(a, 5, 3, 0, -233, -233, 12, 0, 0, 0, false) + || test_crop(a, 0, 3, 4, 7, -233, 8, 0, 0, 0, false) + || test_crop(a, 5, 0, 5, 4, -233, 4, 0, 0, 0, false) + || test_crop(a, 5, 3, 6, 2, 7, 9, 0, 0, 0, false) + || test_crop(a, 0, 3, 4, -233, 4, 5, 0, 0, 0, false) + || test_crop(a, 5, 0, 5, 6, 6, 8, 0, 0, 0, false) + || test_crop(a, 5, 3, 6, 4, 4, 4, 0, 0, 0, false) + + || test_crop(a, 0, 0, 0, -233, -233, -233, 0, 0, 0, true) + || test_crop(a, 0, 3, 0, 10, -233, -233, 0, 0, 0, true) + || test_crop(a, 5, 0, 0, -233, 6, -233, 0, 0, 0, true) + || test_crop(a, 5, 3, 0, -233, -233, 12, 0, 0, 0, true) + || test_crop(a, 0, 3, 4, 7, -233, 8, 0, 0, 0, true) + || test_crop(a, 5, 0, 5, 4, -233, 4, 0, 0, 0, true) + || test_crop(a, 5, 3, 6, 2, 7, 9, 0, 0, 0, true) + || test_crop(a, 0, 3, 4, -233, 4, 5, 0, 0, 0, true) + || test_crop(a, 5, 0, 5, 6, 6, 8, 0, 0, 0, true) + || test_crop(a, 5, 3, 6, 4, 4, 4, 0, 0, 0, true) + ; +} + +static int test_crop_1() +{ + ncnn::Mat a = RandomMat(13, 11, 16); + + return 0 + || test_crop(a, 0, 0, 0, -233, -233, -233, 3, 4, 6, false) + || test_crop(a, 0, 3, 0, 10, -233, -233, 0, 3, 4, false) + || test_crop(a, 5, 0, 0, -233, 6, -233, 5, 0, 2, false) + || test_crop(a, 5, 3, 0, -233, -233, 12, 2, 1, 1, false) + || test_crop(a, 0, 3, 4, 7, -233, 8, 3, 4, 4, false) + || test_crop(a, 5, 0, 5, 4, -233, 4, 0, 3, 0, false) + || test_crop(a, 5, 3, 6, 2, 7, 9, 1, 2, 3, false) + || test_crop(a, 0, 3, 4, -233, 4, 5, 3, 2, 1, false) + + || test_crop(a, 0, 0, 0, -233, -233, -233, 3, 4, 6, true) + || test_crop(a, 0, 3, 0, 10, -233, -233, 0, 3, 4, true) + || test_crop(a, 5, 0, 0, -233, 6, -233, 5, 0, 2, true) + || test_crop(a, 5, 3, 0, -233, -233, 12, 2, 1, 1, true) + || test_crop(a, 0, 3, 4, 7, -233, 8, 3, 4, 4, true) + || test_crop(a, 5, 0, 5, 4, -233, 4, 0, 3, 0, true) + || test_crop(a, 5, 3, 6, 2, 7, 9, 1, 2, 3, true) + || test_crop(a, 0, 3, 4, -233, 4, 5, 3, 2, 1, true) + ; +} + +static int test_crop_2() +{ + ncnn::Mat a = RandomMat(13, 11, 17); + + return 0 + || test_crop(a, 0, 0, 0, -233, -233, -233, 3, 4, 6, false) + || test_crop(a, 0, 3, 0, 10, -233, -233, 0, 3, 4, false) + || test_crop(a, 5, 0, 0, -233, 6, -233, 5, 0, 2, false) + || test_crop(a, 5, 3, 0, -233, -233, 12, 2, 1, 1, false) + || test_crop(a, 0, 3, 4, 7, -233, 8, 3, 4, 4, false) + || test_crop(a, 5, 0, 5, 4, -233, 4, 0, 3, 0, false) + || test_crop(a, 5, 3, 6, 2, 7, 9, 1, 2, 3, false) + || test_crop(a, 0, 3, 4, -233, 4, 5, 3, 2, 1, false) + + || test_crop(a, 0, 0, 0, -233, -233, -233, 3, 4, 6, true) + || test_crop(a, 0, 3, 0, 10, -233, -233, 0, 3, 4, true) + || test_crop(a, 5, 0, 0, -233, 6, -233, 5, 0, 2, true) + || test_crop(a, 5, 3, 0, -233, -233, 12, 2, 1, 1, true) + || test_crop(a, 0, 3, 4, 7, -233, 8, 3, 4, 4, true) + || test_crop(a, 5, 0, 5, 4, -233, 4, 0, 3, 0, true) + || test_crop(a, 5, 3, 6, 2, 7, 9, 1, 2, 3, true) + || test_crop(a, 0, 3, 4, -233, 4, 5, 3, 2, 1, true) + ; +} + +static int test_crop_3() +{ + ncnn::Mat a = RandomMat(13, 11, 16); + + return 0 + || test_crop(a, IntArrayMat(0, 0, 0), IntArrayMat(100, 100, 100), IntArrayMat(1, 2, 3), false) + || test_crop(a, IntArrayMat(4), IntArrayMat(8), IntArrayMat(1), false) + || test_crop(a, IntArrayMat(2), IntArrayMat(7), IntArrayMat(2), false) + || test_crop(a, IntArrayMat(3), IntArrayMat(5), IntArrayMat(3), false) + || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(1, 2), false) + || test_crop(a, IntArrayMat(1, 4), IntArrayMat(-5, 7), IntArrayMat(2, -3), false) + || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(-1, -2), false) + || test_crop(a, IntArrayMat(1, 2, 3), IntArrayMat(-3, -2, -1), IntArrayMat(-3, -2, -1), false) + + || test_crop(a, IntArrayMat(0, 0, 0), IntArrayMat(100, 100, 100), IntArrayMat(1, 2, 3), true) + || test_crop(a, IntArrayMat(4), IntArrayMat(8), IntArrayMat(1), true) + || test_crop(a, IntArrayMat(2), IntArrayMat(7), IntArrayMat(2), true) + || test_crop(a, IntArrayMat(3), IntArrayMat(5), IntArrayMat(3), true) + || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(1, 2), true) + || test_crop(a, IntArrayMat(1, 4), IntArrayMat(-5, 7), IntArrayMat(3, -3), true) + || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(-1, -2), true) + || test_crop(a, IntArrayMat(1, 2, 3), IntArrayMat(-3, -2, -1), IntArrayMat(-3, -2, -1), true) + ; +} + +static int test_crop_4() +{ + ncnn::Mat a = RandomMat(13, 11, 17); + + return 0 + || test_crop(a, IntArrayMat(0, 0, 0), IntArrayMat(100, 100, 100), IntArrayMat(1, 2, 3), false) + || test_crop(a, IntArrayMat(4), IntArrayMat(8), IntArrayMat(1), false) + || test_crop(a, IntArrayMat(2), IntArrayMat(7), IntArrayMat(2), false) + || test_crop(a, IntArrayMat(3), IntArrayMat(5), IntArrayMat(3), false) + || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(1, 2), false) + || test_crop(a, IntArrayMat(1, 4), IntArrayMat(-5, 7), IntArrayMat(2, -3), false) + || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(-1, -2), false) + || test_crop(a, IntArrayMat(1, 2, 3), IntArrayMat(-3, -2, -1), IntArrayMat(-3, -2, -1), false) + + || test_crop(a, IntArrayMat(0, 0, 0), IntArrayMat(100, 100, 100), IntArrayMat(1, 2, 3), true) + || test_crop(a, IntArrayMat(4), IntArrayMat(8), IntArrayMat(1), true) + || test_crop(a, IntArrayMat(2), IntArrayMat(7), IntArrayMat(2), true) + || test_crop(a, IntArrayMat(3), IntArrayMat(5), IntArrayMat(3), true) + || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(1, 2), true) + || test_crop(a, IntArrayMat(1, 4), IntArrayMat(-5, 7), IntArrayMat(3, -3), true) + || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(-1, -2), true) + || test_crop(a, IntArrayMat(1, 2, 3), IntArrayMat(-3, -2, -1), IntArrayMat(-3, -2, -1), true) + ; +} + +int main() +{ + SRAND(7767517); + + return 0 + || test_crop_0() + || test_crop_1() + || test_crop_2() + || test_crop_3() + || test_crop_4() + ; +}