ncnn

Форк
0
/
test_expanddims.cpp 
174 строки · 5.1 Кб
1
// Tencent is pleased to support the open source community by making ncnn available.
2
//
3
// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
4
//
5
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6
// in compliance with the License. You may obtain a copy of the License at
7
//
8
// https://opensource.org/licenses/BSD-3-Clause
9
//
10
// Unless required by applicable law or agreed to in writing, software distributed
11
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13
// specific language governing permissions and limitations under the License.
14

15
#include "testutil.h"
16

17
static int test_expanddims(const ncnn::Mat& a, int expand_w, int expand_h, int expand_d, int expand_c)
18
{
19
    ncnn::ParamDict pd;
20
    pd.set(0, expand_w);
21
    pd.set(1, expand_h);
22
    pd.set(11, expand_d);
23
    pd.set(2, expand_c);
24

25
    std::vector<ncnn::Mat> weights(0);
26

27
    int ret = test_layer("ExpandDims", pd, weights, a);
28
    if (ret != 0)
29
    {
30
        fprintf(stderr, "test_expanddims failed a.dims=%d a=(%d %d %d %d) expand_w=%d expand_h=%d expand_d=%d expand_c=%d\n", a.dims, a.w, a.h, a.d, a.c, expand_w, expand_h, expand_d, expand_c);
31
    }
32

33
    return ret;
34
}
35

36
static ncnn::Mat IntArrayMat(int a0)
37
{
38
    ncnn::Mat m(1);
39
    int* p = m;
40
    p[0] = a0;
41
    return m;
42
}
43

44
static ncnn::Mat IntArrayMat(int a0, int a1)
45
{
46
    ncnn::Mat m(2);
47
    int* p = m;
48
    p[0] = a0;
49
    p[1] = a1;
50
    return m;
51
}
52

53
static ncnn::Mat IntArrayMat(int a0, int a1, int a2)
54
{
55
    ncnn::Mat m(3);
56
    int* p = m;
57
    p[0] = a0;
58
    p[1] = a1;
59
    p[2] = a2;
60
    return m;
61
}
62

63
static ncnn::Mat IntArrayMat(int a0, int a1, int a2, int a3)
64
{
65
    ncnn::Mat m(4);
66
    int* p = m;
67
    p[0] = a0;
68
    p[1] = a1;
69
    p[2] = a2;
70
    p[3] = a3;
71
    return m;
72
}
73

74
static void print_int_array(const ncnn::Mat& a)
75
{
76
    const int* pa = a;
77

78
    fprintf(stderr, "[");
79
    for (int i = 0; i < a.w; i++)
80
    {
81
        fprintf(stderr, " %d", pa[i]);
82
    }
83
    fprintf(stderr, " ]");
84
}
85

86
static int test_expanddims_axes(const ncnn::Mat& a, const ncnn::Mat& axes)
87
{
88
    ncnn::ParamDict pd;
89
    pd.set(3, axes);
90

91
    std::vector<ncnn::Mat> weights(0);
92

93
    int ret = test_layer("ExpandDims", pd, weights, a);
94
    if (ret != 0)
95
    {
96
        fprintf(stderr, "test_expanddims_axes failed a.dims=%d a=(%d %d %d %d)\n", a.dims, a.w, a.h, a.d, a.c);
97
        fprintf(stderr, " axes=");
98
        print_int_array(axes);
99
        fprintf(stderr, "\n");
100
    }
101

102
    return ret;
103
}
104

105
static int test_expanddims_all_params(const ncnn::Mat& a)
106
{
107
    return 0
108
           || test_expanddims(a, 0, 0, 0, 0)
109
           || test_expanddims(a, 0, 0, 0, 1)
110
           || test_expanddims(a, 0, 0, 1, 0)
111
           || test_expanddims(a, 0, 0, 1, 1)
112
           || test_expanddims(a, 0, 1, 0, 0)
113
           || test_expanddims(a, 0, 1, 0, 1)
114
           || test_expanddims(a, 0, 1, 1, 0)
115
           || test_expanddims(a, 0, 1, 1, 1)
116
           || test_expanddims(a, 1, 0, 0, 0)
117
           || test_expanddims(a, 1, 0, 0, 1)
118
           || test_expanddims(a, 1, 0, 1, 0)
119
           || test_expanddims(a, 1, 0, 1, 1)
120
           || test_expanddims(a, 1, 1, 0, 0)
121
           || test_expanddims(a, 1, 1, 0, 1)
122
           || test_expanddims(a, 1, 1, 1, 0)
123
           || test_expanddims(a, 1, 1, 1, 1)
124

125
           || test_expanddims_axes(a, IntArrayMat(0))
126
           || test_expanddims_axes(a, IntArrayMat(1))
127
           || test_expanddims_axes(a, IntArrayMat(2))
128
           || test_expanddims_axes(a, IntArrayMat(3))
129
           || test_expanddims_axes(a, IntArrayMat(0, 1))
130
           || test_expanddims_axes(a, IntArrayMat(0, 2))
131
           || test_expanddims_axes(a, IntArrayMat(0, 3))
132
           || test_expanddims_axes(a, IntArrayMat(1, 2))
133
           || test_expanddims_axes(a, IntArrayMat(1, 3))
134
           || test_expanddims_axes(a, IntArrayMat(2, 3))
135
           || test_expanddims_axes(a, IntArrayMat(0, 1, 2))
136
           || test_expanddims_axes(a, IntArrayMat(0, 1, 3))
137
           || test_expanddims_axes(a, IntArrayMat(0, 2, 3))
138
           || test_expanddims_axes(a, IntArrayMat(1, 2, 3))
139
           || test_expanddims_axes(a, IntArrayMat(0, 1, 2, 3));
140
}
141

142
static int test_expanddims_0()
143
{
144
    return 0
145
           || test_expanddims_all_params(RandomMat(3, 12, 16))
146
           || test_expanddims_all_params(RandomMat(3, 1, 16))
147
           || test_expanddims_all_params(RandomMat(1, 33, 15))
148
           || test_expanddims_all_params(RandomMat(1, 14, 1))
149
           || test_expanddims_all_params(RandomMat(12, 13, 1))
150
           || test_expanddims_all_params(RandomMat(1, 1, 1));
151
}
152

153
static int test_expanddims_1()
154
{
155
    return 0
156
           || test_expanddims_all_params(RandomMat(14, 16))
157
           || test_expanddims_all_params(RandomMat(1, 14))
158
           || test_expanddims_all_params(RandomMat(11, 1))
159
           || test_expanddims_all_params(RandomMat(1, 1));
160
}
161

162
static int test_expanddims_2()
163
{
164
    return 0
165
           || test_expanddims_all_params(RandomMat(120))
166
           || test_expanddims_all_params(RandomMat(1));
167
}
168

169
int main()
170
{
171
    SRAND(7767517);
172

173
    return test_expanddims_0() || test_expanddims_1() || test_expanddims_2();
174
}
175

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.