feat: 切换后端至PaddleOCR-NCNN,切换工程为CMake

1.项目后端整体迁移至PaddleOCR-NCNN算法,已通过基本的兼容性测试
2.工程改为使用CMake组织,后续为了更好地兼容第三方库,不再提供QMake工程
3.重整权利声明文件,重整代码工程,确保最小化侵权风险

Log: 切换后端至PaddleOCR-NCNN,切换工程为CMake
Change-Id: I4d5d2c5d37505a4a24b389b1a4c5d12f17bfa38c
This commit is contained in:
wangzhengyang
2022-05-10 09:54:44 +08:00
parent ecdd171c6f
commit 718c41634f
10018 changed files with 3593797 additions and 186748 deletions

133
3rdparty/ncnn/tests/CMakeLists.txt vendored Normal file
View File

@ -0,0 +1,133 @@
if(MSVC)
# warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
add_definitions(/wd4996)
endif()
macro(ncnn_add_test name)
add_executable(test_${name} test_${name}.cpp)
target_link_libraries(test_${name} PRIVATE ncnn)
add_test(NAME test_${name} COMMAND ${CMAKE_COMMAND} -DTEST_EXECUTABLE=$<TARGET_FILE:test_${name}> -P ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/run_test.cmake)
# add test to a virtual project group
set_property(TARGET test_${name} PROPERTY FOLDER "tests")
endmacro()
macro(ncnn_add_layer_test class)
string(TOLOWER ${class} name)
# enable if WITH_LAYER_xxx option ON
if(${WITH_LAYER_${name}})
ncnn_add_test(${name})
endif()
endmacro()
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src/layer)
if(NCNN_PIXEL_AFFINE)
ncnn_add_test(mat_pixel_affine)
endif()
if(NCNN_PIXEL_DRAWING)
ncnn_add_test(mat_pixel_drawing)
endif()
if(NCNN_PIXEL_ROTATE)
ncnn_add_test(mat_pixel_rotate)
endif()
if(NCNN_PIXEL)
ncnn_add_test(mat_pixel_resize)
ncnn_add_test(mat_pixel)
ncnn_add_test(squeezenet)
endif()
ncnn_add_test(c_api)
ncnn_add_test(cpu)
if(NCNN_VULKAN)
ncnn_add_test(command)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
target_link_libraries(test_squeezenet PRIVATE nodefs.js)
endif()
ncnn_add_layer_test(AbsVal)
ncnn_add_layer_test(BatchNorm)
ncnn_add_layer_test(Bias)
ncnn_add_layer_test(BinaryOp)
ncnn_add_layer_test(Cast)
ncnn_add_layer_test(Clip)
ncnn_add_layer_test(Concat)
ncnn_add_layer_test(Convolution)
ncnn_add_layer_test(Convolution1D)
ncnn_add_layer_test(Convolution3D)
ncnn_add_layer_test(ConvolutionDepthWise)
ncnn_add_layer_test(ConvolutionDepthWise1D)
ncnn_add_layer_test(ConvolutionDepthWise3D)
ncnn_add_layer_test(Crop)
ncnn_add_layer_test(Deconvolution)
ncnn_add_layer_test(Deconvolution1D)
ncnn_add_layer_test(Deconvolution3D)
ncnn_add_layer_test(DeconvolutionDepthWise)
ncnn_add_layer_test(DeconvolutionDepthWise1D)
ncnn_add_layer_test(DeconvolutionDepthWise3D)
ncnn_add_layer_test(DeepCopy)
ncnn_add_layer_test(Dequantize)
ncnn_add_layer_test(Dropout)
ncnn_add_layer_test(Eltwise)
ncnn_add_layer_test(ELU)
ncnn_add_layer_test(ExpandDims)
ncnn_add_layer_test(Flatten)
ncnn_add_layer_test(GELU)
ncnn_add_layer_test(Gemm)
ncnn_add_layer_test(GroupNorm)
ncnn_add_layer_test(GRU)
ncnn_add_layer_test(HardSigmoid)
ncnn_add_layer_test(HardSwish)
ncnn_add_layer_test(InnerProduct)
ncnn_add_layer_test(InstanceNorm)
ncnn_add_layer_test(Interp)
ncnn_add_layer_test(LayerNorm)
ncnn_add_layer_test(LRN)
ncnn_add_layer_test(LSTM)
ncnn_add_layer_test(MatMul)
ncnn_add_layer_test(MemoryData)
ncnn_add_layer_test(Mish)
ncnn_add_layer_test(MultiHeadAttention)
ncnn_add_layer_test(Noop)
ncnn_add_layer_test(Normalize)
ncnn_add_layer_test(Packing)
ncnn_add_layer_test(Padding)
ncnn_add_layer_test(Permute)
ncnn_add_layer_test(PixelShuffle)
ncnn_add_layer_test(Pooling)
ncnn_add_layer_test(Pooling1D)
ncnn_add_layer_test(Pooling3D)
ncnn_add_layer_test(PReLU)
ncnn_add_layer_test(PriorBox)
ncnn_add_layer_test(Quantize)
ncnn_add_layer_test(Reduction)
ncnn_add_layer_test(ReLU)
ncnn_add_layer_test(Reorg)
ncnn_add_layer_test(Requantize)
ncnn_add_layer_test(Reshape)
ncnn_add_layer_test(RNN)
ncnn_add_layer_test(ROIPooling)
ncnn_add_layer_test(ROIAlign)
ncnn_add_layer_test(Scale)
ncnn_add_layer_test(SELU)
ncnn_add_layer_test(ShuffleChannel)
ncnn_add_layer_test(Sigmoid)
ncnn_add_layer_test(Slice)
ncnn_add_layer_test(Softmax)
ncnn_add_layer_test(Softplus)
ncnn_add_layer_test(Squeeze)
ncnn_add_layer_test(Swish)
ncnn_add_layer_test(TanH)
ncnn_add_layer_test(Tile)
ncnn_add_layer_test(UnaryOp)
ncnn_add_layer_test(Yolov3DetectionOutput)

97
3rdparty/ncnn/tests/prng.h vendored Normal file
View File

@ -0,0 +1,97 @@
/* Portable pseudorandom number generator
* Based on a 24,55 Fibonacci generator, using 55/503 rejection
* c.f.- TAoCP, 3.2.2(7), for j=24,k=55,m=2^64
*
* THIS FILE IS PUBLIC DOMAIN CODE.
*
* Written by Bob Adolf.
* Attribution is appreciated but by no means legally required.
*
* This function is sufficient for most non-crypto applications.
* It passes all but one of George Marsaglia's "diehard" randomness tests.
* (overlapping 5-tuple permutations, which is allegedly buggy)
*/
#ifndef __PRNG_H__
#define __PRNG_H__
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define LAG1 (UINT16_C(24))
#define LAG2 (UINT16_C(55))
#define RAND_SSIZE ((UINT16_C(1)) << 6)
#define RAND_SMASK (RAND_SSIZE - 1)
#define RAND_EXHAUST_LIMIT LAG2
// 10x is a heuristic, it just needs to be large enough to remove correlation
#define RAND_REFILL_COUNT ((LAG2 * 10) - RAND_EXHAUST_LIMIT)
struct prng_rand_t
{
uint64_t s[RAND_SSIZE]; // Lags
uint_fast16_t i; // Location of the current lag
uint_fast16_t c; // Exhaustion count
};
#define PRNG_RAND_MAX UINT64_MAX
static inline uint64_t prng_rand(struct prng_rand_t* state)
{
uint_fast16_t i;
uint_fast16_t r, new_rands = 0;
if (!state->c)
{ // Randomness exhausted, run forward to refill
new_rands += RAND_REFILL_COUNT + 1;
state->c = RAND_EXHAUST_LIMIT - 1;
}
else
{
new_rands = 1;
state->c--;
}
for (r = 0; r < new_rands; r++)
{
i = state->i;
state->s[i & RAND_SMASK] = state->s[(i + RAND_SSIZE - LAG1) & RAND_SMASK]
+ state->s[(i + RAND_SSIZE - LAG2) & RAND_SMASK];
state->i++;
}
return state->s[i & RAND_SMASK];
}
static inline void prng_srand(uint64_t seed, struct prng_rand_t* state)
{
uint_fast16_t i;
// Naive seed
state->c = RAND_EXHAUST_LIMIT;
state->i = 0;
state->s[0] = seed;
for (i = 1; i < RAND_SSIZE; i++)
{
// Arbitrary magic, mostly to eliminate the effect of low-value seeds.
// Probably could be better, but the run-up obviates any real need to.
state->s[i] = i * (UINT64_C(2147483647)) + seed;
}
// Run forward 10,000 numbers
for (i = 0; i < 10000; i++)
{
prng_rand(state);
}
}
// Clean up our macros
#undef LAG1
#undef LAG2
#undef RAND_SSIZE
#undef RAND_SMASK
#undef RAND_EXHAUST_LIMIT
#undef RAND_REFILL_COUNT
// PRNG_RAND_MAX is exported
#endif

65
3rdparty/ncnn/tests/test_absval.cpp vendored Normal file
View File

@ -0,0 +1,65 @@
// 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/absval.h"
#include "testutil.h"
static int test_absval(const ncnn::Mat& a)
{
ncnn::ParamDict pd;
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::AbsVal>("AbsVal", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_absval failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
}
return ret;
}
static int test_absval_0()
{
return 0
|| test_absval(RandomMat(5, 7, 24))
|| test_absval(RandomMat(7, 9, 12))
|| test_absval(RandomMat(3, 5, 13));
}
static int test_absval_1()
{
return 0
|| test_absval(RandomMat(15, 24))
|| test_absval(RandomMat(19, 12))
|| test_absval(RandomMat(17, 15));
}
static int test_absval_2()
{
return 0
|| test_absval(RandomMat(128))
|| test_absval(RandomMat(124))
|| test_absval(RandomMat(127));
}
int main()
{
SRAND(7767517);
return 0
|| test_absval_0()
|| test_absval_1()
|| test_absval_2();
}

100
3rdparty/ncnn/tests/test_batchnorm.cpp vendored Normal file
View File

@ -0,0 +1,100 @@
// 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/batchnorm.h"
#include "testutil.h"
static int test_batchnorm(const ncnn::Mat& a, float eps)
{
int channels;
if (a.dims == 1) channels = a.w;
if (a.dims == 2) channels = a.h;
if (a.dims == 3 || a.dims == 4) channels = a.c;
ncnn::ParamDict pd;
pd.set(0, channels); // channels
pd.set(1, eps); // eps
std::vector<ncnn::Mat> weights(4);
weights[0] = RandomMat(channels);
weights[1] = RandomMat(channels);
weights[2] = RandomMat(channels);
weights[3] = RandomMat(channels);
// var must be positive
Randomize(weights[2], 0.001f, 2.f);
int ret = test_layer<ncnn::BatchNorm>("BatchNorm", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_batchnorm failed a.dims=%d a=(%d %d %d %d) eps=%f\n", a.dims, a.w, a.h, a.d, a.c, eps);
}
return ret;
}
static int test_batchnorm_0()
{
return 0
|| test_batchnorm(RandomMat(5, 6, 7, 24), 0.f)
|| test_batchnorm(RandomMat(5, 6, 7, 24), 0.01f)
|| test_batchnorm(RandomMat(7, 8, 9, 12), 0.f)
|| test_batchnorm(RandomMat(7, 8, 9, 12), 0.001f)
|| test_batchnorm(RandomMat(3, 4, 5, 13), 0.f)
|| test_batchnorm(RandomMat(3, 4, 5, 13), 0.001f);
}
static int test_batchnorm_1()
{
return 0
|| test_batchnorm(RandomMat(5, 7, 24), 0.f)
|| test_batchnorm(RandomMat(5, 7, 24), 0.01f)
|| test_batchnorm(RandomMat(7, 9, 12), 0.f)
|| test_batchnorm(RandomMat(7, 9, 12), 0.001f)
|| test_batchnorm(RandomMat(3, 5, 13), 0.f)
|| test_batchnorm(RandomMat(3, 5, 13), 0.001f);
}
static int test_batchnorm_2()
{
return 0
|| test_batchnorm(RandomMat(15, 24), 0.f)
|| test_batchnorm(RandomMat(15, 24), 0.01f)
|| test_batchnorm(RandomMat(17, 12), 0.f)
|| test_batchnorm(RandomMat(17, 12), 0.001f)
|| test_batchnorm(RandomMat(19, 15), 0.f)
|| test_batchnorm(RandomMat(19, 15), 0.001f);
}
static int test_batchnorm_3()
{
return 0
|| test_batchnorm(RandomMat(128), 0.f)
|| test_batchnorm(RandomMat(128), 0.001f)
|| test_batchnorm(RandomMat(124), 0.f)
|| test_batchnorm(RandomMat(124), 0.1f)
|| test_batchnorm(RandomMat(127), 0.f)
|| test_batchnorm(RandomMat(127), 0.1f);
}
int main()
{
SRAND(7767517);
return 0
|| test_batchnorm_0()
|| test_batchnorm_1()
|| test_batchnorm_2()
|| test_batchnorm_3();
}

60
3rdparty/ncnn/tests/test_bias.cpp vendored Normal file
View File

@ -0,0 +1,60 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 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/bias.h"
#include "testutil.h"
static int test_bias(const ncnn::Mat& a)
{
int channels = a.c;
ncnn::ParamDict pd;
pd.set(0, channels);
std::vector<ncnn::Mat> weights(1);
weights[0] = RandomMat(channels);
int ret = test_layer<ncnn::Bias>("Bias", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_bias failed a.dims=%d a=(%d %d %d %d)\n", a.dims, a.w, a.h, a.d, a.c);
}
return ret;
}
static int test_bias_0()
{
return 0
|| test_bias(RandomMat(5, 6, 7, 24))
|| test_bias(RandomMat(7, 8, 9, 12))
|| test_bias(RandomMat(3, 4, 5, 13));
}
static int test_bias_1()
{
return 0
|| test_bias(RandomMat(5, 7, 24))
|| test_bias(RandomMat(7, 9, 12))
|| test_bias(RandomMat(3, 5, 13));
}
int main()
{
SRAND(7767517);
return 0
|| test_bias_0()
|| test_bias_1();
}

431
3rdparty/ncnn/tests/test_binaryop.cpp vendored Normal file
View File

@ -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<ncnn::Mat> weights(0);
std::vector<ncnn::Mat> ab(2);
ab[0] = a;
ab[1] = b;
int ret = test_layer<ncnn::BinaryOp>("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<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::BinaryOp>("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 = 0; 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;
}

337
3rdparty/ncnn/tests/test_c_api.cpp vendored Normal file
View File

@ -0,0 +1,337 @@
// 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 <string.h>
#include "c_api.h"
static int test_c_api_0()
{
ncnn_mat_t a = ncnn_mat_create_1d(2, NULL);
ncnn_mat_t b = ncnn_mat_create_1d(2, NULL);
ncnn_mat_t c = 0;
ncnn_option_t opt = ncnn_option_create();
// set a and b
{
ncnn_mat_fill_float(a, 2.f);
ncnn_mat_fill_float(b, 3.f);
}
// c = a + b
{
ncnn_layer_t op = ncnn_layer_create_by_type("BinaryOp");
// load param
{
ncnn_paramdict_t pd = ncnn_paramdict_create();
ncnn_paramdict_set_int(pd, 0, 0); // op_type = ADD
op->load_param(op, pd);
ncnn_paramdict_destroy(pd);
}
// load model
{
ncnn_modelbin_t mb = ncnn_modelbin_create_from_mat_array(0, 0);
op->load_model(op, mb);
ncnn_modelbin_destroy(mb);
}
op->create_pipeline(op, opt);
const ncnn_mat_t bottom_blobs[2] = {a, b};
ncnn_mat_t top_blobs[1] = {0};
op->forward_n(op, bottom_blobs, 2, top_blobs, 1, opt);
c = top_blobs[0];
op->destroy_pipeline(op, opt);
ncnn_layer_destroy(op);
}
// check c == a + b
bool success = false;
if (c)
{
int dims = ncnn_mat_get_dims(c);
int w = ncnn_mat_get_w(c);
const float* c_data = (const float*)ncnn_mat_get_data(c);
success = dims == 1 && w == 2 && c_data[0] == 5.f && c_data[1] == 5.f;
}
ncnn_option_destroy(opt);
ncnn_mat_destroy(a);
ncnn_mat_destroy(b);
ncnn_mat_destroy(c);
if (!success)
{
fprintf(stderr, "test_c_api_0 failed\n");
}
return success ? 0 : -1;
}
static int test_c_api_1()
{
ncnn_mat_t a = ncnn_mat_create_1d(24, NULL);
// set a
{
const float data[] = {
0, 1, 2, 3, 4, 5, 6, 7,
10, 11, 12, 13, 14, 15, 16, 17,
20, 21, 22, 23, 24, 25, 26, 27
};
float* a_data = (float*)ncnn_mat_get_data(a);
memcpy(a_data, data, 24 * sizeof(float));
}
ncnn_mat_t b = ncnn_mat_reshape_3d(a, 4, 2, 3, NULL);
ncnn_mat_t c = 0;
ncnn_option_t opt = ncnn_option_create();
// c = reorg(b, 2)
{
ncnn_layer_t op = ncnn_layer_create_by_type("Reorg");
// load param
{
ncnn_paramdict_t pd = ncnn_paramdict_create();
ncnn_paramdict_set_int(pd, 0, 2); // stride
op->load_param(op, pd);
ncnn_paramdict_destroy(pd);
}
// load model
{
ncnn_modelbin_t mb = ncnn_modelbin_create_from_mat_array(0, 0);
op->load_model(op, mb);
ncnn_modelbin_destroy(mb);
}
op->create_pipeline(op, opt);
op->forward_1(op, b, &c, opt);
op->destroy_pipeline(op, opt);
ncnn_layer_destroy(op);
}
// check c
bool success = false;
if (c)
{
int dims = ncnn_mat_get_dims(c);
int w = ncnn_mat_get_w(c);
int h = ncnn_mat_get_h(c);
int ch = ncnn_mat_get_c(c);
success = dims == 3 && w == 2 && h == 1 && ch == 12;
const float expected[] = {
0, 2,
1, 3,
4, 6,
5, 7,
10, 12,
11, 13,
14, 16,
15, 17,
20, 22,
21, 23,
24, 26,
25, 27
};
ncnn_mat_t c2 = 0;
ncnn_flatten(c, &c2, opt);
const float* c2_data = (const float*)ncnn_mat_get_data(c2);
if (memcmp(c2_data, expected, 24) != 0)
{
success = false;
}
ncnn_mat_destroy(c2);
}
ncnn_option_destroy(opt);
ncnn_mat_destroy(a);
ncnn_mat_destroy(b);
ncnn_mat_destroy(c);
if (!success)
{
fprintf(stderr, "test_c_api_1 failed\n");
}
return success ? 0 : -1;
}
static int mylayer_forward_inplace_1(const ncnn_layer_t layer, ncnn_mat_t bottom_top_blob, const ncnn_option_t opt)
{
int w = ncnn_mat_get_w(bottom_top_blob);
int h = ncnn_mat_get_h(bottom_top_blob);
int channels = ncnn_mat_get_c(bottom_top_blob);
int size = w * h;
#pragma omp parallel for num_threads(ncnn_option_get_num_threads(opt))
for (int q = 0; q < channels; q++)
{
float* ptr = (float*)ncnn_mat_get_channel_data(bottom_top_blob, q);
for (int i = 0; i < size; i++)
{
*ptr = *ptr + 100.f;
ptr++;
}
}
return 0;
}
static ncnn_layer_t mylayer_creator(void* /*userdata*/)
{
ncnn_layer_t layer = ncnn_layer_create();
ncnn_layer_set_one_blob_only(layer, 1);
ncnn_layer_set_support_inplace(layer, 1);
layer->forward_inplace_1 = mylayer_forward_inplace_1;
return layer;
}
static void mylayer_destroyer(ncnn_layer_t layer, void* /*userdata*/)
{
ncnn_layer_destroy(layer);
}
static size_t emptydr_read(ncnn_datareader_t /*dr*/, void* buf, size_t size)
{
memset(buf, 0, size);
return size;
}
static int test_c_api_2()
{
// datareader from empty
ncnn_datareader_t emptydr = ncnn_datareader_create();
{
emptydr->read = emptydr_read;
}
ncnn_option_t opt = ncnn_option_create();
{
ncnn_option_set_num_threads(opt, 1);
}
ncnn_net_t net = ncnn_net_create();
{
ncnn_net_set_option(net, opt);
ncnn_net_register_custom_layer_by_type(net, "MyLayer", mylayer_creator, mylayer_destroyer, 0);
const char param_txt[] = "7767517\n2 2\nInput input 0 1 data\nMyLayer mylayer 1 1 data output\n";
ncnn_net_load_param_memory(net, param_txt);
ncnn_net_load_model_datareader(net, emptydr);
}
ncnn_mat_t a = ncnn_mat_create_1d(24, NULL);
// set a
{
const float data[] = {
0, 1, 2, 3, 4, 5, 6, 7,
10, 11, 12, 13, 14, 15, 16, 17,
20, 21, 22, 23, 24, 25, 26, 27
};
float* a_data = (float*)ncnn_mat_get_data(a);
memcpy(a_data, data, 24 * sizeof(float));
}
ncnn_mat_t b = ncnn_mat_reshape_3d(a, 4, 2, 3, NULL);
ncnn_mat_t c = 0;
{
ncnn_extractor_t ex = ncnn_extractor_create(net);
ncnn_extractor_input(ex, "data", b);
ncnn_extractor_extract(ex, "output", &c);
ncnn_extractor_destroy(ex);
}
ncnn_net_destroy(net);
// check c
bool success = false;
if (c)
{
int dims = ncnn_mat_get_dims(c);
int w = ncnn_mat_get_w(c);
int h = ncnn_mat_get_h(c);
int ch = ncnn_mat_get_c(c);
success = dims == 3 && w == 4 && h == 2 && ch == 3;
const float expected[] = {
100, 101, 102, 103, 104, 105, 106, 107,
110, 111, 112, 113, 114, 115, 116, 117,
120, 121, 122, 123, 124, 125, 126, 127
};
ncnn_mat_t c2 = 0;
ncnn_flatten(c, &c2, opt);
const float* c2_data = (const float*)ncnn_mat_get_data(c2);
if (memcmp(c2_data, expected, 24) != 0)
{
success = false;
}
ncnn_mat_destroy(c2);
}
ncnn_mat_destroy(a);
ncnn_mat_destroy(b);
ncnn_mat_destroy(c);
ncnn_option_destroy(opt);
ncnn_datareader_destroy(emptydr);
if (!success)
{
fprintf(stderr, "test_c_api_2 failed\n");
}
return success ? 0 : -1;
}
int main()
{
return test_c_api_0() || test_c_api_1() || test_c_api_2();
}

691
3rdparty/ncnn/tests/test_cast.cpp vendored Normal file
View File

@ -0,0 +1,691 @@
// 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/cast.h"
#include "testutil.h"
static int test_cast_cpu(const ncnn::Mat& a, int type_from, int type_to)
{
ncnn::ParamDict pd;
pd.set(0, type_from);
pd.set(1, type_to);
std::vector<ncnn::Mat> weights(0);
ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = false;
opt.use_int8_inference = false;
opt.use_packing_layout = false;
ncnn::Layer* op = ncnn::create_layer("Cast");
op->load_param(pd);
ncnn::ModelBinFromMatArray mb(weights.data());
op->load_model(mb);
op->create_pipeline(opt);
ncnn::Mat a_fp16;
if (type_from == 2)
{
ncnn::cast_float32_to_float16(a, a_fp16, opt);
}
else if (type_from == 4)
{
ncnn::cast_float32_to_bfloat16(a, a_fp16, opt);
}
else
{
a_fp16 = a;
}
ncnn::Mat b;
((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
ncnn::Mat c;
op->forward(a_fp16, c, opt);
op->destroy_pipeline(opt);
delete op;
if (CompareMat(b, c, 0.001) != 0)
{
fprintf(stderr, "test_cast_cpu failed a.dims=%d a=(%d %d %d %d) type_from=%d type_to=%d\n", a.dims, a.w, a.h, a.d, a.c, type_from, type_to);
return -1;
}
return 0;
}
static int test_cast_cpu_packed(const ncnn::Mat& a, int type_from, int type_to)
{
ncnn::ParamDict pd;
pd.set(0, type_from);
pd.set(1, type_to);
std::vector<ncnn::Mat> weights(0);
ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = false;
opt.use_packing_layout = false;
ncnn::Layer* op = ncnn::create_layer("Cast");
op->load_param(pd);
ncnn::ModelBinFromMatArray mb(weights.data());
op->load_model(mb);
op->create_pipeline(opt);
ncnn::Mat a_fp16;
if (type_from == 2)
{
ncnn::cast_float32_to_float16(a, a_fp16, opt);
}
else if (type_from == 4)
{
ncnn::cast_float32_to_bfloat16(a, a_fp16, opt);
}
else
{
a_fp16 = a;
}
ncnn::Mat b;
((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
ncnn::Mat a4;
ncnn::convert_packing(a, a4, 4, opt);
ncnn::Mat a4_fp16;
if (type_from == 2)
{
ncnn::cast_float32_to_float16(a4, a4_fp16, opt);
}
else if (type_from == 4)
{
ncnn::cast_float32_to_bfloat16(a4, a4_fp16, opt);
}
else
{
a4_fp16 = a4;
}
ncnn::Mat c;
op->forward(a4_fp16, c, opt);
op->destroy_pipeline(opt);
delete op;
if (CompareMat(b, c, 0.001) != 0)
{
fprintf(stderr, "test_cast_cpu_packed failed a.dims=%d a=(%d %d %d %d) type_from=%d type_to=%d\n", a.dims, a.w, a.h, a.d, a.c, type_from, type_to);
return -1;
}
return 0;
}
#if NCNN_VULKAN
static int test_cast_gpu_fp16p(const ncnn::Mat& a, int type_from, int type_to)
{
if (type_to == 4 || type_from == 4)
return 0;
ncnn::ParamDict pd;
pd.set(0, type_from);
pd.set(1, type_to);
std::vector<ncnn::Mat> weights(0);
ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = true;
opt.use_int8_inference = false;
opt.use_fp16_packed = true;
opt.use_fp16_storage = false;
opt.use_fp16_arithmetic = false;
opt.use_int8_storage = false;
opt.use_int8_arithmetic = false;
opt.use_packing_layout = true;
opt.use_image_storage = false;
ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator();
ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator();
opt.blob_vkallocator = blob_vkallocator;
opt.workspace_vkallocator = blob_vkallocator;
opt.staging_vkallocator = staging_vkallocator;
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;
ncnn::Layer* op = ncnn::create_layer("Cast");
op->vkdev = vkdev;
op->load_param(pd);
ncnn::ModelBinFromMatArray mb(weights.data());
op->load_model(mb);
op->create_pipeline(opt);
ncnn::Mat a_fp16;
if (type_from == 2)
{
ncnn::cast_float32_to_float16(a, a_fp16, opt);
}
else
{
a_fp16 = a;
}
ncnn::Mat b;
((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
ncnn::Mat d;
// pack
ncnn::Mat a4;
ncnn::convert_packing(a, a4, 4, opt);
ncnn::Mat a4_fp16;
if (type_from == 2 && a4.elempack == 4)
{
ncnn::cast_float32_to_float16(a4, a4_fp16, opt);
}
else
{
a4_fp16 = a4;
}
// forward
ncnn::VkCompute cmd(vkdev);
// upload
ncnn::VkMat a4_gpu;
cmd.record_clone(a4_fp16, a4_gpu, opt);
ncnn::VkMat d4_gpu;
if (op->support_inplace)
{
op->forward_inplace(a4_gpu, cmd, opt);
d4_gpu = a4_gpu;
}
else
{
op->forward(a4_gpu, d4_gpu, cmd, opt);
}
// download
cmd.record_clone(d4_gpu, d, opt);
cmd.submit_and_wait();
op->destroy_pipeline(opt);
delete op;
vkdev->reclaim_blob_allocator(blob_vkallocator);
vkdev->reclaim_staging_allocator(staging_vkallocator);
if (CompareMat(b, d, 0.001) != 0)
{
fprintf(stderr, "test_cast_gpu_fp16p failed a.dims=%d a=(%d %d %d %d) type_from=%d type_to=%d\n", a.dims, a.w, a.h, a.d, a.c, type_from, type_to);
return -1;
}
return 0;
}
static int test_cast_gpu_fp16p_pack8(const ncnn::Mat& a, int type_from, int type_to)
{
if (type_to == 4 || type_from == 4)
return 0;
ncnn::ParamDict pd;
pd.set(0, type_from);
pd.set(1, type_to);
std::vector<ncnn::Mat> weights(0);
ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = true;
opt.use_int8_inference = false;
opt.use_fp16_packed = true;
opt.use_fp16_storage = false;
opt.use_fp16_arithmetic = false;
opt.use_int8_storage = false;
opt.use_int8_arithmetic = false;
opt.use_packing_layout = true;
opt.use_shader_pack8 = true;
opt.use_image_storage = false;
ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator();
ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator();
opt.blob_vkallocator = blob_vkallocator;
opt.workspace_vkallocator = blob_vkallocator;
opt.staging_vkallocator = staging_vkallocator;
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;
ncnn::Layer* op = ncnn::create_layer("Cast");
op->vkdev = vkdev;
op->load_param(pd);
ncnn::ModelBinFromMatArray mb(weights.data());
op->load_model(mb);
op->create_pipeline(opt);
ncnn::Mat a_fp16;
if (type_from == 2)
{
ncnn::cast_float32_to_float16(a, a_fp16, opt);
}
else
{
a_fp16 = a;
}
ncnn::Mat b;
((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
ncnn::Mat d;
// pack
ncnn::Mat a4;
ncnn::convert_packing(a, a4, 8, opt);
if (a4.elempack != 8)
ncnn::convert_packing(a, a4, 4, opt);
ncnn::Mat a4_fp16;
if (type_from == 2 && (a4.elempack == 4 || a4.elempack == 8))
{
ncnn::cast_float32_to_float16(a4, a4_fp16, opt);
}
else
{
a4_fp16 = a4;
}
// forward
ncnn::VkCompute cmd(vkdev);
// upload
ncnn::VkMat a4_gpu;
cmd.record_clone(a4_fp16, a4_gpu, opt);
ncnn::VkMat d4_gpu;
if (op->support_inplace)
{
op->forward_inplace(a4_gpu, cmd, opt);
d4_gpu = a4_gpu;
}
else
{
op->forward(a4_gpu, d4_gpu, cmd, opt);
}
// download
cmd.record_clone(d4_gpu, d, opt);
cmd.submit_and_wait();
op->destroy_pipeline(opt);
delete op;
vkdev->reclaim_blob_allocator(blob_vkallocator);
vkdev->reclaim_staging_allocator(staging_vkallocator);
if (CompareMat(b, d, 0.001) != 0)
{
fprintf(stderr, "test_cast_gpu_fp16p_pack8 failed a.dims=%d a=(%d %d %d %d) type_from=%d type_to=%d\n", a.dims, a.w, a.h, a.d, a.c, type_from, type_to);
return -1;
}
return 0;
}
static int test_cast_gpu_image_fp16p(const ncnn::Mat& a, int type_from, int type_to)
{
if (type_to == 4 || type_from == 4)
return 0;
ncnn::ParamDict pd;
pd.set(0, type_from);
pd.set(1, type_to);
std::vector<ncnn::Mat> weights(0);
ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = true;
opt.use_int8_inference = false;
opt.use_fp16_packed = true;
opt.use_fp16_storage = false;
opt.use_fp16_arithmetic = false;
opt.use_int8_storage = false;
opt.use_int8_arithmetic = false;
opt.use_packing_layout = true;
opt.use_image_storage = true;
ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator();
ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator();
opt.blob_vkallocator = blob_vkallocator;
opt.workspace_vkallocator = blob_vkallocator;
opt.staging_vkallocator = staging_vkallocator;
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;
ncnn::Layer* op = ncnn::create_layer("Cast");
op->vkdev = vkdev;
op->load_param(pd);
ncnn::ModelBinFromMatArray mb(weights.data());
op->load_model(mb);
op->create_pipeline(opt);
ncnn::Mat a_fp16;
if (type_from == 2)
{
ncnn::cast_float32_to_float16(a, a_fp16, opt);
}
else
{
a_fp16 = a;
}
ncnn::Mat b;
((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
ncnn::Mat d;
// pack
ncnn::Mat a4;
ncnn::convert_packing(a, a4, 4, opt);
ncnn::Mat a4_fp16;
if (type_from == 2 && a4.elempack == 4)
{
ncnn::cast_float32_to_float16(a4, a4_fp16, opt);
}
else
{
a4_fp16 = a4;
}
// forward
ncnn::VkCompute cmd(vkdev);
// upload
ncnn::VkImageMat a4_gpu;
cmd.record_clone(a4_fp16, a4_gpu, opt);
ncnn::VkImageMat d4_gpu;
if (op->support_inplace)
{
op->forward_inplace(a4_gpu, cmd, opt);
d4_gpu = a4_gpu;
}
else
{
op->forward(a4_gpu, d4_gpu, cmd, opt);
}
// download
cmd.record_clone(d4_gpu, d, opt);
cmd.submit_and_wait();
op->destroy_pipeline(opt);
delete op;
vkdev->reclaim_blob_allocator(blob_vkallocator);
vkdev->reclaim_staging_allocator(staging_vkallocator);
if (CompareMat(b, d, 0.001) != 0)
{
fprintf(stderr, "test_cast_gpu_image_fp16p failed a.dims=%d a=(%d %d %d %d) type_from=%d type_to=%d\n", a.dims, a.w, a.h, a.d, a.c, type_from, type_to);
return -1;
}
return 0;
}
static int test_cast_gpu_image_fp16p_pack8(const ncnn::Mat& a, int type_from, int type_to)
{
if (type_to == 4 || type_from == 4)
return 0;
ncnn::ParamDict pd;
pd.set(0, type_from);
pd.set(1, type_to);
std::vector<ncnn::Mat> weights(0);
ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = true;
opt.use_int8_inference = false;
opt.use_fp16_packed = true;
opt.use_fp16_storage = false;
opt.use_fp16_arithmetic = false;
opt.use_int8_storage = false;
opt.use_int8_arithmetic = false;
opt.use_packing_layout = true;
opt.use_shader_pack8 = true;
opt.use_image_storage = true;
ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator();
ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator();
opt.blob_vkallocator = blob_vkallocator;
opt.workspace_vkallocator = blob_vkallocator;
opt.staging_vkallocator = staging_vkallocator;
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;
ncnn::Layer* op = ncnn::create_layer("Cast");
op->vkdev = vkdev;
op->load_param(pd);
ncnn::ModelBinFromMatArray mb(weights.data());
op->load_model(mb);
op->create_pipeline(opt);
ncnn::Mat a_fp16;
if (type_from == 2)
{
ncnn::cast_float32_to_float16(a, a_fp16, opt);
}
else
{
a_fp16 = a;
}
ncnn::Mat b;
((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
ncnn::Mat d;
// pack
ncnn::Mat a4;
ncnn::convert_packing(a, a4, 8, opt);
if (a4.elempack != 8)
ncnn::convert_packing(a, a4, 4, opt);
ncnn::Mat a4_fp16;
if (type_from == 2 && (a4.elempack == 4 || a4.elempack == 8))
{
ncnn::cast_float32_to_float16(a4, a4_fp16, opt);
}
else
{
a4_fp16 = a4;
}
// forward
ncnn::VkCompute cmd(vkdev);
// upload
ncnn::VkImageMat a4_gpu;
cmd.record_clone(a4_fp16, a4_gpu, opt);
ncnn::VkImageMat d4_gpu;
if (op->support_inplace)
{
op->forward_inplace(a4_gpu, cmd, opt);
d4_gpu = a4_gpu;
}
else
{
op->forward(a4_gpu, d4_gpu, cmd, opt);
}
// download
cmd.record_clone(d4_gpu, d, opt);
cmd.submit_and_wait();
op->destroy_pipeline(opt);
delete op;
vkdev->reclaim_blob_allocator(blob_vkallocator);
vkdev->reclaim_staging_allocator(staging_vkallocator);
if (CompareMat(b, d, 0.001) != 0)
{
fprintf(stderr, "test_cast_gpu_image_fp16p_pack8 failed a.dims=%d a=(%d %d %d %d) type_from=%d type_to=%d\n", a.dims, a.w, a.h, a.d, a.c, type_from, type_to);
return -1;
}
return 0;
}
#endif // NCNN_VULKAN
static int test_cast(const ncnn::Mat& a, int type_from, int type_to)
{
return 0
|| test_cast_cpu(a, type_from, type_to)
|| test_cast_cpu_packed(a, type_from, type_to)
#if NCNN_VULKAN
|| test_cast_gpu_fp16p(a, type_from, type_to)
|| test_cast_gpu_fp16p_pack8(a, type_from, type_to)
|| test_cast_gpu_image_fp16p(a, type_from, type_to)
|| test_cast_gpu_image_fp16p_pack8(a, type_from, type_to)
#endif // NCNN_VULKAN
;
}
static int test_cast_0()
{
return 0
|| test_cast(RandomMat(5, 6, 7, 16), 1, 2)
|| test_cast(RandomMat(3, 4, 5, 13), 1, 2)
|| test_cast(RandomMat(5, 6, 7, 16), 2, 1)
|| test_cast(RandomMat(3, 4, 5, 13), 2, 1)
|| test_cast(RandomMat(5, 6, 7, 16), 1, 4)
|| test_cast(RandomMat(3, 4, 5, 13), 1, 4)
|| test_cast(RandomMat(5, 6, 7, 16), 4, 1)
|| test_cast(RandomMat(3, 4, 5, 13), 4, 1);
}
static int test_cast_1()
{
return 0
|| test_cast(RandomMat(5, 7, 16), 1, 2)
|| test_cast(RandomMat(3, 5, 13), 1, 2)
|| test_cast(RandomMat(5, 7, 16), 2, 1)
|| test_cast(RandomMat(3, 5, 13), 2, 1)
|| test_cast(RandomMat(5, 7, 16), 1, 4)
|| test_cast(RandomMat(3, 5, 13), 1, 4)
|| test_cast(RandomMat(5, 7, 16), 4, 1)
|| test_cast(RandomMat(3, 5, 13), 4, 1);
}
static int test_cast_2()
{
return 0
|| test_cast(RandomMat(6, 16), 1, 2)
|| test_cast(RandomMat(7, 15), 1, 2)
|| test_cast(RandomMat(6, 16), 2, 1)
|| test_cast(RandomMat(7, 15), 2, 1)
|| test_cast(RandomMat(6, 16), 1, 4)
|| test_cast(RandomMat(7, 15), 1, 4)
|| test_cast(RandomMat(6, 16), 4, 1)
|| test_cast(RandomMat(7, 15), 4, 1);
}
static int test_cast_3()
{
return 0
|| test_cast(RandomMat(128), 1, 2)
|| test_cast(RandomMat(127), 1, 2)
|| test_cast(RandomMat(128), 2, 1)
|| test_cast(RandomMat(127), 2, 1)
|| test_cast(RandomMat(128), 1, 4)
|| test_cast(RandomMat(127), 1, 4)
|| test_cast(RandomMat(128), 4, 1)
|| test_cast(RandomMat(127), 4, 1);
}
int main()
{
SRAND(7767517);
return 0
|| test_cast_0()
|| test_cast_1()
|| test_cast_2()
|| test_cast_3();
}

67
3rdparty/ncnn/tests/test_clip.cpp vendored Normal file
View File

@ -0,0 +1,67 @@
// 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/clip.h"
#include "testutil.h"
static int test_clip(const ncnn::Mat& a, float min, float max)
{
ncnn::ParamDict pd;
pd.set(0, min);
pd.set(1, max);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Clip>("Clip", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_clip failed a.dims=%d a=(%d,%d,%d) min=%f max=%f\n", a.dims, a.w, a.h, a.c, min, max);
}
return ret;
}
static int test_clip_0()
{
return 0
|| test_clip(RandomMat(5, 7, 24), -1.f, 1.f)
|| test_clip(RandomMat(7, 9, 12), -1.f, 1.f)
|| test_clip(RandomMat(3, 5, 13), -1.f, 1.f);
}
static int test_clip_1()
{
return 0
|| test_clip(RandomMat(15, 24), -1.f, 1.f)
|| test_clip(RandomMat(17, 12), -1.f, 1.f)
|| test_clip(RandomMat(19, 15), -1.f, 1.f);
}
static int test_clip_2()
{
return 0
|| test_clip(RandomMat(128), -1.f, 1.f)
|| test_clip(RandomMat(124), -1.f, 1.f)
|| test_clip(RandomMat(127), -1.f, 1.f);
}
int main()
{
SRAND(7767517);
return 0
|| test_clip_0()
|| test_clip_1()
|| test_clip_2();
}

231
3rdparty/ncnn/tests/test_command.cpp vendored Normal file
View File

@ -0,0 +1,231 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 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 "command.h"
#include "gpu.h"
#include "mat.h"
#include "testutil.h"
static int test_command_upload_download(const ncnn::Mat& a)
{
ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
ncnn::VkAllocator* blob_allocator = vkdev->acquire_blob_allocator();
ncnn::VkAllocator* staging_allocator = vkdev->acquire_staging_allocator();
ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = true;
opt.blob_vkallocator = blob_allocator;
opt.staging_vkallocator = staging_allocator;
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;
ncnn::Mat d;
ncnn::Mat e;
{
ncnn::VkCompute cmd(vkdev);
ncnn::VkMat b1;
ncnn::VkImageMat b2;
ncnn::VkImageMat c1;
ncnn::VkMat c2;
cmd.record_upload(a, b1, opt);
cmd.record_upload(a, c1, opt);
cmd.record_buffer_to_image(b1, b2, opt);
cmd.record_image_to_buffer(c1, c2, opt);
cmd.record_download(b2, d, opt);
cmd.record_download(c2, e, opt);
cmd.submit_and_wait();
}
vkdev->reclaim_blob_allocator(blob_allocator);
vkdev->reclaim_staging_allocator(staging_allocator);
if (CompareMat(a, d, 0.001) != 0)
{
fprintf(stderr, "test_command_upload_download buffer failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
return -1;
}
if (CompareMat(a, e, 0.001) != 0)
{
fprintf(stderr, "test_command_upload_download image failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
return -1;
}
return 0;
}
static int test_command_clone(const ncnn::Mat& a)
{
ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
ncnn::VkAllocator* blob_allocator = vkdev->acquire_blob_allocator();
ncnn::VkAllocator* staging_allocator = vkdev->acquire_staging_allocator();
ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = true;
opt.blob_vkallocator = blob_allocator;
opt.staging_vkallocator = staging_allocator;
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;
ncnn::Mat d;
ncnn::Mat e;
{
ncnn::VkCompute cmd(vkdev);
ncnn::VkMat b1;
ncnn::VkMat b2;
ncnn::VkImageMat b3;
ncnn::VkImageMat c1;
ncnn::VkImageMat c2;
ncnn::VkMat c3;
cmd.record_clone(a, b1, opt);
cmd.record_clone(a, c1, opt);
cmd.record_clone(b1, b2, opt);
cmd.record_clone(c1, c2, opt);
cmd.record_clone(b2, b3, opt);
cmd.record_clone(c2, c3, opt);
cmd.record_clone(b3, d, opt);
cmd.record_clone(c3, e, opt);
cmd.submit_and_wait();
}
vkdev->reclaim_blob_allocator(blob_allocator);
vkdev->reclaim_staging_allocator(staging_allocator);
if (CompareMat(a, d, 0.001) != 0)
{
fprintf(stderr, "test_command_clone buffer failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
return -1;
}
if (CompareMat(a, e, 0.001) != 0)
{
fprintf(stderr, "test_command_clone image failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
return -1;
}
return 0;
}
static int test_command_transfer(const ncnn::Mat& a)
{
ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
ncnn::VkAllocator* blob_allocator = vkdev->acquire_blob_allocator();
ncnn::VkAllocator* staging_allocator = vkdev->acquire_staging_allocator();
ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = true;
opt.blob_vkallocator = blob_allocator;
opt.staging_vkallocator = staging_allocator;
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;
ncnn::Mat d;
ncnn::Mat e;
{
ncnn::VkTransfer cmd1(vkdev);
ncnn::VkMat b1;
ncnn::VkImageMat c1;
cmd1.record_upload(a, b1, opt, false);
cmd1.record_upload(a, c1, opt);
cmd1.submit_and_wait();
ncnn::VkCompute cmd2(vkdev);
cmd2.record_download(b1, d, opt);
cmd2.record_download(c1, e, opt);
cmd2.submit_and_wait();
}
vkdev->reclaim_blob_allocator(blob_allocator);
vkdev->reclaim_staging_allocator(staging_allocator);
if (CompareMat(a, d, 0.001) != 0)
{
fprintf(stderr, "test_command_transfer buffer failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
return -1;
}
if (CompareMat(a, e, 0.001) != 0)
{
fprintf(stderr, "test_command_transfer image failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
return -1;
}
return 0;
}
static int test_command_0()
{
return 0
|| test_command_upload_download(RandomMat(5, 7, 24))
|| test_command_upload_download(RandomMat(7, 9, 12))
|| test_command_upload_download(RandomMat(3, 5, 13))
|| test_command_upload_download(RandomMat(15, 24))
|| test_command_upload_download(RandomMat(19, 12))
|| test_command_upload_download(RandomMat(17, 15))
|| test_command_upload_download(RandomMat(128))
|| test_command_upload_download(RandomMat(124))
|| test_command_upload_download(RandomMat(127));
}
static int test_command_1()
{
return 0
|| test_command_clone(RandomMat(5, 7, 24))
|| test_command_clone(RandomMat(7, 9, 12))
|| test_command_clone(RandomMat(3, 5, 13))
|| test_command_clone(RandomMat(15, 24))
|| test_command_clone(RandomMat(19, 12))
|| test_command_clone(RandomMat(17, 15))
|| test_command_clone(RandomMat(128))
|| test_command_clone(RandomMat(124))
|| test_command_clone(RandomMat(127));
}
static int test_command_2()
{
return 0
|| test_command_transfer(RandomMat(5, 7, 24))
|| test_command_transfer(RandomMat(7, 9, 12))
|| test_command_transfer(RandomMat(3, 5, 13))
|| test_command_transfer(RandomMat(15, 24))
|| test_command_transfer(RandomMat(19, 12))
|| test_command_transfer(RandomMat(17, 15))
|| test_command_transfer(RandomMat(128))
|| test_command_transfer(RandomMat(124))
|| test_command_transfer(RandomMat(127));
}
int main()
{
SRAND(7767517);
return test_command_0() || test_command_1() || test_command_2();
}

304
3rdparty/ncnn/tests/test_concat.cpp vendored Normal file
View File

@ -0,0 +1,304 @@
// 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/concat.h"
#include "testutil.h"
static int test_concat(const std::vector<ncnn::Mat>& a, int axis)
{
ncnn::ParamDict pd;
pd.set(0, axis); //axis
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Concat>("Concat", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_concat failed a[0].dims=%d a[0]=(%d %d %d) axis=%d\n", a[0].dims, a[0].w, a[0].h, a[0].c, axis);
}
return ret;
}
static int test_concat_0()
{
std::vector<ncnn::Mat> a(3);
a[0] = RandomMat(16, 12, 24);
a[1] = RandomMat(16, 12, 24);
a[2] = RandomMat(16, 12, 24);
std::vector<ncnn::Mat> b(3);
b[0] = RandomMat(16, 12, 64);
b[1] = RandomMat(16, 12, 64);
b[2] = RandomMat(16, 12, 64);
return 0
|| test_concat(a, 0)
|| test_concat(a, 1)
|| test_concat(a, 2)
|| test_concat(a, -1)
|| test_concat(a, -2)
|| test_concat(a, -3)
|| test_concat(b, 0)
|| test_concat(b, 1)
|| test_concat(b, 2)
|| test_concat(b, -1)
|| test_concat(b, -2)
|| test_concat(b, -3);
}
static int test_concat_1()
{
std::vector<ncnn::Mat> a(3);
a[0] = RandomMat(7, 3, 3);
a[1] = RandomMat(7, 3, 8);
a[2] = RandomMat(7, 3, 5);
std::vector<ncnn::Mat> b(3);
b[0] = RandomMat(9, 5, 8);
b[1] = RandomMat(9, 5, 4);
b[2] = RandomMat(9, 5, 12);
std::vector<ncnn::Mat> c(3);
c[0] = RandomMat(7, 3, 6);
c[1] = RandomMat(7, 3, 16);
c[2] = RandomMat(7, 3, 10);
std::vector<ncnn::Mat> d(3);
d[0] = RandomMat(9, 5, 16);
d[1] = RandomMat(9, 5, 8);
d[2] = RandomMat(9, 5, 24);
std::vector<ncnn::Mat> e(2);
e[0] = RandomMat(7, 3, 16);
e[1] = RandomMat(7, 3, 4);
return 0
|| test_concat(a, 0)
|| test_concat(a, -3)
|| test_concat(b, 0)
|| test_concat(b, -3)
|| test_concat(c, 0)
|| test_concat(c, -3)
|| test_concat(d, 0)
|| test_concat(d, -3)
|| test_concat(e, 0)
|| test_concat(e, -3);
}
static int test_concat_2()
{
std::vector<ncnn::Mat> a(3);
a[0] = RandomMat(7, 3, 5);
a[1] = RandomMat(7, 8, 5);
a[2] = RandomMat(7, 5, 5);
std::vector<ncnn::Mat> b(3);
b[0] = RandomMat(9, 8, 12);
b[1] = RandomMat(9, 3, 12);
b[2] = RandomMat(9, 5, 12);
std::vector<ncnn::Mat> c(3);
c[0] = RandomMat(7, 3, 10);
c[1] = RandomMat(7, 8, 10);
c[2] = RandomMat(7, 5, 10);
std::vector<ncnn::Mat> d(3);
d[0] = RandomMat(9, 8, 24);
d[1] = RandomMat(9, 3, 24);
d[2] = RandomMat(9, 5, 24);
return 0
|| test_concat(a, 1)
|| test_concat(a, -2)
|| test_concat(b, 1)
|| test_concat(b, -2)
|| test_concat(c, 1)
|| test_concat(c, -2)
|| test_concat(d, 1)
|| test_concat(d, -2);
}
static int test_concat_3()
{
std::vector<ncnn::Mat> a(3);
a[0] = RandomMat(8, 9, 3);
a[1] = RandomMat(3, 9, 3);
a[2] = RandomMat(5, 9, 3);
std::vector<ncnn::Mat> b(3);
b[0] = RandomMat(1, 7, 16);
b[1] = RandomMat(8, 7, 16);
b[2] = RandomMat(7, 7, 16);
std::vector<ncnn::Mat> c(3);
c[0] = RandomMat(8, 9, 6);
c[1] = RandomMat(3, 9, 6);
c[2] = RandomMat(5, 9, 6);
std::vector<ncnn::Mat> d(3);
d[0] = RandomMat(1, 7, 32);
d[1] = RandomMat(8, 7, 32);
d[2] = RandomMat(7, 7, 32);
return 0
|| test_concat(a, 2)
|| test_concat(a, -1)
|| test_concat(b, 2)
|| test_concat(b, -1)
|| test_concat(c, 2)
|| test_concat(c, -1)
|| test_concat(d, 2)
|| test_concat(d, -1);
}
static int test_concat_4()
{
std::vector<ncnn::Mat> a(3);
a[0] = RandomMat(11, 3);
a[1] = RandomMat(11, 8);
a[2] = RandomMat(11, 5);
std::vector<ncnn::Mat> b(3);
b[0] = RandomMat(15, 12);
b[1] = RandomMat(15, 8);
b[2] = RandomMat(15, 4);
std::vector<ncnn::Mat> c(3);
c[0] = RandomMat(11, 6);
c[1] = RandomMat(11, 16);
c[2] = RandomMat(11, 10);
std::vector<ncnn::Mat> d(3);
d[0] = RandomMat(15, 24);
d[1] = RandomMat(15, 16);
d[2] = RandomMat(15, 8);
std::vector<ncnn::Mat> e(2);
e[0] = RandomMat(11, 4);
e[1] = RandomMat(11, 32);
return 0
|| test_concat(a, 0)
|| test_concat(a, -2)
|| test_concat(b, 0)
|| test_concat(b, -2)
|| test_concat(c, 0)
|| test_concat(c, -2)
|| test_concat(d, 0)
|| test_concat(d, -2)
|| test_concat(e, 0)
|| test_concat(e, -2);
}
static int test_concat_5()
{
std::vector<ncnn::Mat> a(3);
a[0] = RandomMat(9, 7);
a[1] = RandomMat(8, 7);
a[2] = RandomMat(11, 7);
std::vector<ncnn::Mat> b(3);
b[0] = RandomMat(13, 24);
b[1] = RandomMat(18, 24);
b[2] = RandomMat(15, 24);
std::vector<ncnn::Mat> c(3);
c[0] = RandomMat(9, 14);
c[1] = RandomMat(8, 14);
c[2] = RandomMat(11, 14);
std::vector<ncnn::Mat> d(3);
d[0] = RandomMat(13, 48);
d[1] = RandomMat(18, 48);
d[2] = RandomMat(15, 48);
return 0
|| test_concat(a, 1)
|| test_concat(a, -1)
|| test_concat(b, 1)
|| test_concat(b, -1)
|| test_concat(c, 1)
|| test_concat(c, -1)
|| test_concat(d, 1)
|| test_concat(d, -1);
}
static int test_concat_6()
{
std::vector<ncnn::Mat> a(3);
a[0] = RandomMat(3);
a[1] = RandomMat(8);
a[2] = RandomMat(5);
std::vector<ncnn::Mat> b(3);
b[0] = RandomMat(4);
b[1] = RandomMat(8);
b[2] = RandomMat(12);
std::vector<ncnn::Mat> c(3);
c[0] = RandomMat(6);
c[1] = RandomMat(16);
c[2] = RandomMat(10);
std::vector<ncnn::Mat> d(3);
d[0] = RandomMat(8);
d[1] = RandomMat(16);
d[2] = RandomMat(24);
return 0
|| test_concat(a, 0)
|| test_concat(a, -1)
|| test_concat(b, 0)
|| test_concat(b, -1)
|| test_concat(c, 0)
|| test_concat(c, -1)
|| test_concat(d, 0)
|| test_concat(d, -1);
}
int main()
{
SRAND(7767517);
return 0
|| test_concat_0()
|| test_concat_1()
|| test_concat_2()
|| test_concat_3()
|| test_concat_4()
|| test_concat_5()
|| test_concat_6();
}

427
3rdparty/ncnn/tests/test_convolution.cpp vendored Normal file
View File

@ -0,0 +1,427 @@
// 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<ncnn::Mat> 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);
Randomize(weights[0], -1, 1);
epsilon = 0.002;
}
int ret = test_layer<ncnn::Convolution>("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 = 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(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
|| 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);
}
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<ncnn::Mat> weights(bias ? 2 : 1);
weights[0] = RandomMat(outch * w * kernel * kernel);
if (bias)
weights[1] = RandomMat(outch);
int ret = test_layer<ncnn::Convolution>("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<ncnn::Mat> as(bias ? 3 : 2);
as[0] = a;
as[1] = RandomMat(kernel, kernel, c, outch);
if (bias)
as[2] = RandomMat(outch);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Convolution>("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<ncnn::Mat> 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<ncnn::Convolution>("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);
}
#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
}

View File

@ -0,0 +1,210 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 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/convolution1d.h"
#include "testutil.h"
static int test_convolution1d(int w, int h, int outh, int kernel, int dilation, int stride, int pad, int bias)
{
ncnn::Mat a = RandomMat(w, h);
ncnn::ParamDict pd;
pd.set(0, outh); // 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, outh * h * kernel);
int activation_type = RAND() % 6; // 0 1 2 3 4 5
ncnn::Mat activation_params(2);
activation_params[0] = RandomFloat(-1, 0); // alpha
activation_params[1] = RandomFloat(0, 1); // beta
pd.set(9, activation_type);
pd.set(10, activation_params);
std::vector<ncnn::Mat> weights(bias ? 2 : 1);
weights[0] = RandomMat(outh * h * kernel);
if (bias)
weights[1] = RandomMat(outh);
int ret = test_layer<ncnn::Convolution1D>("Convolution1D", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_convolution1d failed w=%d h=%d outh=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, outh, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]);
}
return ret;
}
static int test_convolution1d_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 = 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_convolution1d(9, 1, 1, k, d, s, p, 1)
|| test_convolution1d(9, 4, 13, k, d, s, p, 0)
|| test_convolution1d(9, 13, 4, k, d, s, p, 1)
|| test_convolution1d(9, 12, 12, k, d, s, p, 0)
|| test_convolution1d(9, 8, 12, k, d, s, p, 1)
|| test_convolution1d(9, 8, 13, k, d, s, p, 0)
|| test_convolution1d(9, 13, 8, k, d, s, p, 1)
|| test_convolution1d(9, 12, 16, k, d, s, p, 0)
|| test_convolution1d(9, 15, 15, k, d, s, p, 0)
|| test_convolution1d(9, 16, 16, k, d, s, p, 0)
|| test_convolution1d(18, 1, 1, k, d, s, p, 1)
|| test_convolution1d(18, 4, 13, k, d, s, p, 0)
|| test_convolution1d(18, 13, 4, k, d, s, p, 1)
|| test_convolution1d(18, 12, 12, k, d, s, p, 0)
|| test_convolution1d(18, 8, 12, k, d, s, p, 1)
|| test_convolution1d(18, 8, 13, k, d, s, p, 0)
|| test_convolution1d(18, 13, 8, k, d, s, p, 1)
|| test_convolution1d(18, 12, 16, k, d, s, p, 0)
|| test_convolution1d(18, 15, 15, k, d, s, p, 0)
|| test_convolution1d(18, 16, 16, k, d, s, p, 0)
|| test_convolution1d(25, 1, 1, k, d, s, p, 1)
|| test_convolution1d(25, 4, 13, k, d, s, p, 0)
|| test_convolution1d(25, 13, 4, k, d, s, p, 1)
|| test_convolution1d(25, 12, 12, k, d, s, p, 0)
|| test_convolution1d(25, 8, 12, k, d, s, p, 1)
|| test_convolution1d(25, 8, 13, k, d, s, p, 0)
|| test_convolution1d(25, 13, 8, k, d, s, p, 1)
|| test_convolution1d(25, 12, 16, k, d, s, p, 0)
|| test_convolution1d(25, 15, 15, k, d, s, p, 0)
|| test_convolution1d(25, 16, 16, k, d, s, p, 0);
if (ret != 0)
return -1;
}
return 0
|| test_convolution1d(7, 1, 4, 3, 1, 1, 1, 1)
|| test_convolution1d(14, 1, 4, 3, 1, 2, 1, 1)
|| test_convolution1d(15, 4, 4, 3, 1, 1, 1, 1)
|| test_convolution1d(15, 8, 8, 3, 1, 1, 1, 1)
|| test_convolution1d(11, 8, 16, 3, 1, 1, 1, 1)
|| test_convolution1d(13, 16, 24, 3, 1, 1, 1, 1)
|| test_convolution1d(8, 16, 24, 3, 1, 1, 1, 0)
|| test_convolution1d(4, 16, 24, 3, 1, 1, 1, 1)
|| test_convolution1d(4, 16, 24, 3, 1, 1, 1, 0)
|| test_convolution1d(6, 64, 64, 3, 1, 2, 0, 1);
}
static int test_convolution1d_dynamic(int w, int h, int outh, int kernel, int dilation, int stride, int pad, int bias)
{
ncnn::Mat a = RandomMat(w, h);
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<ncnn::Mat> as(bias ? 3 : 2);
as[0] = a;
as[1] = RandomMat(kernel, h, outh);
if (bias)
as[2] = RandomMat(outh);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Convolution1D>("Convolution1D", pd, weights, as);
if (ret != 0)
{
fprintf(stderr, "test_convolution1d_dynamic failed w=%d h=%d outh=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, outh, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]);
}
return ret;
}
static int test_convolution1d_1()
{
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_convolution1d_dynamic(11, 1, 1, k, d, s, p, 1)
|| test_convolution1d_dynamic(11, 4, 13, k, d, s, p, 0)
|| test_convolution1d_dynamic(11, 13, 4, k, d, s, p, 1)
|| test_convolution1d_dynamic(11, 12, 12, k, d, s, p, 0)
|| test_convolution1d_dynamic(11, 8, 12, k, d, s, p, 1)
|| test_convolution1d_dynamic(11, 8, 13, k, d, s, p, 0)
|| test_convolution1d_dynamic(11, 13, 8, k, d, s, p, 1)
|| test_convolution1d_dynamic(11, 12, 16, k, d, s, p, 0)
|| test_convolution1d_dynamic(11, 15, 15, k, d, s, p, 0)
|| test_convolution1d_dynamic(11, 16, 16, k, d, s, p, 0);
if (ret != 0)
return -1;
}
return 0;
}
int main()
{
SRAND(7767517);
return test_convolution1d_0() || test_convolution1d_1();
}

View File

@ -0,0 +1,95 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 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/convolution3d.h"
#include "testutil.h"
static int test_convolution3d(int w, int h, int d, int c, int outch, int kernel, int dilation, int stride, int pad, int bias)
{
ncnn::Mat a = RandomMat(w, h, d, c);
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 * c * kernel * kernel * kernel);
int activation_type = RAND() % 6; // 0 1 2 3 4 5
ncnn::Mat activation_params(2);
activation_params[0] = RandomFloat(-1, 0); // alpha
activation_params[1] = RandomFloat(0, 1); // beta
pd.set(9, activation_type);
pd.set(10, activation_params);
std::vector<ncnn::Mat> weights(bias ? 2 : 1);
weights[0] = RandomMat(outch * c * kernel * kernel * kernel);
if (bias)
weights[1] = RandomMat(outch);
int ret = test_layer<ncnn::Convolution3D>("Convolution3D", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_convolution3d failed w=%d h=%d d=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, d, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]);
}
return ret;
}
static int test_convolution3d_0()
{
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_convolution3d(11, 10, 9, 1, 1, k, d, s, p, 1)
|| test_convolution3d(11, 10, 9, 4, 13, k, d, s, p, 0)
|| test_convolution3d(11, 10, 9, 13, 4, k, d, s, p, 1)
|| test_convolution3d(11, 10, 9, 12, 12, k, d, s, p, 0)
|| test_convolution3d(11, 10, 9, 8, 12, k, d, s, p, 1)
|| test_convolution3d(11, 10, 9, 8, 13, k, d, s, p, 0)
|| test_convolution3d(11, 10, 9, 13, 8, k, d, s, p, 1)
|| test_convolution3d(11, 10, 9, 12, 16, k, d, s, p, 0)
|| test_convolution3d(11, 10, 9, 15, 15, k, d, s, p, 0)
|| test_convolution3d(11, 10, 9, 16, 16, k, d, s, p, 0);
if (ret != 0)
return -1;
}
return 0;
}
int main()
{
SRAND(7767517);
return test_convolution3d_0();
}

View File

@ -0,0 +1,346 @@
// 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(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, 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);
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<ncnn::Mat> weights(2);
weights[0] = RandomMat(outch / group * c / group * kernel * kernel * group);
weights[1] = RandomMat(outch);
int ret = test_layer<ncnn::ConvolutionDepthWise>("ConvolutionDepthWise", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_convolutiondepthwise 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_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 = 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(15, 7, 1, 1, k, d, s, p, 1, 1)
|| test_convolutiondepthwise(15, 7, 2, 2, k, d, s, p, 0, 1)
|| test_convolutiondepthwise(15, 7, 2, 2, k, d, s, p, 1, 2)
|| test_convolutiondepthwise(15, 7, 3, 3, k, d, s, p, 0, 3)
|| test_convolutiondepthwise(15, 7, 4, 2, k, d, s, p, 1, 2)
|| test_convolutiondepthwise(15, 7, 4, 4, k, d, s, p, 0, 4)
|| test_convolutiondepthwise(15, 7, 7, 7, k, d, s, p, 1, 7)
|| test_convolutiondepthwise(15, 7, 8, 8, k, d, s, p, 0, 2)
|| test_convolutiondepthwise(15, 7, 8, 8, k, d, s, p, 1, 8)
|| test_convolutiondepthwise(15, 7, 12, 12, k, d, s, p, 0, 4)
|| test_convolutiondepthwise(15, 7, 15, 15, k, d, s, p, 1, 15)
|| test_convolutiondepthwise(15, 7, 16, 8, k, d, s, p, 0, 2)
|| test_convolutiondepthwise(15, 7, 16, 16, k, d, s, p, 1, 16)
|| test_convolutiondepthwise(18, 17, 1, 1, k, d, s, p, 1, 1)
|| test_convolutiondepthwise(18, 17, 2, 2, k, d, s, p, 0, 1)
|| test_convolutiondepthwise(18, 17, 2, 2, k, d, s, p, 1, 2)
|| test_convolutiondepthwise(18, 17, 3, 3, k, d, s, p, 0, 3)
|| test_convolutiondepthwise(18, 17, 4, 2, k, d, s, p, 1, 2)
|| test_convolutiondepthwise(18, 17, 4, 4, k, d, s, p, 0, 4)
|| test_convolutiondepthwise(18, 17, 7, 7, k, d, s, p, 1, 7)
|| test_convolutiondepthwise(18, 17, 8, 8, k, d, s, p, 0, 2)
|| test_convolutiondepthwise(18, 17, 8, 8, k, d, s, p, 1, 8)
|| test_convolutiondepthwise(18, 17, 12, 12, k, d, s, p, 0, 4)
|| test_convolutiondepthwise(18, 17, 15, 15, k, d, s, p, 1, 15)
|| test_convolutiondepthwise(18, 17, 16, 8, k, d, s, p, 0, 2)
|| test_convolutiondepthwise(18, 17, 16, 16, k, d, s, p, 1, 16)
|| test_convolutiondepthwise(25, 33, 1, 1, k, d, s, p, 1, 1)
|| test_convolutiondepthwise(25, 33, 2, 2, k, d, s, p, 0, 1)
|| test_convolutiondepthwise(25, 33, 2, 2, k, d, s, p, 1, 2)
|| test_convolutiondepthwise(25, 33, 3, 3, k, d, s, p, 0, 3)
|| test_convolutiondepthwise(25, 33, 4, 2, k, d, s, p, 1, 2)
|| test_convolutiondepthwise(25, 33, 4, 4, k, d, s, p, 0, 4)
|| test_convolutiondepthwise(25, 33, 7, 7, k, d, s, p, 1, 7)
|| test_convolutiondepthwise(25, 33, 8, 8, k, d, s, p, 0, 2)
|| test_convolutiondepthwise(25, 33, 8, 8, k, d, s, p, 1, 8)
|| test_convolutiondepthwise(25, 33, 12, 12, k, d, s, p, 0, 4)
|| test_convolutiondepthwise(25, 33, 15, 15, k, d, s, p, 1, 15)
|| test_convolutiondepthwise(25, 33, 16, 8, k, d, s, p, 0, 2)
|| test_convolutiondepthwise(25, 33, 16, 16, k, d, s, p, 1, 16);
if (ret != 0)
return -1;
}
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<ncnn::Mat> as(bias ? 3 : 2);
as[0] = a;
as[1] = RandomMat(kernel, kernel, c / group, outch);
if (bias)
as[2] = RandomMat(outch);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::ConvolutionDepthWise>("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<ncnn::Mat> 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<ncnn::ConvolutionDepthWise>("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
}

View File

@ -0,0 +1,213 @@
// 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/convolutiondepthwise1d.h"
#include "testutil.h"
static int test_convolutiondepthwise1d(int w, int h, int outh, int kernel, int dilation, int stride, int pad, int bias, int group)
{
ncnn::Mat a = RandomMat(w, h);
ncnn::ParamDict pd;
pd.set(0, outh); // 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, outh / group * h / group * kernel * kernel * group);
pd.set(7, group);
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<ncnn::Mat> weights(2);
weights[0] = RandomMat(outh / group * h / group * kernel * kernel * group);
weights[1] = RandomMat(outh);
int ret = test_layer<ncnn::ConvolutionDepthWise1D>("ConvolutionDepthWise1D", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_convolutiondepthwise1d failed w=%d h=%d outh=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d group=%d act=%d actparams=[%f,%f]\n", w, h, outh, kernel, dilation, stride, pad, bias, group, activation_type, activation_params[0], activation_params[1]);
}
return ret;
}
static int test_convolutiondepthwise1d_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 = 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_convolutiondepthwise1d(15, 1, 1, k, d, s, p, 1, 1)
|| test_convolutiondepthwise1d(15, 2, 2, k, d, s, p, 0, 1)
|| test_convolutiondepthwise1d(15, 2, 2, k, d, s, p, 1, 2)
|| test_convolutiondepthwise1d(15, 3, 3, k, d, s, p, 0, 3)
|| test_convolutiondepthwise1d(15, 4, 2, k, d, s, p, 1, 2)
|| test_convolutiondepthwise1d(15, 4, 4, k, d, s, p, 0, 4)
|| test_convolutiondepthwise1d(15, 7, 7, k, d, s, p, 1, 7)
|| test_convolutiondepthwise1d(15, 8, 8, k, d, s, p, 0, 2)
|| test_convolutiondepthwise1d(15, 8, 8, k, d, s, p, 1, 8)
|| test_convolutiondepthwise1d(15, 12, 12, k, d, s, p, 0, 4)
|| test_convolutiondepthwise1d(15, 15, 15, k, d, s, p, 1, 15)
|| test_convolutiondepthwise1d(15, 16, 8, k, d, s, p, 0, 2)
|| test_convolutiondepthwise1d(15, 16, 16, k, d, s, p, 1, 16)
|| test_convolutiondepthwise1d(18, 1, 1, k, d, s, p, 1, 1)
|| test_convolutiondepthwise1d(18, 2, 2, k, d, s, p, 0, 1)
|| test_convolutiondepthwise1d(18, 2, 2, k, d, s, p, 1, 2)
|| test_convolutiondepthwise1d(18, 3, 3, k, d, s, p, 0, 3)
|| test_convolutiondepthwise1d(18, 4, 2, k, d, s, p, 1, 2)
|| test_convolutiondepthwise1d(18, 4, 4, k, d, s, p, 0, 4)
|| test_convolutiondepthwise1d(18, 7, 7, k, d, s, p, 1, 7)
|| test_convolutiondepthwise1d(18, 8, 8, k, d, s, p, 0, 2)
|| test_convolutiondepthwise1d(18, 8, 8, k, d, s, p, 1, 8)
|| test_convolutiondepthwise1d(18, 12, 12, k, d, s, p, 0, 4)
|| test_convolutiondepthwise1d(18, 15, 15, k, d, s, p, 1, 15)
|| test_convolutiondepthwise1d(18, 16, 8, k, d, s, p, 0, 2)
|| test_convolutiondepthwise1d(18, 16, 16, k, d, s, p, 1, 16)
|| test_convolutiondepthwise1d(25, 1, 1, k, d, s, p, 1, 1)
|| test_convolutiondepthwise1d(25, 2, 2, k, d, s, p, 0, 1)
|| test_convolutiondepthwise1d(25, 2, 2, k, d, s, p, 1, 2)
|| test_convolutiondepthwise1d(25, 3, 3, k, d, s, p, 0, 3)
|| test_convolutiondepthwise1d(25, 4, 2, k, d, s, p, 1, 2)
|| test_convolutiondepthwise1d(25, 4, 4, k, d, s, p, 0, 4)
|| test_convolutiondepthwise1d(25, 7, 7, k, d, s, p, 1, 7)
|| test_convolutiondepthwise1d(25, 8, 8, k, d, s, p, 0, 2)
|| test_convolutiondepthwise1d(25, 8, 8, k, d, s, p, 1, 8)
|| test_convolutiondepthwise1d(25, 12, 12, k, d, s, p, 0, 4)
|| test_convolutiondepthwise1d(25, 15, 15, k, d, s, p, 1, 15)
|| test_convolutiondepthwise1d(25, 16, 8, k, d, s, p, 0, 2)
|| test_convolutiondepthwise1d(25, 16, 16, k, d, s, p, 1, 16);
if (ret != 0)
return -1;
}
return 0;
}
static int test_convolutiondepthwise1d_dynamic(int w, int h, int outh, int kernel, int dilation, int stride, int pad, int bias, int group)
{
ncnn::Mat a = RandomMat(w, h);
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<ncnn::Mat> as(bias ? 3 : 2);
as[0] = a;
as[1] = RandomMat(kernel, h / group, outh);
if (bias)
as[2] = RandomMat(outh);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::ConvolutionDepthWise1D>("ConvolutionDepthWise1D", pd, weights, as);
if (ret != 0)
{
fprintf(stderr, "test_convolutiondepthwise1d_dynamic failed w=%d h=%d outh=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d group=%d act=%d actparams=[%f,%f]\n", w, h, outh, kernel, dilation, stride, pad, bias, group, activation_type, activation_params[0], activation_params[1]);
}
return ret;
}
static int test_convolutiondepthwise1d_1()
{
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_convolutiondepthwise1d_dynamic(11, 1, 1, k, d, s, p, 1, 1)
|| test_convolutiondepthwise1d_dynamic(11, 2, 2, k, d, s, p, 0, 1)
|| test_convolutiondepthwise1d_dynamic(11, 2, 2, k, d, s, p, 1, 2)
|| test_convolutiondepthwise1d_dynamic(11, 3, 3, k, d, s, p, 0, 3)
|| test_convolutiondepthwise1d_dynamic(11, 4, 2, k, d, s, p, 1, 2)
|| test_convolutiondepthwise1d_dynamic(11, 4, 4, k, d, s, p, 0, 4)
|| test_convolutiondepthwise1d_dynamic(11, 7, 7, k, d, s, p, 1, 7)
|| test_convolutiondepthwise1d_dynamic(11, 8, 8, k, d, s, p, 0, 2)
|| test_convolutiondepthwise1d_dynamic(11, 8, 8, k, d, s, p, 1, 8)
|| test_convolutiondepthwise1d_dynamic(11, 12, 12, k, d, s, p, 0, 4)
|| test_convolutiondepthwise1d_dynamic(11, 15, 15, k, d, s, p, 1, 15)
|| test_convolutiondepthwise1d_dynamic(11, 16, 8, k, d, s, p, 0, 2)
|| test_convolutiondepthwise1d_dynamic(11, 16, 16, k, d, s, p, 1, 16);
if (ret != 0)
return -1;
}
return 0;
}
int main()
{
SRAND(7767517);
return test_convolutiondepthwise1d_0() || test_convolutiondepthwise1d_1();
}

View File

@ -0,0 +1,98 @@
// 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/convolutiondepthwise3d.h"
#include "testutil.h"
static int test_convolutiondepthwise3d(int w, int h, int d, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, int group)
{
ncnn::Mat a = RandomMat(w, h, d, c);
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 / group * c / group * kernel * kernel * kernel * group);
pd.set(7, group);
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<ncnn::Mat> weights(2);
weights[0] = RandomMat(outch / group * c / group * kernel * kernel * kernel * group);
weights[1] = RandomMat(outch);
int ret = test_layer<ncnn::ConvolutionDepthWise3D>("ConvolutionDepthWise3D", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_convolutiondepthwise3d failed w=%d h=%d d=%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, d, c, outch, kernel, dilation, stride, pad, bias, group, activation_type, activation_params[0], activation_params[1]);
}
return ret;
}
static int test_convolutiondepthwise3d_0()
{
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_convolutiondepthwise3d(11, 10, 9, 1, 1, k, d, s, p, 1, 1)
|| test_convolutiondepthwise3d(11, 10, 9, 2, 2, k, d, s, p, 0, 1)
|| test_convolutiondepthwise3d(11, 10, 9, 2, 2, k, d, s, p, 1, 2)
|| test_convolutiondepthwise3d(11, 10, 9, 3, 3, k, d, s, p, 0, 3)
|| test_convolutiondepthwise3d(11, 10, 9, 4, 2, k, d, s, p, 1, 2)
|| test_convolutiondepthwise3d(11, 10, 9, 4, 4, k, d, s, p, 0, 4)
|| test_convolutiondepthwise3d(11, 10, 9, 7, 7, k, d, s, p, 1, 7)
|| test_convolutiondepthwise3d(11, 10, 9, 8, 8, k, d, s, p, 0, 2)
|| test_convolutiondepthwise3d(11, 10, 9, 8, 8, k, d, s, p, 1, 8)
|| test_convolutiondepthwise3d(11, 10, 9, 12, 12, k, d, s, p, 0, 4)
|| test_convolutiondepthwise3d(11, 10, 9, 15, 15, k, d, s, p, 1, 15)
|| test_convolutiondepthwise3d(11, 10, 9, 16, 8, k, d, s, p, 0, 2)
|| test_convolutiondepthwise3d(11, 10, 9, 16, 16, k, d, s, p, 1, 16);
if (ret != 0)
return -1;
}
return 0;
}
int main()
{
SRAND(7767517);
return test_convolutiondepthwise3d_0();
}

130
3rdparty/ncnn/tests/test_cpu.cpp vendored Normal file
View File

@ -0,0 +1,130 @@
#include <stdio.h>
#include "cpu.h"
#if defined __ANDROID__ || defined __linux__ || defined __APPLE__
static int test_cpu_set()
{
ncnn::CpuSet set;
if (set.num_enabled() != 0)
{
fprintf(stderr, "By default all cpus should be disabled\n");
return 1;
}
set.enable(0);
if (!set.is_enabled(0))
{
fprintf(stderr, "CpuSet enable doesn't work\n");
return 1;
}
if (set.num_enabled() != 1)
{
fprintf(stderr, "Only one cpu should be enabled\n");
return 1;
}
set.disable(0);
if (set.is_enabled(0))
{
fprintf(stderr, "CpuSet disable doesn't work\n");
return 1;
}
return 0;
}
#else
static int test_cpu_set()
{
return 0;
}
#endif
#if defined __ANDROID__ || defined __linux__
static int test_cpu_info()
{
if (ncnn::get_cpu_count() >= 0 && ncnn::get_little_cpu_count() >= 0 && ncnn::get_big_cpu_count() >= 0)
{
return 0;
}
else
{
fprintf(stderr, "The system cannot have a negative number of processors\n");
return 1;
}
}
static int test_cpu_omp()
{
if (ncnn::get_omp_num_threads() >= 0 && ncnn::get_omp_thread_num() >= 0 && ncnn::get_omp_dynamic() >= 0)
{
return 0;
}
else
{
fprintf(stderr, "The OMP cannot have a negative number of processors\n");
return 1;
}
ncnn::set_omp_num_threads(1);
ncnn::set_omp_dynamic(1);
}
static int test_cpu_powersave()
{
if (ncnn::get_cpu_powersave() >= 0)
{
return 0;
}
else
{
fprintf(stderr, "By default powersave must be zero\n");
return 1;
}
if (ncnn::set_cpu_powersave(-1) == -1 && ncnn::set_cpu_powersave(3) == -1)
{
return 0;
}
else
{
fprintf(stderr, "Set cpu powersave for `-1 < argument < 2` works incorrectly.\n");
return 1;
}
}
#else
static int test_cpu_info()
{
return 0;
}
static int test_cpu_omp()
{
return 0;
}
static int test_cpu_powersave()
{
return 0;
}
#endif
int main()
{
return 0
|| test_cpu_set()
|| test_cpu_info()
|| test_cpu_omp()
|| test_cpu_powersave();
}

733
3rdparty/ncnn/tests/test_crop.cpp vendored Normal file
View File

@ -0,0 +1,733 @@
// 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, int outw, int outh, int outd, int outc, int woffset2, int hoffset2, int doffset2, int coffset2)
{
ncnn::ParamDict pd;
pd.set(0, woffset); // woffset
pd.set(1, hoffset); // hoffset
pd.set(13, doffset); // doffset
pd.set(2, coffset); // coffset
pd.set(3, outw); // outw
pd.set(4, outh); // outh
pd.set(14, outd); // outd
pd.set(5, outc); // outc
pd.set(6, woffset2); // woffset2
pd.set(7, hoffset2); // hoffset2
pd.set(15, doffset2); // doffset2
pd.set(8, coffset2); // coffset2
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Crop>("Crop", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_crop failed a.dims=%d a=(%d %d %d %d) woffset=%d hoffset=%d doffset=%d coffset=%d outw=%d outh=%d outd=%d outc=%d woffset2=%d hoffset2=%d doffset2=%d coffset2=%d\n", a.dims, a.w, a.h, a.d, a.c, woffset, hoffset, doffset, coffset, outw, outh, outd, outc, woffset2, hoffset2, doffset2, coffset2);
}
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<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Crop>("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<ncnn::Mat> weights(0);
std::vector<ncnn::Mat> ab(2);
ab[0] = a;
ab[1] = ref;
int ret = test_layer<ncnn::Crop>("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
|| test_crop(a, 0, 0, 0, 0, -233, 0, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 11, 0, 0, 0, -233, 0, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 11, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 12, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 16, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 11, 0, 0, 0, -233, 0, 0, 0, 12, 0, 0, 0)
|| test_crop(a, 12, 0, 0, 0, -233, 0, 0, 0, 16, 0, 0, 0)
|| 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
|| test_crop(a, 0, 0, 0, 0, -233, -233, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 5, 0, 0, 0, -233, -233, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 0, 11, 0, 0, -233, -233, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 0, 12, 0, 0, -233, -233, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 6, 12, 0, 0, -233, -233, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 4, 8, 0, 0, -233, -233, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 5, 0, 0, 0, 5, -233, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 6, 0, 0, 0, 6, -233, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 4, 0, 0, 0, 4, -233, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 0, 11, 0, 0, -233, 12, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 0, 12, 0, 0, -233, 16, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 0, 8, 0, 0, -233, 7, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 5, 11, 0, 0, 4, 16, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 6, 12, 0, 0, 5, 7, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 4, 8, 0, 0, 6, 12, 0, 0, 0, 0, 0, 0)
|| test_crop(a, 5, 0, 0, 0, -233, -233, 0, 0, 5, 0, 0, 0)
|| test_crop(a, 6, 0, 0, 0, -233, -233, 0, 0, 6, 0, 0, 0)
|| test_crop(a, 4, 0, 0, 0, -233, -233, 0, 0, 4, 0, 0, 0)
|| test_crop(a, 0, 11, 0, 0, -233, -233, 0, 0, 0, 12, 0, 0)
|| test_crop(a, 0, 12, 0, 0, -233, -233, 0, 0, 0, 16, 0, 0)
|| test_crop(a, 0, 8, 0, 0, -233, -233, 0, 0, 0, 7, 0, 0)
|| test_crop(a, 5, 11, 0, 0, -233, -233, 0, 0, 4, 16, 0, 0)
|| test_crop(a, 6, 12, 0, 0, -233, -233, 0, 0, 5, 7, 0, 0)
|| 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
|| test_crop(a, 0, 0, 0, 0, -233, -233, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 6, 0, 0, 0, -233, -233, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 5, 0, 0, -233, -233, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 4, 0, 0, -233, -233, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 0, 0, 12, -233, -233, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 5, 5, 0, 0, -233, -233, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 4, 4, 0, 0, -233, -233, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 6, 0, 12, -233, -233, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 5, 0, 0, 11, -233, -233, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 4, 0, 0, 8, -233, -233, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 6, 6, 0, 12, -233, -233, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 5, 0, 0, 0, 6, -233, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 6, 0, 0, 0, 4, -233, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 4, 0, 0, 0, 5, -233, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 5, 0, 0, -233, 4, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 6, 0, 0, -233, 5, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 4, 0, 0, -233, 6, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 0, 0, 11, -233, -233, 0, 7, 0, 0, 0, 0)
|| test_crop(a, 0, 0, 0, 12, -233, -233, 0, 12, 0, 0, 0, 0)
|| test_crop(a, 0, 0, 0, 8, -233, -233, 0, 16, 0, 0, 0, 0)
|| test_crop(a, 5, 5, 0, 0, 4, 4, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 6, 6, 0, 0, 6, 6, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 4, 4, 0, 0, 5, 5, 0, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 5, 0, 11, -233, 6, 0, 12, 0, 0, 0, 0)
|| test_crop(a, 0, 6, 0, 12, -233, 4, 0, 16, 0, 0, 0, 0)
|| test_crop(a, 0, 4, 0, 8, -233, 5, 0, 7, 0, 0, 0, 0)
|| test_crop(a, 5, 0, 0, 11, 4, -233, 0, 16, 0, 0, 0, 0)
|| test_crop(a, 6, 0, 0, 12, 5, -233, 0, 7, 0, 0, 0, 0)
|| test_crop(a, 4, 0, 0, 8, 4, -233, 0, 12, 0, 0, 0, 0)
|| test_crop(a, 5, 3, 0, 11, 6, 5, 0, 12, 0, 0, 0, 0)
|| test_crop(a, 6, 4, 0, 12, 4, 4, 0, 16, 0, 0, 0, 0)
|| test_crop(a, 4, 5, 0, 8, 5, 3, 0, 7, 0, 0, 0, 0)
|| test_crop(a, 5, 0, 0, 0, -233, -233, 0, -233, 4, 0, 0, 0)
|| test_crop(a, 6, 0, 0, 0, -233, -233, 0, -233, 5, 0, 0, 0)
|| test_crop(a, 4, 0, 0, 0, -233, -233, 0, -233, 6, 0, 0, 0)
|| test_crop(a, 0, 5, 0, 0, -233, -233, 0, -233, 0, 5, 0, 0)
|| test_crop(a, 0, 6, 0, 0, -233, -233, 0, -233, 0, 6, 0, 0)
|| test_crop(a, 0, 4, 0, 0, -233, -233, 0, -233, 0, 4, 0, 0)
|| test_crop(a, 0, 0, 0, 11, -233, -233, 0, -233, 0, 0, 0, 12)
|| test_crop(a, 0, 0, 0, 12, -233, -233, 0, -233, 0, 0, 0, 16)
|| test_crop(a, 0, 0, 0, 8, -233, -233, 0, -233, 0, 0, 0, 7)
|| test_crop(a, 5, 4, 0, 0, -233, -233, 0, -233, 4, 2, 0, 0)
|| test_crop(a, 6, 3, 0, 0, -233, -233, 0, -233, 5, 3, 0, 0)
|| test_crop(a, 4, 2, 0, 0, -233, -233, 0, -233, 6, 4, 0, 0)
|| test_crop(a, 0, 5, 0, 11, -233, -233, 0, -233, 0, 5, 0, 7)
|| test_crop(a, 0, 6, 0, 12, -233, -233, 0, -233, 0, 6, 0, 12)
|| test_crop(a, 0, 4, 0, 8, -233, -233, 0, -233, 0, 4, 0, 16)
|| test_crop(a, 5, 0, 0, 11, -233, -233, 0, -233, 6, 0, 0, 12)
|| test_crop(a, 6, 0, 0, 12, -233, -233, 0, -233, 4, 0, 0, 16)
|| test_crop(a, 4, 0, 0, 8, -233, -233, 0, -233, 5, 0, 0, 7)
|| test_crop(a, 5, 2, 0, 11, -233, -233, 0, -233, 4, 3, 0, 16)
|| test_crop(a, 6, 3, 0, 12, -233, -233, 0, -233, 5, 4, 0, 7)
|| 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
|| test_crop(a, 0, 0, 0, 0, -233, -233, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 6, 0, 0, 0, -233, -233, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 5, 0, 0, -233, -233, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 4, 0, 0, -233, -233, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 0, 5, 0, -233, -233, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 0, 4, 0, -233, -233, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 0, 0, 12, -233, -233, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 5, 5, 5, 0, -233, -233, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 4, 4, 4, 0, -233, -233, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 6, 6, 12, -233, -233, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 5, 0, 5, 11, -233, -233, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 4, 0, 4, 8, -233, -233, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 6, 6, 6, 12, -233, -233, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 5, 0, 0, 0, 6, -233, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 6, 0, 0, 0, 5, -233, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 4, 0, 0, 0, 4, -233, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 5, 0, 0, -233, 4, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 6, 0, 0, -233, 5, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 4, 0, 0, -233, 6, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 0, 5, 0, -233, -233, 6, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 0, 6, 0, -233, -233, 4, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 0, 4, 0, -233, -233, 5, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 0, 0, 11, -233, -233, -233, 7, 0, 0, 0, 0)
|| test_crop(a, 0, 0, 0, 12, -233, -233, -233, 12, 0, 0, 0, 0)
|| test_crop(a, 0, 0, 0, 8, -233, -233, -233, 16, 0, 0, 0, 0)
|| test_crop(a, 5, 2, 0, 0, 6, 5, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 6, 3, 0, 0, 5, 6, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 4, 4, 0, 0, 4, 4, -233, -233, 0, 0, 0, 0)
|| test_crop(a, 5, 0, 3, 0, 2, -233, 5, -233, 0, 0, 0, 0)
|| test_crop(a, 6, 0, 2, 0, 3, -233, 3, -233, 0, 0, 0, 0)
|| test_crop(a, 4, 0, 4, 0, 4, -233, 4, -233, 0, 0, 0, 0)
|| test_crop(a, 0, 5, 0, 11, -233, 1, -233, 12, 0, 0, 0, 0)
|| test_crop(a, 0, 6, 0, 12, -233, 2, -233, 16, 0, 0, 0, 0)
|| test_crop(a, 0, 4, 0, 8, -233, 3, -233, 7, 0, 0, 0, 0)
|| test_crop(a, 0, 0, 4, 11, -233, -233, 3, 16, 0, 0, 0, 0)
|| test_crop(a, 0, 0, 5, 12, -233, -233, 2, 7, 0, 0, 0, 0)
|| test_crop(a, 0, 0, 6, 8, -233, -233, 1, 16, 0, 0, 0, 0)
|| test_crop(a, 5, 0, 5, 11, 1, -233, 2, 16, 0, 0, 0, 0)
|| test_crop(a, 6, 0, 6, 12, 2, -233, 3, 7, 0, 0, 0, 0)
|| test_crop(a, 4, 0, 4, 8, 3, -233, 1, 16, 0, 0, 0, 0)
|| test_crop(a, 4, 6, 3, 11, 2, 3, 4, 12, 0, 0, 0, 0)
|| test_crop(a, 5, 5, 4, 12, 3, 4, 5, 16, 0, 0, 0, 0)
|| test_crop(a, 6, 4, 2, 8, 4, 5, 6, 7, 0, 0, 0, 0)
|| test_crop(a, 5, 0, 0, 0, -233, -233, -233, -233, 2, 0, 0, 0)
|| test_crop(a, 6, 0, 0, 0, -233, -233, -233, -233, 3, 0, 0, 0)
|| test_crop(a, 4, 0, 0, 0, -233, -233, -233, -233, 4, 0, 0, 0)
|| test_crop(a, 0, 4, 0, 0, -233, -233, -233, -233, 0, 4, 0, 0)
|| test_crop(a, 0, 3, 0, 0, -233, -233, -233, -233, 0, 3, 0, 0)
|| test_crop(a, 0, 2, 0, 0, -233, -233, -233, -233, 0, 2, 0, 0)
|| test_crop(a, 0, 0, 4, 0, -233, -233, -233, -233, 0, 0, 4, 0)
|| test_crop(a, 0, 0, 5, 0, -233, -233, -233, -233, 0, 0, 2, 0)
|| test_crop(a, 0, 0, 6, 0, -233, -233, -233, -233, 0, 0, 3, 0)
|| test_crop(a, 0, 0, 0, 11, -233, -233, -233, -233, 0, 0, 0, 12)
|| test_crop(a, 0, 0, 0, 12, -233, -233, -233, -233, 0, 0, 0, 16)
|| test_crop(a, 0, 0, 0, 8, -233, -233, -233, -233, 0, 0, 0, 7)
|| test_crop(a, 5, 3, 0, 0, -233, -233, -233, -233, 5, 2, 0, 0)
|| test_crop(a, 6, 4, 0, 0, -233, -233, -233, -233, 3, 3, 0, 0)
|| test_crop(a, 4, 4, 0, 0, -233, -233, -233, -233, 2, 5, 0, 0)
|| test_crop(a, 0, 4, 0, 11, -233, -233, -233, -233, 0, 3, 0, 7)
|| test_crop(a, 0, 3, 0, 12, -233, -233, -233, -233, 0, 4, 0, 12)
|| test_crop(a, 0, 2, 0, 8, -233, -233, -233, -233, 0, 5, 0, 16)
|| test_crop(a, 0, 4, 4, 0, -233, -233, -233, -233, 0, 4, 1, 0)
|| test_crop(a, 0, 5, 5, 0, -233, -233, -233, -233, 0, 2, 2, 0)
|| test_crop(a, 0, 2, 6, 0, -233, -233, -233, -233, 0, 1, 3, 0)
|| test_crop(a, 3, 0, 0, 11, -233, -233, -233, -233, 3, 0, 0, 12)
|| test_crop(a, 4, 0, 0, 12, -233, -233, -233, -233, 4, 0, 0, 16)
|| test_crop(a, 5, 0, 0, 8, -233, -233, -233, -233, 2, 0, 0, 7)
|| test_crop(a, 0, 4, 4, 11, -233, -233, -233, -233, 0, 4, 4, 12)
|| test_crop(a, 0, 5, 5, 12, -233, -233, -233, -233, 0, 4, 4, 16)
|| test_crop(a, 0, 6, 6, 8, -233, -233, -233, -233, 0, 3, 3, 7)
|| test_crop(a, 1, 1, 1, 11, -233, -233, -233, -233, 1, 1, 1, 16)
|| test_crop(a, 2, 2, 2, 12, -233, -233, -233, -233, 2, 2, 2, 7)
|| 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);
return 0
|| 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));
}

View File

@ -0,0 +1,117 @@
// 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/deconvolution.h"
#include "testutil.h"
static int test_deconvolution(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, int output_pad_right, int output_pad_bottom, int output_w, int output_h)
{
ncnn::Mat a = RandomMat(w, h, c);
if (output_w > 0 && output_h > 0 && pad != -233 && pad != -234)
{
pad = -233;
}
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 * c * kernel * kernel);
int activation_type = RAND() % 5; // 0 1 2 3 4
ncnn::Mat activation_params(2);
activation_params[0] = RandomFloat(-1, 0); // alpha
activation_params[1] = RandomFloat(0, 1); // beta
pd.set(9, activation_type);
pd.set(10, activation_params);
pd.set(18, output_pad_right);
pd.set(19, output_pad_bottom);
pd.set(20, output_w);
pd.set(21, output_h);
std::vector<ncnn::Mat> weights(2);
weights[0] = RandomMat(outch * c * kernel * kernel);
weights[1] = RandomMat(outch);
int ret = test_layer<ncnn::Deconvolution>("Deconvolution", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_deconvolution failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f] output_pad_right=%d output_pad_bottom=%d output_w=%d output_h=%d\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1], output_pad_right, output_pad_bottom, output_w, output_h);
}
return ret;
}
static int test_deconvolution_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, -233},
{4, 1, 2, -234},
{4, 2, 1, -234},
{5, 1, 1, 2},
{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_deconvolution(9, 7, 1, 1, k, d, s, p, 1, 0, 0, 0, 0)
|| test_deconvolution(9, 7, 4, 13, k, d, s, p, 0, 1, 1, 7, 5)
|| test_deconvolution(9, 7, 13, 4, k, d, s, p, 1, 1, 0, 0, 0)
|| test_deconvolution(9, 7, 4, 8, k, d, s, p, 0, 0, 1, 0, 0)
|| test_deconvolution(9, 7, 8, 4, k, d, s, p, 1, 0, 0, 7, 5)
|| test_deconvolution(9, 7, 8, 13, k, d, s, p, 0, 2, 2, 0, 0)
|| test_deconvolution(9, 7, 13, 8, k, d, s, p, 1, 2, 0, 0, 0)
|| test_deconvolution(9, 7, 16, 16, k, d, s, p, 0, 0, 2, 7, 5);
if (ret != 0)
return -1;
}
return 0
|| test_deconvolution(7, 5, 24, 32, 4, 2, 2, 2, 1, 0, 0, 0, 0)
|| test_deconvolution(7, 5, 32, 24, 4, 2, 2, 2, 1, 0, 0, 0, 0)
|| test_deconvolution(7, 5, 28, 32, 4, 2, 2, 2, 1, 0, 0, 0, 0)
|| test_deconvolution(7, 5, 32, 28, 4, 2, 2, 2, 1, 0, 0, 0, 0)
|| test_deconvolution(7, 5, 26, 32, 4, 2, 2, 2, 1, 0, 0, 0, 0)
|| test_deconvolution(7, 5, 32, 26, 4, 2, 2, 2, 1, 0, 0, 0, 0);
}
int main()
{
SRAND(7767517);
return test_deconvolution_0();
}

View File

@ -0,0 +1,109 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2022 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/deconvolution1d.h"
#include "testutil.h"
static int test_deconvolution1d(int w, int h, int outh, int kernel, int dilation, int stride, int pad, int bias, int output_pad_right, int output_w)
{
ncnn::Mat a = RandomMat(w, h);
if (output_w > 0 && pad != -233 && pad != -234)
{
pad = -233;
}
ncnn::ParamDict pd;
pd.set(0, outh);
pd.set(1, kernel);
pd.set(2, dilation);
pd.set(3, stride);
pd.set(4, pad);
pd.set(5, bias);
pd.set(6, outh * h * kernel);
int activation_type = RAND() % 5; // 0 1 2 3 4
ncnn::Mat activation_params(2);
activation_params[0] = RandomFloat(-1, 0); // alpha
activation_params[1] = RandomFloat(0, 1); // beta
pd.set(9, activation_type);
pd.set(10, activation_params);
pd.set(18, output_pad_right);
pd.set(20, output_w);
std::vector<ncnn::Mat> weights(2);
weights[0] = RandomMat(outh * h * kernel);
weights[1] = RandomMat(outh);
int ret = test_layer<ncnn::Deconvolution1D>("Deconvolution1D", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_deconvolution1d failed w=%d h=%d outh=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f] output_pad_right=%d output_w=%d\n", w, h, outh, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1], output_pad_right, output_w);
}
return ret;
}
static int test_deconvolution1d_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, -233},
{4, 1, 2, -234},
{4, 2, 1, -234},
{5, 1, 1, 2},
{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_deconvolution1d(9, 1, 1, k, d, s, p, 1, 0, 0)
|| test_deconvolution1d(9, 4, 13, k, d, s, p, 0, 1, 7)
|| test_deconvolution1d(9, 13, 4, k, d, s, p, 1, 1, 0)
|| test_deconvolution1d(9, 4, 8, k, d, s, p, 0, 0, 0)
|| test_deconvolution1d(9, 8, 4, k, d, s, p, 1, 0, 7)
|| test_deconvolution1d(9, 8, 13, k, d, s, p, 0, 2, 0)
|| test_deconvolution1d(9, 13, 8, k, d, s, p, 1, 2, 0)
|| test_deconvolution1d(9, 16, 16, k, d, s, p, 0, 0, 7);
if (ret != 0)
return -1;
}
return 0;
}
int main()
{
SRAND(7767517);
return test_deconvolution1d_0();
}

View File

@ -0,0 +1,104 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2022 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/deconvolution3d.h"
#include "testutil.h"
static int test_deconvolution3d(int w, int h, int d, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, int output_pad_right, int output_pad_bottom, int output_pad_behind, int output_w, int output_h, int output_d)
{
ncnn::Mat a = RandomMat(w, h, d, c);
if (output_w > 0 && output_h > 0 && output_d > 0 && pad != -233 && pad != -234)
{
pad = -233;
}
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 * kernel);
int activation_type = RAND() % 5; // 0 1 2 3 4
ncnn::Mat activation_params(2);
activation_params[0] = RandomFloat(-1, 0); // alpha
activation_params[1] = RandomFloat(0, 1); // beta
pd.set(9, activation_type);
pd.set(10, activation_params);
pd.set(18, output_pad_right);
pd.set(19, output_pad_bottom);
pd.set(20, output_pad_behind);
pd.set(25, output_w);
pd.set(26, output_h);
pd.set(27, output_d);
std::vector<ncnn::Mat> weights(2);
weights[0] = RandomMat(outch * c * kernel * kernel * kernel);
weights[1] = RandomMat(outch);
int ret = test_layer<ncnn::Deconvolution3D>("Deconvolution3D", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_deconvolution3d failed w=%d h=%d d=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f] output_pad_right=%d output_pad_bottom=%d output_pad_behind=%d output_w=%d output_h=%d output_d=%d\n", w, h, d, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1], output_pad_right, output_pad_bottom, output_pad_behind, output_w, output_h, output_d);
}
return ret;
}
static int test_deconvolution3d_0()
{
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_deconvolution3d(9, 8, 7, 1, 1, k, d, s, p, 1, 0, 0, 0, 0, 0, 0)
|| test_deconvolution3d(9, 8, 7, 4, 13, k, d, s, p, 0, 1, 1, 1, 7, 6, 5)
|| test_deconvolution3d(9, 8, 7, 13, 4, k, d, s, p, 1, 1, 0, 0, 0, 0, 0)
|| test_deconvolution3d(9, 8, 7, 4, 8, k, d, s, p, 0, 0, 1, 0, 0, 0, 0)
|| test_deconvolution3d(9, 8, 7, 8, 4, k, d, s, p, 1, 0, 0, 0, 7, 6, 5)
|| test_deconvolution3d(9, 8, 7, 8, 13, k, d, s, p, 0, 2, 2, 2, 0, 0, 0)
|| test_deconvolution3d(9, 8, 7, 13, 8, k, d, s, p, 1, 2, 0, 2, 0, 0, 0)
|| test_deconvolution3d(9, 8, 7, 16, 16, k, d, s, p, 0, 0, 2, 0, 7, 6, 5);
if (ret != 0)
return -1;
}
return 0;
}
int main()
{
SRAND(7767517);
return test_deconvolution3d_0();
}

View File

@ -0,0 +1,117 @@
// 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/deconvolutiondepthwise.h"
#include "testutil.h"
static int test_deconvolutiondepthwise(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, int group, int output_pad_right, int output_pad_bottom, int output_w, int output_h)
{
ncnn::Mat a = RandomMat(w, h, c);
if (output_w > 0 && output_h > 0 && pad != -233 && pad != -234)
{
pad = -233;
}
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 / group * c / group * kernel * kernel * group);
pd.set(7, group);
int activation_type = RAND() % 5; // 0 1 2 3 4
ncnn::Mat activation_params(2);
activation_params[0] = RandomFloat(-1, 0); // alpha
activation_params[1] = RandomFloat(0, 1); // beta
pd.set(9, activation_type);
pd.set(10, activation_params);
pd.set(18, output_pad_right);
pd.set(19, output_pad_bottom);
pd.set(20, output_w);
pd.set(21, output_h);
std::vector<ncnn::Mat> weights(2);
weights[0] = RandomMat(outch / group * c / group * kernel * kernel * group);
weights[1] = RandomMat(outch);
int ret = test_layer<ncnn::DeconvolutionDepthWise>("DeconvolutionDepthWise", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_deconvolutiondepthwise 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] output_pad_right=%d output_pad_bottom=%d output_w=%d output_h=%d\n", w, h, c, outch, kernel, dilation, stride, pad, bias, group, activation_type, activation_params[0], activation_params[1], output_pad_right, output_pad_bottom, output_w, output_h);
}
return ret;
}
static int test_deconvolutiondepthwise_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, -233},
{4, 1, 2, -234},
{4, 2, 1, -234},
{5, 1, 1, 2},
{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_deconvolutiondepthwise(15, 7, 1, 1, k, d, s, p, 1, 1, 0, 0, 0, 0)
|| test_deconvolutiondepthwise(15, 7, 2, 2, k, d, s, p, 0, 1, 1, 1, 7, 5)
|| test_deconvolutiondepthwise(15, 7, 2, 2, k, d, s, p, 1, 2, 1, 0, 0, 0)
|| test_deconvolutiondepthwise(15, 7, 3, 3, k, d, s, p, 0, 3, 0, 1, 0, 0)
|| test_deconvolutiondepthwise(15, 7, 4, 2, k, d, s, p, 1, 2, 0, 0, 7, 5)
|| test_deconvolutiondepthwise(15, 7, 4, 4, k, d, s, p, 0, 4, 2, 2, 0, 0)
|| test_deconvolutiondepthwise(15, 7, 7, 7, k, d, s, p, 1, 7, 2, 0, 0, 0)
|| test_deconvolutiondepthwise(15, 7, 8, 8, k, d, s, p, 0, 2, 0, 2, 7, 5)
|| test_deconvolutiondepthwise(15, 7, 8, 8, k, d, s, p, 1, 8, 0, 0, 0, 0)
|| test_deconvolutiondepthwise(15, 7, 12, 12, k, d, s, p, 0, 4, 3, 3, 0, 0)
|| test_deconvolutiondepthwise(15, 7, 15, 15, k, d, s, p, 1, 15, 3, 0, 7, 5)
|| test_deconvolutiondepthwise(15, 7, 16, 8, k, d, s, p, 0, 2, 0, 3, 0, 0)
|| test_deconvolutiondepthwise(15, 7, 16, 16, k, d, s, p, 1, 16, 0, 0, 0, 0);
if (ret != 0)
return -1;
}
return 0;
}
int main()
{
SRAND(7767517);
return test_deconvolutiondepthwise_0();
}

View File

@ -0,0 +1,115 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2022 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/deconvolutiondepthwise1d.h"
#include "testutil.h"
static int test_deconvolutiondepthwise1d(int w, int h, int outh, int kernel, int dilation, int stride, int pad, int bias, int group, int output_pad_right, int output_w)
{
ncnn::Mat a = RandomMat(w, h);
if (output_w > 0 && pad != -233 && pad != -234)
{
pad = -233;
}
ncnn::ParamDict pd;
pd.set(0, outh);
pd.set(1, kernel);
pd.set(2, dilation);
pd.set(3, stride);
pd.set(4, pad);
pd.set(5, bias);
pd.set(6, outh / group * h / group * kernel * group);
pd.set(7, group);
int activation_type = RAND() % 5; // 0 1 2 3 4
ncnn::Mat activation_params(2);
activation_params[0] = RandomFloat(-1, 0); // alpha
activation_params[1] = RandomFloat(0, 1); // beta
pd.set(9, activation_type);
pd.set(10, activation_params);
pd.set(18, output_pad_right);
pd.set(20, output_w);
std::vector<ncnn::Mat> weights(2);
weights[0] = RandomMat(outh / group * h / group * kernel * group);
weights[1] = RandomMat(outh);
int ret = test_layer<ncnn::DeconvolutionDepthWise1D>("DeconvolutionDepthWise1D", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_deconvolutiondepthwise1d failed w=%d h=%d outh=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d group=%d act=%d actparams=[%f,%f] output_pad_right=%d output_w=%d\n", w, h, outh, kernel, dilation, stride, pad, bias, group, activation_type, activation_params[0], activation_params[1], output_pad_right, output_w);
}
return ret;
}
static int test_deconvolutiondepthwise1d_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, -233},
{4, 1, 2, -234},
{4, 2, 1, -234},
{5, 1, 1, 2},
{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_deconvolutiondepthwise1d(15, 1, 1, k, d, s, p, 1, 1, 0, 0)
|| test_deconvolutiondepthwise1d(15, 2, 2, k, d, s, p, 0, 1, 1, 7)
|| test_deconvolutiondepthwise1d(15, 2, 2, k, d, s, p, 1, 2, 1, 0)
|| test_deconvolutiondepthwise1d(15, 3, 3, k, d, s, p, 0, 3, 0, 0)
|| test_deconvolutiondepthwise1d(15, 4, 2, k, d, s, p, 1, 2, 0, 7)
|| test_deconvolutiondepthwise1d(15, 4, 4, k, d, s, p, 0, 4, 2, 0)
|| test_deconvolutiondepthwise1d(15, 7, 7, k, d, s, p, 1, 7, 2, 0)
|| test_deconvolutiondepthwise1d(15, 8, 8, k, d, s, p, 0, 2, 0, 7)
|| test_deconvolutiondepthwise1d(15, 8, 8, k, d, s, p, 1, 8, 0, 0)
|| test_deconvolutiondepthwise1d(15, 12, 12, k, d, s, p, 0, 4, 3, 0)
|| test_deconvolutiondepthwise1d(15, 15, 15, k, d, s, p, 1, 15, 3, 7)
|| test_deconvolutiondepthwise1d(15, 16, 8, k, d, s, p, 0, 2, 0, 0)
|| test_deconvolutiondepthwise1d(15, 16, 16, k, d, s, p, 1, 16, 0, 0);
if (ret != 0)
return -1;
}
return 0;
}
int main()
{
SRAND(7767517);
return test_deconvolutiondepthwise1d_0();
}

View File

@ -0,0 +1,110 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2022 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/deconvolutiondepthwise3d.h"
#include "testutil.h"
static int test_deconvolutiondepthwise3d(int w, int h, int d, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, int group, int output_pad_right, int output_pad_bottom, int output_pad_behind, int output_w, int output_h, int output_d)
{
ncnn::Mat a = RandomMat(w, h, d, c);
if (output_w > 0 && output_h > 0 && output_d > 0 && pad != -233 && pad != -234)
{
pad = -233;
}
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 * kernel * group);
pd.set(7, group);
int activation_type = RAND() % 5; // 0 1 2 3 4
ncnn::Mat activation_params(2);
activation_params[0] = RandomFloat(-1, 0); // alpha
activation_params[1] = RandomFloat(0, 1); // beta
pd.set(9, activation_type);
pd.set(10, activation_params);
pd.set(18, output_pad_right);
pd.set(19, output_pad_bottom);
pd.set(20, output_pad_behind);
pd.set(25, output_w);
pd.set(26, output_h);
pd.set(27, output_d);
std::vector<ncnn::Mat> weights(2);
weights[0] = RandomMat(outch / group * c / group * kernel * kernel * kernel * group);
weights[1] = RandomMat(outch);
int ret = test_layer<ncnn::DeconvolutionDepthWise3D>("DeconvolutionDepthWise3D", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_deconvolutiondepthwise3d failed w=%d h=%d d=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d group=%d act=%d actparams=[%f,%f] output_pad_right=%d output_pad_bottom=%d output_pad_behind=%d output_w=%d output_h=%d output_d=%d\n", w, h, c, outch, kernel, dilation, stride, pad, bias, group, activation_type, activation_params[0], activation_params[1], output_pad_right, output_pad_bottom, output_pad_behind, output_w, output_h, output_d);
}
return ret;
}
static int test_deconvolutiondepthwise3d_0()
{
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_deconvolutiondepthwise3d(15, 11, 7, 1, 1, k, d, s, p, 1, 1, 0, 0, 0, 0, 0, 0)
|| test_deconvolutiondepthwise3d(15, 11, 7, 2, 2, k, d, s, p, 0, 1, 1, 1, 1, 7, 6, 5)
|| test_deconvolutiondepthwise3d(15, 11, 7, 2, 2, k, d, s, p, 1, 2, 1, 0, 0, 0, 0, 0)
|| test_deconvolutiondepthwise3d(15, 11, 7, 3, 3, k, d, s, p, 0, 3, 0, 1, 0, 0, 0, 0)
|| test_deconvolutiondepthwise3d(15, 11, 7, 4, 2, k, d, s, p, 1, 2, 0, 0, 0, 7, 6, 5)
|| test_deconvolutiondepthwise3d(15, 11, 7, 4, 4, k, d, s, p, 0, 4, 2, 2, 2, 0, 0, 0)
|| test_deconvolutiondepthwise3d(15, 11, 7, 7, 7, k, d, s, p, 1, 7, 2, 0, 2, 0, 0, 0)
|| test_deconvolutiondepthwise3d(15, 11, 7, 8, 8, k, d, s, p, 0, 2, 0, 2, 0, 7, 6, 5)
|| test_deconvolutiondepthwise3d(15, 11, 7, 8, 8, k, d, s, p, 1, 8, 0, 0, 0, 0, 0, 0)
|| test_deconvolutiondepthwise3d(15, 11, 7, 12, 12, k, d, s, p, 0, 4, 3, 3, 3, 0, 0, 0)
|| test_deconvolutiondepthwise3d(15, 11, 7, 15, 15, k, d, s, p, 1, 15, 3, 0, 0, 7, 6, 5)
|| test_deconvolutiondepthwise3d(15, 11, 7, 16, 8, k, d, s, p, 0, 2, 0, 3, 3, 0, 0, 0)
|| test_deconvolutiondepthwise3d(15, 11, 7, 16, 16, k, d, s, p, 1, 16, 0, 0, 0, 0, 0, 0);
if (ret != 0)
return -1;
}
return 0;
}
int main()
{
SRAND(7767517);
return test_deconvolutiondepthwise3d_0();
}

65
3rdparty/ncnn/tests/test_deepcopy.cpp vendored Normal file
View File

@ -0,0 +1,65 @@
// 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/deepcopy.h"
#include "testutil.h"
static int test_deepcopy(const ncnn::Mat& a)
{
ncnn::ParamDict pd;
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::DeepCopy>("DeepCopy", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_deepcopy failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
}
return ret;
}
static int test_deepcopy_0()
{
return 0
|| test_deepcopy(RandomMat(5, 7, 24))
|| test_deepcopy(RandomMat(7, 9, 12))
|| test_deepcopy(RandomMat(3, 5, 13));
}
static int test_deepcopy_1()
{
return 0
|| test_deepcopy(RandomMat(15, 24))
|| test_deepcopy(RandomMat(17, 12))
|| test_deepcopy(RandomMat(19, 15));
}
static int test_deepcopy_2()
{
return 0
|| test_deepcopy(RandomMat(128))
|| test_deepcopy(RandomMat(124))
|| test_deepcopy(RandomMat(127));
}
int main()
{
SRAND(7767517);
return 0
|| test_deepcopy_0()
|| test_deepcopy_1()
|| test_deepcopy_2();
}

161
3rdparty/ncnn/tests/test_dequantize.cpp vendored Normal file
View File

@ -0,0 +1,161 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 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/dequantize.h"
#include "testutil.h"
static int test_dequantize(const ncnn::Mat& a, int scale_data_size, int bias_data_size)
{
ncnn::ParamDict pd;
pd.set(0, scale_data_size);
pd.set(1, bias_data_size);
std::vector<ncnn::Mat> weights(bias_data_size ? 2 : 1);
weights[0] = RandomMat(scale_data_size);
if (bias_data_size)
weights[1] = RandomMat(bias_data_size);
int flag = TEST_LAYER_DISABLE_AUTO_INPUT_CASTING;
int ret = test_layer<ncnn::Dequantize>("Dequantize", pd, weights, a, 0.001, 0, flag);
if (ret != 0)
{
fprintf(stderr, "test_dequantize failed a.dims=%d a=(%d %d %d) scale_data_size=%d bias_data_size=%d\n", a.dims, a.w, a.h, a.c, scale_data_size, bias_data_size);
}
return ret;
}
static int test_dequantize_pack8(const ncnn::Mat& a, int scale_data_size, int bias_data_size)
{
ncnn::ParamDict pd;
pd.set(0, scale_data_size);
pd.set(1, bias_data_size);
std::vector<ncnn::Mat> weights(bias_data_size ? 2 : 1);
weights[0] = RandomMat(scale_data_size);
if (bias_data_size)
weights[1] = RandomMat(bias_data_size);
int flag = TEST_LAYER_DISABLE_AUTO_INPUT_CASTING | TEST_LAYER_ENABLE_FORCE_INPUT_PACK8;
int ret = test_layer<ncnn::Dequantize>("Dequantize", pd, weights, a, 0.001, 0, flag);
if (ret != 0)
{
fprintf(stderr, "test_dequantize_pack8 failed a.dims=%d a=(%d %d %d) scale_data_size=%d bias_data_size=%d\n", a.dims, a.w, a.h, a.c, scale_data_size, bias_data_size);
}
return ret;
}
static int test_dequantize_0()
{
return 0
|| test_dequantize(RandomIntMat(5, 7, 24), 1, 24)
|| test_dequantize(RandomIntMat(5, 7, 24), 1, 1)
|| test_dequantize(RandomIntMat(5, 7, 24), 1, 0)
|| test_dequantize(RandomIntMat(5, 7, 24), 24, 24)
|| test_dequantize(RandomIntMat(5, 7, 24), 24, 1)
|| test_dequantize(RandomIntMat(5, 7, 24), 24, 0)
|| test_dequantize(RandomIntMat(7, 9, 12), 1, 12)
|| test_dequantize(RandomIntMat(7, 9, 12), 1, 1)
|| test_dequantize(RandomIntMat(7, 9, 12), 1, 0)
|| test_dequantize(RandomIntMat(7, 9, 12), 12, 12)
|| test_dequantize(RandomIntMat(7, 9, 12), 12, 1)
|| test_dequantize(RandomIntMat(7, 9, 12), 12, 0)
|| test_dequantize(RandomIntMat(3, 5, 13), 1, 13)
|| test_dequantize(RandomIntMat(3, 5, 13), 1, 1)
|| test_dequantize(RandomIntMat(3, 5, 13), 1, 0)
|| test_dequantize(RandomIntMat(3, 5, 13), 13, 13)
|| test_dequantize(RandomIntMat(3, 5, 13), 13, 1)
|| test_dequantize(RandomIntMat(3, 5, 13), 13, 0);
}
static int test_dequantize_1()
{
return 0
|| test_dequantize(RandomIntMat(15, 24), 1, 24)
|| test_dequantize(RandomIntMat(15, 24), 1, 1)
|| test_dequantize(RandomIntMat(15, 24), 1, 0)
|| test_dequantize(RandomIntMat(15, 24), 24, 24)
|| test_dequantize(RandomIntMat(15, 24), 24, 1)
|| test_dequantize(RandomIntMat(15, 24), 24, 0)
|| test_dequantize(RandomIntMat(17, 12), 1, 12)
|| test_dequantize(RandomIntMat(17, 12), 1, 1)
|| test_dequantize(RandomIntMat(17, 12), 1, 0)
|| test_dequantize(RandomIntMat(17, 12), 12, 12)
|| test_dequantize(RandomIntMat(17, 12), 12, 1)
|| test_dequantize(RandomIntMat(17, 12), 12, 0)
|| test_dequantize(RandomIntMat(19, 15), 1, 15)
|| test_dequantize(RandomIntMat(19, 15), 1, 1)
|| test_dequantize(RandomIntMat(19, 15), 1, 0)
|| test_dequantize(RandomIntMat(19, 15), 15, 15)
|| test_dequantize(RandomIntMat(19, 15), 15, 1)
|| test_dequantize(RandomIntMat(19, 15), 15, 0);
}
static int test_dequantize_2()
{
return 0
|| test_dequantize(RandomIntMat(128), 1, 128)
|| test_dequantize(RandomIntMat(128), 1, 1)
|| test_dequantize(RandomIntMat(128), 1, 0)
|| test_dequantize(RandomIntMat(128), 128, 128)
|| test_dequantize(RandomIntMat(128), 128, 1)
|| test_dequantize(RandomIntMat(128), 128, 0)
|| test_dequantize(RandomIntMat(124), 1, 124)
|| test_dequantize(RandomIntMat(124), 1, 1)
|| test_dequantize(RandomIntMat(124), 1, 0)
|| test_dequantize(RandomIntMat(124), 124, 124)
|| test_dequantize(RandomIntMat(124), 124, 1)
|| test_dequantize(RandomIntMat(124), 124, 0)
|| test_dequantize(RandomIntMat(127), 1, 127)
|| test_dequantize(RandomIntMat(127), 1, 1)
|| test_dequantize(RandomIntMat(127), 1, 0)
|| test_dequantize(RandomIntMat(127), 127, 127)
|| test_dequantize(RandomIntMat(127), 127, 1)
|| test_dequantize(RandomIntMat(127), 127, 0);
}
static int test_dequantize_3()
{
return 0
|| test_dequantize_pack8(RandomIntMat(5, 7, 24), 1, 24)
|| test_dequantize_pack8(RandomIntMat(5, 7, 24), 1, 1)
|| test_dequantize_pack8(RandomIntMat(5, 7, 24), 1, 0)
|| test_dequantize_pack8(RandomIntMat(5, 7, 24), 24, 24)
|| test_dequantize_pack8(RandomIntMat(5, 7, 24), 24, 1)
|| test_dequantize_pack8(RandomIntMat(5, 7, 24), 24, 0)
|| test_dequantize_pack8(RandomIntMat(15, 24), 1, 24)
|| test_dequantize_pack8(RandomIntMat(15, 24), 1, 1)
|| test_dequantize_pack8(RandomIntMat(15, 24), 1, 0)
|| test_dequantize_pack8(RandomIntMat(15, 24), 24, 24)
|| test_dequantize_pack8(RandomIntMat(15, 24), 24, 1)
|| test_dequantize_pack8(RandomIntMat(15, 24), 24, 0)
|| test_dequantize_pack8(RandomIntMat(128), 1, 128)
|| test_dequantize_pack8(RandomIntMat(128), 1, 1)
|| test_dequantize_pack8(RandomIntMat(128), 1, 0)
|| test_dequantize_pack8(RandomIntMat(128), 128, 128)
|| test_dequantize_pack8(RandomIntMat(128), 128, 1)
|| test_dequantize_pack8(RandomIntMat(128), 128, 0);
}
int main()
{
SRAND(7767517);
return 0
|| test_dequantize_0()
|| test_dequantize_1()
|| test_dequantize_2()
|| test_dequantize_3();
}

75
3rdparty/ncnn/tests/test_dropout.cpp vendored Normal file
View File

@ -0,0 +1,75 @@
// 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/dropout.h"
#include "testutil.h"
static int test_dropout(const ncnn::Mat& a, float scale)
{
ncnn::ParamDict pd;
pd.set(0, scale);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Dropout>("Dropout", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_dropout failed a.dims=%d a=(%d %d %d) scale=%f\n", a.dims, a.w, a.h, a.c, scale);
}
return ret;
}
static int test_dropout_0()
{
return 0
|| test_dropout(RandomMat(5, 7, 24), 1.f)
|| test_dropout(RandomMat(5, 7, 24), 0.2f)
|| test_dropout(RandomMat(7, 9, 12), 1.f)
|| test_dropout(RandomMat(7, 9, 12), 0.3f)
|| test_dropout(RandomMat(3, 5, 13), 1.f)
|| test_dropout(RandomMat(3, 5, 13), 0.5f);
}
static int test_dropout_1()
{
return 0
|| test_dropout(RandomMat(15, 24), 1.f)
|| test_dropout(RandomMat(15, 24), 0.6f)
|| test_dropout(RandomMat(19, 12), 1.f)
|| test_dropout(RandomMat(19, 12), 0.4f)
|| test_dropout(RandomMat(17, 15), 1.f)
|| test_dropout(RandomMat(17, 15), 0.7f);
}
static int test_dropout_2()
{
return 0
|| test_dropout(RandomMat(128), 1.f)
|| test_dropout(RandomMat(128), 0.4f)
|| test_dropout(RandomMat(124), 1.f)
|| test_dropout(RandomMat(124), 0.1f)
|| test_dropout(RandomMat(127), 1.f)
|| test_dropout(RandomMat(127), 0.5f);
}
int main()
{
SRAND(7767517);
return 0
|| test_dropout_0()
|| test_dropout_1()
|| test_dropout_2();
}

335
3rdparty/ncnn/tests/test_eltwise.cpp vendored Normal file
View File

@ -0,0 +1,335 @@
// 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/eltwise.h"
#include "testutil.h"
static void print_float_array(const ncnn::Mat& a)
{
fprintf(stderr, "[");
for (int i = 0; i < a.w; i++)
{
fprintf(stderr, " %f", a[i]);
}
fprintf(stderr, " ]");
}
static int test_eltwise(const std::vector<ncnn::Mat>& a, int op_type, const ncnn::Mat& coeffs)
{
ncnn::ParamDict pd;
pd.set(0, op_type);
pd.set(1, coeffs);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Eltwise>("Eltwise", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_eltwise failed a[0].dims=%d a[0]=(%d %d %d) op_type=%d", a[0].dims, a[0].w, a[0].h, a[0].c, op_type);
fprintf(stderr, " coeffs=");
print_float_array(coeffs);
fprintf(stderr, "\n");
}
return ret;
}
static int test_eltwise_0()
{
std::vector<ncnn::Mat> a(2);
a[0] = RandomMat(16, 12, 12);
a[1] = RandomMat(16, 12, 12);
std::vector<ncnn::Mat> b(2);
b[0] = RandomMat(15, 11, 24);
b[1] = RandomMat(15, 11, 24);
std::vector<ncnn::Mat> c(2);
c[0] = RandomMat(9, 5, 7);
c[1] = RandomMat(9, 5, 7);
return 0
|| test_eltwise(a, 0, ncnn::Mat())
|| test_eltwise(a, 1, ncnn::Mat())
|| test_eltwise(a, 2, ncnn::Mat())
|| test_eltwise(b, 0, ncnn::Mat())
|| test_eltwise(b, 1, ncnn::Mat())
|| test_eltwise(b, 2, ncnn::Mat())
|| test_eltwise(c, 0, ncnn::Mat())
|| test_eltwise(c, 1, ncnn::Mat())
|| test_eltwise(c, 2, ncnn::Mat())
|| test_eltwise(a, 0, RandomMat(2))
|| test_eltwise(a, 1, RandomMat(2))
|| test_eltwise(a, 2, RandomMat(2))
|| test_eltwise(b, 0, RandomMat(2))
|| test_eltwise(b, 1, RandomMat(2))
|| test_eltwise(b, 2, RandomMat(2))
|| test_eltwise(c, 0, RandomMat(2))
|| test_eltwise(c, 1, RandomMat(2))
|| test_eltwise(c, 2, RandomMat(2));
}
static int test_eltwise_1()
{
std::vector<ncnn::Mat> a(3);
a[0] = RandomMat(15, 11, 16);
a[1] = RandomMat(15, 11, 16);
a[2] = RandomMat(15, 11, 16);
return 0
|| test_eltwise(a, 0, ncnn::Mat())
|| test_eltwise(a, 1, ncnn::Mat())
|| test_eltwise(a, 2, ncnn::Mat())
|| test_eltwise(a, 0, RandomMat(3))
|| test_eltwise(a, 1, RandomMat(3))
|| test_eltwise(a, 2, RandomMat(3));
}
static int test_eltwise_2()
{
std::vector<ncnn::Mat> a(4);
a[0] = RandomMat(7, 3, 5);
a[1] = RandomMat(7, 3, 5);
a[2] = RandomMat(7, 3, 5);
a[3] = RandomMat(7, 3, 5);
return 0
|| test_eltwise(a, 0, ncnn::Mat())
|| test_eltwise(a, 1, ncnn::Mat())
|| test_eltwise(a, 2, ncnn::Mat())
|| test_eltwise(a, 0, RandomMat(4))
|| test_eltwise(a, 1, RandomMat(4))
|| test_eltwise(a, 2, RandomMat(4));
}
static int test_eltwise_3()
{
std::vector<ncnn::Mat> a(5);
a[0] = RandomMat(12, 4, 16);
a[1] = RandomMat(12, 4, 16);
a[2] = RandomMat(12, 4, 16);
a[3] = RandomMat(12, 4, 16);
a[4] = RandomMat(12, 4, 16);
return 0
|| test_eltwise(a, 0, ncnn::Mat())
|| test_eltwise(a, 1, ncnn::Mat())
|| test_eltwise(a, 2, ncnn::Mat())
|| test_eltwise(a, 0, RandomMat(5))
|| test_eltwise(a, 1, RandomMat(5))
|| test_eltwise(a, 2, RandomMat(5));
}
static int test_eltwise_4()
{
std::vector<ncnn::Mat> a(2);
a[0] = RandomMat(16, 12);
a[1] = RandomMat(16, 12);
std::vector<ncnn::Mat> b(2);
b[0] = RandomMat(11, 24);
b[1] = RandomMat(11, 24);
std::vector<ncnn::Mat> c(2);
c[0] = RandomMat(9, 7);
c[1] = RandomMat(9, 7);
return 0
|| test_eltwise(a, 0, ncnn::Mat())
|| test_eltwise(a, 1, ncnn::Mat())
|| test_eltwise(a, 2, ncnn::Mat())
|| test_eltwise(b, 0, ncnn::Mat())
|| test_eltwise(b, 1, ncnn::Mat())
|| test_eltwise(b, 2, ncnn::Mat())
|| test_eltwise(c, 0, ncnn::Mat())
|| test_eltwise(c, 1, ncnn::Mat())
|| test_eltwise(c, 2, ncnn::Mat())
|| test_eltwise(a, 0, RandomMat(2))
|| test_eltwise(a, 1, RandomMat(2))
|| test_eltwise(a, 2, RandomMat(2))
|| test_eltwise(b, 0, RandomMat(2))
|| test_eltwise(b, 1, RandomMat(2))
|| test_eltwise(b, 2, RandomMat(2))
|| test_eltwise(c, 0, RandomMat(2))
|| test_eltwise(c, 1, RandomMat(2))
|| test_eltwise(c, 2, RandomMat(2));
}
static int test_eltwise_5()
{
std::vector<ncnn::Mat> a(3);
a[0] = RandomMat(15, 16);
a[1] = RandomMat(15, 16);
a[2] = RandomMat(15, 16);
return 0
|| test_eltwise(a, 0, ncnn::Mat())
|| test_eltwise(a, 1, ncnn::Mat())
|| test_eltwise(a, 2, ncnn::Mat())
|| test_eltwise(a, 0, RandomMat(3))
|| test_eltwise(a, 1, RandomMat(3))
|| test_eltwise(a, 2, RandomMat(3));
}
static int test_eltwise_6()
{
std::vector<ncnn::Mat> a(4);
a[0] = RandomMat(7, 5);
a[1] = RandomMat(7, 5);
a[2] = RandomMat(7, 5);
a[3] = RandomMat(7, 5);
return 0
|| test_eltwise(a, 0, ncnn::Mat())
|| test_eltwise(a, 1, ncnn::Mat())
|| test_eltwise(a, 2, ncnn::Mat())
|| test_eltwise(a, 0, RandomMat(4))
|| test_eltwise(a, 1, RandomMat(4))
|| test_eltwise(a, 2, RandomMat(4));
}
static int test_eltwise_7()
{
std::vector<ncnn::Mat> a(5);
a[0] = RandomMat(12, 16);
a[1] = RandomMat(12, 16);
a[2] = RandomMat(12, 16);
a[3] = RandomMat(12, 16);
a[4] = RandomMat(12, 16);
return 0
|| test_eltwise(a, 0, ncnn::Mat())
|| test_eltwise(a, 1, ncnn::Mat())
|| test_eltwise(a, 2, ncnn::Mat())
|| test_eltwise(a, 0, RandomMat(5))
|| test_eltwise(a, 1, RandomMat(5))
|| test_eltwise(a, 2, RandomMat(5));
}
static int test_eltwise_8()
{
std::vector<ncnn::Mat> a(2);
a[0] = RandomMat(12);
a[1] = RandomMat(12);
std::vector<ncnn::Mat> b(2);
b[0] = RandomMat(24);
b[1] = RandomMat(24);
std::vector<ncnn::Mat> c(2);
c[0] = RandomMat(19);
c[1] = RandomMat(19);
return 0
|| test_eltwise(a, 0, ncnn::Mat())
|| test_eltwise(a, 1, ncnn::Mat())
|| test_eltwise(a, 2, ncnn::Mat())
|| test_eltwise(b, 0, ncnn::Mat())
|| test_eltwise(b, 1, ncnn::Mat())
|| test_eltwise(b, 2, ncnn::Mat())
|| test_eltwise(c, 0, ncnn::Mat())
|| test_eltwise(c, 1, ncnn::Mat())
|| test_eltwise(c, 2, ncnn::Mat())
|| test_eltwise(a, 0, RandomMat(2))
|| test_eltwise(a, 1, RandomMat(2))
|| test_eltwise(a, 2, RandomMat(2))
|| test_eltwise(b, 0, RandomMat(2))
|| test_eltwise(b, 1, RandomMat(2))
|| test_eltwise(b, 2, RandomMat(2))
|| test_eltwise(c, 0, RandomMat(2))
|| test_eltwise(c, 1, RandomMat(2))
|| test_eltwise(c, 2, RandomMat(2));
}
static int test_eltwise_9()
{
std::vector<ncnn::Mat> a(3);
a[0] = RandomMat(36);
a[1] = RandomMat(36);
a[2] = RandomMat(36);
return 0
|| test_eltwise(a, 0, ncnn::Mat())
|| test_eltwise(a, 1, ncnn::Mat())
|| test_eltwise(a, 2, ncnn::Mat())
|| test_eltwise(a, 0, RandomMat(3))
|| test_eltwise(a, 1, RandomMat(3))
|| test_eltwise(a, 2, RandomMat(3));
}
static int test_eltwise_10()
{
std::vector<ncnn::Mat> a(4);
a[0] = RandomMat(15);
a[1] = RandomMat(15);
a[2] = RandomMat(15);
a[3] = RandomMat(15);
return 0
|| test_eltwise(a, 0, ncnn::Mat())
|| test_eltwise(a, 1, ncnn::Mat())
|| test_eltwise(a, 2, ncnn::Mat())
|| test_eltwise(a, 0, RandomMat(4))
|| test_eltwise(a, 1, RandomMat(4))
|| test_eltwise(a, 2, RandomMat(4));
}
static int test_eltwise_11()
{
std::vector<ncnn::Mat> a(5);
a[0] = RandomMat(16);
a[1] = RandomMat(16);
a[2] = RandomMat(16);
a[3] = RandomMat(16);
a[4] = RandomMat(16);
return 0
|| test_eltwise(a, 0, ncnn::Mat())
|| test_eltwise(a, 1, ncnn::Mat())
|| test_eltwise(a, 2, ncnn::Mat())
|| test_eltwise(a, 0, RandomMat(5))
|| test_eltwise(a, 1, RandomMat(5))
|| test_eltwise(a, 2, RandomMat(5));
}
int main()
{
SRAND(7767517);
return 0
|| test_eltwise_0()
|| test_eltwise_1()
|| test_eltwise_2()
|| test_eltwise_3()
|| test_eltwise_4()
|| test_eltwise_5()
|| test_eltwise_6()
|| test_eltwise_7()
|| test_eltwise_8()
|| test_eltwise_9()
|| test_eltwise_10()
|| test_eltwise_11();
}

67
3rdparty/ncnn/tests/test_elu.cpp vendored Normal file
View File

@ -0,0 +1,67 @@
// 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/elu.h"
#include "testutil.h"
static int test_elu(const ncnn::Mat& a)
{
ncnn::ParamDict pd;
float alpha = RandomFloat(0.001f, 1000.f);
pd.set(0, alpha); //alpha
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::ELU>("ELU", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_elu failed alpha=%f\n", alpha);
}
return ret;
}
static int test_elu_0()
{
return 0
|| test_elu(RandomMat(5, 7, 24))
|| test_elu(RandomMat(7, 9, 12))
|| test_elu(RandomMat(3, 5, 13));
}
static int test_elu_1()
{
return 0
|| test_elu(RandomMat(15, 24))
|| test_elu(RandomMat(17, 12))
|| test_elu(RandomMat(19, 15));
}
static int test_elu_2()
{
return 0
|| test_elu(RandomMat(128))
|| test_elu(RandomMat(124))
|| test_elu(RandomMat(127));
}
int main()
{
SRAND(7767517);
return 0
|| test_elu_0()
|| test_elu_1()
|| test_elu_2();
}

138
3rdparty/ncnn/tests/test_expanddims.cpp vendored Normal file
View File

@ -0,0 +1,138 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 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/expanddims.h"
#include "testutil.h"
static int test_expanddims(const ncnn::Mat& a, int expand_w, int expand_h, int expand_c)
{
ncnn::ParamDict pd;
pd.set(0, expand_w);
pd.set(1, expand_h);
pd.set(2, expand_c);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::ExpandDims>("ExpandDims", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_expanddims failed a.dims=%d a=(%d %d %d) expand_w=%d expand_h=%d expand_c=%d\n", a.dims, a.w, a.h, a.c, expand_w, expand_h, expand_c);
}
return ret;
}
static ncnn::Mat IntArrayMat(int a0)
{
ncnn::Mat m(1);
int* p = m;
p[0] = a0;
return m;
}
static ncnn::Mat IntArrayMat(int a0, int a1)
{
ncnn::Mat m(2);
int* p = m;
p[0] = a0;
p[1] = a1;
return m;
}
static ncnn::Mat IntArrayMat(int a0, int a1, int a2)
{
ncnn::Mat m(3);
int* p = m;
p[0] = a0;
p[1] = a1;
p[2] = a2;
return m;
}
static void print_int_array(const ncnn::Mat& a)
{
const int* pa = a;
fprintf(stderr, "[");
for (int i = 0; i < a.w; i++)
{
fprintf(stderr, " %d", pa[i]);
}
fprintf(stderr, " ]");
}
static int test_expanddims_axes(const ncnn::Mat& a, const ncnn::Mat& axes)
{
ncnn::ParamDict pd;
pd.set(3, axes);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::ExpandDims>("ExpandDims", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_expanddims_axes failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
fprintf(stderr, " axes=");
print_int_array(axes);
fprintf(stderr, "\n");
}
return ret;
}
static int test_expand_0()
{
ncnn::Mat as[7];
as[0] = RandomMat(1, 1, 1);
as[1] = RandomMat(14, 16);
as[2] = RandomMat(1, 14);
as[3] = RandomMat(11, 1);
as[4] = RandomMat(1, 1);
as[5] = RandomMat(120);
as[6] = RandomMat(1);
for (int i = 0; i < 7; i++)
{
const ncnn::Mat& a = as[i];
int ret = 0
|| test_expanddims(a, 0, 0, 0)
|| test_expanddims(a, 0, 0, 1)
|| test_expanddims(a, 0, 1, 0)
|| test_expanddims(a, 0, 1, 1)
|| test_expanddims(a, 1, 0, 0)
|| test_expanddims(a, 1, 0, 1)
|| test_expanddims(a, 1, 1, 0)
|| test_expanddims(a, 1, 1, 1)
|| test_expanddims_axes(a, IntArrayMat(0))
|| test_expanddims_axes(a, IntArrayMat(1))
|| test_expanddims_axes(a, IntArrayMat(2))
|| test_expanddims_axes(a, IntArrayMat(0, 1))
|| test_expanddims_axes(a, IntArrayMat(0, 2))
|| test_expanddims_axes(a, IntArrayMat(1, 2))
|| test_expanddims_axes(a, IntArrayMat(0, 1, 2));
if (ret != 0)
return ret;
}
return 0;
}
int main()
{
SRAND(7767517);
return test_expand_0();
}

100
3rdparty/ncnn/tests/test_flatten.cpp vendored Normal file
View File

@ -0,0 +1,100 @@
// 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/flatten.h"
#include "testutil.h"
static int test_flatten(const ncnn::Mat& a)
{
ncnn::ParamDict pd;
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Flatten>("Flatten", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_flatten failed a.dims=%d a=(%d %d %d %d)\n", a.dims, a.w, a.h, a.d, a.c);
}
return ret;
}
static int test_flatten_0()
{
return 0
|| test_flatten(RandomMat(2, 3, 4, 4))
|| test_flatten(RandomMat(3, 5, 7, 8))
|| test_flatten(RandomMat(1, 1, 1, 16))
|| test_flatten(RandomMat(9, 10, 2, 16))
|| test_flatten(RandomMat(1, 7, 1, 1))
|| test_flatten(RandomMat(6, 6, 6, 15))
|| test_flatten(RandomMat(2, 4, 4))
|| test_flatten(RandomMat(3, 5, 8))
|| test_flatten(RandomMat(1, 1, 16))
|| test_flatten(RandomMat(9, 10, 16))
|| test_flatten(RandomMat(1, 7, 1))
|| test_flatten(RandomMat(6, 6, 15))
|| test_flatten(RandomMat(13, 13))
|| test_flatten(RandomMat(16, 16))
|| test_flatten(RandomMat(8, 12))
|| test_flatten(RandomMat(8, 2))
|| test_flatten(RandomMat(32))
|| test_flatten(RandomMat(17));
}
static int test_flatten_int8(const ncnn::Mat& a)
{
ncnn::ParamDict pd;
std::vector<ncnn::Mat> weights(0);
int flag = TEST_LAYER_DISABLE_AUTO_INPUT_CASTING | TEST_LAYER_DISABLE_GPU_TESTING;
int ret = test_layer<ncnn::Flatten>("Flatten", pd, weights, a, 0.001, 0, flag);
if (ret != 0)
{
fprintf(stderr, "test_flatten_int8 failed a.dims=%d a=(%d %d %d %d)\n", a.dims, a.w, a.h, a.d, a.c);
}
return ret;
}
static int test_flatten_1()
{
return 0
|| test_flatten_int8(RandomS8Mat(2, 3, 4, 4))
|| test_flatten_int8(RandomS8Mat(3, 5, 7, 8))
|| test_flatten_int8(RandomS8Mat(1, 1, 1, 16))
|| test_flatten_int8(RandomS8Mat(9, 10, 2, 16))
|| test_flatten_int8(RandomS8Mat(1, 7, 1, 1))
|| test_flatten_int8(RandomS8Mat(6, 6, 6, 15))
|| test_flatten_int8(RandomS8Mat(2, 4, 16))
|| test_flatten_int8(RandomS8Mat(3, 5, 32))
|| test_flatten_int8(RandomS8Mat(1, 1, 64))
|| test_flatten_int8(RandomS8Mat(9, 10, 64))
|| test_flatten_int8(RandomS8Mat(1, 7, 4))
|| test_flatten_int8(RandomS8Mat(6, 6, 70))
|| test_flatten_int8(RandomS8Mat(13, 52))
|| test_flatten_int8(RandomS8Mat(16, 64))
|| test_flatten_int8(RandomS8Mat(8, 48))
|| test_flatten_int8(RandomS8Mat(8, 8))
|| test_flatten_int8(RandomS8Mat(128))
|| test_flatten_int8(RandomS8Mat(127));
}
int main()
{
SRAND(7767517);
return test_flatten_0() || test_flatten_1();
}

75
3rdparty/ncnn/tests/test_gelu.cpp vendored Normal file
View File

@ -0,0 +1,75 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 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/gelu.h"
#include "testutil.h"
static int test_gelu(const ncnn::Mat& a, bool fast_gelu)
{
ncnn::ParamDict pd;
pd.set(0, fast_gelu ? 1 : 0);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::GELU>("GELU", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_gelu failed a.dims=%d a=(%d %d %d) fast_gelu=%s\n", a.dims, a.w, a.h, a.c, fast_gelu ? "true" : "false");
}
return ret;
}
static int test_gelu_0()
{
return 0
|| test_gelu(RandomMat(5, 7, 24), false)
|| test_gelu(RandomMat(5, 7, 24), true)
|| test_gelu(RandomMat(7, 9, 12), false)
|| test_gelu(RandomMat(7, 9, 12), true)
|| test_gelu(RandomMat(3, 5, 13), false)
|| test_gelu(RandomMat(3, 5, 13), true);
}
static int test_gelu_1()
{
return 0
|| test_gelu(RandomMat(15, 24), false)
|| test_gelu(RandomMat(15, 24), true)
|| test_gelu(RandomMat(17, 12), false)
|| test_gelu(RandomMat(17, 12), true)
|| test_gelu(RandomMat(19, 15), false)
|| test_gelu(RandomMat(19, 15), true);
}
static int test_gelu_2()
{
return 0
|| test_gelu(RandomMat(128), false)
|| test_gelu(RandomMat(128), true)
|| test_gelu(RandomMat(124), false)
|| test_gelu(RandomMat(124), true)
|| test_gelu(RandomMat(127), false)
|| test_gelu(RandomMat(127), true);
}
int main()
{
SRAND(7767517);
return 0
|| test_gelu_0()
|| test_gelu_1()
|| test_gelu_2();
}

174
3rdparty/ncnn/tests/test_gemm.cpp vendored Normal file
View File

@ -0,0 +1,174 @@
// 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/gemm.h"
#include "testutil.h"
static int test_gemm(int M, int N, int K, float alpha, int transA, int transB)
{
ncnn::ParamDict pd;
pd.set(0, alpha);
pd.set(1, 1.f); // beta
pd.set(2, transA);
pd.set(3, transB);
std::vector<ncnn::Mat> weights(0);
std::vector<ncnn::Mat> a(2);
a[0] = transA ? ncnn::Mat(M, K) : ncnn::Mat(K, M);
a[1] = transB ? ncnn::Mat(K, N) : ncnn::Mat(N, K);
Randomize(a[0]);
Randomize(a[1]);
int ret = test_layer<ncnn::Gemm>("Gemm", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_gemm failed M=%d N=%d K=%d alpha=%f transA=%d transB=%d\n", M, N, K, alpha, transA, transB);
}
return ret;
}
static int test_gemm_bias(int M, int N, int K, const ncnn::Mat& C, float alpha, float beta, int transA, int transB)
{
ncnn::ParamDict pd;
pd.set(0, alpha);
pd.set(1, beta);
pd.set(2, transA);
pd.set(3, transB);
std::vector<ncnn::Mat> weights(0);
std::vector<ncnn::Mat> a(3);
a[0] = transA ? ncnn::Mat(M, K) : ncnn::Mat(K, M);
a[1] = transB ? ncnn::Mat(K, N) : ncnn::Mat(N, K);
a[2] = C;
Randomize(a[0]);
Randomize(a[1]);
int ret = test_layer<ncnn::Gemm>("Gemm", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_gemm_bias failed M=%d N=%d K=%d C.dims=%d C=(%d %d %d) alpha=%f transA=%d transB=%d\n", M, N, K, C.dims, C.w, C.h, C.c, alpha, transA, transB);
}
return ret;
}
static int test_gemm_0()
{
return 0
|| test_gemm(13, 14, 15, 0.1f, 0, 0)
|| test_gemm(13, 14, 15, 0.3f, 1, 0)
|| test_gemm(13, 14, 15, -0.4f, 0, 1)
|| test_gemm(13, 14, 15, 1.7f, 1, 1)
|| test_gemm(16, 24, 15, 0.1f, 0, 0)
|| test_gemm(16, 24, 15, 0.3f, 1, 0)
|| test_gemm(16, 24, 15, -0.4f, 0, 1)
|| test_gemm(16, 24, 15, 1.7f, 1, 1);
}
static int test_gemm_1()
{
return 0
|| test_gemm_bias(13, 14, 15, RandomMat(1), 0.1f, 0.2f, 0, 0)
|| test_gemm_bias(13, 14, 15, RandomMat(1), 0.4f, -1.2f, 1, 0)
|| test_gemm_bias(13, 14, 15, RandomMat(1), -0.3f, 3.f, 0, 1)
|| test_gemm_bias(13, 14, 15, RandomMat(1), 1.7f, 1.f, 1, 1)
|| test_gemm_bias(16, 24, 15, RandomMat(1), 0.1f, 0.2f, 0, 0)
|| test_gemm_bias(16, 24, 15, RandomMat(1), 0.4f, -1.2f, 1, 0)
|| test_gemm_bias(16, 24, 15, RandomMat(1), -0.3f, 3.f, 0, 1)
|| test_gemm_bias(16, 24, 15, RandomMat(1), 1.7f, 1.f, 1, 1);
}
static int test_gemm_2()
{
return 0
|| test_gemm_bias(13, 14, 15, RandomMat(13), 0.1f, 1.f, 0, 0)
|| test_gemm_bias(13, 14, 15, RandomMat(13), 0.4f, 2.f, 1, 0)
|| test_gemm_bias(13, 14, 15, RandomMat(13), -0.3f, 0.11f, 0, 1)
|| test_gemm_bias(13, 14, 15, RandomMat(13), 1.7f, -20.f, 1, 1)
|| test_gemm_bias(16, 24, 15, RandomMat(13), 0.1f, 1.f, 0, 0)
|| test_gemm_bias(16, 24, 15, RandomMat(13), 0.4f, 2.f, 1, 0)
|| test_gemm_bias(16, 24, 15, RandomMat(13), -0.3f, 0.11f, 0, 1)
|| test_gemm_bias(16, 24, 15, RandomMat(13), 1.7f, -20.f, 1, 1);
}
static int test_gemm_3()
{
return 0
|| test_gemm_bias(13, 14, 15, RandomMat(13, 1), 0.1f, 4.f, 0, 0)
|| test_gemm_bias(13, 14, 15, RandomMat(13, 1), 0.4f, 1.f, 1, 0)
|| test_gemm_bias(13, 14, 15, RandomMat(13, 1), -0.3f, -0.01f, 0, 1)
|| test_gemm_bias(13, 14, 15, RandomMat(13, 1), 1.7f, 0.3f, 1, 1)
|| test_gemm_bias(16, 24, 15, RandomMat(13, 1), 0.1f, 4.f, 0, 0)
|| test_gemm_bias(16, 24, 15, RandomMat(13, 1), 0.4f, 1.f, 1, 0)
|| test_gemm_bias(16, 24, 15, RandomMat(13, 1), -0.3f, -0.01f, 0, 1)
|| test_gemm_bias(16, 24, 15, RandomMat(13, 1), 1.7f, 0.3f, 1, 1);
}
static int test_gemm_4()
{
return 0
|| test_gemm_bias(13, 14, 15, RandomMat(13, 14), 0.1f, 6.f, 0, 0)
|| test_gemm_bias(13, 14, 15, RandomMat(13, 14), 0.4f, 1.22f, 1, 0)
|| test_gemm_bias(13, 14, 15, RandomMat(13, 14), -0.3f, 1.01f, 0, 1)
|| test_gemm_bias(13, 14, 15, RandomMat(13, 14), 1.7f, 0.3f, 1, 1)
|| test_gemm_bias(16, 24, 15, RandomMat(13, 14), 0.1f, 6.f, 0, 0)
|| test_gemm_bias(16, 24, 15, RandomMat(13, 14), 0.4f, 1.22f, 1, 0)
|| test_gemm_bias(16, 24, 15, RandomMat(13, 14), -0.3f, 1.01f, 0, 1)
|| test_gemm_bias(16, 24, 15, RandomMat(13, 14), 1.7f, 0.3f, 1, 1);
}
static int test_gemm_5()
{
return 0
|| test_gemm_bias(13, 14, 15, RandomMat(1, 14), 0.1f, 0.4f, 0, 0)
|| test_gemm_bias(13, 14, 15, RandomMat(1, 14), 0.4f, -1.f, 1, 0)
|| test_gemm_bias(13, 14, 15, RandomMat(1, 14), -0.3f, -0.21f, 0, 1)
|| test_gemm_bias(13, 14, 15, RandomMat(1, 14), 1.7f, 1.3f, 1, 1)
|| test_gemm_bias(16, 24, 15, RandomMat(1, 14), 0.1f, 0.4f, 0, 0)
|| test_gemm_bias(16, 24, 15, RandomMat(1, 14), 0.4f, -1.f, 1, 0)
|| test_gemm_bias(16, 24, 15, RandomMat(1, 14), -0.3f, -0.21f, 0, 1)
|| test_gemm_bias(16, 24, 15, RandomMat(1, 14), 1.7f, 1.3f, 1, 1);
}
static int test_gemm_6()
{
return 0
|| test_gemm_bias(13, 14, 15, RandomMat(14), 0.1f, 0.4f, 0, 0)
|| test_gemm_bias(13, 14, 15, RandomMat(14), 0.4f, -1.f, 1, 0)
|| test_gemm_bias(13, 14, 15, RandomMat(14), -0.3f, -0.21f, 0, 1)
|| test_gemm_bias(13, 14, 15, RandomMat(14), 1.7f, 1.3f, 1, 1)
|| test_gemm_bias(16, 24, 15, RandomMat(14), 0.1f, 0.4f, 0, 0)
|| test_gemm_bias(16, 24, 15, RandomMat(14), 0.4f, -1.f, 1, 0)
|| test_gemm_bias(16, 24, 15, RandomMat(14), -0.3f, -0.21f, 0, 1)
|| test_gemm_bias(16, 24, 15, RandomMat(14), 1.7f, 1.3f, 1, 1);
}
int main()
{
SRAND(7767517);
return 0
|| test_gemm_0()
|| test_gemm_1()
|| test_gemm_2()
|| test_gemm_3()
|| test_gemm_4()
|| test_gemm_5()
|| test_gemm_6();
}

57
3rdparty/ncnn/tests/test_groupnorm.cpp vendored Normal file
View File

@ -0,0 +1,57 @@
// 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/groupnorm.h"
#include "testutil.h"
static int test_groupnorm(const ncnn::Mat& a, int group, float eps)
{
int channels = a.c;
ncnn::ParamDict pd;
pd.set(0, group);
pd.set(1, channels);
pd.set(2, eps);
std::vector<ncnn::Mat> weights(2);
weights[0] = RandomMat(channels);
weights[1] = RandomMat(channels);
int ret = test_layer<ncnn::GroupNorm>("GroupNorm", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_groupnorm failed a.dims=%d a=(%d %d %d) group=%d eps=%f\n", a.dims, a.w, a.h, a.c, group, eps);
}
return ret;
}
static int test_groupnorm_0()
{
return 0
|| test_groupnorm(RandomMat(6, 4, 2), 1, 0.01f)
|| test_groupnorm(RandomMat(3, 3, 8), 2, 0.002f)
|| test_groupnorm(RandomMat(4, 5, 6), 3, 0.01f)
|| test_groupnorm(RandomMat(5, 6, 12), 4, 0.02f)
|| test_groupnorm(RandomMat(6, 7, 24), 2, 0.001f)
|| test_groupnorm(RandomMat(8, 9, 24), 3, 0.0001f);
}
int main()
{
SRAND(7767517);
return 0
|| test_groupnorm_0();
}

256
3rdparty/ncnn/tests/test_gru.cpp vendored Normal file
View File

@ -0,0 +1,256 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 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/gru.h"
#include "testutil.h"
static int test_gru(const ncnn::Mat& a, int outch, int direction)
{
int input_size = a.w;
int num_directions = direction == 2 ? 2 : 1;
ncnn::ParamDict pd;
pd.set(0, outch);
pd.set(1, outch * input_size * 3 * num_directions);
pd.set(2, direction);
std::vector<ncnn::Mat> weights(3);
weights[0] = RandomMat(outch * input_size * 3 * num_directions);
weights[1] = RandomMat(outch * 4 * num_directions);
weights[2] = RandomMat(outch * outch * 3 * num_directions);
int ret = test_layer<ncnn::GRU>("GRU", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_gru failed a.dims=%d a=(%d %d %d) outch=%d, direction = %d \n", a.dims, a.w, a.h, a.c, outch, direction);
}
return ret;
}
int test_gru_layer_with_hidden(const ncnn::Mat& a, int outch, int direction)
{
int input_size = a.w;
int num_directions = direction == 2 ? 2 : 1;
ncnn::ParamDict pd;
pd.set(0, outch);
pd.set(1, outch * input_size * 3 * num_directions);
pd.set(2, direction);
std::vector<ncnn::Mat> weights(3);
weights[0] = RandomMat(outch * input_size * 3 * num_directions);
weights[1] = RandomMat(outch * 4 * num_directions);
weights[2] = RandomMat(outch * outch * 3 * num_directions);
// initial hidden state
ncnn::Mat hidden = RandomMat(outch, num_directions);
std::vector<ncnn::Mat> as(2);
as[0] = a;
as[1] = hidden;
int ret = test_layer<ncnn::GRU>("GRU", pd, weights, as, 2);
if (ret != 0)
{
fprintf(stderr, "test_gru_layer_with_hidden failed a.dims=%d a=(%d %d %d) outch=%d, direction = %d \n", a.dims, a.w, a.h, a.c, outch, direction);
}
return ret;
}
int test_gru_layer_with_hidden_input(const ncnn::Mat& a, int outch, int direction)
{
int input_size = a.w;
int num_directions = direction == 2 ? 2 : 1;
ncnn::ParamDict pd;
pd.set(0, outch);
pd.set(1, outch * input_size * 3 * num_directions);
pd.set(2, direction);
std::vector<ncnn::Mat> weights(3);
weights[0] = RandomMat(outch * input_size * 3 * num_directions);
weights[1] = RandomMat(outch * 4 * num_directions);
weights[2] = RandomMat(outch * outch * 3 * num_directions);
// initial hidden state
ncnn::Mat hidden = RandomMat(outch, num_directions);
std::vector<ncnn::Mat> as(2);
as[0] = a;
as[1] = hidden;
int ret = test_layer<ncnn::GRU>("GRU", pd, weights, as, 1);
if (ret != 0)
{
fprintf(stderr, "test_gru_layer_with_hidden_input failed a.dims=%d a=(%d %d %d) outch=%d, direction = %d \n", a.dims, a.w, a.h, a.c, outch, direction);
}
return ret;
}
int test_gru_layer_with_hidden_output(const ncnn::Mat& a, int outch, int direction)
{
int input_size = a.w;
int num_directions = direction == 2 ? 2 : 1;
ncnn::ParamDict pd;
pd.set(0, outch);
pd.set(1, outch * input_size * 3 * num_directions);
pd.set(2, direction);
std::vector<ncnn::Mat> weights(3);
weights[0] = RandomMat(outch * input_size * 3 * num_directions);
weights[1] = RandomMat(outch * 4 * num_directions);
weights[2] = RandomMat(outch * outch * 3 * num_directions);
std::vector<ncnn::Mat> as(1);
as[0] = a;
int ret = test_layer<ncnn::GRU>("GRU", pd, weights, as, 2);
if (ret != 0)
{
fprintf(stderr, "test_gru_layer_with_hidden_output failed a.dims=%d a=(%d %d %d) outch=%d, direction = %d \n", a.dims, a.w, a.h, a.c, outch, direction);
}
return ret;
}
static int test_gru_0()
{
return 0
|| test_gru(RandomMat(4, 1), 2, 2)
|| test_gru(RandomMat(8, 2), 2, 2)
|| test_gru(RandomMat(16, 8), 7, 2)
|| test_gru(RandomMat(17, 8), 8, 2)
|| test_gru(RandomMat(19, 15), 8, 2)
|| test_gru(RandomMat(5, 16), 16, 2)
|| test_gru(RandomMat(3, 16), 8, 2)
|| test_gru(RandomMat(8, 16), 16, 2)
|| test_gru(RandomMat(2, 5), 17, 2);
}
static int test_gru_1()
{
return 0
|| test_gru_layer_with_hidden(RandomMat(4, 4), 1, 2)
|| test_gru_layer_with_hidden(RandomMat(8, 2), 2, 2)
|| test_gru_layer_with_hidden(RandomMat(16, 8), 7, 2)
|| test_gru_layer_with_hidden(RandomMat(17, 8), 8, 2)
|| test_gru_layer_with_hidden(RandomMat(19, 15), 8, 2)
|| test_gru_layer_with_hidden(RandomMat(5, 16), 16, 2)
|| test_gru_layer_with_hidden(RandomMat(3, 16), 8, 2)
|| test_gru_layer_with_hidden(RandomMat(2, 5), 99, 2)
|| test_gru_layer_with_hidden(RandomMat(4, 4), 1, 1)
|| test_gru_layer_with_hidden(RandomMat(8, 2), 2, 1)
|| test_gru_layer_with_hidden(RandomMat(16, 8), 7, 1)
|| test_gru_layer_with_hidden(RandomMat(17, 8), 8, 1)
|| test_gru_layer_with_hidden(RandomMat(19, 15), 8, 1)
|| test_gru_layer_with_hidden(RandomMat(5, 16), 16, 1)
|| test_gru_layer_with_hidden(RandomMat(3, 16), 8, 1)
|| test_gru_layer_with_hidden(RandomMat(2, 5), 99, 1)
|| test_gru_layer_with_hidden(RandomMat(4, 2), 1, 0)
|| test_gru_layer_with_hidden(RandomMat(8, 2), 2, 0)
|| test_gru_layer_with_hidden(RandomMat(16, 8), 7, 0)
|| test_gru_layer_with_hidden(RandomMat(17, 8), 8, 0)
|| test_gru_layer_with_hidden(RandomMat(19, 15), 8, 0)
|| test_gru_layer_with_hidden(RandomMat(5, 16), 16, 0)
|| test_gru_layer_with_hidden(RandomMat(3, 16), 8, 0)
|| test_gru_layer_with_hidden(RandomMat(2, 5), 17, 0)
|| test_gru_layer_with_hidden_input(RandomMat(4, 4), 1, 2)
|| test_gru_layer_with_hidden_input(RandomMat(8, 2), 2, 2)
|| test_gru_layer_with_hidden_input(RandomMat(16, 8), 7, 2)
|| test_gru_layer_with_hidden_input(RandomMat(17, 8), 8, 2)
|| test_gru_layer_with_hidden_input(RandomMat(19, 15), 8, 2)
|| test_gru_layer_with_hidden_input(RandomMat(5, 16), 16, 2)
|| test_gru_layer_with_hidden_input(RandomMat(3, 16), 8, 2)
|| test_gru_layer_with_hidden_input(RandomMat(2, 5), 99, 2)
|| test_gru_layer_with_hidden_input(RandomMat(4, 4), 1, 1)
|| test_gru_layer_with_hidden_input(RandomMat(8, 2), 2, 1)
|| test_gru_layer_with_hidden_input(RandomMat(16, 8), 7, 1)
|| test_gru_layer_with_hidden_input(RandomMat(17, 8), 8, 1)
|| test_gru_layer_with_hidden_input(RandomMat(19, 15), 8, 1)
|| test_gru_layer_with_hidden_input(RandomMat(5, 16), 16, 1)
|| test_gru_layer_with_hidden_input(RandomMat(3, 16), 8, 1)
|| test_gru_layer_with_hidden_input(RandomMat(2, 5), 99, 1)
|| test_gru_layer_with_hidden_input(RandomMat(4, 2), 1, 0)
|| test_gru_layer_with_hidden_input(RandomMat(8, 2), 2, 0)
|| test_gru_layer_with_hidden_input(RandomMat(16, 8), 7, 0)
|| test_gru_layer_with_hidden_input(RandomMat(17, 8), 8, 0)
|| test_gru_layer_with_hidden_input(RandomMat(19, 15), 8, 0)
|| test_gru_layer_with_hidden_input(RandomMat(5, 16), 16, 0)
|| test_gru_layer_with_hidden_input(RandomMat(3, 16), 8, 0)
|| test_gru_layer_with_hidden_input(RandomMat(2, 5), 17, 0)
|| test_gru_layer_with_hidden_output(RandomMat(4, 4), 1, 2)
|| test_gru_layer_with_hidden_output(RandomMat(8, 2), 2, 2)
|| test_gru_layer_with_hidden_output(RandomMat(16, 8), 7, 2)
|| test_gru_layer_with_hidden_output(RandomMat(17, 8), 8, 2)
|| test_gru_layer_with_hidden_output(RandomMat(19, 15), 8, 2)
|| test_gru_layer_with_hidden_output(RandomMat(5, 16), 16, 2)
|| test_gru_layer_with_hidden_output(RandomMat(3, 16), 8, 2)
|| test_gru_layer_with_hidden_output(RandomMat(2, 5), 99, 2)
|| test_gru_layer_with_hidden_output(RandomMat(4, 4), 1, 1)
|| test_gru_layer_with_hidden_output(RandomMat(8, 2), 2, 1)
|| test_gru_layer_with_hidden_output(RandomMat(16, 8), 7, 1)
|| test_gru_layer_with_hidden_output(RandomMat(17, 8), 8, 1)
|| test_gru_layer_with_hidden_output(RandomMat(19, 15), 8, 1)
|| test_gru_layer_with_hidden_output(RandomMat(5, 16), 16, 1)
|| test_gru_layer_with_hidden_output(RandomMat(3, 16), 8, 1)
|| test_gru_layer_with_hidden_output(RandomMat(2, 5), 99, 1)
|| test_gru_layer_with_hidden_output(RandomMat(4, 2), 1, 0)
|| test_gru_layer_with_hidden_output(RandomMat(8, 2), 2, 0)
|| test_gru_layer_with_hidden_output(RandomMat(16, 8), 7, 0)
|| test_gru_layer_with_hidden_output(RandomMat(17, 8), 8, 0)
|| test_gru_layer_with_hidden_output(RandomMat(19, 15), 8, 0)
|| test_gru_layer_with_hidden_output(RandomMat(5, 16), 16, 0)
|| test_gru_layer_with_hidden_output(RandomMat(3, 16), 8, 0)
|| test_gru_layer_with_hidden_output(RandomMat(2, 5), 17, 0);
}
static int test_gru_2()
{
return 0
|| test_gru(RandomMat(4, 1), 1, 0)
|| test_gru(RandomMat(8, 2), 2, 0)
|| test_gru(RandomMat(16, 8), 7, 0)
|| test_gru(RandomMat(17, 8), 8, 0)
|| test_gru(RandomMat(19, 15), 8, 0)
|| test_gru(RandomMat(5, 16), 16, 0)
|| test_gru(RandomMat(3, 16), 8, 0)
|| test_gru(RandomMat(8, 16), 16, 0)
|| test_gru(RandomMat(2, 5), 17, 0);
}
static int test_gru_3()
{
return 0
|| test_gru(RandomMat(4, 1), 1, 1)
|| test_gru(RandomMat(8, 2), 2, 1)
|| test_gru(RandomMat(16, 8), 7, 1)
|| test_gru(RandomMat(17, 8), 8, 1)
|| test_gru(RandomMat(19, 15), 8, 1)
|| test_gru(RandomMat(5, 16), 16, 1)
|| test_gru(RandomMat(3, 16), 8, 1)
|| test_gru(RandomMat(8, 16), 16, 1)
|| test_gru(RandomMat(2, 5), 17, 1);
}
int main()
{
SRAND(7767517);
return test_gru_0() || test_gru_1() || test_gru_2() || test_gru_3();
}

View File

@ -0,0 +1,67 @@
// 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/hardsigmoid.h"
#include "testutil.h"
static int test_hardsigmoid(const ncnn::Mat& a, float alpha, float beta)
{
ncnn::ParamDict pd;
pd.set(0, alpha);
pd.set(0, beta);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::HardSigmoid>("HardSigmoid", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_hardsigmoid failed a.dims=%d a=(%d %d %d) alpha=%f beta=%f\n", a.dims, a.w, a.h, a.c, alpha, beta);
}
return ret;
}
static int test_hardsigmoid_0()
{
return 0
|| test_hardsigmoid(RandomMat(5, 7, 24), 0.5f, 0.5f)
|| test_hardsigmoid(RandomMat(7, 9, 12), 0.5f, 0.5f)
|| test_hardsigmoid(RandomMat(3, 5, 13), 0.5f, 0.5f);
}
static int test_hardsigmoid_1()
{
return 0
|| test_hardsigmoid(RandomMat(15, 24), 0.5f, 0.5f)
|| test_hardsigmoid(RandomMat(17, 12), 0.5f, 0.5f)
|| test_hardsigmoid(RandomMat(19, 15), 0.5f, 0.5f);
}
static int test_hardsigmoid_2()
{
return 0
|| test_hardsigmoid(RandomMat(128), 0.5f, 0.5f)
|| test_hardsigmoid(RandomMat(124), 0.5f, 0.5f)
|| test_hardsigmoid(RandomMat(127), 0.5f, 0.5f);
}
int main()
{
SRAND(7767517);
return 0
|| test_hardsigmoid_0()
|| test_hardsigmoid_1()
|| test_hardsigmoid_2();
}

67
3rdparty/ncnn/tests/test_hardswish.cpp vendored Normal file
View File

@ -0,0 +1,67 @@
// 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/hardswish.h"
#include "testutil.h"
static int test_hardswish(const ncnn::Mat& a, float alpha, float beta)
{
ncnn::ParamDict pd;
pd.set(0, alpha);
pd.set(1, beta);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::HardSwish>("HardSwish", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_hardswish failed a.dims=%d a=(%d %d %d) alpha=%f beta=%f\n", a.dims, a.w, a.h, a.c, alpha, beta);
}
return ret;
}
static int test_hardswish_0()
{
return 0
|| test_hardswish(RandomMat(5, 7, 24), 0.2f, 0.5f)
|| test_hardswish(RandomMat(7, 9, 12), 0.2f, 0.5f)
|| test_hardswish(RandomMat(3, 5, 13), 0.2f, 0.5f);
}
static int test_hardswish_1()
{
return 0
|| test_hardswish(RandomMat(15, 24), 0.2f, 0.5f)
|| test_hardswish(RandomMat(17, 12), 0.2f, 0.5f)
|| test_hardswish(RandomMat(19, 15), 0.2f, 0.5f);
}
static int test_hardswish_2()
{
return 0
|| test_hardswish(RandomMat(128), 0.2f, 0.5f)
|| test_hardswish(RandomMat(124), 0.2f, 0.5f)
|| test_hardswish(RandomMat(127), 0.2f, 0.5f);
}
int main()
{
SRAND(7767517);
return 0
|| test_hardswish_0()
|| test_hardswish_1()
|| test_hardswish_2();
}

View File

@ -0,0 +1,277 @@
// 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/innerproduct.h"
#include "testutil.h"
static int test_innerproduct(const ncnn::Mat& a, int outch, int bias)
{
ncnn::ParamDict pd;
pd.set(0, outch); // num_output
pd.set(1, bias); // bias_term
pd.set(2, outch * a.w * a.h * a.c);
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<ncnn::Mat> weights(bias ? 2 : 1);
weights[0] = RandomMat(outch * a.w * a.h * a.c);
if (bias)
weights[1] = RandomMat(outch);
int ret = test_layer<ncnn::InnerProduct>("InnerProduct", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_innerproduct failed a.dims=%d a=(%d %d %d) outch=%d bias=%d act=%d actparams=[%f,%f]\n", a.dims, a.w, a.h, a.c, outch, bias, activation_type, activation_params[0], activation_params[1]);
}
return ret;
}
static int test_innerproduct_0()
{
return 0
|| test_innerproduct(RandomMat(1, 3, 1), 1, 1)
|| test_innerproduct(RandomMat(3, 2, 2), 2, 0)
|| test_innerproduct(RandomMat(9, 3, 8), 7, 1)
|| test_innerproduct(RandomMat(2, 2, 8), 8, 0)
|| test_innerproduct(RandomMat(4, 3, 15), 8, 1)
|| test_innerproduct(RandomMat(6, 2, 16), 16, 0)
|| test_innerproduct(RandomMat(6, 2, 16), 7, 1)
|| test_innerproduct(RandomMat(6, 2, 5), 16, 1);
}
static int test_innerproduct_1()
{
return 0
|| test_innerproduct(RandomMat(1, 1), 1, 1)
|| test_innerproduct(RandomMat(3, 2), 2, 0)
|| test_innerproduct(RandomMat(9, 8), 7, 1)
|| test_innerproduct(RandomMat(2, 8), 8, 0)
|| test_innerproduct(RandomMat(4, 15), 8, 1)
|| test_innerproduct(RandomMat(6, 16), 16, 0)
|| test_innerproduct(RandomMat(6, 16), 7, 1)
|| test_innerproduct(RandomMat(6, 5), 16, 1);
}
static int test_innerproduct_2()
{
return 0
|| test_innerproduct(RandomMat(1), 1, 1)
|| test_innerproduct(RandomMat(2), 2, 0)
|| test_innerproduct(RandomMat(8), 7, 1)
|| test_innerproduct(RandomMat(8), 8, 0)
|| test_innerproduct(RandomMat(15), 8, 1)
|| test_innerproduct(RandomMat(15), 15, 1)
|| test_innerproduct(RandomMat(16), 16, 0)
|| test_innerproduct(RandomMat(16), 7, 1)
|| test_innerproduct(RandomMat(5), 16, 0)
|| test_innerproduct(RandomMat(32), 16, 1)
|| test_innerproduct(RandomMat(12), 16, 0)
|| test_innerproduct(RandomMat(16), 12, 1)
|| test_innerproduct(RandomMat(24), 32, 1);
}
#if NCNN_INT8
static int test_innerproduct_int8(const ncnn::Mat& a, int outch, int bias)
{
ncnn::ParamDict pd;
pd.set(0, outch); // num_output
pd.set(1, bias); // bias_term
pd.set(2, outch * a.w * a.h * a.c);
pd.set(8, 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<ncnn::Mat> weights(bias ? 4 : 3);
const int k = a.w * a.h * a.c;
weights[0] = RandomMat(outch * k);
ncnn::Mat weight_scales = scales_mat(weights[0], outch, k, k);
ncnn::Mat input_scales = scales_mat(a, 1, k, k);
if (bias)
{
weights[1] = RandomMat(outch);
weights[2] = weight_scales;
weights[3] = input_scales;
}
else
{
weights[1] = weight_scales;
weights[2] = input_scales;
}
int flag = TEST_LAYER_DISABLE_GPU_TESTING;
int ret = test_layer<ncnn::InnerProduct>("InnerProduct", pd, weights, a, 0.001f, 0, flag);
if (ret != 0)
{
fprintf(stderr, "test_innerproduct_int8 failed a.dims=%d a=(%d %d %d) outch=%d bias=%d act=%d actparams=[%f,%f]\n", a.dims, a.w, a.h, a.c, outch, bias, activation_type, activation_params[0], activation_params[1]);
}
return ret;
}
static int test_innerproduct_3()
{
return 0
|| test_innerproduct_int8(RandomMat(1, 3, 1), 1, 1)
|| test_innerproduct_int8(RandomMat(3, 2, 2), 2, 1)
|| test_innerproduct_int8(RandomMat(5, 3, 3), 3, 1)
|| test_innerproduct_int8(RandomMat(7, 2, 3), 12, 1)
|| test_innerproduct_int8(RandomMat(9, 3, 4), 4, 1)
|| test_innerproduct_int8(RandomMat(2, 2, 7), 7, 1)
|| test_innerproduct_int8(RandomMat(4, 3, 8), 3, 1)
|| test_innerproduct_int8(RandomMat(6, 2, 8), 8, 1)
|| test_innerproduct_int8(RandomMat(8, 3, 15), 15, 1)
|| test_innerproduct_int8(RandomMat(7, 2, 16), 4, 1)
|| test_innerproduct_int8(RandomMat(6, 3, 16), 16, 1);
}
#endif // NCNN_INT8
static int test_innerproduct_gemm(const ncnn::Mat& a, int outch, int bias)
{
ncnn::ParamDict pd;
pd.set(0, outch);
pd.set(1, bias);
pd.set(2, outch * a.w);
int activation_type = RAND() % 7;
ncnn::Mat activation_params(2);
activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha
activation_params[1] = RandomFloat(0, 1);
pd.set(9, activation_type);
pd.set(10, activation_params);
std::vector<ncnn::Mat> weights(bias ? 2 : 1);
weights[0] = RandomMat(outch * a.w);
if (bias)
weights[1] = RandomMat(outch);
int ret = test_layer<ncnn::InnerProduct>("InnerProduct", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_innerproduct_gemm failed a.dims=%d a=(%d %d %d) outch=%d bias=%d act=%d actparams=[%f,%f]\n", a.dims, a.w, a.h, a.c, outch, bias, activation_type, activation_params[0], activation_params[1]);
}
return ret;
}
static int test_innerproduct_4()
{
return 0
|| test_innerproduct_gemm(RandomMat(1, 5), 1, 1)
|| test_innerproduct_gemm(RandomMat(3, 2), 2, 0)
|| test_innerproduct_gemm(RandomMat(9, 8), 7, 1)
|| test_innerproduct_gemm(RandomMat(2, 8), 8, 0)
|| test_innerproduct_gemm(RandomMat(13, 12), 8, 1)
|| test_innerproduct_gemm(RandomMat(16, 12), 16, 0)
|| test_innerproduct_gemm(RandomMat(11, 24), 8, 0)
|| test_innerproduct_gemm(RandomMat(13, 24), 12, 1)
|| test_innerproduct_gemm(RandomMat(15, 12), 20, 1)
|| test_innerproduct_gemm(RandomMat(16, 12), 11, 1)
|| test_innerproduct_gemm(RandomMat(19, 16), 16, 1)
|| test_innerproduct_gemm(RandomMat(14, 15), 8, 1)
|| test_innerproduct_gemm(RandomMat(17, 15), 12, 1)
|| test_innerproduct_gemm(RandomMat(12, 16), 7, 1)
|| test_innerproduct_gemm(RandomMat(11, 32), 32, 1)
|| test_innerproduct_gemm(RandomMat(12, 32), 24, 1)
|| test_innerproduct_gemm(RandomMat(13, 32), 12, 1)
|| test_innerproduct_gemm(RandomMat(14, 32), 14, 1)
|| test_innerproduct_gemm(RandomMat(15, 32), 32, 1)
|| test_innerproduct_gemm(RandomMat(16, 24), 32, 1)
|| test_innerproduct_gemm(RandomMat(17, 12), 32, 1)
|| test_innerproduct_gemm(RandomMat(18, 14), 32, 1);
}
#if NCNN_INT8
static int test_innerproduct_gemm_int8(const ncnn::Mat& a, int outch, int bias)
{
ncnn::ParamDict pd;
pd.set(0, outch);
pd.set(1, bias);
pd.set(2, outch * a.w);
pd.set(8, 1); // int8_scale_term
std::vector<ncnn::Mat> weights(bias ? 4 : 3);
const int k = a.w;
weights[0] = RandomMat(outch * k);
ncnn::Mat weight_scales = scales_mat(weights[0], outch, k, k);
ncnn::Mat input_scales = scales_mat(a, 1, k, k);
if (bias)
{
weights[1] = RandomMat(outch);
weights[2] = weight_scales;
weights[3] = input_scales;
}
else
{
weights[1] = weight_scales;
weights[2] = input_scales;
}
int flag = TEST_LAYER_DISABLE_GPU_TESTING;
int ret = test_layer<ncnn::InnerProduct>("InnerProduct", pd, weights, a, 0.001f, 0, flag);
if (ret != 0)
{
fprintf(stderr, "test_innerproduct_gemm_int8 failed a.dims=%d a=(%d %d %d) outch=%d bias=%d\n", a.dims, a.w, a.h, a.c, outch, bias);
}
return ret;
}
static int test_innerproduct_5()
{
return 0
|| test_innerproduct_gemm_int8(RandomMat(1, 5), 1, 1)
|| test_innerproduct_gemm_int8(RandomMat(3, 2), 2, 0)
|| test_innerproduct_gemm_int8(RandomMat(9, 8), 7, 1)
|| test_innerproduct_gemm_int8(RandomMat(2, 8), 8, 0)
|| test_innerproduct_gemm_int8(RandomMat(13, 12), 8, 1)
|| test_innerproduct_gemm_int8(RandomMat(16, 12), 16, 0)
|| test_innerproduct_gemm_int8(RandomMat(4, 15), 8, 1)
|| test_innerproduct_gemm_int8(RandomMat(6, 16), 16, 0)
|| test_innerproduct_gemm_int8(RandomMat(12, 16), 7, 1);
}
#endif // NCNN_INT8
int main()
{
SRAND(7767517);
#if NCNN_INT8
return 0
|| test_innerproduct_0()
|| test_innerproduct_1()
|| test_innerproduct_2()
|| test_innerproduct_3()
|| test_innerproduct_4()
|| test_innerproduct_5();
#else
return 0
|| test_innerproduct_0()
|| test_innerproduct_1()
|| test_innerproduct_2()
|| test_innerproduct_4();
#endif
}

View File

@ -0,0 +1,57 @@
// 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/instancenorm.h"
#include "testutil.h"
static int test_instancenorm(const ncnn::Mat& a, float eps, int affine)
{
int channels = a.c;
ncnn::ParamDict pd;
pd.set(0, affine ? channels : 0);
pd.set(1, eps);
pd.set(2, affine);
std::vector<ncnn::Mat> weights(2);
weights[0] = RandomMat(channels);
weights[1] = RandomMat(channels);
int ret = test_layer<ncnn::InstanceNorm>("InstanceNorm", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_instancenorm failed a.dims=%d a=(%d %d %d) eps=%f affine=%d\n", a.dims, a.w, a.h, a.c, eps, affine);
}
return ret;
}
static int test_instancenorm_0()
{
return 0
|| test_instancenorm(RandomMat(6, 4, 2), 0.01f, 0)
|| test_instancenorm(RandomMat(3, 3, 12), 0.002f, 0)
|| test_instancenorm(RandomMat(5, 7, 16), 0.02f, 0)
|| test_instancenorm(RandomMat(6, 4, 2), 0.01f, 1)
|| test_instancenorm(RandomMat(3, 3, 12), 0.002f, 1)
|| test_instancenorm(RandomMat(5, 7, 16), 0.02f, 1);
}
int main()
{
SRAND(7767517);
return 0
|| test_instancenorm_0();
}

523
3rdparty/ncnn/tests/test_interp.cpp vendored Normal file
View File

@ -0,0 +1,523 @@
// 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/interp.h"
#include "testutil.h"
static int test_interp(const ncnn::Mat& a, int resize_type, float height_scale, float width_scale, int output_height, int output_width)
{
ncnn::ParamDict pd;
pd.set(0, resize_type);
pd.set(1, height_scale);
pd.set(2, width_scale);
pd.set(3, output_height);
pd.set(4, output_width);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Interp>("Interp", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_interp failed a.dims=%d a=(%d %d %d) resize_type=%d height_scale=%f width_scale=%f output_height=%d output_width=%d\n", a.dims, a.w, a.h, a.c, resize_type, height_scale, width_scale, output_height, output_width);
}
return ret;
}
static int test_interp_ref(const ncnn::Mat& a, int resize_type, int output_height, int output_width)
{
ncnn::ParamDict pd;
pd.set(0, resize_type);
pd.set(5, 1);
std::vector<ncnn::Mat> as(2);
as[0] = a;
as[1] = ncnn::Mat(output_width, output_height, 1);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Interp>("Interp", pd, weights, as);
if (ret != 0)
{
fprintf(stderr, "test_interp_ref failed a.dims=%d a=(%d %d %d) resize_type=%d output_height=%d output_width=%d\n", a.dims, a.w, a.h, a.c, resize_type, output_height, output_width);
}
return ret;
}
static int test_interp_align_corner(const ncnn::Mat& a, int resize_type, float height_scale, float width_scale, int output_height, int output_width, int align_corner)
{
ncnn::ParamDict pd;
pd.set(0, resize_type);
pd.set(1, height_scale);
pd.set(2, width_scale);
pd.set(3, output_height);
pd.set(4, output_width);
pd.set(6, align_corner);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Interp>("Interp", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_interp failed a.dims=%d a=(%d %d %d) resize_type=%d height_scale=%f width_scale=%f output_height=%d output_width=%d align_corner=%d\n", a.dims, a.w, a.h, a.c, resize_type, height_scale, width_scale, output_height, output_width, align_corner);
}
return ret;
}
static int test_interp(const ncnn::Mat& a, int resize_type, float width_scale, int output_width)
{
ncnn::ParamDict pd;
pd.set(0, resize_type);
pd.set(1, 1.f);
pd.set(2, width_scale);
pd.set(3, 0);
pd.set(4, output_width);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Interp>("Interp", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_interp failed a.dims=%d a=(%d %d %d) resize_type=%d width_scale=%f output_width=%d\n", a.dims, a.w, a.h, a.c, resize_type, width_scale, output_width);
}
return ret;
}
static int test_interp_ref(const ncnn::Mat& a, int resize_type, int output_width)
{
ncnn::ParamDict pd;
pd.set(0, resize_type);
pd.set(5, 1);
std::vector<ncnn::Mat> as(2);
as[0] = a;
as[1] = ncnn::Mat(output_width, 1);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Interp>("Interp", pd, weights, as);
if (ret != 0)
{
fprintf(stderr, "test_interp_ref failed a.dims=%d a=(%d %d %d) resize_type=%d output_width=%d\n", a.dims, a.w, a.h, a.c, resize_type, output_width);
}
return ret;
}
static int test_interp_align_corner(const ncnn::Mat& a, int resize_type, float width_scale, int output_width, int align_corner)
{
ncnn::ParamDict pd;
pd.set(0, resize_type);
pd.set(1, 1.f);
pd.set(2, width_scale);
pd.set(3, 0);
pd.set(4, output_width);
pd.set(6, align_corner);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Interp>("Interp", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_interp failed a.dims=%d a=(%d %d %d) resize_type=%d width_scale=%f output_width=%d align_corner=%d\n", a.dims, a.w, a.h, a.c, resize_type, width_scale, output_width, align_corner);
}
return ret;
}
static int test_interp_0()
{
ncnn::Mat a = RandomMat(15, 16, 7);
ncnn::Mat b = RandomMat(14, 17, 12);
ncnn::Mat c = RandomMat(13, 14, 32);
return 0
|| test_interp(a, 1, 2.f, 2.f, 0, 0)
|| test_interp(a, 1, 4.f, 0.5f, 0, 0)
|| test_interp(a, 1, 1.2f, 1.2f, 0, 0)
|| test_interp(a, 1, 0.8f, 0.8f, 0, 0)
|| test_interp(a, 1, 1.f, 1.f, 10, 12)
|| test_interp(a, 1, 1.f, 1.f, 2, 2)
|| test_interp(a, 1, 1.f, 1.f, 15, 16)
|| test_interp_ref(a, 1, 10, 12)
|| test_interp_ref(a, 1, 2, 2)
|| test_interp_ref(a, 1, 15, 16)
|| test_interp(b, 1, 2.f, 2.f, 0, 0)
|| test_interp(b, 1, 4.f, 0.5f, 0, 0)
|| test_interp(b, 1, 1.2f, 1.2f, 0, 0)
|| test_interp(b, 1, 0.8f, 0.8f, 0, 0)
|| test_interp(b, 1, 1.f, 1.f, 10, 12)
|| test_interp(b, 1, 1.f, 1.f, 2, 2)
|| test_interp(b, 1, 1.f, 1.f, 14, 17)
|| test_interp_ref(b, 1, 10, 12)
|| test_interp_ref(b, 1, 2, 2)
|| test_interp_ref(b, 1, 14, 17)
|| test_interp(c, 1, 2.f, 2.f, 0, 0)
|| test_interp(c, 1, 4.f, 0.5f, 0, 0)
|| test_interp(c, 1, 1.2f, 1.2f, 0, 0)
|| test_interp(c, 1, 0.8f, 0.8f, 0, 0)
|| test_interp(c, 1, 1.f, 1.f, 10, 12)
|| test_interp(c, 1, 1.f, 1.f, 2, 2)
|| test_interp(c, 1, 1.f, 1.f, 14, 17)
|| test_interp_ref(c, 1, 10, 12)
|| test_interp_ref(c, 1, 2, 2)
|| test_interp_ref(c, 1, 14, 17);
}
static int test_interp_1()
{
ncnn::Mat a = RandomMat(15, 16, 7);
ncnn::Mat b = RandomMat(14, 17, 12);
ncnn::Mat c = RandomMat(13, 14, 32);
return 0
|| test_interp(a, 2, 2.f, 2.f, 0, 0)
|| test_interp(a, 2, 4.f, 0.5f, 0, 0)
|| test_interp(a, 2, 1.2f, 1.2f, 0, 0)
|| test_interp(a, 2, 0.8f, 0.8f, 0, 0)
|| test_interp(a, 2, 1.f, 1.f, 10, 12)
|| test_interp(a, 2, 1.f, 1.f, 2, 2)
|| test_interp(a, 2, 1.f, 1.f, 15, 16)
|| test_interp_align_corner(a, 2, 2.f, 2.f, 0, 0, 1)
|| test_interp_align_corner(a, 2, 4.f, 0.5f, 0, 0, 1)
|| test_interp_align_corner(a, 2, 1.2f, 1.2f, 0, 0, 1)
|| test_interp_align_corner(a, 2, 0.8f, 0.8f, 0, 0, 1)
|| test_interp_align_corner(a, 2, 1.f, 1.f, 10, 12, 1)
|| test_interp_align_corner(a, 2, 1.f, 1.f, 2, 2, 1)
|| test_interp_align_corner(a, 2, 1.f, 1.f, 15, 16, 1)
|| test_interp_ref(a, 2, 10, 12)
|| test_interp_ref(a, 2, 2, 2)
|| test_interp_ref(a, 2, 15, 16)
|| test_interp(b, 2, 2.f, 2.f, 0, 0)
|| test_interp(b, 2, 4.f, 0.5f, 0, 0)
|| test_interp(b, 2, 1.2f, 1.2f, 0, 0)
|| test_interp(b, 2, 0.8f, 0.8f, 0, 0)
|| test_interp(b, 2, 1.f, 1.f, 10, 12)
|| test_interp(b, 2, 1.f, 1.f, 2, 2)
|| test_interp(b, 2, 1.f, 1.f, 14, 17)
|| test_interp_align_corner(b, 2, 2.f, 2.f, 0, 0, 1)
|| test_interp_align_corner(b, 2, 4.f, 0.5f, 0, 0, 1)
|| test_interp_align_corner(b, 2, 1.2f, 1.2f, 0, 0, 1)
|| test_interp_align_corner(b, 2, 0.8f, 0.8f, 0, 0, 1)
|| test_interp_align_corner(b, 2, 1.f, 1.f, 10, 12, 1)
|| test_interp_align_corner(b, 2, 1.f, 1.f, 2, 2, 1)
|| test_interp_align_corner(b, 2, 1.f, 1.f, 14, 17, 1)
|| test_interp_ref(b, 2, 10, 12)
|| test_interp_ref(b, 2, 2, 2)
|| test_interp_ref(b, 2, 14, 17)
|| test_interp(c, 2, 2.f, 2.f, 0, 0)
|| test_interp(c, 2, 4.f, 0.5f, 0, 0)
|| test_interp(c, 2, 1.2f, 1.2f, 0, 0)
|| test_interp(c, 2, 0.8f, 0.8f, 0, 0)
|| test_interp(c, 2, 1.f, 1.f, 10, 12)
|| test_interp(c, 2, 1.f, 1.f, 2, 2)
|| test_interp(c, 2, 1.f, 1.f, 14, 17)
|| test_interp_align_corner(c, 2, 2.f, 2.f, 0, 0, 1)
|| test_interp_align_corner(c, 2, 4.f, 0.5f, 0, 0, 1)
|| test_interp_align_corner(c, 2, 1.2f, 1.2f, 0, 0, 1)
|| test_interp_align_corner(c, 2, 0.8f, 0.8f, 0, 0, 1)
|| test_interp_align_corner(c, 2, 1.f, 1.f, 10, 12, 1)
|| test_interp_align_corner(c, 2, 1.f, 1.f, 2, 2, 1)
|| test_interp_align_corner(c, 2, 1.f, 1.f, 14, 17, 1)
|| test_interp_ref(c, 2, 10, 12)
|| test_interp_ref(c, 2, 2, 2)
|| test_interp_ref(c, 2, 14, 17);
}
static int test_interp_2()
{
ncnn::Mat a = RandomMat(16, 17, 13);
ncnn::Mat b = RandomMat(18, 19, 12);
ncnn::Mat c = RandomMat(13, 14, 32);
return 0
|| test_interp(a, 3, 2.f, 2.f, 0, 0)
|| test_interp(a, 3, 4.f, 0.5f, 0, 0)
|| test_interp(a, 3, 1.2f, 1.2f, 0, 0)
|| test_interp(a, 3, 0.8f, 0.8f, 0, 0)
|| test_interp(a, 3, 1.f, 1.f, 10, 12)
|| test_interp(a, 3, 1.f, 1.f, 2, 2)
|| test_interp(a, 3, 1.f, 1.f, 6, 7)
|| test_interp(a, 3, 1.f, 1.f, 16, 17)
|| test_interp_align_corner(a, 3, 2.f, 2.f, 0, 0, 1)
|| test_interp_align_corner(a, 3, 4.f, 0.5f, 0, 0, 1)
|| test_interp_align_corner(a, 3, 1.2f, 1.2f, 0, 0, 1)
|| test_interp_align_corner(a, 3, 0.8f, 0.8f, 0, 0, 1)
|| test_interp_align_corner(a, 3, 1.f, 1.f, 10, 12, 1)
|| test_interp_align_corner(a, 3, 1.f, 1.f, 2, 2, 1)
|| test_interp_align_corner(a, 3, 1.f, 1.f, 6, 7, 1)
|| test_interp_align_corner(a, 3, 1.f, 1.f, 16, 17, 1)
|| test_interp_ref(a, 3, 2, 2)
|| test_interp_ref(a, 3, 6, 7)
|| test_interp_ref(a, 3, 16, 17)
|| test_interp(b, 3, 2.f, 2.f, 0, 0)
|| test_interp(b, 3, 4.f, 0.5f, 0, 0)
|| test_interp(b, 3, 1.2f, 1.2f, 0, 0)
|| test_interp(b, 3, 0.8f, 0.8f, 0, 0)
|| test_interp(b, 3, 1.f, 1.f, 10, 12)
|| test_interp(b, 3, 1.f, 1.f, 2, 2)
|| test_interp(b, 3, 1.f, 1.f, 6, 7)
|| test_interp(b, 3, 1.f, 1.f, 18, 19)
|| test_interp_align_corner(b, 3, 2.f, 2.f, 0, 0, 1)
|| test_interp_align_corner(b, 3, 4.f, 0.5f, 0, 0, 1)
|| test_interp_align_corner(b, 3, 1.2f, 1.2f, 0, 0, 1)
|| test_interp_align_corner(b, 3, 0.8f, 0.8f, 0, 0, 1)
|| test_interp_align_corner(b, 3, 1.f, 1.f, 10, 12, 1)
|| test_interp_align_corner(b, 3, 1.f, 1.f, 2, 2, 1)
|| test_interp_align_corner(b, 3, 1.f, 1.f, 6, 7, 1)
|| test_interp_align_corner(b, 3, 1.f, 1.f, 18, 19, 1)
|| test_interp_ref(b, 3, 2, 2)
|| test_interp_ref(b, 3, 6, 7)
|| test_interp_ref(b, 3, 18, 19)
|| test_interp(c, 3, 2.f, 2.f, 0, 0)
|| test_interp(c, 3, 4.f, 0.5f, 0, 0)
|| test_interp(c, 3, 1.2f, 1.2f, 0, 0)
|| test_interp(c, 3, 0.8f, 0.8f, 0, 0)
|| test_interp(c, 3, 1.f, 1.f, 10, 12)
|| test_interp(c, 3, 1.f, 1.f, 2, 2)
|| test_interp(c, 3, 1.f, 1.f, 6, 7)
|| test_interp(c, 3, 1.f, 1.f, 18, 19)
|| test_interp_align_corner(c, 3, 2.f, 2.f, 0, 0, 1)
|| test_interp_align_corner(c, 3, 4.f, 0.5f, 0, 0, 1)
|| test_interp_align_corner(c, 3, 1.2f, 1.2f, 0, 0, 1)
|| test_interp_align_corner(c, 3, 0.8f, 0.8f, 0, 0, 1)
|| test_interp_align_corner(c, 3, 1.f, 1.f, 10, 12, 1)
|| test_interp_align_corner(c, 3, 1.f, 1.f, 2, 2, 1)
|| test_interp_align_corner(c, 3, 1.f, 1.f, 6, 7, 1)
|| test_interp_align_corner(c, 3, 1.f, 1.f, 18, 19, 1)
|| test_interp_ref(c, 3, 2, 2)
|| test_interp_ref(c, 3, 6, 7)
|| test_interp_ref(c, 3, 18, 19);
}
static int test_interp_3()
{
ncnn::Mat a = RandomMat(15, 7);
ncnn::Mat b = RandomMat(14, 12);
ncnn::Mat c = RandomMat(13, 32);
return 0
|| test_interp(a, 1, 2.f, 0)
|| test_interp(a, 1, 0.5f, 0)
|| test_interp(a, 1, 1.2f, 0)
|| test_interp(a, 1, 0.8f, 0)
|| test_interp(a, 1, 1.f, 12)
|| test_interp(a, 1, 1.f, 2)
|| test_interp(a, 1, 1.f, 16)
|| test_interp_ref(a, 1, 12)
|| test_interp_ref(a, 1, 2)
|| test_interp_ref(a, 1, 16)
|| test_interp(b, 1, 2.f, 0)
|| test_interp(b, 1, 0.5f, 0)
|| test_interp(b, 1, 1.2f, 0)
|| test_interp(b, 1, 0.8f, 0)
|| test_interp(b, 1, 1.f, 12)
|| test_interp(b, 1, 1.f, 2)
|| test_interp(b, 1, 1.f, 17)
|| test_interp_ref(b, 1, 12)
|| test_interp_ref(b, 1, 2)
|| test_interp_ref(b, 1, 17)
|| test_interp(c, 1, 2.f, 0)
|| test_interp(c, 1, 0.5f, 0)
|| test_interp(c, 1, 1.2f, 0)
|| test_interp(c, 1, 0.8f, 0)
|| test_interp(c, 1, 1.f, 12)
|| test_interp(c, 1, 1.f, 2)
|| test_interp(c, 1, 1.f, 17)
|| test_interp_ref(c, 1, 12)
|| test_interp_ref(c, 1, 2)
|| test_interp_ref(c, 1, 17);
}
static int test_interp_4()
{
ncnn::Mat a = RandomMat(15, 7);
ncnn::Mat b = RandomMat(14, 12);
ncnn::Mat c = RandomMat(13, 32);
return 0
|| test_interp(a, 2, 2.f, 0)
|| test_interp(a, 2, 0.5f, 0)
|| test_interp(a, 2, 1.2f, 0)
|| test_interp(a, 2, 0.8f, 0)
|| test_interp(a, 2, 1.f, 12)
|| test_interp(a, 2, 1.f, 2)
|| test_interp(a, 2, 1.f, 16)
|| test_interp_align_corner(a, 2, 2.f, 0, 1)
|| test_interp_align_corner(a, 2, 0.5f, 0, 1)
|| test_interp_align_corner(a, 2, 1.2f, 0, 1)
|| test_interp_align_corner(a, 2, 0.8f, 0, 1)
|| test_interp_align_corner(a, 2, 1.f, 12, 1)
|| test_interp_align_corner(a, 2, 1.f, 2, 1)
|| test_interp_align_corner(a, 2, 1.f, 16, 1)
|| test_interp_ref(a, 2, 12)
|| test_interp_ref(a, 2, 2)
|| test_interp_ref(a, 2, 16)
|| test_interp(b, 2, 2.f, 0)
|| test_interp(b, 2, 0.5f, 0)
|| test_interp(b, 2, 1.2f, 0)
|| test_interp(b, 2, 0.8f, 0)
|| test_interp(b, 2, 1.f, 12)
|| test_interp(b, 2, 1.f, 2)
|| test_interp(b, 2, 1.f, 17)
|| test_interp_align_corner(b, 2, 2.f, 0, 1)
|| test_interp_align_corner(b, 2, 0.5f, 0, 1)
|| test_interp_align_corner(b, 2, 1.2f, 0, 1)
|| test_interp_align_corner(b, 2, 0.8f, 0, 1)
|| test_interp_align_corner(b, 2, 1.f, 12, 1)
|| test_interp_align_corner(b, 2, 1.f, 2, 1)
|| test_interp_align_corner(b, 2, 1.f, 17, 1)
|| test_interp_ref(b, 2, 12)
|| test_interp_ref(b, 2, 2)
|| test_interp_ref(b, 2, 17)
|| test_interp(c, 2, 2.f, 0)
|| test_interp(c, 2, 0.5f, 0)
|| test_interp(c, 2, 1.2f, 0)
|| test_interp(c, 2, 0.8f, 0)
|| test_interp(c, 2, 1.f, 12)
|| test_interp(c, 2, 1.f, 2)
|| test_interp(c, 2, 1.f, 17)
|| test_interp_align_corner(c, 2, 2.f, 0, 1)
|| test_interp_align_corner(c, 2, 0.5f, 0, 1)
|| test_interp_align_corner(c, 2, 1.2f, 0, 1)
|| test_interp_align_corner(c, 2, 0.8f, 0, 1)
|| test_interp_align_corner(c, 2, 1.f, 12, 1)
|| test_interp_align_corner(c, 2, 1.f, 2, 1)
|| test_interp_align_corner(c, 2, 1.f, 17, 1)
|| test_interp_ref(c, 2, 12)
|| test_interp_ref(c, 2, 2)
|| test_interp_ref(c, 2, 17);
}
static int test_interp_5()
{
ncnn::Mat a = RandomMat(16, 13);
ncnn::Mat b = RandomMat(18, 12);
ncnn::Mat c = RandomMat(13, 32);
return 0
|| test_interp(a, 3, 2.f, 0)
|| test_interp(a, 3, 0.5f, 0)
|| test_interp(a, 3, 1.2f, 0)
|| test_interp(a, 3, 0.8f, 0)
|| test_interp(a, 3, 1.f, 12)
|| test_interp(a, 3, 1.f, 2)
|| test_interp(a, 3, 1.f, 7)
|| test_interp(a, 3, 1.f, 17)
|| test_interp_align_corner(a, 3, 2.f, 0, 1)
|| test_interp_align_corner(a, 3, 0.5f, 0, 1)
|| test_interp_align_corner(a, 3, 1.2f, 0, 1)
|| test_interp_align_corner(a, 3, 0.8f, 0, 1)
|| test_interp_align_corner(a, 3, 1.f, 12, 1)
|| test_interp_align_corner(a, 3, 1.f, 2, 1)
|| test_interp_align_corner(a, 3, 1.f, 7, 1)
|| test_interp_align_corner(a, 3, 1.f, 17, 1)
|| test_interp_ref(a, 3, 2)
|| test_interp_ref(a, 3, 7)
|| test_interp_ref(a, 3, 17)
|| test_interp(b, 3, 2.f, 0)
|| test_interp(b, 3, 0.5f, 0)
|| test_interp(b, 3, 1.2f, 0)
|| test_interp(b, 3, 0.8f, 0)
|| test_interp(b, 3, 1.f, 12)
|| test_interp(b, 3, 1.f, 2)
|| test_interp(b, 3, 1.f, 7)
|| test_interp(b, 3, 1.f, 19)
|| test_interp_align_corner(b, 3, 2.f, 0, 1)
|| test_interp_align_corner(b, 3, 0.5f, 0, 1)
|| test_interp_align_corner(b, 3, 1.2f, 0, 1)
|| test_interp_align_corner(b, 3, 0.8f, 0, 1)
|| test_interp_align_corner(b, 3, 1.f, 12, 1)
|| test_interp_align_corner(b, 3, 1.f, 2, 1)
|| test_interp_align_corner(b, 3, 1.f, 7, 1)
|| test_interp_align_corner(b, 3, 1.f, 19, 1)
|| test_interp_ref(b, 3, 2)
|| test_interp_ref(b, 3, 7)
|| test_interp_ref(b, 3, 19)
|| test_interp(c, 3, 2.f, 0)
|| test_interp(c, 3, 0.5f, 0)
|| test_interp(c, 3, 1.2f, 0)
|| test_interp(c, 3, 0.8f, 0)
|| test_interp(c, 3, 1.f, 12)
|| test_interp(c, 3, 1.f, 2)
|| test_interp(c, 3, 1.f, 7)
|| test_interp(c, 3, 1.f, 19)
|| test_interp_align_corner(c, 3, 2.f, 0, 1)
|| test_interp_align_corner(c, 3, 0.5f, 0, 1)
|| test_interp_align_corner(c, 3, 1.2f, 0, 1)
|| test_interp_align_corner(c, 3, 0.8f, 0, 1)
|| test_interp_align_corner(c, 3, 1.f, 12, 1)
|| test_interp_align_corner(c, 3, 1.f, 2, 1)
|| test_interp_align_corner(c, 3, 1.f, 7, 1)
|| test_interp_align_corner(c, 3, 1.f, 19, 1)
|| test_interp_ref(c, 3, 2)
|| test_interp_ref(c, 3, 7)
|| test_interp_ref(c, 3, 19);
}
static int test_interp_6()
{
ncnn::Mat a = RandomMat(17);
ncnn::Mat b = RandomMat(20);
ncnn::Mat c = RandomMat(48);
return 0
|| test_interp(a, 1, 2.f, 3.f, 0, 0)
|| test_interp(a, 1, 1.f, 1.f, 10, 12)
|| test_interp(a, 1, 1.f, 1.f, 15, 16)
|| test_interp_ref(a, 1, 10, 12)
|| test_interp_ref(a, 1, 4, 4)
|| test_interp_ref(a, 1, 15, 16)
|| test_interp(b, 1, 4.f, 5.f, 0, 0)
|| test_interp(b, 1, 1.f, 1.f, 10, 12)
|| test_interp(b, 1, 1.f, 1.f, 14, 17)
|| test_interp_ref(b, 1, 5, 5)
|| test_interp_ref(b, 1, 14, 17)
|| test_interp(c, 1, 6.f, 7.f, 0, 0)
|| test_interp(c, 1, 1.f, 1.f, 10, 12)
|| test_interp(c, 1, 1.f, 1.f, 14, 17)
|| test_interp_ref(c, 1, 6, 6)
|| test_interp_ref(c, 1, 14, 17);
}
int main()
{
SRAND(7767517);
return 0
|| test_interp_0()
|| test_interp_1()
|| test_interp_2()
|| test_interp_3()
|| test_interp_4()
|| test_interp_5()
|| test_interp_6();
}

77
3rdparty/ncnn/tests/test_layernorm.cpp vendored Normal file
View File

@ -0,0 +1,77 @@
// 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/layernorm.h"
#include "testutil.h"
static int test_layernorm(const ncnn::Mat& a, float eps, int affine)
{
int affine_size = a.dims == 2 ? a.w : a.w * a.h;
ncnn::ParamDict pd;
pd.set(0, affine_size);
pd.set(1, eps);
pd.set(2, affine);
std::vector<ncnn::Mat> weights(2);
weights[0] = RandomMat(affine_size);
weights[1] = RandomMat(affine_size);
int ret = test_layer<ncnn::LayerNorm>("LayerNorm", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_layernorm failed a.dims=%d a=(%d %d %d) eps=%f affine=%d\n", a.dims, a.w, a.h, a.c, eps, affine);
}
return ret;
}
static int test_layernorm_0()
{
return 0
|| test_layernorm(RandomMat(6, 4, 2), 0.01f, 0)
|| test_layernorm(RandomMat(4, 5, 6), 0.01f, 0)
|| test_layernorm(RandomMat(3, 3, 8), 0.002f, 0)
|| test_layernorm(RandomMat(5, 6, 12), 0.02f, 0)
|| test_layernorm(RandomMat(6, 7, 24), 0.001f, 0)
|| test_layernorm(RandomMat(6, 4, 2), 0.01f, 1)
|| test_layernorm(RandomMat(4, 5, 6), 0.01f, 1)
|| test_layernorm(RandomMat(3, 3, 8), 0.002f, 1)
|| test_layernorm(RandomMat(5, 6, 12), 0.02f, 1)
|| test_layernorm(RandomMat(6, 7, 24), 0.001f, 1);
}
static int test_layernorm_1()
{
return 0
|| test_layernorm(RandomMat(4, 2), 0.01f, 0)
|| test_layernorm(RandomMat(5, 6), 0.01f, 0)
|| test_layernorm(RandomMat(3, 8), 0.002f, 0)
|| test_layernorm(RandomMat(6, 12), 0.02f, 0)
|| test_layernorm(RandomMat(7, 24), 0.001f, 0)
|| test_layernorm(RandomMat(4, 2), 0.01f, 1)
|| test_layernorm(RandomMat(5, 6), 0.01f, 1)
|| test_layernorm(RandomMat(3, 8), 0.002f, 1)
|| test_layernorm(RandomMat(6, 12), 0.02f, 1)
|| test_layernorm(RandomMat(7, 24), 0.001f, 1);
}
int main()
{
SRAND(7767517);
return 0
|| test_layernorm_0()
|| test_layernorm_1();
}

79
3rdparty/ncnn/tests/test_lrn.cpp vendored Normal file
View File

@ -0,0 +1,79 @@
// 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/lrn.h"
#include "testutil.h"
static int test_lrn(const ncnn::Mat& a, int region_type, int local_size, float alpha, float beta, float bias)
{
ncnn::ParamDict pd;
pd.set(0, region_type);
pd.set(1, local_size);
pd.set(2, alpha);
pd.set(3, beta);
pd.set(4, bias);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::LRN>("LRN", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_lrn failed a.dims=%d a=(%d %d %d) region_type=%d local_size=%d alpha=%f beta=%f bias=%f\n", a.dims, a.w, a.h, a.c, region_type, local_size, alpha, beta, bias);
}
return ret;
}
static int test_lrn_0()
{
ncnn::Mat a = RandomMat(11, 7, 12);
return 0
|| test_lrn(a, 0, 1, 1.f, 0.75f, 1.f)
|| test_lrn(a, 0, 5, 2.f, 0.12f, 1.33f)
|| test_lrn(a, 1, 1, 0.6f, 0.4f, 2.4f)
|| test_lrn(a, 1, 3, 1.f, 0.75f, 0.5f);
}
static int test_lrn_1()
{
ncnn::Mat a = RandomMat(10, 8, 16);
return 0
|| test_lrn(a, 0, 1, 1.f, 0.75f, 1.f)
|| test_lrn(a, 0, 5, 2.f, 0.12f, 1.33f)
|| test_lrn(a, 1, 1, 0.6f, 0.4f, 2.4f)
|| test_lrn(a, 1, 3, 1.f, 0.75f, 0.5f);
}
static int test_lrn_2()
{
ncnn::Mat a = RandomMat(12, 10, 9);
return 0
|| test_lrn(a, 0, 1, 1.f, 0.75f, 1.f)
|| test_lrn(a, 0, 5, 2.f, 0.12f, 1.33f)
|| test_lrn(a, 1, 1, 0.6f, 0.4f, 2.4f)
|| test_lrn(a, 1, 3, 1.f, 0.75f, 0.5f);
}
int main()
{
SRAND(7767517);
return 0
|| test_lrn_0()
|| test_lrn_1()
|| test_lrn_2();
}

263
3rdparty/ncnn/tests/test_lstm.cpp vendored Normal file
View File

@ -0,0 +1,263 @@
// 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/lstm.h"
#include "testutil.h"
static int test_lstm(const ncnn::Mat& a, int outch, int direction)
{
int input_size = a.w;
int num_directions = direction == 2 ? 2 : 1;
ncnn::ParamDict pd;
pd.set(0, outch);
pd.set(1, outch * input_size * 4 * num_directions);
pd.set(2, direction);
std::vector<ncnn::Mat> weights(3);
weights[0] = RandomMat(outch * input_size * 4 * num_directions);
weights[1] = RandomMat(outch * 4 * num_directions);
weights[2] = RandomMat(outch * outch * 4 * num_directions);
int ret = test_layer<ncnn::LSTM>("LSTM", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_lstm failed a.dims=%d a=(%d %d %d) outch=%d, direction = %d \n", a.dims, a.w, a.h, a.c, outch, direction);
}
return ret;
}
int test_lstm_layer_with_hidden(const ncnn::Mat& a, int outch, int direction)
{
int input_size = a.w;
int num_directions = direction == 2 ? 2 : 1;
ncnn::ParamDict pd;
pd.set(0, outch);
pd.set(1, outch * input_size * 4 * num_directions);
pd.set(2, direction);
std::vector<ncnn::Mat> weights(3);
weights[0] = RandomMat(outch * input_size * 4 * num_directions);
weights[1] = RandomMat(outch * 4 * num_directions);
weights[2] = RandomMat(outch * outch * 4 * num_directions);
// initial hidden state
ncnn::Mat hidden = RandomMat(outch, num_directions);
// initial cell state
ncnn::Mat cell = RandomMat(outch, num_directions);
std::vector<ncnn::Mat> as(3);
as[0] = a;
as[1] = hidden;
as[2] = cell;
int ret = test_layer<ncnn::LSTM>("LSTM", pd, weights, as, 3);
if (ret != 0)
{
fprintf(stderr, "test_lstm_layer_with_hidden failed a.dims=%d a=(%d %d %d) outch=%d, direction = %d \n", a.dims, a.w, a.h, a.c, outch, direction);
}
return ret;
}
int test_lstm_layer_with_hidden_input(const ncnn::Mat& a, int outch, int direction)
{
int input_size = a.w;
int num_directions = direction == 2 ? 2 : 1;
ncnn::ParamDict pd;
pd.set(0, outch);
pd.set(1, outch * input_size * 4 * num_directions);
pd.set(2, direction);
std::vector<ncnn::Mat> weights(3);
weights[0] = RandomMat(outch * input_size * 4 * num_directions);
weights[1] = RandomMat(outch * 4 * num_directions);
weights[2] = RandomMat(outch * outch * 4 * num_directions);
// initial hidden state
ncnn::Mat hidden = RandomMat(outch, num_directions);
// initial cell state
ncnn::Mat cell = RandomMat(outch, num_directions);
std::vector<ncnn::Mat> as(3);
as[0] = a;
as[1] = hidden;
as[2] = cell;
int ret = test_layer<ncnn::LSTM>("LSTM", pd, weights, as, 1);
if (ret != 0)
{
fprintf(stderr, "test_lstm_layer_with_hidden_input failed a.dims=%d a=(%d %d %d) outch=%d, direction = %d \n", a.dims, a.w, a.h, a.c, outch, direction);
}
return ret;
}
int test_lstm_layer_with_hidden_output(const ncnn::Mat& a, int outch, int direction)
{
int input_size = a.w;
int num_directions = direction == 2 ? 2 : 1;
ncnn::ParamDict pd;
pd.set(0, outch);
pd.set(1, outch * input_size * 4 * num_directions);
pd.set(2, direction);
std::vector<ncnn::Mat> weights(3);
weights[0] = RandomMat(outch * input_size * 4 * num_directions);
weights[1] = RandomMat(outch * 4 * num_directions);
weights[2] = RandomMat(outch * outch * 4 * num_directions);
std::vector<ncnn::Mat> as(1);
as[0] = a;
int ret = test_layer<ncnn::LSTM>("LSTM", pd, weights, as, 3);
if (ret != 0)
{
fprintf(stderr, "test_lstm_layer_with_hidden_output failed a.dims=%d a=(%d %d %d) outch=%d, direction = %d \n", a.dims, a.w, a.h, a.c, outch, direction);
}
return ret;
}
static int test_lstm_0()
{
return 0
|| test_lstm(RandomMat(4, 1), 2, 2)
|| test_lstm(RandomMat(8, 2), 2, 2)
|| test_lstm(RandomMat(16, 8), 7, 2)
|| test_lstm(RandomMat(17, 8), 8, 2)
|| test_lstm(RandomMat(19, 15), 8, 2)
|| test_lstm(RandomMat(5, 16), 16, 2)
|| test_lstm(RandomMat(3, 16), 8, 2)
|| test_lstm(RandomMat(8, 16), 16, 2)
|| test_lstm(RandomMat(2, 5), 17, 2);
}
static int test_lstm_1()
{
return 0
|| test_lstm_layer_with_hidden(RandomMat(4, 4), 1, 2)
|| test_lstm_layer_with_hidden(RandomMat(8, 2), 2, 2)
|| test_lstm_layer_with_hidden(RandomMat(16, 8), 7, 2)
|| test_lstm_layer_with_hidden(RandomMat(17, 8), 8, 2)
|| test_lstm_layer_with_hidden(RandomMat(19, 15), 8, 2)
|| test_lstm_layer_with_hidden(RandomMat(5, 16), 16, 2)
|| test_lstm_layer_with_hidden(RandomMat(3, 16), 8, 2)
|| test_lstm_layer_with_hidden(RandomMat(2, 5), 99, 2)
|| test_lstm_layer_with_hidden(RandomMat(4, 4), 1, 1)
|| test_lstm_layer_with_hidden(RandomMat(8, 2), 2, 1)
|| test_lstm_layer_with_hidden(RandomMat(16, 8), 7, 1)
|| test_lstm_layer_with_hidden(RandomMat(17, 8), 8, 1)
|| test_lstm_layer_with_hidden(RandomMat(19, 15), 8, 1)
|| test_lstm_layer_with_hidden(RandomMat(5, 16), 16, 1)
|| test_lstm_layer_with_hidden(RandomMat(3, 16), 8, 1)
|| test_lstm_layer_with_hidden(RandomMat(2, 5), 99, 1)
|| test_lstm_layer_with_hidden(RandomMat(4, 2), 1, 0)
|| test_lstm_layer_with_hidden(RandomMat(8, 2), 2, 0)
|| test_lstm_layer_with_hidden(RandomMat(16, 8), 7, 0)
|| test_lstm_layer_with_hidden(RandomMat(17, 8), 8, 0)
|| test_lstm_layer_with_hidden(RandomMat(19, 15), 8, 0)
|| test_lstm_layer_with_hidden(RandomMat(5, 16), 16, 0)
|| test_lstm_layer_with_hidden(RandomMat(3, 16), 8, 0)
|| test_lstm_layer_with_hidden(RandomMat(2, 5), 17, 0)
|| test_lstm_layer_with_hidden_input(RandomMat(4, 4), 1, 2)
|| test_lstm_layer_with_hidden_input(RandomMat(8, 2), 2, 2)
|| test_lstm_layer_with_hidden_input(RandomMat(16, 8), 7, 2)
|| test_lstm_layer_with_hidden_input(RandomMat(17, 8), 8, 2)
|| test_lstm_layer_with_hidden_input(RandomMat(19, 15), 8, 2)
|| test_lstm_layer_with_hidden_input(RandomMat(5, 16), 16, 2)
|| test_lstm_layer_with_hidden_input(RandomMat(3, 16), 8, 2)
|| test_lstm_layer_with_hidden_input(RandomMat(2, 5), 99, 2)
|| test_lstm_layer_with_hidden_input(RandomMat(4, 4), 1, 1)
|| test_lstm_layer_with_hidden_input(RandomMat(8, 2), 2, 1)
|| test_lstm_layer_with_hidden_input(RandomMat(16, 8), 7, 1)
|| test_lstm_layer_with_hidden_input(RandomMat(17, 8), 8, 1)
|| test_lstm_layer_with_hidden_input(RandomMat(19, 15), 8, 1)
|| test_lstm_layer_with_hidden_input(RandomMat(5, 16), 16, 1)
|| test_lstm_layer_with_hidden_input(RandomMat(3, 16), 8, 1)
|| test_lstm_layer_with_hidden_input(RandomMat(2, 5), 99, 1)
|| test_lstm_layer_with_hidden_input(RandomMat(4, 2), 1, 0)
|| test_lstm_layer_with_hidden_input(RandomMat(8, 2), 2, 0)
|| test_lstm_layer_with_hidden_input(RandomMat(16, 8), 7, 0)
|| test_lstm_layer_with_hidden_input(RandomMat(17, 8), 8, 0)
|| test_lstm_layer_with_hidden_input(RandomMat(19, 15), 8, 0)
|| test_lstm_layer_with_hidden_input(RandomMat(5, 16), 16, 0)
|| test_lstm_layer_with_hidden_input(RandomMat(3, 16), 8, 0)
|| test_lstm_layer_with_hidden_input(RandomMat(2, 5), 17, 0)
|| test_lstm_layer_with_hidden_output(RandomMat(4, 4), 1, 2)
|| test_lstm_layer_with_hidden_output(RandomMat(8, 2), 2, 2)
|| test_lstm_layer_with_hidden_output(RandomMat(16, 8), 7, 2)
|| test_lstm_layer_with_hidden_output(RandomMat(17, 8), 8, 2)
|| test_lstm_layer_with_hidden_output(RandomMat(19, 15), 8, 2)
|| test_lstm_layer_with_hidden_output(RandomMat(5, 16), 16, 2)
|| test_lstm_layer_with_hidden_output(RandomMat(3, 16), 8, 2)
|| test_lstm_layer_with_hidden_output(RandomMat(2, 5), 99, 2)
|| test_lstm_layer_with_hidden_output(RandomMat(4, 4), 1, 1)
|| test_lstm_layer_with_hidden_output(RandomMat(8, 2), 2, 1)
|| test_lstm_layer_with_hidden_output(RandomMat(16, 8), 7, 1)
|| test_lstm_layer_with_hidden_output(RandomMat(17, 8), 8, 1)
|| test_lstm_layer_with_hidden_output(RandomMat(19, 15), 8, 1)
|| test_lstm_layer_with_hidden_output(RandomMat(5, 16), 16, 1)
|| test_lstm_layer_with_hidden_output(RandomMat(3, 16), 8, 1)
|| test_lstm_layer_with_hidden_output(RandomMat(2, 5), 99, 1)
|| test_lstm_layer_with_hidden_output(RandomMat(4, 2), 1, 0)
|| test_lstm_layer_with_hidden_output(RandomMat(8, 2), 2, 0)
|| test_lstm_layer_with_hidden_output(RandomMat(16, 8), 7, 0)
|| test_lstm_layer_with_hidden_output(RandomMat(17, 8), 8, 0)
|| test_lstm_layer_with_hidden_output(RandomMat(19, 15), 8, 0)
|| test_lstm_layer_with_hidden_output(RandomMat(5, 16), 16, 0)
|| test_lstm_layer_with_hidden_output(RandomMat(3, 16), 8, 0)
|| test_lstm_layer_with_hidden_output(RandomMat(2, 5), 17, 0);
}
static int test_lstm_2()
{
return 0
|| test_lstm(RandomMat(4, 1), 1, 0)
|| test_lstm(RandomMat(8, 2), 2, 0)
|| test_lstm(RandomMat(16, 8), 7, 0)
|| test_lstm(RandomMat(17, 8), 8, 0)
|| test_lstm(RandomMat(19, 15), 8, 0)
|| test_lstm(RandomMat(5, 16), 16, 0)
|| test_lstm(RandomMat(3, 16), 8, 0)
|| test_lstm(RandomMat(8, 16), 16, 0)
|| test_lstm(RandomMat(2, 5), 17, 0);
}
static int test_lstm_3()
{
return 0
|| test_lstm(RandomMat(4, 1), 1, 1)
|| test_lstm(RandomMat(8, 2), 2, 1)
|| test_lstm(RandomMat(16, 8), 7, 1)
|| test_lstm(RandomMat(17, 8), 8, 1)
|| test_lstm(RandomMat(19, 15), 8, 1)
|| test_lstm(RandomMat(5, 16), 16, 1)
|| test_lstm(RandomMat(3, 16), 8, 1)
|| test_lstm(RandomMat(8, 16), 16, 1)
|| test_lstm(RandomMat(2, 5), 17, 1);
}
int main()
{
SRAND(7767517);
return 0 || test_lstm_0() || test_lstm_1() || test_lstm_2() || test_lstm_3();
}

454
3rdparty/ncnn/tests/test_mat_pixel.cpp vendored Normal file
View File

@ -0,0 +1,454 @@
// 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 "mat.h"
#include "prng.h"
#include <string.h>
static struct prng_rand_t g_prng_rand_state;
#define SRAND(seed) prng_srand(seed, &g_prng_rand_state)
#define RAND() prng_rand(&g_prng_rand_state)
static ncnn::Mat RandomMat(int w, int h, int elempack)
{
ncnn::Mat m(w, h, (size_t)elempack, elempack);
unsigned char* p = m;
for (int i = 0; i < w * h * elempack; i++)
{
p[i] = RAND() % 256;
}
return m;
}
static ncnn::Mat FilledMat(int w, int h, int elempack, unsigned char v)
{
ncnn::Mat m(w, h, (size_t)elempack, elempack);
unsigned char* p = m;
for (int i = 0; i < w * h * elempack; i++)
{
p[i] = v;
}
return m;
}
static int test_mat_pixel_gray(int w, int h)
{
int pixel_type_from[5] = {ncnn::Mat::PIXEL_GRAY, ncnn::Mat::PIXEL_GRAY2RGB, ncnn::Mat::PIXEL_GRAY2BGR, ncnn::Mat::PIXEL_GRAY2RGBA, ncnn::Mat::PIXEL_GRAY2BGRA};
int pixel_type_to[5] = {ncnn::Mat::PIXEL_GRAY, ncnn::Mat::PIXEL_RGB2GRAY, ncnn::Mat::PIXEL_BGR2GRAY, ncnn::Mat::PIXEL_RGBA2GRAY, ncnn::Mat::PIXEL_BGRA2GRAY};
ncnn::Mat a = RandomMat(w, h, 1);
// FIXME enable more convert types
for (int i = 0; i < 1; i++)
{
ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
ncnn::Mat b(w, h, (size_t)1u, 1);
m.to_pixels(b, pixel_type_to[i]);
if (memcmp(a, b, w * h * 1) != 0)
{
fprintf(stderr, "test_mat_pixel_gray failed w=%d h=%d pixel_type=%d\n", w, h, i);
return -1;
}
}
return 0;
}
static int test_mat_pixel_rgb(int w, int h)
{
int pixel_type_from[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGB2RGBA, ncnn::Mat::PIXEL_RGB2BGRA};
int pixel_type_to[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_RGBA2RGB, ncnn::Mat::PIXEL_BGRA2RGB};
ncnn::Mat a = RandomMat(w, h, 3);
// FIXME enable more convert types
for (int i = 0; i < 2; i++)
{
ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
ncnn::Mat b(w, h, (size_t)3u, 3);
m.to_pixels(b, pixel_type_to[i]);
if (memcmp(a, b, w * h * 3) != 0)
{
fprintf(stderr, "test_mat_pixel_rgb failed w=%d h=%d pixel_type=%d\n", w, h, i);
return -1;
}
}
return 0;
}
static int test_mat_pixel_bgr(int w, int h)
{
int pixel_type_from[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_BGR2RGBA, ncnn::Mat::PIXEL_BGR2BGRA};
int pixel_type_to[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGBA2BGR, ncnn::Mat::PIXEL_BGRA2BGR};
ncnn::Mat a = RandomMat(w, h, 3);
// FIXME enable more convert types
for (int i = 0; i < 2; i++)
{
ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
ncnn::Mat b(w, h, (size_t)3u, 3);
m.to_pixels(b, pixel_type_to[i]);
if (memcmp(a, b, w * h * 3) != 0)
{
fprintf(stderr, "test_mat_pixel_bgr failed w=%d h=%d pixel_type=%d\n", w, h, i);
return -1;
}
}
return 0;
}
static int test_mat_pixel_rgba(int w, int h)
{
int pixel_type_from[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_RGBA2BGRA};
int pixel_type_to[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_BGRA2RGBA};
ncnn::Mat a = RandomMat(w, h, 4);
for (int i = 0; i < 2; i++)
{
ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
ncnn::Mat b(w, h, (size_t)4u, 4);
m.to_pixels(b, pixel_type_to[i]);
if (memcmp(a, b, w * h * 4) != 0)
{
fprintf(stderr, "test_mat_pixel_rgba failed w=%d h=%d pixel_type=%d\n", w, h, i);
return -1;
}
}
return 0;
}
static int test_mat_pixel_bgra(int w, int h)
{
int pixel_type_from[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_BGRA2RGBA};
int pixel_type_to[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_RGBA2BGRA};
ncnn::Mat a = RandomMat(w, h, 4);
for (int i = 0; i < 2; i++)
{
ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
ncnn::Mat b(w, h, (size_t)4u, 4);
m.to_pixels(b, pixel_type_to[i]);
if (memcmp(a, b, w * h * 4) != 0)
{
fprintf(stderr, "test_mat_pixel_bgra failed w=%d h=%d pixel_type=%d\n", w, h, i);
return -1;
}
}
return 0;
}
static int test_mat_pixel_roi_gray(int w, int h, int roix, int roiy, int roiw, int roih)
{
ncnn::Option opt;
opt.num_threads = 1;
int pixel_type_from[5] = {ncnn::Mat::PIXEL_GRAY, ncnn::Mat::PIXEL_GRAY2RGB, ncnn::Mat::PIXEL_GRAY2BGR, ncnn::Mat::PIXEL_GRAY2RGBA, ncnn::Mat::PIXEL_GRAY2BGRA};
int pixel_type_to[5] = {ncnn::Mat::PIXEL_GRAY, ncnn::Mat::PIXEL_RGB2GRAY, ncnn::Mat::PIXEL_BGR2GRAY, ncnn::Mat::PIXEL_RGBA2GRAY, ncnn::Mat::PIXEL_BGRA2GRAY};
ncnn::Mat a = RandomMat(w, h, 1);
ncnn::Mat a2;
ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
// FIXME enable more convert types
for (int i = 0; i < 1; i++)
{
ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
ncnn::Mat b(roiw, roih, (size_t)1u, 1);
m.to_pixels(b, pixel_type_to[i]);
ncnn::Mat b2;
ncnn::Mat c2;
ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
ncnn::convert_packing(b2, c2, 1, opt);
if (memcmp(b, c2, roiw * roih * 1) != 0)
{
fprintf(stderr, "test_mat_pixel_roi_gray failed w=%d h=%d roi=[%d %d %d %d] pixel_type=%d\n", w, h, roix, roiy, roiw, roih, i);
return -1;
}
}
return 0;
}
static int test_mat_pixel_roi_rgb(int w, int h, int roix, int roiy, int roiw, int roih)
{
ncnn::Option opt;
opt.num_threads = 1;
int pixel_type_from[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGB2RGBA, ncnn::Mat::PIXEL_RGB2BGRA};
int pixel_type_to[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_RGBA2RGB, ncnn::Mat::PIXEL_BGRA2RGB};
ncnn::Mat a = RandomMat(w, h, 3);
ncnn::Mat a2;
ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
// FIXME enable more convert types
for (int i = 0; i < 2; i++)
{
ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
ncnn::Mat b(roiw, roih, (size_t)3u, 3);
m.to_pixels(b, pixel_type_to[i]);
ncnn::Mat b2;
ncnn::Mat c2;
ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
ncnn::convert_packing(b2, c2, 3, opt);
if (memcmp(b, c2, roiw * roih * 3) != 0)
{
fprintf(stderr, "test_mat_pixel_roi_rgb failed w=%d h=%d roi=[%d %d %d %d] pixel_type=%d\n", w, h, roix, roiy, roiw, roih, i);
return -1;
}
}
return 0;
}
static int test_mat_pixel_roi_bgr(int w, int h, int roix, int roiy, int roiw, int roih)
{
ncnn::Option opt;
opt.num_threads = 1;
int pixel_type_from[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_BGR2RGBA, ncnn::Mat::PIXEL_BGR2BGRA};
int pixel_type_to[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGBA2BGR, ncnn::Mat::PIXEL_BGRA2BGR};
ncnn::Mat a = RandomMat(w, h, 3);
ncnn::Mat a2;
ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
// FIXME enable more convert types
for (int i = 0; i < 2; i++)
{
ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
ncnn::Mat b(roiw, roih, (size_t)3u, 3);
m.to_pixels(b, pixel_type_to[i]);
ncnn::Mat b2;
ncnn::Mat c2;
ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
ncnn::convert_packing(b2, c2, 3, opt);
if (memcmp(b, c2, roiw * roih * 3) != 0)
{
fprintf(stderr, "test_mat_pixel_roi_bgr failed w=%d h=%d roi=[%d %d %d %d] pixel_type=%d\n", w, h, roix, roiy, roiw, roih, i);
return -1;
}
}
return 0;
}
static int test_mat_pixel_roi_rgba(int w, int h, int roix, int roiy, int roiw, int roih)
{
ncnn::Option opt;
opt.num_threads = 1;
int pixel_type_from[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_RGBA2BGRA};
int pixel_type_to[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_BGRA2RGBA};
ncnn::Mat a = RandomMat(w, h, 4);
ncnn::Mat a2;
ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
for (int i = 0; i < 2; i++)
{
ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
ncnn::Mat b(roiw, roih, (size_t)4u, 4);
m.to_pixels(b, pixel_type_to[i]);
ncnn::Mat b2;
ncnn::Mat c2;
ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
ncnn::convert_packing(b2, c2, 4, opt);
if (memcmp(b, c2, roiw * roih * 4) != 0)
{
fprintf(stderr, "test_mat_pixel_roi_rgba failed w=%d h=%d roi=[%d %d %d %d] pixel_type=%d\n", w, h, roix, roiy, roiw, roih, i);
return -1;
}
}
return 0;
}
static int test_mat_pixel_roi_bgra(int w, int h, int roix, int roiy, int roiw, int roih)
{
ncnn::Option opt;
opt.num_threads = 1;
int pixel_type_from[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_BGRA2RGBA};
int pixel_type_to[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_RGBA2BGRA};
ncnn::Mat a = RandomMat(w, h, 4);
ncnn::Mat a2;
ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
for (int i = 0; i < 2; i++)
{
ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
ncnn::Mat b(roiw, roih, (size_t)4u, 4);
m.to_pixels(b, pixel_type_to[i]);
ncnn::Mat b2;
ncnn::Mat c2;
ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
ncnn::convert_packing(b2, c2, 4, opt);
if (memcmp(b, c2, roiw * roih * 4) != 0)
{
fprintf(stderr, "test_mat_pixel_roi_bgra failed w=%d h=%d roi=[%d %d %d %d] pixel_type=%d\n", w, h, roix, roiy, roiw, roih, i);
return -1;
}
}
return 0;
}
static int test_mat_pixel_yuv420sp2rgb(int w, int h)
{
ncnn::Mat nv21 = RandomMat(w, h / 2 * 3, 1);
ncnn::Mat nv12 = nv21.clone();
// swap VU to UV
unsigned char* p = (unsigned char*)nv12 + w * h;
for (int i = 0; i < w * h / 4; i++)
{
unsigned char v = p[0];
unsigned char u = p[1];
p[0] = u;
p[1] = v;
p += 2;
}
ncnn::Mat rgb(w, h, (size_t)3u, 3);
yuv420sp2rgb(nv21, w, h, rgb);
ncnn::Mat rgb2(w, h, (size_t)3u, 3);
yuv420sp2rgb_nv12(nv12, w, h, rgb2);
if (memcmp(rgb, rgb2, w * h * 3) != 0)
{
fprintf(stderr, "test_mat_pixel_yuv420sp2rgb failed w=%d h=%d\n", w, h);
return -1;
}
return 0;
}
static int test_mat_pixel_0()
{
return 0
|| test_mat_pixel_gray(16, 16)
|| test_mat_pixel_rgb(16, 16)
|| test_mat_pixel_bgr(16, 16)
|| test_mat_pixel_rgba(16, 16)
|| test_mat_pixel_bgra(16, 16);
}
static int test_mat_pixel_1()
{
return 0
|| test_mat_pixel_gray(15, 15)
|| test_mat_pixel_rgb(15, 15)
|| test_mat_pixel_bgr(15, 15)
|| test_mat_pixel_rgba(15, 15)
|| test_mat_pixel_bgra(15, 15);
}
static int test_mat_pixel_2()
{
return 0
|| test_mat_pixel_gray(1, 1)
|| test_mat_pixel_rgb(1, 1)
|| test_mat_pixel_bgr(1, 1)
|| test_mat_pixel_rgba(1, 1)
|| test_mat_pixel_bgra(1, 1);
}
static int test_mat_pixel_3()
{
return 0
|| test_mat_pixel_gray(3, 3)
|| test_mat_pixel_rgb(3, 3)
|| test_mat_pixel_bgr(3, 3)
|| test_mat_pixel_rgba(3, 3)
|| test_mat_pixel_bgra(3, 3);
}
static int test_mat_pixel_4()
{
return 0
|| test_mat_pixel_roi_gray(16, 16, 1, 1, 13, 13)
|| test_mat_pixel_roi_rgb(16, 16, 2, 1, 11, 11)
|| test_mat_pixel_roi_bgr(16, 16, 1, 2, 11, 9)
|| test_mat_pixel_roi_rgba(16, 16, 3, 2, 9, 11)
|| test_mat_pixel_roi_bgra(16, 16, 2, 3, 9, 7);
}
static int test_mat_pixel_5()
{
return 0
|| test_mat_pixel_roi_gray(15, 15, 2, 3, 2, 3)
|| test_mat_pixel_roi_rgb(15, 15, 3, 4, 5, 4)
|| test_mat_pixel_roi_bgr(15, 15, 4, 5, 6, 7)
|| test_mat_pixel_roi_rgba(15, 15, 6, 6, 3, 1)
|| test_mat_pixel_roi_bgra(15, 15, 7, 3, 1, 1);
}
static int test_mat_pixel_6()
{
return 0
|| test_mat_pixel_yuv420sp2rgb(16, 16)
|| test_mat_pixel_yuv420sp2rgb(12, 12)
|| test_mat_pixel_yuv420sp2rgb(2, 2)
|| test_mat_pixel_yuv420sp2rgb(6, 6);
}
int main()
{
SRAND(7767517);
return 0
|| test_mat_pixel_0()
|| test_mat_pixel_1()
|| test_mat_pixel_2()
|| test_mat_pixel_3()
|| test_mat_pixel_4()
|| test_mat_pixel_5()
|| test_mat_pixel_6();
}

View File

@ -0,0 +1,507 @@
// 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 "mat.h"
#include "prng.h"
#include <math.h>
#include <string.h>
static struct prng_rand_t g_prng_rand_state;
#define SRAND(seed) prng_srand(seed, &g_prng_rand_state)
#define RAND() prng_rand(&g_prng_rand_state)
static ncnn::Mat generate_ncnn_logo(int w, int h)
{
// clang-format off
// *INDENT-OFF*
static const unsigned char ncnn_logo_data[16][16] =
{
{245, 245, 33, 245, 245, 245, 245, 245, 245, 245, 245, 245, 245, 33, 245, 245},
{245, 33, 33, 33, 245, 245, 245, 245, 245, 245, 245, 245, 33, 33, 33, 245},
{245, 33, 158, 158, 33, 245, 245, 245, 245, 245, 245, 33, 158, 158, 33, 245},
{ 33, 117, 158, 224, 158, 33, 245, 245, 245, 245, 33, 158, 224, 158, 117, 33},
{ 33, 117, 224, 224, 224, 66, 33, 33, 33, 33, 66, 224, 224, 224, 117, 33},
{ 33, 189, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 189, 33},
{ 33, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 33},
{ 33, 224, 224, 97, 97, 97, 97, 224, 224, 97, 97, 97, 97, 224, 224, 33},
{ 33, 224, 224, 97, 33, 0, 189, 224, 224, 97, 0, 33, 97, 224, 224, 33},
{ 33, 224, 224, 97, 33, 0, 189, 224, 224, 97, 0, 33, 97, 224, 224, 33},
{ 33, 224, 224, 97, 97, 97, 97, 224, 224, 97, 189, 189, 97, 224, 224, 33},
{ 33, 66, 66, 66, 224, 224, 224, 224, 224, 224, 224, 224, 66, 66, 66, 33},
{ 66, 158, 158, 66, 66, 224, 224, 224, 224, 224, 224, 66, 158, 158, 66, 66},
{ 66, 158, 158, 208, 66, 224, 224, 224, 224, 224, 224, 66, 158, 158, 208, 66},
{ 66, 224, 202, 158, 66, 224, 224, 224, 224, 224, 224, 66, 224, 202, 158, 66},
{ 66, 158, 224, 158, 66, 224, 224, 224, 224, 224, 224, 66, 158, 224, 158, 66}
};
// *INDENT-ON*
// clang-format on
ncnn::Mat m(w, h, (size_t)1, 1);
resize_bilinear_c1((const unsigned char*)ncnn_logo_data, 16, 16, m, w, h);
return m;
}
static ncnn::Mat RandomMat(int w, int h, int elempack)
{
ncnn::Mat image = generate_ncnn_logo(w, h);
ncnn::Mat m(w, h, 1, (size_t)elempack, elempack);
for (int i = 0; i < h; i++)
{
unsigned char* p = m.row<unsigned char>(i);
const unsigned char* pb = image.row<const unsigned char>(i);
for (int j = 0; j < w; j++)
{
for (int k = 0; k < elempack; k++)
{
p[k] = pb[0];
}
p += elempack;
pb += 1;
}
}
return m;
}
static bool NearlyEqual(unsigned char a, unsigned char b)
{
return abs(a - b) <= 10;
}
static int CompareNearlyEqual(const ncnn::Mat& a, const ncnn::Mat& b)
{
for (int i = 0; i < a.h; i++)
{
const unsigned char* pa = a.row<const unsigned char>(i);
const unsigned char* pb = b.row<const unsigned char>(i);
for (int j = 0; j < a.w; j++)
{
for (int k = 0; k < a.elempack; k++)
{
if (!NearlyEqual(pa[k], pb[k]))
{
fprintf(stderr, "value not match at h:%d w:%d [%d] expect %d but got %d\n", i, j, k, pa[k], pb[k]);
return -1;
}
}
pa += a.elempack;
pb += a.elempack;
}
}
return 0;
}
static int test_mat_pixel_affine_a(int w, int h)
{
for (int c = 1; c <= 4; c++)
{
ncnn::Mat a0 = RandomMat(w, h, c);
float tm[6];
float tm_inv[6];
ncnn::get_rotation_matrix(10.f, 0.15f, w / 2, h / 2, tm);
ncnn::invert_affine_transform(tm, tm_inv);
ncnn::Mat a1(w / 2, h / 2, (size_t)c, c);
ncnn::Mat a2 = a0.clone();
if (c == 1)
{
ncnn::warpaffine_bilinear_c1(a0, w, h, a1, w / 2, h / 2, tm, 0);
ncnn::warpaffine_bilinear_c1(a1, w / 2, h / 2, a2, w, h, tm_inv, -233);
}
if (c == 2)
{
ncnn::warpaffine_bilinear_c2(a0, w, h, a1, w / 2, h / 2, tm, 0);
ncnn::warpaffine_bilinear_c2(a1, w / 2, h / 2, a2, w, h, tm_inv, -233);
}
if (c == 3)
{
ncnn::warpaffine_bilinear_c3(a0, w, h, a1, w / 2, h / 2, tm, 0);
ncnn::warpaffine_bilinear_c3(a1, w / 2, h / 2, a2, w, h, tm_inv, -233);
}
if (c == 4)
{
ncnn::warpaffine_bilinear_c4(a0, w, h, a1, w / 2, h / 2, tm, 0);
ncnn::warpaffine_bilinear_c4(a1, w / 2, h / 2, a2, w, h, tm_inv, -233);
}
if (CompareNearlyEqual(a0, a2) != 0)
{
fprintf(stderr, "test_mat_pixel_affine_a failed w=%d h=%d c=%d\n", w, h, c);
return -1;
}
}
return 0;
}
static int test_mat_pixel_affine_b(int w, int h)
{
for (int c = 1; c <= 4; c++)
{
ncnn::Mat a0 = RandomMat(w, h, c);
float tm[6];
float tm_inv[6];
ncnn::get_rotation_matrix(20.f, 0.25f, w / 4, h / 4, tm);
ncnn::invert_affine_transform(tm, tm_inv);
ncnn::Mat a1(w / 4, h / 4, (size_t)c, c);
ncnn::Mat a2 = a0.clone();
if (c == 1)
{
ncnn::warpaffine_bilinear_c1(a0, w, h, a1, w / 4, h / 4, tm, 0);
ncnn::warpaffine_bilinear_c1(a1, w / 4, h / 4, a2, w, h, tm_inv, -233);
}
if (c == 2)
{
ncnn::warpaffine_bilinear_c2(a0, w, h, a1, w / 4, h / 4, tm, 0);
ncnn::warpaffine_bilinear_c2(a1, w / 4, h / 4, a2, w, h, tm_inv, -233);
}
if (c == 3)
{
ncnn::warpaffine_bilinear_c3(a0, w, h, a1, w / 4, h / 4, tm, 0);
ncnn::warpaffine_bilinear_c3(a1, w / 4, h / 4, a2, w, h, tm_inv, -233);
}
if (c == 4)
{
ncnn::warpaffine_bilinear_c4(a0, w, h, a1, w / 4, h / 4, tm, 0);
ncnn::warpaffine_bilinear_c4(a1, w / 4, h / 4, a2, w, h, tm_inv, -233);
}
if (CompareNearlyEqual(a0, a2) != 0)
{
fprintf(stderr, "test_mat_pixel_affine_b failed w=%d h=%d c=%d\n", w, h, c);
return -1;
}
}
return 0;
}
static int test_mat_pixel_affine_c(int w, int h)
{
for (int c = 1; c <= 4; c++)
{
ncnn::Mat a0 = RandomMat(w, h, c);
float tm[6];
float tm_inv[6];
ncnn::get_rotation_matrix(-30.f, 0.6f, w / 2, h / 2, tm);
ncnn::invert_affine_transform(tm, tm_inv);
ncnn::Mat a1(w / 2, h / 2, (size_t)c, c);
ncnn::Mat a2 = a0.clone();
if (c == 1)
{
ncnn::warpaffine_bilinear_c1(a0, w, h, a1, w / 2, h / 2, tm, 0);
ncnn::warpaffine_bilinear_c1(a1, w / 2, h / 2, a2, w, h, tm_inv, -233);
}
if (c == 2)
{
ncnn::warpaffine_bilinear_c2(a0, w, h, a1, w / 2, h / 2, tm, 0);
ncnn::warpaffine_bilinear_c2(a1, w / 2, h / 2, a2, w, h, tm_inv, -233);
}
if (c == 3)
{
ncnn::warpaffine_bilinear_c3(a0, w, h, a1, w / 2, h / 2, tm, 0);
ncnn::warpaffine_bilinear_c3(a1, w / 2, h / 2, a2, w, h, tm_inv, -233);
}
if (c == 4)
{
ncnn::warpaffine_bilinear_c4(a0, w, h, a1, w / 2, h / 2, tm, 0);
ncnn::warpaffine_bilinear_c4(a1, w / 2, h / 2, a2, w, h, tm_inv, -233);
}
if (CompareNearlyEqual(a0, a2) != 0)
{
fprintf(stderr, "test_mat_pixel_affine_c failed w=%d h=%d c=%d\n", w, h, c);
return -1;
}
}
return 0;
}
static int test_mat_pixel_affine_d(int w, int h)
{
for (int c = 1; c <= 4; c++)
{
ncnn::Mat a0 = RandomMat(w, h, c);
const float points_from[4] = {w / 8.f, h / 8.f, w / 8.f + 1.f, h / 8.f + 3.f};
const float points_to[4] = {w / 2.f, h / 2.f, w / 2.f + 2.f, h / 2.f};
float tm[6];
float tm_inv[6];
ncnn::get_affine_transform(points_from, points_to, 2, tm);
ncnn::invert_affine_transform(tm, tm_inv);
ncnn::Mat a1(w / 4, h / 4, (size_t)c, c);
ncnn::Mat a2 = a0.clone();
if (c == 1)
{
ncnn::warpaffine_bilinear_c1(a0, w, h, a1, w / 4, h / 4, tm, 0);
ncnn::warpaffine_bilinear_c1(a1, w / 4, h / 4, a2, w, h, tm_inv, -233);
}
if (c == 2)
{
ncnn::warpaffine_bilinear_c2(a0, w, h, a1, w / 4, h / 4, tm, 0);
ncnn::warpaffine_bilinear_c2(a1, w / 4, h / 4, a2, w, h, tm_inv, -233);
}
if (c == 3)
{
ncnn::warpaffine_bilinear_c3(a0, w, h, a1, w / 4, h / 4, tm, 0);
ncnn::warpaffine_bilinear_c3(a1, w / 4, h / 4, a2, w, h, tm_inv, -233);
}
if (c == 4)
{
ncnn::warpaffine_bilinear_c4(a0, w, h, a1, w / 4, h / 4, tm, 0);
ncnn::warpaffine_bilinear_c4(a1, w / 4, h / 4, a2, w, h, tm_inv, -233);
}
if (CompareNearlyEqual(a0, a2) != 0)
{
fprintf(stderr, "test_mat_pixel_affine_d failed w=%d h=%d c=%d\n", w, h, c);
return -1;
}
}
return 0;
}
static int test_mat_pixel_affine_e(int w, int h)
{
for (int c = 1; c <= 4; c++)
{
ncnn::Mat a0 = RandomMat(w, h, c);
float tm[6];
float tm_inv[6];
ncnn::get_rotation_matrix(-180.f, 0.5f, w / 2, h / 2, tm);
ncnn::invert_affine_transform(tm, tm_inv);
ncnn::Mat a1(w, h, (size_t)c, c);
ncnn::Mat a2 = a0.clone();
if (c == 1)
{
ncnn::warpaffine_bilinear_c1(a0, w, h, a1, w, h, tm, 0);
ncnn::warpaffine_bilinear_c1(a1, w, h, a2, w, h, tm_inv, -233);
}
if (c == 2)
{
ncnn::warpaffine_bilinear_c2(a0, w, h, a1, w, h, tm, 0);
ncnn::warpaffine_bilinear_c2(a1, w, h, a2, w, h, tm_inv, -233);
}
if (c == 3)
{
ncnn::warpaffine_bilinear_c3(a0, w, h, a1, w, h, tm, 0);
ncnn::warpaffine_bilinear_c3(a1, w, h, a2, w, h, tm_inv, -233);
}
if (c == 4)
{
ncnn::warpaffine_bilinear_c4(a0, w, h, a1, w, h, tm, 0);
ncnn::warpaffine_bilinear_c4(a1, w, h, a2, w, h, tm_inv, -233);
}
if (CompareNearlyEqual(a0, a2) != 0)
{
fprintf(stderr, "test_mat_pixel_affine_e failed w=%d h=%d c=%d\n", w, h, c);
return -1;
}
}
return 0;
}
static int test_mat_pixel_affine_f(int w, int h)
{
for (int c = 1; c <= 4; c++)
{
ncnn::Mat a0 = RandomMat(w, h, c);
float tm[6];
float tm_inv[6];
ncnn::get_rotation_matrix(0.002f, 1.0f, w / 2, h / 2, tm);
ncnn::invert_affine_transform(tm, tm_inv);
ncnn::Mat a1(w * 2, h * 2, (size_t)c, c);
ncnn::Mat a2 = a0.clone();
if (c == 1)
{
ncnn::warpaffine_bilinear_c1(a0, w, h, a1, w * 2, h * 2, tm, 0);
ncnn::warpaffine_bilinear_c1(a1, w * 2, h * 2, a2, w, h, tm_inv, -233);
}
if (c == 2)
{
ncnn::warpaffine_bilinear_c2(a0, w, h, a1, w * 2, h * 2, tm, 0);
ncnn::warpaffine_bilinear_c2(a1, w * 2, h * 2, a2, w, h, tm_inv, -233);
}
if (c == 3)
{
ncnn::warpaffine_bilinear_c3(a0, w, h, a1, w * 2, h * 2, tm, 0);
ncnn::warpaffine_bilinear_c3(a1, w * 2, h * 2, a2, w, h, tm_inv, -233);
}
if (c == 4)
{
ncnn::warpaffine_bilinear_c4(a0, w, h, a1, w * 2, h * 2, tm, 0);
ncnn::warpaffine_bilinear_c4(a1, w * 2, h * 2, a2, w, h, tm_inv, -233);
}
if (CompareNearlyEqual(a0, a2) != 0)
{
fprintf(stderr, "test_mat_pixel_affine_f failed w=%d h=%d c=%d\n", w, h, c);
return -1;
}
}
return 0;
}
static int test_mat_pixel_affine_g(int w, int h)
{
for (int c = 1; c <= 4; c++)
{
ncnn::Mat a0 = RandomMat(w, h, c);
float tm[6];
float tm_inv[6];
ncnn::get_rotation_matrix(0.f, 0.5f, w / 2 + 4, h / 2 - 3, tm);
ncnn::invert_affine_transform(tm, tm_inv);
ncnn::Mat a1(w, h, (size_t)c, c);
ncnn::Mat a2 = a0.clone();
if (c == 1)
{
ncnn::warpaffine_bilinear_c1(a0, w, h, a1, w, h, tm, 0);
ncnn::warpaffine_bilinear_c1(a1, w, h, a2, w, h, tm_inv, -233);
}
if (c == 2)
{
ncnn::warpaffine_bilinear_c2(a0, w, h, a1, w, h, tm, 0);
ncnn::warpaffine_bilinear_c2(a1, w, h, a2, w, h, tm_inv, -233);
}
if (c == 3)
{
ncnn::warpaffine_bilinear_c3(a0, w, h, a1, w, h, tm, 0);
ncnn::warpaffine_bilinear_c3(a1, w, h, a2, w, h, tm_inv, -233);
}
if (c == 4)
{
ncnn::warpaffine_bilinear_c4(a0, w, h, a1, w, h, tm, 0);
ncnn::warpaffine_bilinear_c4(a1, w, h, a2, w, h, tm_inv, -233);
}
if (CompareNearlyEqual(a0, a2) != 0)
{
fprintf(stderr, "test_mat_pixel_affine_g failed w=%d h=%d c=%d\n", w, h, c);
return -1;
}
}
return 0;
}
static int test_mat_pixel_affine_0()
{
return 0
|| test_mat_pixel_affine_a(60, 70)
|| test_mat_pixel_affine_b(60, 70)
|| test_mat_pixel_affine_c(60, 70)
|| test_mat_pixel_affine_d(60, 70)
|| test_mat_pixel_affine_e(60, 70)
|| test_mat_pixel_affine_f(60, 70)
|| test_mat_pixel_affine_g(60, 70)
|| test_mat_pixel_affine_a(120, 160)
|| test_mat_pixel_affine_b(120, 160)
|| test_mat_pixel_affine_c(120, 160)
|| test_mat_pixel_affine_d(120, 160)
|| test_mat_pixel_affine_e(120, 160)
|| test_mat_pixel_affine_f(120, 160)
|| test_mat_pixel_affine_g(120, 160)
|| test_mat_pixel_affine_a(220, 330)
|| test_mat_pixel_affine_b(220, 330)
|| test_mat_pixel_affine_c(220, 330)
|| test_mat_pixel_affine_d(220, 330)
|| test_mat_pixel_affine_e(220, 330)
|| test_mat_pixel_affine_f(220, 330)
|| test_mat_pixel_affine_g(220, 330);
}
static int test_mat_pixel_affine_yuv420sp(int w, int h)
{
ncnn::Mat a0(w, h * 3 / 2, (size_t)1u, 1);
ncnn::Mat a0_y = RandomMat(w, h, 1);
ncnn::Mat a0_uv = RandomMat(w / 2, h / 2, 2);
memcpy(a0, a0_y, w * h);
memcpy((unsigned char*)a0 + w * h, a0_uv, w * h / 2);
float tm[6];
float tm_inv[6];
ncnn::get_rotation_matrix(-70.f, 0.3f, w / 2, h / 2, tm);
ncnn::invert_affine_transform(tm, tm_inv);
ncnn::Mat a1(w / 2, (h / 2) * 3 / 2, (size_t)1u, 1);
ncnn::Mat a2 = a0.clone();
ncnn::warpaffine_bilinear_yuv420sp(a0, w, h, a1, w / 2, h / 2, tm, 0);
ncnn::warpaffine_bilinear_yuv420sp(a1, w / 2, h / 2, a2, w, h, tm_inv, -233);
// Y
if (CompareNearlyEqual(ncnn::Mat(w, h, (unsigned char*)a0, (size_t)1u, 1), ncnn::Mat(w, h, (unsigned char*)a2, (size_t)1u, 1)) != 0)
{
fprintf(stderr, "test_mat_pixel_affine_yuv420sp Y failed w=%d h=%d\n", w, h);
return -1;
}
// UV
if (CompareNearlyEqual(ncnn::Mat(w / 2, h / 2, (unsigned char*)a0 + w * h, (size_t)2u, 2), ncnn::Mat(w / 2, h / 2, (unsigned char*)a2 + w * h, (size_t)2u, 2)) != 0)
{
fprintf(stderr, "test_mat_pixel_affine_yuv420sp UV failed w=%d h=%d\n", w, h);
return -1;
}
return 0;
}
static int test_mat_pixel_affine_1()
{
return 0
|| test_mat_pixel_affine_yuv420sp(40, 40)
|| test_mat_pixel_affine_yuv420sp(120, 160)
|| test_mat_pixel_affine_yuv420sp(220, 340);
}
int main()
{
SRAND(7767517);
return test_mat_pixel_affine_0() || test_mat_pixel_affine_1();
}

View File

@ -0,0 +1,753 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 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 "mat.h"
#include "prng.h"
#include <string.h>
static struct prng_rand_t g_prng_rand_state;
#define SRAND(seed) prng_srand(seed, &g_prng_rand_state)
#define RAND() prng_rand(&g_prng_rand_state)
static int RandomInt(int a, int b)
{
float random = ((float)RAND()) / (float)uint64_t(-1); //RAND_MAX;
int diff = b - a;
float r = random * diff;
return a + (int)r;
}
static int RandomInt2(int a, int b)
{
float random = ((float)RAND()) / (float)uint64_t(-1); //RAND_MAX;
int diff = b - a;
float r = random * diff;
return (a + (int)r + 1) / 2 * 2;
}
static int test_mat_pixel_drawing_c1(int w, int h)
{
ncnn::Mat a(w, h, (size_t)1u, 1);
ncnn::Mat b(h, w, (size_t)1u, 1);
int _color = 0;
unsigned char* color = (unsigned char*)&_color;
// fill with color
color[0] = 255;
ncnn::draw_rectangle_c1(a, w, h, 0, 0, w, h, _color, -1);
ncnn::draw_rectangle_c1(b, h, w, 0, 0, h, w, _color, -1);
// draw rectangle
int rx = RandomInt(0, w);
int ry = RandomInt(0, h);
int rw = RandomInt(0, w - rx);
int rh = RandomInt(0, h - ry);
color[0] = 100;
ncnn::draw_rectangle_c1(a, w, h, rx, ry, rw, rh, _color, 3);
ncnn::draw_rectangle_c1(b, h, w, ry, rx, rh, rw, _color, 3);
// draw filled rectangle out of image
color[0] = 144;
ncnn::draw_rectangle_c1(a, w, h, w - 10, -10, 20, 30, _color, -1);
ncnn::draw_rectangle_c1(b, h, w, -10, w - 10, 30, 20, _color, -1);
color[0] = 166;
ncnn::draw_rectangle_c1(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 7);
ncnn::draw_rectangle_c1(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 7);
// draw rectangle out of image
color[0] = 44;
ncnn::draw_rectangle_c1(a, w, h, rx + w / 2, ry + h / 2, rw, rh, _color, 1);
ncnn::draw_rectangle_c1(b, h, w, ry + h / 2, rx + w / 2, rh, rw, _color, 1);
color[0] = 66;
ncnn::draw_rectangle_c1(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 7);
ncnn::draw_rectangle_c1(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 7);
// draw filled circle
int cx = RandomInt(0, w);
int cy = RandomInt(0, h);
int radius = RandomInt(0, std::min(w, h));
color[0] = 20;
ncnn::draw_circle_c1(a, w, h, cx, cy, radius, _color, -1);
ncnn::draw_circle_c1(b, h, w, cy, cx, radius, _color, -1);
// draw filled circle out of image
color[0] = 230;
ncnn::draw_circle_c1(a, w, h, 10, -4, 6, _color, -1);
ncnn::draw_circle_c1(b, h, w, -4, 10, 6, _color, -1);
// draw circle out of image
color[0] = 130;
ncnn::draw_circle_c1(a, w, h, cx, cy, radius + std::min(w, h) / 2, _color, 5);
ncnn::draw_circle_c1(b, h, w, cy, cx, radius + std::min(w, h) / 2, _color, 5);
// draw line
int x0 = RandomInt(0, w);
int y0 = RandomInt(0, h);
int x1 = RandomInt(0, w);
int y1 = RandomInt(0, h);
color[0] = 233;
ncnn::draw_line_c1(a, w, h, x0, y0, x1, y1, _color, 7);
ncnn::draw_line_c1(b, h, w, y0, x0, y1, x1, _color, 7);
// draw line out of image
color[0] = 192;
ncnn::draw_line_c1(a, w, h, x0 - w, y0 - h, x1 + w, y1 + h, _color, 1);
ncnn::draw_line_c1(b, h, w, y0 - h, x0 - w, y1 + h, x1 + w, _color, 1);
// transpose b
ncnn::Mat c(w, h, (size_t)1u, 1);
ncnn::kanna_rotate_c1(b, h, w, c, w, h, 5);
// draw text
const char text[] = "saJIEWdl\nj43@o";
int tx = RandomInt(0, w / 2);
int ty = RandomInt(0, h / 2);
int fontpixelsize = 10;
color[0] = 128;
ncnn::draw_text_c1(a, w, h, text, tx, ty, fontpixelsize, _color);
int tw;
int th;
ncnn::get_text_drawing_size(text, fontpixelsize, &tw, &th);
const int len = strlen(text);
for (int i = 0; i < 8; i++)
{
const char ch[2] = {text[i], '\0'};
ncnn::draw_text_c1(c, w, h, ch, tx + tw / 8 * i, ty, fontpixelsize, _color);
}
for (int i = 9; i < len; i++)
{
const char ch[2] = {text[i], '\0'};
ncnn::draw_text_c1(c, w, h, ch, tx + tw / 8 * (i - 9), ty + th / 2, fontpixelsize, _color);
}
// draw text out of image
fontpixelsize = std::max(w, h) / 2;
color[0] = 228;
ncnn::draw_text_c1(a, w, h, "QAQ", -3, -5, fontpixelsize, _color);
ncnn::get_text_drawing_size("QAQ", fontpixelsize, &tw, &th);
ncnn::draw_text_c1(c, w, h, "Q", -3, -5, fontpixelsize, _color);
ncnn::draw_text_c1(c, w, h, "A", -3 + tw / 3, -5, fontpixelsize, _color);
ncnn::draw_text_c1(c, w, h, "Q", -3 + tw / 3 * 2, -5, fontpixelsize, _color);
if (memcmp(a, c, w * h) != 0)
{
fprintf(stderr, "test_mat_pixel_drawing_c1 failed w=%d h=%d\n", w, h);
return -1;
}
return 0;
}
static int test_mat_pixel_drawing_c2(int w, int h)
{
ncnn::Mat a(w, h, (size_t)2u, 2);
ncnn::Mat b(h, w, (size_t)2u, 2);
int _color = 0;
unsigned char* color = (unsigned char*)&_color;
// fill with color
color[0] = 255;
color[1] = 251;
ncnn::draw_rectangle_c2(a, w, h, 0, 0, w, h, _color, -1);
ncnn::draw_rectangle_c2(b, h, w, 0, 0, h, w, _color, -1);
// draw rectangle
int rx = RandomInt(0, w);
int ry = RandomInt(0, h);
int rw = RandomInt(0, w - rx);
int rh = RandomInt(0, h - ry);
color[0] = 100;
color[1] = 130;
ncnn::draw_rectangle_c2(a, w, h, rx, ry, rw, rh, _color, 3);
ncnn::draw_rectangle_c2(b, h, w, ry, rx, rh, rw, _color, 3);
// draw filled rectangle out of image
color[0] = 144;
color[1] = 133;
ncnn::draw_rectangle_c2(a, w, h, w - 10, -10, 20, 30, _color, -1);
ncnn::draw_rectangle_c2(b, h, w, -10, w - 10, 30, 20, _color, -1);
color[0] = 166;
color[1] = 133;
ncnn::draw_rectangle_c2(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 7);
ncnn::draw_rectangle_c2(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 7);
// draw rectangle out of image
color[0] = 44;
color[1] = 33;
ncnn::draw_rectangle_c2(a, w, h, rx + w / 2, ry + h / 2, rw, rh, _color, 1);
ncnn::draw_rectangle_c2(b, h, w, ry + h / 2, rx + w / 2, rh, rw, _color, 1);
color[0] = 66;
color[1] = 44;
ncnn::draw_rectangle_c2(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 7);
ncnn::draw_rectangle_c2(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 7);
// draw filled circle
int cx = RandomInt(0, w);
int cy = RandomInt(0, h);
int radius = RandomInt(0, std::min(w, h));
color[0] = 20;
color[1] = 120;
ncnn::draw_circle_c2(a, w, h, cx, cy, radius, _color, -1);
ncnn::draw_circle_c2(b, h, w, cy, cx, radius, _color, -1);
// draw filled circle out of image
color[0] = 230;
color[1] = 130;
ncnn::draw_circle_c2(a, w, h, 10, -4, 6, _color, -1);
ncnn::draw_circle_c2(b, h, w, -4, 10, 6, _color, -1);
// draw circle out of image
color[0] = 130;
color[1] = 30;
ncnn::draw_circle_c2(a, w, h, cx, cy, radius + std::min(w, h) / 2, _color, 5);
ncnn::draw_circle_c2(b, h, w, cy, cx, radius + std::min(w, h) / 2, _color, 5);
// draw line
int x0 = RandomInt(0, w);
int y0 = RandomInt(0, h);
int x1 = RandomInt(0, w);
int y1 = RandomInt(0, h);
color[0] = 233;
color[1] = 233;
ncnn::draw_line_c2(a, w, h, x0, y0, x1, y1, _color, 7);
ncnn::draw_line_c2(b, h, w, y0, x0, y1, x1, _color, 7);
// draw line out of image
color[0] = 192;
color[1] = 192;
ncnn::draw_line_c2(a, w, h, x0 - w, y0 - h, x1 + w, y1 + h, _color, 1);
ncnn::draw_line_c2(b, h, w, y0 - h, x0 - w, y1 + h, x1 + w, _color, 1);
// transpose b
ncnn::Mat c(w, h, (size_t)2u, 2);
ncnn::kanna_rotate_c2(b, h, w, c, w, h, 5);
// draw text
const char text[] = "Q`~\\=f\nPN\'/<DSA";
int tx = RandomInt(0, w / 2);
int ty = RandomInt(0, h / 2);
int fontpixelsize = 12;
color[0] = 0;
color[1] = 128;
ncnn::draw_text_c2(a, w, h, text, tx, ty, fontpixelsize, _color);
int tw;
int th;
ncnn::get_text_drawing_size(text, fontpixelsize, &tw, &th);
const int len = strlen(text);
for (int i = 0; i < 6; i++)
{
const char ch[2] = {text[i], '\0'};
ncnn::draw_text_c2(c, w, h, ch, tx + tw / 8 * i, ty, fontpixelsize, _color);
}
for (int i = 7; i < len; i++)
{
const char ch[2] = {text[i], '\0'};
ncnn::draw_text_c2(c, w, h, ch, tx + tw / 8 * (i - 7), ty + th / 2, fontpixelsize, _color);
}
// draw text out of image
fontpixelsize = std::max(w, h) / 3;
color[0] = 228;
color[1] = 0;
ncnn::draw_text_c2(a, w, h, "!@#$%^&", -1, -2, fontpixelsize, _color);
ncnn::get_text_drawing_size("!@#$%^&", fontpixelsize, &tw, &th);
ncnn::draw_text_c2(c, w, h, "!@#", -1, -2, fontpixelsize, _color);
ncnn::draw_text_c2(c, w, h, "$", -1 + tw / 7 * 3, -2, fontpixelsize, _color);
ncnn::draw_text_c2(c, w, h, "%^&", -1 + tw / 7 * 4, -2, fontpixelsize, _color);
if (memcmp(a, c, w * h * 2) != 0)
{
fprintf(stderr, "test_mat_pixel_drawing_c2 failed w=%d h=%d\n", w, h);
return -1;
}
return 0;
}
static int test_mat_pixel_drawing_c3(int w, int h)
{
ncnn::Mat a(w, h, (size_t)3u, 3);
ncnn::Mat b(h, w, (size_t)3u, 3);
int _color = 0;
unsigned char* color = (unsigned char*)&_color;
// fill with color
color[0] = 255;
color[1] = 251;
color[2] = 244;
ncnn::draw_rectangle_c3(a, w, h, 0, 0, w, h, _color, -1);
ncnn::draw_rectangle_c3(b, h, w, 0, 0, h, w, _color, -1);
// draw rectangle
int rx = RandomInt(0, w);
int ry = RandomInt(0, h);
int rw = RandomInt(0, w - rx);
int rh = RandomInt(0, h - ry);
color[0] = 100;
color[1] = 130;
color[2] = 150;
ncnn::draw_rectangle_c3(a, w, h, rx, ry, rw, rh, _color, 3);
ncnn::draw_rectangle_c3(b, h, w, ry, rx, rh, rw, _color, 3);
// draw filled rectangle out of image
color[0] = 144;
color[1] = 133;
color[2] = 122;
ncnn::draw_rectangle_c3(a, w, h, w - 10, -10, 20, 30, _color, -1);
ncnn::draw_rectangle_c3(b, h, w, -10, w - 10, 30, 20, _color, -1);
color[0] = 166;
color[1] = 133;
color[2] = 122;
ncnn::draw_rectangle_c3(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 7);
ncnn::draw_rectangle_c3(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 7);
// draw rectangle out of image
color[0] = 44;
color[1] = 33;
color[2] = 22;
ncnn::draw_rectangle_c3(a, w, h, rx + w / 2, ry + h / 2, rw, rh, _color, 1);
ncnn::draw_rectangle_c3(b, h, w, ry + h / 2, rx + w / 2, rh, rw, _color, 1);
color[0] = 66;
color[1] = 44;
color[2] = 33;
ncnn::draw_rectangle_c3(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 7);
ncnn::draw_rectangle_c3(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 7);
// draw filled circle
int cx = RandomInt(0, w);
int cy = RandomInt(0, h);
int radius = RandomInt(0, std::min(w, h));
color[0] = 20;
color[1] = 120;
color[2] = 220;
ncnn::draw_circle_c3(a, w, h, cx, cy, radius, _color, -1);
ncnn::draw_circle_c3(b, h, w, cy, cx, radius, _color, -1);
// draw filled circle out of image
color[0] = 230;
color[1] = 130;
color[2] = 110;
ncnn::draw_circle_c3(a, w, h, 10, -4, 6, _color, -1);
ncnn::draw_circle_c3(b, h, w, -4, 10, 6, _color, -1);
// draw circle out of image
color[0] = 130;
color[1] = 30;
color[2] = 230;
ncnn::draw_circle_c3(a, w, h, cx, cy, radius + std::min(w, h) / 2, _color, 5);
ncnn::draw_circle_c3(b, h, w, cy, cx, radius + std::min(w, h) / 2, _color, 5);
// draw line
int x0 = RandomInt(0, w);
int y0 = RandomInt(0, h);
int x1 = RandomInt(0, w);
int y1 = RandomInt(0, h);
color[0] = 233;
color[1] = 233;
color[2] = 233;
ncnn::draw_line_c3(a, w, h, x0, y0, x1, y1, _color, 7);
ncnn::draw_line_c3(b, h, w, y0, x0, y1, x1, _color, 7);
// draw line out of image
color[0] = 192;
color[1] = 192;
color[2] = 0;
ncnn::draw_line_c3(a, w, h, x0 - w, y0 - h, x1 + w, y1 + h, _color, 1);
ncnn::draw_line_c3(b, h, w, y0 - h, x0 - w, y1 + h, x1 + w, _color, 1);
// transpose b
ncnn::Mat c(w, h, (size_t)3u, 3);
ncnn::kanna_rotate_c3(b, h, w, c, w, h, 5);
// draw text
const char text[] = "Q`~\\=f\nPN\'/<DSA";
int tx = RandomInt(0, w / 2);
int ty = RandomInt(0, h / 2);
int fontpixelsize = 12;
color[0] = 0;
color[1] = 128;
color[2] = 128;
ncnn::draw_text_c3(a, w, h, text, tx, ty, fontpixelsize, _color);
int tw;
int th;
ncnn::get_text_drawing_size(text, fontpixelsize, &tw, &th);
const int len = strlen(text);
for (int i = 0; i < 6; i++)
{
const char ch[2] = {text[i], '\0'};
ncnn::draw_text_c3(c, w, h, ch, tx + tw / 8 * i, ty, fontpixelsize, _color);
}
for (int i = 7; i < len; i++)
{
const char ch[2] = {text[i], '\0'};
ncnn::draw_text_c3(c, w, h, ch, tx + tw / 8 * (i - 7), ty + th / 2, fontpixelsize, _color);
}
// draw text out of image
fontpixelsize = std::max(w, h) / 2;
color[0] = 228;
color[1] = 0;
color[2] = 128;
ncnn::draw_text_c3(a, w, h, "qwqwqwq", -13, -15, fontpixelsize, _color);
ncnn::get_text_drawing_size("qwqwqwq", fontpixelsize, &tw, &th);
ncnn::draw_text_c3(c, w, h, "qwq", -13, -15, fontpixelsize, _color);
ncnn::draw_text_c3(c, w, h, "w", -13 + tw / 7 * 3, -15, fontpixelsize, _color);
ncnn::draw_text_c3(c, w, h, "qwq", -13 + tw / 7 * 4, -15, fontpixelsize, _color);
if (memcmp(a, c, w * h * 3) != 0)
{
fprintf(stderr, "test_mat_pixel_drawing_c3 failed w=%d h=%d\n", w, h);
return -1;
}
return 0;
}
static int test_mat_pixel_drawing_c4(int w, int h)
{
ncnn::Mat a(w, h, (size_t)4u, 4);
ncnn::Mat b(h, w, (size_t)4u, 4);
int _color = 0;
unsigned char* color = (unsigned char*)&_color;
// fill with color
color[0] = 255;
color[1] = 255;
color[2] = 255;
color[3] = 0;
ncnn::draw_rectangle_c4(a, w, h, 0, 0, w, h, _color, -1);
ncnn::draw_rectangle_c4(b, h, w, 0, 0, h, w, _color, -1);
// draw rectangle
int rx = RandomInt(0, w);
int ry = RandomInt(0, h);
int rw = RandomInt(0, w - rx);
int rh = RandomInt(0, h - ry);
color[0] = 100;
color[1] = 20;
color[2] = 200;
color[3] = 100;
ncnn::draw_rectangle_c4(a, w, h, rx, ry, rw, rh, _color, 3);
ncnn::draw_rectangle_c4(b, h, w, ry, rx, rh, rw, _color, 3);
// draw filled rectangle out of image
color[0] = 144;
color[1] = 133;
color[2] = 122;
color[3] = 30;
ncnn::draw_rectangle_c4(a, w, h, w - 10, -10, 20, 30, _color, -1);
ncnn::draw_rectangle_c4(b, h, w, -10, w - 10, 30, 20, _color, -1);
color[0] = 166;
color[1] = 133;
color[2] = 122;
color[3] = 20;
ncnn::draw_rectangle_c4(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 7);
ncnn::draw_rectangle_c4(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 7);
// draw rectangle out of image
color[0] = 44;
color[1] = 144;
color[2] = 44;
color[3] = 144;
ncnn::draw_rectangle_c4(a, w, h, rx + w / 2, ry + h / 2, rw, rh, _color, 1);
ncnn::draw_rectangle_c4(b, h, w, ry + h / 2, rx + w / 2, rh, rw, _color, 1);
color[0] = 66;
color[1] = 44;
color[2] = 33;
color[3] = 112;
ncnn::draw_rectangle_c4(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 7);
ncnn::draw_rectangle_c4(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 7);
// draw filled circle
int cx = RandomInt(0, w);
int cy = RandomInt(0, h);
int radius = RandomInt(0, std::min(w, h));
color[0] = 10;
color[1] = 2;
color[2] = 200;
color[3] = 20;
ncnn::draw_circle_c4(a, w, h, cx, cy, radius, _color, -1);
ncnn::draw_circle_c4(b, h, w, cy, cx, radius, _color, -1);
// draw filled circle out of image
color[0] = 230;
color[1] = 130;
color[2] = 110;
color[3] = 5;
ncnn::draw_circle_c4(a, w, h, 10, -4, 6, _color, -1);
ncnn::draw_circle_c4(b, h, w, -4, 10, 6, _color, -1);
// draw circle out of image
color[0] = 130;
color[1] = 255;
color[2] = 130;
color[3] = 255;
ncnn::draw_circle_c4(a, w, h, cx, cy, radius + std::min(w, h) / 2, _color, 5);
ncnn::draw_circle_c4(b, h, w, cy, cx, radius + std::min(w, h) / 2, _color, 5);
// draw line
int x0 = RandomInt(0, w);
int y0 = RandomInt(0, h);
int x1 = RandomInt(0, w);
int y1 = RandomInt(0, h);
color[0] = 233;
color[1] = 233;
color[2] = 233;
color[3] = 233;
ncnn::draw_line_c4(a, w, h, x0, y0, x1, y1, _color, 7);
ncnn::draw_line_c4(b, h, w, y0, x0, y1, x1, _color, 7);
// draw line out of image
color[0] = 192;
color[1] = 22;
color[2] = 1;
color[3] = 0;
ncnn::draw_line_c4(a, w, h, x0 - w, y0 - h, x1 + w, y1 + h, _color, 1);
ncnn::draw_line_c4(b, h, w, y0 - h, x0 - w, y1 + h, x1 + w, _color, 1);
// transpose b
ncnn::Mat c(w, h, (size_t)4u, 4);
ncnn::kanna_rotate_c4(b, h, w, c, w, h, 5);
// draw text
const char text[] = "!@)\n($ 34\n2]\"M,";
int tx = RandomInt(0, w / 2);
int ty = RandomInt(0, h / 2);
int fontpixelsize = 23;
color[0] = 11;
color[1] = 128;
color[2] = 12;
color[3] = 128;
ncnn::draw_text_c4(a, w, h, text, tx, ty, fontpixelsize, _color);
int tw;
int th;
ncnn::get_text_drawing_size(text, fontpixelsize, &tw, &th);
const int len = strlen(text);
for (int i = 0; i < 3; i++)
{
const char ch[2] = {text[i], '\0'};
ncnn::draw_text_c4(c, w, h, ch, tx + tw / 5 * i, ty, fontpixelsize, _color);
}
for (int i = 4; i < 9; i++)
{
const char ch[2] = {text[i], '\0'};
ncnn::draw_text_c4(c, w, h, ch, tx + tw / 5 * (i - 4), ty + th / 3, fontpixelsize, _color);
}
for (int i = 10; i < len; i++)
{
const char ch[2] = {text[i], '\0'};
ncnn::draw_text_c4(c, w, h, ch, tx + tw / 5 * (i - 10), ty + th / 3 * 2, fontpixelsize, _color);
}
// draw text out of image
fontpixelsize = std::max(w, h) / 3;
color[0] = 228;
color[1] = 0;
color[2] = 128;
color[3] = 200;
ncnn::draw_text_c4(a, w, h, "=_+!//zzzz", -13, -15, fontpixelsize, _color);
ncnn::get_text_drawing_size("=_+!//zzzz", fontpixelsize, &tw, &th);
ncnn::draw_text_c4(c, w, h, "=_+", -13, -15, fontpixelsize, _color);
ncnn::draw_text_c4(c, w, h, "!", -13 + tw / 10 * 3, -15, fontpixelsize, _color);
ncnn::draw_text_c4(c, w, h, "//zzzz", -13 + tw / 10 * 4, -15, fontpixelsize, _color);
if (memcmp(a, c, w * h * 4) != 0)
{
fprintf(stderr, "test_mat_pixel_drawing_c4 failed w=%d h=%d\n", w, h);
return -1;
}
return 0;
}
static int test_mat_pixel_drawing_0()
{
return 0
|| test_mat_pixel_drawing_c1(22, 33)
|| test_mat_pixel_drawing_c2(22, 23)
|| test_mat_pixel_drawing_c3(32, 23)
|| test_mat_pixel_drawing_c4(42, 13)
|| test_mat_pixel_drawing_c1(202, 303)
|| test_mat_pixel_drawing_c2(202, 203)
|| test_mat_pixel_drawing_c3(302, 203)
|| test_mat_pixel_drawing_c4(402, 103);
}
static int test_mat_pixel_drawing_yuv420sp(int w, int h)
{
ncnn::Mat a(w, h * 3 / 2, (size_t)1u, 1);
ncnn::Mat b(h, w * 3 / 2, (size_t)1u, 1);
int _color = 0;
unsigned char* color = (unsigned char*)&_color;
// fill with color
color[0] = 255;
color[1] = 255;
color[2] = 255;
ncnn::draw_rectangle_yuv420sp(a, w, h, 0, 0, w, h, _color, -1);
ncnn::draw_rectangle_yuv420sp(b, h, w, 0, 0, h, w, _color, -1);
// draw rectangle
int rx = RandomInt2(0, w);
int ry = RandomInt2(0, h);
int rw = RandomInt2(0, w - rx);
int rh = RandomInt2(0, h - ry);
color[0] = 100;
color[1] = 20;
color[2] = 200;
ncnn::draw_rectangle_yuv420sp(a, w, h, rx, ry, rw, rh, _color, 4);
ncnn::draw_rectangle_yuv420sp(b, h, w, ry, rx, rh, rw, _color, 4);
// draw filled rectangle out of image
color[0] = 144;
color[1] = 133;
color[2] = 122;
ncnn::draw_rectangle_yuv420sp(a, w, h, w - 10, -10, 20, 30, _color, -1);
ncnn::draw_rectangle_yuv420sp(b, h, w, -10, w - 10, 30, 20, _color, -1);
color[0] = 166;
color[1] = 133;
color[2] = 122;
ncnn::draw_rectangle_yuv420sp(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 8);
ncnn::draw_rectangle_yuv420sp(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 8);
// draw rectangle out of image
color[0] = 44;
color[1] = 144;
color[2] = 44;
ncnn::draw_rectangle_yuv420sp(a, w, h, rx + w / 2, ry + h / 2, rw, rh, _color, 2);
ncnn::draw_rectangle_yuv420sp(b, h, w, ry + h / 2, rx + w / 2, rh, rw, _color, 2);
color[0] = 66;
color[1] = 44;
color[2] = 33;
ncnn::draw_rectangle_yuv420sp(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 8);
ncnn::draw_rectangle_yuv420sp(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 8);
// draw filled circle
int cx = RandomInt2(0, w);
int cy = RandomInt2(0, h);
int radius = RandomInt2(0, std::min(w, h));
color[0] = 10;
color[1] = 2;
color[2] = 200;
ncnn::draw_circle_yuv420sp(a, w, h, cx, cy, radius, _color, -1);
ncnn::draw_circle_yuv420sp(b, h, w, cy, cx, radius, _color, -1);
// draw filled circle out of image
color[0] = 230;
color[1] = 130;
color[2] = 110;
ncnn::draw_circle_yuv420sp(a, w, h, 10, -4, 6, _color, -1);
ncnn::draw_circle_yuv420sp(b, h, w, -4, 10, 6, _color, -1);
// draw circle out of image
color[0] = 130;
color[1] = 255;
color[2] = 130;
ncnn::draw_circle_yuv420sp(a, w, h, cx, cy, radius + std::min(w, h) / 2, _color, 6);
ncnn::draw_circle_yuv420sp(b, h, w, cy, cx, radius + std::min(w, h) / 2, _color, 6);
// draw line
int x0 = RandomInt2(0, w);
int y0 = RandomInt2(0, h);
int x1 = RandomInt2(0, w);
int y1 = RandomInt2(0, h);
color[0] = 233;
color[1] = 233;
color[2] = 233;
ncnn::draw_line_yuv420sp(a, w, h, x0, y0, x1, y1, _color, 8);
ncnn::draw_line_yuv420sp(b, h, w, y0, x0, y1, x1, _color, 8);
// draw line out of image
color[0] = 192;
color[1] = 22;
color[2] = 1;
ncnn::draw_line_yuv420sp(a, w, h, x0 - w, y0 - h, x1 + w, y1 + h, _color, 2);
ncnn::draw_line_yuv420sp(b, h, w, y0 - h, x0 - w, y1 + h, x1 + w, _color, 2);
// transpose b
ncnn::Mat c(w, h * 3 / 2, (size_t)1u, 1);
ncnn::kanna_rotate_yuv420sp(b, h, w, c, w, h, 5);
// draw text
const char text[] = "!@)\n($ 34\n2]\"M,";
int tx = RandomInt2(0, w / 2);
int ty = RandomInt2(0, h / 2);
int fontpixelsize = 24;
color[0] = 11;
color[1] = 128;
color[2] = 12;
ncnn::draw_text_yuv420sp(a, w, h, text, tx, ty, fontpixelsize, _color);
int tw;
int th;
ncnn::get_text_drawing_size(text, fontpixelsize, &tw, &th);
const int len = strlen(text);
for (int i = 0; i < 3; i++)
{
const char ch[2] = {text[i], '\0'};
ncnn::draw_text_yuv420sp(c, w, h, ch, tx + tw / 5 * i, ty, fontpixelsize, _color);
}
for (int i = 4; i < 9; i++)
{
const char ch[2] = {text[i], '\0'};
ncnn::draw_text_yuv420sp(c, w, h, ch, tx + tw / 5 * (i - 4), ty + th / 3, fontpixelsize, _color);
}
for (int i = 10; i < len; i++)
{
const char ch[2] = {text[i], '\0'};
ncnn::draw_text_yuv420sp(c, w, h, ch, tx + tw / 5 * (i - 10), ty + th / 3 * 2, fontpixelsize, _color);
}
// draw text out of image
fontpixelsize = (std::max(w, h) / 3 + 1) / 2 * 2;
color[0] = 228;
color[1] = 0;
color[2] = 128;
ncnn::draw_text_yuv420sp(a, w, h, "=_+!//zzzz", -14, -12, fontpixelsize, _color);
ncnn::get_text_drawing_size("=_+!//zzzz", fontpixelsize, &tw, &th);
ncnn::draw_text_yuv420sp(c, w, h, "=_+", -14, -12, fontpixelsize, _color);
ncnn::draw_text_yuv420sp(c, w, h, "!", -14 + tw / 10 * 3, -12, fontpixelsize, _color);
ncnn::draw_text_yuv420sp(c, w, h, "//zzzz", -14 + tw / 10 * 4, -12, fontpixelsize, _color);
if (memcmp(a, c, w * h * 3 / 2) != 0)
{
fprintf(stderr, "test_mat_pixel_drawing_yuv420sp failed w=%d h=%d\n", w, h);
return -1;
}
return 0;
}
static int test_mat_pixel_drawing_1()
{
return 0
|| test_mat_pixel_drawing_yuv420sp(10, 10)
|| test_mat_pixel_drawing_yuv420sp(120, 160)
|| test_mat_pixel_drawing_yuv420sp(220, 340);
}
int main()
{
SRAND(7767517);
return 0
|| test_mat_pixel_drawing_0()
|| test_mat_pixel_drawing_1();
}

View File

@ -0,0 +1,340 @@
// 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 "mat.h"
#include "prng.h"
#include <math.h>
#include <string.h>
static struct prng_rand_t g_prng_rand_state;
#define SRAND(seed) prng_srand(seed, &g_prng_rand_state)
#define RAND() prng_rand(&g_prng_rand_state)
static ncnn::Mat RandomMat(int w, int h, int elempack)
{
ncnn::Mat m(w, h, 1, (size_t)elempack, elempack);
unsigned char* p = m;
for (int i = 0; i < w * h * elempack; i++)
{
p[i] = RAND() % 256;
}
return m;
}
static bool NearlyEqual(float a, float b, float epsilon)
{
if (a == b)
return true;
float diff = fabs(a - b);
if (diff <= epsilon)
return true;
// relative error
return diff < epsilon * std::max(fabs(a), fabs(b));
}
static int Compare(const ncnn::Mat& a, const ncnn::Mat& b, float epsilon = 0.001)
{
#define CHECK_MEMBER(m) \
if (a.m != b.m) \
{ \
fprintf(stderr, #m " not match expect %d but got %d\n", (int)a.m, (int)b.m); \
return -1; \
}
CHECK_MEMBER(dims)
CHECK_MEMBER(w)
CHECK_MEMBER(h)
CHECK_MEMBER(c)
CHECK_MEMBER(elemsize)
CHECK_MEMBER(elempack)
#undef CHECK_MEMBER
for (int q = 0; q < a.c; q++)
{
const ncnn::Mat ma = a.channel(q);
const ncnn::Mat mb = b.channel(q);
for (int i = 0; i < a.h; i++)
{
const float* pa = ma.row(i);
const float* pb = mb.row(i);
for (int j = 0; j < a.w; j++)
{
if (!NearlyEqual(pa[j], pb[j], epsilon))
{
fprintf(stderr, "value not match at c:%d h:%d w:%d expect %f but got %f\n", q, i, j, pa[j], pb[j]);
return -1;
}
}
}
}
return 0;
}
static int test_mat_pixel_resize(int w, int h, int ch, int target_width, int target_height)
{
ncnn::Option opt;
opt.num_threads = 1;
ncnn::Mat a = RandomMat(w, h, ch);
ncnn::Mat b(target_width, target_height, 1, (size_t)ch, ch);
if (ch == 1) resize_bilinear_c1(a, w, h, b, target_width, target_height);
if (ch == 2) resize_bilinear_c2(a, w, h, b, target_width, target_height);
if (ch == 3) resize_bilinear_c3(a, w, h, b, target_width, target_height);
if (ch == 4) resize_bilinear_c4(a, w, h, b, target_width, target_height);
ncnn::Mat a2;
ncnn::convert_packing(a, a2, 1, opt);
ncnn::Mat b2;
ncnn::convert_packing(b, b2, 1, opt);
for (int i = 0; i < ch; i++)
{
ncnn::Mat c = ncnn::Mat::from_pixels(a2.channel(i), ncnn::Mat::PIXEL_GRAY, w, h);
ncnn::Mat d = ncnn::Mat::from_pixels(b2.channel(i), ncnn::Mat::PIXEL_GRAY, target_width, target_height);
ncnn::Mat e;
ncnn::resize_bilinear(c, e, target_width, target_height, opt);
if (Compare(e, d, 0.5) != 0)
{
fprintf(stderr, "test_mat_pixel_resize failed w=%d h=%d ch=%d target_width=%d target_height=%d\n", w, h, ch, target_width, target_height);
return -1;
}
}
return 0;
}
static int test_mat_pixel_roi_resize_gray(int w, int h, int roix, int roiy, int roiw, int roih, int target_width, int target_height)
{
ncnn::Option opt;
opt.num_threads = 1;
int pixel_type_from[5] = {ncnn::Mat::PIXEL_GRAY, ncnn::Mat::PIXEL_GRAY2RGB, ncnn::Mat::PIXEL_GRAY2BGR, ncnn::Mat::PIXEL_GRAY2RGBA, ncnn::Mat::PIXEL_GRAY2BGRA};
int pixel_type_to[5] = {ncnn::Mat::PIXEL_GRAY, ncnn::Mat::PIXEL_RGB2GRAY, ncnn::Mat::PIXEL_BGR2GRAY, ncnn::Mat::PIXEL_RGBA2GRAY, ncnn::Mat::PIXEL_BGRA2GRAY};
ncnn::Mat a = RandomMat(w, h, 1);
ncnn::Mat a2;
ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
// FIXME enable more convert types
for (int i = 0; i < 1; i++)
{
ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
ncnn::Mat b2;
ncnn::Mat c2;
ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
ncnn::convert_packing(b2, c2, 1, opt);
ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
{
fprintf(stderr, "test_mat_pixel_roi_resize_gray failed w=%d h=%d roi=[%d %d %d %d] target_width=%d target_height=%d pixel_type=%d\n", w, h, roix, roiy, roiw, roih, target_width, target_height, i);
return -1;
}
}
return 0;
}
static int test_mat_pixel_roi_resize_rgb(int w, int h, int roix, int roiy, int roiw, int roih, int target_width, int target_height)
{
ncnn::Option opt;
opt.num_threads = 1;
int pixel_type_from[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGB2RGBA, ncnn::Mat::PIXEL_RGB2BGRA};
int pixel_type_to[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_RGBA2RGB, ncnn::Mat::PIXEL_BGRA2RGB};
ncnn::Mat a = RandomMat(w, h, 3);
ncnn::Mat a2;
ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
// FIXME enable more convert types
for (int i = 0; i < 2; i++)
{
ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
ncnn::Mat b2;
ncnn::Mat c2;
ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
ncnn::convert_packing(b2, c2, 3, opt);
ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
{
fprintf(stderr, "test_mat_pixel_roi_resize_rgb failed w=%d h=%d roi=[%d %d %d %d] target_width=%d target_height=%d pixel_type=%d\n", w, h, roix, roiy, roiw, roih, target_width, target_height, i);
return -1;
}
}
return 0;
}
static int test_mat_pixel_roi_resize_bgr(int w, int h, int roix, int roiy, int roiw, int roih, int target_width, int target_height)
{
ncnn::Option opt;
opt.num_threads = 1;
int pixel_type_from[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_BGR2RGBA, ncnn::Mat::PIXEL_BGR2BGRA};
int pixel_type_to[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGBA2BGR, ncnn::Mat::PIXEL_BGRA2BGR};
ncnn::Mat a = RandomMat(w, h, 3);
ncnn::Mat a2;
ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
// FIXME enable more convert types
for (int i = 0; i < 2; i++)
{
ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
ncnn::Mat b2;
ncnn::Mat c2;
ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
ncnn::convert_packing(b2, c2, 3, opt);
ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
{
fprintf(stderr, "test_mat_pixel_roi_resize_bgr failed w=%d h=%d roi=[%d %d %d %d] target_width=%d target_height=%d pixel_type=%d\n", w, h, roix, roiy, roiw, roih, target_width, target_height, i);
return -1;
}
}
return 0;
}
static int test_mat_pixel_roi_resize_rgba(int w, int h, int roix, int roiy, int roiw, int roih, int target_width, int target_height)
{
ncnn::Option opt;
opt.num_threads = 1;
int pixel_type_from[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_RGBA2BGRA};
int pixel_type_to[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_BGRA2RGBA};
ncnn::Mat a = RandomMat(w, h, 4);
ncnn::Mat a2;
ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
for (int i = 0; i < 2; i++)
{
ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
ncnn::Mat b2;
ncnn::Mat c2;
ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
ncnn::convert_packing(b2, c2, 4, opt);
ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
{
fprintf(stderr, "test_mat_pixel_roi_resize_rgba failed w=%d h=%d roi=[%d %d %d %d] target_width=%d target_height=%d pixel_type=%d\n", w, h, roix, roiy, roiw, roih, target_width, target_height, i);
return -1;
}
}
return 0;
}
static int test_mat_pixel_roi_resize_bgra(int w, int h, int roix, int roiy, int roiw, int roih, int target_width, int target_height)
{
ncnn::Option opt;
opt.num_threads = 1;
int pixel_type_from[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_BGRA2RGBA};
int pixel_type_to[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_RGBA2BGRA};
ncnn::Mat a = RandomMat(w, h, 4);
ncnn::Mat a2;
ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
for (int i = 0; i < 2; i++)
{
ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
ncnn::Mat b2;
ncnn::Mat c2;
ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
ncnn::convert_packing(b2, c2, 4, opt);
ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
{
fprintf(stderr, "test_mat_pixel_roi_resize_bgra failed w=%d h=%d roi=[%d %d %d %d] target_width=%d target_height=%d pixel_type=%d\n", w, h, roix, roiy, roiw, roih, target_width, target_height, i);
return -1;
}
}
return 0;
}
static int test_mat_pixel_0()
{
for (int c = 1; c <= 4; c++)
{
int ret = 0
|| test_mat_pixel_resize(24, 48, c, 24, 48)
|| test_mat_pixel_resize(13, 17, c, 11, 14)
|| test_mat_pixel_resize(33, 23, c, 5, 6)
|| test_mat_pixel_resize(5, 4, c, 11, 16)
|| test_mat_pixel_resize(23, 11, c, 15, 21);
if (ret != 0)
return ret;
}
return 0;
}
static int test_mat_pixel_1()
{
return 0
|| test_mat_pixel_roi_resize_gray(16, 16, 1, 1, 13, 13, 10, 11)
|| test_mat_pixel_roi_resize_rgb(16, 16, 2, 1, 11, 11, 2, 3)
|| test_mat_pixel_roi_resize_bgr(16, 16, 1, 2, 11, 9, 22, 13)
|| test_mat_pixel_roi_resize_rgba(16, 16, 3, 2, 9, 11, 12, 4)
|| test_mat_pixel_roi_resize_bgra(16, 16, 2, 3, 9, 7, 7, 7);
}
static int test_mat_pixel_2()
{
return 0
|| test_mat_pixel_roi_resize_gray(15, 15, 2, 3, 2, 3, 2, 2)
|| test_mat_pixel_roi_resize_rgb(15, 15, 3, 4, 5, 4, 5, 4)
|| test_mat_pixel_roi_resize_bgr(15, 15, 4, 5, 6, 7, 4, 1)
|| test_mat_pixel_roi_resize_rgba(15, 15, 6, 6, 3, 4, 1, 3)
|| test_mat_pixel_roi_resize_bgra(15, 15, 7, 3, 1, 1, 1, 1);
}
int main()
{
SRAND(7767517);
return test_mat_pixel_0() || test_mat_pixel_1() || test_mat_pixel_2();
}

View File

@ -0,0 +1,224 @@
// 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 "mat.h"
#include "prng.h"
#include <string.h>
static struct prng_rand_t g_prng_rand_state;
#define SRAND(seed) prng_srand(seed, &g_prng_rand_state)
#define RAND() prng_rand(&g_prng_rand_state)
static ncnn::Mat RandomMat(int w, int h, int elempack)
{
ncnn::Mat m(w, h, (size_t)elempack, elempack);
unsigned char* p = m;
for (int i = 0; i < w * h * elempack; i++)
{
p[i] = RAND() % 256;
}
return m;
}
static int test_mat_pixel_rotate_c1(int w, int h)
{
ncnn::Mat a0 = RandomMat(w, h, 1);
ncnn::Mat a1(w, h, (size_t)1u, 1);
ncnn::Mat a2(w, h, (size_t)1u, 1);
ncnn::Mat a3(w, h, (size_t)1u, 1);
ncnn::Mat a4(w, h, (size_t)1u, 1);
ncnn::Mat a5(h, w, (size_t)1u, 1);
ncnn::Mat a6(w, h, (size_t)1u, 1);
ncnn::Mat a7(h, w, (size_t)1u, 1);
ncnn::Mat a8(w, h, (size_t)1u, 1);
ncnn::kanna_rotate_c1(a0, w, h, a1, w, h, 1);
ncnn::kanna_rotate_c1(a1, w, h, a2, w, h, 2);
ncnn::kanna_rotate_c1(a2, w, h, a3, w, h, 3);
ncnn::kanna_rotate_c1(a3, w, h, a4, w, h, 4);
ncnn::kanna_rotate_c1(a4, w, h, a5, h, w, 5);
ncnn::kanna_rotate_c1(a5, h, w, a6, w, h, 6);
ncnn::kanna_rotate_c1(a6, w, h, a7, h, w, 7);
ncnn::kanna_rotate_c1(a7, h, w, a8, w, h, 8);
if (memcmp(a0, a8, w * h * 1) != 0)
{
fprintf(stderr, "test_mat_pixel_rotate_c1 failed w=%d h=%d\n", w, h);
return -1;
}
return 0;
}
static int test_mat_pixel_rotate_c2(int w, int h)
{
ncnn::Mat a0 = RandomMat(w, h, 2);
ncnn::Mat a1(w, h, (size_t)2u, 2);
ncnn::Mat a2(w, h, (size_t)2u, 2);
ncnn::Mat a3(w, h, (size_t)2u, 2);
ncnn::Mat a4(w, h, (size_t)2u, 2);
ncnn::Mat a5(h, w, (size_t)2u, 2);
ncnn::Mat a6(w, h, (size_t)2u, 2);
ncnn::Mat a7(h, w, (size_t)2u, 2);
ncnn::Mat a8(w, h, (size_t)2u, 2);
ncnn::kanna_rotate_c2(a0, w, h, a1, w, h, 1);
ncnn::kanna_rotate_c2(a1, w, h, a2, w, h, 2);
ncnn::kanna_rotate_c2(a2, w, h, a3, w, h, 3);
ncnn::kanna_rotate_c2(a3, w, h, a4, w, h, 4);
ncnn::kanna_rotate_c2(a4, w, h, a5, h, w, 5);
ncnn::kanna_rotate_c2(a5, h, w, a6, w, h, 6);
ncnn::kanna_rotate_c2(a6, w, h, a7, h, w, 7);
ncnn::kanna_rotate_c2(a7, h, w, a8, w, h, 8);
if (memcmp(a0, a8, w * h * 2) != 0)
{
fprintf(stderr, "test_mat_pixel_rotate_c2 failed w=%d h=%d\n", w, h);
return -1;
}
return 0;
}
static int test_mat_pixel_rotate_c3(int w, int h)
{
ncnn::Mat a0 = RandomMat(w, h, 3);
ncnn::Mat a1(w, h, (size_t)3u, 3);
ncnn::Mat a2(w, h, (size_t)3u, 3);
ncnn::Mat a3(w, h, (size_t)3u, 3);
ncnn::Mat a4(w, h, (size_t)3u, 3);
ncnn::Mat a5(h, w, (size_t)3u, 3);
ncnn::Mat a6(w, h, (size_t)3u, 3);
ncnn::Mat a7(h, w, (size_t)3u, 3);
ncnn::Mat a8(w, h, (size_t)3u, 3);
ncnn::kanna_rotate_c3(a0, w, h, a1, w, h, 1);
ncnn::kanna_rotate_c3(a1, w, h, a2, w, h, 2);
ncnn::kanna_rotate_c3(a2, w, h, a3, w, h, 3);
ncnn::kanna_rotate_c3(a3, w, h, a4, w, h, 4);
ncnn::kanna_rotate_c3(a4, w, h, a5, h, w, 5);
ncnn::kanna_rotate_c3(a5, h, w, a6, w, h, 6);
ncnn::kanna_rotate_c3(a6, w, h, a7, h, w, 7);
ncnn::kanna_rotate_c3(a7, h, w, a8, w, h, 8);
if (memcmp(a0, a8, w * h * 3) != 0)
{
fprintf(stderr, "test_mat_pixel_rotate_c3 failed w=%d h=%d\n", w, h);
return -1;
}
return 0;
}
static int test_mat_pixel_rotate_c4(int w, int h)
{
ncnn::Mat a0 = RandomMat(w, h, 4);
ncnn::Mat a1(w, h, (size_t)4u, 4);
ncnn::Mat a2(w, h, (size_t)4u, 4);
ncnn::Mat a3(w, h, (size_t)4u, 4);
ncnn::Mat a4(w, h, (size_t)4u, 4);
ncnn::Mat a5(h, w, (size_t)4u, 4);
ncnn::Mat a6(w, h, (size_t)4u, 4);
ncnn::Mat a7(h, w, (size_t)4u, 4);
ncnn::Mat a8(w, h, (size_t)4u, 4);
ncnn::kanna_rotate_c4(a0, w, h, a1, w, h, 1);
ncnn::kanna_rotate_c4(a1, w, h, a2, w, h, 2);
ncnn::kanna_rotate_c4(a2, w, h, a3, w, h, 3);
ncnn::kanna_rotate_c4(a3, w, h, a4, w, h, 4);
ncnn::kanna_rotate_c4(a4, w, h, a5, h, w, 5);
ncnn::kanna_rotate_c4(a5, h, w, a6, w, h, 6);
ncnn::kanna_rotate_c4(a6, w, h, a7, h, w, 7);
ncnn::kanna_rotate_c4(a7, h, w, a8, w, h, 8);
if (memcmp(a0, a8, w * h * 4) != 0)
{
fprintf(stderr, "test_mat_pixel_rotate_c4 failed w=%d h=%d\n", w, h);
return -1;
}
return 0;
}
static int test_mat_pixel_rotate_0()
{
return 0
|| test_mat_pixel_rotate_c1(6, 7)
|| test_mat_pixel_rotate_c2(6, 7)
|| test_mat_pixel_rotate_c3(6, 7)
|| test_mat_pixel_rotate_c4(6, 7)
|| test_mat_pixel_rotate_c1(12, 16)
|| test_mat_pixel_rotate_c2(12, 16)
|| test_mat_pixel_rotate_c3(12, 16)
|| test_mat_pixel_rotate_c4(12, 16)
|| test_mat_pixel_rotate_c1(22, 33)
|| test_mat_pixel_rotate_c2(22, 33)
|| test_mat_pixel_rotate_c3(22, 33)
|| test_mat_pixel_rotate_c4(22, 33);
}
static int test_mat_pixel_rotate_yuv420sp(int w, int h)
{
ncnn::Mat a0 = RandomMat(w, h * 3 / 2, 1);
ncnn::Mat a1(w, h * 3 / 2, (size_t)1u, 1);
ncnn::Mat a2(w, h * 3 / 2, (size_t)1u, 1);
ncnn::Mat a3(w, h * 3 / 2, (size_t)1u, 1);
ncnn::Mat a4(w, h * 3 / 2, (size_t)1u, 1);
ncnn::Mat a5(h, w * 3 / 2, (size_t)1u, 1);
ncnn::Mat a6(w, h * 3 / 2, (size_t)1u, 1);
ncnn::Mat a7(h, w * 3 / 2, (size_t)1u, 1);
ncnn::Mat a8(w, h * 3 / 2, (size_t)1u, 1);
ncnn::kanna_rotate_yuv420sp(a0, w, h, a1, w, h, 1);
ncnn::kanna_rotate_yuv420sp(a1, w, h, a2, w, h, 2);
ncnn::kanna_rotate_yuv420sp(a2, w, h, a3, w, h, 3);
ncnn::kanna_rotate_yuv420sp(a3, w, h, a4, w, h, 4);
ncnn::kanna_rotate_yuv420sp(a4, w, h, a5, h, w, 5);
ncnn::kanna_rotate_yuv420sp(a5, h, w, a6, w, h, 6);
ncnn::kanna_rotate_yuv420sp(a6, w, h, a7, h, w, 7);
ncnn::kanna_rotate_yuv420sp(a7, h, w, a8, w, h, 8);
if (memcmp(a0, a8, w * h * 3 / 2) != 0)
{
fprintf(stderr, "test_mat_pixel_rotate_yuv420sp failed w=%d h=%d\n", w, h);
return -1;
}
return 0;
}
static int test_mat_pixel_rotate_1()
{
return 0
|| test_mat_pixel_rotate_yuv420sp(6, 4)
|| test_mat_pixel_rotate_yuv420sp(12, 16)
|| test_mat_pixel_rotate_yuv420sp(22, 34);
}
int main()
{
SRAND(7767517);
return 0
|| test_mat_pixel_rotate_0()
|| test_mat_pixel_rotate_1();
}

255
3rdparty/ncnn/tests/test_matmul.cpp vendored Normal file
View File

@ -0,0 +1,255 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2022 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/matmul.h"
#include "testutil.h"
static int test_matmul(const ncnn::Mat& a, const ncnn::Mat& b)
{
ncnn::ParamDict pd;
pd.set(0, 0); // transB
std::vector<ncnn::Mat> weights(0);
std::vector<ncnn::Mat> as(2);
as[0] = a;
as[1] = b;
int ret = test_layer<ncnn::MatMul>("MatMul", pd, weights, as);
if (ret != 0)
{
fprintf(stderr, "test_matmul failed a.dims=%d a=(%d %d %d %d) b.dims=%d b=(%d %d %d %d)\n", a.dims, a.w, a.h, a.d, a.c, b.dims, b.w, b.h, b.d, b.c);
}
return ret;
}
static int test_matmul_transb(const ncnn::Mat& a, const ncnn::Mat& b)
{
ncnn::ParamDict pd;
pd.set(0, 1); // transB
std::vector<ncnn::Mat> weights(0);
std::vector<ncnn::Mat> as(2);
as[0] = a;
as[1] = b;
int ret = test_layer<ncnn::MatMul>("MatMul", pd, weights, as);
if (ret != 0)
{
fprintf(stderr, "test_matmul_transb failed a.dims=%d a=(%d %d %d %d) b.dims=%d b=(%d %d %d %d)\n", a.dims, a.w, a.h, a.d, a.c, b.dims, b.w, b.h, b.d, b.c);
}
return ret;
}
static int test_matmul_0()
{
return 0
|| test_matmul(RandomMat(124), RandomMat(124))
|| test_matmul(RandomMat(127), RandomMat(127))
|| test_matmul(RandomMat(128), RandomMat(128));
}
static int test_matmul_1()
{
return 0
|| test_matmul(RandomMat(5), RandomMat(6, 5))
|| test_matmul(RandomMat(16), RandomMat(12, 16))
|| test_matmul(RandomMat(11), RandomMat(16, 11))
|| test_matmul_transb(RandomMat(5), RandomMat(5, 6))
|| test_matmul_transb(RandomMat(16), RandomMat(16, 12))
|| test_matmul_transb(RandomMat(11), RandomMat(11, 16));
}
static int test_matmul_2()
{
return 0
|| test_matmul(RandomMat(13), RandomMat(7, 13, 12))
|| test_matmul(RandomMat(24), RandomMat(6, 24, 16))
|| test_matmul(RandomMat(20), RandomMat(8, 20, 19))
|| test_matmul_transb(RandomMat(13), RandomMat(13, 7, 12))
|| test_matmul_transb(RandomMat(24), RandomMat(24, 6, 16))
|| test_matmul_transb(RandomMat(20), RandomMat(20, 8, 19));
}
static int test_matmul_3()
{
return 0
|| test_matmul(RandomMat(13), RandomMat(7, 13, 5, 12))
|| test_matmul(RandomMat(24), RandomMat(6, 24, 4, 16))
|| test_matmul(RandomMat(20), RandomMat(8, 20, 3, 19))
|| test_matmul_transb(RandomMat(13), RandomMat(13, 7, 5, 12))
|| test_matmul_transb(RandomMat(24), RandomMat(24, 6, 4, 16))
|| test_matmul_transb(RandomMat(20), RandomMat(20, 8, 3, 19));
}
static int test_matmul_4()
{
return 0
|| test_matmul(RandomMat(5, 6), RandomMat(5))
|| test_matmul(RandomMat(16, 12), RandomMat(16))
|| test_matmul(RandomMat(11, 16), RandomMat(11));
}
static int test_matmul_5()
{
return 0
|| test_matmul(RandomMat(32, 3, 10), RandomMat(32))
|| test_matmul(RandomMat(31, 4, 16), RandomMat(31))
|| test_matmul(RandomMat(28, 5, 28), RandomMat(28));
}
static int test_matmul_6()
{
return 0
|| test_matmul(RandomMat(32, 3, 4, 10), RandomMat(32))
|| test_matmul(RandomMat(31, 4, 5, 16), RandomMat(31))
|| test_matmul(RandomMat(18, 5, 6, 28), RandomMat(18));
}
static int test_matmul_7()
{
return 0
|| test_matmul(RandomMat(14, 10), RandomMat(5, 14))
|| test_matmul(RandomMat(16, 16), RandomMat(10, 16))
|| test_matmul(RandomMat(14, 28), RandomMat(9, 14))
|| test_matmul_transb(RandomMat(14, 10), RandomMat(14, 5))
|| test_matmul_transb(RandomMat(16, 16), RandomMat(16, 10))
|| test_matmul_transb(RandomMat(14, 28), RandomMat(14, 9));
}
static int test_matmul_8()
{
return 0
|| test_matmul(RandomMat(5, 4), RandomMat(4, 5, 12))
|| test_matmul(RandomMat(5, 14), RandomMat(5, 5, 16))
|| test_matmul(RandomMat(5, 24), RandomMat(6, 5, 19))
|| test_matmul_transb(RandomMat(5, 4), RandomMat(5, 4, 12))
|| test_matmul_transb(RandomMat(5, 14), RandomMat(5, 5, 16))
|| test_matmul_transb(RandomMat(5, 24), RandomMat(5, 6, 19));
}
static int test_matmul_9()
{
return 0
|| test_matmul(RandomMat(5, 4), RandomMat(4, 5, 2, 12))
|| test_matmul(RandomMat(5, 14), RandomMat(5, 5, 3, 16))
|| test_matmul(RandomMat(5, 24), RandomMat(6, 5, 4, 19))
|| test_matmul_transb(RandomMat(5, 4), RandomMat(5, 4, 2, 12))
|| test_matmul_transb(RandomMat(5, 14), RandomMat(5, 5, 3, 16))
|| test_matmul_transb(RandomMat(5, 24), RandomMat(5, 6, 4, 19));
}
static int test_matmul_10()
{
return 0
|| test_matmul(RandomMat(14, 23, 10), RandomMat(5, 14))
|| test_matmul(RandomMat(16, 22, 16), RandomMat(10, 16))
|| test_matmul(RandomMat(14, 20, 28), RandomMat(9, 14))
|| test_matmul_transb(RandomMat(14, 23, 10), RandomMat(14, 5))
|| test_matmul_transb(RandomMat(16, 22, 16), RandomMat(16, 10))
|| test_matmul_transb(RandomMat(14, 20, 28), RandomMat(14, 9));
}
static int test_matmul_11()
{
return 0
|| test_matmul(RandomMat(14, 13, 2, 10), RandomMat(5, 14))
|| test_matmul(RandomMat(16, 12, 3, 16), RandomMat(10, 16))
|| test_matmul(RandomMat(14, 10, 4, 28), RandomMat(9, 14))
|| test_matmul_transb(RandomMat(14, 13, 2, 10), RandomMat(14, 5))
|| test_matmul_transb(RandomMat(16, 12, 3, 16), RandomMat(16, 10))
|| test_matmul_transb(RandomMat(14, 10, 4, 28), RandomMat(14, 9));
}
static int test_matmul_12()
{
return 0
|| test_matmul(RandomMat(14, 23, 10), RandomMat(5, 14, 10))
|| test_matmul(RandomMat(16, 22, 16), RandomMat(10, 16, 16))
|| test_matmul(RandomMat(14, 20, 28), RandomMat(9, 14, 28))
|| test_matmul_transb(RandomMat(14, 23, 10), RandomMat(14, 5, 10))
|| test_matmul_transb(RandomMat(16, 22, 16), RandomMat(16, 10, 16))
|| test_matmul_transb(RandomMat(14, 20, 28), RandomMat(14, 9, 28));
}
static int test_matmul_13()
{
return 0
|| test_matmul(RandomMat(14, 23, 10), RandomMat(5, 14, 1, 16))
|| test_matmul(RandomMat(16, 22, 9), RandomMat(10, 16, 1, 17))
|| test_matmul(RandomMat(14, 20, 8), RandomMat(9, 14, 1, 18))
|| test_matmul_transb(RandomMat(14, 23, 10), RandomMat(14, 5, 1, 16))
|| test_matmul_transb(RandomMat(16, 22, 9), RandomMat(16, 10, 1, 17))
|| test_matmul_transb(RandomMat(14, 20, 8), RandomMat(14, 9, 1, 18));
}
static int test_matmul_14()
{
return 0
|| test_matmul(RandomMat(14, 23, 10, 1), RandomMat(5, 14, 1, 16))
|| test_matmul(RandomMat(16, 22, 9, 1), RandomMat(10, 16, 1, 17))
|| test_matmul(RandomMat(14, 20, 8, 1), RandomMat(9, 14, 1, 18))
|| test_matmul_transb(RandomMat(14, 23, 10, 1), RandomMat(14, 5, 1, 16))
|| test_matmul_transb(RandomMat(16, 22, 9, 1), RandomMat(16, 10, 1, 17))
|| test_matmul_transb(RandomMat(14, 20, 8, 1), RandomMat(14, 9, 1, 18));
}
static int test_matmul_15()
{
return 0
|| test_matmul(RandomMat(14, 23, 10, 16), RandomMat(5, 14, 10, 16))
|| test_matmul(RandomMat(16, 22, 9, 17), RandomMat(10, 16, 9, 17))
|| test_matmul(RandomMat(14, 20, 8, 18), RandomMat(9, 14, 8, 18))
|| test_matmul_transb(RandomMat(14, 23, 10, 16), RandomMat(14, 5, 10, 16))
|| test_matmul_transb(RandomMat(16, 22, 9, 17), RandomMat(16, 10, 9, 17))
|| test_matmul_transb(RandomMat(14, 20, 8, 18), RandomMat(14, 9, 8, 18));
}
int main()
{
SRAND(7767517);
return 0
|| test_matmul_0()
|| test_matmul_1()
|| test_matmul_2()
|| test_matmul_3()
|| test_matmul_4()
|| test_matmul_5()
|| test_matmul_6()
|| test_matmul_7()
|| test_matmul_8()
|| test_matmul_9()
|| test_matmul_10()
|| test_matmul_11()
|| test_matmul_12()
|| test_matmul_13()
|| test_matmul_14()
|| test_matmul_15();
}

68
3rdparty/ncnn/tests/test_memorydata.cpp vendored Normal file
View File

@ -0,0 +1,68 @@
// 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/memorydata.h"
#include "testutil.h"
static int test_memorydata(const ncnn::Mat& a)
{
ncnn::ParamDict pd;
pd.set(0, a.w);
pd.set(1, a.h);
pd.set(2, a.c);
std::vector<ncnn::Mat> weights(1);
weights[0] = a;
std::vector<ncnn::Mat> as(0);
int ret = test_layer<ncnn::MemoryData>("MemoryData", pd, weights, as, 1);
if (ret != 0)
{
fprintf(stderr, "test_memorydata failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
}
return ret;
}
static int test_memorydata_0()
{
return 0
|| test_memorydata(RandomMat(5, 7, 16))
|| test_memorydata(RandomMat(3, 5, 13));
}
static int test_memorydata_1()
{
return 0
|| test_memorydata(RandomMat(6, 16))
|| test_memorydata(RandomMat(7, 15));
}
static int test_memorydata_2()
{
return 0
|| test_memorydata(RandomMat(128))
|| test_memorydata(RandomMat(127));
}
int main()
{
SRAND(7767517);
return 0
|| test_memorydata_0()
|| test_memorydata_1()
|| test_memorydata_2();
}

65
3rdparty/ncnn/tests/test_mish.cpp vendored Normal file
View File

@ -0,0 +1,65 @@
// 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/mish.h"
#include "testutil.h"
static int test_mish(const ncnn::Mat& a)
{
ncnn::ParamDict pd;
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Mish>("Mish", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_mish failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
}
return ret;
}
static int test_mish_0()
{
return 0
|| test_mish(RandomMat(5, 7, 24))
|| test_mish(RandomMat(7, 9, 12))
|| test_mish(RandomMat(3, 5, 13));
}
static int test_mish_1()
{
return 0
|| test_mish(RandomMat(15, 24))
|| test_mish(RandomMat(17, 12))
|| test_mish(RandomMat(19, 15));
}
static int test_mish_2()
{
return 0
|| test_mish(RandomMat(128))
|| test_mish(RandomMat(124))
|| test_mish(RandomMat(127));
}
int main()
{
SRAND(7767517);
return 0
|| test_mish_0()
|| test_mish_1()
|| test_mish_2();
}

View File

@ -0,0 +1,103 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 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/multiheadattention.h"
#include "testutil.h"
static int test_multiheadattention(const ncnn::Mat& a, int num_heads)
{
int embed_dim = a.w;
ncnn::ParamDict pd;
pd.set(0, embed_dim);
pd.set(1, num_heads);
pd.set(2, embed_dim * embed_dim);
std::vector<ncnn::Mat> weights(8);
weights[0] = RandomMat(embed_dim * embed_dim);
weights[1] = RandomMat(embed_dim);
weights[2] = RandomMat(embed_dim * embed_dim);
weights[3] = RandomMat(embed_dim);
weights[4] = RandomMat(embed_dim * embed_dim);
weights[5] = RandomMat(embed_dim);
weights[6] = RandomMat(embed_dim * embed_dim);
weights[7] = RandomMat(embed_dim);
std::vector<ncnn::Mat> as(3);
as[0] = a;
as[1] = a;
as[2] = a;
int ret = test_layer<ncnn::MultiHeadAttention>("MultiHeadAttention", pd, weights, as);
if (ret != 0)
{
fprintf(stderr, "test_multiheadattention failed a=(%d %d)\n", a.w, a.h);
}
return ret;
}
static int test_multiheadattention_sameqkv(const ncnn::Mat& a, int num_heads)
{
int embed_dim = a.w;
ncnn::ParamDict pd;
pd.set(0, embed_dim);
pd.set(1, num_heads);
pd.set(2, embed_dim * embed_dim);
std::vector<ncnn::Mat> weights(8);
weights[0] = RandomMat(embed_dim * embed_dim);
weights[1] = RandomMat(embed_dim);
weights[2] = RandomMat(embed_dim * embed_dim);
weights[3] = RandomMat(embed_dim);
weights[4] = RandomMat(embed_dim * embed_dim);
weights[5] = RandomMat(embed_dim);
weights[6] = RandomMat(embed_dim * embed_dim);
weights[7] = RandomMat(embed_dim);
std::vector<ncnn::Mat> as(1);
as[0] = a;
int ret = test_layer<ncnn::MultiHeadAttention>("MultiHeadAttention", pd, weights, as);
if (ret != 0)
{
fprintf(stderr, "test_multiheadattention failed a=(%d %d)\n", a.w, a.h);
}
return ret;
}
static int test_multiheadattention_0()
{
return 0
|| test_multiheadattention(RandomMat(64, 128), 4)
|| test_multiheadattention(RandomMat(64, 127), 16);
}
static int test_multiheadattention_1()
{
return 0
|| test_multiheadattention_sameqkv(RandomMat(64, 128), 8)
|| test_multiheadattention_sameqkv(RandomMat(64, 127), 32);
}
int main()
{
SRAND(7767517);
return 0
|| test_multiheadattention_0()
|| test_multiheadattention_1();
}

68
3rdparty/ncnn/tests/test_noop.cpp vendored Normal file
View File

@ -0,0 +1,68 @@
// 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/noop.h"
#include "testutil.h"
static int test_noop(const ncnn::Mat& a)
{
ncnn::ParamDict pd;
std::vector<ncnn::Mat> weights(0);
std::vector<ncnn::Mat> as(1);
as[0] = a;
int ret = test_layer<ncnn::Noop>("Noop", pd, weights, as, 1);
if (ret != 0)
{
fprintf(stderr, "test_noop failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
}
return ret;
}
static int test_noop_0()
{
return 0
|| test_noop(RandomMat(5, 7, 24))
|| test_noop(RandomMat(7, 9, 12))
|| test_noop(RandomMat(3, 5, 13));
}
static int test_noop_1()
{
return 0
|| test_noop(RandomMat(15, 24))
|| test_noop(RandomMat(17, 12))
|| test_noop(RandomMat(19, 15));
}
static int test_noop_2()
{
return 0
|| test_noop(RandomMat(128))
|| test_noop(RandomMat(124))
|| test_noop(RandomMat(127));
}
int main()
{
SRAND(7767517);
return 0
|| test_noop_0()
|| test_noop_1()
|| test_noop_2();
}

110
3rdparty/ncnn/tests/test_normalize.cpp vendored Normal file
View File

@ -0,0 +1,110 @@
// 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/normalize.h"
#include "testutil.h"
static int test_normalize(const ncnn::Mat& a, int across_spatial, int across_channel, int channel_shared, float eps, int eps_mode)
{
int scale_data_size = channel_shared ? 1 : a.c;
ncnn::ParamDict pd;
pd.set(0, across_spatial);
pd.set(4, across_channel);
pd.set(1, channel_shared);
pd.set(2, eps);
pd.set(3, scale_data_size);
pd.set(9, eps_mode);
std::vector<ncnn::Mat> weights(1);
weights[0] = RandomMat(scale_data_size);
int ret = test_layer<ncnn::Normalize>("Normalize", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_normalize failed a.dims=%d a=(%d %d %d) across_spatial=%d across_channel=%d channel_shared=%d eps=%f eps_mode=%d\n", a.dims, a.w, a.h, a.c, across_spatial, across_channel, channel_shared, eps, eps_mode);
}
return ret;
}
static int test_normalize_0()
{
ncnn::Mat a = RandomMat(6, 4, 2);
ncnn::Mat b = RandomMat(5, 7, 8);
return 0
|| test_normalize(a, 1, 0, 0, 0.01f, 0)
|| test_normalize(a, 1, 0, 0, 0.001f, 1)
|| test_normalize(a, 1, 0, 0, 0.002f, 2)
|| test_normalize(a, 1, 0, 1, 0.01f, 0)
|| test_normalize(a, 1, 0, 1, 0.001f, 1)
|| test_normalize(a, 1, 0, 1, 0.002f, 2)
|| test_normalize(b, 1, 0, 0, 0.01f, 0)
|| test_normalize(b, 1, 0, 0, 0.001f, 1)
|| test_normalize(b, 1, 0, 0, 0.002f, 2)
|| test_normalize(b, 1, 0, 1, 0.01f, 0)
|| test_normalize(b, 1, 0, 1, 0.001f, 1)
|| test_normalize(b, 1, 0, 1, 0.002f, 2);
}
static int test_normalize_1()
{
ncnn::Mat a = RandomMat(5, 6, 3);
ncnn::Mat b = RandomMat(3, 4, 8);
return 0
|| test_normalize(a, 0, 1, 0, 0.01f, 0)
|| test_normalize(a, 0, 1, 0, 0.001f, 1)
|| test_normalize(a, 0, 1, 0, 0.002f, 2)
|| test_normalize(a, 0, 1, 1, 0.01f, 0)
|| test_normalize(a, 0, 1, 1, 0.001f, 1)
|| test_normalize(a, 0, 1, 1, 0.002f, 2)
|| test_normalize(b, 0, 1, 0, 0.01f, 0)
|| test_normalize(b, 0, 1, 0, 0.001f, 1)
|| test_normalize(b, 0, 1, 0, 0.002f, 2)
|| test_normalize(b, 0, 1, 1, 0.01f, 0)
|| test_normalize(b, 0, 1, 1, 0.001f, 1)
|| test_normalize(b, 0, 1, 1, 0.002f, 2);
}
static int test_normalize_2()
{
ncnn::Mat a = RandomMat(2, 3, 5);
ncnn::Mat b = RandomMat(4, 6, 8);
return 0
|| test_normalize(a, 1, 1, 0, 0.01f, 0)
|| test_normalize(a, 1, 1, 0, 0.001f, 1)
|| test_normalize(a, 1, 1, 0, 0.002f, 2)
|| test_normalize(a, 1, 1, 1, 0.01f, 0)
|| test_normalize(a, 1, 1, 1, 0.001f, 1)
|| test_normalize(a, 1, 1, 1, 0.002f, 2)
|| test_normalize(b, 1, 1, 0, 0.01f, 0)
|| test_normalize(b, 1, 1, 0, 0.001f, 1)
|| test_normalize(b, 1, 1, 0, 0.002f, 2)
|| test_normalize(b, 1, 1, 1, 0.01f, 0)
|| test_normalize(b, 1, 1, 1, 0.001f, 1)
|| test_normalize(b, 1, 1, 1, 0.002f, 2);
}
int main()
{
SRAND(7767517);
return 0
|| test_normalize_0()
|| test_normalize_1()
|| test_normalize_2();
}

828
3rdparty/ncnn/tests/test_packing.cpp vendored Normal file
View File

@ -0,0 +1,828 @@
// 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/packing.h"
#include "testutil.h"
static int test_packing_cpu_fp32(const ncnn::Mat& a, int in_elempack, int out_elempack)
{
ncnn::ParamDict pd;
pd.set(0, out_elempack);
std::vector<ncnn::Mat> weights(0);
ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = false;
opt.use_int8_inference = false;
opt.use_fp16_storage = false;
opt.use_fp16_arithmetic = false;
opt.use_packing_layout = false;
ncnn::Layer* op = ncnn::create_layer("Packing");
op->load_param(pd);
ncnn::ModelBinFromMatArray mb(weights.data());
op->load_model(mb);
op->create_pipeline(opt);
ncnn::Mat ap;
ncnn::convert_packing(a, ap, in_elempack, opt);
ncnn::Mat b;
((ncnn::Packing*)op)->ncnn::Packing::forward(ap, b, opt);
ncnn::Mat c;
op->forward(ap, c, opt);
op->destroy_pipeline(opt);
delete op;
if (CompareMat(b, c, 0.001) != 0)
{
fprintf(stderr, "test_packing_cpu_fp32 failed a.dims=%d a=(%d %d %d %d) in_elempack=%d out_elempack=%d\n", a.dims, a.w, a.h, a.d, a.c, in_elempack, out_elempack);
return -1;
}
return 0;
}
static int test_packing_cpu_fp16(const ncnn::Mat& a, int in_elempack, int out_elempack)
{
ncnn::ParamDict pd;
pd.set(0, out_elempack);
std::vector<ncnn::Mat> weights(0);
ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = false;
opt.use_int8_inference = false;
opt.use_fp16_storage = true;
opt.use_fp16_arithmetic = true;
opt.use_packing_layout = false;
ncnn::Layer* op = ncnn::create_layer("Packing");
if (!op->support_fp16_storage)
{
delete op;
return 0;
}
op->load_param(pd);
ncnn::ModelBinFromMatArray mb(weights.data());
op->load_model(mb);
op->create_pipeline(opt);
ncnn::Mat a16;
ncnn::cast_float32_to_float16(a, a16, opt);
ncnn::Mat ap;
ncnn::convert_packing(a16, ap, in_elempack, opt);
ncnn::Mat b;
((ncnn::Packing*)op)->ncnn::Packing::forward(ap, b, opt);
ncnn::Mat c;
op->forward(ap, c, opt);
op->destroy_pipeline(opt);
delete op;
ncnn::Mat c32;
ncnn::cast_float16_to_float32(c, c32, opt);
if (CompareMat(b, c32, 0.001) != 0)
{
fprintf(stderr, "test_packing_cpu_fp16 failed a.dims=%d a=(%d %d %d %d) in_elempack=%d out_elempack=%d\n", a.dims, a.w, a.h, a.d, a.c, in_elempack, out_elempack);
return -1;
}
return 0;
}
static int test_packing_cpu_int8(const ncnn::Mat& a, int in_elempack, int out_elempack)
{
ncnn::ParamDict pd;
pd.set(0, out_elempack);
std::vector<ncnn::Mat> weights(0);
ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = false;
opt.use_int8_inference = false;
opt.use_fp16_storage = false;
opt.use_fp16_arithmetic = false;
opt.use_packing_layout = false;
ncnn::Layer* op = ncnn::create_layer("Packing");
op->load_param(pd);
ncnn::ModelBinFromMatArray mb(weights.data());
op->load_model(mb);
op->create_pipeline(opt);
ncnn::Mat a8;
if (a.dims == 1) a8 = RandomS8Mat(a.w);
if (a.dims == 2) a8 = RandomS8Mat(a.w, a.h);
if (a.dims == 3) a8 = RandomS8Mat(a.w, a.h, a.c);
if (a.dims == 4) a8 = RandomS8Mat(a.w, a.h, a.d, a.c);
ncnn::Mat ap;
ncnn::convert_packing(a8, ap, in_elempack, opt);
ncnn::Mat b;
((ncnn::Packing*)op)->ncnn::Packing::forward(ap, b, opt);
ncnn::Mat c;
op->forward(ap, c, opt);
op->destroy_pipeline(opt);
delete op;
ncnn::Mat b32;
ncnn::cast_int8_to_float32(b, b32, opt);
ncnn::Mat c32;
ncnn::cast_int8_to_float32(c, c32, opt);
if (CompareMat(b32, c32, 0.001) != 0)
{
fprintf(stderr, "test_packing_cpu_int8 failed a.dims=%d a=(%d %d %d %d) in_elempack=%d out_elempack=%d\n", a.dims, a.w, a.h, a.d, a.c, in_elempack, out_elempack);
return -1;
}
return 0;
}
static int test_packing_cpu(const ncnn::Mat& a, int in_elempack, int out_elempack)
{
return 0
|| test_packing_cpu_fp32(a, in_elempack, out_elempack)
|| test_packing_cpu_fp16(a, in_elempack, out_elempack)
|| test_packing_cpu_int8(a, in_elempack, out_elempack);
}
#if NCNN_VULKAN
#include "layer/vulkan/packing_vulkan.h"
static int test_packing_gpu_buffer(const ncnn::Mat& a, int in_elempack, int out_elempack)
{
ncnn::ParamDict pd;
pd.set(0, out_elempack);
pd.set(2, 1); // cast_type_from
pd.set(3, 1); // cast_type_to
pd.set(4, 0); // storage_type_from
pd.set(5, 0); // storage_type_to
std::vector<ncnn::Mat> weights(0);
ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = true;
opt.use_int8_inference = false;
opt.use_fp16_packed = false;
opt.use_fp16_storage = false;
opt.use_fp16_arithmetic = false;
opt.use_int8_storage = false;
opt.use_int8_arithmetic = false;
opt.use_packing_layout = true;
opt.use_shader_pack8 = true;
opt.use_image_storage = false;
ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator();
ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator();
opt.blob_vkallocator = blob_vkallocator;
opt.workspace_vkallocator = blob_vkallocator;
opt.staging_vkallocator = staging_vkallocator;
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;
ncnn::Layer* op = ncnn::create_layer("Packing");
op->vkdev = vkdev;
op->load_param(pd);
ncnn::ModelBinFromMatArray mb(weights.data());
op->load_model(mb);
op->create_pipeline(opt);
ncnn::Mat ap;
ncnn::convert_packing(a, ap, in_elempack, opt);
ncnn::Mat b;
((ncnn::Packing*)op)->ncnn::Packing::forward(ap, b, opt);
ncnn::Mat d;
// forward
ncnn::VkCompute cmd(vkdev);
// upload
ncnn::VkMat a_gpu;
cmd.record_clone(ap, a_gpu, opt);
ncnn::VkMat d_gpu;
op->forward(a_gpu, d_gpu, cmd, opt);
// download
cmd.record_clone(d_gpu, d, opt);
cmd.submit_and_wait();
op->destroy_pipeline(opt);
delete op;
vkdev->reclaim_blob_allocator(blob_vkallocator);
vkdev->reclaim_staging_allocator(staging_vkallocator);
if (CompareMat(b, d, 0.001) != 0)
{
fprintf(stderr, "test_packing_gpu_buffer failed a.dims=%d a=(%d %d %d %d) in_elempack=%d out_elempack=%d\n", a.dims, a.w, a.h, a.d, a.c, in_elempack, out_elempack);
return -1;
}
return 0;
}
static int test_packing_gpu_image(const ncnn::Mat& a, int in_elempack, int out_elempack)
{
ncnn::ParamDict pd;
pd.set(0, out_elempack);
pd.set(2, 1); // cast_type_from
pd.set(3, 1); // cast_type_to
pd.set(4, 1); // storage_type_from
pd.set(5, 1); // storage_type_to
std::vector<ncnn::Mat> weights(0);
ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = true;
opt.use_int8_inference = false;
opt.use_fp16_packed = false;
opt.use_fp16_storage = false;
opt.use_fp16_arithmetic = false;
opt.use_int8_storage = false;
opt.use_int8_arithmetic = false;
opt.use_packing_layout = true;
opt.use_shader_pack8 = true;
opt.use_image_storage = true;
ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator();
ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator();
opt.blob_vkallocator = blob_vkallocator;
opt.workspace_vkallocator = blob_vkallocator;
opt.staging_vkallocator = staging_vkallocator;
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;
ncnn::Layer* op = ncnn::create_layer("Packing");
op->vkdev = vkdev;
op->load_param(pd);
ncnn::ModelBinFromMatArray mb(weights.data());
op->load_model(mb);
op->create_pipeline(opt);
ncnn::Mat ap;
ncnn::convert_packing(a, ap, in_elempack, opt);
ncnn::Mat b;
((ncnn::Packing*)op)->ncnn::Packing::forward(ap, b, opt);
ncnn::Mat d;
// forward
ncnn::VkCompute cmd(vkdev);
// upload
ncnn::VkImageMat a_gpu;
cmd.record_clone(ap, a_gpu, opt);
ncnn::VkImageMat d_gpu;
op->forward(a_gpu, d_gpu, cmd, opt);
// download
cmd.record_clone(d_gpu, d, opt);
cmd.submit_and_wait();
op->destroy_pipeline(opt);
delete op;
vkdev->reclaim_blob_allocator(blob_vkallocator);
vkdev->reclaim_staging_allocator(staging_vkallocator);
if (CompareMat(b, d, 0.001) != 0)
{
fprintf(stderr, "test_packing_gpu_image failed a.dims=%d a=(%d %d %d %d) in_elempack=%d out_elempack=%d\n", a.dims, a.w, a.h, a.d, a.c, in_elempack, out_elempack);
return -1;
}
return 0;
}
static int test_packing_gpu_buffer2image(const ncnn::Mat& a, int in_elempack, int out_elempack)
{
ncnn::ParamDict pd;
pd.set(0, out_elempack);
pd.set(2, 1); // cast_type_from
pd.set(3, 1); // cast_type_to
pd.set(4, 0); // storage_type_from
pd.set(5, 1); // storage_type_to
std::vector<ncnn::Mat> weights(0);
ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = true;
opt.use_int8_inference = false;
opt.use_fp16_packed = false;
opt.use_fp16_storage = false;
opt.use_fp16_arithmetic = false;
opt.use_int8_storage = false;
opt.use_int8_arithmetic = false;
opt.use_packing_layout = true;
opt.use_shader_pack8 = true;
opt.use_image_storage = true;
ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator();
ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator();
opt.blob_vkallocator = blob_vkallocator;
opt.workspace_vkallocator = blob_vkallocator;
opt.staging_vkallocator = staging_vkallocator;
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;
ncnn::Packing_vulkan* op = new ncnn::Packing_vulkan;
op->vkdev = vkdev;
op->load_param(pd);
ncnn::ModelBinFromMatArray mb(weights.data());
op->load_model(mb);
op->create_pipeline(opt);
ncnn::Mat ap;
ncnn::convert_packing(a, ap, in_elempack, opt);
ncnn::Mat b;
((ncnn::Packing*)op)->ncnn::Packing::forward(ap, b, opt);
ncnn::Mat d;
// forward
ncnn::VkCompute cmd(vkdev);
// upload
ncnn::VkMat a_gpu;
cmd.record_clone(ap, a_gpu, opt);
ncnn::VkImageMat d_gpu;
op->forward(a_gpu, d_gpu, cmd, opt);
// download
cmd.record_clone(d_gpu, d, opt);
cmd.submit_and_wait();
op->destroy_pipeline(opt);
delete op;
vkdev->reclaim_blob_allocator(blob_vkallocator);
vkdev->reclaim_staging_allocator(staging_vkallocator);
if (CompareMat(b, d, 0.001) != 0)
{
fprintf(stderr, "test_packing_gpu_buffer2image failed a.dims=%d a=(%d %d %d %d) in_elempack=%d out_elempack=%d\n", a.dims, a.w, a.h, a.d, a.c, in_elempack, out_elempack);
return -1;
}
return 0;
}
static int test_packing_gpu_image2buffer(const ncnn::Mat& a, int in_elempack, int out_elempack)
{
ncnn::ParamDict pd;
pd.set(0, out_elempack);
pd.set(2, 1); // cast_type_from
pd.set(3, 1); // cast_type_to
pd.set(4, 1); // storage_type_from
pd.set(5, 0); // storage_type_to
std::vector<ncnn::Mat> weights(0);
ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = true;
opt.use_int8_inference = false;
opt.use_fp16_packed = false;
opt.use_fp16_storage = false;
opt.use_fp16_arithmetic = false;
opt.use_int8_storage = false;
opt.use_int8_arithmetic = false;
opt.use_packing_layout = true;
opt.use_shader_pack8 = true;
opt.use_image_storage = true;
ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator();
ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator();
opt.blob_vkallocator = blob_vkallocator;
opt.workspace_vkallocator = blob_vkallocator;
opt.staging_vkallocator = staging_vkallocator;
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;
ncnn::Packing_vulkan* op = new ncnn::Packing_vulkan;
op->vkdev = vkdev;
op->load_param(pd);
ncnn::ModelBinFromMatArray mb(weights.data());
op->load_model(mb);
op->create_pipeline(opt);
ncnn::Mat ap;
ncnn::convert_packing(a, ap, in_elempack, opt);
ncnn::Mat b;
((ncnn::Packing*)op)->ncnn::Packing::forward(ap, b, opt);
ncnn::Mat d;
// forward
ncnn::VkCompute cmd(vkdev);
// upload
ncnn::VkImageMat a_gpu;
cmd.record_clone(ap, a_gpu, opt);
ncnn::VkMat d_gpu;
op->forward(a_gpu, d_gpu, cmd, opt);
// download
cmd.record_clone(d_gpu, d, opt);
cmd.submit_and_wait();
op->destroy_pipeline(opt);
delete op;
vkdev->reclaim_blob_allocator(blob_vkallocator);
vkdev->reclaim_staging_allocator(staging_vkallocator);
if (CompareMat(b, d, 0.001) != 0)
{
fprintf(stderr, "test_packing_gpu_image2buffer failed a.dims=%d a=(%d %d %d %d) in_elempack=%d out_elempack=%d\n", a.dims, a.w, a.h, a.d, a.c, in_elempack, out_elempack);
return -1;
}
return 0;
}
#endif
static int test_packing_0()
{
ncnn::Mat a = RandomMat(9, 7, 10, 16);
ncnn::Mat b = RandomMat(9, 7, 10, 3);
return 0
|| test_packing_cpu(a, 1, 1)
|| test_packing_cpu(a, 4, 4)
|| test_packing_cpu(a, 4, 8)
|| test_packing_cpu(a, 1, 4)
|| test_packing_cpu(a, 4, 1)
|| test_packing_cpu(a, 1, 8)
|| test_packing_cpu(a, 8, 1)
|| test_packing_cpu(a, 4, 8)
|| test_packing_cpu(a, 8, 4)
|| test_packing_cpu(a, 1, 16)
|| test_packing_cpu(a, 16, 1)
|| test_packing_cpu(a, 4, 16)
|| test_packing_cpu(a, 16, 4)
|| test_packing_cpu(a, 8, 16)
|| test_packing_cpu(a, 16, 8)
|| test_packing_cpu(b, 1, 1)
|| test_packing_cpu(b, 4, 4)
|| test_packing_cpu(b, 4, 8)
|| test_packing_cpu(b, 1, 4)
|| test_packing_cpu(b, 4, 1)
|| test_packing_cpu(b, 1, 8)
|| test_packing_cpu(b, 8, 1)
|| test_packing_cpu(b, 4, 8)
|| test_packing_cpu(b, 8, 4)
|| test_packing_cpu(b, 1, 16)
|| test_packing_cpu(b, 16, 1)
|| test_packing_cpu(b, 4, 16)
|| test_packing_cpu(b, 16, 4)
|| test_packing_cpu(b, 8, 16)
|| test_packing_cpu(b, 16, 8)
#if NCNN_VULKAN
|| test_packing_gpu_buffer(a, 1, 1)
|| test_packing_gpu_buffer(a, 4, 4)
|| test_packing_gpu_buffer(a, 8, 8)
|| test_packing_gpu_buffer(a, 1, 4)
|| test_packing_gpu_buffer(a, 4, 1)
|| test_packing_gpu_buffer(a, 1, 8)
|| test_packing_gpu_buffer(a, 8, 1)
|| test_packing_gpu_buffer(a, 4, 8)
|| test_packing_gpu_buffer(a, 8, 4)
|| test_packing_gpu_image(a, 1, 1)
|| test_packing_gpu_image(a, 4, 4)
|| test_packing_gpu_image(a, 8, 8)
|| test_packing_gpu_image(a, 1, 4)
|| test_packing_gpu_image(a, 4, 1)
|| test_packing_gpu_image(a, 1, 8)
|| test_packing_gpu_image(a, 8, 1)
|| test_packing_gpu_image(a, 4, 8)
|| test_packing_gpu_image(a, 8, 4)
|| test_packing_gpu_buffer2image(a, 1, 1)
|| test_packing_gpu_buffer2image(a, 4, 4)
|| test_packing_gpu_buffer2image(a, 8, 8)
|| test_packing_gpu_buffer2image(a, 1, 4)
|| test_packing_gpu_buffer2image(a, 4, 1)
|| test_packing_gpu_buffer2image(a, 1, 8)
|| test_packing_gpu_buffer2image(a, 8, 1)
|| test_packing_gpu_buffer2image(a, 4, 8)
|| test_packing_gpu_buffer2image(a, 8, 4)
|| test_packing_gpu_image2buffer(a, 1, 1)
|| test_packing_gpu_image2buffer(a, 4, 4)
|| test_packing_gpu_image2buffer(a, 8, 8)
|| test_packing_gpu_image2buffer(a, 1, 4)
|| test_packing_gpu_image2buffer(a, 4, 1)
|| test_packing_gpu_image2buffer(a, 1, 8)
|| test_packing_gpu_image2buffer(a, 8, 1)
|| test_packing_gpu_image2buffer(a, 4, 8)
|| test_packing_gpu_image2buffer(a, 8, 4)
#endif // NCNN_VULKAN
;
}
static int test_packing_1()
{
ncnn::Mat a = RandomMat(9, 10, 16);
ncnn::Mat b = RandomMat(9, 10, 3);
return 0
|| test_packing_cpu(a, 1, 1)
|| test_packing_cpu(a, 4, 4)
|| test_packing_cpu(a, 4, 8)
|| test_packing_cpu(a, 1, 4)
|| test_packing_cpu(a, 4, 1)
|| test_packing_cpu(a, 1, 8)
|| test_packing_cpu(a, 8, 1)
|| test_packing_cpu(a, 4, 8)
|| test_packing_cpu(a, 8, 4)
|| test_packing_cpu(a, 1, 16)
|| test_packing_cpu(a, 16, 1)
|| test_packing_cpu(a, 4, 16)
|| test_packing_cpu(a, 16, 4)
|| test_packing_cpu(a, 8, 16)
|| test_packing_cpu(a, 16, 8)
|| test_packing_cpu(b, 1, 1)
|| test_packing_cpu(b, 4, 4)
|| test_packing_cpu(b, 4, 8)
|| test_packing_cpu(b, 1, 4)
|| test_packing_cpu(b, 4, 1)
|| test_packing_cpu(b, 1, 8)
|| test_packing_cpu(b, 8, 1)
|| test_packing_cpu(b, 4, 8)
|| test_packing_cpu(b, 8, 4)
|| test_packing_cpu(b, 1, 16)
|| test_packing_cpu(b, 16, 1)
|| test_packing_cpu(b, 4, 16)
|| test_packing_cpu(b, 16, 4)
|| test_packing_cpu(b, 8, 16)
|| test_packing_cpu(b, 16, 8)
#if NCNN_VULKAN
|| test_packing_gpu_buffer(a, 1, 1)
|| test_packing_gpu_buffer(a, 4, 4)
|| test_packing_gpu_buffer(a, 8, 8)
|| test_packing_gpu_buffer(a, 1, 4)
|| test_packing_gpu_buffer(a, 4, 1)
|| test_packing_gpu_buffer(a, 1, 8)
|| test_packing_gpu_buffer(a, 8, 1)
|| test_packing_gpu_buffer(a, 4, 8)
|| test_packing_gpu_buffer(a, 8, 4)
|| test_packing_gpu_image(a, 1, 1)
|| test_packing_gpu_image(a, 4, 4)
|| test_packing_gpu_image(a, 8, 8)
|| test_packing_gpu_image(a, 1, 4)
|| test_packing_gpu_image(a, 4, 1)
|| test_packing_gpu_image(a, 1, 8)
|| test_packing_gpu_image(a, 8, 1)
|| test_packing_gpu_image(a, 4, 8)
|| test_packing_gpu_image(a, 8, 4)
|| test_packing_gpu_buffer2image(a, 1, 1)
|| test_packing_gpu_buffer2image(a, 4, 4)
|| test_packing_gpu_buffer2image(a, 8, 8)
|| test_packing_gpu_buffer2image(a, 1, 4)
|| test_packing_gpu_buffer2image(a, 4, 1)
|| test_packing_gpu_buffer2image(a, 1, 8)
|| test_packing_gpu_buffer2image(a, 8, 1)
|| test_packing_gpu_buffer2image(a, 4, 8)
|| test_packing_gpu_buffer2image(a, 8, 4)
|| test_packing_gpu_image2buffer(a, 1, 1)
|| test_packing_gpu_image2buffer(a, 4, 4)
|| test_packing_gpu_image2buffer(a, 8, 8)
|| test_packing_gpu_image2buffer(a, 1, 4)
|| test_packing_gpu_image2buffer(a, 4, 1)
|| test_packing_gpu_image2buffer(a, 1, 8)
|| test_packing_gpu_image2buffer(a, 8, 1)
|| test_packing_gpu_image2buffer(a, 4, 8)
|| test_packing_gpu_image2buffer(a, 8, 4)
#endif // NCNN_VULKAN
;
}
static int test_packing_2()
{
ncnn::Mat a = RandomMat(19, 16);
return 0
|| test_packing_cpu(a, 1, 1)
|| test_packing_cpu(a, 4, 4)
|| test_packing_cpu(a, 4, 8)
|| test_packing_cpu(a, 1, 4)
|| test_packing_cpu(a, 4, 1)
|| test_packing_cpu(a, 1, 8)
|| test_packing_cpu(a, 8, 1)
|| test_packing_cpu(a, 4, 8)
|| test_packing_cpu(a, 8, 4)
|| test_packing_cpu(a, 1, 16)
|| test_packing_cpu(a, 16, 1)
|| test_packing_cpu(a, 4, 16)
|| test_packing_cpu(a, 16, 4)
|| test_packing_cpu(a, 8, 16)
|| test_packing_cpu(a, 16, 8)
#if NCNN_VULKAN
|| test_packing_gpu_buffer(a, 1, 1)
|| test_packing_gpu_buffer(a, 4, 4)
|| test_packing_gpu_buffer(a, 8, 8)
|| test_packing_gpu_buffer(a, 1, 4)
|| test_packing_gpu_buffer(a, 4, 1)
|| test_packing_gpu_buffer(a, 1, 8)
|| test_packing_gpu_buffer(a, 8, 1)
|| test_packing_gpu_buffer(a, 4, 8)
|| test_packing_gpu_buffer(a, 8, 4)
|| test_packing_gpu_image(a, 1, 1)
|| test_packing_gpu_image(a, 4, 4)
|| test_packing_gpu_image(a, 8, 8)
|| test_packing_gpu_image(a, 1, 4)
|| test_packing_gpu_image(a, 4, 1)
|| test_packing_gpu_image(a, 1, 8)
|| test_packing_gpu_image(a, 8, 1)
|| test_packing_gpu_image(a, 4, 8)
|| test_packing_gpu_image(a, 8, 4)
|| test_packing_gpu_buffer2image(a, 1, 1)
|| test_packing_gpu_buffer2image(a, 4, 4)
|| test_packing_gpu_buffer2image(a, 8, 8)
|| test_packing_gpu_buffer2image(a, 1, 4)
|| test_packing_gpu_buffer2image(a, 4, 1)
|| test_packing_gpu_buffer2image(a, 1, 8)
|| test_packing_gpu_buffer2image(a, 8, 1)
|| test_packing_gpu_buffer2image(a, 4, 8)
|| test_packing_gpu_buffer2image(a, 8, 4)
|| test_packing_gpu_image2buffer(a, 1, 1)
|| test_packing_gpu_image2buffer(a, 4, 4)
|| test_packing_gpu_image2buffer(a, 8, 8)
|| test_packing_gpu_image2buffer(a, 1, 4)
|| test_packing_gpu_image2buffer(a, 4, 1)
|| test_packing_gpu_image2buffer(a, 1, 8)
|| test_packing_gpu_image2buffer(a, 8, 1)
|| test_packing_gpu_image2buffer(a, 4, 8)
|| test_packing_gpu_image2buffer(a, 8, 4)
#endif // NCNN_VULKAN
;
}
static int test_packing_3()
{
ncnn::Mat a = RandomMat(80);
return 0
|| test_packing_cpu(a, 1, 1)
|| test_packing_cpu(a, 4, 4)
|| test_packing_cpu(a, 4, 8)
|| test_packing_cpu(a, 1, 4)
|| test_packing_cpu(a, 4, 1)
|| test_packing_cpu(a, 1, 8)
|| test_packing_cpu(a, 8, 1)
|| test_packing_cpu(a, 4, 8)
|| test_packing_cpu(a, 8, 4)
|| test_packing_cpu(a, 1, 16)
|| test_packing_cpu(a, 16, 1)
|| test_packing_cpu(a, 4, 16)
|| test_packing_cpu(a, 16, 4)
|| test_packing_cpu(a, 8, 16)
|| test_packing_cpu(a, 16, 8)
#if NCNN_VULKAN
|| test_packing_gpu_buffer(a, 1, 1)
|| test_packing_gpu_buffer(a, 4, 4)
|| test_packing_gpu_buffer(a, 8, 8)
|| test_packing_gpu_buffer(a, 1, 4)
|| test_packing_gpu_buffer(a, 4, 1)
|| test_packing_gpu_buffer(a, 1, 8)
|| test_packing_gpu_buffer(a, 8, 1)
|| test_packing_gpu_buffer(a, 4, 8)
|| test_packing_gpu_buffer(a, 8, 4)
|| test_packing_gpu_image(a, 1, 1)
|| test_packing_gpu_image(a, 4, 4)
|| test_packing_gpu_image(a, 8, 8)
|| test_packing_gpu_image(a, 1, 4)
|| test_packing_gpu_image(a, 4, 1)
|| test_packing_gpu_image(a, 1, 8)
|| test_packing_gpu_image(a, 8, 1)
|| test_packing_gpu_image(a, 4, 8)
|| test_packing_gpu_image(a, 8, 4)
|| test_packing_gpu_buffer2image(a, 1, 1)
|| test_packing_gpu_buffer2image(a, 4, 4)
|| test_packing_gpu_buffer2image(a, 8, 8)
|| test_packing_gpu_buffer2image(a, 1, 4)
|| test_packing_gpu_buffer2image(a, 4, 1)
|| test_packing_gpu_buffer2image(a, 1, 8)
|| test_packing_gpu_buffer2image(a, 8, 1)
|| test_packing_gpu_buffer2image(a, 4, 8)
|| test_packing_gpu_buffer2image(a, 8, 4)
|| test_packing_gpu_image2buffer(a, 1, 1)
|| test_packing_gpu_image2buffer(a, 4, 4)
|| test_packing_gpu_image2buffer(a, 8, 8)
|| test_packing_gpu_image2buffer(a, 1, 4)
|| test_packing_gpu_image2buffer(a, 4, 1)
|| test_packing_gpu_image2buffer(a, 1, 8)
|| test_packing_gpu_image2buffer(a, 8, 1)
|| test_packing_gpu_image2buffer(a, 4, 8)
|| test_packing_gpu_image2buffer(a, 8, 4)
#endif // NCNN_VULKAN
;
}
int main()
{
SRAND(7767517);
return 0
|| test_packing_0()
|| test_packing_1()
|| test_packing_2()
|| test_packing_3();
}

446
3rdparty/ncnn/tests/test_padding.cpp vendored Normal file
View File

@ -0,0 +1,446 @@
// 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/padding.h"
#include "testutil.h"
static int test_padding(const ncnn::Mat& a, int top, int bottom, int left, int right, int front, int behind, int type, float value, int per_channel_pad_data_size)
{
ncnn::ParamDict pd;
pd.set(0, top);
pd.set(1, bottom);
pd.set(2, left);
pd.set(3, right);
pd.set(4, type);
pd.set(5, value);
pd.set(6, per_channel_pad_data_size);
pd.set(7, front);
pd.set(8, behind);
std::vector<ncnn::Mat> weights(per_channel_pad_data_size ? 1 : 0);
if (per_channel_pad_data_size)
weights[0] = RandomMat(per_channel_pad_data_size);
int ret = test_layer<ncnn::Padding>("Padding", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_padding failed a.dims=%d a=(%d %d %d %d) top=%d bottom=%d left=%d right=%d front=%d behind=%d type=%d value=%f per_channel_pad_data_size=%d\n", a.dims, a.w, a.h, a.d, a.c, top, bottom, left, right, front, behind, type, value, per_channel_pad_data_size);
}
return ret;
}
static int test_padding_0()
{
ncnn::Mat a = RandomMat(9, 10, 11, 96);
ncnn::Mat b = RandomMat(10, 12, 13, 44);
ncnn::Mat c = RandomMat(8, 7, 9, 13);
return 0
|| test_padding(a, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding(b, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding(c, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding(a, 2, 2, 2, 2, 2, 2, 0, 1.f, 0)
|| test_padding(b, 2, 2, 2, 2, 2, 2, 0, 2.f, 0)
|| test_padding(c, 2, 2, 2, 2, 2, 2, 0, -3.f, 0)
|| test_padding(a, 2, 1, 2, 1, 2, 1, 0, 0.f, a.c)
|| test_padding(b, 2, 1, 2, 1, 2, 1, 0, 0.f, b.c)
|| test_padding(c, 2, 1, 2, 1, 2, 1, 0, 0.f, c.c)
|| test_padding(a, 0, 1, 0, 1, 0, 1, 0, 0.f, 0)
|| test_padding(b, 0, 1, 0, 1, 0, 1, 0, 0.f, 0)
|| test_padding(c, 0, 1, 0, 1, 0, 1, 0, 0.f, 0)
|| test_padding(a, 1, 2, 3, 4, 5, 6, 0, 0.f, 0)
|| test_padding(b, 1, 2, 3, 4, 5, 6, 0, 0.f, 0)
|| test_padding(c, 1, 2, 3, 4, 5, 6, 0, 0.f, 0)
|| test_padding(a, 2, 3, 2, 3, 2, 3, 0, 0.f, 0)
|| test_padding(b, 2, 3, 2, 3, 2, 3, 0, 0.f, 0)
|| test_padding(c, 2, 3, 2, 3, 2, 3, 0, 0.f, 0)
|| test_padding(a, 1, 1, 1, 1, 1, 1, 0, -1.f, 0)
|| test_padding(b, 1, 1, 1, 1, 1, 1, 0, -2.f, 0)
|| test_padding(c, 1, 1, 1, 1, 1, 1, 0, 3.f, 0);
}
static int test_padding_1()
{
ncnn::Mat a = RandomMat(9, 11, 96);
ncnn::Mat b = RandomMat(10, 13, 44);
ncnn::Mat c = RandomMat(8, 9, 13);
return 0
|| test_padding(a, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding(b, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding(c, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding(a, 2, 2, 2, 2, 0, 0, 0, 1.f, 0)
|| test_padding(b, 2, 2, 2, 2, 0, 0, 0, 2.f, 0)
|| test_padding(c, 2, 2, 2, 2, 0, 0, 0, -3.f, 0)
|| test_padding(a, 2, 1, 2, 1, 0, 0, 0, 0.f, a.c)
|| test_padding(b, 2, 1, 2, 1, 0, 0, 0, 0.f, b.c)
|| test_padding(c, 2, 1, 2, 1, 0, 0, 0, 0.f, c.c)
|| test_padding(a, 0, 1, 0, 1, 0, 0, 1, 0.f, 0)
|| test_padding(b, 0, 1, 0, 1, 0, 0, 1, 0.f, 0)
|| test_padding(c, 0, 1, 0, 1, 0, 0, 1, 0.f, 0)
|| test_padding(a, 1, 2, 3, 4, 0, 0, 1, 0.f, 0)
|| test_padding(b, 1, 2, 3, 4, 0, 0, 1, 0.f, 0)
|| test_padding(c, 1, 2, 3, 4, 0, 0, 1, 0.f, 0)
|| test_padding(a, 2, 3, 2, 3, 0, 0, 2, 0.f, 0)
|| test_padding(b, 2, 3, 2, 3, 0, 0, 2, 0.f, 0)
|| test_padding(c, 2, 3, 2, 3, 0, 0, 2, 0.f, 0)
|| test_padding(a, 1, 1, 1, 1, 1, 1, 0, -1.f, 0)
|| test_padding(b, 1, 1, 1, 1, 1, 1, 0, -2.f, 0)
|| test_padding(c, 1, 1, 1, 1, 1, 1, 0, 3.f, 0)
|| test_padding(a, 2, 1, 0, 0, 2, 3, 0, 0.f, a.c + 5)
|| test_padding(b, 2, 1, 0, 0, 2, 3, 0, 0.f, b.c + 5)
|| test_padding(c, 2, 1, 0, 0, 2, 3, 0, 0.f, c.c + 5)
|| test_padding(a, 1, 2, 3, 4, 32, 16, 0, 0.f, a.c + 48)
|| test_padding(b, 1, 2, 3, 4, 32, 16, 0, 0.f, b.c + 48)
|| test_padding(c, 1, 2, 3, 4, 32, 16, 0, 0.f, c.c + 48)
|| test_padding(a, 0, 0, 0, 0, 3, 1, 1, 0.f, 0)
|| test_padding(b, 0, 0, 0, 0, 3, 1, 1, 0.f, 0)
|| test_padding(c, 0, 0, 0, 0, 3, 1, 1, 0.f, 0)
|| test_padding(a, 2, 0, 1, 0, 4, 4, 1, 0.f, 0)
|| test_padding(b, 2, 0, 1, 0, 4, 4, 1, 0.f, 0)
|| test_padding(c, 2, 0, 1, 0, 4, 4, 1, 0.f, 0)
|| test_padding(a, 2, 0, 2, 0, 0, 2, 2, 0.f, 0)
|| test_padding(b, 2, 0, 2, 0, 0, 2, 2, 0.f, 0)
|| test_padding(c, 2, 0, 2, 0, 0, 2, 2, 0.f, 0)
|| test_padding(a, 4, 2, 1, 3, 3, 5, 2, 0.f, 0)
|| test_padding(b, 4, 2, 1, 3, 3, 5, 2, 0.f, 0)
|| test_padding(c, 4, 2, 1, 3, 3, 5, 2, 0.f, 0);
}
static int test_padding_2()
{
ncnn::Mat a = RandomMat(15, 96);
ncnn::Mat b = RandomMat(19, 44);
ncnn::Mat c = RandomMat(17, 15);
return 0
|| test_padding(a, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding(b, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding(c, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding(a, 0, 0, 1, 1, 0, 0, 0, 1.f, 0)
|| test_padding(b, 0, 0, 1, 1, 0, 0, 0, 2.f, 0)
|| test_padding(c, 0, 0, 1, 1, 0, 0, 0, -3.f, 0)
|| test_padding(a, 0, 0, 3, 4, 0, 0, 1, 0.f, 0)
|| test_padding(b, 0, 0, 3, 4, 0, 0, 1, 0.f, 0)
|| test_padding(c, 0, 0, 3, 4, 0, 0, 1, 0.f, 0)
|| test_padding(a, 0, 0, 3, 2, 0, 0, 2, 0.f, 0)
|| test_padding(b, 0, 0, 3, 2, 0, 0, 2, 0.f, 0)
|| test_padding(c, 0, 0, 3, 2, 0, 0, 2, 0.f, 0)
|| test_padding(a, 2, 2, 2, 2, 0, 0, 0, 1.f, 0)
|| test_padding(b, 2, 2, 2, 2, 0, 0, 0, 2.f, 0)
|| test_padding(c, 2, 2, 2, 2, 0, 0, 0, -3.f, 0)
|| test_padding(a, 16, 16, 2, 5, 0, 0, 0, -1.f, 0)
|| test_padding(b, 16, 16, 2, 5, 0, 0, 0, -2.f, 0)
|| test_padding(c, 16, 16, 2, 5, 0, 0, 0, 3.f, 0)
|| test_padding(a, 3, 1, 3, 1, 0, 0, 1, 0.f, 0)
|| test_padding(b, 3, 1, 3, 1, 0, 0, 1, 0.f, 0)
|| test_padding(c, 3, 1, 3, 1, 0, 0, 1, 0.f, 0)
|| test_padding(a, 4, 4, 0, 1, 0, 0, 1, 0.f, 0)
|| test_padding(b, 4, 4, 0, 1, 0, 0, 1, 0.f, 0)
|| test_padding(c, 4, 4, 0, 1, 0, 0, 1, 0.f, 0)
|| test_padding(a, 2, 3, 2, 3, 0, 0, 2, 0.f, 0)
|| test_padding(b, 2, 3, 2, 3, 0, 0, 2, 0.f, 0)
|| test_padding(c, 2, 3, 2, 3, 0, 0, 2, 0.f, 0)
|| test_padding(a, 2, 6, 1, 0, 0, 0, 2, 0.f, 0)
|| test_padding(b, 2, 6, 1, 0, 0, 0, 2, 0.f, 0)
|| test_padding(c, 2, 6, 1, 0, 0, 0, 2, 0.f, 0);
}
static int test_padding_3()
{
ncnn::Mat a = RandomMat(128);
ncnn::Mat b = RandomMat(124);
ncnn::Mat c = RandomMat(127);
return 0
|| test_padding(a, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding(b, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding(c, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding(a, 0, 0, 2, 2, 0, 0, 0, 1.f, 0)
|| test_padding(b, 0, 0, 2, 2, 0, 0, 0, 2.f, 0)
|| test_padding(c, 0, 0, 2, 2, 0, 0, 0, -3.f, 0)
|| test_padding(a, 0, 0, 32, 16, 0, 0, 0, -1.f, 0)
|| test_padding(b, 0, 0, 32, 16, 0, 0, 0, -2.f, 0)
|| test_padding(c, 0, 0, 32, 16, 0, 0, 0, 3.f, 0)
|| test_padding(a, 0, 0, 0, 1, 0, 0, 1, 0.f, 0)
|| test_padding(b, 0, 0, 0, 1, 0, 0, 1, 0.f, 0)
|| test_padding(c, 0, 0, 0, 1, 0, 0, 1, 0.f, 0)
|| test_padding(a, 0, 0, 4, 12, 0, 0, 1, 0.f, 0)
|| test_padding(b, 0, 0, 4, 12, 0, 0, 1, 0.f, 0)
|| test_padding(c, 0, 0, 4, 12, 0, 0, 1, 0.f, 0)
|| test_padding(a, 0, 0, 2, 3, 0, 0, 2, 0.f, 0)
|| test_padding(b, 0, 0, 2, 3, 0, 0, 2, 0.f, 0)
|| test_padding(c, 0, 0, 2, 3, 0, 0, 2, 0.f, 0)
|| test_padding(a, 0, 0, 10, 6, 0, 0, 2, 0.f, 0)
|| test_padding(b, 0, 0, 10, 6, 0, 0, 2, 0.f, 0)
|| test_padding(c, 0, 0, 10, 6, 0, 0, 2, 0.f, 0);
}
static int test_padding_int8(const ncnn::Mat& a, int top, int bottom, int left, int right, int front, int behind, int type, float value, int per_channel_pad_data_size)
{
// TODO enable padding int8 with per_channel_pad_data_size
per_channel_pad_data_size = 0;
ncnn::ParamDict pd;
pd.set(0, top);
pd.set(1, bottom);
pd.set(2, left);
pd.set(3, right);
pd.set(4, type);
pd.set(5, value);
pd.set(6, per_channel_pad_data_size);
pd.set(7, front);
pd.set(8, behind);
std::vector<ncnn::Mat> weights(per_channel_pad_data_size ? 1 : 0);
if (per_channel_pad_data_size)
weights[0] = RandomMat(per_channel_pad_data_size);
int flag = TEST_LAYER_DISABLE_AUTO_INPUT_CASTING | TEST_LAYER_DISABLE_GPU_TESTING;
int ret = test_layer<ncnn::Padding>("Padding", pd, weights, a, 0.001, 0, flag);
if (ret != 0)
{
fprintf(stderr, "test_padding_int8 failed a.dims=%d a=(%d %d %d %d) top=%d bottom=%d left=%d right=%d front=%d behind=%d type=%d value=%f per_channel_pad_data_size=%d\n", a.dims, a.w, a.h, a.d, a.c, top, bottom, left, right, front, behind, type, value, per_channel_pad_data_size);
}
return ret;
}
static int test_padding_4()
{
ncnn::Mat a = RandomS8Mat(9, 10, 11, 96);
ncnn::Mat b = RandomS8Mat(10, 12, 13, 44);
ncnn::Mat c = RandomS8Mat(8, 7, 9, 13);
return 0
|| test_padding_int8(a, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding_int8(b, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding_int8(c, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding_int8(a, 2, 2, 2, 2, 1, 1, 0, 1.f, 0)
|| test_padding_int8(b, 2, 2, 2, 2, 1, 1, 0, 2.f, 0)
|| test_padding_int8(c, 2, 2, 2, 2, 1, 1, 0, -3.f, 0)
|| test_padding_int8(a, 2, 1, 2, 1, 2, 1, 0, 0.f, a.c)
|| test_padding_int8(b, 2, 1, 2, 1, 2, 1, 0, 0.f, b.c)
|| test_padding_int8(c, 2, 1, 2, 1, 2, 1, 0, 0.f, c.c)
|| test_padding_int8(a, 0, 1, 0, 1, 0, 1, 0, 0.f, 0)
|| test_padding_int8(b, 0, 1, 0, 1, 0, 1, 0, 0.f, 0)
|| test_padding_int8(c, 0, 1, 0, 1, 0, 1, 0, 0.f, 0)
|| test_padding_int8(a, 1, 2, 3, 4, 5, 6, 0, 0.f, 0)
|| test_padding_int8(b, 1, 2, 3, 4, 5, 6, 0, 0.f, 0)
|| test_padding_int8(c, 1, 2, 3, 4, 5, 6, 0, 0.f, 0)
|| test_padding_int8(a, 2, 3, 2, 3, 2, 3, 0, 0.f, 0)
|| test_padding_int8(b, 2, 3, 2, 3, 2, 3, 0, 0.f, 0)
|| test_padding_int8(c, 2, 3, 2, 3, 2, 3, 0, 0.f, 0)
|| test_padding_int8(a, 1, 1, 1, 1, 1, 1, 0, -1.f, 0)
|| test_padding_int8(b, 1, 1, 1, 1, 1, 1, 0, -2.f, 0)
|| test_padding_int8(c, 1, 1, 1, 1, 1, 1, 0, 3.f, 0);
}
static int test_padding_5()
{
ncnn::Mat a = RandomS8Mat(9, 11, 96);
ncnn::Mat b = RandomS8Mat(10, 13, 44);
ncnn::Mat c = RandomS8Mat(8, 9, 13);
return 0
|| test_padding_int8(a, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding_int8(b, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding_int8(c, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding_int8(a, 2, 2, 2, 2, 0, 0, 0, 1.f, 0)
|| test_padding_int8(b, 2, 2, 2, 2, 0, 0, 0, 2.f, 0)
|| test_padding_int8(c, 2, 2, 2, 2, 0, 0, 0, -3.f, 0)
|| test_padding_int8(a, 2, 1, 2, 1, 0, 0, 0, 0.f, a.c)
|| test_padding_int8(b, 2, 1, 2, 1, 0, 0, 0, 0.f, b.c)
|| test_padding_int8(c, 2, 1, 2, 1, 0, 0, 0, 0.f, c.c)
|| test_padding_int8(a, 0, 1, 0, 1, 0, 0, 1, 0.f, 0)
|| test_padding_int8(b, 0, 1, 0, 1, 0, 0, 1, 0.f, 0)
|| test_padding_int8(c, 0, 1, 0, 1, 0, 0, 1, 0.f, 0)
|| test_padding_int8(a, 1, 2, 3, 4, 0, 0, 1, 0.f, 0)
|| test_padding_int8(b, 1, 2, 3, 4, 0, 0, 1, 0.f, 0)
|| test_padding_int8(c, 1, 2, 3, 4, 0, 0, 1, 0.f, 0)
|| test_padding_int8(a, 2, 3, 2, 3, 0, 0, 2, 0.f, 0)
|| test_padding_int8(b, 2, 3, 2, 3, 0, 0, 2, 0.f, 0)
|| test_padding_int8(c, 2, 3, 2, 3, 0, 0, 2, 0.f, 0)
|| test_padding_int8(a, 1, 1, 1, 1, 1, 1, 0, -1.f, 0)
|| test_padding_int8(b, 1, 1, 1, 1, 1, 1, 0, -2.f, 0)
|| test_padding_int8(c, 1, 1, 1, 1, 1, 1, 0, 3.f, 0)
|| test_padding_int8(a, 2, 1, 0, 0, 2, 3, 0, 0.f, a.c + 5)
|| test_padding_int8(b, 2, 1, 0, 0, 2, 3, 0, 0.f, b.c + 5)
|| test_padding_int8(c, 2, 1, 0, 0, 2, 3, 0, 0.f, c.c + 5)
|| test_padding_int8(a, 1, 2, 3, 4, 8, 4, 0, 0.f, a.c + 12)
|| test_padding_int8(b, 1, 2, 3, 4, 8, 4, 0, 0.f, b.c + 12)
|| test_padding_int8(c, 1, 2, 3, 4, 8, 4, 0, 0.f, c.c + 12)
|| test_padding_int8(a, 0, 0, 0, 0, 3, 1, 1, 0.f, 0)
|| test_padding_int8(b, 0, 0, 0, 0, 3, 1, 1, 0.f, 0)
|| test_padding_int8(c, 0, 0, 0, 0, 3, 1, 1, 0.f, 0)
|| test_padding_int8(a, 2, 0, 1, 0, 4, 4, 1, 0.f, 0)
|| test_padding_int8(b, 2, 0, 1, 0, 4, 4, 1, 0.f, 0)
|| test_padding_int8(c, 2, 0, 1, 0, 4, 4, 1, 0.f, 0)
|| test_padding_int8(a, 2, 0, 2, 0, 0, 2, 2, 0.f, 0)
|| test_padding_int8(b, 2, 0, 2, 0, 0, 2, 2, 0.f, 0)
|| test_padding_int8(c, 2, 0, 2, 0, 0, 2, 2, 0.f, 0)
|| test_padding_int8(a, 4, 2, 1, 3, 3, 5, 2, 0.f, 0)
|| test_padding_int8(b, 4, 2, 1, 3, 3, 5, 2, 0.f, 0)
|| test_padding_int8(c, 4, 2, 1, 3, 3, 5, 2, 0.f, 0);
}
static int test_padding_6()
{
ncnn::Mat a = RandomS8Mat(15, 96);
ncnn::Mat b = RandomS8Mat(19, 44);
ncnn::Mat c = RandomS8Mat(17, 15);
return 0
|| test_padding_int8(a, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding_int8(b, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding_int8(c, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding_int8(a, 0, 0, 1, 1, 0, 0, 0, 1.f, 0)
|| test_padding_int8(b, 0, 0, 1, 1, 0, 0, 0, 2.f, 0)
|| test_padding_int8(c, 0, 0, 1, 1, 0, 0, 0, -3.f, 0)
|| test_padding_int8(a, 0, 0, 3, 4, 0, 0, 1, 0.f, 0)
|| test_padding_int8(b, 0, 0, 3, 4, 0, 0, 1, 0.f, 0)
|| test_padding_int8(c, 0, 0, 3, 4, 0, 0, 1, 0.f, 0)
|| test_padding_int8(a, 0, 0, 3, 2, 0, 0, 2, 0.f, 0)
|| test_padding_int8(b, 0, 0, 3, 2, 0, 0, 2, 0.f, 0)
|| test_padding_int8(c, 0, 0, 3, 2, 0, 0, 2, 0.f, 0)
|| test_padding_int8(a, 2, 2, 2, 2, 0, 0, 0, 1.f, 0)
|| test_padding_int8(b, 2, 2, 2, 2, 0, 0, 0, 2.f, 0)
|| test_padding_int8(c, 2, 2, 2, 2, 0, 0, 0, -3.f, 0)
|| test_padding_int8(a, 8, 8, 2, 5, 0, 0, 0, -1.f, 0)
|| test_padding_int8(b, 8, 8, 2, 5, 0, 0, 0, -2.f, 0)
|| test_padding_int8(c, 8, 8, 2, 5, 0, 0, 0, 3.f, 0)
|| test_padding_int8(a, 3, 1, 3, 1, 0, 0, 1, 0.f, 0)
|| test_padding_int8(b, 3, 1, 3, 1, 0, 0, 1, 0.f, 0)
|| test_padding_int8(c, 3, 1, 3, 1, 0, 0, 1, 0.f, 0)
|| test_padding_int8(a, 4, 4, 0, 1, 0, 0, 1, 0.f, 0)
|| test_padding_int8(b, 4, 4, 0, 1, 0, 0, 1, 0.f, 0)
|| test_padding_int8(c, 4, 4, 0, 1, 0, 0, 1, 0.f, 0)
|| test_padding_int8(a, 2, 3, 2, 3, 0, 0, 2, 0.f, 0)
|| test_padding_int8(b, 2, 3, 2, 3, 0, 0, 2, 0.f, 0)
|| test_padding_int8(c, 2, 3, 2, 3, 0, 0, 2, 0.f, 0)
|| test_padding_int8(a, 2, 6, 1, 0, 0, 0, 2, 0.f, 0)
|| test_padding_int8(b, 2, 6, 1, 0, 0, 0, 2, 0.f, 0)
|| test_padding_int8(c, 2, 6, 1, 0, 0, 0, 2, 0.f, 0);
}
static int test_padding_7()
{
ncnn::Mat a = RandomS8Mat(128);
ncnn::Mat b = RandomS8Mat(124);
ncnn::Mat c = RandomS8Mat(127);
return 0
|| test_padding_int8(a, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding_int8(b, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding_int8(c, 0, 0, 0, 0, 0, 0, 0, 0.f, 0)
|| test_padding_int8(a, 0, 0, 2, 2, 0, 0, 0, 1.f, 0)
|| test_padding_int8(b, 0, 0, 2, 2, 0, 0, 0, 2.f, 0)
|| test_padding_int8(c, 0, 0, 2, 2, 0, 0, 0, -3.f, 0)
|| test_padding_int8(a, 0, 0, 16, 8, 0, 0, 0, -1.f, 0)
|| test_padding_int8(b, 0, 0, 16, 8, 0, 0, 0, -2.f, 0)
|| test_padding_int8(c, 0, 0, 16, 8, 0, 0, 0, 3.f, 0)
|| test_padding_int8(a, 0, 0, 0, 1, 0, 0, 1, 0.f, 0)
|| test_padding_int8(b, 0, 0, 0, 1, 0, 0, 1, 0.f, 0)
|| test_padding_int8(c, 0, 0, 0, 1, 0, 0, 1, 0.f, 0)
|| test_padding_int8(a, 0, 0, 4, 12, 0, 0, 1, 0.f, 0)
|| test_padding_int8(b, 0, 0, 4, 12, 0, 0, 1, 0.f, 0)
|| test_padding_int8(c, 0, 0, 4, 12, 0, 0, 1, 0.f, 0)
|| test_padding_int8(a, 0, 0, 2, 3, 0, 0, 2, 0.f, 0)
|| test_padding_int8(b, 0, 0, 2, 3, 0, 0, 2, 0.f, 0)
|| test_padding_int8(c, 0, 0, 2, 3, 0, 0, 2, 0.f, 0)
|| test_padding_int8(a, 0, 0, 10, 6, 0, 0, 2, 0.f, 0)
|| test_padding_int8(b, 0, 0, 10, 6, 0, 0, 2, 0.f, 0)
|| test_padding_int8(c, 0, 0, 10, 6, 0, 0, 2, 0.f, 0);
}
int main()
{
SRAND(7767517);
return test_padding_0()
|| test_padding_1()
|| test_padding_2()
|| test_padding_3()
|| test_padding_4()
|| test_padding_5()
|| test_padding_6()
|| test_padding_7();
}

127
3rdparty/ncnn/tests/test_permute.cpp vendored Normal file
View File

@ -0,0 +1,127 @@
// 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/permute.h"
#include "testutil.h"
static int test_permute(const ncnn::Mat& a, int order_type)
{
ncnn::ParamDict pd;
pd.set(0, order_type);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Permute>("Permute", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_permute failed a.dims=%d a=(%d %d %d %d) order_type=%d\n", a.dims, a.w, a.h, a.d, a.c, order_type);
}
return ret;
}
static int test_permute_0()
{
ncnn::Mat a = RandomMat(128);
ncnn::Mat b = RandomMat(127);
return 0
|| test_permute(a, 0)
|| test_permute(b, 0);
}
static int test_permute_1()
{
ncnn::Mat a = RandomMat(12, 32);
ncnn::Mat b = RandomMat(8, 15);
ncnn::Mat c = RandomMat(11, 16);
ncnn::Mat d = RandomMat(7, 9);
for (int order_type = 0; order_type < 2; order_type++)
{
int ret = 0
|| test_permute(a, order_type)
|| test_permute(b, order_type)
|| test_permute(c, order_type)
|| test_permute(d, order_type);
if (ret != 0)
return -1;
}
return 0;
}
static int test_permute_2()
{
ncnn::Mat a = RandomMat(8, 16, 32);
ncnn::Mat b = RandomMat(12, 8, 16);
ncnn::Mat c = RandomMat(7, 14, 12);
ncnn::Mat d = RandomMat(4, 4, 13);
ncnn::Mat e = RandomMat(1, 2, 7);
ncnn::Mat f = RandomMat(8, 5, 6);
for (int order_type = 0; order_type < 6; order_type++)
{
int ret = 0
|| test_permute(a, order_type)
|| test_permute(b, order_type)
|| test_permute(c, order_type)
|| test_permute(d, order_type)
|| test_permute(e, order_type)
|| test_permute(f, order_type);
if (ret != 0)
return -1;
}
return 0;
}
static int test_permute_3()
{
ncnn::Mat a = RandomMat(8, 12, 16, 32);
ncnn::Mat b = RandomMat(12, 4, 8, 16);
ncnn::Mat c = RandomMat(7, 8, 14, 12);
ncnn::Mat d = RandomMat(4, 4, 4, 13);
ncnn::Mat e = RandomMat(1, 2, 3, 7);
ncnn::Mat f = RandomMat(8, 6, 5, 6);
for (int order_type = 0; order_type < 24; order_type++)
{
int ret = 0
|| test_permute(a, order_type)
|| test_permute(b, order_type)
|| test_permute(c, order_type)
|| test_permute(d, order_type)
|| test_permute(e, order_type)
|| test_permute(f, order_type);
if (ret != 0)
return -1;
}
return 0;
}
int main()
{
SRAND(7767517);
return 0
|| test_permute_0()
|| test_permute_1()
|| test_permute_2()
|| test_permute_3();
}

View File

@ -0,0 +1,67 @@
// 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/pixelshuffle.h"
#include "testutil.h"
static int test_pixelshuffle(const ncnn::Mat& a, int upscale_factor, int mode)
{
ncnn::ParamDict pd;
pd.set(0, upscale_factor);
pd.set(1, mode);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::PixelShuffle>("PixelShuffle", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_pixelshuffle failed a.dims=%d a=(%d %d %d) upscale_factor=%d mode=%d\n", a.dims, a.w, a.h, a.c, upscale_factor, mode);
}
return ret;
}
static int test_pixelshuffle_0()
{
return 0
|| test_pixelshuffle(RandomMat(7, 7, 1), 1, 0)
|| test_pixelshuffle(RandomMat(7, 7, 8), 2, 0)
|| test_pixelshuffle(RandomMat(7, 7, 12), 2, 0)
|| test_pixelshuffle(RandomMat(7, 7, 64), 4, 0)
|| test_pixelshuffle(RandomMat(7, 7, 32), 2, 0)
|| test_pixelshuffle(RandomMat(7, 7, 48), 2, 0)
|| test_pixelshuffle(RandomMat(7, 7, 36), 3, 0)
|| test_pixelshuffle(RandomMat(7, 7, 72), 3, 0)
|| test_pixelshuffle(RandomMat(7, 7, 90), 3, 0);
}
static int test_pixelshuffle_1()
{
return 0
|| test_pixelshuffle(RandomMat(7, 7, 1), 1, 1)
|| test_pixelshuffle(RandomMat(7, 7, 8), 2, 1)
|| test_pixelshuffle(RandomMat(7, 7, 12), 2, 1)
|| test_pixelshuffle(RandomMat(7, 7, 64), 4, 1)
|| test_pixelshuffle(RandomMat(7, 7, 32), 2, 1)
|| test_pixelshuffle(RandomMat(7, 7, 48), 2, 1)
|| test_pixelshuffle(RandomMat(7, 7, 36), 3, 1)
|| test_pixelshuffle(RandomMat(7, 7, 90), 3, 1);
}
int main()
{
SRAND(7767517);
return test_pixelshuffle_0() || test_pixelshuffle_1();
}

263
3rdparty/ncnn/tests/test_pooling.cpp vendored Normal file
View File

@ -0,0 +1,263 @@
// 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/pooling.h"
#include "testutil.h"
static int test_pooling(int w, int h, int c, int pooling_type, int kernel, int stride, int pad, int global_pooling, int pad_mode, int avgpool_count_include_pad, int adaptive_pooling, int out_w)
{
ncnn::Mat a = RandomMat(w, h, c);
ncnn::ParamDict pd;
pd.set(0, pooling_type); // pooling_type
pd.set(1, kernel); // kernel_w
pd.set(2, stride); // stride_w
pd.set(3, pad); // pad_w
pd.set(4, global_pooling); // global_pooling
pd.set(5, pad_mode); // pad_mode
pd.set(6, avgpool_count_include_pad); // avgpool_count_include_pad
pd.set(7, adaptive_pooling); // adaptive_pooling
pd.set(8, out_w); // out_w
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Pooling>("Pooling", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_pooling failed w=%d h=%d c=%d pooling_type=%d kernel=%d stride=%d pad=%d global_pooling=%d pad_mode=%d avgpool_count_include_pad=%d adaptive_pooling=%d out_w=%d\n", w, h, c, pooling_type, kernel, stride, pad, global_pooling, pad_mode, avgpool_count_include_pad, adaptive_pooling, out_w);
}
return ret;
}
static int test_pooling_0()
{
static const int ksp[11][3] = {
{2, 1, 0},
{2, 2, 0},
{3, 1, 0},
{3, 2, 1},
{4, 1, 0},
{4, 2, 1},
{5, 1, 0},
{5, 2, 2},
{7, 1, 0},
{7, 2, 1},
{7, 3, 2},
};
for (int i = 0; i < 11; i++)
{
int ret = 0
|| test_pooling(9, 7, 1, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0, 0, 0)
|| test_pooling(9, 7, 2, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0, 0, 0)
|| test_pooling(9, 7, 3, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 2, 0, 0, 0)
|| test_pooling(9, 7, 4, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 3, 0, 0, 0)
|| test_pooling(9, 7, 7, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0, 0, 0)
|| test_pooling(9, 7, 8, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0, 0, 0)
|| test_pooling(9, 7, 15, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 2, 0, 0, 0)
|| test_pooling(9, 7, 16, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 3, 0, 0, 0);
if (ret != 0)
return -1;
}
return 0;
}
static int test_pooling_1()
{
static const int ksp[11][3] = {
{2, 1, 0},
{2, 2, 0},
{3, 1, 0},
{3, 2, 1},
{4, 1, 0},
{4, 2, 1},
{5, 1, 0},
{5, 2, 2},
{7, 1, 0},
{7, 2, 1},
{7, 3, 2},
};
for (int i = 0; i < 11; i++)
{
int ret = 0
|| test_pooling(9, 7, 1, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0, 0, 0)
|| test_pooling(9, 7, 2, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0, 0, 0)
|| test_pooling(9, 7, 3, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 1, 0, 0)
|| test_pooling(9, 7, 4, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0, 0, 0)
|| test_pooling(9, 7, 7, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0, 0, 0)
|| test_pooling(9, 7, 8, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 1, 0, 0)
|| test_pooling(9, 7, 12, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 2, 1, 0, 0)
|| test_pooling(9, 7, 15, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0, 0, 0)
|| test_pooling(9, 7, 16, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0, 0, 0)
|| test_pooling(9, 7, 64, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 3, 1, 0, 0);
if (ret != 0)
return -1;
}
return 0;
}
static int test_pooling_2()
{
return 0
|| test_pooling(2, 5, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling(5, 2, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling(3, 6, 3, 0, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling(6, 3, 3, 1, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling(4, 4, 4, 0, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling(6, 4, 4, 1, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling(8, 7, 8, 0, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling(7, 8, 8, 1, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling(11, 13, 16, 0, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling(13, 11, 16, 1, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling(48, 48, 4, 0, 2, 2, 0, 0, 0, 0, 0, 0)
|| test_pooling(48, 48, 15, 0, 2, 2, 1, 0, 0, 0, 0, 0);
}
// adaptive avg pool
static int test_pooling_3()
{
return 0
|| test_pooling(2, 5, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling(2, 5, 1, 1, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling(2, 5, 1, 1, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling(2, 5, 1, 1, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling(2, 5, 1, 1, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling(2, 5, 1, 1, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling(5, 2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling(5, 2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling(5, 2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling(5, 2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling(5, 2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling(5, 2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling(3, 6, 3, 1, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling(3, 6, 3, 1, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling(3, 6, 3, 1, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling(3, 6, 3, 1, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling(3, 6, 3, 1, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling(3, 6, 3, 1, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling(4, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling(4, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling(4, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling(4, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling(4, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling(4, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling(4, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 7)
|| test_pooling(4, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 8)
|| test_pooling(6, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling(6, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling(6, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling(6, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling(6, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling(6, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling(8, 7, 8, 1, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling(8, 7, 8, 1, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling(8, 7, 8, 1, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling(8, 7, 8, 1, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling(8, 7, 8, 1, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling(8, 7, 8, 1, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling(8, 7, 8, 1, 1, 1, 0, 0, 0, 0, 1, 7)
|| test_pooling(8, 7, 8, 1, 1, 1, 0, 0, 0, 0, 1, 8)
|| test_pooling(8, 7, 8, 1, 1, 1, 0, 0, 0, 0, 1, 9)
|| test_pooling(11, 13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 1)
|| test_pooling(11, 13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 3)
|| test_pooling(11, 13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 5)
|| test_pooling(11, 13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 7)
|| test_pooling(11, 13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 9)
|| test_pooling(11, 13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 11)
|| test_pooling(11, 13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 13)
|| test_pooling(13, 11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 2)
|| test_pooling(13, 11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 4)
|| test_pooling(13, 11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 6)
|| test_pooling(13, 11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 8)
|| test_pooling(13, 11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 10)
|| test_pooling(13, 11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 12);
}
// adaptive max pool
static int test_pooling_4()
{
return 0
|| test_pooling(2, 5, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling(2, 5, 1, 0, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling(2, 5, 1, 0, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling(2, 5, 1, 0, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling(2, 5, 1, 0, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling(2, 5, 1, 0, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling(5, 2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling(5, 2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling(5, 2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling(5, 2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling(5, 2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling(5, 2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling(3, 6, 3, 0, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling(3, 6, 3, 0, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling(3, 6, 3, 0, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling(3, 6, 3, 0, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling(3, 6, 3, 0, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling(3, 6, 3, 0, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling(4, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling(4, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling(4, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling(4, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling(4, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling(4, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling(4, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 7)
|| test_pooling(4, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 8)
|| test_pooling(6, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling(6, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling(6, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling(6, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling(6, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling(6, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling(8, 7, 8, 0, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling(8, 7, 8, 0, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling(8, 7, 8, 0, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling(8, 7, 8, 0, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling(8, 7, 8, 0, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling(8, 7, 8, 0, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling(8, 7, 8, 0, 1, 1, 0, 0, 0, 0, 1, 7)
|| test_pooling(8, 7, 8, 0, 1, 1, 0, 0, 0, 0, 1, 8)
|| test_pooling(8, 7, 8, 0, 1, 1, 0, 0, 0, 0, 1, 9)
|| test_pooling(11, 13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 1)
|| test_pooling(11, 13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 3)
|| test_pooling(11, 13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 5)
|| test_pooling(11, 13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 7)
|| test_pooling(11, 13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 9)
|| test_pooling(11, 13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 11)
|| test_pooling(11, 13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 13)
|| test_pooling(13, 11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 2)
|| test_pooling(13, 11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 4)
|| test_pooling(13, 11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 6)
|| test_pooling(13, 11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 8)
|| test_pooling(13, 11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 10)
|| test_pooling(13, 11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 12);
}
int main()
{
SRAND(7767517);
return 0
|| test_pooling_0()
|| test_pooling_1()
|| test_pooling_2()
|| test_pooling_3()
|| test_pooling_4();
}

261
3rdparty/ncnn/tests/test_pooling1d.cpp vendored Normal file
View File

@ -0,0 +1,261 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 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/pooling1d.h"
#include "testutil.h"
static int test_pooling1d(int w, int h, int pooling_type, int kernel, int stride, int pad, int global_pooling, int pad_mode, int avgpool_count_include_pad, int adaptive_pooling, int out_w)
{
ncnn::Mat a = RandomMat(w, h);
ncnn::ParamDict pd;
pd.set(0, pooling_type); // pooling_type
pd.set(1, kernel); // kernel_w
pd.set(2, stride); // stride_w
pd.set(3, pad); // pad_w
pd.set(4, global_pooling); // global_pooling
pd.set(5, pad_mode); // pad_mode
pd.set(6, avgpool_count_include_pad); // avgpool_count_include_pad
pd.set(7, adaptive_pooling); // adaptive_pooling
pd.set(8, out_w); // out_w
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Pooling1D>("Pooling1D", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_pooling1d failed w=%d h=%d pooling_type=%d kernel=%d stride=%d pad=%d global_pooling=%d pad_mode=%d avgpool_count_include_pad=%d adaptive_pooling=%d out_w=%d\n", w, h, pooling_type, kernel, stride, pad, global_pooling, pad_mode, avgpool_count_include_pad, adaptive_pooling, out_w);
}
return ret;
}
static int test_pooling1d_0()
{
static const int ksp[11][3] = {
{2, 1, 0},
{2, 2, 0},
{3, 1, 0},
{3, 2, 1},
{4, 1, 0},
{4, 2, 1},
{5, 1, 0},
{5, 2, 2},
{7, 1, 0},
{7, 2, 1},
{7, 3, 2},
};
for (int i = 0; i < 11; i++)
{
int ret = 0
|| test_pooling1d(9, 1, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0, 0, 0)
|| test_pooling1d(9, 2, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0, 0, 0)
|| test_pooling1d(9, 3, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 2, 0, 0, 0)
|| test_pooling1d(9, 4, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 3, 0, 0, 0)
|| test_pooling1d(9, 7, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0, 0, 0)
|| test_pooling1d(9, 8, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0, 0, 0)
|| test_pooling1d(9, 15, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 2, 0, 0, 0)
|| test_pooling1d(9, 16, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 3, 0, 0, 0);
if (ret != 0)
return -1;
}
return 0;
}
static int test_pooling1d_1()
{
static const int ksp[11][3] = {
{2, 1, 0},
{2, 2, 0},
{3, 1, 0},
{3, 2, 1},
{4, 1, 0},
{4, 2, 1},
{5, 1, 0},
{5, 2, 2},
{7, 1, 0},
{7, 2, 1},
{7, 3, 2},
};
for (int i = 0; i < 11; i++)
{
int ret = 0
|| test_pooling1d(9, 1, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0, 0, 0)
|| test_pooling1d(9, 2, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0, 0, 0)
|| test_pooling1d(9, 3, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 1, 0, 0)
|| test_pooling1d(9, 4, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0, 0, 0)
|| test_pooling1d(9, 7, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0, 0, 0)
|| test_pooling1d(9, 8, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 1, 0, 0)
|| test_pooling1d(9, 12, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 2, 1, 0, 0)
|| test_pooling1d(9, 15, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0, 0, 0)
|| test_pooling1d(9, 16, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0, 0, 0)
|| test_pooling1d(9, 64, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 3, 1, 0, 0);
if (ret != 0)
return -1;
}
return 0;
}
static int test_pooling1d_2()
{
return 0
|| test_pooling1d(2, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling1d(5, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling1d(3, 3, 0, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling1d(6, 3, 1, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling1d(4, 4, 0, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling1d(6, 4, 1, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling1d(8, 8, 0, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling1d(7, 8, 1, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling1d(11, 16, 0, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling1d(13, 16, 1, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling1d(48, 4, 0, 2, 2, 0, 0, 0, 0, 0, 0)
|| test_pooling1d(48, 15, 0, 2, 2, 1, 0, 0, 0, 0, 0);
}
// adaptive avg pool
static int test_pooling1d_3()
{
return 0
|| test_pooling1d(2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling1d(2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling1d(2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling1d(2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling1d(2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling1d(2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling1d(5, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling1d(5, 1, 1, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling1d(5, 1, 1, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling1d(5, 1, 1, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling1d(5, 1, 1, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling1d(5, 1, 1, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling1d(3, 3, 1, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling1d(3, 3, 1, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling1d(3, 3, 1, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling1d(3, 3, 1, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling1d(3, 3, 1, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling1d(3, 3, 1, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling1d(4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling1d(4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling1d(4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling1d(4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling1d(4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling1d(4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling1d(4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 7)
|| test_pooling1d(4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 8)
|| test_pooling1d(6, 4, 1, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling1d(6, 4, 1, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling1d(6, 4, 1, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling1d(6, 4, 1, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling1d(6, 4, 1, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling1d(6, 4, 1, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling1d(8, 8, 1, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling1d(8, 8, 1, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling1d(8, 8, 1, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling1d(8, 8, 1, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling1d(8, 8, 1, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling1d(8, 8, 1, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling1d(8, 8, 1, 1, 1, 0, 0, 0, 0, 1, 7)
|| test_pooling1d(8, 8, 1, 1, 1, 0, 0, 0, 0, 1, 8)
|| test_pooling1d(11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 1)
|| test_pooling1d(11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 3)
|| test_pooling1d(11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 5)
|| test_pooling1d(11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 7)
|| test_pooling1d(11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 9)
|| test_pooling1d(11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 11)
|| test_pooling1d(11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 13)
|| test_pooling1d(13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 2)
|| test_pooling1d(13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 4)
|| test_pooling1d(13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 6)
|| test_pooling1d(13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 8)
|| test_pooling1d(13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 10)
|| test_pooling1d(13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 12);
}
// adaptive max pool
static int test_pooling1d_4()
{
return 0
|| test_pooling1d(2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling1d(2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling1d(2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling1d(2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling1d(2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling1d(2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling1d(5, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling1d(5, 1, 0, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling1d(5, 1, 0, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling1d(5, 1, 0, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling1d(5, 1, 0, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling1d(5, 1, 0, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling1d(3, 3, 0, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling1d(3, 3, 0, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling1d(3, 3, 0, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling1d(3, 3, 0, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling1d(3, 3, 0, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling1d(3, 3, 0, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling1d(4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling1d(4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling1d(4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling1d(4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling1d(4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling1d(4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling1d(4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 7)
|| test_pooling1d(4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 8)
|| test_pooling1d(6, 4, 0, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling1d(6, 4, 0, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling1d(6, 4, 0, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling1d(6, 4, 0, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling1d(6, 4, 0, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling1d(6, 4, 0, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling1d(8, 8, 0, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling1d(8, 8, 0, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling1d(8, 8, 0, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling1d(8, 8, 0, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling1d(8, 8, 0, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling1d(8, 8, 0, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling1d(8, 8, 0, 1, 1, 0, 0, 0, 0, 1, 7)
|| test_pooling1d(8, 8, 0, 1, 1, 0, 0, 0, 0, 1, 8)
|| test_pooling1d(11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 1)
|| test_pooling1d(11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 3)
|| test_pooling1d(11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 5)
|| test_pooling1d(11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 7)
|| test_pooling1d(11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 9)
|| test_pooling1d(11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 11)
|| test_pooling1d(11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 13)
|| test_pooling1d(13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 2)
|| test_pooling1d(13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 4)
|| test_pooling1d(13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 6)
|| test_pooling1d(13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 8)
|| test_pooling1d(13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 10)
|| test_pooling1d(13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 12);
}
int main()
{
SRAND(7767517);
return 0
|| test_pooling1d_0()
|| test_pooling1d_1()
|| test_pooling1d_2()
|| test_pooling1d_3()
|| test_pooling1d_4();
}

263
3rdparty/ncnn/tests/test_pooling3d.cpp vendored Normal file
View File

@ -0,0 +1,263 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 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/pooling3d.h"
#include "testutil.h"
static int test_pooling3d(int w, int h, int d, int c, int pooling_type, int kernel, int stride, int pad, int global_pooling, int pad_mode, int avgpool_count_include_pad, int adaptive_pooling, int out_w)
{
ncnn::Mat a = RandomMat(w, h, d, c);
ncnn::ParamDict pd;
pd.set(0, pooling_type); // pooling_type
pd.set(1, kernel); // kernel_w
pd.set(2, stride); // stride_w
pd.set(3, pad); // pad_w
pd.set(4, global_pooling); // global_pooling
pd.set(5, pad_mode); // pad_mode
pd.set(6, avgpool_count_include_pad); // avgpool_count_include_pad
pd.set(7, adaptive_pooling); // adaptive_pooling
pd.set(8, out_w); // out_w
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Pooling3D>("Pooling3D", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_pooling3d failed w=%d h=%d d=%d c=%d pooling_type=%d kernel=%d stride=%d pad=%d global_pooling=%d pad_mode=%d avgpool_count_include_pad=%d adaptive_pooling=%d out_w=%d\n", w, h, d, c, pooling_type, kernel, stride, pad, global_pooling, pad_mode, avgpool_count_include_pad, adaptive_pooling, out_w);
}
return ret;
}
static int test_pooling3d_0()
{
static const int ksp[11][3] = {
{2, 1, 0},
{2, 2, 0},
{3, 1, 0},
{3, 2, 1},
{4, 1, 0},
{4, 2, 1},
{5, 1, 0},
{5, 2, 2},
{7, 1, 0},
{7, 2, 1},
{7, 3, 2},
};
for (int i = 0; i < 11; i++)
{
int ret = 0
|| test_pooling3d(9, 8, 7, 1, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0, 0, 0)
|| test_pooling3d(9, 8, 7, 2, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0, 0, 0)
|| test_pooling3d(9, 8, 7, 3, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 2, 0, 0, 0)
|| test_pooling3d(9, 8, 7, 4, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 3, 0, 0, 0)
|| test_pooling3d(9, 8, 7, 7, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0, 0, 0)
|| test_pooling3d(9, 8, 7, 8, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0, 0, 0)
|| test_pooling3d(9, 8, 7, 15, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 2, 0, 0, 0)
|| test_pooling3d(9, 8, 7, 16, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 3, 0, 0, 0);
if (ret != 0)
return -1;
}
return 0;
}
static int test_pooling3d_1()
{
static const int ksp[11][3] = {
{2, 1, 0},
{2, 2, 0},
{3, 1, 0},
{3, 2, 1},
{4, 1, 0},
{4, 2, 1},
{5, 1, 0},
{5, 2, 2},
{7, 1, 0},
{7, 2, 1},
{7, 3, 2},
};
for (int i = 0; i < 11; i++)
{
int ret = 0
|| test_pooling3d(9, 8, 7, 1, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0, 0, 0)
|| test_pooling3d(9, 8, 7, 2, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0, 0, 0)
|| test_pooling3d(9, 8, 7, 3, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 1, 0, 0)
|| test_pooling3d(9, 8, 7, 4, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0, 0, 0)
|| test_pooling3d(9, 8, 7, 7, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0, 0, 0)
|| test_pooling3d(9, 8, 7, 8, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 1, 0, 0)
|| test_pooling3d(9, 8, 7, 12, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 2, 1, 0, 0)
|| test_pooling3d(9, 8, 7, 15, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0, 0, 0)
|| test_pooling3d(9, 8, 7, 16, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0, 0, 0)
|| test_pooling3d(9, 8, 7, 64, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 3, 1, 0, 0);
if (ret != 0)
return -1;
}
return 0;
}
static int test_pooling3d_2()
{
return 0
|| test_pooling3d(2, 2, 5, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling3d(5, 2, 2, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling3d(3, 3, 6, 3, 0, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling3d(6, 3, 3, 3, 1, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling3d(4, 4, 4, 4, 0, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling3d(6, 5, 4, 4, 1, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling3d(8, 7, 7, 8, 0, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling3d(7, 7, 8, 8, 1, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling3d(11, 12, 13, 16, 0, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling3d(13, 12, 11, 16, 1, 1, 1, 0, 1, 0, 0, 0, 0)
|| test_pooling3d(48, 48, 48, 4, 0, 2, 2, 0, 0, 0, 0, 0, 0)
|| test_pooling3d(48, 48, 48, 15, 0, 2, 2, 1, 0, 0, 0, 0, 0);
}
// adaptive avg pool
static int test_pooling3d_3()
{
return 0
|| test_pooling3d(2, 2, 5, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling3d(2, 2, 5, 1, 1, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling3d(2, 2, 5, 1, 1, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling3d(2, 2, 5, 1, 1, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling3d(2, 2, 5, 1, 1, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling3d(2, 2, 5, 1, 1, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling3d(5, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling3d(5, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling3d(5, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling3d(5, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling3d(5, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling3d(5, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling3d(3, 4, 6, 3, 1, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling3d(3, 4, 6, 3, 1, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling3d(3, 4, 6, 3, 1, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling3d(3, 4, 6, 3, 1, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling3d(3, 4, 6, 3, 1, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling3d(3, 4, 6, 3, 1, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling3d(4, 4, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling3d(4, 4, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling3d(4, 4, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling3d(4, 4, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling3d(4, 4, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling3d(4, 4, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling3d(4, 4, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 7)
|| test_pooling3d(4, 4, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 8)
|| test_pooling3d(6, 5, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling3d(6, 5, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling3d(6, 5, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling3d(6, 5, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling3d(6, 5, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling3d(6, 5, 4, 4, 1, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling3d(8, 7, 7, 8, 1, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling3d(8, 7, 7, 8, 1, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling3d(8, 7, 7, 8, 1, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling3d(8, 7, 7, 8, 1, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling3d(8, 7, 7, 8, 1, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling3d(8, 7, 7, 8, 1, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling3d(8, 7, 7, 8, 1, 1, 1, 0, 0, 0, 0, 1, 7)
|| test_pooling3d(8, 7, 7, 8, 1, 1, 1, 0, 0, 0, 0, 1, 8)
|| test_pooling3d(8, 7, 7, 8, 1, 1, 1, 0, 0, 0, 0, 1, 9)
|| test_pooling3d(11, 12, 13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 1)
|| test_pooling3d(11, 12, 13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 3)
|| test_pooling3d(11, 12, 13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 5)
|| test_pooling3d(11, 12, 13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 7)
|| test_pooling3d(11, 12, 13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 9)
|| test_pooling3d(11, 12, 13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 11)
|| test_pooling3d(11, 12, 13, 16, 1, 1, 1, 0, 0, 0, 1, 0, 13)
|| test_pooling3d(13, 12, 11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 2)
|| test_pooling3d(13, 12, 11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 4)
|| test_pooling3d(13, 12, 11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 6)
|| test_pooling3d(13, 12, 11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 8)
|| test_pooling3d(13, 12, 11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 10)
|| test_pooling3d(13, 12, 11, 16, 1, 1, 1, 0, 0, 0, 1, 0, 12);
}
// adaptive max pool
static int test_pooling3d_4()
{
return 0
|| test_pooling3d(2, 2, 5, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling3d(2, 2, 5, 1, 0, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling3d(2, 2, 5, 1, 0, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling3d(2, 2, 5, 1, 0, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling3d(2, 2, 5, 1, 0, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling3d(2, 2, 5, 1, 0, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling3d(5, 2, 2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling3d(5, 2, 2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling3d(5, 2, 2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling3d(5, 2, 2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling3d(5, 2, 2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling3d(5, 2, 2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling3d(3, 4, 6, 3, 0, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling3d(3, 4, 6, 3, 0, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling3d(3, 4, 6, 3, 0, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling3d(3, 4, 6, 3, 0, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling3d(3, 4, 6, 3, 0, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling3d(3, 4, 6, 3, 0, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling3d(4, 4, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling3d(4, 4, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling3d(4, 4, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling3d(4, 4, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling3d(4, 4, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling3d(4, 4, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling3d(4, 4, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 7)
|| test_pooling3d(4, 4, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 8)
|| test_pooling3d(6, 5, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling3d(6, 5, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling3d(6, 5, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling3d(6, 5, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling3d(6, 5, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling3d(6, 5, 4, 4, 0, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling3d(8, 7, 7, 8, 0, 1, 1, 0, 0, 0, 0, 1, 1)
|| test_pooling3d(8, 7, 7, 8, 0, 1, 1, 0, 0, 0, 0, 1, 2)
|| test_pooling3d(8, 7, 7, 8, 0, 1, 1, 0, 0, 0, 0, 1, 3)
|| test_pooling3d(8, 7, 7, 8, 0, 1, 1, 0, 0, 0, 0, 1, 4)
|| test_pooling3d(8, 7, 7, 8, 0, 1, 1, 0, 0, 0, 0, 1, 5)
|| test_pooling3d(8, 7, 7, 8, 0, 1, 1, 0, 0, 0, 0, 1, 6)
|| test_pooling3d(8, 7, 7, 8, 0, 1, 1, 0, 0, 0, 0, 1, 7)
|| test_pooling3d(8, 7, 7, 8, 0, 1, 1, 0, 0, 0, 0, 1, 8)
|| test_pooling3d(8, 7, 7, 8, 0, 1, 1, 0, 0, 0, 0, 1, 9)
|| test_pooling3d(11, 12, 13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 1)
|| test_pooling3d(11, 12, 13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 3)
|| test_pooling3d(11, 12, 13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 5)
|| test_pooling3d(11, 12, 13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 7)
|| test_pooling3d(11, 12, 13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 9)
|| test_pooling3d(11, 12, 13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 11)
|| test_pooling3d(11, 12, 13, 16, 0, 1, 1, 0, 0, 0, 1, 0, 13)
|| test_pooling3d(13, 12, 11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 2)
|| test_pooling3d(13, 12, 11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 4)
|| test_pooling3d(13, 12, 11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 6)
|| test_pooling3d(13, 12, 11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 8)
|| test_pooling3d(13, 12, 11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 10)
|| test_pooling3d(13, 12, 11, 16, 0, 1, 1, 0, 0, 0, 1, 0, 12);
}
int main()
{
SRAND(7767517);
return 0
|| test_pooling3d_0()
|| test_pooling3d_1()
|| test_pooling3d_2()
|| test_pooling3d_3()
|| test_pooling3d_4();
}

76
3rdparty/ncnn/tests/test_prelu.cpp vendored Normal file
View File

@ -0,0 +1,76 @@
// 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/prelu.h"
#include "testutil.h"
static int test_prelu(const ncnn::Mat& a, int num_slope)
{
ncnn::ParamDict pd;
pd.set(0, num_slope);
std::vector<ncnn::Mat> weights(1);
weights[0] = RandomMat(num_slope);
int ret = test_layer<ncnn::PReLU>("PReLU", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_prelu failed a.dims=%d a=(%d %d %d) num_slope=%d\n", a.dims, a.w, a.h, a.c, num_slope);
}
return ret;
}
static int test_prelu_0()
{
return 0
|| test_prelu(RandomMat(5, 7, 24), 24)
|| test_prelu(RandomMat(5, 7, 24), 1)
|| test_prelu(RandomMat(7, 9, 12), 12)
|| test_prelu(RandomMat(7, 9, 12), 1)
|| test_prelu(RandomMat(3, 5, 13), 13)
|| test_prelu(RandomMat(3, 5, 13), 1);
}
static int test_prelu_1()
{
return 0
|| test_prelu(RandomMat(15, 24), 24)
|| test_prelu(RandomMat(15, 24), 1)
|| test_prelu(RandomMat(17, 12), 12)
|| test_prelu(RandomMat(17, 12), 1)
|| test_prelu(RandomMat(19, 15), 15)
|| test_prelu(RandomMat(19, 15), 1);
}
static int test_prelu_2()
{
return 0
|| test_prelu(RandomMat(128), 128)
|| test_prelu(RandomMat(128), 1)
|| test_prelu(RandomMat(124), 124)
|| test_prelu(RandomMat(124), 1)
|| test_prelu(RandomMat(127), 127)
|| test_prelu(RandomMat(127), 1);
}
int main()
{
SRAND(7767517);
return 0
|| test_prelu_0()
|| test_prelu_1()
|| test_prelu_2();
}

117
3rdparty/ncnn/tests/test_priorbox.cpp vendored Normal file
View File

@ -0,0 +1,117 @@
// 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/priorbox.h"
#include "testutil.h"
static int test_priorbox_caffe()
{
ncnn::Mat min_sizes(1);
min_sizes[0] = 105.f;
ncnn::Mat max_sizes(1);
max_sizes[0] = 150.f;
ncnn::Mat aspect_ratios(2);
aspect_ratios[0] = 2.f;
aspect_ratios[1] = 3.f;
ncnn::ParamDict pd;
pd.set(0, min_sizes);
pd.set(1, max_sizes);
pd.set(2, aspect_ratios);
pd.set(3, 0.1f); // variances[0]
pd.set(4, 0.1f); // variances[1]
pd.set(5, 0.2f); // variances[2]
pd.set(6, 0.2f); // variances[3]
pd.set(7, 1); // flip
pd.set(8, 0); // clip
pd.set(9, -233); // image_width
pd.set(10, -233); // image_height
pd.set(11, -233.f); // step_width
pd.set(12, -233.f); // step_height
pd.set(13, 0.f); // offset
pd.set(14, 0.f); // step_mmdetection
pd.set(15, 0.f); // center_mmdetection
std::vector<ncnn::Mat> weights(0);
std::vector<ncnn::Mat> as(2);
as[0] = RandomMat(72, 72, 1);
as[1] = RandomMat(512, 512, 1);
int ret = test_layer<ncnn::PriorBox>("PriorBox", pd, weights, as, 1);
if (ret != 0)
{
fprintf(stderr, "test_priorbox_caffe failed\n");
}
return ret;
}
static int test_priorbox_mxnet()
{
ncnn::Mat min_sizes(2);
min_sizes[0] = 0.15f;
min_sizes[1] = 0.2121f;
ncnn::Mat max_sizes(0);
ncnn::Mat aspect_ratios(5);
aspect_ratios[0] = 1.f;
aspect_ratios[1] = 2.f;
aspect_ratios[2] = 0.5f;
aspect_ratios[3] = 3.f;
aspect_ratios[4] = 0.333333;
ncnn::ParamDict pd;
pd.set(0, min_sizes);
pd.set(1, max_sizes);
pd.set(2, aspect_ratios);
pd.set(3, 0.1f); // variances[0]
pd.set(4, 0.1f); // variances[1]
pd.set(5, 0.2f); // variances[2]
pd.set(6, 0.2f); // variances[3]
pd.set(7, 0); // flip
pd.set(8, 0); // clip
pd.set(9, -233); // image_width
pd.set(10, -233); // image_height
pd.set(11, -233.f); // step_width
pd.set(12, -233.f); // step_height
pd.set(13, 0.5f); // offset
pd.set(14, 0.f); // step_mmdetection
pd.set(15, 0.f); // center_mmdetection
std::vector<ncnn::Mat> weights(0);
std::vector<ncnn::Mat> as(1);
as[0] = RandomMat(72, 72, 1);
int ret = test_layer<ncnn::PriorBox>("PriorBox", pd, weights, as, 1);
if (ret != 0)
{
fprintf(stderr, "test_priorbox_mxnet failed\n");
}
return ret;
}
int main()
{
SRAND(7767517);
return 0
|| test_priorbox_caffe()
|| test_priorbox_mxnet();
}

90
3rdparty/ncnn/tests/test_quantize.cpp vendored Normal file
View File

@ -0,0 +1,90 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 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/quantize.h"
#include "testutil.h"
static int test_quantize(const ncnn::Mat& a, float scale_low, float scale_high)
{
ncnn::Mat scale_data;
if (scale_low == scale_high)
{
scale_data.create(1);
scale_data[0] = scale_low;
}
else
{
if (a.dims == 1) scale_data.create(a.w);
if (a.dims == 2) scale_data.create(a.h);
if (a.dims == 3) scale_data.create(a.c);
Randomize(scale_data, scale_low, scale_high);
}
ncnn::ParamDict pd;
pd.set(0, scale_data.w);
std::vector<ncnn::Mat> weights(1);
weights[0] = scale_data;
int ret = test_layer<ncnn::Quantize>("Quantize", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_quantize failed a.dims=%d a=(%d %d %d) scale_low=%f scale_high=%f\n", a.dims, a.w, a.h, a.c, scale_low, scale_high);
}
return ret;
}
static int test_quantize_0()
{
return 0
|| test_quantize(RandomMat(5, 7, 24), 100.f, 100.f)
|| test_quantize(RandomMat(5, 7, 24), 120.f, 140.f)
|| test_quantize(RandomMat(7, 9, 12), 100.f, 100.f)
|| test_quantize(RandomMat(7, 9, 12), 120.f, 140.f)
|| test_quantize(RandomMat(3, 5, 13), 100.f, 100.f)
|| test_quantize(RandomMat(3, 5, 13), 120.f, 140.f);
}
static int test_quantize_1()
{
return 0
|| test_quantize(RandomMat(15, 24), 100.f, 100.f)
|| test_quantize(RandomMat(15, 24), 120.f, 140.f)
|| test_quantize(RandomMat(17, 12), 100.f, 100.f)
|| test_quantize(RandomMat(17, 12), 120.f, 140.f)
|| test_quantize(RandomMat(19, 15), 100.f, 100.f)
|| test_quantize(RandomMat(19, 15), 120.f, 140.f);
}
static int test_quantize_2()
{
return 0
|| test_quantize(RandomMat(128), 100.f, 100.f)
|| test_quantize(RandomMat(128), 120.f, 140.f)
|| test_quantize(RandomMat(124), 100.f, 100.f)
|| test_quantize(RandomMat(124), 120.f, 140.f)
|| test_quantize(RandomMat(127), 100.f, 100.f)
|| test_quantize(RandomMat(127), 120.f, 140.f);
}
int main()
{
SRAND(7767517);
return 0
|| test_quantize_0()
|| test_quantize_1()
|| test_quantize_2();
}

381
3rdparty/ncnn/tests/test_reduction.cpp vendored Normal file
View File

@ -0,0 +1,381 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 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/reduction.h"
#include "testutil.h"
#define OP_TYPE_MAX 11
static int op_type = 0;
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_reduction(const ncnn::Mat& _a, float coeff, int keepdims)
{
ncnn::Mat a = _a;
if (op_type == 9 || op_type == 10)
{
// value must be positive for logsum and logsumexp
Randomize(a, 0.001f, 2.f);
}
ncnn::ParamDict pd;
pd.set(0, op_type);
pd.set(1, 1); // reduce_all
pd.set(2, coeff);
pd.set(4, keepdims);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Reduction>("Reduction", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_reduction failed a.dims=%d a=(%d %d %d %d) op_type=%d coeff=%f keepdims=%d reduce_all=1\n", a.dims, a.w, a.h, a.d, a.c, op_type, coeff, keepdims);
}
return ret;
}
static int test_reduction(const ncnn::Mat& _a, float coeff, int keepdims, const ncnn::Mat& axes)
{
ncnn::Mat a = _a;
if (op_type == 9 || op_type == 10)
{
// value must be positive for logsum and logsumexp
Randomize(a, 0.001f, 2.f);
}
ncnn::ParamDict pd;
pd.set(0, op_type);
pd.set(1, 0); // reduce_all
pd.set(2, coeff);
pd.set(3, axes);
pd.set(4, keepdims);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Reduction>("Reduction", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_reduction failed a.dims=%d a=(%d %d %d %d) op_type=%d coeff=%f keepdims=%d", a.dims, a.w, a.h, a.d, a.c, op_type, coeff, keepdims);
fprintf(stderr, " axes=");
print_int_array(axes);
fprintf(stderr, "\n");
}
return ret;
}
static int test_reduction_0()
{
return 0
|| test_reduction(RandomMat(5, 6, 7, 24), 1.f, 0)
|| test_reduction(RandomMat(5, 6, 7, 24), 2.f, 0)
|| test_reduction(RandomMat(7, 8, 9, 12), 1.f, 0)
|| test_reduction(RandomMat(7, 8, 9, 12), 2.f, 0)
|| test_reduction(RandomMat(3, 4, 5, 13), 1.f, 0)
|| test_reduction(RandomMat(3, 4, 5, 13), 2.f, 0)
|| test_reduction(RandomMat(5, 6, 7, 24), 1.f, 1)
|| test_reduction(RandomMat(5, 6, 7, 24), 2.f, 1)
|| test_reduction(RandomMat(7, 8, 9, 12), 1.f, 1)
|| test_reduction(RandomMat(7, 8, 9, 12), 2.f, 1)
|| test_reduction(RandomMat(3, 4, 5, 13), 1.f, 1)
|| test_reduction(RandomMat(3, 4, 5, 13), 2.f, 1)
|| test_reduction(RandomMat(5, 6, 7, 24), 1.f, 0, IntArrayMat(0))
|| test_reduction(RandomMat(5, 6, 7, 24), 2.f, 0, IntArrayMat(1))
|| test_reduction(RandomMat(5, 6, 7, 24), 1.f, 0, IntArrayMat(2))
|| test_reduction(RandomMat(5, 6, 7, 24), 2.f, 0, IntArrayMat(3))
|| test_reduction(RandomMat(5, 6, 7, 24), 1.f, 0, IntArrayMat(0, 1))
|| test_reduction(RandomMat(5, 6, 7, 24), 2.f, 0, IntArrayMat(0, 2))
|| test_reduction(RandomMat(5, 6, 7, 24), 1.f, 0, IntArrayMat(0, 3))
|| test_reduction(RandomMat(5, 6, 7, 24), 2.f, 0, IntArrayMat(1, 2))
|| test_reduction(RandomMat(5, 6, 7, 24), 1.f, 0, IntArrayMat(1, 3))
|| test_reduction(RandomMat(5, 6, 7, 24), 2.f, 0, IntArrayMat(2, 3))
|| test_reduction(RandomMat(5, 6, 7, 24), 1.f, 0, IntArrayMat(0, 1, 2))
|| test_reduction(RandomMat(5, 6, 7, 24), 2.f, 0, IntArrayMat(0, 1, 3))
|| test_reduction(RandomMat(5, 6, 7, 24), 1.f, 0, IntArrayMat(0, 2, 3))
|| test_reduction(RandomMat(5, 6, 7, 24), 2.f, 0, IntArrayMat(1, 2, 3))
|| test_reduction(RandomMat(5, 6, 7, 24), 1.f, 0, IntArrayMat(0, 1, 2, 3))
|| test_reduction(RandomMat(7, 8, 9, 12), 1.f, 0, IntArrayMat(0))
|| test_reduction(RandomMat(7, 8, 9, 12), 2.f, 0, IntArrayMat(1))
|| test_reduction(RandomMat(7, 8, 9, 12), 1.f, 0, IntArrayMat(2))
|| test_reduction(RandomMat(7, 8, 9, 12), 2.f, 0, IntArrayMat(3))
|| test_reduction(RandomMat(7, 8, 9, 12), 1.f, 0, IntArrayMat(0, 1))
|| test_reduction(RandomMat(7, 8, 9, 12), 2.f, 0, IntArrayMat(0, 2))
|| test_reduction(RandomMat(7, 8, 9, 12), 1.f, 0, IntArrayMat(0, 3))
|| test_reduction(RandomMat(7, 8, 9, 12), 2.f, 0, IntArrayMat(1, 2))
|| test_reduction(RandomMat(7, 8, 9, 12), 1.f, 0, IntArrayMat(1, 3))
|| test_reduction(RandomMat(7, 8, 9, 12), 2.f, 0, IntArrayMat(2, 3))
|| test_reduction(RandomMat(7, 8, 9, 12), 1.f, 0, IntArrayMat(0, 1, 2))
|| test_reduction(RandomMat(7, 8, 9, 12), 2.f, 0, IntArrayMat(0, 1, 3))
|| test_reduction(RandomMat(7, 8, 9, 12), 1.f, 0, IntArrayMat(0, 2, 3))
|| test_reduction(RandomMat(7, 8, 9, 12), 2.f, 0, IntArrayMat(1, 2, 3))
|| test_reduction(RandomMat(7, 8, 9, 12), 1.f, 0, IntArrayMat(0, 1, 2, 3))
|| test_reduction(RandomMat(3, 4, 5, 13), 1.f, 0, IntArrayMat(0))
|| test_reduction(RandomMat(3, 4, 5, 13), 2.f, 0, IntArrayMat(1))
|| test_reduction(RandomMat(3, 4, 5, 13), 1.f, 0, IntArrayMat(2))
|| test_reduction(RandomMat(3, 4, 5, 13), 2.f, 0, IntArrayMat(3))
|| test_reduction(RandomMat(3, 4, 5, 13), 1.f, 0, IntArrayMat(0, 1))
|| test_reduction(RandomMat(3, 4, 5, 13), 2.f, 0, IntArrayMat(0, 2))
|| test_reduction(RandomMat(3, 4, 5, 13), 1.f, 0, IntArrayMat(0, 3))
|| test_reduction(RandomMat(3, 4, 5, 13), 2.f, 0, IntArrayMat(1, 2))
|| test_reduction(RandomMat(3, 4, 5, 13), 1.f, 0, IntArrayMat(1, 3))
|| test_reduction(RandomMat(3, 4, 5, 13), 2.f, 0, IntArrayMat(2, 3))
|| test_reduction(RandomMat(3, 4, 5, 13), 1.f, 0, IntArrayMat(0, 1, 2))
|| test_reduction(RandomMat(3, 4, 5, 13), 2.f, 0, IntArrayMat(0, 1, 3))
|| test_reduction(RandomMat(3, 4, 5, 13), 1.f, 0, IntArrayMat(0, 2, 3))
|| test_reduction(RandomMat(3, 4, 5, 13), 2.f, 0, IntArrayMat(1, 2, 3))
|| test_reduction(RandomMat(3, 4, 5, 13), 1.f, 0, IntArrayMat(0, 1, 2, 3))
|| test_reduction(RandomMat(5, 6, 7, 24), 1.f, 1, IntArrayMat(0))
|| test_reduction(RandomMat(5, 6, 7, 24), 2.f, 1, IntArrayMat(1))
|| test_reduction(RandomMat(5, 6, 7, 24), 1.f, 1, IntArrayMat(2))
|| test_reduction(RandomMat(5, 6, 7, 24), 2.f, 1, IntArrayMat(3))
|| test_reduction(RandomMat(5, 6, 7, 24), 1.f, 1, IntArrayMat(0, 1))
|| test_reduction(RandomMat(5, 6, 7, 24), 2.f, 1, IntArrayMat(0, 2))
|| test_reduction(RandomMat(5, 6, 7, 24), 1.f, 1, IntArrayMat(0, 3))
|| test_reduction(RandomMat(5, 6, 7, 24), 2.f, 1, IntArrayMat(1, 2))
|| test_reduction(RandomMat(5, 6, 7, 24), 1.f, 1, IntArrayMat(1, 3))
|| test_reduction(RandomMat(5, 6, 7, 24), 2.f, 1, IntArrayMat(2, 3))
|| test_reduction(RandomMat(5, 6, 7, 24), 1.f, 1, IntArrayMat(0, 1, 2))
|| test_reduction(RandomMat(5, 6, 7, 24), 2.f, 1, IntArrayMat(0, 1, 3))
|| test_reduction(RandomMat(5, 6, 7, 24), 1.f, 1, IntArrayMat(0, 2, 3))
|| test_reduction(RandomMat(5, 6, 7, 24), 2.f, 1, IntArrayMat(1, 2, 3))
|| test_reduction(RandomMat(5, 6, 7, 24), 1.f, 1, IntArrayMat(0, 1, 2, 3))
|| test_reduction(RandomMat(7, 8, 9, 12), 1.f, 1, IntArrayMat(0))
|| test_reduction(RandomMat(7, 8, 9, 12), 2.f, 1, IntArrayMat(1))
|| test_reduction(RandomMat(7, 8, 9, 12), 1.f, 1, IntArrayMat(2))
|| test_reduction(RandomMat(7, 8, 9, 12), 2.f, 1, IntArrayMat(3))
|| test_reduction(RandomMat(7, 8, 9, 12), 1.f, 1, IntArrayMat(0, 1))
|| test_reduction(RandomMat(7, 8, 9, 12), 2.f, 1, IntArrayMat(0, 2))
|| test_reduction(RandomMat(7, 8, 9, 12), 1.f, 1, IntArrayMat(0, 3))
|| test_reduction(RandomMat(7, 8, 9, 12), 2.f, 1, IntArrayMat(1, 2))
|| test_reduction(RandomMat(7, 8, 9, 12), 1.f, 1, IntArrayMat(1, 3))
|| test_reduction(RandomMat(7, 8, 9, 12), 2.f, 1, IntArrayMat(2, 3))
|| test_reduction(RandomMat(7, 8, 9, 12), 1.f, 1, IntArrayMat(0, 1, 2))
|| test_reduction(RandomMat(7, 8, 9, 12), 2.f, 1, IntArrayMat(0, 1, 3))
|| test_reduction(RandomMat(7, 8, 9, 12), 1.f, 1, IntArrayMat(0, 2, 3))
|| test_reduction(RandomMat(7, 8, 9, 12), 2.f, 1, IntArrayMat(1, 2, 3))
|| test_reduction(RandomMat(7, 8, 9, 12), 1.f, 1, IntArrayMat(0, 1, 2, 3))
|| test_reduction(RandomMat(3, 4, 5, 13), 1.f, 1, IntArrayMat(0))
|| test_reduction(RandomMat(3, 4, 5, 13), 2.f, 1, IntArrayMat(1))
|| test_reduction(RandomMat(3, 4, 5, 13), 1.f, 1, IntArrayMat(2))
|| test_reduction(RandomMat(3, 4, 5, 13), 2.f, 1, IntArrayMat(3))
|| test_reduction(RandomMat(3, 4, 5, 13), 1.f, 1, IntArrayMat(0, 1))
|| test_reduction(RandomMat(3, 4, 5, 13), 2.f, 1, IntArrayMat(0, 2))
|| test_reduction(RandomMat(3, 4, 5, 13), 1.f, 1, IntArrayMat(0, 3))
|| test_reduction(RandomMat(3, 4, 5, 13), 2.f, 1, IntArrayMat(1, 2))
|| test_reduction(RandomMat(3, 4, 5, 13), 1.f, 1, IntArrayMat(1, 3))
|| test_reduction(RandomMat(3, 4, 5, 13), 2.f, 1, IntArrayMat(2, 3))
|| test_reduction(RandomMat(3, 4, 5, 13), 1.f, 1, IntArrayMat(0, 1, 2))
|| test_reduction(RandomMat(3, 4, 5, 13), 2.f, 1, IntArrayMat(0, 1, 3))
|| test_reduction(RandomMat(3, 4, 5, 13), 1.f, 1, IntArrayMat(0, 2, 3))
|| test_reduction(RandomMat(3, 4, 5, 13), 2.f, 1, IntArrayMat(1, 2, 3))
|| test_reduction(RandomMat(3, 4, 5, 13), 1.f, 1, IntArrayMat(0, 1, 2, 3));
}
static int test_reduction_1()
{
return 0
|| test_reduction(RandomMat(5, 7, 24), 1.f, 0)
|| test_reduction(RandomMat(5, 7, 24), 2.f, 0)
|| test_reduction(RandomMat(7, 9, 12), 1.f, 0)
|| test_reduction(RandomMat(7, 9, 12), 2.f, 0)
|| test_reduction(RandomMat(3, 5, 13), 1.f, 0)
|| test_reduction(RandomMat(3, 5, 13), 2.f, 0)
|| test_reduction(RandomMat(5, 7, 24), 1.f, 1)
|| test_reduction(RandomMat(5, 7, 24), 2.f, 1)
|| test_reduction(RandomMat(7, 9, 12), 1.f, 1)
|| test_reduction(RandomMat(7, 9, 12), 2.f, 1)
|| test_reduction(RandomMat(3, 5, 13), 1.f, 1)
|| test_reduction(RandomMat(3, 5, 13), 2.f, 1)
|| test_reduction(RandomMat(5, 7, 24), 1.f, 0, IntArrayMat(0))
|| test_reduction(RandomMat(5, 7, 24), 2.f, 0, IntArrayMat(1))
|| test_reduction(RandomMat(5, 7, 24), 1.f, 0, IntArrayMat(0, 1))
|| test_reduction(RandomMat(5, 7, 24), 2.f, 0, IntArrayMat(0, 2))
|| test_reduction(RandomMat(5, 7, 24), 1.f, 0, IntArrayMat(1, 2))
|| test_reduction(RandomMat(5, 7, 24), 2.f, 0, IntArrayMat(0, 1, 2))
|| test_reduction(RandomMat(7, 9, 12), 1.f, 0, IntArrayMat(0))
|| test_reduction(RandomMat(7, 9, 12), 2.f, 0, IntArrayMat(1))
|| test_reduction(RandomMat(7, 9, 12), 1.f, 0, IntArrayMat(0, 1))
|| test_reduction(RandomMat(7, 9, 12), 2.f, 0, IntArrayMat(0, 2))
|| test_reduction(RandomMat(7, 9, 12), 1.f, 0, IntArrayMat(1, 2))
|| test_reduction(RandomMat(7, 9, 12), 2.f, 0, IntArrayMat(0, 1, 2))
|| test_reduction(RandomMat(3, 5, 13), 1.f, 0, IntArrayMat(0))
|| test_reduction(RandomMat(3, 5, 13), 2.f, 0, IntArrayMat(1))
|| test_reduction(RandomMat(3, 5, 13), 1.f, 0, IntArrayMat(0, 1))
|| test_reduction(RandomMat(3, 5, 13), 2.f, 0, IntArrayMat(0, 2))
|| test_reduction(RandomMat(3, 5, 13), 1.f, 0, IntArrayMat(1, 2))
|| test_reduction(RandomMat(3, 5, 13), 2.f, 0, IntArrayMat(0, 1, 2))
|| test_reduction(RandomMat(5, 7, 24), 1.f, 1, IntArrayMat(0))
|| test_reduction(RandomMat(5, 7, 24), 2.f, 1, IntArrayMat(1))
|| test_reduction(RandomMat(5, 7, 24), 1.f, 1, IntArrayMat(0, 1))
|| test_reduction(RandomMat(5, 7, 24), 2.f, 1, IntArrayMat(0, 2))
|| test_reduction(RandomMat(5, 7, 24), 1.f, 1, IntArrayMat(1, 2))
|| test_reduction(RandomMat(5, 7, 24), 2.f, 1, IntArrayMat(0, 1, 2))
|| test_reduction(RandomMat(7, 9, 12), 1.f, 1, IntArrayMat(0))
|| test_reduction(RandomMat(7, 9, 12), 2.f, 1, IntArrayMat(1))
|| test_reduction(RandomMat(7, 9, 12), 1.f, 1, IntArrayMat(0, 1))
|| test_reduction(RandomMat(7, 9, 12), 2.f, 1, IntArrayMat(0, 2))
|| test_reduction(RandomMat(7, 9, 12), 1.f, 1, IntArrayMat(1, 2))
|| test_reduction(RandomMat(7, 9, 12), 2.f, 1, IntArrayMat(0, 1, 2))
|| test_reduction(RandomMat(3, 5, 13), 1.f, 1, IntArrayMat(0))
|| test_reduction(RandomMat(3, 5, 13), 2.f, 1, IntArrayMat(1))
|| test_reduction(RandomMat(3, 5, 13), 1.f, 1, IntArrayMat(0, 1))
|| test_reduction(RandomMat(3, 5, 13), 2.f, 1, IntArrayMat(0, 2))
|| test_reduction(RandomMat(3, 5, 13), 1.f, 1, IntArrayMat(1, 2))
|| test_reduction(RandomMat(3, 5, 13), 2.f, 1, IntArrayMat(0, 1, 2));
}
static int test_reduction_2()
{
return 0
|| test_reduction(RandomMat(15, 24), 1.f, 0)
|| test_reduction(RandomMat(15, 24), 2.f, 0)
|| test_reduction(RandomMat(17, 12), 1.f, 0)
|| test_reduction(RandomMat(17, 12), 2.f, 0)
|| test_reduction(RandomMat(19, 15), 1.f, 0)
|| test_reduction(RandomMat(19, 15), 2.f, 0)
|| test_reduction(RandomMat(15, 24), 1.f, 1)
|| test_reduction(RandomMat(15, 24), 2.f, 1)
|| test_reduction(RandomMat(17, 12), 1.f, 1)
|| test_reduction(RandomMat(17, 12), 2.f, 1)
|| test_reduction(RandomMat(19, 15), 1.f, 1)
|| test_reduction(RandomMat(19, 15), 2.f, 1)
|| test_reduction(RandomMat(15, 24), 1.f, 0, IntArrayMat(0))
|| test_reduction(RandomMat(15, 24), 2.f, 0, IntArrayMat(1))
|| test_reduction(RandomMat(15, 24), 1.f, 0, IntArrayMat(0, 1))
|| test_reduction(RandomMat(17, 12), 2.f, 0, IntArrayMat(0))
|| test_reduction(RandomMat(17, 12), 1.f, 0, IntArrayMat(1))
|| test_reduction(RandomMat(17, 12), 2.f, 0, IntArrayMat(0, 1))
|| test_reduction(RandomMat(19, 15), 1.f, 0, IntArrayMat(0))
|| test_reduction(RandomMat(19, 15), 2.f, 0, IntArrayMat(1))
|| test_reduction(RandomMat(19, 15), 1.f, 0, IntArrayMat(0, 1))
|| test_reduction(RandomMat(15, 24), 2.f, 1, IntArrayMat(0))
|| test_reduction(RandomMat(15, 24), 1.f, 1, IntArrayMat(1))
|| test_reduction(RandomMat(15, 24), 2.f, 1, IntArrayMat(0, 1))
|| test_reduction(RandomMat(17, 12), 1.f, 1, IntArrayMat(0))
|| test_reduction(RandomMat(17, 12), 2.f, 1, IntArrayMat(1))
|| test_reduction(RandomMat(17, 12), 1.f, 1, IntArrayMat(0, 1))
|| test_reduction(RandomMat(19, 15), 2.f, 1, IntArrayMat(0))
|| test_reduction(RandomMat(19, 15), 1.f, 1, IntArrayMat(1))
|| test_reduction(RandomMat(19, 15), 2.f, 1, IntArrayMat(0, 1));
}
static int test_reduction_3()
{
return 0
|| test_reduction(RandomMat(128), 1.f, 0)
|| test_reduction(RandomMat(128), 2.f, 0)
|| test_reduction(RandomMat(124), 1.f, 0)
|| test_reduction(RandomMat(124), 2.f, 0)
|| test_reduction(RandomMat(127), 1.f, 0)
|| test_reduction(RandomMat(127), 2.f, 0)
|| test_reduction(RandomMat(128), 1.f, 1)
|| test_reduction(RandomMat(128), 2.f, 1)
|| test_reduction(RandomMat(124), 1.f, 1)
|| test_reduction(RandomMat(124), 2.f, 1)
|| test_reduction(RandomMat(127), 1.f, 1)
|| test_reduction(RandomMat(127), 2.f, 1)
|| test_reduction(RandomMat(128), 1.f, 0, IntArrayMat(0))
|| test_reduction(RandomMat(128), 2.f, 0, IntArrayMat(0))
|| test_reduction(RandomMat(124), 1.f, 0, IntArrayMat(0))
|| test_reduction(RandomMat(124), 2.f, 0, IntArrayMat(0))
|| test_reduction(RandomMat(127), 1.f, 0, IntArrayMat(0))
|| test_reduction(RandomMat(127), 2.f, 0, IntArrayMat(0))
|| test_reduction(RandomMat(128), 1.f, 1, IntArrayMat(0))
|| test_reduction(RandomMat(128), 2.f, 1, IntArrayMat(0))
|| test_reduction(RandomMat(124), 1.f, 1, IntArrayMat(0))
|| test_reduction(RandomMat(124), 2.f, 1, IntArrayMat(0))
|| test_reduction(RandomMat(127), 1.f, 1, IntArrayMat(0))
|| test_reduction(RandomMat(127), 1.f, 1, IntArrayMat(0));
}
int main()
{
SRAND(7767517);
for (op_type = 0; op_type < OP_TYPE_MAX; op_type++)
{
int ret = 0
|| test_reduction_0()
|| test_reduction_1()
|| test_reduction_2()
|| test_reduction_3();
if (ret != 0)
return ret;
}
return 0;
}

87
3rdparty/ncnn/tests/test_relu.cpp vendored Normal file
View File

@ -0,0 +1,87 @@
// 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/relu.h"
#include "testutil.h"
static int test_relu(const ncnn::Mat& a, float slope)
{
ncnn::ParamDict pd;
pd.set(0, slope); //slope
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::ReLU>("ReLU", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_relu failed a.dims=%d a=(%d %d %d %d) slope=%f\n", a.dims, a.w, a.h, a.d, a.c, slope);
}
return ret;
}
static int test_relu_0()
{
return 0
|| test_relu(RandomMat(5, 6, 7, 24), 0.f)
|| test_relu(RandomMat(5, 6, 7, 24), 0.1f)
|| test_relu(RandomMat(7, 8, 9, 12), 0.f)
|| test_relu(RandomMat(7, 8, 9, 12), 0.1f)
|| test_relu(RandomMat(3, 4, 5, 13), 0.f)
|| test_relu(RandomMat(3, 4, 5, 13), 0.1f);
}
static int test_relu_1()
{
return 0
|| test_relu(RandomMat(5, 7, 24), 0.f)
|| test_relu(RandomMat(5, 7, 24), 0.1f)
|| test_relu(RandomMat(7, 9, 12), 0.f)
|| test_relu(RandomMat(7, 9, 12), 0.1f)
|| test_relu(RandomMat(3, 5, 13), 0.f)
|| test_relu(RandomMat(3, 5, 13), 0.1f);
}
static int test_relu_2()
{
return 0
|| test_relu(RandomMat(15, 24), 0.f)
|| test_relu(RandomMat(15, 24), 0.1f)
|| test_relu(RandomMat(17, 12), 0.f)
|| test_relu(RandomMat(17, 12), 0.1f)
|| test_relu(RandomMat(19, 15), 0.f)
|| test_relu(RandomMat(19, 15), 0.1f);
}
static int test_relu_3()
{
return 0
|| test_relu(RandomMat(128), 0.f)
|| test_relu(RandomMat(128), 0.1f)
|| test_relu(RandomMat(124), 0.f)
|| test_relu(RandomMat(124), 0.1f)
|| test_relu(RandomMat(127), 0.f)
|| test_relu(RandomMat(127), 0.1f);
}
int main()
{
SRAND(7767517);
return 0
|| test_relu_0()
|| test_relu_1()
|| test_relu_2()
|| test_relu_3();
}

66
3rdparty/ncnn/tests/test_reorg.cpp vendored Normal file
View File

@ -0,0 +1,66 @@
// 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/reorg.h"
#include "testutil.h"
static int test_reorg(const ncnn::Mat& a, int stride, int mode)
{
ncnn::ParamDict pd;
pd.set(0, stride);
pd.set(1, mode);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Reorg>("Reorg", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_reorg failed a.dims=%d a=(%d %d %d) stride=%d mode=%d\n", a.dims, a.w, a.h, a.c, stride, mode);
}
return ret;
}
static int test_reorg_0()
{
return 0
|| test_reorg(RandomMat(6, 7, 1), 1, 0)
|| test_reorg(RandomMat(6, 6, 2), 2, 0)
|| test_reorg(RandomMat(6, 8, 3), 2, 0)
|| test_reorg(RandomMat(4, 4, 4), 4, 0)
|| test_reorg(RandomMat(8, 8, 8), 2, 0)
|| test_reorg(RandomMat(10, 10, 12), 2, 0)
|| test_reorg(RandomMat(9, 9, 4), 3, 0)
|| test_reorg(RandomMat(9, 9, 16), 3, 0);
}
static int test_reorg_1()
{
return 0
|| test_reorg(RandomMat(6, 7, 1), 1, 1)
|| test_reorg(RandomMat(6, 6, 2), 2, 1)
|| test_reorg(RandomMat(6, 8, 3), 2, 1)
|| test_reorg(RandomMat(4, 4, 4), 4, 1)
|| test_reorg(RandomMat(8, 8, 8), 2, 1)
|| test_reorg(RandomMat(10, 10, 12), 2, 1)
|| test_reorg(RandomMat(9, 9, 4), 3, 1)
|| test_reorg(RandomMat(9, 9, 16), 3, 1);
}
int main()
{
SRAND(7767517);
return test_reorg_0() || test_reorg_1();
}

277
3rdparty/ncnn/tests/test_requantize.cpp vendored Normal file
View File

@ -0,0 +1,277 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 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/requantize.h"
#include "testutil.h"
static int test_requantize(const ncnn::Mat& a, int scale_in_data_size, int scale_out_data_size, int bias_data_size, int activation_type, float alpha, float beta)
{
ncnn::ParamDict pd;
pd.set(0, scale_in_data_size);
pd.set(1, scale_out_data_size);
pd.set(2, bias_data_size);
ncnn::Mat activation_params(2);
activation_params[0] = alpha;
activation_params[1] = beta;
pd.set(3, activation_type);
pd.set(4, activation_params);
std::vector<ncnn::Mat> weights(bias_data_size ? 3 : 2);
weights[0] = RandomMat(scale_in_data_size);
weights[1] = RandomMat(scale_out_data_size);
if (bias_data_size)
weights[2] = RandomMat(bias_data_size);
Randomize(weights[0], 0.0001, 0.001);
Randomize(weights[1], 10, 100);
int flag = TEST_LAYER_DISABLE_AUTO_INPUT_CASTING;
int ret = test_layer<ncnn::Requantize>("Requantize", pd, weights, a, 1, 0, flag);
if (ret != 0)
{
fprintf(stderr, "test_requantize failed a.dims=%d a=(%d %d %d) scale_in_data_size=%d scale_out_data_size=%d bias_data_size=%d act=%d actparams=[%f,%f]\n", a.dims, a.w, a.h, a.c, scale_in_data_size, scale_out_data_size, bias_data_size, activation_type, activation_params[0], activation_params[1]);
}
return ret;
}
static int test_requantize(const ncnn::Mat& a, int scale_in_data_size, int scale_out_data_size, int bias_data_size)
{
return 0
|| test_requantize(a, scale_in_data_size, scale_out_data_size, bias_data_size, 0, 0.f, 0.f)
|| test_requantize(a, scale_in_data_size, scale_out_data_size, bias_data_size, 1, 0.f, 0.f)
|| test_requantize(a, scale_in_data_size, scale_out_data_size, bias_data_size, 2, RandomFloat(0, 1), 0.f)
|| test_requantize(a, scale_in_data_size, scale_out_data_size, bias_data_size, 3, RandomFloat(-1, 0), RandomFloat(0, 1))
|| test_requantize(a, scale_in_data_size, scale_out_data_size, bias_data_size, 4, 0.f, 0.f)
|| test_requantize(a, scale_in_data_size, scale_out_data_size, bias_data_size, 5, 0.f, 0.f);
}
static int test_requantize_pack8(const ncnn::Mat& a, int scale_in_data_size, int scale_out_data_size, int bias_data_size, int activation_type, float alpha, float beta)
{
ncnn::ParamDict pd;
pd.set(0, scale_in_data_size);
pd.set(1, scale_out_data_size);
pd.set(2, bias_data_size);
ncnn::Mat activation_params(2);
activation_params[0] = alpha;
activation_params[1] = beta;
pd.set(3, activation_type);
pd.set(4, activation_params);
std::vector<ncnn::Mat> weights(bias_data_size ? 3 : 2);
weights[0] = RandomMat(scale_in_data_size);
weights[1] = RandomMat(scale_out_data_size);
if (bias_data_size)
weights[2] = RandomMat(bias_data_size);
Randomize(weights[0], 0.0001, 0.001);
Randomize(weights[1], 10, 100);
int flag = TEST_LAYER_DISABLE_AUTO_INPUT_CASTING | TEST_LAYER_ENABLE_FORCE_INPUT_PACK8;
int ret = test_layer<ncnn::Requantize>("Requantize", pd, weights, a, 1, 0, flag);
if (ret != 0)
{
fprintf(stderr, "test_requantize_pack8 failed a.dims=%d a=(%d %d %d) scale_in_data_size=%d scale_out_data_size=%d bias_data_size=%d act=%d actparams=[%f,%f]\n", a.dims, a.w, a.h, a.c, scale_in_data_size, scale_out_data_size, bias_data_size, activation_type, activation_params[0], activation_params[1]);
}
return ret;
}
static int test_requantize_pack8(const ncnn::Mat& a, int scale_in_data_size, int scale_out_data_size, int bias_data_size)
{
return 0
|| test_requantize_pack8(a, scale_in_data_size, scale_out_data_size, bias_data_size, 0, 0.f, 0.f)
|| test_requantize_pack8(a, scale_in_data_size, scale_out_data_size, bias_data_size, 1, 0.f, 0.f)
|| test_requantize_pack8(a, scale_in_data_size, scale_out_data_size, bias_data_size, 2, RandomFloat(0, 1), 0.f)
|| test_requantize_pack8(a, scale_in_data_size, scale_out_data_size, bias_data_size, 3, RandomFloat(-1, 0), RandomFloat(0, 1))
|| test_requantize_pack8(a, scale_in_data_size, scale_out_data_size, bias_data_size, 4, 0.f, 0.f)
|| test_requantize_pack8(a, scale_in_data_size, scale_out_data_size, bias_data_size, 5, 0.f, 0.f);
}
static int test_requantize_0()
{
return 0
|| test_requantize(RandomIntMat(5, 7, 24), 1, 1, 24)
|| test_requantize(RandomIntMat(5, 7, 24), 1, 1, 1)
|| test_requantize(RandomIntMat(5, 7, 24), 1, 1, 0)
|| test_requantize(RandomIntMat(5, 7, 24), 24, 24, 24)
|| test_requantize(RandomIntMat(5, 7, 24), 24, 24, 1)
|| test_requantize(RandomIntMat(5, 7, 24), 24, 24, 0)
|| test_requantize(RandomIntMat(5, 7, 24), 1, 24, 24)
|| test_requantize(RandomIntMat(5, 7, 24), 1, 24, 1)
|| test_requantize(RandomIntMat(5, 7, 24), 1, 24, 0)
|| test_requantize(RandomIntMat(5, 7, 24), 24, 1, 24)
|| test_requantize(RandomIntMat(5, 7, 24), 24, 1, 1)
|| test_requantize(RandomIntMat(5, 7, 24), 24, 1, 0)
|| test_requantize(RandomIntMat(7, 9, 12), 1, 1, 12)
|| test_requantize(RandomIntMat(7, 9, 12), 1, 1, 1)
|| test_requantize(RandomIntMat(7, 9, 12), 1, 1, 0)
|| test_requantize(RandomIntMat(7, 9, 12), 12, 12, 12)
|| test_requantize(RandomIntMat(7, 9, 12), 12, 12, 1)
|| test_requantize(RandomIntMat(7, 9, 12), 12, 12, 0)
|| test_requantize(RandomIntMat(7, 9, 12), 1, 12, 12)
|| test_requantize(RandomIntMat(7, 9, 12), 1, 12, 1)
|| test_requantize(RandomIntMat(7, 9, 12), 1, 12, 0)
|| test_requantize(RandomIntMat(7, 9, 12), 12, 1, 12)
|| test_requantize(RandomIntMat(7, 9, 12), 12, 1, 1)
|| test_requantize(RandomIntMat(7, 9, 12), 12, 1, 0)
|| test_requantize(RandomIntMat(3, 5, 13), 1, 1, 13)
|| test_requantize(RandomIntMat(3, 5, 13), 1, 1, 1)
|| test_requantize(RandomIntMat(3, 5, 13), 1, 1, 0)
|| test_requantize(RandomIntMat(3, 5, 13), 13, 13, 13)
|| test_requantize(RandomIntMat(3, 5, 13), 13, 13, 1)
|| test_requantize(RandomIntMat(3, 5, 13), 13, 13, 0)
|| test_requantize(RandomIntMat(3, 5, 13), 1, 13, 13)
|| test_requantize(RandomIntMat(3, 5, 13), 1, 13, 1)
|| test_requantize(RandomIntMat(3, 5, 13), 1, 13, 0)
|| test_requantize(RandomIntMat(3, 5, 13), 13, 1, 13)
|| test_requantize(RandomIntMat(3, 5, 13), 13, 1, 1)
|| test_requantize(RandomIntMat(3, 5, 13), 13, 1, 0);
}
static int test_requantize_1()
{
return 0
|| test_requantize(RandomIntMat(15, 24), 1, 1, 24)
|| test_requantize(RandomIntMat(15, 24), 1, 1, 1)
|| test_requantize(RandomIntMat(15, 24), 1, 1, 0)
|| test_requantize(RandomIntMat(15, 24), 24, 24, 24)
|| test_requantize(RandomIntMat(15, 24), 24, 24, 1)
|| test_requantize(RandomIntMat(15, 24), 24, 24, 0)
|| test_requantize(RandomIntMat(15, 24), 1, 24, 24)
|| test_requantize(RandomIntMat(15, 24), 1, 24, 1)
|| test_requantize(RandomIntMat(15, 24), 1, 24, 0)
|| test_requantize(RandomIntMat(15, 24), 24, 1, 24)
|| test_requantize(RandomIntMat(15, 24), 24, 1, 1)
|| test_requantize(RandomIntMat(15, 24), 24, 1, 0)
|| test_requantize(RandomIntMat(17, 12), 1, 1, 12)
|| test_requantize(RandomIntMat(17, 12), 1, 1, 1)
|| test_requantize(RandomIntMat(17, 12), 1, 1, 0)
|| test_requantize(RandomIntMat(17, 12), 12, 12, 12)
|| test_requantize(RandomIntMat(17, 12), 12, 12, 1)
|| test_requantize(RandomIntMat(17, 12), 12, 12, 0)
|| test_requantize(RandomIntMat(17, 12), 1, 12, 12)
|| test_requantize(RandomIntMat(17, 12), 1, 12, 1)
|| test_requantize(RandomIntMat(17, 12), 1, 12, 0)
|| test_requantize(RandomIntMat(17, 12), 12, 1, 12)
|| test_requantize(RandomIntMat(17, 12), 12, 1, 1)
|| test_requantize(RandomIntMat(17, 12), 12, 1, 0)
|| test_requantize(RandomIntMat(19, 15), 1, 1, 15)
|| test_requantize(RandomIntMat(19, 15), 1, 1, 1)
|| test_requantize(RandomIntMat(19, 15), 1, 1, 0)
|| test_requantize(RandomIntMat(19, 15), 15, 15, 15)
|| test_requantize(RandomIntMat(19, 15), 15, 15, 1)
|| test_requantize(RandomIntMat(19, 15), 15, 15, 0)
|| test_requantize(RandomIntMat(19, 15), 1, 15, 15)
|| test_requantize(RandomIntMat(19, 15), 1, 15, 1)
|| test_requantize(RandomIntMat(19, 15), 1, 15, 0)
|| test_requantize(RandomIntMat(19, 15), 15, 1, 15)
|| test_requantize(RandomIntMat(19, 15), 15, 1, 1)
|| test_requantize(RandomIntMat(19, 15), 15, 1, 0);
}
static int test_requantize_2()
{
return 0
|| test_requantize(RandomIntMat(128), 1, 1, 128)
|| test_requantize(RandomIntMat(128), 1, 1, 1)
|| test_requantize(RandomIntMat(128), 1, 1, 0)
|| test_requantize(RandomIntMat(128), 128, 128, 128)
|| test_requantize(RandomIntMat(128), 128, 128, 1)
|| test_requantize(RandomIntMat(128), 128, 128, 0)
|| test_requantize(RandomIntMat(128), 1, 128, 128)
|| test_requantize(RandomIntMat(128), 1, 128, 1)
|| test_requantize(RandomIntMat(128), 1, 128, 0)
|| test_requantize(RandomIntMat(128), 128, 1, 128)
|| test_requantize(RandomIntMat(128), 128, 1, 1)
|| test_requantize(RandomIntMat(128), 128, 1, 0)
|| test_requantize(RandomIntMat(124), 1, 1, 124)
|| test_requantize(RandomIntMat(124), 1, 1, 1)
|| test_requantize(RandomIntMat(124), 1, 1, 0)
|| test_requantize(RandomIntMat(124), 124, 124, 124)
|| test_requantize(RandomIntMat(124), 124, 124, 1)
|| test_requantize(RandomIntMat(124), 124, 124, 0)
|| test_requantize(RandomIntMat(124), 1, 124, 124)
|| test_requantize(RandomIntMat(124), 1, 124, 1)
|| test_requantize(RandomIntMat(124), 1, 124, 0)
|| test_requantize(RandomIntMat(124), 124, 1, 124)
|| test_requantize(RandomIntMat(124), 124, 1, 1)
|| test_requantize(RandomIntMat(124), 124, 1, 0)
|| test_requantize(RandomIntMat(127), 1, 1, 127)
|| test_requantize(RandomIntMat(127), 1, 1, 1)
|| test_requantize(RandomIntMat(127), 1, 1, 0)
|| test_requantize(RandomIntMat(127), 127, 127, 127)
|| test_requantize(RandomIntMat(127), 127, 127, 1)
|| test_requantize(RandomIntMat(127), 127, 127, 0)
|| test_requantize(RandomIntMat(127), 1, 127, 127)
|| test_requantize(RandomIntMat(127), 1, 127, 1)
|| test_requantize(RandomIntMat(127), 1, 127, 0)
|| test_requantize(RandomIntMat(127), 127, 1, 127)
|| test_requantize(RandomIntMat(127), 127, 1, 1)
|| test_requantize(RandomIntMat(127), 127, 1, 0);
}
static int test_requantize_3()
{
return 0
|| test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 1, 24)
|| test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 1, 1)
|| test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 1, 0)
|| test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 24, 24)
|| test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 24, 1)
|| test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 24, 0)
|| test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 24, 24)
|| test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 24, 1)
|| test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 24, 0)
|| test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 1, 24)
|| test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 1, 1)
|| test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 1, 0)
|| test_requantize_pack8(RandomIntMat(15, 24), 1, 1, 24)
|| test_requantize_pack8(RandomIntMat(15, 24), 1, 1, 1)
|| test_requantize_pack8(RandomIntMat(15, 24), 1, 1, 0)
|| test_requantize_pack8(RandomIntMat(15, 24), 24, 24, 24)
|| test_requantize_pack8(RandomIntMat(15, 24), 24, 24, 1)
|| test_requantize_pack8(RandomIntMat(15, 24), 24, 24, 0)
|| test_requantize_pack8(RandomIntMat(15, 24), 1, 24, 24)
|| test_requantize_pack8(RandomIntMat(15, 24), 1, 24, 1)
|| test_requantize_pack8(RandomIntMat(15, 24), 1, 24, 0)
|| test_requantize_pack8(RandomIntMat(15, 24), 24, 1, 24)
|| test_requantize_pack8(RandomIntMat(15, 24), 24, 1, 1)
|| test_requantize_pack8(RandomIntMat(15, 24), 24, 1, 0)
|| test_requantize_pack8(RandomIntMat(128), 1, 1, 128)
|| test_requantize_pack8(RandomIntMat(128), 1, 1, 1)
|| test_requantize_pack8(RandomIntMat(128), 1, 1, 0)
|| test_requantize_pack8(RandomIntMat(128), 128, 128, 128)
|| test_requantize_pack8(RandomIntMat(128), 128, 128, 1)
|| test_requantize_pack8(RandomIntMat(128), 128, 128, 0)
|| test_requantize_pack8(RandomIntMat(128), 1, 128, 128)
|| test_requantize_pack8(RandomIntMat(128), 1, 128, 1)
|| test_requantize_pack8(RandomIntMat(128), 1, 128, 0)
|| test_requantize_pack8(RandomIntMat(128), 128, 1, 128)
|| test_requantize_pack8(RandomIntMat(128), 128, 1, 1)
|| test_requantize_pack8(RandomIntMat(128), 128, 1, 0);
}
int main()
{
SRAND(7767517);
return 0
|| test_requantize_0()
|| test_requantize_1()
|| test_requantize_2()
|| test_requantize_3();
}

374
3rdparty/ncnn/tests/test_reshape.cpp vendored Normal file
View File

@ -0,0 +1,374 @@
// 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/reshape.h"
#include "testutil.h"
static int test_reshape(const ncnn::Mat& a, int outw, int outh, int outd, int outc)
{
ncnn::ParamDict pd;
pd.set(0, outw); // w
pd.set(1, outh); // h
pd.set(11, outd); // d
pd.set(2, outc); // c
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Reshape>("Reshape", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_reshape failed a.dims=%d a=(%d %d %d %d) outw=%d outh=%d outd=%d outc=%d\n", a.dims, a.w, a.h, a.d, a.c, outw, outh, outd, outc);
}
return ret;
}
static int test_reshape_permute(const ncnn::Mat& a, int outw, int outh, int outd, int outc)
{
ncnn::ParamDict pd;
pd.set(0, outw); // w
pd.set(1, outh); // h
pd.set(11, outd); // d
pd.set(2, outc); // c
pd.set(3, 1); // permute
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Reshape>("Reshape", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_reshape_permute failed a.dims=%d a=(%d %d %d %d) outw=%d outh=%d outd=%d outc=%d\n", a.dims, a.w, a.h, a.d, a.c, outw, outh, outd, outc);
}
return ret;
}
static int test_reshape_0()
{
ncnn::Mat a = RandomMat(3, 8, 5, 32);
return 0
|| test_reshape(a, 5, 8, 3, 32)
|| test_reshape(a, 3, 8, 32, 5)
|| test_reshape(a, 32, 5, 8, 3)
|| test_reshape(a, 4, 3, 16, -1)
|| test_reshape(a, 6, 16, -1, 2)
|| test_reshape(a, 4, -1, 8, 8)
|| test_reshape(a, -1, 16, 6, 2)
|| test_reshape(a, 16, 6, -233, -1)
|| test_reshape(a, 8, -1, -233, 8)
|| test_reshape(a, -1, 6, -233, 16)
|| test_reshape(a, 8, -1, -233, -233)
|| test_reshape(a, -1, 6, -233, -233)
|| test_reshape(a, -1, -233, -233, -233);
}
static int test_reshape_1()
{
ncnn::Mat a = RandomMat(4, 5, 12, 26);
return 0
|| test_reshape(a, 12, 5, 4, 26)
|| test_reshape(a, 4, 26, 12, 5)
|| test_reshape(a, 13, 5, 12, 8)
|| test_reshape(a, 2, 10, 8, -1)
|| test_reshape(a, 13, 4, -1, 10)
|| test_reshape(a, 13, -1, 12, 4)
|| test_reshape(a, -1, 26, 3, 16)
|| test_reshape(a, 13, 4, -233, -1)
|| test_reshape(a, 26, -1, -233, 12)
|| test_reshape(a, -1, 13, -233, 16)
|| test_reshape(a, 12, -1, -233, -233)
|| test_reshape(a, -1, 24, -233, -233)
|| test_reshape(a, -1, -233, -233, -233);
}
static int test_reshape_2()
{
ncnn::Mat a = RandomMat(6, 7, 32);
return 0
|| test_reshape(a, 6, 8, 4, 7)
|| test_reshape(a, 2, 6, 7, 16)
|| test_reshape(a, 7, 6, -233, 32)
|| test_reshape(a, 6, 32, -233, 7)
|| test_reshape(a, 32, 7, -233, 6)
|| test_reshape(a, 2, 6, -233, -1)
|| test_reshape(a, -1, 8, -233, 2)
|| test_reshape(a, -1, 4, -233, -233)
|| test_reshape(a, 8, -1, -233, -233)
|| test_reshape(a, 32, 42, -233, -233)
|| test_reshape(a, -1, -233, -233, -233);
}
static int test_reshape_3()
{
ncnn::Mat a = RandomMat(4, 14, 26);
return 0
|| test_reshape(a, 13, 4, 2, 14)
|| test_reshape(a, 1, 13, 7, 16)
|| test_reshape(a, 14, 8, -233, 13)
|| test_reshape(a, 4, 13, -233, 28)
|| test_reshape(a, 13, 14, -233, 8)
|| test_reshape(a, 2, 7, -233, -1)
|| test_reshape(a, -1, 13, -233, 2)
|| test_reshape(a, -1, 4, -233, -233)
|| test_reshape(a, 8, -1, -233, -233)
|| test_reshape(a, 16, 91, -233, -233)
|| test_reshape(a, -1, -233, -233, -233);
}
static int test_reshape_4()
{
ncnn::Mat a = RandomMat(14, 32);
return 0
|| test_reshape(a, 2, 7, 2, 16)
|| test_reshape(a, 16, 1, 7, 4)
|| test_reshape(a, 7, 2, -233, 32)
|| test_reshape(a, 4, 16, -233, 7)
|| test_reshape(a, 16, 14, -233, 2)
|| test_reshape(a, 2, 4, -233, -1)
|| test_reshape(a, -1, 8, -233, 2)
|| test_reshape(a, 28, 16, -233, -233)
|| test_reshape(a, -1, 14, -233, -233)
|| test_reshape(a, 16, -1, -233, -233)
|| test_reshape(a, -1, -233, -233, -233);
}
static int test_reshape_5()
{
ncnn::Mat a = RandomMat(12, 28);
return 0
|| test_reshape(a, 4, 3, 2, 14)
|| test_reshape(a, 1, 3, 14, 8)
|| test_reshape(a, 7, 2, -233, 24)
|| test_reshape(a, 2, 24, -233, 7)
|| test_reshape(a, 12, 14, -233, 2)
|| test_reshape(a, 2, 4, -233, -1)
|| test_reshape(a, -1, 4, -233, 2)
|| test_reshape(a, 21, 16, -233, -233)
|| test_reshape(a, -1, 14, -233, -233)
|| test_reshape(a, 3, -1, -233, -233)
|| test_reshape(a, -1, -233, -233, -233);
}
static int test_reshape_6()
{
ncnn::Mat a = RandomMat(240);
return 0
|| test_reshape(a, 1, 1, 1, 240)
|| test_reshape(a, 10, 1, 1, 24)
|| test_reshape(a, 3, 5, -233, 16)
|| test_reshape(a, 3, 8, -233, 10)
|| test_reshape(a, 8, 5, -233, 6)
|| test_reshape(a, 2, 5, -233, -1)
|| test_reshape(a, -1, 5, -233, 4)
|| test_reshape(a, 8, 30, -233, -233)
|| test_reshape(a, -1, 2, -233, -233)
|| test_reshape(a, 24, -1, -233, -233)
|| test_reshape(a, -1, -233, -233, -233);
}
static int test_reshape_7()
{
ncnn::Mat a = RandomMat(210);
return 0
|| test_reshape(a, 1, 1, 210, 1)
|| test_reshape(a, 5, 2, 7, 3)
|| test_reshape(a, 3, 5, -233, 14)
|| test_reshape(a, 3, 14, -233, 5)
|| test_reshape(a, 14, 5, -233, 3)
|| test_reshape(a, 2, 5, -233, -1)
|| test_reshape(a, -1, 5, -233, 2)
|| test_reshape(a, 6, 35, -233, -233)
|| test_reshape(a, -1, 7, -233, -233)
|| test_reshape(a, 21, -1, -233, -233)
|| test_reshape(a, -1, -233, -233, -233);
}
static int test_reshape_8()
{
ncnn::Mat a = RandomMat(3, 8, 5, 32);
return 0
|| test_reshape_permute(a, 5, 8, 3, 32)
|| test_reshape_permute(a, 3, 8, 32, 5)
|| test_reshape_permute(a, 32, 5, 8, 3)
|| test_reshape_permute(a, 4, 3, 16, -1)
|| test_reshape_permute(a, 6, 16, -1, 2)
|| test_reshape_permute(a, 4, -1, 8, 8)
|| test_reshape_permute(a, -1, 16, 6, 2)
|| test_reshape_permute(a, 16, 6, -233, -1)
|| test_reshape_permute(a, 8, -1, -233, 8)
|| test_reshape_permute(a, -1, 6, -233, 16)
|| test_reshape_permute(a, 8, -1, -233, -233)
|| test_reshape_permute(a, -1, 6, -233, -233)
|| test_reshape_permute(a, -1, -233, -233, -233);
}
static int test_reshape_9()
{
ncnn::Mat a = RandomMat(4, 5, 12, 26);
return 0
|| test_reshape_permute(a, 12, 5, 4, 26)
|| test_reshape_permute(a, 4, 26, 12, 5)
|| test_reshape_permute(a, 13, 5, 12, 8)
|| test_reshape_permute(a, 2, 10, 8, -1)
|| test_reshape_permute(a, 13, 4, -1, 10)
|| test_reshape_permute(a, 13, -1, 12, 4)
|| test_reshape_permute(a, -1, 26, 3, 16)
|| test_reshape_permute(a, 13, 4, -233, -1)
|| test_reshape_permute(a, 26, -1, -233, 12)
|| test_reshape_permute(a, -1, 13, -233, 16)
|| test_reshape_permute(a, 12, -1, -233, -233)
|| test_reshape_permute(a, -1, 24, -233, -233)
|| test_reshape_permute(a, -1, -233, -233, -233);
}
static int test_reshape_10()
{
ncnn::Mat a = RandomMat(6, 7, 32);
return 0
|| test_reshape_permute(a, 6, 8, 4, 7)
|| test_reshape_permute(a, 2, 6, 7, 16)
|| test_reshape_permute(a, 7, 6, -233, 32)
|| test_reshape_permute(a, 6, 32, -233, 7)
|| test_reshape_permute(a, 32, 7, -233, 6)
|| test_reshape_permute(a, 2, 6, -233, -1)
|| test_reshape_permute(a, -1, 8, -233, 2)
|| test_reshape_permute(a, -1, 4, -233, -233)
|| test_reshape_permute(a, 8, -1, -233, -233)
|| test_reshape_permute(a, 32, 42, -233, -233)
|| test_reshape_permute(a, -1, -233, -233, -233);
}
static int test_reshape_11()
{
ncnn::Mat a = RandomMat(4, 14, 26);
return 0
|| test_reshape_permute(a, 13, 4, 2, 14)
|| test_reshape_permute(a, 1, 13, 7, 16)
|| test_reshape_permute(a, 14, 8, -233, 13)
|| test_reshape_permute(a, 4, 13, -233, 28)
|| test_reshape_permute(a, 13, 14, -233, 8)
|| test_reshape_permute(a, 2, 7, -233, -1)
|| test_reshape_permute(a, -1, 13, -233, 2)
|| test_reshape_permute(a, -1, 4, -233, -233)
|| test_reshape_permute(a, 8, -1, -233, -233)
|| test_reshape_permute(a, 16, 91, -233, -233)
|| test_reshape_permute(a, -1, -233, -233, -233);
}
static int test_reshape_12()
{
ncnn::Mat a = RandomMat(14, 32);
return 0
|| test_reshape_permute(a, 2, 7, 2, 16)
|| test_reshape_permute(a, 16, 1, 7, 4)
|| test_reshape_permute(a, 7, 2, -233, 32)
|| test_reshape_permute(a, 4, 16, -233, 7)
|| test_reshape_permute(a, 16, 14, -233, 2)
|| test_reshape_permute(a, 2, 4, -233, -1)
|| test_reshape_permute(a, -1, 8, -233, 2)
|| test_reshape_permute(a, 28, 16, -233, -233)
|| test_reshape_permute(a, -1, 14, -233, -233)
|| test_reshape_permute(a, 16, -1, -233, -233)
|| test_reshape_permute(a, -1, -233, -233, -233);
}
static int test_reshape_13()
{
ncnn::Mat a = RandomMat(12, 28);
return 0
|| test_reshape_permute(a, 4, 3, 2, 14)
|| test_reshape_permute(a, 1, 3, 14, 8)
|| test_reshape_permute(a, 7, 2, -233, 24)
|| test_reshape_permute(a, 2, 24, -233, 7)
|| test_reshape_permute(a, 12, 14, -233, 2)
|| test_reshape_permute(a, 2, 4, -233, -1)
|| test_reshape_permute(a, -1, 4, -233, 2)
|| test_reshape_permute(a, 21, 16, -233, -233)
|| test_reshape_permute(a, -1, 14, -233, -233)
|| test_reshape_permute(a, 3, -1, -233, -233)
|| test_reshape_permute(a, -1, -233, -233, -233);
}
static int test_reshape_14()
{
ncnn::Mat a = RandomMat(240);
return 0
|| test_reshape_permute(a, 1, 1, 1, 240)
|| test_reshape_permute(a, 10, 1, 1, 24)
|| test_reshape_permute(a, 3, 5, -233, 16)
|| test_reshape_permute(a, 3, 8, -233, 10)
|| test_reshape_permute(a, 8, 5, -233, 6)
|| test_reshape_permute(a, 2, 5, -233, -1)
|| test_reshape_permute(a, -1, 5, -233, 4)
|| test_reshape_permute(a, 8, 30, -233, -233)
|| test_reshape_permute(a, -1, 2, -233, -233)
|| test_reshape_permute(a, 24, -1, -233, -233)
|| test_reshape_permute(a, -1, -233, -233, -233);
}
static int test_reshape_15()
{
ncnn::Mat a = RandomMat(210);
return 0
|| test_reshape_permute(a, 1, 1, 210, 1)
|| test_reshape_permute(a, 5, 2, 7, 3)
|| test_reshape_permute(a, 3, 5, -233, 14)
|| test_reshape_permute(a, 3, 14, -233, 5)
|| test_reshape_permute(a, 14, 5, -233, 3)
|| test_reshape_permute(a, 2, 5, -233, -1)
|| test_reshape_permute(a, -1, 5, -233, 2)
|| test_reshape_permute(a, 6, 35, -233, -233)
|| test_reshape_permute(a, -1, 7, -233, -233)
|| test_reshape_permute(a, 21, -1, -233, -233)
|| test_reshape_permute(a, -1, -233, -233, -233);
}
int main()
{
SRAND(7767517);
return 0
|| test_reshape_0()
|| test_reshape_1()
|| test_reshape_2()
|| test_reshape_3()
|| test_reshape_4()
|| test_reshape_5()
|| test_reshape_6()
|| test_reshape_7()
|| test_reshape_8()
|| test_reshape_9()
|| test_reshape_10()
|| test_reshape_11()
|| test_reshape_12()
|| test_reshape_13()
|| test_reshape_14()
|| test_reshape_15();
}

256
3rdparty/ncnn/tests/test_rnn.cpp vendored Normal file
View File

@ -0,0 +1,256 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 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/rnn.h"
#include "testutil.h"
static int test_rnn(const ncnn::Mat& a, int outch, int direction)
{
int input_size = a.w;
int num_directions = direction == 2 ? 2 : 1;
ncnn::ParamDict pd;
pd.set(0, outch);
pd.set(1, outch * input_size * num_directions);
pd.set(2, direction);
std::vector<ncnn::Mat> weights(3);
weights[0] = RandomMat(outch * input_size * num_directions);
weights[1] = RandomMat(outch * num_directions);
weights[2] = RandomMat(outch * outch * num_directions);
int ret = test_layer<ncnn::RNN>("RNN", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_rnn failed a.dims=%d a=(%d %d %d) outch=%d, direction = %d \n", a.dims, a.w, a.h, a.c, outch, direction);
}
return ret;
}
int test_rnn_layer_with_hidden(const ncnn::Mat& a, int outch, int direction)
{
int input_size = a.w;
int num_directions = direction == 2 ? 2 : 1;
ncnn::ParamDict pd;
pd.set(0, outch);
pd.set(1, outch * input_size * num_directions);
pd.set(2, direction);
std::vector<ncnn::Mat> weights(3);
weights[0] = RandomMat(outch * input_size * num_directions);
weights[1] = RandomMat(outch * num_directions);
weights[2] = RandomMat(outch * outch * num_directions);
// initial hidden state
ncnn::Mat hidden = RandomMat(outch, num_directions);
std::vector<ncnn::Mat> as(2);
as[0] = a;
as[1] = hidden;
int ret = test_layer<ncnn::RNN>("RNN", pd, weights, as, 2);
if (ret != 0)
{
fprintf(stderr, "test_rnn_layer_with_hidden failed a.dims=%d a=(%d %d %d) outch=%d, direction = %d \n", a.dims, a.w, a.h, a.c, outch, direction);
}
return ret;
}
int test_rnn_layer_with_hidden_input(const ncnn::Mat& a, int outch, int direction)
{
int input_size = a.w;
int num_directions = direction == 2 ? 2 : 1;
ncnn::ParamDict pd;
pd.set(0, outch);
pd.set(1, outch * input_size * num_directions);
pd.set(2, direction);
std::vector<ncnn::Mat> weights(3);
weights[0] = RandomMat(outch * input_size * num_directions);
weights[1] = RandomMat(outch * num_directions);
weights[2] = RandomMat(outch * outch * num_directions);
// initial hidden state
ncnn::Mat hidden = RandomMat(outch, num_directions);
std::vector<ncnn::Mat> as(2);
as[0] = a;
as[1] = hidden;
int ret = test_layer<ncnn::RNN>("RNN", pd, weights, as, 1);
if (ret != 0)
{
fprintf(stderr, "test_rnn_layer_with_hidden_input failed a.dims=%d a=(%d %d %d) outch=%d, direction = %d \n", a.dims, a.w, a.h, a.c, outch, direction);
}
return ret;
}
int test_rnn_layer_with_hidden_output(const ncnn::Mat& a, int outch, int direction)
{
int input_size = a.w;
int num_directions = direction == 2 ? 2 : 1;
ncnn::ParamDict pd;
pd.set(0, outch);
pd.set(1, outch * input_size * num_directions);
pd.set(2, direction);
std::vector<ncnn::Mat> weights(3);
weights[0] = RandomMat(outch * input_size * num_directions);
weights[1] = RandomMat(outch * num_directions);
weights[2] = RandomMat(outch * outch * num_directions);
std::vector<ncnn::Mat> as(1);
as[0] = a;
int ret = test_layer<ncnn::RNN>("RNN", pd, weights, as, 2);
if (ret != 0)
{
fprintf(stderr, "test_rnn_layer_with_hidden_output failed a.dims=%d a=(%d %d %d) outch=%d, direction = %d \n", a.dims, a.w, a.h, a.c, outch, direction);
}
return ret;
}
static int test_rnn_0()
{
return 0
|| test_rnn(RandomMat(4, 1), 2, 2)
|| test_rnn(RandomMat(8, 2), 2, 2)
|| test_rnn(RandomMat(16, 8), 7, 2)
|| test_rnn(RandomMat(17, 8), 8, 2)
|| test_rnn(RandomMat(19, 15), 8, 2)
|| test_rnn(RandomMat(5, 16), 16, 2)
|| test_rnn(RandomMat(3, 16), 8, 2)
|| test_rnn(RandomMat(8, 16), 16, 2)
|| test_rnn(RandomMat(2, 5), 17, 2);
}
static int test_rnn_1()
{
return 0
|| test_rnn_layer_with_hidden(RandomMat(4, 4), 1, 2)
|| test_rnn_layer_with_hidden(RandomMat(8, 2), 2, 2)
|| test_rnn_layer_with_hidden(RandomMat(16, 8), 7, 2)
|| test_rnn_layer_with_hidden(RandomMat(17, 8), 8, 2)
|| test_rnn_layer_with_hidden(RandomMat(19, 15), 8, 2)
|| test_rnn_layer_with_hidden(RandomMat(5, 16), 16, 2)
|| test_rnn_layer_with_hidden(RandomMat(3, 16), 8, 2)
|| test_rnn_layer_with_hidden(RandomMat(2, 5), 99, 2)
|| test_rnn_layer_with_hidden(RandomMat(4, 4), 1, 1)
|| test_rnn_layer_with_hidden(RandomMat(8, 2), 2, 1)
|| test_rnn_layer_with_hidden(RandomMat(16, 8), 7, 1)
|| test_rnn_layer_with_hidden(RandomMat(17, 8), 8, 1)
|| test_rnn_layer_with_hidden(RandomMat(19, 15), 8, 1)
|| test_rnn_layer_with_hidden(RandomMat(5, 16), 16, 1)
|| test_rnn_layer_with_hidden(RandomMat(3, 16), 8, 1)
|| test_rnn_layer_with_hidden(RandomMat(2, 5), 99, 1)
|| test_rnn_layer_with_hidden(RandomMat(4, 2), 1, 0)
|| test_rnn_layer_with_hidden(RandomMat(8, 2), 2, 0)
|| test_rnn_layer_with_hidden(RandomMat(16, 8), 7, 0)
|| test_rnn_layer_with_hidden(RandomMat(17, 8), 8, 0)
|| test_rnn_layer_with_hidden(RandomMat(19, 15), 8, 0)
|| test_rnn_layer_with_hidden(RandomMat(5, 16), 16, 0)
|| test_rnn_layer_with_hidden(RandomMat(3, 16), 8, 0)
|| test_rnn_layer_with_hidden(RandomMat(2, 5), 17, 0)
|| test_rnn_layer_with_hidden_input(RandomMat(4, 4), 1, 2)
|| test_rnn_layer_with_hidden_input(RandomMat(8, 2), 2, 2)
|| test_rnn_layer_with_hidden_input(RandomMat(16, 8), 7, 2)
|| test_rnn_layer_with_hidden_input(RandomMat(17, 8), 8, 2)
|| test_rnn_layer_with_hidden_input(RandomMat(19, 15), 8, 2)
|| test_rnn_layer_with_hidden_input(RandomMat(5, 16), 16, 2)
|| test_rnn_layer_with_hidden_input(RandomMat(3, 16), 8, 2)
|| test_rnn_layer_with_hidden_input(RandomMat(2, 5), 99, 2)
|| test_rnn_layer_with_hidden_input(RandomMat(4, 4), 1, 1)
|| test_rnn_layer_with_hidden_input(RandomMat(8, 2), 2, 1)
|| test_rnn_layer_with_hidden_input(RandomMat(16, 8), 7, 1)
|| test_rnn_layer_with_hidden_input(RandomMat(17, 8), 8, 1)
|| test_rnn_layer_with_hidden_input(RandomMat(19, 15), 8, 1)
|| test_rnn_layer_with_hidden_input(RandomMat(5, 16), 16, 1)
|| test_rnn_layer_with_hidden_input(RandomMat(3, 16), 8, 1)
|| test_rnn_layer_with_hidden_input(RandomMat(2, 5), 99, 1)
|| test_rnn_layer_with_hidden_input(RandomMat(4, 2), 1, 0)
|| test_rnn_layer_with_hidden_input(RandomMat(8, 2), 2, 0)
|| test_rnn_layer_with_hidden_input(RandomMat(16, 8), 7, 0)
|| test_rnn_layer_with_hidden_input(RandomMat(17, 8), 8, 0)
|| test_rnn_layer_with_hidden_input(RandomMat(19, 15), 8, 0)
|| test_rnn_layer_with_hidden_input(RandomMat(5, 16), 16, 0)
|| test_rnn_layer_with_hidden_input(RandomMat(3, 16), 8, 0)
|| test_rnn_layer_with_hidden_input(RandomMat(2, 5), 17, 0)
|| test_rnn_layer_with_hidden_output(RandomMat(4, 4), 1, 2)
|| test_rnn_layer_with_hidden_output(RandomMat(8, 2), 2, 2)
|| test_rnn_layer_with_hidden_output(RandomMat(16, 8), 7, 2)
|| test_rnn_layer_with_hidden_output(RandomMat(17, 8), 8, 2)
|| test_rnn_layer_with_hidden_output(RandomMat(19, 15), 8, 2)
|| test_rnn_layer_with_hidden_output(RandomMat(5, 16), 16, 2)
|| test_rnn_layer_with_hidden_output(RandomMat(3, 16), 8, 2)
|| test_rnn_layer_with_hidden_output(RandomMat(2, 5), 99, 2)
|| test_rnn_layer_with_hidden_output(RandomMat(4, 4), 1, 1)
|| test_rnn_layer_with_hidden_output(RandomMat(8, 2), 2, 1)
|| test_rnn_layer_with_hidden_output(RandomMat(16, 8), 7, 1)
|| test_rnn_layer_with_hidden_output(RandomMat(17, 8), 8, 1)
|| test_rnn_layer_with_hidden_output(RandomMat(19, 15), 8, 1)
|| test_rnn_layer_with_hidden_output(RandomMat(5, 16), 16, 1)
|| test_rnn_layer_with_hidden_output(RandomMat(3, 16), 8, 1)
|| test_rnn_layer_with_hidden_output(RandomMat(2, 5), 99, 1)
|| test_rnn_layer_with_hidden_output(RandomMat(4, 2), 1, 0)
|| test_rnn_layer_with_hidden_output(RandomMat(8, 2), 2, 0)
|| test_rnn_layer_with_hidden_output(RandomMat(16, 8), 7, 0)
|| test_rnn_layer_with_hidden_output(RandomMat(17, 8), 8, 0)
|| test_rnn_layer_with_hidden_output(RandomMat(19, 15), 8, 0)
|| test_rnn_layer_with_hidden_output(RandomMat(5, 16), 16, 0)
|| test_rnn_layer_with_hidden_output(RandomMat(3, 16), 8, 0)
|| test_rnn_layer_with_hidden_output(RandomMat(2, 5), 17, 0);
}
static int test_rnn_2()
{
return 0
|| test_rnn(RandomMat(4, 1), 1, 0)
|| test_rnn(RandomMat(8, 2), 2, 0)
|| test_rnn(RandomMat(16, 8), 7, 0)
|| test_rnn(RandomMat(17, 8), 8, 0)
|| test_rnn(RandomMat(19, 15), 8, 0)
|| test_rnn(RandomMat(5, 16), 16, 0)
|| test_rnn(RandomMat(3, 16), 8, 0)
|| test_rnn(RandomMat(8, 16), 16, 0)
|| test_rnn(RandomMat(2, 5), 17, 0);
}
static int test_rnn_3()
{
return 0
|| test_rnn(RandomMat(4, 1), 1, 1)
|| test_rnn(RandomMat(8, 2), 2, 1)
|| test_rnn(RandomMat(16, 8), 7, 1)
|| test_rnn(RandomMat(17, 8), 8, 1)
|| test_rnn(RandomMat(19, 15), 8, 1)
|| test_rnn(RandomMat(5, 16), 16, 1)
|| test_rnn(RandomMat(3, 16), 8, 1)
|| test_rnn(RandomMat(8, 16), 16, 1)
|| test_rnn(RandomMat(2, 5), 17, 1);
}
int main()
{
SRAND(7767517);
return test_rnn_0() || test_rnn_1() || test_rnn_2() || test_rnn_3();
}

66
3rdparty/ncnn/tests/test_roialign.cpp vendored Normal file
View File

@ -0,0 +1,66 @@
// 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.h"
#include "layer/roialign.h"
#include "testutil.h"
static int test_roialign(int w, int h, int c, int pooled_width, int pooled_height, float spatial_scale, int sampling_ratio, bool aligned, int version)
{
std::vector<ncnn::Mat> a;
a.push_back(RandomMat(w, h, c));
ncnn::Mat b(4);
b[0] = RandomFloat(0.001, w - 2.001); //roi_x1
b[2] = RandomFloat(b[0] + 1.001, w - 1.001); //roi_x2
b[1] = RandomFloat(0.001, h - 2.001); //roi_y1
b[3] = RandomFloat(b[2] + 1.001, h - 1.001); //roi_y2
a.push_back(b);
ncnn::ParamDict pd;
pd.set(0, pooled_width); // pooled_width
pd.set(1, pooled_height); // pooled_height
pd.set(2, spatial_scale); // spatial_scale
pd.set(3, sampling_ratio); // sampling_ratio
pd.set(4, aligned); // aligned
pd.set(5, version); // version
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::ROIAlign>("ROIAlign", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_roialign failed base_w=%d base_h=%d base_c=%d pooled_width=%d pooled_height=%d spatial_scale=%4f.3\n", w, h, c, pooled_width, pooled_height, spatial_scale);
}
return ret;
}
static int test_roialign_0()
{
return 0
|| test_roialign(56, 56, 1, 28, 28, 0.50000, 0, 0, 0)
|| test_roialign(28, 28, 3, 14, 14, 0.25000, 1, 1, 1)
|| test_roialign(14, 14, 4, 7, 7, 0.12500, 2, 0, 1)
|| test_roialign(14, 14, 8, 7, 7, 0.06250, 3, 1, 0)
|| test_roialign(7, 7, 12, 3, 3, 0.03125, 4, 0, 0)
|| test_roialign(7, 7, 16, 3, 3, 0.03125, 4, 1, 1);
}
int main()
{
SRAND(7767517);
return 0
|| test_roialign_0();
}

67
3rdparty/ncnn/tests/test_roipooling.cpp vendored Normal file
View File

@ -0,0 +1,67 @@
// 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.h"
#include "layer/roipooling.h"
#include "testutil.h"
static int test_roipooling(int w, int h, int c, int pooled_width, int pooled_height, float spatial_scale)
{
std::vector<ncnn::Mat> a;
a.push_back(RandomMat(w, h, c));
ncnn::Mat b(4);
b[0] = RandomFloat(0.001, w - 2.001); //roi_x1
b[2] = RandomFloat(b[0] + 1.001, w - 1.001); //roi_x2
b[1] = RandomFloat(0.001, h - 2.001); //roi_y1
b[3] = RandomFloat(b[2] + 1.001, h - 1.001); //roi_y2
a.push_back(b);
ncnn::ParamDict pd;
pd.set(0, pooled_width); // pooled_width
pd.set(1, pooled_height); // pooled_height
pd.set(2, spatial_scale); // spatial_scale
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::ROIPooling>("ROIPooling", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_roipooling failed base_w=%d base_h=%d base_c=%d pooled_width=%d pooled_height=%d spatial_scale=%4f.3\n", w, h, c, pooled_width, pooled_height, spatial_scale);
}
return ret;
}
static int test_roipooling_0()
{
int ret = 0
|| test_roipooling(112, 112, 16, 56, 56, 0.50000)
|| test_roipooling(56, 56, 32, 28, 28, 0.25000)
|| test_roipooling(28, 28, 64, 14, 14, 0.12500)
|| test_roipooling(14, 14, 128, 27, 17, 0.06250)
|| test_roipooling(7, 7, 256, 3, 3, 0.03125);
if (ret != 0)
return -1;
return 0;
}
int main()
{
SRAND(7767517);
return 0
|| test_roipooling_0();
}

136
3rdparty/ncnn/tests/test_scale.cpp vendored Normal file
View File

@ -0,0 +1,136 @@
// 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/scale.h"
#include "testutil.h"
static int test_scale(const ncnn::Mat& a, int bias)
{
int scale_data_size;
if (a.dims == 1) scale_data_size = a.w;
if (a.dims == 2) scale_data_size = a.h;
if (a.dims == 3) scale_data_size = a.c;
ncnn::ParamDict pd;
pd.set(0, scale_data_size);
pd.set(1, bias);
std::vector<ncnn::Mat> weights(bias ? 2 : 1);
weights[0] = RandomMat(scale_data_size);
if (bias)
weights[1] = RandomMat(scale_data_size);
int ret = test_layer<ncnn::Scale>("Scale", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_scale failed a.dims=%d a=(%d %d %d) bias=%d\n", a.dims, a.w, a.h, a.c, bias);
}
return ret;
}
static int test_scale_attention(const ncnn::Mat& a)
{
int scale_data_size;
if (a.dims == 1) scale_data_size = a.w;
if (a.dims == 2) scale_data_size = a.h;
if (a.dims == 3) scale_data_size = a.c;
ncnn::ParamDict pd;
pd.set(0, -233);
std::vector<ncnn::Mat> weights(0);
std::vector<ncnn::Mat> ab(2);
ab[0] = a;
ab[1] = RandomMat(scale_data_size);
int ret = test_layer<ncnn::Scale>("Scale", pd, weights, ab, 2);
if (ret != 0)
{
fprintf(stderr, "test_scale_attention failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
}
return ret;
}
static int test_scale_0()
{
return 0
|| test_scale(RandomMat(5, 7, 24), 0)
|| test_scale(RandomMat(5, 7, 24), 1)
|| test_scale(RandomMat(7, 9, 12), 0)
|| test_scale(RandomMat(7, 9, 12), 1)
|| test_scale(RandomMat(3, 5, 13), 0)
|| test_scale(RandomMat(3, 5, 13), 1);
}
static int test_scale_1()
{
return 0
|| test_scale(RandomMat(15, 24), 0)
|| test_scale(RandomMat(15, 24), 1)
|| test_scale(RandomMat(17, 12), 0)
|| test_scale(RandomMat(17, 12), 1)
|| test_scale(RandomMat(19, 15), 0)
|| test_scale(RandomMat(19, 15), 1);
}
static int test_scale_2()
{
return 0
|| test_scale(RandomMat(128), 0)
|| test_scale(RandomMat(128), 1)
|| test_scale(RandomMat(124), 0)
|| test_scale(RandomMat(124), 1)
|| test_scale(RandomMat(127), 0)
|| test_scale(RandomMat(127), 1);
}
static int test_scale_3()
{
return 0
|| test_scale_attention(RandomMat(5, 7, 24))
|| test_scale_attention(RandomMat(7, 9, 12))
|| test_scale_attention(RandomMat(3, 5, 13));
}
static int test_scale_4()
{
return 0
|| test_scale_attention(RandomMat(15, 24))
|| test_scale_attention(RandomMat(17, 12))
|| test_scale_attention(RandomMat(19, 15));
}
static int test_scale_5()
{
return 0
|| test_scale_attention(RandomMat(128))
|| test_scale_attention(RandomMat(124))
|| test_scale_attention(RandomMat(127));
}
int main()
{
SRAND(7767517);
return 0
|| test_scale_0()
|| test_scale_1()
|| test_scale_2()
|| test_scale_3()
|| test_scale_4()
|| test_scale_5();
}

67
3rdparty/ncnn/tests/test_selu.cpp vendored Normal file
View File

@ -0,0 +1,67 @@
// Xavier Hsinyuan is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 Xavier Hsinyuan <me@lstlx.com>. 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/selu.h"
#include "testutil.h"
static int test_selu(const ncnn::Mat& a, float alpha, float lambda)
{
ncnn::ParamDict pd;
pd.set(0, alpha);
pd.set(1, lambda);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::SELU>("SELU", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_selu failed a.dims=%d a=(%d %d %d) alpha=%f lambda=%f\n", a.dims, a.w, a.h, a.c, alpha, lambda);
}
return ret;
}
static int test_selu_0()
{
return 0
|| test_selu(RandomMat(5, 7, 24), 1.673264f, 1.050700f)
|| test_selu(RandomMat(7, 9, 12), 1.673264f, 1.050700f)
|| test_selu(RandomMat(3, 5, 13), 1.673264f, 1.050700f);
}
static int test_selu_1()
{
return 0
|| test_selu(RandomMat(15, 24), 1.673264f, 1.050700f)
|| test_selu(RandomMat(17, 12), 1.673264f, 1.050700f)
|| test_selu(RandomMat(19, 15), 1.673264f, 1.050700f);
}
static int test_selu_2()
{
return 0
|| test_selu(RandomMat(128), 1.673264f, 1.050700f)
|| test_selu(RandomMat(124), 1.673264f, 1.050700f)
|| test_selu(RandomMat(127), 1.673264f, 1.050700f);
}
int main()
{
SRAND(7767517);
return 0
|| test_selu_0()
|| test_selu_1()
|| test_selu_2();
}

View File

@ -0,0 +1,88 @@
// 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/shufflechannel.h"
#include "testutil.h"
static int test_shufflechannel(int w, int h, int c, int group, int reverse)
{
ncnn::Mat a = RandomMat(w, h, c);
ncnn::ParamDict pd;
pd.set(0, group);
pd.set(1, reverse);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::ShuffleChannel>("ShuffleChannel", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_shufflechannel failed w=%d h=%d c=%d group=%d reverse=%d\n", w, h, c, group, reverse);
}
return ret;
}
static int test_shufflechannel_0()
{
return 0
|| test_shufflechannel(3, 7, 1, 1, 0)
|| test_shufflechannel(5, 7, 2, 2, 0)
|| test_shufflechannel(3, 9, 3, 3, 0)
|| test_shufflechannel(5, 7, 4, 2, 0)
|| test_shufflechannel(3, 7, 12, 3, 0)
|| test_shufflechannel(5, 9, 12, 4, 0)
|| test_shufflechannel(3, 7, 12, 6, 0)
|| test_shufflechannel(5, 7, 15, 3, 0)
|| test_shufflechannel(3, 9, 15, 5, 0)
|| test_shufflechannel(5, 7, 16, 2, 0)
|| test_shufflechannel(5, 9, 16, 4, 0)
|| test_shufflechannel(3, 7, 16, 8, 0)
|| test_shufflechannel(5, 7, 20, 2, 0)
|| test_shufflechannel(5, 7, 24, 2, 0)
|| test_shufflechannel(3, 7, 24, 3, 0)
|| test_shufflechannel(5, 9, 24, 4, 0)
|| test_shufflechannel(3, 7, 32, 8, 0)
|| test_shufflechannel(5, 9, 64, 4, 0);
}
static int test_shufflechannel_1()
{
return 0
|| test_shufflechannel(3, 7, 1, 1, 1)
|| test_shufflechannel(5, 7, 2, 2, 1)
|| test_shufflechannel(3, 9, 3, 3, 1)
|| test_shufflechannel(5, 7, 4, 2, 1)
|| test_shufflechannel(3, 7, 12, 3, 1)
|| test_shufflechannel(5, 9, 12, 4, 1)
|| test_shufflechannel(3, 7, 12, 6, 1)
|| test_shufflechannel(5, 7, 15, 3, 1)
|| test_shufflechannel(3, 9, 15, 5, 1)
|| test_shufflechannel(5, 7, 16, 2, 1)
|| test_shufflechannel(5, 9, 16, 4, 1)
|| test_shufflechannel(3, 7, 16, 8, 1)
|| test_shufflechannel(5, 7, 20, 2, 1)
|| test_shufflechannel(5, 7, 24, 2, 1)
|| test_shufflechannel(3, 7, 24, 3, 1)
|| test_shufflechannel(5, 9, 24, 4, 1)
|| test_shufflechannel(3, 7, 32, 8, 1)
|| test_shufflechannel(3, 7, 64, 4, 1);
}
int main()
{
SRAND(7767517);
return test_shufflechannel_0() || test_shufflechannel_1();
}

65
3rdparty/ncnn/tests/test_sigmoid.cpp vendored Normal file
View File

@ -0,0 +1,65 @@
// 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/sigmoid.h"
#include "testutil.h"
static int test_sigmoid(const ncnn::Mat& a)
{
ncnn::ParamDict pd;
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Sigmoid>("Sigmoid", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_sigmoid failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
}
return ret;
}
static int test_sigmoid_0()
{
return 0
|| test_sigmoid(RandomMat(5, 7, 24))
|| test_sigmoid(RandomMat(7, 9, 12))
|| test_sigmoid(RandomMat(3, 5, 13));
}
static int test_sigmoid_1()
{
return 0
|| test_sigmoid(RandomMat(15, 24))
|| test_sigmoid(RandomMat(17, 12))
|| test_sigmoid(RandomMat(19, 15));
}
static int test_sigmoid_2()
{
return 0
|| test_sigmoid(RandomMat(128))
|| test_sigmoid(RandomMat(124))
|| test_sigmoid(RandomMat(127));
}
int main()
{
SRAND(7767517);
return 0
|| test_sigmoid_0()
|| test_sigmoid_1()
|| test_sigmoid_2();
}

202
3rdparty/ncnn/tests/test_slice.cpp vendored Normal file
View File

@ -0,0 +1,202 @@
// 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/slice.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 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_slice(const ncnn::Mat& a, const ncnn::Mat& slices, int axis)
{
ncnn::ParamDict pd;
pd.set(0, slices);
pd.set(1, axis);
std::vector<ncnn::Mat> weights(0);
std::vector<ncnn::Mat> a0(1);
a0[0] = a;
int ret = test_layer<ncnn::Slice>("Slice", pd, weights, a0, slices.w);
if (ret != 0)
{
fprintf(stderr, "test_slice failed a.dims=%d a=(%d %d %d)", a.dims, a.w, a.h, a.c);
fprintf(stderr, " slices=");
print_int_array(slices);
fprintf(stderr, " axis=%d\n", axis);
}
return ret;
}
static int test_slice_0()
{
ncnn::Mat a = RandomMat(48, 36, 48);
return 0
|| test_slice(a, IntArrayMat(-233, -233, -233), 0)
|| test_slice(a, IntArrayMat(-233, -233, -233), 1)
|| test_slice(a, IntArrayMat(-233, -233, -233), 2)
|| test_slice(a, IntArrayMat(-233, -233, -233), -1)
|| test_slice(a, IntArrayMat(-233, -233, -233), -2)
|| test_slice(a, IntArrayMat(-233, -233, -233), -3);
}
static int test_slice_1()
{
ncnn::Mat a = RandomMat(17, 13, 32);
ncnn::Mat b = RandomMat(16, 15, 40);
ncnn::Mat c = RandomMat(15, 14, 20);
return 0
|| test_slice(a, IntArrayMat(3, 16, -233), 0)
|| test_slice(a, IntArrayMat(3, 16, -233), -3)
|| test_slice(b, IntArrayMat(8, 32), 0)
|| test_slice(b, IntArrayMat(8, 32), -3)
|| test_slice(c, IntArrayMat(4, -233), 0)
|| test_slice(c, IntArrayMat(4, -233), -3);
}
static int test_slice_2()
{
ncnn::Mat a = RandomMat(17, 32, 11);
ncnn::Mat b = RandomMat(17, 32, 24);
ncnn::Mat c = RandomMat(17, 32, 48);
return 0
|| test_slice(a, IntArrayMat(3, 16, -233), 1)
|| test_slice(a, IntArrayMat(3, 16, -233), -2)
|| test_slice(b, IntArrayMat(8, 3, -233), 1)
|| test_slice(b, IntArrayMat(8, 3, -233), -2)
|| test_slice(c, IntArrayMat(3, 16, 5), 1)
|| test_slice(c, IntArrayMat(3, 16, 5), -2);
}
static int test_slice_3()
{
ncnn::Mat a = RandomMat(16, 17, 11);
ncnn::Mat b = RandomMat(16, 17, 24);
ncnn::Mat c = RandomMat(16, 17, 16);
return 0
|| test_slice(a, IntArrayMat(5, 4, 7), 2)
|| test_slice(a, IntArrayMat(5, 4, 7), -1)
|| test_slice(b, IntArrayMat(5, 4, 7), 2)
|| test_slice(b, IntArrayMat(5, 4, 7), -1)
|| test_slice(c, IntArrayMat(5, 4, 7), 2)
|| test_slice(c, IntArrayMat(5, 4, 7), -1);
}
static int test_slice_4()
{
ncnn::Mat a = RandomMat(37, 32);
ncnn::Mat b = RandomMat(36, 40);
ncnn::Mat c = RandomMat(35, 20);
ncnn::Mat d = RandomMat(15, 11);
ncnn::Mat e = RandomMat(32, 64);
ncnn::Mat f = RandomMat(32, 24);
return 0
|| test_slice(a, IntArrayMat(3, 16, 5), 0)
|| test_slice(a, IntArrayMat(3, 16, 5), -2)
|| test_slice(b, IntArrayMat(32, -233), 0)
|| test_slice(b, IntArrayMat(32, -233), -2)
|| test_slice(c, IntArrayMat(4, -233), 0)
|| test_slice(c, IntArrayMat(4, -233), -2)
|| test_slice(d, IntArrayMat(3, -233, -233), 1)
|| test_slice(d, IntArrayMat(3, -233, -233), -1)
|| test_slice(e, IntArrayMat(5, 8, -233), 1)
|| test_slice(e, IntArrayMat(5, 8, -233), -1)
|| test_slice(f, IntArrayMat(3, 16, 5), 1)
|| test_slice(f, IntArrayMat(3, 16, 5), -1);
}
static int test_slice_5()
{
ncnn::Mat a = RandomMat(32);
ncnn::Mat b = RandomMat(20);
ncnn::Mat c = RandomMat(24);
return 0
|| test_slice(a, IntArrayMat(3, 16, 5), 0)
|| test_slice(a, IntArrayMat(3, 16, 5), -1)
|| test_slice(b, IntArrayMat(4, -233), 0)
|| test_slice(b, IntArrayMat(4, -233), -1)
|| test_slice(c, IntArrayMat(4, 16, -233), 0)
|| test_slice(c, IntArrayMat(4, 16, -233), -1);
}
int main()
{
SRAND(7767517);
return 0
|| test_slice_0()
|| test_slice_1()
|| test_slice_2()
|| test_slice_3()
|| test_slice_4()
|| test_slice_5();
}

112
3rdparty/ncnn/tests/test_softmax.cpp vendored Normal file
View File

@ -0,0 +1,112 @@
// 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/softmax.h"
#include "testutil.h"
static int test_softmax(const ncnn::Mat& a, int axis)
{
ncnn::ParamDict pd;
pd.set(0, axis); // axis
pd.set(1, 1); // fixbug0
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Softmax>("Softmax", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_softmax failed a.dims=%d a=(%d %d %d) axis=%d\n", a.dims, a.w, a.h, a.c, axis);
}
return ret;
}
static int test_softmax_0()
{
ncnn::Mat a = RandomMat(25, 27, 32);
ncnn::Mat b = RandomMat(27, 29, 28);
ncnn::Mat c = RandomMat(23, 25, 27);
return 0
|| test_softmax(a, 0)
|| test_softmax(a, 1)
|| test_softmax(a, 2)
|| test_softmax(a, -1)
|| test_softmax(a, -2)
|| test_softmax(a, -3)
|| test_softmax(b, 0)
|| test_softmax(b, 1)
|| test_softmax(b, 2)
|| test_softmax(b, -1)
|| test_softmax(b, -2)
|| test_softmax(b, -3)
|| test_softmax(c, 0)
|| test_softmax(c, 1)
|| test_softmax(c, 2)
|| test_softmax(c, -1)
|| test_softmax(c, -2)
|| test_softmax(c, -3);
}
static int test_softmax_1()
{
ncnn::Mat a = RandomMat(25, 32);
ncnn::Mat b = RandomMat(27, 28);
ncnn::Mat c = RandomMat(29, 27);
return 0
|| test_softmax(a, 0)
|| test_softmax(a, 1)
|| test_softmax(a, -1)
|| test_softmax(a, -2)
|| test_softmax(b, 0)
|| test_softmax(b, 1)
|| test_softmax(b, -1)
|| test_softmax(b, -2)
|| test_softmax(c, 0)
|| test_softmax(c, 1)
|| test_softmax(c, -1)
|| test_softmax(c, -2);
}
static int test_softmax_2()
{
ncnn::Mat a = RandomMat(128);
ncnn::Mat b = RandomMat(124);
ncnn::Mat c = RandomMat(127);
return 0
|| test_softmax(a, 0)
|| test_softmax(a, -1)
|| test_softmax(b, 0)
|| test_softmax(b, -1)
|| test_softmax(c, 0)
|| test_softmax(c, -1);
}
int main()
{
SRAND(7767517);
return 0
|| test_softmax_0()
|| test_softmax_1()
|| test_softmax_2();
}

65
3rdparty/ncnn/tests/test_softplus.cpp vendored Normal file
View File

@ -0,0 +1,65 @@
// 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/softplus.h"
#include "testutil.h"
static int test_softplus(const ncnn::Mat& a)
{
ncnn::ParamDict pd;
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Softplus>("Softplus", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_softplus failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
}
return ret;
}
static int test_softplus_0()
{
return 0
|| test_softplus(RandomMat(5, 7, 24))
|| test_softplus(RandomMat(7, 9, 12))
|| test_softplus(RandomMat(3, 5, 13));
}
static int test_softplus_1()
{
return 0
|| test_softplus(RandomMat(15, 24))
|| test_softplus(RandomMat(17, 12))
|| test_softplus(RandomMat(19, 15));
}
static int test_softplus_2()
{
return 0
|| test_softplus(RandomMat(128))
|| test_softplus(RandomMat(124))
|| test_softplus(RandomMat(127));
}
int main()
{
SRAND(7767517);
return 0
|| test_softplus_0()
|| test_softplus_1()
|| test_softplus_2();
}

143
3rdparty/ncnn/tests/test_squeeze.cpp vendored Normal file
View File

@ -0,0 +1,143 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 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/squeeze.h"
#include "testutil.h"
static int test_squeeze(const ncnn::Mat& a, int squeeze_w, int squeeze_h, int squeeze_c)
{
ncnn::ParamDict pd;
pd.set(0, squeeze_w);
pd.set(1, squeeze_h);
pd.set(2, squeeze_c);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Squeeze>("Squeeze", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_squeeze failed a.dims=%d a=(%d %d %d) squeeze_w=%d squeeze_h=%d squeeze_c=%d\n", a.dims, a.w, a.h, a.c, squeeze_w, squeeze_h, squeeze_c);
}
return ret;
}
static ncnn::Mat IntArrayMat(int a0)
{
ncnn::Mat m(1);
int* p = m;
p[0] = a0;
return m;
}
static ncnn::Mat IntArrayMat(int a0, int a1)
{
ncnn::Mat m(2);
int* p = m;
p[0] = a0;
p[1] = a1;
return m;
}
static ncnn::Mat IntArrayMat(int a0, int a1, int a2)
{
ncnn::Mat m(3);
int* p = m;
p[0] = a0;
p[1] = a1;
p[2] = a2;
return m;
}
static void print_int_array(const ncnn::Mat& a)
{
const int* pa = a;
fprintf(stderr, "[");
for (int i = 0; i < a.w; i++)
{
fprintf(stderr, " %d", pa[i]);
}
fprintf(stderr, " ]");
}
static int test_squeeze_axes(const ncnn::Mat& a, const ncnn::Mat& axes)
{
ncnn::ParamDict pd;
pd.set(3, axes);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Squeeze>("Squeeze", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_squeeze_axes failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
fprintf(stderr, " axes=");
print_int_array(axes);
fprintf(stderr, "\n");
}
return ret;
}
static int test_squeeze_0()
{
ncnn::Mat as[12];
as[0] = RandomMat(3, 12, 16);
as[1] = RandomMat(3, 1, 16);
as[2] = RandomMat(1, 33, 15);
as[3] = RandomMat(1, 14, 1);
as[4] = RandomMat(12, 13, 1);
as[5] = RandomMat(1, 1, 1);
as[6] = RandomMat(14, 16);
as[7] = RandomMat(1, 14);
as[8] = RandomMat(11, 1);
as[9] = RandomMat(1, 1);
as[10] = RandomMat(120);
as[11] = RandomMat(1);
for (int i = 0; i < 12; i++)
{
const ncnn::Mat& a = as[i];
int ret = 0
|| test_squeeze(a, 0, 0, 0)
|| test_squeeze(a, 0, 0, 1)
|| test_squeeze(a, 0, 1, 0)
|| test_squeeze(a, 0, 1, 1)
|| test_squeeze(a, 1, 0, 0)
|| test_squeeze(a, 1, 0, 1)
|| test_squeeze(a, 1, 1, 0)
|| test_squeeze(a, 1, 1, 1)
|| test_squeeze_axes(a, IntArrayMat(0))
|| test_squeeze_axes(a, IntArrayMat(1))
|| test_squeeze_axes(a, IntArrayMat(2))
|| test_squeeze_axes(a, IntArrayMat(0, 1))
|| test_squeeze_axes(a, IntArrayMat(0, 2))
|| test_squeeze_axes(a, IntArrayMat(1, 2))
|| test_squeeze_axes(a, IntArrayMat(0, 1, 2));
if (ret != 0)
return ret;
}
return 0;
}
int main()
{
SRAND(7767517);
return test_squeeze_0();
}

328
3rdparty/ncnn/tests/test_squeezenet.cpp vendored Normal file
View File

@ -0,0 +1,328 @@
// 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 "platform.h"
#include "net.h"
#include "testutil.h"
#include <stdio.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
static ncnn::Mat generate_ncnn_logo(int pixel_type_to, int w, int h)
{
// clang-format off
// *INDENT-OFF*
static const unsigned char ncnn_logo_data[16][16] =
{
{245, 245, 33, 245, 245, 245, 245, 245, 245, 245, 245, 245, 245, 33, 245, 245},
{245, 33, 33, 33, 245, 245, 245, 245, 245, 245, 245, 245, 33, 33, 33, 245},
{245, 33, 158, 158, 33, 245, 245, 245, 245, 245, 245, 33, 158, 158, 33, 245},
{ 33, 117, 158, 224, 158, 33, 245, 245, 245, 245, 33, 158, 224, 158, 117, 33},
{ 33, 117, 224, 224, 224, 66, 33, 33, 33, 33, 66, 224, 224, 224, 117, 33},
{ 33, 189, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 189, 33},
{ 33, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 33},
{ 33, 224, 224, 97, 97, 97, 97, 224, 224, 97, 97, 97, 97, 224, 224, 33},
{ 33, 224, 224, 97, 33, 0, 189, 224, 224, 97, 0, 33, 97, 224, 224, 33},
{ 33, 224, 224, 97, 33, 0, 189, 224, 224, 97, 0, 33, 97, 224, 224, 33},
{ 33, 224, 224, 97, 97, 97, 97, 224, 224, 97, 189, 189, 97, 224, 224, 33},
{ 33, 66, 66, 66, 224, 224, 224, 224, 224, 224, 224, 224, 66, 66, 66, 33},
{ 66, 158, 158, 66, 66, 224, 224, 224, 224, 224, 224, 66, 158, 158, 66, 66},
{ 66, 158, 158, 208, 66, 224, 224, 224, 224, 224, 224, 66, 158, 158, 208, 66},
{ 66, 224, 202, 158, 66, 224, 224, 224, 224, 224, 224, 66, 224, 202, 158, 66},
{ 66, 158, 224, 158, 66, 224, 224, 224, 224, 224, 224, 66, 158, 224, 158, 66}
};
// *INDENT-ON*
// clang-format on
const unsigned char* p_ncnn_logo_data = (const unsigned char*)ncnn_logo_data;
ncnn::Mat logo = ncnn::Mat::from_pixels(p_ncnn_logo_data, ncnn::Mat::PIXEL_GRAY | (pixel_type_to << ncnn::Mat::PIXEL_CONVERT_SHIFT), 16, 16);
ncnn::Mat m;
ncnn::Option opt;
opt.num_threads = 1;
ncnn::resize_nearest(logo, m, w, h, opt);
return m;
}
struct compare_score_index
{
inline bool operator()(const std::pair<float, int>& a, const std::pair<float, int>& b)
{
return a.first > b.first;
}
};
static int check_top2(const std::vector<float>& cls_scores, float epsilon = 0.001)
{
// partial sort topk with index
int size = cls_scores.size();
std::vector<std::pair<float, int> > vec;
vec.resize(size);
for (int i = 0; i < size; i++)
{
vec[i] = std::make_pair(cls_scores[i], i);
}
std::partial_sort(vec.begin(), vec.begin() + 2, vec.end(), compare_score_index());
int expect_indexes[2] = {532, 920};
float expect_scores[2] = {0.189459f, 0.082801f};
for (int i = 0; i < 2; i++)
{
int index = vec[i].second;
float score = vec[i].first;
if (index != expect_indexes[i])
{
fprintf(stderr, "top %d index not match expect %d but got %d\n", i, expect_indexes[i], index);
return -1;
}
if (!NearlyEqual(score, expect_scores[i], epsilon))
{
fprintf(stderr, "top %d score not match expect %f but got %f\n", i, expect_scores[i], score);
return -1;
}
}
return 0;
}
static void fread_or_error(void* buffer, size_t size, size_t count, FILE* fp, const char* s)
{
if (count != fread(buffer, size, count, fp))
{
fprintf(stderr, "Couldn't read from file: %s\n", s);
fclose(fp);
exit(EXIT_FAILURE);
}
}
static std::string read_file_string(const char* filepath)
{
FILE* fp = fopen(filepath, "rb");
if (!fp)
{
fprintf(stderr, "fopen %s failed\n", filepath);
return std::string();
}
fseek(fp, 0, SEEK_END);
int len = ftell(fp);
rewind(fp);
std::string s;
s.resize(len + 1); // +1 for '\0'
fread_or_error((char*)s.c_str(), 1, len, fp, filepath);
fclose(fp);
s[len] = '\0';
return s;
}
static ncnn::Mat read_file_content(const char* filepath)
{
FILE* fp = fopen(filepath, "rb");
if (!fp)
{
fprintf(stderr, "fopen %s failed\n", filepath);
return ncnn::Mat();
}
fseek(fp, 0, SEEK_END);
int len = ftell(fp);
rewind(fp);
ncnn::Mat m(len, (size_t)1u, 1);
fread_or_error(m, 1, len, fp, filepath);
fclose(fp);
return m;
}
static int test_squeezenet(const ncnn::Option& opt, int load_model_type, float epsilon = 0.001)
{
ncnn::Net squeezenet;
squeezenet.opt = opt;
#ifdef __EMSCRIPTEN__
#define MODEL_DIR "/working"
#else
#define MODEL_DIR "../../examples"
#endif
std::string param_str;
ncnn::Mat param_data;
ncnn::Mat model_data;
if (load_model_type == 0)
{
// load from plain model file
squeezenet.load_param(MODEL_DIR "/squeezenet_v1.1.param");
squeezenet.load_model(MODEL_DIR "/squeezenet_v1.1.bin");
}
if (load_model_type == 1)
{
// load from plain model memory
param_str = read_file_string(MODEL_DIR "/squeezenet_v1.1.param");
model_data = read_file_content(MODEL_DIR "/squeezenet_v1.1.bin");
squeezenet.load_param_mem((const char*)param_str.c_str());
squeezenet.load_model((const unsigned char*)model_data);
}
if (load_model_type == 2)
{
// load from binary model file
squeezenet.load_param_bin(MODEL_DIR "/squeezenet_v1.1.param.bin");
squeezenet.load_model(MODEL_DIR "/squeezenet_v1.1.bin");
}
if (load_model_type == 3)
{
// load from binary model memory
param_data = read_file_content(MODEL_DIR "/squeezenet_v1.1.param.bin");
model_data = read_file_content(MODEL_DIR "/squeezenet_v1.1.bin");
squeezenet.load_param((const unsigned char*)param_data);
squeezenet.load_model((const unsigned char*)model_data);
}
ncnn::Mat in = generate_ncnn_logo(ncnn::Mat::PIXEL_BGR, 227, 227);
const float mean_vals[3] = {104.f, 117.f, 123.f};
in.substract_mean_normalize(mean_vals, 0);
ncnn::Extractor ex = squeezenet.create_extractor();
ncnn::Mat out;
if (load_model_type == 0 || load_model_type == 1)
{
ex.input("data", in);
ex.extract("prob", out);
}
if (load_model_type == 2 || load_model_type == 3)
{
ex.input(0, in);
ex.extract(82, out);
}
std::vector<float> cls_scores;
cls_scores.resize(out.w);
for (int j = 0; j < out.w; j++)
{
cls_scores[j] = out[j];
}
return check_top2(cls_scores, epsilon);
}
int main()
{
SRAND(7767517);
#ifdef __EMSCRIPTEN__
EM_ASM(
FS.mkdir('/working');
FS.mount(NODEFS, {root: '../../examples'}, '/working'););
#endif // __EMSCRIPTEN__
ncnn::UnlockedPoolAllocator g_blob_pool_allocator;
ncnn::PoolAllocator g_workspace_pool_allocator;
ncnn::Option opts[4];
opts[0].use_packing_layout = false;
opts[0].use_fp16_packed = false;
opts[0].use_fp16_storage = false;
opts[0].use_fp16_arithmetic = false;
opts[0].use_shader_pack8 = false;
opts[0].use_image_storage = false;
opts[1].use_packing_layout = true;
opts[1].use_fp16_packed = true;
opts[1].use_fp16_storage = false;
opts[1].use_fp16_arithmetic = false;
opts[1].use_shader_pack8 = true;
opts[1].use_image_storage = false;
opts[2].use_packing_layout = true;
opts[2].use_fp16_packed = true;
opts[2].use_fp16_storage = true;
opts[2].use_fp16_arithmetic = false;
opts[2].use_bf16_storage = true;
opts[2].use_shader_pack8 = true;
opts[2].use_image_storage = true;
opts[2].blob_allocator = &g_blob_pool_allocator;
opts[2].workspace_allocator = &g_workspace_pool_allocator;
opts[3].use_packing_layout = true;
opts[3].use_fp16_packed = true;
opts[3].use_fp16_storage = true;
opts[3].use_fp16_arithmetic = false; // FIXME enable me
opts[3].use_bf16_storage = false;
opts[3].use_shader_pack8 = true;
opts[3].use_image_storage = true;
opts[3].blob_allocator = &g_blob_pool_allocator;
opts[3].workspace_allocator = &g_workspace_pool_allocator;
int load_model_types[4] = {0, 1, 2, 3};
for (int i = 0; i < 4; i++)
{
opts[i].num_threads = 1;
}
for (int i = 0; i < 4; i++)
{
const ncnn::Option& opt = opts[i];
float epsilon;
if (opt.use_bf16_storage || opt.use_fp16_packed || opt.use_fp16_storage)
{
epsilon = 0.1;
}
else
{
epsilon = 0.01;
}
int ret;
ncnn::Option opt_cpu = opt;
opt_cpu.use_vulkan_compute = false;
ret = test_squeezenet(opt_cpu, load_model_types[i], epsilon);
if (ret != 0)
{
fprintf(stderr, "test_squeezenet cpu failed use_packing_layout=%d use_fp16_packed=%d use_fp16_storage=%d use_shader_pack8=%d use_bf16_storage=%d use_image_storage=%d\n", opt.use_packing_layout, opt.use_fp16_packed, opt.use_fp16_storage, opt.use_shader_pack8, opt.use_bf16_storage, opt.use_image_storage);
return ret;
}
#if NCNN_VULKAN
ncnn::Option opt_gpu = opt;
opt_gpu.use_vulkan_compute = true;
ret = test_squeezenet(opt_gpu, load_model_types[i], epsilon);
if (ret != 0)
{
fprintf(stderr, "test_squeezenet gpu failed use_packing_layout=%d use_fp16_packed=%d use_fp16_storage=%d use_shader_pack8=%d use_bf16_storage=%d use_image_storage=%d\n", opt.use_packing_layout, opt.use_fp16_packed, opt.use_fp16_storage, opt.use_shader_pack8, opt.use_bf16_storage, opt.use_image_storage);
return ret;
}
#endif // NCNN_VULKAN
}
return 0;
}

65
3rdparty/ncnn/tests/test_swish.cpp vendored Normal file
View File

@ -0,0 +1,65 @@
// 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/swish.h"
#include "testutil.h"
static int test_swish(const ncnn::Mat& a)
{
ncnn::ParamDict pd;
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Swish>("Swish", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_swish failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
}
return ret;
}
static int test_swish_0()
{
return 0
|| test_swish(RandomMat(5, 7, 24))
|| test_swish(RandomMat(7, 9, 12))
|| test_swish(RandomMat(3, 5, 13));
}
static int test_swish_1()
{
return 0
|| test_swish(RandomMat(15, 24))
|| test_swish(RandomMat(17, 12))
|| test_swish(RandomMat(19, 15));
}
static int test_swish_2()
{
return 0
|| test_swish(RandomMat(128))
|| test_swish(RandomMat(124))
|| test_swish(RandomMat(127));
}
int main()
{
SRAND(7767517);
return 0
|| test_swish_0()
|| test_swish_1()
|| test_swish_2();
}

65
3rdparty/ncnn/tests/test_tanh.cpp vendored Normal file
View File

@ -0,0 +1,65 @@
// 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/tanh.h"
#include "testutil.h"
static int test_tanh(const ncnn::Mat& a)
{
ncnn::ParamDict pd;
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::TanH>("TanH", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_tanh failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
}
return ret;
}
static int test_tanh_0()
{
return 0
|| test_tanh(RandomMat(5, 7, 24))
|| test_tanh(RandomMat(7, 9, 12))
|| test_tanh(RandomMat(3, 5, 13));
}
static int test_tanh_1()
{
return 0
|| test_tanh(RandomMat(15, 24))
|| test_tanh(RandomMat(17, 12))
|| test_tanh(RandomMat(19, 15));
}
static int test_tanh_2()
{
return 0
|| test_tanh(RandomMat(128))
|| test_tanh(RandomMat(124))
|| test_tanh(RandomMat(127));
}
int main()
{
SRAND(7767517);
return 0
|| test_tanh_0()
|| test_tanh_1()
|| test_tanh_2();
}

233
3rdparty/ncnn/tests/test_tile.cpp vendored Normal file
View File

@ -0,0 +1,233 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2022 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/tile.h"
#include "testutil.h"
static int test_tile(const ncnn::Mat& a, int axis, int tiles)
{
ncnn::ParamDict pd;
pd.set(0, axis);
pd.set(1, tiles);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Tile>("Tile", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_tile failed a.dims=%d a=(%d %d %d %d) axis=%d tiles=%d\n", a.dims, a.w, a.h, a.d, a.c, axis, tiles);
}
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_tile(const ncnn::Mat& a, const ncnn::Mat& repeats)
{
ncnn::ParamDict pd;
pd.set(2, repeats);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Tile>("Tile", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_tile failed a.dims=%d a=(%d %d %d %d) repeats=", a.dims, a.w, a.h, a.d, a.c);
print_int_array(repeats);
fprintf(stderr, "\n");
}
return ret;
}
static int test_tile_0()
{
ncnn::Mat a = RandomMat(5, 6, 7, 24);
ncnn::Mat b = RandomMat(7, 8, 9, 12);
ncnn::Mat c = RandomMat(3, 4, 5, 13);
return 0
|| test_tile(a, 0, 3)
|| test_tile(a, 1, 4)
|| test_tile(a, 2, 5)
|| test_tile(a, 3, 2)
|| test_tile(b, 0, 3)
|| test_tile(b, 1, 4)
|| test_tile(b, 2, 1)
|| test_tile(b, 3, 2)
|| test_tile(c, 0, 3)
|| test_tile(c, 1, 4)
|| test_tile(c, 2, 5)
|| test_tile(c, 3, 2)
|| test_tile(a, IntArrayMat(3))
|| test_tile(a, IntArrayMat(2, 4))
|| test_tile(a, IntArrayMat(2, 2, 5))
|| test_tile(a, IntArrayMat(3, 1, 3, 2))
|| test_tile(b, IntArrayMat(3, 1))
|| test_tile(b, IntArrayMat(4, 1, 4))
|| test_tile(b, IntArrayMat(2, 2, 2, 1))
|| test_tile(b, IntArrayMat(3, 2, 1))
|| test_tile(c, IntArrayMat(3))
|| test_tile(c, IntArrayMat(1, 1, 4))
|| test_tile(c, IntArrayMat(2, 2, 5))
|| test_tile(c, IntArrayMat(3, 2, 1, 9));
}
static int test_tile_1()
{
ncnn::Mat a = RandomMat(5, 7, 24);
ncnn::Mat b = RandomMat(7, 9, 12);
ncnn::Mat c = RandomMat(3, 5, 13);
return 0
|| test_tile(a, 0, 5)
|| test_tile(a, 1, 4)
|| test_tile(a, 2, 4)
|| test_tile(b, 0, 3)
|| test_tile(b, 1, 3)
|| test_tile(b, 2, 3)
|| test_tile(c, 0, 1)
|| test_tile(c, 1, 2)
|| test_tile(c, 2, 2)
|| test_tile(a, IntArrayMat(5))
|| test_tile(a, IntArrayMat(1, 4))
|| test_tile(a, IntArrayMat(2, 1, 4))
|| test_tile(a, IntArrayMat(1, 2, 1, 4))
|| test_tile(b, IntArrayMat(3))
|| test_tile(b, IntArrayMat(1, 3, 3))
|| test_tile(b, IntArrayMat(2, 3))
|| test_tile(b, IntArrayMat(2, 3, 3, 3))
|| test_tile(c, IntArrayMat(1))
|| test_tile(c, IntArrayMat(2, 1))
|| test_tile(c, IntArrayMat(2, 2, 2))
|| test_tile(c, IntArrayMat(2, 1, 2, 1));
}
static int test_tile_2()
{
ncnn::Mat a = RandomMat(15, 24);
ncnn::Mat b = RandomMat(17, 12);
ncnn::Mat c = RandomMat(19, 13);
return 0
|| test_tile(a, 0, 2)
|| test_tile(a, 1, 1)
|| test_tile(b, 0, 3)
|| test_tile(b, 1, 4)
|| test_tile(c, 0, 5)
|| test_tile(c, 1, 6)
|| test_tile(a, IntArrayMat(2))
|| test_tile(a, IntArrayMat(1, 1))
|| test_tile(a, IntArrayMat(4, 1, 1))
|| test_tile(a, IntArrayMat(2, 4, 4, 1))
|| test_tile(b, IntArrayMat(3))
|| test_tile(b, IntArrayMat(2, 4))
|| test_tile(b, IntArrayMat(2, 4, 3, 1))
|| test_tile(b, IntArrayMat(1, 2, 1, 4))
|| test_tile(c, IntArrayMat(5))
|| test_tile(c, IntArrayMat(6, 1))
|| test_tile(c, IntArrayMat(6, 1, 6))
|| test_tile(c, IntArrayMat(3, 2, 1, 1));
}
static int test_tile_3()
{
ncnn::Mat a = RandomMat(128);
ncnn::Mat b = RandomMat(124);
ncnn::Mat c = RandomMat(127);
return 0
|| test_tile(a, 0, 1)
|| test_tile(a, 0, 2)
|| test_tile(b, 0, 3)
|| test_tile(c, 0, 4)
|| test_tile(a, IntArrayMat(10))
|| test_tile(a, IntArrayMat(10, 1))
|| test_tile(a, IntArrayMat(5, 2, 1))
|| test_tile(a, IntArrayMat(2, 2, 2, 3))
|| test_tile(b, IntArrayMat(2))
|| test_tile(b, IntArrayMat(2, 2))
|| test_tile(b, IntArrayMat(2, 2, 1))
|| test_tile(b, IntArrayMat(4, 1, 2, 2))
|| test_tile(c, IntArrayMat(3))
|| test_tile(c, IntArrayMat(4, 3))
|| test_tile(c, IntArrayMat(1))
|| test_tile(c, IntArrayMat(1, 1))
|| test_tile(c, IntArrayMat(1, 1, 1))
|| test_tile(c, IntArrayMat(1, 3, 2, 2));
}
int main()
{
SRAND(7767517);
return 0
|| test_tile_0()
|| test_tile_1()
|| test_tile_2()
|| test_tile_3();
}

107
3rdparty/ncnn/tests/test_unaryop.cpp vendored Normal file
View File

@ -0,0 +1,107 @@
// 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/unaryop.h"
#include "testutil.h"
#define OP_TYPE_MAX 17
static int op_type = 0;
static int test_unaryop(const ncnn::Mat& _a)
{
ncnn::Mat a = _a;
if (op_type == 2 || op_type == 3)
{
// large dynamic range for floor ceil
for (int i = 0; i < a.total(); i++)
{
a[i] *= 1000;
}
}
if (op_type == 5 || op_type == 6 || op_type == 8)
{
// value must be positive for sqrt rsqrt log
Randomize(a, 0.001f, 2.f);
}
if (op_type == 11 || op_type == 12 || op_type == 13)
{
// smaller range for tan asin acos
Randomize(a, -1.f, 1.f);
}
ncnn::ParamDict pd;
pd.set(0, op_type);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::UnaryOp>("UnaryOp", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_unaryop failed a.dims=%d a=(%d %d %d %d) op_type=%d\n", a.dims, a.w, a.h, a.d, a.c, op_type);
}
return ret;
}
static int test_unaryop_0()
{
return 0
|| test_unaryop(RandomMat(11, 3, 2, 16))
|| test_unaryop(RandomMat(10, 2, 2, 12))
|| test_unaryop(RandomMat(6, 1, 5, 13));
}
static int test_unaryop_1()
{
return 0
|| test_unaryop(RandomMat(11, 7, 16))
|| test_unaryop(RandomMat(10, 4, 12))
|| test_unaryop(RandomMat(6, 5, 13));
}
static int test_unaryop_2()
{
return 0
|| test_unaryop(RandomMat(12, 16))
|| test_unaryop(RandomMat(10, 12))
|| test_unaryop(RandomMat(14, 15));
}
static int test_unaryop_3()
{
return 0
|| test_unaryop(RandomMat(128))
|| test_unaryop(RandomMat(12))
|| test_unaryop(RandomMat(15));
}
int main()
{
SRAND(7767517);
for (op_type = 0; op_type < OP_TYPE_MAX; op_type++)
{
int ret = 0
|| test_unaryop_0()
|| test_unaryop_1()
|| test_unaryop_2()
|| test_unaryop_3();
if (ret != 0)
return ret;
}
return 0;
}

View File

@ -0,0 +1,141 @@
// 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/yolov3detectionoutput.h"
#include "testutil.h"
static int test_yolov3detectionoutput(const std::vector<ncnn::Mat>& a, int num_class,
int num_box, float confidence_threshold, float nms_threshold,
ncnn::Mat& biases, ncnn::Mat& mask, ncnn::Mat& anchors_scale)
{
ncnn::ParamDict pd;
pd.set(0, num_class);
pd.set(1, num_box);
pd.set(2, confidence_threshold);
pd.set(3, nms_threshold);
pd.set(4, biases);
pd.set(5, mask);
pd.set(6, anchors_scale);
std::vector<ncnn::Mat> weights(0);
int ret = test_layer<ncnn::Yolov3DetectionOutput>("Yolov3DetectionOutput", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_yolov3detectionoutput failed a.dims=%d a=(%d %d %d) ", a[0].dims, a[0].w, a[0].h, a[0].c);
fprintf(stderr, " num_class=%d num_box=%d", num_class, num_box);
fprintf(stderr, " confidence_threshold=%f nms_threshold=%f\n", confidence_threshold, nms_threshold);
}
return ret;
}
static ncnn::Mat create_mat_from(const float* src, int length)
{
ncnn::Mat ret(length);
memcpy(ret.data, src, length * sizeof(float));
return ret;
}
static ncnn::Mat MyRandomMat(int w, int h, int c)
{
ncnn::Mat m(w, h, c);
Randomize(m, -15.f, 1.5f);
return m;
}
static int test_yolov3detectionoutput_v4()
{
const float b[] = {12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401};
const float m[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
const float s[] = {9.6, 17.6, 33.6};
ncnn::Mat biases = create_mat_from(b, sizeof(b) / sizeof(b[0]));
ncnn::Mat mask = create_mat_from(m, sizeof(m) / sizeof(m[0]));
ncnn::Mat anchors_scale = create_mat_from(s, sizeof(s) / sizeof(s[0]));
std::vector<ncnn::Mat> a(3);
a[0] = MyRandomMat(76, 76, 255);
a[1] = MyRandomMat(38, 38, 255);
a[2] = MyRandomMat(19, 19, 255);
return 0
|| test_yolov3detectionoutput(a, 80, 3, 0.55f, 0.45f, biases, mask, anchors_scale);
}
static int test_yolov3detectionoutput_v4tiny()
{
const float b[] = {10, 14, 23, 27, 37, 58, 81, 82, 135, 169, 344, 319};
const float m[] = {3, 4, 5, 1, 2, 3};
const float s[] = {33.6, 16.8};
ncnn::Mat biases = create_mat_from(b, sizeof(b) / sizeof(b[0]));
ncnn::Mat mask = create_mat_from(m, sizeof(m) / sizeof(m[0]));
ncnn::Mat anchors_scale = create_mat_from(s, sizeof(s) / sizeof(s[0]));
std::vector<ncnn::Mat> a(2);
a[0] = MyRandomMat(13, 13, 255);
a[1] = MyRandomMat(26, 26, 255);
return 0
|| test_yolov3detectionoutput(a, 80, 3, 0.4f, 0.45f, biases, mask, anchors_scale);
}
static int test_yolov3detectionoutput_v3()
{
const float b[] = {10, 13, 16, 30, 33, 23, 30, 61, 62, 45, 59, 119, 116, 90, 156, 198, 373, 326};
const float m[] = {6, 7, 8, 3, 4, 5, 0, 1, 2};
const float s[] = {32, 16, 8};
ncnn::Mat biases = create_mat_from(b, sizeof(b) / sizeof(b[0]));
ncnn::Mat mask = create_mat_from(m, sizeof(m) / sizeof(m[0]));
ncnn::Mat anchors_scale = create_mat_from(s, sizeof(s) / sizeof(s[0]));
std::vector<ncnn::Mat> a(3);
a[0] = MyRandomMat(19, 19, 255);
a[1] = MyRandomMat(38, 38, 255);
a[2] = MyRandomMat(76, 76, 255);
return 0
|| test_yolov3detectionoutput(a, 80, 3, 0.6f, 0.45f, biases, mask, anchors_scale);
}
static int test_yolov3detectionoutput_v3tiny()
{
const float b[] = {10, 14, 23, 27, 37, 58, 81, 82, 135, 169, 344, 319};
const float m[] = {3, 4, 5, 1, 2, 3};
const float s[] = {32, 16};
ncnn::Mat biases = create_mat_from(b, sizeof(b) / sizeof(b[0]));
ncnn::Mat mask = create_mat_from(m, sizeof(m) / sizeof(m[0]));
ncnn::Mat anchors_scale = create_mat_from(s, sizeof(s) / sizeof(s[0]));
std::vector<ncnn::Mat> a(2);
a[0] = MyRandomMat(13, 13, 255);
a[1] = MyRandomMat(26, 26, 255);
return 0
|| test_yolov3detectionoutput(a, 80, 3, 0.3f, 0.45f, biases, mask, anchors_scale);
}
int main()
{
SRAND(7767517);
return 0
|| test_yolov3detectionoutput_v3tiny()
|| test_yolov3detectionoutput_v3()
|| test_yolov3detectionoutput_v4tiny()
|| test_yolov3detectionoutput_v4();
}

1482
3rdparty/ncnn/tests/testutil.h vendored Normal file

File diff suppressed because it is too large Load Diff