diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2c71bc35ae4..967fbd72bef 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -19,7 +19,18 @@ macro(ncnn_add_layer_test class) # enable if WITH_LAYER_xxx option ON if(${WITH_LAYER_${name}}) - ncnn_add_test(${name}) + file(GLOB test_${name}_SRCS "test_${name}.cpp" "test_${name}_*.cpp" LIST_DIRECTORIES FALSE) + + foreach(test_file ${test_${name}_SRCS}) + get_filename_component(test_filename ${test_file} NAME_WE) + add_executable(${test_filename} ${test_file}) + target_link_libraries(${test_filename} PRIVATE ncnn) + + add_test(NAME ${test_filename} COMMAND ${CMAKE_COMMAND} -DTEST_EXECUTABLE=$ -P ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/run_test.cmake) + + # add test to a virtual project group + set_property(TARGET ${test_filename} PROPERTY FOLDER "tests") + endforeach() endif() endmacro() @@ -77,7 +88,7 @@ ncnn_add_layer_test(DeconvolutionDepthWise) ncnn_add_layer_test(DeconvolutionDepthWise1D) ncnn_add_layer_test(DeconvolutionDepthWise3D) ncnn_add_layer_test(DeepCopy) -# ncnn_add_layer_test(DeformableConv2D) too slow :( +ncnn_add_layer_test(DeformableConv2D) ncnn_add_layer_test(Dequantize) ncnn_add_layer_test(Dropout) ncnn_add_layer_test(Einsum) diff --git a/tests/test_binaryop.cpp b/tests/test_binaryop.cpp index 44e3d1b369e..f79ec024be1 100644 --- a/tests/test_binaryop.cpp +++ b/tests/test_binaryop.cpp @@ -382,7 +382,7 @@ int main() { SRAND(7767517); - for (op_type = 0; op_type < OP_TYPE_MAX; op_type++) + for (op_type = 0; op_type < 3; op_type++) { int ret = 0 || test_binaryop_1() diff --git a/tests/test_binaryop_1.cpp b/tests/test_binaryop_1.cpp new file mode 100644 index 00000000000..bc0ec9c8927 --- /dev/null +++ b/tests/test_binaryop_1.cpp @@ -0,0 +1,431 @@ +// 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 "layer/binaryop.h" +#include "testutil.h" + +#define OP_TYPE_MAX 9 + +static int op_type = 0; + +static int test_binaryop(const ncnn::Mat& _a, const ncnn::Mat& _b) +{ + ncnn::Mat a = _a; + ncnn::Mat b = _b; + if (op_type == 6) + { + // value must be positive for pow + Randomize(a, 0.001f, 2.f); + Randomize(b, 0.001f, 2.f); + } + if (op_type == 3 || op_type == 8) + { + // value must be positive for pow + Randomize(a, 0.1f, 10.f); + Randomize(b, 0.1f, 10.f); + } + + ncnn::ParamDict pd; + pd.set(0, op_type); + pd.set(1, 0); // with_scalar + pd.set(2, 0.f); // b + + std::vector weights(0); + + std::vector ab(2); + ab[0] = a; + ab[1] = b; + + int ret = test_layer("BinaryOp", pd, weights, ab); + if (ret != 0) + { + fprintf(stderr, "test_binaryop failed a.dims=%d a=(%d %d %d %d) b.dims=%d b=(%d %d %d %d) op_type=%d\n", a.dims, a.w, a.h, a.d, a.c, b.dims, b.w, b.h, b.d, b.c, op_type); + } + + return ret; +} + +static int test_binaryop(const ncnn::Mat& _a, float b) +{ + ncnn::Mat a = _a; + if (op_type == 6) + { + // value must be positive for pow + Randomize(a, 0.001f, 2.f); + b = RandomFloat(0.001f, 2.f); + } + + ncnn::ParamDict pd; + pd.set(0, op_type); + pd.set(1, 1); // with_scalar + pd.set(2, b); // b + + std::vector weights(0); + + int ret = test_layer("BinaryOp", pd, weights, a); + if (ret != 0) + { + fprintf(stderr, "test_binaryop failed a.dims=%d a=(%d %d %d %d) b=%f op_type=%d\n", a.dims, a.w, a.h, a.d, a.c, b, op_type); + } + + return ret; +} + +// https://github.com/Tencent/ncnn/wiki/binaryop-broadcasting + +static int test_binaryop_1() +{ + return 0 + || test_binaryop(RandomMat(1), 1.f); +} + +static int test_binaryop_2() +{ + return 0 + || test_binaryop(RandomMat(1), RandomMat(1)) + || test_binaryop(RandomMat(1), RandomMat(4)) + || test_binaryop(RandomMat(1), RandomMat(16)); +} + +static int test_binaryop_3() +{ + return 0 + || test_binaryop(RandomMat(1), RandomMat(11, 3)) + || test_binaryop(RandomMat(1), RandomMat(11, 4)) + || test_binaryop(RandomMat(1), RandomMat(11, 16)); +} + +static int test_binaryop_4() +{ + return 0 + || test_binaryop(RandomMat(1), RandomMat(11, 6, 2)) + || test_binaryop(RandomMat(1), RandomMat(11, 6, 4)) + || test_binaryop(RandomMat(1), RandomMat(11, 6, 16)); +} + +static int test_binaryop_5() +{ + return 0 + || test_binaryop(RandomMat(2), 1.f) + || test_binaryop(RandomMat(4), 1.f) + || test_binaryop(RandomMat(16), 1.f); +} + +static int test_binaryop_6() +{ + return 0 + || test_binaryop(RandomMat(2), RandomMat(1)) + || test_binaryop(RandomMat(4), RandomMat(1)) + || test_binaryop(RandomMat(16), RandomMat(1)); +} + +static int test_binaryop_7() +{ + return 0 + || test_binaryop(RandomMat(2), RandomMat(2)) + || test_binaryop(RandomMat(4), RandomMat(4)) + || test_binaryop(RandomMat(16), RandomMat(16)); +} + +static int test_binaryop_8() +{ + return 0 + || test_binaryop(RandomMat(3), RandomMat(11, 3)) + || test_binaryop(RandomMat(4), RandomMat(11, 4)) + || test_binaryop(RandomMat(16), RandomMat(11, 16)); +} + +static int test_binaryop_9() +{ + return 0 + || test_binaryop(RandomMat(2), RandomMat(11, 6, 2)) + || test_binaryop(RandomMat(4), RandomMat(11, 6, 4)) + || test_binaryop(RandomMat(16), RandomMat(11, 6, 16)); +} + +static int test_binaryop_10() +{ + return 0 + || test_binaryop(RandomMat(11, 3), 1.f) + || test_binaryop(RandomMat(11, 4), 1.f) + || test_binaryop(RandomMat(11, 16), 1.f); +} + +static int test_binaryop_11() +{ + return 0 + || test_binaryop(RandomMat(11, 3), RandomMat(1)) + || test_binaryop(RandomMat(11, 4), RandomMat(1)) + || test_binaryop(RandomMat(11, 16), RandomMat(1)); +} + +static int test_binaryop_12() +{ + return 0 + || test_binaryop(RandomMat(11, 3), RandomMat(3)) + || test_binaryop(RandomMat(11, 4), RandomMat(4)) + || test_binaryop(RandomMat(11, 16), RandomMat(16)); +} + +static int test_binaryop_13() +{ + return 0 + || test_binaryop(RandomMat(11, 3), RandomMat(11, 3)) + || test_binaryop(RandomMat(11, 4), RandomMat(11, 4)) + || test_binaryop(RandomMat(11, 16), RandomMat(11, 16)); +} + +static int test_binaryop_14() +{ + return 0 + || test_binaryop(RandomMat(6, 2), RandomMat(11, 6, 2)) + || test_binaryop(RandomMat(6, 4), RandomMat(11, 6, 4)) + || test_binaryop(RandomMat(6, 16), RandomMat(11, 6, 16)); +} + +static int test_binaryop_15() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 2), 1.f) + || test_binaryop(RandomMat(11, 6, 4), 1.f) + || test_binaryop(RandomMat(11, 6, 16), 1.f); +} + +static int test_binaryop_16() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 2), RandomMat(1)) + || test_binaryop(RandomMat(11, 6, 4), RandomMat(1)) + || test_binaryop(RandomMat(11, 6, 16), RandomMat(1)); +} + +static int test_binaryop_17() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 2), RandomMat(2)) + || test_binaryop(RandomMat(11, 6, 4), RandomMat(4)) + || test_binaryop(RandomMat(11, 6, 16), RandomMat(16)); +} + +static int test_binaryop_18() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 2), RandomMat(6, 2)) + || test_binaryop(RandomMat(11, 6, 4), RandomMat(6, 4)) + || test_binaryop(RandomMat(11, 6, 16), RandomMat(6, 16)); +} + +static int test_binaryop_19() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 2), RandomMat(11, 6, 2)) + || test_binaryop(RandomMat(11, 6, 4), RandomMat(11, 6, 4)) + || test_binaryop(RandomMat(11, 6, 16), RandomMat(11, 6, 16)); +} + +static int test_binaryop_20() +{ + return 0 + || test_binaryop(RandomMat(1), RandomMat(11, 3, 4, 2)) + || test_binaryop(RandomMat(1), RandomMat(11, 3, 4, 4)) + || test_binaryop(RandomMat(1), RandomMat(11, 3, 4, 16)); +} + +static int test_binaryop_21() +{ + return 0 + || test_binaryop(RandomMat(2), RandomMat(11, 3, 4, 2)) + || test_binaryop(RandomMat(4), RandomMat(11, 3, 4, 4)) + || test_binaryop(RandomMat(16), RandomMat(11, 3, 4, 16)); +} + +static int test_binaryop_22() +{ + return 0 + || test_binaryop(RandomMat(4, 2), RandomMat(11, 3, 4, 2)) + || test_binaryop(RandomMat(4, 4), RandomMat(11, 3, 4, 4)) + || test_binaryop(RandomMat(4, 16), RandomMat(11, 3, 4, 16)); +} + +static int test_binaryop_23() +{ + return 0 + || test_binaryop(RandomMat(3, 4, 2), RandomMat(11, 3, 4, 2)) + || test_binaryop(RandomMat(3, 4, 4), RandomMat(11, 3, 4, 4)) + || test_binaryop(RandomMat(3, 4, 16), RandomMat(11, 3, 4, 16)); +} + +static int test_binaryop_24() +{ + return 0 + || test_binaryop(RandomMat(11, 3, 4, 2), 1.f) + || test_binaryop(RandomMat(11, 3, 4, 4), 1.f) + || test_binaryop(RandomMat(11, 3, 4, 16), 1.f); +} + +static int test_binaryop_25() +{ + return 0 + || test_binaryop(RandomMat(11, 3, 4, 2), RandomMat(1)) + || test_binaryop(RandomMat(11, 3, 4, 4), RandomMat(1)) + || test_binaryop(RandomMat(11, 3, 4, 16), RandomMat(1)); +} + +static int test_binaryop_26() +{ + return 0 + || test_binaryop(RandomMat(11, 3, 4, 2), RandomMat(2)) + || test_binaryop(RandomMat(11, 3, 4, 4), RandomMat(4)) + || test_binaryop(RandomMat(11, 3, 4, 16), RandomMat(16)); +} + +static int test_binaryop_27() +{ + return 0 + || test_binaryop(RandomMat(11, 3, 4, 2), RandomMat(4, 2)) + || test_binaryop(RandomMat(11, 3, 4, 4), RandomMat(4, 4)) + || test_binaryop(RandomMat(11, 3, 4, 16), RandomMat(4, 16)); +} + +static int test_binaryop_28() +{ + return 0 + || test_binaryop(RandomMat(11, 3, 4, 2), RandomMat(3, 4, 2)) + || test_binaryop(RandomMat(11, 3, 4, 4), RandomMat(3, 4, 4)) + || test_binaryop(RandomMat(11, 3, 4, 16), RandomMat(3, 4, 16)); +} + +static int test_binaryop_29() +{ + return 0 + || test_binaryop(RandomMat(11, 3, 4, 2), RandomMat(11, 3, 4, 2)) + || test_binaryop(RandomMat(11, 3, 4, 4), RandomMat(11, 3, 4, 4)) + || test_binaryop(RandomMat(11, 3, 4, 16), RandomMat(11, 3, 4, 16)); +} + +static int test_binaryop_s1() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 2), RandomMat(1, 1, 2)) + || test_binaryop(RandomMat(11, 6, 4), RandomMat(1, 1, 4)) + || test_binaryop(RandomMat(11, 6, 16), RandomMat(1, 1, 16)); +} + +static int test_binaryop_s2() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 2), RandomMat(11, 6, 1)) + || test_binaryop(RandomMat(11, 6, 4), RandomMat(11, 6, 1)) + || test_binaryop(RandomMat(11, 6, 16), RandomMat(11, 6, 1)); +} + +static int test_binaryop_s3() +{ + return 0 + || test_binaryop(RandomMat(1, 1, 2), RandomMat(11, 6, 2)) + || test_binaryop(RandomMat(1, 1, 4), RandomMat(11, 6, 4)) + || test_binaryop(RandomMat(1, 1, 16), RandomMat(11, 6, 16)); +} + +static int test_binaryop_s4() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 1), RandomMat(11, 6, 2)) + || test_binaryop(RandomMat(11, 6, 1), RandomMat(11, 6, 4)) + || test_binaryop(RandomMat(11, 6, 1), RandomMat(11, 6, 16)); +} + +static int test_binaryop_s5() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 2), RandomMat(1, 6, 2)) + || test_binaryop(RandomMat(11, 6, 4), RandomMat(1, 6, 4)) + || test_binaryop(RandomMat(11, 6, 16), RandomMat(1, 6, 16)); +} + +static int test_binaryop_s6() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 2), RandomMat(11, 1, 2)) + || test_binaryop(RandomMat(11, 6, 4), RandomMat(11, 1, 4)) + || test_binaryop(RandomMat(11, 6, 16), RandomMat(11, 1, 16)); +} + +static int test_binaryop_s7() +{ + return 0 + || test_binaryop(RandomMat(1, 6, 2), RandomMat(11, 6, 2)) + || test_binaryop(RandomMat(1, 6, 4), RandomMat(11, 6, 4)) + || test_binaryop(RandomMat(1, 6, 16), RandomMat(11, 6, 16)); +} + +static int test_binaryop_s8() +{ + return 0 + || test_binaryop(RandomMat(11, 1, 2), RandomMat(11, 6, 2)) + || test_binaryop(RandomMat(11, 1, 4), RandomMat(11, 6, 4)) + || test_binaryop(RandomMat(11, 1, 16), RandomMat(11, 6, 16)); +} + +int main() +{ + SRAND(7767517); + + for (op_type = 3; op_type < 6; op_type++) + { + int ret = 0 + || test_binaryop_1() + || test_binaryop_2() + || test_binaryop_3() + || test_binaryop_4() + || test_binaryop_5() + || test_binaryop_6() + || test_binaryop_7() + || test_binaryop_8() + || test_binaryop_9() + || test_binaryop_10() + || test_binaryop_11() + || test_binaryop_12() + || test_binaryop_13() + || test_binaryop_14() + || test_binaryop_15() + || test_binaryop_16() + || test_binaryop_17() + || test_binaryop_18() + || test_binaryop_19() + || test_binaryop_20() + || test_binaryop_21() + || test_binaryop_22() + || test_binaryop_23() + || test_binaryop_24() + || test_binaryop_25() + || test_binaryop_26() + || test_binaryop_27() + || test_binaryop_28() + || test_binaryop_29() + || test_binaryop_s1() + || test_binaryop_s2() + || test_binaryop_s3() + || test_binaryop_s4() + || test_binaryop_s5() + || test_binaryop_s6() + || test_binaryop_s7() + || test_binaryop_s8(); + + if (ret != 0) + return ret; + } + + return 0; +} diff --git a/tests/test_binaryop_2.cpp b/tests/test_binaryop_2.cpp new file mode 100644 index 00000000000..1608a2880b0 --- /dev/null +++ b/tests/test_binaryop_2.cpp @@ -0,0 +1,431 @@ +// 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 "layer/binaryop.h" +#include "testutil.h" + +#define OP_TYPE_MAX 9 + +static int op_type = 0; + +static int test_binaryop(const ncnn::Mat& _a, const ncnn::Mat& _b) +{ + ncnn::Mat a = _a; + ncnn::Mat b = _b; + if (op_type == 6) + { + // value must be positive for pow + Randomize(a, 0.001f, 2.f); + Randomize(b, 0.001f, 2.f); + } + if (op_type == 3 || op_type == 8) + { + // value must be positive for pow + Randomize(a, 0.1f, 10.f); + Randomize(b, 0.1f, 10.f); + } + + ncnn::ParamDict pd; + pd.set(0, op_type); + pd.set(1, 0); // with_scalar + pd.set(2, 0.f); // b + + std::vector weights(0); + + std::vector ab(2); + ab[0] = a; + ab[1] = b; + + int ret = test_layer("BinaryOp", pd, weights, ab); + if (ret != 0) + { + fprintf(stderr, "test_binaryop failed a.dims=%d a=(%d %d %d %d) b.dims=%d b=(%d %d %d %d) op_type=%d\n", a.dims, a.w, a.h, a.d, a.c, b.dims, b.w, b.h, b.d, b.c, op_type); + } + + return ret; +} + +static int test_binaryop(const ncnn::Mat& _a, float b) +{ + ncnn::Mat a = _a; + if (op_type == 6) + { + // value must be positive for pow + Randomize(a, 0.001f, 2.f); + b = RandomFloat(0.001f, 2.f); + } + + ncnn::ParamDict pd; + pd.set(0, op_type); + pd.set(1, 1); // with_scalar + pd.set(2, b); // b + + std::vector weights(0); + + int ret = test_layer("BinaryOp", pd, weights, a); + if (ret != 0) + { + fprintf(stderr, "test_binaryop failed a.dims=%d a=(%d %d %d %d) b=%f op_type=%d\n", a.dims, a.w, a.h, a.d, a.c, b, op_type); + } + + return ret; +} + +// https://github.com/Tencent/ncnn/wiki/binaryop-broadcasting + +static int test_binaryop_1() +{ + return 0 + || test_binaryop(RandomMat(1), 1.f); +} + +static int test_binaryop_2() +{ + return 0 + || test_binaryop(RandomMat(1), RandomMat(1)) + || test_binaryop(RandomMat(1), RandomMat(4)) + || test_binaryop(RandomMat(1), RandomMat(16)); +} + +static int test_binaryop_3() +{ + return 0 + || test_binaryop(RandomMat(1), RandomMat(11, 3)) + || test_binaryop(RandomMat(1), RandomMat(11, 4)) + || test_binaryop(RandomMat(1), RandomMat(11, 16)); +} + +static int test_binaryop_4() +{ + return 0 + || test_binaryop(RandomMat(1), RandomMat(11, 6, 2)) + || test_binaryop(RandomMat(1), RandomMat(11, 6, 4)) + || test_binaryop(RandomMat(1), RandomMat(11, 6, 16)); +} + +static int test_binaryop_5() +{ + return 0 + || test_binaryop(RandomMat(2), 1.f) + || test_binaryop(RandomMat(4), 1.f) + || test_binaryop(RandomMat(16), 1.f); +} + +static int test_binaryop_6() +{ + return 0 + || test_binaryop(RandomMat(2), RandomMat(1)) + || test_binaryop(RandomMat(4), RandomMat(1)) + || test_binaryop(RandomMat(16), RandomMat(1)); +} + +static int test_binaryop_7() +{ + return 0 + || test_binaryop(RandomMat(2), RandomMat(2)) + || test_binaryop(RandomMat(4), RandomMat(4)) + || test_binaryop(RandomMat(16), RandomMat(16)); +} + +static int test_binaryop_8() +{ + return 0 + || test_binaryop(RandomMat(3), RandomMat(11, 3)) + || test_binaryop(RandomMat(4), RandomMat(11, 4)) + || test_binaryop(RandomMat(16), RandomMat(11, 16)); +} + +static int test_binaryop_9() +{ + return 0 + || test_binaryop(RandomMat(2), RandomMat(11, 6, 2)) + || test_binaryop(RandomMat(4), RandomMat(11, 6, 4)) + || test_binaryop(RandomMat(16), RandomMat(11, 6, 16)); +} + +static int test_binaryop_10() +{ + return 0 + || test_binaryop(RandomMat(11, 3), 1.f) + || test_binaryop(RandomMat(11, 4), 1.f) + || test_binaryop(RandomMat(11, 16), 1.f); +} + +static int test_binaryop_11() +{ + return 0 + || test_binaryop(RandomMat(11, 3), RandomMat(1)) + || test_binaryop(RandomMat(11, 4), RandomMat(1)) + || test_binaryop(RandomMat(11, 16), RandomMat(1)); +} + +static int test_binaryop_12() +{ + return 0 + || test_binaryop(RandomMat(11, 3), RandomMat(3)) + || test_binaryop(RandomMat(11, 4), RandomMat(4)) + || test_binaryop(RandomMat(11, 16), RandomMat(16)); +} + +static int test_binaryop_13() +{ + return 0 + || test_binaryop(RandomMat(11, 3), RandomMat(11, 3)) + || test_binaryop(RandomMat(11, 4), RandomMat(11, 4)) + || test_binaryop(RandomMat(11, 16), RandomMat(11, 16)); +} + +static int test_binaryop_14() +{ + return 0 + || test_binaryop(RandomMat(6, 2), RandomMat(11, 6, 2)) + || test_binaryop(RandomMat(6, 4), RandomMat(11, 6, 4)) + || test_binaryop(RandomMat(6, 16), RandomMat(11, 6, 16)); +} + +static int test_binaryop_15() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 2), 1.f) + || test_binaryop(RandomMat(11, 6, 4), 1.f) + || test_binaryop(RandomMat(11, 6, 16), 1.f); +} + +static int test_binaryop_16() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 2), RandomMat(1)) + || test_binaryop(RandomMat(11, 6, 4), RandomMat(1)) + || test_binaryop(RandomMat(11, 6, 16), RandomMat(1)); +} + +static int test_binaryop_17() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 2), RandomMat(2)) + || test_binaryop(RandomMat(11, 6, 4), RandomMat(4)) + || test_binaryop(RandomMat(11, 6, 16), RandomMat(16)); +} + +static int test_binaryop_18() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 2), RandomMat(6, 2)) + || test_binaryop(RandomMat(11, 6, 4), RandomMat(6, 4)) + || test_binaryop(RandomMat(11, 6, 16), RandomMat(6, 16)); +} + +static int test_binaryop_19() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 2), RandomMat(11, 6, 2)) + || test_binaryop(RandomMat(11, 6, 4), RandomMat(11, 6, 4)) + || test_binaryop(RandomMat(11, 6, 16), RandomMat(11, 6, 16)); +} + +static int test_binaryop_20() +{ + return 0 + || test_binaryop(RandomMat(1), RandomMat(11, 3, 4, 2)) + || test_binaryop(RandomMat(1), RandomMat(11, 3, 4, 4)) + || test_binaryop(RandomMat(1), RandomMat(11, 3, 4, 16)); +} + +static int test_binaryop_21() +{ + return 0 + || test_binaryop(RandomMat(2), RandomMat(11, 3, 4, 2)) + || test_binaryop(RandomMat(4), RandomMat(11, 3, 4, 4)) + || test_binaryop(RandomMat(16), RandomMat(11, 3, 4, 16)); +} + +static int test_binaryop_22() +{ + return 0 + || test_binaryop(RandomMat(4, 2), RandomMat(11, 3, 4, 2)) + || test_binaryop(RandomMat(4, 4), RandomMat(11, 3, 4, 4)) + || test_binaryop(RandomMat(4, 16), RandomMat(11, 3, 4, 16)); +} + +static int test_binaryop_23() +{ + return 0 + || test_binaryop(RandomMat(3, 4, 2), RandomMat(11, 3, 4, 2)) + || test_binaryop(RandomMat(3, 4, 4), RandomMat(11, 3, 4, 4)) + || test_binaryop(RandomMat(3, 4, 16), RandomMat(11, 3, 4, 16)); +} + +static int test_binaryop_24() +{ + return 0 + || test_binaryop(RandomMat(11, 3, 4, 2), 1.f) + || test_binaryop(RandomMat(11, 3, 4, 4), 1.f) + || test_binaryop(RandomMat(11, 3, 4, 16), 1.f); +} + +static int test_binaryop_25() +{ + return 0 + || test_binaryop(RandomMat(11, 3, 4, 2), RandomMat(1)) + || test_binaryop(RandomMat(11, 3, 4, 4), RandomMat(1)) + || test_binaryop(RandomMat(11, 3, 4, 16), RandomMat(1)); +} + +static int test_binaryop_26() +{ + return 0 + || test_binaryop(RandomMat(11, 3, 4, 2), RandomMat(2)) + || test_binaryop(RandomMat(11, 3, 4, 4), RandomMat(4)) + || test_binaryop(RandomMat(11, 3, 4, 16), RandomMat(16)); +} + +static int test_binaryop_27() +{ + return 0 + || test_binaryop(RandomMat(11, 3, 4, 2), RandomMat(4, 2)) + || test_binaryop(RandomMat(11, 3, 4, 4), RandomMat(4, 4)) + || test_binaryop(RandomMat(11, 3, 4, 16), RandomMat(4, 16)); +} + +static int test_binaryop_28() +{ + return 0 + || test_binaryop(RandomMat(11, 3, 4, 2), RandomMat(3, 4, 2)) + || test_binaryop(RandomMat(11, 3, 4, 4), RandomMat(3, 4, 4)) + || test_binaryop(RandomMat(11, 3, 4, 16), RandomMat(3, 4, 16)); +} + +static int test_binaryop_29() +{ + return 0 + || test_binaryop(RandomMat(11, 3, 4, 2), RandomMat(11, 3, 4, 2)) + || test_binaryop(RandomMat(11, 3, 4, 4), RandomMat(11, 3, 4, 4)) + || test_binaryop(RandomMat(11, 3, 4, 16), RandomMat(11, 3, 4, 16)); +} + +static int test_binaryop_s1() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 2), RandomMat(1, 1, 2)) + || test_binaryop(RandomMat(11, 6, 4), RandomMat(1, 1, 4)) + || test_binaryop(RandomMat(11, 6, 16), RandomMat(1, 1, 16)); +} + +static int test_binaryop_s2() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 2), RandomMat(11, 6, 1)) + || test_binaryop(RandomMat(11, 6, 4), RandomMat(11, 6, 1)) + || test_binaryop(RandomMat(11, 6, 16), RandomMat(11, 6, 1)); +} + +static int test_binaryop_s3() +{ + return 0 + || test_binaryop(RandomMat(1, 1, 2), RandomMat(11, 6, 2)) + || test_binaryop(RandomMat(1, 1, 4), RandomMat(11, 6, 4)) + || test_binaryop(RandomMat(1, 1, 16), RandomMat(11, 6, 16)); +} + +static int test_binaryop_s4() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 1), RandomMat(11, 6, 2)) + || test_binaryop(RandomMat(11, 6, 1), RandomMat(11, 6, 4)) + || test_binaryop(RandomMat(11, 6, 1), RandomMat(11, 6, 16)); +} + +static int test_binaryop_s5() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 2), RandomMat(1, 6, 2)) + || test_binaryop(RandomMat(11, 6, 4), RandomMat(1, 6, 4)) + || test_binaryop(RandomMat(11, 6, 16), RandomMat(1, 6, 16)); +} + +static int test_binaryop_s6() +{ + return 0 + || test_binaryop(RandomMat(11, 6, 2), RandomMat(11, 1, 2)) + || test_binaryop(RandomMat(11, 6, 4), RandomMat(11, 1, 4)) + || test_binaryop(RandomMat(11, 6, 16), RandomMat(11, 1, 16)); +} + +static int test_binaryop_s7() +{ + return 0 + || test_binaryop(RandomMat(1, 6, 2), RandomMat(11, 6, 2)) + || test_binaryop(RandomMat(1, 6, 4), RandomMat(11, 6, 4)) + || test_binaryop(RandomMat(1, 6, 16), RandomMat(11, 6, 16)); +} + +static int test_binaryop_s8() +{ + return 0 + || test_binaryop(RandomMat(11, 1, 2), RandomMat(11, 6, 2)) + || test_binaryop(RandomMat(11, 1, 4), RandomMat(11, 6, 4)) + || test_binaryop(RandomMat(11, 1, 16), RandomMat(11, 6, 16)); +} + +int main() +{ + SRAND(7767517); + + for (op_type = 6; op_type < OP_TYPE_MAX; op_type++) + { + int ret = 0 + || test_binaryop_1() + || test_binaryop_2() + || test_binaryop_3() + || test_binaryop_4() + || test_binaryop_5() + || test_binaryop_6() + || test_binaryop_7() + || test_binaryop_8() + || test_binaryop_9() + || test_binaryop_10() + || test_binaryop_11() + || test_binaryop_12() + || test_binaryop_13() + || test_binaryop_14() + || test_binaryop_15() + || test_binaryop_16() + || test_binaryop_17() + || test_binaryop_18() + || test_binaryop_19() + || test_binaryop_20() + || test_binaryop_21() + || test_binaryop_22() + || test_binaryop_23() + || test_binaryop_24() + || test_binaryop_25() + || test_binaryop_26() + || test_binaryop_27() + || test_binaryop_28() + || test_binaryop_29() + || test_binaryop_s1() + || test_binaryop_s2() + || test_binaryop_s3() + || test_binaryop_s4() + || test_binaryop_s5() + || test_binaryop_s6() + || test_binaryop_s7() + || test_binaryop_s8(); + + if (ret != 0) + return ret; + } + + return 0; +} diff --git a/tests/test_convolution.cpp b/tests/test_convolution.cpp index 5f7f5d20993..9140750e8c1 100644 --- a/tests/test_convolution.cpp +++ b/tests/test_convolution.cpp @@ -82,7 +82,7 @@ static int test_convolution_0() {7, 2, 1, -233}, }; - for (int i = 0; i < 16; i++) + for (int i = 0; i < 12; i++) { const int k = kdsp[i][0]; const int d = kdsp[i][1]; @@ -125,313 +125,12 @@ static int test_convolution_0() return -1; } - return 0 - || test_convolution(7, 5, 1, 4, 3, 1, 1, 1, 1) - || test_convolution(14, 5, 1, 4, 3, 1, 2, 1, 1) - || test_convolution(11, 5, 2, 12, 2, 2, 2, 1, 1) - || test_convolution(15, 11, 4, 4, 3, 1, 1, 1, 1) - || test_convolution(15, 11, 8, 8, 3, 1, 1, 1, 1) - || test_convolution(11, 11, 8, 16, 3, 1, 1, 1, 1) - || test_convolution(13, 16, 16, 24, 3, 1, 1, 1, 1) - || test_convolution(20, 19, 24, 24, 3, 1, 1, 1, 1) - || test_convolution(8, 8, 16, 24, 3, 1, 1, 1, 0) - || test_convolution(4, 8, 16, 24, 3, 1, 1, 1, 1) - || test_convolution(4, 20, 16, 24, 3, 1, 1, 1, 0) - || test_convolution(6, 7, 64, 64, 3, 1, 2, 0, 1) - || test_convolution(15, 17, 24, 32, 1, 1, 1, 0, 0) - || test_convolution(15, 17, 24, 32, 1, 1, 2, 0, 1) - || test_convolution(15, 17, 24, 32, 3, 1, 2, 0, 1) - || test_convolution(15, 17, 32, 24, 1, 1, 1, 0, 0) - || test_convolution(15, 17, 32, 24, 1, 1, 2, 0, 1) - || test_convolution(15, 17, 32, 24, 3, 1, 2, 0, 1) - || test_convolution(15, 17, 32, 28, 1, 1, 1, 0, 0) - || test_convolution(15, 17, 32, 28, 1, 1, 2, 0, 1) - || test_convolution(15, 17, 32, 28, 3, 1, 2, 0, 1) - || test_convolution(15, 17, 26, 32, 1, 1, 1, 0, 0) - || test_convolution(15, 17, 26, 32, 1, 1, 2, 0, 1) - || test_convolution(15, 17, 26, 32, 3, 1, 2, 0, 1) - || test_convolution(15, 17, 32, 26, 1, 1, 1, 0, 0) - || test_convolution(15, 17, 32, 26, 1, 1, 2, 0, 1) - || test_convolution(15, 17, 32, 26, 3, 1, 2, 0, 1) - || test_convolution(30, 30, 32, 26, 3, 1, 1, 1, 0) - || test_convolution(12, 18, 8, 16, 3, 1, 1, 1, 1) - || test_convolution(42, 18, 32, 160, 3, 1, 1, 1, 1) - || test_convolution(12, 18, 32, 160, 3, 1, 1, 1, 1) - || test_convolution(12, 18, 4, 12, 3, 1, 1, 1, 1) - || test_convolution(42, 18, 28, 140, 3, 1, 1, 1, 1) - || test_convolution(12, 18, 28, 140, 3, 1, 1, 1, 1); -} - -static int test_convolution_vec(int w, int outch, int kernel, int dilation, int stride, int pad, int bias) -{ - ncnn::Mat a = RandomMat(w); - - ncnn::ParamDict pd; - pd.set(0, outch); // num_output - pd.set(1, kernel); // kernel_w - pd.set(2, dilation); // dilation_w - pd.set(3, stride); // stride_w - pd.set(4, pad); // pad_w - pd.set(5, bias); // bias_term - pd.set(6, outch * w * kernel * kernel); - - int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 - ncnn::Mat activation_params(2); - activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha - activation_params[1] = RandomFloat(0, 1); // beta - pd.set(9, activation_type); - pd.set(10, activation_params); - - std::vector weights(bias ? 2 : 1); - weights[0] = RandomMat(outch * w * kernel * kernel); - if (bias) - weights[1] = RandomMat(outch); - - int ret = test_layer("Convolution", pd, weights, a); - if (ret != 0) - { - fprintf(stderr, "test_convolution_vec failed w=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]); - } - - return ret; -} - -static int test_convolution_2() -{ - return 0 - || test_convolution_vec(1, 1, 1, 1, 1, 0, 1) - || test_convolution_vec(11, 12, 1, 1, 1, 0, 0) - || test_convolution_vec(20, 15, 1, 1, 1, 0, 1) - || test_convolution_vec(12, 20, 1, 1, 1, 0, 0) - || test_convolution_vec(3, 24, 1, 1, 1, 0, 1) - || test_convolution_vec(24, 5, 1, 1, 1, 0, 0) - || test_convolution_vec(32, 24, 1, 1, 1, 0, 1) - || test_convolution_vec(12, 32, 1, 1, 1, 0, 0) - || test_convolution_vec(64, 20, 1, 1, 1, 0, 1) - || test_convolution_vec(64, 128, 1, 1, 1, 0, 0); -} - -static int test_convolution_dynamic(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias) -{ - ncnn::Mat a = RandomMat(w, h, c); - - ncnn::ParamDict pd; - pd.set(0, 0); - pd.set(1, 0); - pd.set(2, dilation); - pd.set(3, stride); - pd.set(4, pad); - pd.set(5, bias); - pd.set(6, 0); - pd.set(19, 1); // dynamic weight - - int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 - ncnn::Mat activation_params(2); - activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha - activation_params[1] = RandomFloat(0, 1); // beta - pd.set(9, activation_type); - pd.set(10, activation_params); - - std::vector as(bias ? 3 : 2); - as[0] = a; - as[1] = RandomMat(kernel, kernel, c, outch); - if (bias) - as[2] = RandomMat(outch); - - std::vector weights(0); - - int ret = test_layer("Convolution", pd, weights, as); - if (ret != 0) - { - fprintf(stderr, "test_convolution_dynamic failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]); - } - - return ret; -} - -static int test_convolution_3() -{ - static const int kdsp[7][4] = { - {1, 1, 1, 0}, - {1, 1, 2, 0}, - {2, 1, 1, 1}, - {2, 1, 2, -233}, - {3, 1, 1, 1}, - {3, 1, 2, 1}, - {3, 2, 1, -234}, - }; - - for (int i = 0; i < 7; i++) - { - const int k = kdsp[i][0]; - const int d = kdsp[i][1]; - const int s = kdsp[i][2]; - const int p = kdsp[i][3]; - - int ret = 0 - || test_convolution_dynamic(11, 10, 1, 1, k, d, s, p, 1) - || test_convolution_dynamic(11, 10, 4, 13, k, d, s, p, 0) - || test_convolution_dynamic(11, 10, 13, 4, k, d, s, p, 1) - || test_convolution_dynamic(11, 10, 12, 12, k, d, s, p, 0) - || test_convolution_dynamic(11, 10, 8, 12, k, d, s, p, 1) - || test_convolution_dynamic(11, 10, 8, 13, k, d, s, p, 0) - || test_convolution_dynamic(11, 10, 13, 8, k, d, s, p, 1) - || test_convolution_dynamic(11, 10, 12, 16, k, d, s, p, 0) - || test_convolution_dynamic(11, 10, 15, 15, k, d, s, p, 0) - || test_convolution_dynamic(11, 10, 16, 16, k, d, s, p, 0); - - if (ret != 0) - return -1; - } - return 0; } -#if NCNN_INT8 -static int test_convolution_int8(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, bool requant = false) -{ - ncnn::Mat a = RandomMat(w, h, c); - - ncnn::ParamDict pd; - pd.set(0, outch); - pd.set(1, kernel); - pd.set(2, dilation); - pd.set(3, stride); - pd.set(4, pad); - pd.set(5, bias); - pd.set(6, outch * c * kernel * kernel); - pd.set(8, requant ? 101 : 1); // int8_scale_term - - int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 - ncnn::Mat activation_params(2); - activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha - activation_params[1] = RandomFloat(0, 1); // beta - pd.set(9, activation_type); - pd.set(10, activation_params); - - std::vector weights(bias ? 5 : 4); - weights[0] = RandomMat(outch * c * kernel * kernel); - - ncnn::Mat weight_scales = scales_mat(weights[0], outch, c * kernel * kernel, c * kernel * kernel); - ncnn::Mat input_scales = scales_mat(a, 1, w * h * c, a.cstep); - ncnn::Mat top_scales = requant ? scales_mat(a, 1, w * h * c, a.cstep) : ncnn::Mat(); - if (bias) - { - weights[1] = RandomMat(outch); - weights[2] = weight_scales; - weights[3] = input_scales; - weights[4] = top_scales; - } - else - { - weights[1] = weight_scales; - weights[2] = input_scales; - weights[3] = top_scales; - } - - int flag = TEST_LAYER_DISABLE_GPU_TESTING; - int ret = test_layer("Convolution", pd, weights, a, requant ? 1.0f : 0.001f, 0, flag); - if (ret != 0) - { - fprintf(stderr, "test_convolution_int8 failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d requant=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, requant, activation_type, activation_params[0], activation_params[1]); - } - - return ret; -} - -static int test_convolution_1() -{ - static const int kdsp[16][4] = { - {1, 1, 1, 0}, - {1, 1, 2, 0}, - {2, 1, 1, 1}, - {2, 1, 2, -233}, - {3, 1, 1, 1}, - {3, 1, 2, 1}, - {3, 2, 1, 1}, - {4, 1, 1, 2}, - {4, 1, 2, -233}, - {4, 2, 1, -234}, - {5, 1, 1, -234}, - {5, 1, 2, 2}, - {5, 2, 2, 2}, - {7, 1, 1, 3}, - {7, 1, 2, 3}, - {7, 2, 1, -233}, - }; - - for (int i = 0; i < 16; i++) - { - const int k = kdsp[i][0]; - const int d = kdsp[i][1]; - const int s = kdsp[i][2]; - const int p = kdsp[i][3]; - - int ret = 0 - || test_convolution_int8(9, 7, 1, 1, k, d, s, p, 1) - || test_convolution_int8(9, 7, 2, 2, k, d, s, p, 1) - || test_convolution_int8(9, 7, 3, 3, k, d, s, p, 1) - || test_convolution_int8(9, 7, 4, 4, k, d, s, p, 1) - || test_convolution_int8(9, 7, 7, 7, k, d, s, p, 1) - || test_convolution_int8(9, 7, 8, 8, k, d, s, p, 1) - || test_convolution_int8(9, 7, 15, 15, k, d, s, p, 1) - || test_convolution_int8(9, 7, 16, 15, k, d, s, p, 1) - || test_convolution_int8(9, 7, 15, 16, k, d, s, p, 1) - || test_convolution_int8(9, 7, 16, 16, k, d, s, p, 1); - - if (ret != 0) - return -1; - } - for (int i = 0; i < 16; i++) - { - const int k = kdsp[i][0]; - const int d = kdsp[i][1]; - const int s = kdsp[i][2]; - const int p = kdsp[i][3]; - - int ret = 0 - || test_convolution_int8(9, 7, 1, 1, k, d, s, p, 1, true) - || test_convolution_int8(9, 7, 1, 1, k, d, s, p, 1, true) - || test_convolution_int8(9, 7, 2, 2, k, d, s, p, 1, true) - || test_convolution_int8(9, 7, 3, 3, k, d, s, p, 1, true) - || test_convolution_int8(9, 7, 4, 4, k, d, s, p, 1, true) - || test_convolution_int8(9, 7, 7, 7, k, d, s, p, 1, true) - || test_convolution_int8(9, 7, 8, 8, k, d, s, p, 1, true) - || test_convolution_int8(9, 7, 15, 15, k, d, s, p, 1, true) - || test_convolution_int8(9, 7, 16, 15, k, d, s, p, 1, true) - || test_convolution_int8(9, 7, 15, 16, k, d, s, p, 1, true) - || test_convolution_int8(9, 7, 16, 16, k, d, s, p, 1, true); - - if (ret != 0) - return -1; - } - - return 0 - || test_convolution_int8(11, 11, 8, 16, 3, 1, 1, 1, 1) - || test_convolution_int8(13, 16, 16, 24, 3, 1, 1, 1, 1) - || test_convolution_int8(8, 8, 16, 24, 3, 1, 1, 1, 0) - || test_convolution_int8(4, 8, 16, 24, 3, 1, 1, 1, 1) - || test_convolution_int8(4, 20, 16, 24, 3, 1, 1, 1, 0) - || test_convolution_int8(6, 7, 64, 64, 3, 1, 2, 0, 1) - || test_convolution_int8(25, 33, 16, 15, 3, 1, 1, 1, 0) - || test_convolution_int8(7, 7, 15, 12, 3, 1, 1, 1, 0); -} -#endif // NCNN_INT8 - int main() { SRAND(7767517); -#if NCNN_INT8 - return 0 - || test_convolution_0() - || test_convolution_1() - || test_convolution_2() - || test_convolution_3(); -#else - return 0 - || test_convolution_0() - || test_convolution_2() - || test_convolution_3(); -#endif + return test_convolution_0(); } diff --git a/tests/test_convolution_1.cpp b/tests/test_convolution_1.cpp new file mode 100644 index 00000000000..b7641a4ea0f --- /dev/null +++ b/tests/test_convolution_1.cpp @@ -0,0 +1,136 @@ +// Tencent is pleased to support the open source community by making ncnn available. +// +// Copyright (C) 2019 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 "layer/convolution.h" +#include "testutil.h" + +static int test_convolution(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias) +{ + ncnn::Mat a = RandomMat(w, h, c); + + ncnn::ParamDict pd; + pd.set(0, outch); + pd.set(1, kernel); + pd.set(2, dilation); + pd.set(3, stride); + pd.set(4, pad); + pd.set(5, bias); + pd.set(6, outch * c * kernel * kernel); + + int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 + ncnn::Mat activation_params(2); + activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha + activation_params[1] = RandomFloat(0, 1); // beta + pd.set(9, activation_type); + pd.set(10, activation_params); + + std::vector weights(bias ? 2 : 1); + weights[0] = RandomMat(outch * c * kernel * kernel); + if (bias) + weights[1] = RandomMat(outch); + + float epsilon = 0.001; + // larget epsilon for winograd optimization + if (kernel == 3 && dilation == 1 && stride == 1 && c >= 16 && outch >= 16) + { + Randomize(a, -1, 1); + if (c >= 64 || outch >= 64) + Randomize(weights[0], -0.3, 0.3); + else + Randomize(weights[0], -1, 1); + epsilon = 0.002; + } + + int ret = test_layer("Convolution", pd, weights, a, epsilon); + if (ret != 0) + { + fprintf(stderr, "test_convolution failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]); + } + + return ret; +} + +static int test_convolution_0() +{ + static const int kdsp[16][4] = { + {1, 1, 1, 0}, + {1, 1, 2, 0}, + {2, 1, 1, 1}, + {2, 1, 2, -233}, + {3, 1, 1, 1}, + {3, 1, 2, 1}, + {3, 2, 1, 1}, + {4, 1, 1, 2}, + {4, 1, 2, -233}, + {4, 2, 1, -234}, + {5, 1, 1, -234}, + {5, 1, 2, 2}, + {5, 2, 2, 2}, + {7, 1, 1, 3}, + {7, 1, 2, 3}, + {7, 2, 1, -233}, + }; + + for (int i = 12; i < 16; i++) + { + const int k = kdsp[i][0]; + const int d = kdsp[i][1]; + const int s = kdsp[i][2]; + const int p = kdsp[i][3]; + + int ret = 0 + || test_convolution(9, 7, 1, 1, k, d, s, p, 1) + || test_convolution(9, 7, 4, 13, k, d, s, p, 0) + || test_convolution(9, 7, 13, 4, k, d, s, p, 1) + || test_convolution(9, 7, 12, 12, k, d, s, p, 0) + || test_convolution(9, 7, 8, 12, k, d, s, p, 1) + || test_convolution(9, 7, 8, 13, k, d, s, p, 0) + || test_convolution(9, 7, 13, 8, k, d, s, p, 1) + || test_convolution(9, 7, 12, 16, k, d, s, p, 0) + || test_convolution(9, 7, 15, 15, k, d, s, p, 0) + || test_convolution(9, 7, 16, 16, k, d, s, p, 0) + || test_convolution(18, 17, 1, 1, k, d, s, p, 1) + || test_convolution(18, 17, 4, 13, k, d, s, p, 0) + || test_convolution(18, 17, 13, 4, k, d, s, p, 1) + || test_convolution(18, 17, 12, 12, k, d, s, p, 0) + || test_convolution(18, 17, 8, 12, k, d, s, p, 1) + || test_convolution(18, 17, 8, 13, k, d, s, p, 0) + || test_convolution(18, 17, 13, 8, k, d, s, p, 1) + || test_convolution(18, 17, 12, 16, k, d, s, p, 0) + || test_convolution(18, 17, 15, 15, k, d, s, p, 0) + || test_convolution(18, 17, 16, 16, k, d, s, p, 0) + || test_convolution(25, 33, 1, 1, k, d, s, p, 1) + || test_convolution(25, 33, 4, 13, k, d, s, p, 0) + || test_convolution(25, 33, 13, 4, k, d, s, p, 1) + || test_convolution(25, 33, 12, 12, k, d, s, p, 0) + || test_convolution(25, 33, 8, 12, k, d, s, p, 1) + || test_convolution(25, 33, 8, 13, k, d, s, p, 0) + || test_convolution(25, 33, 13, 8, k, d, s, p, 1) + || test_convolution(25, 33, 12, 16, k, d, s, p, 0) + || test_convolution(25, 33, 15, 15, k, d, s, p, 0) + || test_convolution(25, 33, 16, 16, k, d, s, p, 0); + + if (ret != 0) + return -1; + } + + return 0; +} + +int main() +{ + SRAND(7767517); + + return test_convolution_0(); +} diff --git a/tests/test_convolution_2.cpp b/tests/test_convolution_2.cpp new file mode 100644 index 00000000000..2dbaf59b3ba --- /dev/null +++ b/tests/test_convolution_2.cpp @@ -0,0 +1,108 @@ +// Tencent is pleased to support the open source community by making ncnn available. +// +// Copyright (C) 2019 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 "layer/convolution.h" +#include "testutil.h" + +static int test_convolution(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias) +{ + ncnn::Mat a = RandomMat(w, h, c); + + ncnn::ParamDict pd; + pd.set(0, outch); + pd.set(1, kernel); + pd.set(2, dilation); + pd.set(3, stride); + pd.set(4, pad); + pd.set(5, bias); + pd.set(6, outch * c * kernel * kernel); + + int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 + ncnn::Mat activation_params(2); + activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha + activation_params[1] = RandomFloat(0, 1); // beta + pd.set(9, activation_type); + pd.set(10, activation_params); + + std::vector weights(bias ? 2 : 1); + weights[0] = RandomMat(outch * c * kernel * kernel); + if (bias) + weights[1] = RandomMat(outch); + + float epsilon = 0.001; + // larget epsilon for winograd optimization + if (kernel == 3 && dilation == 1 && stride == 1 && c >= 16 && outch >= 16) + { + Randomize(a, -1, 1); + if (c >= 64 || outch >= 64) + Randomize(weights[0], -0.3, 0.3); + else + Randomize(weights[0], -1, 1); + epsilon = 0.002; + } + + int ret = test_layer("Convolution", pd, weights, a, epsilon); + if (ret != 0) + { + fprintf(stderr, "test_convolution failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]); + } + + return ret; +} + +static int test_convolution_0() +{ + return 0 + || test_convolution(7, 5, 1, 4, 3, 1, 1, 1, 1) + || test_convolution(14, 5, 1, 4, 3, 1, 2, 1, 1) + || test_convolution(11, 5, 2, 12, 2, 2, 2, 1, 1) + || test_convolution(15, 11, 4, 4, 3, 1, 1, 1, 1) + || test_convolution(15, 11, 8, 8, 3, 1, 1, 1, 1) + || test_convolution(11, 11, 8, 16, 3, 1, 1, 1, 1) + || test_convolution(13, 16, 16, 24, 3, 1, 1, 1, 1) + || test_convolution(20, 19, 24, 24, 3, 1, 1, 1, 1) + || test_convolution(8, 8, 16, 24, 3, 1, 1, 1, 0) + || test_convolution(4, 8, 16, 24, 3, 1, 1, 1, 1) + || test_convolution(4, 20, 16, 24, 3, 1, 1, 1, 0) + || test_convolution(6, 7, 64, 64, 3, 1, 2, 0, 1) + || test_convolution(15, 17, 24, 32, 1, 1, 1, 0, 0) + || test_convolution(15, 17, 24, 32, 1, 1, 2, 0, 1) + || test_convolution(15, 17, 24, 32, 3, 1, 2, 0, 1) + || test_convolution(15, 17, 32, 24, 1, 1, 1, 0, 0) + || test_convolution(15, 17, 32, 24, 1, 1, 2, 0, 1) + || test_convolution(15, 17, 32, 24, 3, 1, 2, 0, 1) + || test_convolution(15, 17, 32, 28, 1, 1, 1, 0, 0) + || test_convolution(15, 17, 32, 28, 1, 1, 2, 0, 1) + || test_convolution(15, 17, 32, 28, 3, 1, 2, 0, 1) + || test_convolution(15, 17, 26, 32, 1, 1, 1, 0, 0) + || test_convolution(15, 17, 26, 32, 1, 1, 2, 0, 1) + || test_convolution(15, 17, 26, 32, 3, 1, 2, 0, 1) + || test_convolution(15, 17, 32, 26, 1, 1, 1, 0, 0) + || test_convolution(15, 17, 32, 26, 1, 1, 2, 0, 1) + || test_convolution(15, 17, 32, 26, 3, 1, 2, 0, 1) + || test_convolution(30, 30, 32, 26, 3, 1, 1, 1, 0) + || test_convolution(12, 18, 8, 16, 3, 1, 1, 1, 1) + || test_convolution(42, 18, 32, 160, 3, 1, 1, 1, 1) + || test_convolution(12, 18, 32, 160, 3, 1, 1, 1, 1) + || test_convolution(12, 18, 4, 12, 3, 1, 1, 1, 1) + || test_convolution(42, 18, 28, 140, 3, 1, 1, 1, 1) + || test_convolution(12, 18, 28, 140, 3, 1, 1, 1, 1); +} + +int main() +{ + SRAND(7767517); + + return test_convolution_0(); +} diff --git a/tests/test_convolution_3.cpp b/tests/test_convolution_3.cpp new file mode 100644 index 00000000000..3d6f91d096a --- /dev/null +++ b/tests/test_convolution_3.cpp @@ -0,0 +1,288 @@ +// Tencent is pleased to support the open source community by making ncnn available. +// +// Copyright (C) 2019 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 "layer/convolution.h" +#include "testutil.h" + +static int test_convolution_vec(int w, int outch, int kernel, int dilation, int stride, int pad, int bias) +{ + ncnn::Mat a = RandomMat(w); + + ncnn::ParamDict pd; + pd.set(0, outch); // num_output + pd.set(1, kernel); // kernel_w + pd.set(2, dilation); // dilation_w + pd.set(3, stride); // stride_w + pd.set(4, pad); // pad_w + pd.set(5, bias); // bias_term + pd.set(6, outch * w * kernel * kernel); + + int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 + ncnn::Mat activation_params(2); + activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha + activation_params[1] = RandomFloat(0, 1); // beta + pd.set(9, activation_type); + pd.set(10, activation_params); + + std::vector weights(bias ? 2 : 1); + weights[0] = RandomMat(outch * w * kernel * kernel); + if (bias) + weights[1] = RandomMat(outch); + + int ret = test_layer("Convolution", pd, weights, a); + if (ret != 0) + { + fprintf(stderr, "test_convolution_vec failed w=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]); + } + + return ret; +} + +static int test_convolution_2() +{ + return 0 + || test_convolution_vec(1, 1, 1, 1, 1, 0, 1) + || test_convolution_vec(11, 12, 1, 1, 1, 0, 0) + || test_convolution_vec(20, 15, 1, 1, 1, 0, 1) + || test_convolution_vec(12, 20, 1, 1, 1, 0, 0) + || test_convolution_vec(3, 24, 1, 1, 1, 0, 1) + || test_convolution_vec(24, 5, 1, 1, 1, 0, 0) + || test_convolution_vec(32, 24, 1, 1, 1, 0, 1) + || test_convolution_vec(12, 32, 1, 1, 1, 0, 0) + || test_convolution_vec(64, 20, 1, 1, 1, 0, 1) + || test_convolution_vec(64, 128, 1, 1, 1, 0, 0); +} + +static int test_convolution_dynamic(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias) +{ + ncnn::Mat a = RandomMat(w, h, c); + + ncnn::ParamDict pd; + pd.set(0, 0); + pd.set(1, 0); + pd.set(2, dilation); + pd.set(3, stride); + pd.set(4, pad); + pd.set(5, bias); + pd.set(6, 0); + pd.set(19, 1); // dynamic weight + + int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 + ncnn::Mat activation_params(2); + activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha + activation_params[1] = RandomFloat(0, 1); // beta + pd.set(9, activation_type); + pd.set(10, activation_params); + + std::vector as(bias ? 3 : 2); + as[0] = a; + as[1] = RandomMat(kernel, kernel, c, outch); + if (bias) + as[2] = RandomMat(outch); + + std::vector weights(0); + + int ret = test_layer("Convolution", pd, weights, as); + if (ret != 0) + { + fprintf(stderr, "test_convolution_dynamic failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]); + } + + return ret; +} + +static int test_convolution_3() +{ + static const int kdsp[7][4] = { + {1, 1, 1, 0}, + {1, 1, 2, 0}, + {2, 1, 1, 1}, + {2, 1, 2, -233}, + {3, 1, 1, 1}, + {3, 1, 2, 1}, + {3, 2, 1, -234}, + }; + + for (int i = 0; i < 7; i++) + { + const int k = kdsp[i][0]; + const int d = kdsp[i][1]; + const int s = kdsp[i][2]; + const int p = kdsp[i][3]; + + int ret = 0 + || test_convolution_dynamic(11, 10, 1, 1, k, d, s, p, 1) + || test_convolution_dynamic(11, 10, 4, 13, k, d, s, p, 0) + || test_convolution_dynamic(11, 10, 13, 4, k, d, s, p, 1) + || test_convolution_dynamic(11, 10, 12, 12, k, d, s, p, 0) + || test_convolution_dynamic(11, 10, 8, 12, k, d, s, p, 1) + || test_convolution_dynamic(11, 10, 8, 13, k, d, s, p, 0) + || test_convolution_dynamic(11, 10, 13, 8, k, d, s, p, 1) + || test_convolution_dynamic(11, 10, 12, 16, k, d, s, p, 0) + || test_convolution_dynamic(11, 10, 15, 15, k, d, s, p, 0) + || test_convolution_dynamic(11, 10, 16, 16, k, d, s, p, 0); + + if (ret != 0) + return -1; + } + + return 0; +} + +#if NCNN_INT8 +static int test_convolution_int8(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, bool requant = false) +{ + ncnn::Mat a = RandomMat(w, h, c); + + ncnn::ParamDict pd; + pd.set(0, outch); + pd.set(1, kernel); + pd.set(2, dilation); + pd.set(3, stride); + pd.set(4, pad); + pd.set(5, bias); + pd.set(6, outch * c * kernel * kernel); + pd.set(8, requant ? 101 : 1); // int8_scale_term + + int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 + ncnn::Mat activation_params(2); + activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha + activation_params[1] = RandomFloat(0, 1); // beta + pd.set(9, activation_type); + pd.set(10, activation_params); + + std::vector weights(bias ? 5 : 4); + weights[0] = RandomMat(outch * c * kernel * kernel); + + ncnn::Mat weight_scales = scales_mat(weights[0], outch, c * kernel * kernel, c * kernel * kernel); + ncnn::Mat input_scales = scales_mat(a, 1, w * h * c, a.cstep); + ncnn::Mat top_scales = requant ? scales_mat(a, 1, w * h * c, a.cstep) : ncnn::Mat(); + if (bias) + { + weights[1] = RandomMat(outch); + weights[2] = weight_scales; + weights[3] = input_scales; + weights[4] = top_scales; + } + else + { + weights[1] = weight_scales; + weights[2] = input_scales; + weights[3] = top_scales; + } + + int flag = TEST_LAYER_DISABLE_GPU_TESTING; + int ret = test_layer("Convolution", pd, weights, a, requant ? 1.0f : 0.001f, 0, flag); + if (ret != 0) + { + fprintf(stderr, "test_convolution_int8 failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d requant=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, requant, activation_type, activation_params[0], activation_params[1]); + } + + return ret; +} + +static int test_convolution_1() +{ + static const int kdsp[16][4] = { + {1, 1, 1, 0}, + {1, 1, 2, 0}, + {2, 1, 1, 1}, + {2, 1, 2, -233}, + {3, 1, 1, 1}, + {3, 1, 2, 1}, + {3, 2, 1, 1}, + {4, 1, 1, 2}, + {4, 1, 2, -233}, + {4, 2, 1, -234}, + {5, 1, 1, -234}, + {5, 1, 2, 2}, + {5, 2, 2, 2}, + {7, 1, 1, 3}, + {7, 1, 2, 3}, + {7, 2, 1, -233}, + }; + + for (int i = 0; i < 16; i++) + { + const int k = kdsp[i][0]; + const int d = kdsp[i][1]; + const int s = kdsp[i][2]; + const int p = kdsp[i][3]; + + int ret = 0 + || test_convolution_int8(9, 7, 1, 1, k, d, s, p, 1) + || test_convolution_int8(9, 7, 2, 2, k, d, s, p, 1) + || test_convolution_int8(9, 7, 3, 3, k, d, s, p, 1) + || test_convolution_int8(9, 7, 4, 4, k, d, s, p, 1) + || test_convolution_int8(9, 7, 7, 7, k, d, s, p, 1) + || test_convolution_int8(9, 7, 8, 8, k, d, s, p, 1) + || test_convolution_int8(9, 7, 15, 15, k, d, s, p, 1) + || test_convolution_int8(9, 7, 16, 15, k, d, s, p, 1) + || test_convolution_int8(9, 7, 15, 16, k, d, s, p, 1) + || test_convolution_int8(9, 7, 16, 16, k, d, s, p, 1); + + if (ret != 0) + return -1; + } + for (int i = 0; i < 16; i++) + { + const int k = kdsp[i][0]; + const int d = kdsp[i][1]; + const int s = kdsp[i][2]; + const int p = kdsp[i][3]; + + int ret = 0 + || test_convolution_int8(9, 7, 1, 1, k, d, s, p, 1, true) + || test_convolution_int8(9, 7, 1, 1, k, d, s, p, 1, true) + || test_convolution_int8(9, 7, 2, 2, k, d, s, p, 1, true) + || test_convolution_int8(9, 7, 3, 3, k, d, s, p, 1, true) + || test_convolution_int8(9, 7, 4, 4, k, d, s, p, 1, true) + || test_convolution_int8(9, 7, 7, 7, k, d, s, p, 1, true) + || test_convolution_int8(9, 7, 8, 8, k, d, s, p, 1, true) + || test_convolution_int8(9, 7, 15, 15, k, d, s, p, 1, true) + || test_convolution_int8(9, 7, 16, 15, k, d, s, p, 1, true) + || test_convolution_int8(9, 7, 15, 16, k, d, s, p, 1, true) + || test_convolution_int8(9, 7, 16, 16, k, d, s, p, 1, true); + + if (ret != 0) + return -1; + } + + return 0 + || test_convolution_int8(11, 11, 8, 16, 3, 1, 1, 1, 1) + || test_convolution_int8(13, 16, 16, 24, 3, 1, 1, 1, 1) + || test_convolution_int8(8, 8, 16, 24, 3, 1, 1, 1, 0) + || test_convolution_int8(4, 8, 16, 24, 3, 1, 1, 1, 1) + || test_convolution_int8(4, 20, 16, 24, 3, 1, 1, 1, 0) + || test_convolution_int8(6, 7, 64, 64, 3, 1, 2, 0, 1) + || test_convolution_int8(25, 33, 16, 15, 3, 1, 1, 1, 0) + || test_convolution_int8(7, 7, 15, 12, 3, 1, 1, 1, 0); +} +#endif // NCNN_INT8 + +int main() +{ + SRAND(7767517); + +#if NCNN_INT8 + return 0 + || test_convolution_1() + || test_convolution_2() + || test_convolution_3(); +#else + return 0 + || test_convolution_2() + || test_convolution_3(); +#endif +} diff --git a/tests/test_convolutiondepthwise.cpp b/tests/test_convolutiondepthwise.cpp index 03317b68c1e..715fc73662c 100644 --- a/tests/test_convolutiondepthwise.cpp +++ b/tests/test_convolutiondepthwise.cpp @@ -125,222 +125,9 @@ static int test_convolutiondepthwise_0() return 0; } -static int test_convolutiondepthwise_dynamic(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, int group) -{ - ncnn::Mat a = RandomMat(w, h, c); - - ncnn::ParamDict pd; - pd.set(0, 0); - pd.set(1, 0); - pd.set(2, dilation); - pd.set(3, stride); - pd.set(4, pad); - pd.set(5, bias); - pd.set(6, 0); - pd.set(7, group); - pd.set(19, 1); // dynamic weight - - int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 - ncnn::Mat activation_params(2); - activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha - activation_params[1] = RandomFloat(0, 1); // beta - pd.set(9, activation_type); - pd.set(10, activation_params); - - std::vector as(bias ? 3 : 2); - as[0] = a; - as[1] = RandomMat(kernel, kernel, c / group, outch); - if (bias) - as[2] = RandomMat(outch); - - std::vector weights(0); - - int ret = test_layer("ConvolutionDepthWise", pd, weights, as); - if (ret != 0) - { - fprintf(stderr, "test_convolutiondepthwise_dynamic failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d group=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, group, activation_type, activation_params[0], activation_params[1]); - } - - return ret; -} - -static int test_convolutiondepthwise_2() -{ - static const int kdsp[7][4] = { - {1, 1, 1, 0}, - {1, 1, 2, 0}, - {2, 1, 1, 1}, - {2, 1, 2, -233}, - {3, 1, 1, 1}, - {3, 1, 2, 1}, - {3, 2, 1, -234}, - }; - - for (int i = 0; i < 7; i++) - { - const int k = kdsp[i][0]; - const int d = kdsp[i][1]; - const int s = kdsp[i][2]; - const int p = kdsp[i][3]; - - int ret = 0 - || test_convolutiondepthwise_dynamic(11, 10, 1, 1, k, d, s, p, 1, 1) - || test_convolutiondepthwise_dynamic(11, 10, 2, 2, k, d, s, p, 0, 1) - || test_convolutiondepthwise_dynamic(11, 10, 2, 2, k, d, s, p, 1, 2) - || test_convolutiondepthwise_dynamic(11, 10, 3, 3, k, d, s, p, 0, 3) - || test_convolutiondepthwise_dynamic(11, 10, 4, 2, k, d, s, p, 1, 2) - || test_convolutiondepthwise_dynamic(11, 10, 4, 4, k, d, s, p, 0, 4) - || test_convolutiondepthwise_dynamic(11, 10, 7, 7, k, d, s, p, 1, 7) - || test_convolutiondepthwise_dynamic(11, 10, 8, 8, k, d, s, p, 0, 2) - || test_convolutiondepthwise_dynamic(11, 10, 8, 8, k, d, s, p, 1, 8) - || test_convolutiondepthwise_dynamic(11, 10, 12, 12, k, d, s, p, 0, 4) - || test_convolutiondepthwise_dynamic(11, 10, 15, 15, k, d, s, p, 1, 15) - || test_convolutiondepthwise_dynamic(11, 10, 16, 8, k, d, s, p, 0, 2) - || test_convolutiondepthwise_dynamic(11, 10, 16, 16, k, d, s, p, 1, 16); - - if (ret != 0) - return -1; - } - - return 0; -} - -#if NCNN_INT8 -static int test_convolutiondepthwise_int8(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, int group, bool requant = false) -{ - ncnn::Mat a = RandomMat(w, h, c); - - ncnn::ParamDict pd; - pd.set(0, outch); - pd.set(1, kernel); - pd.set(2, dilation); - pd.set(3, stride); - pd.set(4, pad); - pd.set(5, bias); - pd.set(6, outch / group * c / group * kernel * kernel * group); - pd.set(7, group); - pd.set(8, requant ? 101 : 1); // int8_scale_term - - int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 - ncnn::Mat activation_params(2); - activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha - activation_params[1] = RandomFloat(0, 1); // beta - pd.set(9, activation_type); - pd.set(10, activation_params); - - std::vector weights(bias ? 5 : 4); - weights[0] = RandomMat(outch / group * c / group * kernel * kernel * group); - ncnn::Mat weight_scales = scales_mat(weights[0], group, c * kernel * kernel / group, c * kernel * kernel / group); - ncnn::Mat input_scales = scales_mat(a, 1, w * h * c, a.cstep); - ncnn::Mat top_scales = requant ? scales_mat(a, 1, w * h * c, a.cstep) : ncnn::Mat(); - if (bias) - { - weights[1] = RandomMat(outch); - weights[2] = weight_scales; - weights[3] = input_scales; - weights[4] = top_scales; - } - else - { - weights[1] = weight_scales; - weights[2] = input_scales; - weights[3] = top_scales; - } - - int flag = TEST_LAYER_DISABLE_GPU_TESTING; - int ret = test_layer("ConvolutionDepthWise", pd, weights, a, requant ? 1.0f : 0.001f, 0, flag); - if (ret != 0) - { - fprintf(stderr, "test_convolutiondepthwise_int8 failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d group=%d requant=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, group, requant, activation_type, activation_params[0], activation_params[1]); - } - - return ret; -} - -static int test_convolutiondepthwise_1() -{ - static const int kdsp[16][4] = { - {1, 1, 1, 0}, - {1, 1, 2, 0}, - {2, 1, 1, 1}, - {2, 1, 2, -233}, - {3, 1, 1, 1}, - {3, 1, 2, 1}, - {3, 2, 1, 1}, - {4, 1, 1, 2}, - {4, 1, 2, -233}, - {4, 2, 1, -234}, - {5, 1, 1, -234}, - {5, 1, 2, 2}, - {5, 2, 2, 2}, - {7, 1, 1, 3}, - {7, 1, 2, 3}, - {7, 2, 1, -233}, - }; - - for (int i = 0; i < 16; i++) - { - const int k = kdsp[i][0]; - const int d = kdsp[i][1]; - const int s = kdsp[i][2]; - const int p = kdsp[i][3]; - - int ret = 0 - || test_convolutiondepthwise_int8(15, 7, 1, 1, k, d, s, p, 1, 1) - || test_convolutiondepthwise_int8(15, 7, 2, 2, k, d, s, p, 0, 1) - || test_convolutiondepthwise_int8(15, 7, 2, 2, k, d, s, p, 1, 2) - || test_convolutiondepthwise_int8(15, 7, 3, 3, k, d, s, p, 0, 3) - || test_convolutiondepthwise_int8(15, 7, 4, 2, k, d, s, p, 1, 2) - || test_convolutiondepthwise_int8(15, 7, 4, 4, k, d, s, p, 0, 4) - || test_convolutiondepthwise_int8(15, 7, 7, 7, k, d, s, p, 1, 7) - || test_convolutiondepthwise_int8(15, 7, 8, 8, k, d, s, p, 0, 2) - || test_convolutiondepthwise_int8(15, 7, 8, 8, k, d, s, p, 1, 8) - || test_convolutiondepthwise_int8(15, 7, 12, 12, k, d, s, p, 0, 4) - || test_convolutiondepthwise_int8(15, 7, 15, 15, k, d, s, p, 1, 15) - || test_convolutiondepthwise_int8(15, 7, 16, 8, k, d, s, p, 0, 2) - || test_convolutiondepthwise_int8(15, 7, 16, 16, k, d, s, p, 1, 16); - - if (ret != 0) - return -1; - } - - for (int i = 0; i < 16; i++) - { - const int k = kdsp[i][0]; - const int d = kdsp[i][1]; - const int s = kdsp[i][2]; - const int p = kdsp[i][3]; - - int ret = 0 - || test_convolutiondepthwise_int8(9, 7, 1, 1, k, d, s, p, 1, 1, true) - || test_convolutiondepthwise_int8(9, 7, 2, 2, k, d, s, p, 0, 1, true) - || test_convolutiondepthwise_int8(9, 7, 2, 2, k, d, s, p, 1, 2, true) - || test_convolutiondepthwise_int8(9, 7, 3, 3, k, d, s, p, 0, 3, true) - || test_convolutiondepthwise_int8(9, 7, 4, 2, k, d, s, p, 1, 2, true) - || test_convolutiondepthwise_int8(9, 7, 4, 4, k, d, s, p, 0, 4, true) - || test_convolutiondepthwise_int8(9, 7, 7, 7, k, d, s, p, 1, 7, true) - || test_convolutiondepthwise_int8(9, 7, 8, 8, k, d, s, p, 0, 2, true) - || test_convolutiondepthwise_int8(9, 7, 8, 8, k, d, s, p, 1, 8, true) - || test_convolutiondepthwise_int8(9, 7, 12, 12, k, d, s, p, 0, 4, true) - || test_convolutiondepthwise_int8(9, 7, 15, 15, k, d, s, p, 1, 15, true) - || test_convolutiondepthwise_int8(9, 7, 16, 8, k, d, s, p, 0, 2, true) - || test_convolutiondepthwise_int8(9, 7, 16, 16, k, d, s, p, 1, 16, true); - - if (ret != 0) - return -1; - } - - return 0; -} -#endif // NCNN_INT8 - int main() { SRAND(7767517); -#if NCNN_INT8 - return test_convolutiondepthwise_0() || test_convolutiondepthwise_1() || test_convolutiondepthwise_2(); -#else - return test_convolutiondepthwise_0() || test_convolutiondepthwise_2(); -#endif + return test_convolutiondepthwise_0(); } diff --git a/tests/test_convolutiondepthwise_1.cpp b/tests/test_convolutiondepthwise_1.cpp new file mode 100644 index 00000000000..3d10a7a8e85 --- /dev/null +++ b/tests/test_convolutiondepthwise_1.cpp @@ -0,0 +1,236 @@ +// Tencent is pleased to support the open source community by making ncnn available. +// +// Copyright (C) 2019 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 "layer/convolutiondepthwise.h" +#include "testutil.h" + +static int test_convolutiondepthwise_dynamic(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, int group) +{ + ncnn::Mat a = RandomMat(w, h, c); + + ncnn::ParamDict pd; + pd.set(0, 0); + pd.set(1, 0); + pd.set(2, dilation); + pd.set(3, stride); + pd.set(4, pad); + pd.set(5, bias); + pd.set(6, 0); + pd.set(7, group); + pd.set(19, 1); // dynamic weight + + int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 + ncnn::Mat activation_params(2); + activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha + activation_params[1] = RandomFloat(0, 1); // beta + pd.set(9, activation_type); + pd.set(10, activation_params); + + std::vector as(bias ? 3 : 2); + as[0] = a; + as[1] = RandomMat(kernel, kernel, c / group, outch); + if (bias) + as[2] = RandomMat(outch); + + std::vector weights(0); + + int ret = test_layer("ConvolutionDepthWise", pd, weights, as); + if (ret != 0) + { + fprintf(stderr, "test_convolutiondepthwise_dynamic failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d group=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, group, activation_type, activation_params[0], activation_params[1]); + } + + return ret; +} + +static int test_convolutiondepthwise_2() +{ + static const int kdsp[7][4] = { + {1, 1, 1, 0}, + {1, 1, 2, 0}, + {2, 1, 1, 1}, + {2, 1, 2, -233}, + {3, 1, 1, 1}, + {3, 1, 2, 1}, + {3, 2, 1, -234}, + }; + + for (int i = 0; i < 7; i++) + { + const int k = kdsp[i][0]; + const int d = kdsp[i][1]; + const int s = kdsp[i][2]; + const int p = kdsp[i][3]; + + int ret = 0 + || test_convolutiondepthwise_dynamic(11, 10, 1, 1, k, d, s, p, 1, 1) + || test_convolutiondepthwise_dynamic(11, 10, 2, 2, k, d, s, p, 0, 1) + || test_convolutiondepthwise_dynamic(11, 10, 2, 2, k, d, s, p, 1, 2) + || test_convolutiondepthwise_dynamic(11, 10, 3, 3, k, d, s, p, 0, 3) + || test_convolutiondepthwise_dynamic(11, 10, 4, 2, k, d, s, p, 1, 2) + || test_convolutiondepthwise_dynamic(11, 10, 4, 4, k, d, s, p, 0, 4) + || test_convolutiondepthwise_dynamic(11, 10, 7, 7, k, d, s, p, 1, 7) + || test_convolutiondepthwise_dynamic(11, 10, 8, 8, k, d, s, p, 0, 2) + || test_convolutiondepthwise_dynamic(11, 10, 8, 8, k, d, s, p, 1, 8) + || test_convolutiondepthwise_dynamic(11, 10, 12, 12, k, d, s, p, 0, 4) + || test_convolutiondepthwise_dynamic(11, 10, 15, 15, k, d, s, p, 1, 15) + || test_convolutiondepthwise_dynamic(11, 10, 16, 8, k, d, s, p, 0, 2) + || test_convolutiondepthwise_dynamic(11, 10, 16, 16, k, d, s, p, 1, 16); + + if (ret != 0) + return -1; + } + + return 0; +} + +#if NCNN_INT8 +static int test_convolutiondepthwise_int8(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, int group, bool requant = false) +{ + ncnn::Mat a = RandomMat(w, h, c); + + ncnn::ParamDict pd; + pd.set(0, outch); + pd.set(1, kernel); + pd.set(2, dilation); + pd.set(3, stride); + pd.set(4, pad); + pd.set(5, bias); + pd.set(6, outch / group * c / group * kernel * kernel * group); + pd.set(7, group); + pd.set(8, requant ? 101 : 1); // int8_scale_term + + int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 + ncnn::Mat activation_params(2); + activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha + activation_params[1] = RandomFloat(0, 1); // beta + pd.set(9, activation_type); + pd.set(10, activation_params); + + std::vector weights(bias ? 5 : 4); + weights[0] = RandomMat(outch / group * c / group * kernel * kernel * group); + ncnn::Mat weight_scales = scales_mat(weights[0], group, c * kernel * kernel / group, c * kernel * kernel / group); + ncnn::Mat input_scales = scales_mat(a, 1, w * h * c, a.cstep); + ncnn::Mat top_scales = requant ? scales_mat(a, 1, w * h * c, a.cstep) : ncnn::Mat(); + if (bias) + { + weights[1] = RandomMat(outch); + weights[2] = weight_scales; + weights[3] = input_scales; + weights[4] = top_scales; + } + else + { + weights[1] = weight_scales; + weights[2] = input_scales; + weights[3] = top_scales; + } + + int flag = TEST_LAYER_DISABLE_GPU_TESTING; + int ret = test_layer("ConvolutionDepthWise", pd, weights, a, requant ? 1.0f : 0.001f, 0, flag); + if (ret != 0) + { + fprintf(stderr, "test_convolutiondepthwise_int8 failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d group=%d requant=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, group, requant, activation_type, activation_params[0], activation_params[1]); + } + + return ret; +} + +static int test_convolutiondepthwise_1() +{ + static const int kdsp[16][4] = { + {1, 1, 1, 0}, + {1, 1, 2, 0}, + {2, 1, 1, 1}, + {2, 1, 2, -233}, + {3, 1, 1, 1}, + {3, 1, 2, 1}, + {3, 2, 1, 1}, + {4, 1, 1, 2}, + {4, 1, 2, -233}, + {4, 2, 1, -234}, + {5, 1, 1, -234}, + {5, 1, 2, 2}, + {5, 2, 2, 2}, + {7, 1, 1, 3}, + {7, 1, 2, 3}, + {7, 2, 1, -233}, + }; + + for (int i = 0; i < 16; i++) + { + const int k = kdsp[i][0]; + const int d = kdsp[i][1]; + const int s = kdsp[i][2]; + const int p = kdsp[i][3]; + + int ret = 0 + || test_convolutiondepthwise_int8(15, 7, 1, 1, k, d, s, p, 1, 1) + || test_convolutiondepthwise_int8(15, 7, 2, 2, k, d, s, p, 0, 1) + || test_convolutiondepthwise_int8(15, 7, 2, 2, k, d, s, p, 1, 2) + || test_convolutiondepthwise_int8(15, 7, 3, 3, k, d, s, p, 0, 3) + || test_convolutiondepthwise_int8(15, 7, 4, 2, k, d, s, p, 1, 2) + || test_convolutiondepthwise_int8(15, 7, 4, 4, k, d, s, p, 0, 4) + || test_convolutiondepthwise_int8(15, 7, 7, 7, k, d, s, p, 1, 7) + || test_convolutiondepthwise_int8(15, 7, 8, 8, k, d, s, p, 0, 2) + || test_convolutiondepthwise_int8(15, 7, 8, 8, k, d, s, p, 1, 8) + || test_convolutiondepthwise_int8(15, 7, 12, 12, k, d, s, p, 0, 4) + || test_convolutiondepthwise_int8(15, 7, 15, 15, k, d, s, p, 1, 15) + || test_convolutiondepthwise_int8(15, 7, 16, 8, k, d, s, p, 0, 2) + || test_convolutiondepthwise_int8(15, 7, 16, 16, k, d, s, p, 1, 16); + + if (ret != 0) + return -1; + } + + for (int i = 0; i < 16; i++) + { + const int k = kdsp[i][0]; + const int d = kdsp[i][1]; + const int s = kdsp[i][2]; + const int p = kdsp[i][3]; + + int ret = 0 + || test_convolutiondepthwise_int8(9, 7, 1, 1, k, d, s, p, 1, 1, true) + || test_convolutiondepthwise_int8(9, 7, 2, 2, k, d, s, p, 0, 1, true) + || test_convolutiondepthwise_int8(9, 7, 2, 2, k, d, s, p, 1, 2, true) + || test_convolutiondepthwise_int8(9, 7, 3, 3, k, d, s, p, 0, 3, true) + || test_convolutiondepthwise_int8(9, 7, 4, 2, k, d, s, p, 1, 2, true) + || test_convolutiondepthwise_int8(9, 7, 4, 4, k, d, s, p, 0, 4, true) + || test_convolutiondepthwise_int8(9, 7, 7, 7, k, d, s, p, 1, 7, true) + || test_convolutiondepthwise_int8(9, 7, 8, 8, k, d, s, p, 0, 2, true) + || test_convolutiondepthwise_int8(9, 7, 8, 8, k, d, s, p, 1, 8, true) + || test_convolutiondepthwise_int8(9, 7, 12, 12, k, d, s, p, 0, 4, true) + || test_convolutiondepthwise_int8(9, 7, 15, 15, k, d, s, p, 1, 15, true) + || test_convolutiondepthwise_int8(9, 7, 16, 8, k, d, s, p, 0, 2, true) + || test_convolutiondepthwise_int8(9, 7, 16, 16, k, d, s, p, 1, 16, true); + + if (ret != 0) + return -1; + } + + return 0; +} +#endif // NCNN_INT8 + +int main() +{ + SRAND(7767517); + +#if NCNN_INT8 + return test_convolutiondepthwise_1() || test_convolutiondepthwise_2(); +#else + return test_convolutiondepthwise_2(); +#endif +} diff --git a/tests/test_crop.cpp b/tests/test_crop.cpp index caa2876c499..b2a29778fec 100644 --- a/tests/test_crop.cpp +++ b/tests/test_crop.cpp @@ -42,112 +42,6 @@ static int test_crop(const ncnn::Mat& a, int woffset, int hoffset, int doffset, 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 ncnn::Mat IntArrayMat(int a0, int a1, int a2, int a3) -{ - ncnn::Mat m(4); - int* p = m; - p[0] = a0; - p[1] = a1; - p[2] = a2; - p[3] = a3; - return m; -} - -static void print_int_array(const ncnn::Mat& a) -{ - const int* pa = a; - - fprintf(stderr, "["); - for (int i = 0; i < a.w; i++) - { - fprintf(stderr, " %d", pa[i]); - } - fprintf(stderr, " ]"); -} - -static int test_crop(const ncnn::Mat& a, const ncnn::Mat& starts, const ncnn::Mat& ends, const ncnn::Mat& axes) -{ - ncnn::ParamDict pd; - pd.set(9, starts); // starts - pd.set(10, ends); // ends - pd.set(11, axes); // axes - - std::vector weights(0); - - int ret = test_layer("Crop", pd, weights, a); - if (ret != 0) - { - fprintf(stderr, "test_crop failed a.dims=%d a=(%d %d %d %d)", a.dims, a.w, a.h, a.d, 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, "\n"); - } - - return ret; -} - -static int test_crop(const ncnn::Mat& a, int woffset, int hoffset, int doffset, int coffset, const ncnn::Mat& ref) -{ - ncnn::ParamDict pd; - pd.set(0, woffset); - pd.set(1, hoffset); - pd.set(13, doffset); - pd.set(2, coffset); - pd.set(3, 0); // outw - pd.set(4, 0); // outh - pd.set(14, 0); // outd - pd.set(5, 0); // outc - pd.set(6, 0); // woffset2 - pd.set(7, 0); // hoffset2 - pd.set(15, 0); // doffset2 - pd.set(8, 0); // coffset2 - - std::vector weights(0); - - std::vector ab(2); - ab[0] = a; - ab[1] = ref; - - int ret = test_layer("Crop", pd, weights, ab); - if (ret != 0) - { - fprintf(stderr, "test_crop failed a.dims=%d a=(%d %d %d %d) woffset=%d hoffset=%d doffset=%d coffset=%d ref.dims=%d ref=(%d %d %d %d)\n", a.dims, a.w, a.h, a.d, a.c, woffset, hoffset, doffset, coffset, ref.dims, ref.w, ref.h, ref.d, ref.c); - } - - return ret; -} - static int test_crop_0(const ncnn::Mat& a) { return 0 @@ -161,30 +55,6 @@ static int test_crop_0(const ncnn::Mat& a) || test_crop(a, 16, 0, 0, 0, -233, 0, 0, 0, 7, 0, 0, 0); } -static int test_crop_1(const ncnn::Mat& a) -{ - return 0 - || test_crop(a, IntArrayMat(12), IntArrayMat(-233), IntArrayMat(0)) - || test_crop(a, IntArrayMat(16), IntArrayMat(-233), IntArrayMat(0)) - || test_crop(a, IntArrayMat(11), IntArrayMat(11 + 16), IntArrayMat(0)) - || test_crop(a, IntArrayMat(12), IntArrayMat(12 + 7), IntArrayMat(-1)) - || test_crop(a, IntArrayMat(16), IntArrayMat(16 + 12), ncnn::Mat()) - || test_crop(a, IntArrayMat(11), IntArrayMat(-7 + 1), IntArrayMat(0)) - || test_crop(a, IntArrayMat(12), IntArrayMat(-12 + 1), IntArrayMat(-1)) - || test_crop(a, IntArrayMat(16), IntArrayMat(-16 + 1), ncnn::Mat()); -} - -static int test_crop_2(const ncnn::Mat& a) -{ - return 0 - || test_crop(a, 0, 0, 0, 0, a) - || test_crop(a, 0, 0, 0, 0, ncnn::Mat(27)) - - || test_crop(a, 11, 0, 0, 0, ncnn::Mat(7)) - || test_crop(a, 12, 0, 0, 0, ncnn::Mat(12)) - || test_crop(a, 16, 0, 0, 0, ncnn::Mat(16)); -} - static int test_crop_3(const ncnn::Mat& a) { return 0 @@ -220,52 +90,6 @@ static int test_crop_3(const ncnn::Mat& a) || test_crop(a, 4, 8, 0, 0, -233, -233, 0, 0, 6, 12, 0, 0); } -static int test_crop_4(const ncnn::Mat& a) -{ - return 0 - || test_crop(a, IntArrayMat(12), IntArrayMat(-233), IntArrayMat(0)) - || test_crop(a, IntArrayMat(8), IntArrayMat(-233), IntArrayMat(0)) - || test_crop(a, IntArrayMat(4), IntArrayMat(-233), IntArrayMat(1)) - || test_crop(a, IntArrayMat(5, 11), IntArrayMat(-233, -233), IntArrayMat(0, 1)) - - || test_crop(a, IntArrayMat(11), IntArrayMat(11 + 16), IntArrayMat(0)) - || test_crop(a, IntArrayMat(12), IntArrayMat(12 + 7), IntArrayMat(0)) - || test_crop(a, IntArrayMat(8), IntArrayMat(8 + 12), IntArrayMat(-2)) - - || test_crop(a, IntArrayMat(5), IntArrayMat(8), IntArrayMat(1)) - || test_crop(a, IntArrayMat(6), IntArrayMat(9), IntArrayMat(1)) - || test_crop(a, IntArrayMat(4), IntArrayMat(12), IntArrayMat(-1)) - - || test_crop(a, IntArrayMat(11, 5), IntArrayMat(11 + 7, 11), IntArrayMat(0, 1)) - || test_crop(a, IntArrayMat(12, 6), IntArrayMat(12 + 12, 12), IntArrayMat(0, 1)) - || test_crop(a, IntArrayMat(8, 4), IntArrayMat(8 + 16, 10), IntArrayMat(0, -1)) - - || test_crop(a, IntArrayMat(11), IntArrayMat(-16 + 1), IntArrayMat(0)) - || test_crop(a, IntArrayMat(12), IntArrayMat(-7 + 1), IntArrayMat(0)) - || test_crop(a, IntArrayMat(8), IntArrayMat(-12 + 1), IntArrayMat(-2)) - - || test_crop(a, IntArrayMat(5), IntArrayMat(-5 + 1), IntArrayMat(1)) - || test_crop(a, IntArrayMat(6), IntArrayMat(-6 + 1), IntArrayMat(1)) - || test_crop(a, IntArrayMat(4), IntArrayMat(-4 + 1), IntArrayMat(-1)) - - || test_crop(a, IntArrayMat(11, 5), IntArrayMat(-12 + 1, -6 + 1), IntArrayMat(0, 1)) - || test_crop(a, IntArrayMat(12, 6), IntArrayMat(-16 + 1, -5 + 1), IntArrayMat(0, 1)) - || test_crop(a, IntArrayMat(8, 4), IntArrayMat(-7 + 1, -4 + 1), IntArrayMat(-2, -1)); -} - -static int test_crop_5(const ncnn::Mat& a) -{ - return 0 - || test_crop(a, 0, 0, 0, 0, a) - - || test_crop(a, 0, 12, 0, 0, ncnn::Mat(8, 7)) - || test_crop(a, 5, 0, 0, 0, ncnn::Mat(7, 27)) - - || test_crop(a, 5, 11, 0, 0, ncnn::Mat(5, 12)) - || test_crop(a, 6, 12, 0, 0, ncnn::Mat(4, 16)) - || test_crop(a, 4, 8, 0, 0, ncnn::Mat(6, 7)); -} - static int test_crop_6(const ncnn::Mat& a) { return 0 @@ -338,94 +162,6 @@ static int test_crop_6(const ncnn::Mat& a) || test_crop(a, 4, 4, 0, 8, -233, -233, 0, -233, 6, 2, 0, 12); } -static int test_crop_7(const ncnn::Mat& a) -{ - return 0 - || test_crop(a, IntArrayMat(11), IntArrayMat(-233), IntArrayMat(0)) - || test_crop(a, IntArrayMat(8), IntArrayMat(-233), IntArrayMat(0)) - || test_crop(a, IntArrayMat(5), IntArrayMat(-233), IntArrayMat(1)) - || test_crop(a, IntArrayMat(6), IntArrayMat(-233), IntArrayMat(2)) - || test_crop(a, IntArrayMat(4), IntArrayMat(-233), IntArrayMat(-1)) - || test_crop(a, IntArrayMat(12, 6), IntArrayMat(-233, -233), IntArrayMat(0, 1)) - || test_crop(a, IntArrayMat(11, 5), IntArrayMat(-233, -233), IntArrayMat(0, -1)) - || test_crop(a, IntArrayMat(8, 4), IntArrayMat(-233, -233), IntArrayMat(0, 2)) - || test_crop(a, IntArrayMat(6, 6), IntArrayMat(-233, -233), IntArrayMat(1, -1)) - || test_crop(a, IntArrayMat(11, 5, 5), IntArrayMat(-233, -233, -233), IntArrayMat(0, 1, 2)) - || test_crop(a, IntArrayMat(8, 4, 4), IntArrayMat(-233, -233, -233), IntArrayMat(0, 1, -1)) - - || test_crop(a, IntArrayMat(11), IntArrayMat(11 + 7), IntArrayMat(0)) - || test_crop(a, IntArrayMat(12), IntArrayMat(12 + 12), IntArrayMat(0)) - || test_crop(a, IntArrayMat(8), IntArrayMat(8 + 16), IntArrayMat(0)) - - || test_crop(a, IntArrayMat(5), IntArrayMat(13), IntArrayMat(1)) - || test_crop(a, IntArrayMat(6), IntArrayMat(12), IntArrayMat(1)) - || test_crop(a, IntArrayMat(4), IntArrayMat(11), IntArrayMat(-2)) - - || test_crop(a, IntArrayMat(5), IntArrayMat(12), IntArrayMat(2)) - || test_crop(a, IntArrayMat(6), IntArrayMat(11), IntArrayMat(2)) - || test_crop(a, IntArrayMat(4), IntArrayMat(13), IntArrayMat(-1)) - - || test_crop(a, IntArrayMat(11, 5), IntArrayMat(11 + 7, 11), IntArrayMat(0, 1)) - || test_crop(a, IntArrayMat(12, 6), IntArrayMat(12 + 16, 12), IntArrayMat(0, 1)) - || test_crop(a, IntArrayMat(8, 4), IntArrayMat(8 + 12, 13), IntArrayMat(0, -2)) - - || test_crop(a, IntArrayMat(11, 5), IntArrayMat(11 + 16, 13), IntArrayMat(0, 2)) - || test_crop(a, IntArrayMat(12, 6), IntArrayMat(12 + 12, 11), IntArrayMat(0, 2)) - || test_crop(a, IntArrayMat(8, 4), IntArrayMat(8 + 7, 12), IntArrayMat(0, -1)) - - || test_crop(a, IntArrayMat(5, 4), IntArrayMat(12, 12), IntArrayMat(1, 2)) - || test_crop(a, IntArrayMat(6, 3), IntArrayMat(13, 13), IntArrayMat(1, 2)) - || test_crop(a, IntArrayMat(4, 2), IntArrayMat(11, 11), IntArrayMat(-2, -1)) - - || test_crop(a, IntArrayMat(11, 5, 2), IntArrayMat(11 + 7, 11, 11), IntArrayMat(0, 1, 2)) - || test_crop(a, IntArrayMat(12, 6, 4), IntArrayMat(12 + 16, 12, 12), IntArrayMat(0, 1, 2)) - || test_crop(a, IntArrayMat(8, 4, 3), IntArrayMat(8 + 12, 13, 13), IntArrayMat(-3, -2, -1)) - - || test_crop(a, IntArrayMat(11), IntArrayMat(-7 + 1), IntArrayMat(0)) - || test_crop(a, IntArrayMat(12), IntArrayMat(-12 + 1), IntArrayMat(0)) - || test_crop(a, IntArrayMat(8), IntArrayMat(-16 + 1), IntArrayMat(-3)) - - || test_crop(a, IntArrayMat(5), IntArrayMat(-6 + 1), IntArrayMat(1)) - || test_crop(a, IntArrayMat(6), IntArrayMat(-5 + 1), IntArrayMat(1)) - || test_crop(a, IntArrayMat(4), IntArrayMat(-4 + 1), IntArrayMat(-2)) - - || test_crop(a, IntArrayMat(5), IntArrayMat(-5 + 1), IntArrayMat(2)) - || test_crop(a, IntArrayMat(6), IntArrayMat(-4 + 1), IntArrayMat(2)) - || test_crop(a, IntArrayMat(4), IntArrayMat(-6 + 1), IntArrayMat(-1)) - - || test_crop(a, IntArrayMat(11, 5), IntArrayMat(-7 + 1, -4 + 1), IntArrayMat(0, 1)) - || test_crop(a, IntArrayMat(12, 6), IntArrayMat(-12 + 1, -6 + 1), IntArrayMat(0, 1)) - || test_crop(a, IntArrayMat(8, 4), IntArrayMat(-16 + 1, -5 + 1), IntArrayMat(-3, -2)) - - || test_crop(a, IntArrayMat(11, 5), IntArrayMat(-12 + 1, -6 + 1), IntArrayMat(0, 2)) - || test_crop(a, IntArrayMat(12, 6), IntArrayMat(-16 + 1, -5 + 1), IntArrayMat(0, 2)) - || test_crop(a, IntArrayMat(8, 4), IntArrayMat(-7 + 1, -4 + 1), IntArrayMat(-3, -1)) - - || test_crop(a, IntArrayMat(5, 2), IntArrayMat(-5 + 1, -5 + 1), IntArrayMat(1, 2)) - || test_crop(a, IntArrayMat(6, 4), IntArrayMat(-4 + 1, -4 + 1), IntArrayMat(1, 2)) - || test_crop(a, IntArrayMat(4, 3), IntArrayMat(-6 + 1, -6 + 1), IntArrayMat(-2, -1)) - - || test_crop(a, IntArrayMat(11, 5, 4), IntArrayMat(-7 + 1, -5 + 1, -5 + 1), IntArrayMat(0, 1, 2)) - || test_crop(a, IntArrayMat(12, 6, 3), IntArrayMat(-12 + 1, -6 + 1, -6 + 1), IntArrayMat(0, 1, 2)) - || test_crop(a, IntArrayMat(8, 4, 2), IntArrayMat(-16 + 1, -4 + 1, -4 + 1), IntArrayMat(-3, -2, -1)); -} - -static int test_crop_8(const ncnn::Mat& a) -{ - return 0 - || test_crop(a, 0, 0, 0, 0, a) - - || test_crop(a, 0, 5, 0, 0, ncnn::Mat(6, 6)) - || test_crop(a, 6, 0, 0, 0, ncnn::Mat(8, 8)) - || test_crop(a, 5, 2, 0, 0, ncnn::Mat(6, 3)) - || test_crop(a, 6, 3, 0, 0, ncnn::Mat(8, 4)) - || test_crop(a, 4, 4, 0, 0, ncnn::Mat(7, 5)) - - || test_crop(a, 5, 3, 0, 11, ncnn::Mat(7, 3, 7)) - || test_crop(a, 6, 4, 0, 12, ncnn::Mat(6, 4, 12)) - || test_crop(a, 4, 2, 0, 8, ncnn::Mat(5, 5, 16)); -} - static int test_crop_9(const ncnn::Mat& a) { return 0 @@ -524,171 +260,6 @@ static int test_crop_9(const ncnn::Mat& a) || test_crop(a, 3, 3, 3, 8, -233, -233, -233, -233, 3, 3, 3, 12); } -static int test_crop_10(const ncnn::Mat& a) -{ - return 0 - || test_crop(a, IntArrayMat(11), IntArrayMat(-233), IntArrayMat(0)) - || test_crop(a, IntArrayMat(8), IntArrayMat(-233), IntArrayMat(0)) - || test_crop(a, IntArrayMat(6), IntArrayMat(-233), IntArrayMat(1)) - || test_crop(a, IntArrayMat(5), IntArrayMat(-233), IntArrayMat(2)) - || test_crop(a, IntArrayMat(4), IntArrayMat(-233), IntArrayMat(-2)) - || test_crop(a, IntArrayMat(6), IntArrayMat(-233), IntArrayMat(3)) - || test_crop(a, IntArrayMat(5), IntArrayMat(-233), IntArrayMat(-1)) - || test_crop(a, IntArrayMat(8, 4), IntArrayMat(-233, -233), IntArrayMat(0, 1)) - || test_crop(a, IntArrayMat(12, 6), IntArrayMat(-233, -233), IntArrayMat(0, 2)) - || test_crop(a, IntArrayMat(11, 5), IntArrayMat(-233, -233), IntArrayMat(-4, -2)) - || test_crop(a, IntArrayMat(4, 4), IntArrayMat(-233, -233), IntArrayMat(1, 2)) - || test_crop(a, IntArrayMat(12, 6), IntArrayMat(-233, -233), IntArrayMat(0, 3)) - || test_crop(a, IntArrayMat(5, 5), IntArrayMat(-233, -233), IntArrayMat(1, 3)) - || test_crop(a, IntArrayMat(4, 4), IntArrayMat(-233, -233), IntArrayMat(2, 3)) - || test_crop(a, IntArrayMat(12, 6, 6), IntArrayMat(-233, -233, -233), IntArrayMat(0, 1, 2)) - || test_crop(a, IntArrayMat(11, 5, 5), IntArrayMat(-233, -233, -233), IntArrayMat(0, 1, 2)) - || test_crop(a, IntArrayMat(8, 4, 4), IntArrayMat(-233, -233, -233), IntArrayMat(0, 1, 3)) - || test_crop(a, IntArrayMat(12, 6, 6), IntArrayMat(-233, -233, -233), IntArrayMat(0, 2, 3)) - || test_crop(a, IntArrayMat(11, 5, 5), IntArrayMat(-233, -233, -233), IntArrayMat(0, 2, 3)) - || test_crop(a, IntArrayMat(4, 4, 4), IntArrayMat(-233, -233, -233), IntArrayMat(1, 2, 3)) - || test_crop(a, IntArrayMat(6, 6, 6), IntArrayMat(-233, -233, -233), IntArrayMat(1, 2, 3)) - || test_crop(a, IntArrayMat(11, 5, 5, 5), IntArrayMat(-233, -233, -233, -233), IntArrayMat(0, 1, 2, 3)) - || test_crop(a, IntArrayMat(8, 4, 4, 4), IntArrayMat(-233, -233, -233, -233), IntArrayMat(0, 1, 2, 3)) - || test_crop(a, IntArrayMat(12, 6, 6, 6), IntArrayMat(-233, -233, -233, -233), IntArrayMat(-4, -3, -2, -1)) - - || test_crop(a, IntArrayMat(11), IntArrayMat(11 + 16), IntArrayMat(0)) - || test_crop(a, IntArrayMat(12), IntArrayMat(12 + 7), IntArrayMat(0)) - || test_crop(a, IntArrayMat(8), IntArrayMat(8 + 12), IntArrayMat(-4)) - - || test_crop(a, IntArrayMat(5), IntArrayMat(11), IntArrayMat(1)) - || test_crop(a, IntArrayMat(6), IntArrayMat(13), IntArrayMat(1)) - || test_crop(a, IntArrayMat(4), IntArrayMat(12), IntArrayMat(-3)) - - || test_crop(a, IntArrayMat(3), IntArrayMat(12), IntArrayMat(2)) - || test_crop(a, IntArrayMat(4), IntArrayMat(13), IntArrayMat(2)) - || test_crop(a, IntArrayMat(5), IntArrayMat(11), IntArrayMat(-2)) - - || test_crop(a, IntArrayMat(1), IntArrayMat(8), IntArrayMat(3)) - || test_crop(a, IntArrayMat(2), IntArrayMat(7), IntArrayMat(3)) - || test_crop(a, IntArrayMat(3), IntArrayMat(6), IntArrayMat(-1)) - - || test_crop(a, IntArrayMat(11, 5), IntArrayMat(11 + 7, 11), IntArrayMat(0, 1)) - || test_crop(a, IntArrayMat(12, 6), IntArrayMat(12 + 12, 12), IntArrayMat(0, 1)) - || test_crop(a, IntArrayMat(8, 4), IntArrayMat(8 + 16, 13), IntArrayMat(-4, -3)) - - || test_crop(a, IntArrayMat(11, 4), IntArrayMat(11 + 12, 13), IntArrayMat(0, 2)) - || test_crop(a, IntArrayMat(12, 3), IntArrayMat(12 + 16, 11), IntArrayMat(0, 2)) - || test_crop(a, IntArrayMat(8, 2), IntArrayMat(8 + 7, 12), IntArrayMat(-4, -2)) - - || test_crop(a, IntArrayMat(11, 1), IntArrayMat(11 + 16, 5), IntArrayMat(0, 3)) - || test_crop(a, IntArrayMat(12, 2), IntArrayMat(12 + 7, 6), IntArrayMat(0, 3)) - || test_crop(a, IntArrayMat(8, 3), IntArrayMat(8 + 12, 7), IntArrayMat(-4, -1)) - - || test_crop(a, IntArrayMat(3, 3), IntArrayMat(13, 4), IntArrayMat(1, 2)) - || test_crop(a, IntArrayMat(4, 2), IntArrayMat(12, 3), IntArrayMat(1, 2)) - || test_crop(a, IntArrayMat(5, 1), IntArrayMat(11, 2), IntArrayMat(-3, -2)) - - || test_crop(a, IntArrayMat(5, 5), IntArrayMat(11, 8), IntArrayMat(1, 3)) - || test_crop(a, IntArrayMat(4, 6), IntArrayMat(12, 9), IntArrayMat(1, 3)) - || test_crop(a, IntArrayMat(3, 4), IntArrayMat(13, 7), IntArrayMat(-3, -1)) - - || test_crop(a, IntArrayMat(2, 3), IntArrayMat(12, 9), IntArrayMat(2, 3)) - || test_crop(a, IntArrayMat(3, 2), IntArrayMat(11, 7), IntArrayMat(2, 3)) - || test_crop(a, IntArrayMat(4, 1), IntArrayMat(10, 8), IntArrayMat(-2, -1)) - - || test_crop(a, IntArrayMat(11, 2, 2), IntArrayMat(11 + 6, 9, 9), IntArrayMat(0, 1, 2)) - || test_crop(a, IntArrayMat(12, 3, 3), IntArrayMat(12 + 1, 10, 10), IntArrayMat(0, 1, 2)) - || test_crop(a, IntArrayMat(8, 4, 4), IntArrayMat(8 + 3, 11, 11), IntArrayMat(-4, -3, -2)) - - || test_crop(a, IntArrayMat(11, 4, 4), IntArrayMat(11 + 12, 12, 12), IntArrayMat(0, 1, 3)) - || test_crop(a, IntArrayMat(12, 5, 5), IntArrayMat(12 + 8, 11, 11), IntArrayMat(0, 1, 3)) - || test_crop(a, IntArrayMat(8, 6, 6), IntArrayMat(8 + 4, 13, 13), IntArrayMat(-4, -3, -1)) - - || test_crop(a, IntArrayMat(11, 1, 4), IntArrayMat(11 + 5, 12, 12), IntArrayMat(0, 2, 3)) - || test_crop(a, IntArrayMat(12, 3, 3), IntArrayMat(12 + 3, 11, 11), IntArrayMat(0, 2, 3)) - || test_crop(a, IntArrayMat(8, 2, 5), IntArrayMat(8 + 2, 10, 10), IntArrayMat(-4, -2, -1)) - - || test_crop(a, IntArrayMat(1, 1, 1), IntArrayMat(7, 7, 7), IntArrayMat(1, 2, 3)) - || test_crop(a, IntArrayMat(2, 2, 2), IntArrayMat(8, 9, 10), IntArrayMat(1, 2, 3)) - || test_crop(a, IntArrayMat(3, 3, 3), IntArrayMat(11, 12, 13), IntArrayMat(-3, -2, -1)) - - || test_crop(a, IntArrayMat(11, 2, 3, 6), IntArrayMat(11 + 11, 10, 12, 11), IntArrayMat(0, 1, 2, 3)) - || test_crop(a, IntArrayMat(12, 3, 4, 5), IntArrayMat(12 + 12, 9, 11, 13), IntArrayMat(0, 1, 2, 3)) - || test_crop(a, IntArrayMat(8, 4, 5, 4), IntArrayMat(8 + 8, 8, 10, 12), IntArrayMat(-4, -3, -2, -1)) - - || test_crop(a, IntArrayMat(11), IntArrayMat(-7 + 1), IntArrayMat(0)) - || test_crop(a, IntArrayMat(12), IntArrayMat(-12 + 1), IntArrayMat(0)) - || test_crop(a, IntArrayMat(8), IntArrayMat(-16 + 1), IntArrayMat(-4)) - - || test_crop(a, IntArrayMat(5), IntArrayMat(-6 + 1), IntArrayMat(1)) - || test_crop(a, IntArrayMat(6), IntArrayMat(-5 + 1), IntArrayMat(1)) - || test_crop(a, IntArrayMat(4), IntArrayMat(-4 + 1), IntArrayMat(-3)) - - || test_crop(a, IntArrayMat(4), IntArrayMat(-4 + 1), IntArrayMat(2)) - || test_crop(a, IntArrayMat(5), IntArrayMat(-5 + 1), IntArrayMat(2)) - || test_crop(a, IntArrayMat(6), IntArrayMat(-6 + 1), IntArrayMat(-2)) - - || test_crop(a, IntArrayMat(1), IntArrayMat(-5 + 1), IntArrayMat(3)) - || test_crop(a, IntArrayMat(2), IntArrayMat(-4 + 1), IntArrayMat(3)) - || test_crop(a, IntArrayMat(3), IntArrayMat(-3 + 1), IntArrayMat(-1)) - - || test_crop(a, IntArrayMat(11, 3), IntArrayMat(-7 + 1, -3 + 1), IntArrayMat(0, 1)) - || test_crop(a, IntArrayMat(12, 4), IntArrayMat(-12 + 1, -4 + 1), IntArrayMat(0, 1)) - || test_crop(a, IntArrayMat(8, 5), IntArrayMat(-16 + 1, -5 + 1), IntArrayMat(-4, -3)) - - || test_crop(a, IntArrayMat(11, 1), IntArrayMat(-12 + 1, -5 + 1), IntArrayMat(0, 2)) - || test_crop(a, IntArrayMat(12, 2), IntArrayMat(-16 + 1, -4 + 1), IntArrayMat(0, 2)) - || test_crop(a, IntArrayMat(8, 3), IntArrayMat(-7 + 1, -6 + 1), IntArrayMat(-4, -2)) - - || test_crop(a, IntArrayMat(11, 3), IntArrayMat(-12 + 1, -2 + 1), IntArrayMat(0, 3)) - || test_crop(a, IntArrayMat(12, 4), IntArrayMat(-16 + 1, -3 + 1), IntArrayMat(0, 3)) - || test_crop(a, IntArrayMat(8, 5), IntArrayMat(-7 + 1, -4 + 1), IntArrayMat(-4, -1)) - - || test_crop(a, IntArrayMat(2, 3), IntArrayMat(-4 + 1, -2 + 1), IntArrayMat(1, 2)) - || test_crop(a, IntArrayMat(3, 4), IntArrayMat(-2 + 1, -3 + 1), IntArrayMat(1, 2)) - || test_crop(a, IntArrayMat(4, 5), IntArrayMat(-3 + 1, -4 + 1), IntArrayMat(-3, -2)) - - || test_crop(a, IntArrayMat(3, 2), IntArrayMat(-2 + 1, -4 + 1), IntArrayMat(1, 3)) - || test_crop(a, IntArrayMat(4, 3), IntArrayMat(-3 + 1, -2 + 1), IntArrayMat(1, 3)) - || test_crop(a, IntArrayMat(5, 4), IntArrayMat(-4 + 1, -3 + 1), IntArrayMat(-3, -1)) - - || test_crop(a, IntArrayMat(2, 3), IntArrayMat(-4 + 1, -6 + 1), IntArrayMat(2, 3)) - || test_crop(a, IntArrayMat(1, 2), IntArrayMat(-5 + 1, -5 + 1), IntArrayMat(2, 3)) - || test_crop(a, IntArrayMat(3, 1), IntArrayMat(-6 + 1, -4 + 1), IntArrayMat(-2, -1)) - - || test_crop(a, IntArrayMat(11, 3, 3), IntArrayMat(-7 + 1, -3 + 1, -4 + 1), IntArrayMat(0, 1, 2)) - || test_crop(a, IntArrayMat(12, 4, 4), IntArrayMat(-12 + 1, -4 + 1, -3 + 1), IntArrayMat(0, 1, 2)) - || test_crop(a, IntArrayMat(8, 5, 5), IntArrayMat(-16 + 1, -5 + 1, -5 + 1), IntArrayMat(-4, -3, -2)) - - || test_crop(a, IntArrayMat(11, 2, 2), IntArrayMat(-7 + 1, -5 + 1, -4 + 1), IntArrayMat(0, 1, 3)) - || test_crop(a, IntArrayMat(12, 1, 1), IntArrayMat(-12 + 1, -6 + 1, -5 + 1), IntArrayMat(0, 1, 3)) - || test_crop(a, IntArrayMat(8, 3, 3), IntArrayMat(-16 + 1, -4 + 1, -6 + 1), IntArrayMat(-4, -3, -1)) - - || test_crop(a, IntArrayMat(11, 2, 5), IntArrayMat(-7 + 1, -2 + 1, -5 + 1), IntArrayMat(0, 2, 3)) - || test_crop(a, IntArrayMat(12, 3, 3), IntArrayMat(-12 + 1, -3 + 1, -4 + 1), IntArrayMat(0, 2, 3)) - || test_crop(a, IntArrayMat(8, 4, 4), IntArrayMat(-16 + 1, -4 + 1, -3 + 1), IntArrayMat(-4, -2, -1)) - - || test_crop(a, IntArrayMat(1, 3, 3), IntArrayMat(-3 + 1, -6 + 1, -4 + 1), IntArrayMat(1, 2, 3)) - || test_crop(a, IntArrayMat(2, 2, 2), IntArrayMat(-4 + 1, -4 + 1, -5 + 1), IntArrayMat(1, 2, 3)) - || test_crop(a, IntArrayMat(3, 1, 1), IntArrayMat(-5 + 1, -5 + 1, -6 + 1), IntArrayMat(-3, -2, -1)) - - || test_crop(a, IntArrayMat(11, 3, 4, 4), IntArrayMat(-7 + 1, -3 + 1, -2 + 1, -4 + 1), IntArrayMat(0, 1, 2, 3)) - || test_crop(a, IntArrayMat(12, 4, 5, 3), IntArrayMat(-12 + 1, -4 + 1, -3 + 1, -5 + 1), IntArrayMat(0, 1, 2, 3)) - || test_crop(a, IntArrayMat(8, 5, 6, 2), IntArrayMat(-16 + 1, -5 + 1, -4 + 1, -3 + 1), IntArrayMat(-4, -3, -2, -1)); -} - -static int test_crop_11(const ncnn::Mat& a) -{ - return 0 - || test_crop(a, 0, 0, 0, 0, a) - - || test_crop(a, 0, 5, 0, 0, ncnn::Mat(6, 6, 6)) - || test_crop(a, 6, 0, 0, 0, ncnn::Mat(8, 8, 8)) - || test_crop(a, 5, 5, 5, 0, ncnn::Mat(6, 6, 6)) - || test_crop(a, 6, 6, 6, 0, ncnn::Mat(8, 8, 8)) - || test_crop(a, 4, 4, 4, 0, ncnn::Mat(5, 5, 5)) - - || test_crop(a, 3, 3, 3, 11, ncnn::Mat(3, 3, 3, 7)) - || test_crop(a, 4, 4, 4, 12, ncnn::Mat(6, 6, 6, 12)) - || test_crop(a, 5, 5, 5, 8, ncnn::Mat(8, 8, 8, 16)); -} - int main() { SRAND(776757); @@ -697,37 +268,13 @@ int main() || test_crop_0(RandomMat(112)) || test_crop_0(RandomMat(126)) || test_crop_0(RandomMat(127)) - || test_crop_1(RandomMat(112)) - || test_crop_1(RandomMat(126)) - || test_crop_1(RandomMat(127)) - || test_crop_2(RandomMat(112)) - || test_crop_2(RandomMat(126)) - || test_crop_2(RandomMat(127)) || test_crop_3(RandomMat(20, 48)) || test_crop_3(RandomMat(15, 36)) || test_crop_3(RandomMat(16, 33)) - || test_crop_4(RandomMat(20, 48)) - || test_crop_4(RandomMat(15, 36)) - || test_crop_4(RandomMat(16, 33)) - || test_crop_5(RandomMat(20, 48)) - || test_crop_5(RandomMat(15, 36)) - || test_crop_5(RandomMat(16, 33)) || test_crop_6(RandomMat(20, 20, 48)) || test_crop_6(RandomMat(15, 15, 36)) || test_crop_6(RandomMat(16, 16, 33)) - || test_crop_7(RandomMat(20, 20, 48)) - || test_crop_7(RandomMat(15, 15, 36)) - || test_crop_7(RandomMat(16, 16, 33)) - || test_crop_8(RandomMat(20, 20, 48)) - || test_crop_8(RandomMat(15, 15, 36)) - || test_crop_8(RandomMat(16, 16, 33)) || test_crop_9(RandomMat(20, 20, 20, 48)) || test_crop_9(RandomMat(15, 15, 15, 36)) - || test_crop_9(RandomMat(16, 16, 16, 33)) - || test_crop_10(RandomMat(20, 20, 20, 48)) - || test_crop_10(RandomMat(15, 15, 15, 36)) - || test_crop_10(RandomMat(16, 16, 16, 33)) - || test_crop_11(RandomMat(20, 20, 20, 48)) - || test_crop_11(RandomMat(15, 15, 15, 36)) - || test_crop_11(RandomMat(16, 16, 16, 33)); + || test_crop_9(RandomMat(16, 16, 16, 33)); } diff --git a/tests/test_crop_1.cpp b/tests/test_crop_1.cpp new file mode 100644 index 00000000000..c875a51c7fa --- /dev/null +++ b/tests/test_crop_1.cpp @@ -0,0 +1,377 @@ +// 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 "layer/crop.h" +#include "testutil.h" + +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 ncnn::Mat IntArrayMat(int a0, int a1, int a2, int a3) +{ + ncnn::Mat m(4); + int* p = m; + p[0] = a0; + p[1] = a1; + p[2] = a2; + p[3] = a3; + return m; +} + +static void print_int_array(const ncnn::Mat& a) +{ + const int* pa = a; + + fprintf(stderr, "["); + for (int i = 0; i < a.w; i++) + { + fprintf(stderr, " %d", pa[i]); + } + fprintf(stderr, " ]"); +} + +static int test_crop(const ncnn::Mat& a, const ncnn::Mat& starts, const ncnn::Mat& ends, const ncnn::Mat& axes) +{ + ncnn::ParamDict pd; + pd.set(9, starts); // starts + pd.set(10, ends); // ends + pd.set(11, axes); // axes + + std::vector weights(0); + + int ret = test_layer("Crop", pd, weights, a); + if (ret != 0) + { + fprintf(stderr, "test_crop failed a.dims=%d a=(%d %d %d %d)", a.dims, a.w, a.h, a.d, 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, "\n"); + } + + return ret; +} + +static int test_crop_1(const ncnn::Mat& a) +{ + return 0 + || test_crop(a, IntArrayMat(12), IntArrayMat(-233), IntArrayMat(0)) + || test_crop(a, IntArrayMat(16), IntArrayMat(-233), IntArrayMat(0)) + || test_crop(a, IntArrayMat(11), IntArrayMat(11 + 16), IntArrayMat(0)) + || test_crop(a, IntArrayMat(12), IntArrayMat(12 + 7), IntArrayMat(-1)) + || test_crop(a, IntArrayMat(16), IntArrayMat(16 + 12), ncnn::Mat()) + || test_crop(a, IntArrayMat(11), IntArrayMat(-7 + 1), IntArrayMat(0)) + || test_crop(a, IntArrayMat(12), IntArrayMat(-12 + 1), IntArrayMat(-1)) + || test_crop(a, IntArrayMat(16), IntArrayMat(-16 + 1), ncnn::Mat()); +} + +static int test_crop_4(const ncnn::Mat& a) +{ + return 0 + || test_crop(a, IntArrayMat(12), IntArrayMat(-233), IntArrayMat(0)) + || test_crop(a, IntArrayMat(8), IntArrayMat(-233), IntArrayMat(0)) + || test_crop(a, IntArrayMat(4), IntArrayMat(-233), IntArrayMat(1)) + || test_crop(a, IntArrayMat(5, 11), IntArrayMat(-233, -233), IntArrayMat(0, 1)) + + || test_crop(a, IntArrayMat(11), IntArrayMat(11 + 16), IntArrayMat(0)) + || test_crop(a, IntArrayMat(12), IntArrayMat(12 + 7), IntArrayMat(0)) + || test_crop(a, IntArrayMat(8), IntArrayMat(8 + 12), IntArrayMat(-2)) + + || test_crop(a, IntArrayMat(5), IntArrayMat(8), IntArrayMat(1)) + || test_crop(a, IntArrayMat(6), IntArrayMat(9), IntArrayMat(1)) + || test_crop(a, IntArrayMat(4), IntArrayMat(12), IntArrayMat(-1)) + + || test_crop(a, IntArrayMat(11, 5), IntArrayMat(11 + 7, 11), IntArrayMat(0, 1)) + || test_crop(a, IntArrayMat(12, 6), IntArrayMat(12 + 12, 12), IntArrayMat(0, 1)) + || test_crop(a, IntArrayMat(8, 4), IntArrayMat(8 + 16, 10), IntArrayMat(0, -1)) + + || test_crop(a, IntArrayMat(11), IntArrayMat(-16 + 1), IntArrayMat(0)) + || test_crop(a, IntArrayMat(12), IntArrayMat(-7 + 1), IntArrayMat(0)) + || test_crop(a, IntArrayMat(8), IntArrayMat(-12 + 1), IntArrayMat(-2)) + + || test_crop(a, IntArrayMat(5), IntArrayMat(-5 + 1), IntArrayMat(1)) + || test_crop(a, IntArrayMat(6), IntArrayMat(-6 + 1), IntArrayMat(1)) + || test_crop(a, IntArrayMat(4), IntArrayMat(-4 + 1), IntArrayMat(-1)) + + || test_crop(a, IntArrayMat(11, 5), IntArrayMat(-12 + 1, -6 + 1), IntArrayMat(0, 1)) + || test_crop(a, IntArrayMat(12, 6), IntArrayMat(-16 + 1, -5 + 1), IntArrayMat(0, 1)) + || test_crop(a, IntArrayMat(8, 4), IntArrayMat(-7 + 1, -4 + 1), IntArrayMat(-2, -1)); +} + +static int test_crop_7(const ncnn::Mat& a) +{ + return 0 + || test_crop(a, IntArrayMat(11), IntArrayMat(-233), IntArrayMat(0)) + || test_crop(a, IntArrayMat(8), IntArrayMat(-233), IntArrayMat(0)) + || test_crop(a, IntArrayMat(5), IntArrayMat(-233), IntArrayMat(1)) + || test_crop(a, IntArrayMat(6), IntArrayMat(-233), IntArrayMat(2)) + || test_crop(a, IntArrayMat(4), IntArrayMat(-233), IntArrayMat(-1)) + || test_crop(a, IntArrayMat(12, 6), IntArrayMat(-233, -233), IntArrayMat(0, 1)) + || test_crop(a, IntArrayMat(11, 5), IntArrayMat(-233, -233), IntArrayMat(0, -1)) + || test_crop(a, IntArrayMat(8, 4), IntArrayMat(-233, -233), IntArrayMat(0, 2)) + || test_crop(a, IntArrayMat(6, 6), IntArrayMat(-233, -233), IntArrayMat(1, -1)) + || test_crop(a, IntArrayMat(11, 5, 5), IntArrayMat(-233, -233, -233), IntArrayMat(0, 1, 2)) + || test_crop(a, IntArrayMat(8, 4, 4), IntArrayMat(-233, -233, -233), IntArrayMat(0, 1, -1)) + + || test_crop(a, IntArrayMat(11), IntArrayMat(11 + 7), IntArrayMat(0)) + || test_crop(a, IntArrayMat(12), IntArrayMat(12 + 12), IntArrayMat(0)) + || test_crop(a, IntArrayMat(8), IntArrayMat(8 + 16), IntArrayMat(0)) + + || test_crop(a, IntArrayMat(5), IntArrayMat(13), IntArrayMat(1)) + || test_crop(a, IntArrayMat(6), IntArrayMat(12), IntArrayMat(1)) + || test_crop(a, IntArrayMat(4), IntArrayMat(11), IntArrayMat(-2)) + + || test_crop(a, IntArrayMat(5), IntArrayMat(12), IntArrayMat(2)) + || test_crop(a, IntArrayMat(6), IntArrayMat(11), IntArrayMat(2)) + || test_crop(a, IntArrayMat(4), IntArrayMat(13), IntArrayMat(-1)) + + || test_crop(a, IntArrayMat(11, 5), IntArrayMat(11 + 7, 11), IntArrayMat(0, 1)) + || test_crop(a, IntArrayMat(12, 6), IntArrayMat(12 + 16, 12), IntArrayMat(0, 1)) + || test_crop(a, IntArrayMat(8, 4), IntArrayMat(8 + 12, 13), IntArrayMat(0, -2)) + + || test_crop(a, IntArrayMat(11, 5), IntArrayMat(11 + 16, 13), IntArrayMat(0, 2)) + || test_crop(a, IntArrayMat(12, 6), IntArrayMat(12 + 12, 11), IntArrayMat(0, 2)) + || test_crop(a, IntArrayMat(8, 4), IntArrayMat(8 + 7, 12), IntArrayMat(0, -1)) + + || test_crop(a, IntArrayMat(5, 4), IntArrayMat(12, 12), IntArrayMat(1, 2)) + || test_crop(a, IntArrayMat(6, 3), IntArrayMat(13, 13), IntArrayMat(1, 2)) + || test_crop(a, IntArrayMat(4, 2), IntArrayMat(11, 11), IntArrayMat(-2, -1)) + + || test_crop(a, IntArrayMat(11, 5, 2), IntArrayMat(11 + 7, 11, 11), IntArrayMat(0, 1, 2)) + || test_crop(a, IntArrayMat(12, 6, 4), IntArrayMat(12 + 16, 12, 12), IntArrayMat(0, 1, 2)) + || test_crop(a, IntArrayMat(8, 4, 3), IntArrayMat(8 + 12, 13, 13), IntArrayMat(-3, -2, -1)) + + || test_crop(a, IntArrayMat(11), IntArrayMat(-7 + 1), IntArrayMat(0)) + || test_crop(a, IntArrayMat(12), IntArrayMat(-12 + 1), IntArrayMat(0)) + || test_crop(a, IntArrayMat(8), IntArrayMat(-16 + 1), IntArrayMat(-3)) + + || test_crop(a, IntArrayMat(5), IntArrayMat(-6 + 1), IntArrayMat(1)) + || test_crop(a, IntArrayMat(6), IntArrayMat(-5 + 1), IntArrayMat(1)) + || test_crop(a, IntArrayMat(4), IntArrayMat(-4 + 1), IntArrayMat(-2)) + + || test_crop(a, IntArrayMat(5), IntArrayMat(-5 + 1), IntArrayMat(2)) + || test_crop(a, IntArrayMat(6), IntArrayMat(-4 + 1), IntArrayMat(2)) + || test_crop(a, IntArrayMat(4), IntArrayMat(-6 + 1), IntArrayMat(-1)) + + || test_crop(a, IntArrayMat(11, 5), IntArrayMat(-7 + 1, -4 + 1), IntArrayMat(0, 1)) + || test_crop(a, IntArrayMat(12, 6), IntArrayMat(-12 + 1, -6 + 1), IntArrayMat(0, 1)) + || test_crop(a, IntArrayMat(8, 4), IntArrayMat(-16 + 1, -5 + 1), IntArrayMat(-3, -2)) + + || test_crop(a, IntArrayMat(11, 5), IntArrayMat(-12 + 1, -6 + 1), IntArrayMat(0, 2)) + || test_crop(a, IntArrayMat(12, 6), IntArrayMat(-16 + 1, -5 + 1), IntArrayMat(0, 2)) + || test_crop(a, IntArrayMat(8, 4), IntArrayMat(-7 + 1, -4 + 1), IntArrayMat(-3, -1)) + + || test_crop(a, IntArrayMat(5, 2), IntArrayMat(-5 + 1, -5 + 1), IntArrayMat(1, 2)) + || test_crop(a, IntArrayMat(6, 4), IntArrayMat(-4 + 1, -4 + 1), IntArrayMat(1, 2)) + || test_crop(a, IntArrayMat(4, 3), IntArrayMat(-6 + 1, -6 + 1), IntArrayMat(-2, -1)) + + || test_crop(a, IntArrayMat(11, 5, 4), IntArrayMat(-7 + 1, -5 + 1, -5 + 1), IntArrayMat(0, 1, 2)) + || test_crop(a, IntArrayMat(12, 6, 3), IntArrayMat(-12 + 1, -6 + 1, -6 + 1), IntArrayMat(0, 1, 2)) + || test_crop(a, IntArrayMat(8, 4, 2), IntArrayMat(-16 + 1, -4 + 1, -4 + 1), IntArrayMat(-3, -2, -1)); +} + +static int test_crop_10(const ncnn::Mat& a) +{ + return 0 + || test_crop(a, IntArrayMat(11), IntArrayMat(-233), IntArrayMat(0)) + || test_crop(a, IntArrayMat(8), IntArrayMat(-233), IntArrayMat(0)) + || test_crop(a, IntArrayMat(6), IntArrayMat(-233), IntArrayMat(1)) + || test_crop(a, IntArrayMat(5), IntArrayMat(-233), IntArrayMat(2)) + || test_crop(a, IntArrayMat(4), IntArrayMat(-233), IntArrayMat(-2)) + || test_crop(a, IntArrayMat(6), IntArrayMat(-233), IntArrayMat(3)) + || test_crop(a, IntArrayMat(5), IntArrayMat(-233), IntArrayMat(-1)) + || test_crop(a, IntArrayMat(8, 4), IntArrayMat(-233, -233), IntArrayMat(0, 1)) + || test_crop(a, IntArrayMat(12, 6), IntArrayMat(-233, -233), IntArrayMat(0, 2)) + || test_crop(a, IntArrayMat(11, 5), IntArrayMat(-233, -233), IntArrayMat(-4, -2)) + || test_crop(a, IntArrayMat(4, 4), IntArrayMat(-233, -233), IntArrayMat(1, 2)) + || test_crop(a, IntArrayMat(12, 6), IntArrayMat(-233, -233), IntArrayMat(0, 3)) + || test_crop(a, IntArrayMat(5, 5), IntArrayMat(-233, -233), IntArrayMat(1, 3)) + || test_crop(a, IntArrayMat(4, 4), IntArrayMat(-233, -233), IntArrayMat(2, 3)) + || test_crop(a, IntArrayMat(12, 6, 6), IntArrayMat(-233, -233, -233), IntArrayMat(0, 1, 2)) + || test_crop(a, IntArrayMat(11, 5, 5), IntArrayMat(-233, -233, -233), IntArrayMat(0, 1, 2)) + || test_crop(a, IntArrayMat(8, 4, 4), IntArrayMat(-233, -233, -233), IntArrayMat(0, 1, 3)) + || test_crop(a, IntArrayMat(12, 6, 6), IntArrayMat(-233, -233, -233), IntArrayMat(0, 2, 3)) + || test_crop(a, IntArrayMat(11, 5, 5), IntArrayMat(-233, -233, -233), IntArrayMat(0, 2, 3)) + || test_crop(a, IntArrayMat(4, 4, 4), IntArrayMat(-233, -233, -233), IntArrayMat(1, 2, 3)) + || test_crop(a, IntArrayMat(6, 6, 6), IntArrayMat(-233, -233, -233), IntArrayMat(1, 2, 3)) + || test_crop(a, IntArrayMat(11, 5, 5, 5), IntArrayMat(-233, -233, -233, -233), IntArrayMat(0, 1, 2, 3)) + || test_crop(a, IntArrayMat(8, 4, 4, 4), IntArrayMat(-233, -233, -233, -233), IntArrayMat(0, 1, 2, 3)) + || test_crop(a, IntArrayMat(12, 6, 6, 6), IntArrayMat(-233, -233, -233, -233), IntArrayMat(-4, -3, -2, -1)) + + || test_crop(a, IntArrayMat(11), IntArrayMat(11 + 16), IntArrayMat(0)) + || test_crop(a, IntArrayMat(12), IntArrayMat(12 + 7), IntArrayMat(0)) + || test_crop(a, IntArrayMat(8), IntArrayMat(8 + 12), IntArrayMat(-4)) + + || test_crop(a, IntArrayMat(5), IntArrayMat(11), IntArrayMat(1)) + || test_crop(a, IntArrayMat(6), IntArrayMat(13), IntArrayMat(1)) + || test_crop(a, IntArrayMat(4), IntArrayMat(12), IntArrayMat(-3)) + + || test_crop(a, IntArrayMat(3), IntArrayMat(12), IntArrayMat(2)) + || test_crop(a, IntArrayMat(4), IntArrayMat(13), IntArrayMat(2)) + || test_crop(a, IntArrayMat(5), IntArrayMat(11), IntArrayMat(-2)) + + || test_crop(a, IntArrayMat(1), IntArrayMat(8), IntArrayMat(3)) + || test_crop(a, IntArrayMat(2), IntArrayMat(7), IntArrayMat(3)) + || test_crop(a, IntArrayMat(3), IntArrayMat(6), IntArrayMat(-1)) + + || test_crop(a, IntArrayMat(11, 5), IntArrayMat(11 + 7, 11), IntArrayMat(0, 1)) + || test_crop(a, IntArrayMat(12, 6), IntArrayMat(12 + 12, 12), IntArrayMat(0, 1)) + || test_crop(a, IntArrayMat(8, 4), IntArrayMat(8 + 16, 13), IntArrayMat(-4, -3)) + + || test_crop(a, IntArrayMat(11, 4), IntArrayMat(11 + 12, 13), IntArrayMat(0, 2)) + || test_crop(a, IntArrayMat(12, 3), IntArrayMat(12 + 16, 11), IntArrayMat(0, 2)) + || test_crop(a, IntArrayMat(8, 2), IntArrayMat(8 + 7, 12), IntArrayMat(-4, -2)) + + || test_crop(a, IntArrayMat(11, 1), IntArrayMat(11 + 16, 5), IntArrayMat(0, 3)) + || test_crop(a, IntArrayMat(12, 2), IntArrayMat(12 + 7, 6), IntArrayMat(0, 3)) + || test_crop(a, IntArrayMat(8, 3), IntArrayMat(8 + 12, 7), IntArrayMat(-4, -1)) + + || test_crop(a, IntArrayMat(3, 3), IntArrayMat(13, 4), IntArrayMat(1, 2)) + || test_crop(a, IntArrayMat(4, 2), IntArrayMat(12, 3), IntArrayMat(1, 2)) + || test_crop(a, IntArrayMat(5, 1), IntArrayMat(11, 2), IntArrayMat(-3, -2)) + + || test_crop(a, IntArrayMat(5, 5), IntArrayMat(11, 8), IntArrayMat(1, 3)) + || test_crop(a, IntArrayMat(4, 6), IntArrayMat(12, 9), IntArrayMat(1, 3)) + || test_crop(a, IntArrayMat(3, 4), IntArrayMat(13, 7), IntArrayMat(-3, -1)) + + || test_crop(a, IntArrayMat(2, 3), IntArrayMat(12, 9), IntArrayMat(2, 3)) + || test_crop(a, IntArrayMat(3, 2), IntArrayMat(11, 7), IntArrayMat(2, 3)) + || test_crop(a, IntArrayMat(4, 1), IntArrayMat(10, 8), IntArrayMat(-2, -1)) + + || test_crop(a, IntArrayMat(11, 2, 2), IntArrayMat(11 + 6, 9, 9), IntArrayMat(0, 1, 2)) + || test_crop(a, IntArrayMat(12, 3, 3), IntArrayMat(12 + 1, 10, 10), IntArrayMat(0, 1, 2)) + || test_crop(a, IntArrayMat(8, 4, 4), IntArrayMat(8 + 3, 11, 11), IntArrayMat(-4, -3, -2)) + + || test_crop(a, IntArrayMat(11, 4, 4), IntArrayMat(11 + 12, 12, 12), IntArrayMat(0, 1, 3)) + || test_crop(a, IntArrayMat(12, 5, 5), IntArrayMat(12 + 8, 11, 11), IntArrayMat(0, 1, 3)) + || test_crop(a, IntArrayMat(8, 6, 6), IntArrayMat(8 + 4, 13, 13), IntArrayMat(-4, -3, -1)) + + || test_crop(a, IntArrayMat(11, 1, 4), IntArrayMat(11 + 5, 12, 12), IntArrayMat(0, 2, 3)) + || test_crop(a, IntArrayMat(12, 3, 3), IntArrayMat(12 + 3, 11, 11), IntArrayMat(0, 2, 3)) + || test_crop(a, IntArrayMat(8, 2, 5), IntArrayMat(8 + 2, 10, 10), IntArrayMat(-4, -2, -1)) + + || test_crop(a, IntArrayMat(1, 1, 1), IntArrayMat(7, 7, 7), IntArrayMat(1, 2, 3)) + || test_crop(a, IntArrayMat(2, 2, 2), IntArrayMat(8, 9, 10), IntArrayMat(1, 2, 3)) + || test_crop(a, IntArrayMat(3, 3, 3), IntArrayMat(11, 12, 13), IntArrayMat(-3, -2, -1)) + + || test_crop(a, IntArrayMat(11, 2, 3, 6), IntArrayMat(11 + 11, 10, 12, 11), IntArrayMat(0, 1, 2, 3)) + || test_crop(a, IntArrayMat(12, 3, 4, 5), IntArrayMat(12 + 12, 9, 11, 13), IntArrayMat(0, 1, 2, 3)) + || test_crop(a, IntArrayMat(8, 4, 5, 4), IntArrayMat(8 + 8, 8, 10, 12), IntArrayMat(-4, -3, -2, -1)) + + || test_crop(a, IntArrayMat(11), IntArrayMat(-7 + 1), IntArrayMat(0)) + || test_crop(a, IntArrayMat(12), IntArrayMat(-12 + 1), IntArrayMat(0)) + || test_crop(a, IntArrayMat(8), IntArrayMat(-16 + 1), IntArrayMat(-4)) + + || test_crop(a, IntArrayMat(5), IntArrayMat(-6 + 1), IntArrayMat(1)) + || test_crop(a, IntArrayMat(6), IntArrayMat(-5 + 1), IntArrayMat(1)) + || test_crop(a, IntArrayMat(4), IntArrayMat(-4 + 1), IntArrayMat(-3)) + + || test_crop(a, IntArrayMat(4), IntArrayMat(-4 + 1), IntArrayMat(2)) + || test_crop(a, IntArrayMat(5), IntArrayMat(-5 + 1), IntArrayMat(2)) + || test_crop(a, IntArrayMat(6), IntArrayMat(-6 + 1), IntArrayMat(-2)) + + || test_crop(a, IntArrayMat(1), IntArrayMat(-5 + 1), IntArrayMat(3)) + || test_crop(a, IntArrayMat(2), IntArrayMat(-4 + 1), IntArrayMat(3)) + || test_crop(a, IntArrayMat(3), IntArrayMat(-3 + 1), IntArrayMat(-1)) + + || test_crop(a, IntArrayMat(11, 3), IntArrayMat(-7 + 1, -3 + 1), IntArrayMat(0, 1)) + || test_crop(a, IntArrayMat(12, 4), IntArrayMat(-12 + 1, -4 + 1), IntArrayMat(0, 1)) + || test_crop(a, IntArrayMat(8, 5), IntArrayMat(-16 + 1, -5 + 1), IntArrayMat(-4, -3)) + + || test_crop(a, IntArrayMat(11, 1), IntArrayMat(-12 + 1, -5 + 1), IntArrayMat(0, 2)) + || test_crop(a, IntArrayMat(12, 2), IntArrayMat(-16 + 1, -4 + 1), IntArrayMat(0, 2)) + || test_crop(a, IntArrayMat(8, 3), IntArrayMat(-7 + 1, -6 + 1), IntArrayMat(-4, -2)) + + || test_crop(a, IntArrayMat(11, 3), IntArrayMat(-12 + 1, -2 + 1), IntArrayMat(0, 3)) + || test_crop(a, IntArrayMat(12, 4), IntArrayMat(-16 + 1, -3 + 1), IntArrayMat(0, 3)) + || test_crop(a, IntArrayMat(8, 5), IntArrayMat(-7 + 1, -4 + 1), IntArrayMat(-4, -1)) + + || test_crop(a, IntArrayMat(2, 3), IntArrayMat(-4 + 1, -2 + 1), IntArrayMat(1, 2)) + || test_crop(a, IntArrayMat(3, 4), IntArrayMat(-2 + 1, -3 + 1), IntArrayMat(1, 2)) + || test_crop(a, IntArrayMat(4, 5), IntArrayMat(-3 + 1, -4 + 1), IntArrayMat(-3, -2)) + + || test_crop(a, IntArrayMat(3, 2), IntArrayMat(-2 + 1, -4 + 1), IntArrayMat(1, 3)) + || test_crop(a, IntArrayMat(4, 3), IntArrayMat(-3 + 1, -2 + 1), IntArrayMat(1, 3)) + || test_crop(a, IntArrayMat(5, 4), IntArrayMat(-4 + 1, -3 + 1), IntArrayMat(-3, -1)) + + || test_crop(a, IntArrayMat(2, 3), IntArrayMat(-4 + 1, -6 + 1), IntArrayMat(2, 3)) + || test_crop(a, IntArrayMat(1, 2), IntArrayMat(-5 + 1, -5 + 1), IntArrayMat(2, 3)) + || test_crop(a, IntArrayMat(3, 1), IntArrayMat(-6 + 1, -4 + 1), IntArrayMat(-2, -1)) + + || test_crop(a, IntArrayMat(11, 3, 3), IntArrayMat(-7 + 1, -3 + 1, -4 + 1), IntArrayMat(0, 1, 2)) + || test_crop(a, IntArrayMat(12, 4, 4), IntArrayMat(-12 + 1, -4 + 1, -3 + 1), IntArrayMat(0, 1, 2)) + || test_crop(a, IntArrayMat(8, 5, 5), IntArrayMat(-16 + 1, -5 + 1, -5 + 1), IntArrayMat(-4, -3, -2)) + + || test_crop(a, IntArrayMat(11, 2, 2), IntArrayMat(-7 + 1, -5 + 1, -4 + 1), IntArrayMat(0, 1, 3)) + || test_crop(a, IntArrayMat(12, 1, 1), IntArrayMat(-12 + 1, -6 + 1, -5 + 1), IntArrayMat(0, 1, 3)) + || test_crop(a, IntArrayMat(8, 3, 3), IntArrayMat(-16 + 1, -4 + 1, -6 + 1), IntArrayMat(-4, -3, -1)) + + || test_crop(a, IntArrayMat(11, 2, 5), IntArrayMat(-7 + 1, -2 + 1, -5 + 1), IntArrayMat(0, 2, 3)) + || test_crop(a, IntArrayMat(12, 3, 3), IntArrayMat(-12 + 1, -3 + 1, -4 + 1), IntArrayMat(0, 2, 3)) + || test_crop(a, IntArrayMat(8, 4, 4), IntArrayMat(-16 + 1, -4 + 1, -3 + 1), IntArrayMat(-4, -2, -1)) + + || test_crop(a, IntArrayMat(1, 3, 3), IntArrayMat(-3 + 1, -6 + 1, -4 + 1), IntArrayMat(1, 2, 3)) + || test_crop(a, IntArrayMat(2, 2, 2), IntArrayMat(-4 + 1, -4 + 1, -5 + 1), IntArrayMat(1, 2, 3)) + || test_crop(a, IntArrayMat(3, 1, 1), IntArrayMat(-5 + 1, -5 + 1, -6 + 1), IntArrayMat(-3, -2, -1)) + + || test_crop(a, IntArrayMat(11, 3, 4, 4), IntArrayMat(-7 + 1, -3 + 1, -2 + 1, -4 + 1), IntArrayMat(0, 1, 2, 3)) + || test_crop(a, IntArrayMat(12, 4, 5, 3), IntArrayMat(-12 + 1, -4 + 1, -3 + 1, -5 + 1), IntArrayMat(0, 1, 2, 3)) + || test_crop(a, IntArrayMat(8, 5, 6, 2), IntArrayMat(-16 + 1, -5 + 1, -4 + 1, -3 + 1), IntArrayMat(-4, -3, -2, -1)); +} + +int main() +{ + SRAND(776757); + + return 0 + || test_crop_1(RandomMat(112)) + || test_crop_1(RandomMat(126)) + || test_crop_1(RandomMat(127)) + || test_crop_4(RandomMat(20, 48)) + || test_crop_4(RandomMat(15, 36)) + || test_crop_4(RandomMat(16, 33)) + || test_crop_7(RandomMat(20, 20, 48)) + || test_crop_7(RandomMat(15, 15, 36)) + || test_crop_7(RandomMat(16, 16, 33)) + || test_crop_10(RandomMat(20, 20, 20, 48)) + || test_crop_10(RandomMat(15, 15, 15, 36)) + || test_crop_10(RandomMat(16, 16, 16, 33)); +} diff --git a/tests/test_crop_2.cpp b/tests/test_crop_2.cpp new file mode 100644 index 00000000000..287634b973e --- /dev/null +++ b/tests/test_crop_2.cpp @@ -0,0 +1,122 @@ +// 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 "layer/crop.h" +#include "testutil.h" + +static int test_crop(const ncnn::Mat& a, int woffset, int hoffset, int doffset, int coffset, const ncnn::Mat& ref) +{ + ncnn::ParamDict pd; + pd.set(0, woffset); + pd.set(1, hoffset); + pd.set(13, doffset); + pd.set(2, coffset); + pd.set(3, 0); // outw + pd.set(4, 0); // outh + pd.set(14, 0); // outd + pd.set(5, 0); // outc + pd.set(6, 0); // woffset2 + pd.set(7, 0); // hoffset2 + pd.set(15, 0); // doffset2 + pd.set(8, 0); // coffset2 + + std::vector weights(0); + + std::vector ab(2); + ab[0] = a; + ab[1] = ref; + + int ret = test_layer("Crop", pd, weights, ab); + if (ret != 0) + { + fprintf(stderr, "test_crop failed a.dims=%d a=(%d %d %d %d) woffset=%d hoffset=%d doffset=%d coffset=%d ref.dims=%d ref=(%d %d %d %d)\n", a.dims, a.w, a.h, a.d, a.c, woffset, hoffset, doffset, coffset, ref.dims, ref.w, ref.h, ref.d, ref.c); + } + + return ret; +} + +static int test_crop_2(const ncnn::Mat& a) +{ + return 0 + || test_crop(a, 0, 0, 0, 0, a) + || test_crop(a, 0, 0, 0, 0, ncnn::Mat(27)) + + || test_crop(a, 11, 0, 0, 0, ncnn::Mat(7)) + || test_crop(a, 12, 0, 0, 0, ncnn::Mat(12)) + || test_crop(a, 16, 0, 0, 0, ncnn::Mat(16)); +} + +static int test_crop_5(const ncnn::Mat& a) +{ + return 0 + || test_crop(a, 0, 0, 0, 0, a) + + || test_crop(a, 0, 12, 0, 0, ncnn::Mat(8, 7)) + || test_crop(a, 5, 0, 0, 0, ncnn::Mat(7, 27)) + + || test_crop(a, 5, 11, 0, 0, ncnn::Mat(5, 12)) + || test_crop(a, 6, 12, 0, 0, ncnn::Mat(4, 16)) + || test_crop(a, 4, 8, 0, 0, ncnn::Mat(6, 7)); +} + +static int test_crop_8(const ncnn::Mat& a) +{ + return 0 + || test_crop(a, 0, 0, 0, 0, a) + + || test_crop(a, 0, 5, 0, 0, ncnn::Mat(6, 6)) + || test_crop(a, 6, 0, 0, 0, ncnn::Mat(8, 8)) + || test_crop(a, 5, 2, 0, 0, ncnn::Mat(6, 3)) + || test_crop(a, 6, 3, 0, 0, ncnn::Mat(8, 4)) + || test_crop(a, 4, 4, 0, 0, ncnn::Mat(7, 5)) + + || test_crop(a, 5, 3, 0, 11, ncnn::Mat(7, 3, 7)) + || test_crop(a, 6, 4, 0, 12, ncnn::Mat(6, 4, 12)) + || test_crop(a, 4, 2, 0, 8, ncnn::Mat(5, 5, 16)); +} + +static int test_crop_11(const ncnn::Mat& a) +{ + return 0 + || test_crop(a, 0, 0, 0, 0, a) + + || test_crop(a, 0, 5, 0, 0, ncnn::Mat(6, 6, 6)) + || test_crop(a, 6, 0, 0, 0, ncnn::Mat(8, 8, 8)) + || test_crop(a, 5, 5, 5, 0, ncnn::Mat(6, 6, 6)) + || test_crop(a, 6, 6, 6, 0, ncnn::Mat(8, 8, 8)) + || test_crop(a, 4, 4, 4, 0, ncnn::Mat(5, 5, 5)) + + || test_crop(a, 3, 3, 3, 11, ncnn::Mat(3, 3, 3, 7)) + || test_crop(a, 4, 4, 4, 12, ncnn::Mat(6, 6, 6, 12)) + || test_crop(a, 5, 5, 5, 8, ncnn::Mat(8, 8, 8, 16)); +} + +int main() +{ + SRAND(776757); + + return 0 + || test_crop_2(RandomMat(112)) + || test_crop_2(RandomMat(126)) + || test_crop_2(RandomMat(127)) + || test_crop_5(RandomMat(20, 48)) + || test_crop_5(RandomMat(15, 36)) + || test_crop_5(RandomMat(16, 33)) + || test_crop_8(RandomMat(20, 20, 48)) + || test_crop_8(RandomMat(15, 15, 36)) + || test_crop_8(RandomMat(16, 16, 33)) + || test_crop_11(RandomMat(20, 20, 20, 48)) + || test_crop_11(RandomMat(15, 15, 15, 36)) + || test_crop_11(RandomMat(16, 16, 16, 33)); +} diff --git a/tests/test_deformableconv2d.cpp b/tests/test_deformableconv2d.cpp index b62557df98c..6cc2d614138 100644 --- a/tests/test_deformableconv2d.cpp +++ b/tests/test_deformableconv2d.cpp @@ -59,7 +59,7 @@ static int test_deformableconv2d(int w, int h, int c, int outch, int kernel, int static int test_deformableconv2d_0() { - static const int kdsp[16][4] = { + static const int kdsp[10][4] = { {1, 1, 1, 0}, {1, 1, 2, 0}, {2, 1, 1, 1}, @@ -67,18 +67,12 @@ static int test_deformableconv2d_0() {3, 1, 1, 1}, {3, 1, 2, 1}, {3, 2, 1, 1}, - {4, 1, 1, 0}, {4, 1, 2, 1}, - {4, 2, 1, 1}, - {5, 1, 1, 2}, {5, 1, 2, 2}, {5, 2, 2, 2}, - {7, 1, 1, 3}, - {7, 1, 2, 3}, - {7, 2, 1, 3}, }; - for (int i = 0; i < 16; i++) + for (int i = 0; i < 4; i++) { const int k = kdsp[i][0]; const int d = kdsp[i][1]; diff --git a/tests/test_deformableconv2d_1.cpp b/tests/test_deformableconv2d_1.cpp new file mode 100644 index 00000000000..2f2febf469d --- /dev/null +++ b/tests/test_deformableconv2d_1.cpp @@ -0,0 +1,120 @@ +// Tencent is pleased to support the open source community by making ncnn available. +// +// Copyright (C) 2019 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 "layer/deformableconv2d.h" +#include "testutil.h" + +static int test_deformableconv2d(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias) +{ + const int kernel_extent_w = dilation * (kernel - 1) + 1; + const int kernel_extent_h = dilation * (kernel - 1) + 1; + const int out_w = (w + pad + pad - kernel_extent_w) / stride + 1; + const int out_h = (h + pad + pad - kernel_extent_h) / stride + 1; + std::vector a(3); + a[0] = RandomMat(w, h, c); + a[1] = RandomMat(out_w, out_h, kernel * kernel * 2); + a[2] = RandomMat(out_w, out_h, kernel * kernel); + + ncnn::ParamDict pd; + pd.set(0, outch); + pd.set(1, kernel); + pd.set(2, dilation); + pd.set(3, stride); + pd.set(4, pad); + pd.set(5, bias); + pd.set(6, outch * c * kernel * kernel); + + int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 + ncnn::Mat activation_params(2); + activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha + activation_params[1] = RandomFloat(0, 1); // beta + pd.set(9, activation_type); + pd.set(10, activation_params); + + std::vector weights(bias ? 2 : 1); + weights[0] = RandomMat(outch * c * kernel * kernel); + if (bias) + weights[1] = RandomMat(outch); + + float epsilon = 0.001; + int ret = test_layer("DeformableConv2D", pd, weights, a, 1, epsilon); + if (ret != 0) + { + fprintf(stderr, "test_deformableconv2d failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]); + } + + return ret; +} + +static int test_deformableconv2d_0() +{ + static const int kdsp[10][4] = { + {1, 1, 1, 0}, + {1, 1, 2, 0}, + {2, 1, 1, 1}, + {2, 1, 2, 0}, + {3, 1, 1, 1}, + {3, 1, 2, 1}, + {3, 2, 1, 1}, + {4, 1, 2, 1}, + {5, 1, 2, 2}, + {5, 2, 2, 2}, + }; + + for (int i = 4; i < 6; i++) + { + const int k = kdsp[i][0]; + const int d = kdsp[i][1]; + const int s = kdsp[i][2]; + const int p = kdsp[i][3]; + + int ret = 0 + || test_deformableconv2d(9, 7, 1, 1, k, d, s, p, 1) + || test_deformableconv2d(9, 7, 4, 13, k, d, s, p, 0) + || test_deformableconv2d(9, 7, 13, 4, k, d, s, p, 1) + || test_deformableconv2d(9, 7, 4, 8, k, d, s, p, 0) + || test_deformableconv2d(9, 7, 8, 4, k, d, s, p, 1) + || test_deformableconv2d(9, 7, 8, 13, k, d, s, p, 0) + || test_deformableconv2d(9, 7, 13, 8, k, d, s, p, 1) + || test_deformableconv2d(9, 7, 16, 16, k, d, s, p, 0) + || test_deformableconv2d(16, 16, 1 * 3, 1 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 1 * 3, 4 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 1 * 3, 8 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 1 * 3, 16 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 4 * 3, 1 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 4 * 3, 4 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 4 * 3, 8 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 4 * 3, 16 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 8 * 3, 1 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 8 * 3, 4 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 8 * 3, 8 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 8 * 3, 16 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 16 * 3, 1 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 16 * 3, 4 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 16 * 3, 8 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 16 * 3, 16 * 3, k, d, s, p, 1); + + if (ret != 0) + return -1; + } + + return 0; +} + +int main() +{ + SRAND(7767517); + + return test_deformableconv2d_0(); +} diff --git a/tests/test_deformableconv2d_2.cpp b/tests/test_deformableconv2d_2.cpp new file mode 100644 index 00000000000..130761d87d5 --- /dev/null +++ b/tests/test_deformableconv2d_2.cpp @@ -0,0 +1,120 @@ +// Tencent is pleased to support the open source community by making ncnn available. +// +// Copyright (C) 2019 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 "layer/deformableconv2d.h" +#include "testutil.h" + +static int test_deformableconv2d(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias) +{ + const int kernel_extent_w = dilation * (kernel - 1) + 1; + const int kernel_extent_h = dilation * (kernel - 1) + 1; + const int out_w = (w + pad + pad - kernel_extent_w) / stride + 1; + const int out_h = (h + pad + pad - kernel_extent_h) / stride + 1; + std::vector a(3); + a[0] = RandomMat(w, h, c); + a[1] = RandomMat(out_w, out_h, kernel * kernel * 2); + a[2] = RandomMat(out_w, out_h, kernel * kernel); + + ncnn::ParamDict pd; + pd.set(0, outch); + pd.set(1, kernel); + pd.set(2, dilation); + pd.set(3, stride); + pd.set(4, pad); + pd.set(5, bias); + pd.set(6, outch * c * kernel * kernel); + + int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 + ncnn::Mat activation_params(2); + activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha + activation_params[1] = RandomFloat(0, 1); // beta + pd.set(9, activation_type); + pd.set(10, activation_params); + + std::vector weights(bias ? 2 : 1); + weights[0] = RandomMat(outch * c * kernel * kernel); + if (bias) + weights[1] = RandomMat(outch); + + float epsilon = 0.001; + int ret = test_layer("DeformableConv2D", pd, weights, a, 1, epsilon); + if (ret != 0) + { + fprintf(stderr, "test_deformableconv2d failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]); + } + + return ret; +} + +static int test_deformableconv2d_0() +{ + static const int kdsp[10][4] = { + {1, 1, 1, 0}, + {1, 1, 2, 0}, + {2, 1, 1, 1}, + {2, 1, 2, 0}, + {3, 1, 1, 1}, + {3, 1, 2, 1}, + {3, 2, 1, 1}, + {4, 1, 2, 1}, + {5, 1, 2, 2}, + {5, 2, 2, 2}, + }; + + for (int i = 6; i < 8; i++) + { + const int k = kdsp[i][0]; + const int d = kdsp[i][1]; + const int s = kdsp[i][2]; + const int p = kdsp[i][3]; + + int ret = 0 + || test_deformableconv2d(9, 7, 1, 1, k, d, s, p, 1) + || test_deformableconv2d(9, 7, 4, 13, k, d, s, p, 0) + || test_deformableconv2d(9, 7, 13, 4, k, d, s, p, 1) + || test_deformableconv2d(9, 7, 4, 8, k, d, s, p, 0) + || test_deformableconv2d(9, 7, 8, 4, k, d, s, p, 1) + || test_deformableconv2d(9, 7, 8, 13, k, d, s, p, 0) + || test_deformableconv2d(9, 7, 13, 8, k, d, s, p, 1) + || test_deformableconv2d(9, 7, 16, 16, k, d, s, p, 0) + || test_deformableconv2d(16, 16, 1 * 3, 1 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 1 * 3, 4 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 1 * 3, 8 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 1 * 3, 16 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 4 * 3, 1 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 4 * 3, 4 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 4 * 3, 8 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 4 * 3, 16 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 8 * 3, 1 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 8 * 3, 4 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 8 * 3, 8 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 8 * 3, 16 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 16 * 3, 1 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 16 * 3, 4 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 16 * 3, 8 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 16 * 3, 16 * 3, k, d, s, p, 1); + + if (ret != 0) + return -1; + } + + return 0; +} + +int main() +{ + SRAND(7767517); + + return test_deformableconv2d_0(); +} diff --git a/tests/test_deformableconv2d_3.cpp b/tests/test_deformableconv2d_3.cpp new file mode 100644 index 00000000000..1a78a004db0 --- /dev/null +++ b/tests/test_deformableconv2d_3.cpp @@ -0,0 +1,120 @@ +// Tencent is pleased to support the open source community by making ncnn available. +// +// Copyright (C) 2019 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 "layer/deformableconv2d.h" +#include "testutil.h" + +static int test_deformableconv2d(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias) +{ + const int kernel_extent_w = dilation * (kernel - 1) + 1; + const int kernel_extent_h = dilation * (kernel - 1) + 1; + const int out_w = (w + pad + pad - kernel_extent_w) / stride + 1; + const int out_h = (h + pad + pad - kernel_extent_h) / stride + 1; + std::vector a(3); + a[0] = RandomMat(w, h, c); + a[1] = RandomMat(out_w, out_h, kernel * kernel * 2); + a[2] = RandomMat(out_w, out_h, kernel * kernel); + + ncnn::ParamDict pd; + pd.set(0, outch); + pd.set(1, kernel); + pd.set(2, dilation); + pd.set(3, stride); + pd.set(4, pad); + pd.set(5, bias); + pd.set(6, outch * c * kernel * kernel); + + int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 + ncnn::Mat activation_params(2); + activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha + activation_params[1] = RandomFloat(0, 1); // beta + pd.set(9, activation_type); + pd.set(10, activation_params); + + std::vector weights(bias ? 2 : 1); + weights[0] = RandomMat(outch * c * kernel * kernel); + if (bias) + weights[1] = RandomMat(outch); + + float epsilon = 0.001; + int ret = test_layer("DeformableConv2D", pd, weights, a, 1, epsilon); + if (ret != 0) + { + fprintf(stderr, "test_deformableconv2d failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]); + } + + return ret; +} + +static int test_deformableconv2d_0() +{ + static const int kdsp[10][4] = { + {1, 1, 1, 0}, + {1, 1, 2, 0}, + {2, 1, 1, 1}, + {2, 1, 2, 0}, + {3, 1, 1, 1}, + {3, 1, 2, 1}, + {3, 2, 1, 1}, + {4, 1, 2, 1}, + {5, 1, 2, 2}, + {5, 2, 2, 2}, + }; + + for (int i = 8; i < 10; i++) + { + const int k = kdsp[i][0]; + const int d = kdsp[i][1]; + const int s = kdsp[i][2]; + const int p = kdsp[i][3]; + + int ret = 0 + || test_deformableconv2d(9, 7, 1, 1, k, d, s, p, 1) + || test_deformableconv2d(9, 7, 4, 13, k, d, s, p, 0) + || test_deformableconv2d(9, 7, 13, 4, k, d, s, p, 1) + || test_deformableconv2d(9, 7, 4, 8, k, d, s, p, 0) + || test_deformableconv2d(9, 7, 8, 4, k, d, s, p, 1) + || test_deformableconv2d(9, 7, 8, 13, k, d, s, p, 0) + || test_deformableconv2d(9, 7, 13, 8, k, d, s, p, 1) + || test_deformableconv2d(9, 7, 16, 16, k, d, s, p, 0) + || test_deformableconv2d(16, 16, 1 * 3, 1 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 1 * 3, 4 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 1 * 3, 8 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 1 * 3, 16 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 4 * 3, 1 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 4 * 3, 4 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 4 * 3, 8 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 4 * 3, 16 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 8 * 3, 1 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 8 * 3, 4 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 8 * 3, 8 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 8 * 3, 16 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 16 * 3, 1 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 16 * 3, 4 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 16 * 3, 8 * 3, k, d, s, p, 1) + || test_deformableconv2d(16, 16, 16 * 3, 16 * 3, k, d, s, p, 1); + + if (ret != 0) + return -1; + } + + return 0; +} + +int main() +{ + SRAND(7767517); + + return test_deformableconv2d_0(); +} diff --git a/tests/test_deformableconv2d_4.cpp b/tests/test_deformableconv2d_4.cpp new file mode 100644 index 00000000000..eca9f289dec --- /dev/null +++ b/tests/test_deformableconv2d_4.cpp @@ -0,0 +1,76 @@ +// Tencent is pleased to support the open source community by making ncnn available. +// +// Copyright (C) 2019 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 "layer/deformableconv2d.h" +#include "testutil.h" + +static int test_deformableconv2d(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias) +{ + const int kernel_extent_w = dilation * (kernel - 1) + 1; + const int kernel_extent_h = dilation * (kernel - 1) + 1; + const int out_w = (w + pad + pad - kernel_extent_w) / stride + 1; + const int out_h = (h + pad + pad - kernel_extent_h) / stride + 1; + std::vector a(3); + a[0] = RandomMat(w, h, c); + a[1] = RandomMat(out_w, out_h, kernel * kernel * 2); + a[2] = RandomMat(out_w, out_h, kernel * kernel); + + ncnn::ParamDict pd; + pd.set(0, outch); + pd.set(1, kernel); + pd.set(2, dilation); + pd.set(3, stride); + pd.set(4, pad); + pd.set(5, bias); + pd.set(6, outch * c * kernel * kernel); + + int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 + ncnn::Mat activation_params(2); + activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha + activation_params[1] = RandomFloat(0, 1); // beta + pd.set(9, activation_type); + pd.set(10, activation_params); + + std::vector weights(bias ? 2 : 1); + weights[0] = RandomMat(outch * c * kernel * kernel); + if (bias) + weights[1] = RandomMat(outch); + + float epsilon = 0.001; + int ret = test_layer("DeformableConv2D", pd, weights, a, 1, epsilon); + if (ret != 0) + { + fprintf(stderr, "test_deformableconv2d failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]); + } + + return ret; +} + +static int test_deformableconv2d_0() +{ + return 0 + || test_deformableconv2d(7, 5, 24, 32, 4, 2, 2, 2, 1) + || test_deformableconv2d(7, 5, 32, 24, 4, 2, 2, 2, 1) + || test_deformableconv2d(7, 5, 28, 32, 4, 2, 2, 2, 1) + || test_deformableconv2d(7, 5, 32, 28, 4, 2, 2, 2, 1) + || test_deformableconv2d(7, 5, 26, 32, 4, 2, 2, 2, 1) + || test_deformableconv2d(7, 5, 32, 26, 4, 2, 2, 2, 1); +} + +int main() +{ + SRAND(7767517); + + return test_deformableconv2d_0(); +}