Elffuss BraidHub

kikocis/ncnn public

d7a5f98c0f60 · 4.0 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2017 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 "expanddims.h"
16
17namespace ncnn {
18
19ExpandDims::ExpandDims()
20{
21 one_blob_only = true;
22 support_inplace = false;
23}
24
25int ExpandDims::load_param(const ParamDict& pd)
26{
27 expand_w = pd.get(0, 0);
28 expand_h = pd.get(1, 0);
29 expand_d = pd.get(11, 0);
30 expand_c = pd.get(2, 0);
31 axes = pd.get(3, Mat());
32
33 return 0;
34}
35
36int ExpandDims::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
37{
38 int w = bottom_blob.w;
39 int h = bottom_blob.h;
40 int channels = bottom_blob.c;
41 int dims = bottom_blob.dims;
42
43 bool _expand_w = false;
44 bool _expand_h = false;
45 bool _expand_d = false;
46 bool _expand_c = false;
47
48 if (axes.empty())
49 {
50 _expand_w = expand_w;
51 _expand_h = expand_h;
52 _expand_d = expand_d;
53 _expand_c = expand_c;
54 }
55 else
56 {
57 const int* axes_ptr = axes;
58 for (int i = 0; i < axes.w; i++)
59 {
60 int axis = axes_ptr[i];
61 if (axis < 0)
62 axis = dims + 1 + axis;
63
64 if (dims == 1 && axis == 0)
65 {
66 _expand_h = true;
67 }
68 if (dims == 1 && axis == 1)
69 {
70 _expand_w = true;
71 }
72 if (dims == 2 && axis == 0)
73 {
74 _expand_c = true;
75 }
76 if (dims == 2 && axis == 1)
77 {
78 _expand_h = true;
79 }
80 if (dims == 2 && axis == 2)
81 {
82 _expand_w = true;
83 }
84 if (dims == 3 && axis == 0)
85 {
86 _expand_c = true;
87 }
88 if (dims == 3 && axis == 1)
89 {
90 _expand_d = true;
91 }
92 if (dims == 3 && axis == 2)
93 {
94 _expand_h = true;
95 }
96 if (dims == 3 && axis == 3)
97 {
98 _expand_w = true;
99 }
100 }
101 }
102
103 top_blob = bottom_blob;
104
105 if (dims == 1)
106 {
107 if (_expand_w && _expand_h)
108 {
109 top_blob = bottom_blob.reshape(1, w, 1, opt.blob_allocator);
110 }
111 else if (_expand_w)
112 {
113 top_blob = bottom_blob.reshape(1, w, opt.blob_allocator);
114 }
115 else if (_expand_h)
116 {
117 top_blob = bottom_blob.reshape(w, 1, opt.blob_allocator);
118 }
119 }
120
121 if (dims == 2)
122 {
123 if (_expand_w)
124 {
125 top_blob = bottom_blob.reshape(1, w, h, opt.blob_allocator);
126 }
127 else if (_expand_h)
128 {
129 top_blob = bottom_blob.reshape(w, 1, h, opt.blob_allocator);
130 }
131 else if (_expand_c)
132 {
133 top_blob = bottom_blob.reshape(w, h, 1, opt.blob_allocator);
134 }
135 }
136
137 if (dims == 3)
138 {
139 if (_expand_w)
140 {
141 top_blob = bottom_blob.reshape(1, w, h, channels, opt.blob_allocator);
142 }
143 else if (_expand_h)
144 {
145 top_blob = bottom_blob.reshape(w, 1, h, channels, opt.blob_allocator);
146 }
147 else if (_expand_d)
148 {
149 top_blob = bottom_blob.reshape(w, h, 1, channels, opt.blob_allocator);
150 }
151 else if (_expand_c)
152 {
153 top_blob = bottom_blob.reshape(w, h, channels, 1, opt.blob_allocator);
154 }
155 }
156
157 if (top_blob.empty())
158 return -100;
159
160 return 0;
161}
162
163} // namespace ncnn