| 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 "squeeze.h" |
| 16 | |
| 17 | namespace ncnn { |
| 18 | |
| 19 | Squeeze::Squeeze() |
| 20 | { |
| 21 | one_blob_only = true; |
| 22 | support_inplace = false; |
| 23 | } |
| 24 | |
| 25 | int Squeeze::load_param(const ParamDict& pd) |
| 26 | { |
| 27 | squeeze_w = pd.get(0, 0); |
| 28 | squeeze_h = pd.get(1, 0); |
| 29 | squeeze_d = pd.get(11, 0); |
| 30 | squeeze_c = pd.get(2, 0); |
| 31 | axes = pd.get(3, Mat()); |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | int Squeeze::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 d = bottom_blob.d; |
| 41 | int channels = bottom_blob.c; |
| 42 | int dims = bottom_blob.dims; |
| 43 | |
| 44 | bool _squeeze_w = false; |
| 45 | bool _squeeze_h = false; |
| 46 | bool _squeeze_d = false; |
| 47 | bool _squeeze_c = false; |
| 48 | |
| 49 | if (axes.empty()) |
| 50 | { |
| 51 | _squeeze_w = w == 1 && squeeze_w; |
| 52 | _squeeze_h = h == 1 && squeeze_h; |
| 53 | _squeeze_d = d == 1 && squeeze_d; |
| 54 | _squeeze_c = channels == 1 && squeeze_c; |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | const int* axes_ptr = axes; |
| 59 | for (int i = 0; i < axes.w; i++) |
| 60 | { |
| 61 | int axis = axes_ptr[i]; |
| 62 | if (axis < 0) |
| 63 | axis = dims + axis; |
| 64 | |
| 65 | if (dims == 1 && axis == 0) |
| 66 | { |
| 67 | _squeeze_w = w == 1; |
| 68 | } |
| 69 | if (dims == 2 && axis == 0) |
| 70 | { |
| 71 | _squeeze_h = h == 1; |
| 72 | } |
| 73 | if (dims == 2 && axis == 1) |
| 74 | { |
| 75 | _squeeze_w = w == 1; |
| 76 | } |
| 77 | if (dims == 3 && axis == 0) |
| 78 | { |
| 79 | _squeeze_c = channels == 1; |
| 80 | } |
| 81 | if (dims == 3 && axis == 1) |
| 82 | { |
| 83 | _squeeze_h = h == 1; |
| 84 | } |
| 85 | if (dims == 3 && axis == 2) |
| 86 | { |
| 87 | _squeeze_w = w == 1; |
| 88 | } |
| 89 | if (dims == 4 && axis == 0) |
| 90 | { |
| 91 | _squeeze_c = channels == 1; |
| 92 | } |
| 93 | if (dims == 4 && axis == 1) |
| 94 | { |
| 95 | _squeeze_d = d == 1; |
| 96 | } |
| 97 | if (dims == 4 && axis == 2) |
| 98 | { |
| 99 | _squeeze_h = h == 1; |
| 100 | } |
| 101 | if (dims == 4 && axis == 3) |
| 102 | { |
| 103 | _squeeze_w = w == 1; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | top_blob = bottom_blob; |
| 109 | |
| 110 | if (dims == 1) |
| 111 | { |
| 112 | if (_squeeze_w) |
| 113 | { |
| 114 | top_blob = bottom_blob.reshape(1, opt.blob_allocator); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (dims == 2) |
| 119 | { |
| 120 | if (_squeeze_w && _squeeze_h) |
| 121 | { |
| 122 | top_blob = bottom_blob.reshape(1, opt.blob_allocator); |
| 123 | } |
| 124 | else if (_squeeze_w) |
| 125 | { |
| 126 | top_blob = bottom_blob.reshape(h, opt.blob_allocator); |
| 127 | } |
| 128 | else if (_squeeze_h) |
| 129 | { |
| 130 | top_blob = bottom_blob.reshape(w, opt.blob_allocator); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if (dims == 3) |
| 135 | { |
| 136 | if (_squeeze_w && _squeeze_h && _squeeze_c) |
| 137 | { |
| 138 | top_blob = bottom_blob.reshape(1, opt.blob_allocator); |
| 139 | } |
| 140 | else if (_squeeze_w && _squeeze_h) |
| 141 | { |
| 142 | top_blob = bottom_blob.reshape(channels, opt.blob_allocator); |
| 143 | } |
| 144 | else if (_squeeze_h && _squeeze_c) |
| 145 | { |
| 146 | top_blob = bottom_blob.reshape(w, opt.blob_allocator); |
| 147 | } |
| 148 | else if (_squeeze_w && _squeeze_c) |
| 149 | { |
| 150 | top_blob = bottom_blob.reshape(h, opt.blob_allocator); |
| 151 | } |
| 152 | else if (_squeeze_w) |
| 153 | { |
| 154 | top_blob = bottom_blob.reshape(h, channels, opt.blob_allocator); |
| 155 | } |
| 156 | else if (_squeeze_h) |
| 157 | { |
| 158 | top_blob = bottom_blob.reshape(w, channels, opt.blob_allocator); |
| 159 | } |
| 160 | else if (_squeeze_c) |
| 161 | { |
| 162 | top_blob = bottom_blob.reshape(w, h, opt.blob_allocator); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if (dims == 4) |
| 167 | { |
| 168 | if (_squeeze_w && _squeeze_h && _squeeze_d && _squeeze_c) |
| 169 | { |
| 170 | top_blob = bottom_blob.reshape(1, opt.blob_allocator); |
| 171 | } |
| 172 | else if (_squeeze_w && _squeeze_h && _squeeze_d) |
| 173 | { |
| 174 | top_blob = bottom_blob.reshape(channels, opt.blob_allocator); |
| 175 | } |
| 176 | else if (_squeeze_h && _squeeze_d && _squeeze_c) |
| 177 | { |
| 178 | top_blob = bottom_blob.reshape(w, opt.blob_allocator); |
| 179 | } |
| 180 | else if (_squeeze_w && _squeeze_d && _squeeze_c) |
| 181 | { |
| 182 | top_blob = bottom_blob.reshape(h, opt.blob_allocator); |
| 183 | } |
| 184 | else if (_squeeze_w && _squeeze_h && _squeeze_c) |
| 185 | { |
| 186 | top_blob = bottom_blob.reshape(d, opt.blob_allocator); |
| 187 | } |
| 188 | else if (_squeeze_w && _squeeze_h) |
| 189 | { |
| 190 | top_blob = bottom_blob.reshape(d, channels, opt.blob_allocator); |
| 191 | } |
| 192 | else if (_squeeze_w && _squeeze_d) |
| 193 | { |
| 194 | top_blob = bottom_blob.reshape(h, channels, opt.blob_allocator); |
| 195 | } |
| 196 | else if (_squeeze_h && _squeeze_d) |
| 197 | { |
| 198 | top_blob = bottom_blob.reshape(w, channels, opt.blob_allocator); |
| 199 | } |
| 200 | else if (_squeeze_h && _squeeze_c) |
| 201 | { |
| 202 | top_blob = bottom_blob.reshape(w, d, opt.blob_allocator); |
| 203 | } |
| 204 | else if (_squeeze_w && _squeeze_c) |
| 205 | { |
| 206 | top_blob = bottom_blob.reshape(h, d, opt.blob_allocator); |
| 207 | } |
| 208 | else if (_squeeze_d && _squeeze_c) |
| 209 | { |
| 210 | top_blob = bottom_blob.reshape(w, h, opt.blob_allocator); |
| 211 | } |
| 212 | else if (_squeeze_w) |
| 213 | { |
| 214 | top_blob = bottom_blob.reshape(h, d, channels, opt.blob_allocator); |
| 215 | } |
| 216 | else if (_squeeze_h) |
| 217 | { |
| 218 | top_blob = bottom_blob.reshape(w, d, channels, opt.blob_allocator); |
| 219 | } |
| 220 | else if (_squeeze_d) |
| 221 | { |
| 222 | top_blob = bottom_blob.reshape(w, h, channels, opt.blob_allocator); |
| 223 | } |
| 224 | else if (_squeeze_c) |
| 225 | { |
| 226 | top_blob = bottom_blob.reshape(w, h, d, opt.blob_allocator); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if (top_blob.empty()) |
| 231 | return -100; |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | } // namespace ncnn |