| 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 "priorbox.h" |
| 16 | |
| 17 | namespace ncnn { |
| 18 | |
| 19 | PriorBox::PriorBox() |
| 20 | { |
| 21 | one_blob_only = false; |
| 22 | support_inplace = false; |
| 23 | } |
| 24 | |
| 25 | int PriorBox::load_param(const ParamDict& pd) |
| 26 | { |
| 27 | min_sizes = pd.get(0, Mat()); |
| 28 | max_sizes = pd.get(1, Mat()); |
| 29 | aspect_ratios = pd.get(2, Mat()); |
| 30 | variances[0] = pd.get(3, 0.1f); |
| 31 | variances[1] = pd.get(4, 0.1f); |
| 32 | variances[2] = pd.get(5, 0.2f); |
| 33 | variances[3] = pd.get(6, 0.2f); |
| 34 | flip = pd.get(7, 1); |
| 35 | clip = pd.get(8, 0); |
| 36 | image_width = pd.get(9, 0); |
| 37 | image_height = pd.get(10, 0); |
| 38 | step_width = pd.get(11, -233.f); |
| 39 | step_height = pd.get(12, -233.f); |
| 40 | offset = pd.get(13, 0.f); |
| 41 | step_mmdetection = pd.get(14, 0); |
| 42 | center_mmdetection = pd.get(15, 0); |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | int PriorBox::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 48 | { |
| 49 | int w = bottom_blobs[0].w; |
| 50 | int h = bottom_blobs[0].h; |
| 51 | |
| 52 | if (bottom_blobs.size() == 1 && image_width == -233 && image_height == -233 && max_sizes.empty()) |
| 53 | { |
| 54 | // mxnet style _contrib_MultiBoxPrior |
| 55 | float step_w = step_width; |
| 56 | float step_h = step_height; |
| 57 | if (step_w == -233) |
| 58 | step_w = 1.f / (float)w; |
| 59 | if (step_h == -233) |
| 60 | step_h = 1.f / (float)h; |
| 61 | |
| 62 | int num_sizes = min_sizes.w; |
| 63 | int num_ratios = aspect_ratios.w; |
| 64 | |
| 65 | int num_prior = num_sizes - 1 + num_ratios; |
| 66 | |
| 67 | Mat& top_blob = top_blobs[0]; |
| 68 | top_blob.create(4 * w * h * num_prior, 4u, opt.blob_allocator); |
| 69 | if (top_blob.empty()) |
| 70 | return -100; |
| 71 | |
| 72 | #pragma omp parallel for num_threads(opt.num_threads) |
| 73 | for (int i = 0; i < h; i++) |
| 74 | { |
| 75 | float* box = (float*)top_blob + i * w * num_prior * 4; |
| 76 | |
| 77 | float center_x = offset * step_w; |
| 78 | float center_y = offset * step_h + i * step_h; |
| 79 | |
| 80 | for (int j = 0; j < w; j++) |
| 81 | { |
| 82 | // ratio = 1, various sizes |
| 83 | for (int k = 0; k < num_sizes; k++) |
| 84 | { |
| 85 | float size = min_sizes[k]; |
| 86 | float cw = size * h / w / 2; |
| 87 | float ch = size / 2; |
| 88 | |
| 89 | box[0] = center_x - cw; |
| 90 | box[1] = center_y - ch; |
| 91 | box[2] = center_x + cw; |
| 92 | box[3] = center_y + ch; |
| 93 | box += 4; |
| 94 | } |
| 95 | |
| 96 | // various ratios, size = min_size = size[0] |
| 97 | float size = min_sizes[0]; |
| 98 | for (int p = 1; p < num_ratios; p++) |
| 99 | { |
| 100 | float ratio = sqrtf(aspect_ratios[p]); |
| 101 | float cw = size * h / w * ratio / 2; |
| 102 | float ch = size / ratio / 2; |
| 103 | |
| 104 | box[0] = center_x - cw; |
| 105 | box[1] = center_y - ch; |
| 106 | box[2] = center_x + cw; |
| 107 | box[3] = center_y + ch; |
| 108 | box += 4; |
| 109 | } |
| 110 | |
| 111 | center_x += step_w; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (clip) |
| 116 | { |
| 117 | float* box = top_blob; |
| 118 | for (int i = 0; i < top_blob.w; i++) |
| 119 | { |
| 120 | box[i] = std::min(std::max(box[i], 0.f), 1.f); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | int image_w = image_width; |
| 128 | int image_h = image_height; |
| 129 | if (image_w == -233) |
| 130 | image_w = bottom_blobs[1].w; |
| 131 | if (image_h == -233) |
| 132 | image_h = bottom_blobs[1].h; |
| 133 | |
| 134 | float step_w = step_width; |
| 135 | float step_h = step_height; |
| 136 | if (step_w == -233) |
| 137 | { |
| 138 | step_w = (float)image_w / w; |
| 139 | if (step_mmdetection) |
| 140 | step_w = ceilf((float)image_w / w); |
| 141 | } |
| 142 | if (step_h == -233) |
| 143 | { |
| 144 | step_h = (float)image_h / h; |
| 145 | if (step_mmdetection) |
| 146 | step_h = ceilf((float)image_h / h); |
| 147 | } |
| 148 | |
| 149 | int num_min_size = min_sizes.w; |
| 150 | int num_max_size = max_sizes.w; |
| 151 | int num_aspect_ratio = aspect_ratios.w; |
| 152 | |
| 153 | int num_prior = num_min_size * num_aspect_ratio + num_min_size + num_max_size; |
| 154 | if (flip) |
| 155 | num_prior += num_min_size * num_aspect_ratio; |
| 156 | |
| 157 | Mat& top_blob = top_blobs[0]; |
| 158 | top_blob.create(4 * w * h * num_prior, 2, 4u, opt.blob_allocator); |
| 159 | if (top_blob.empty()) |
| 160 | return -100; |
| 161 | |
| 162 | #pragma omp parallel for num_threads(opt.num_threads) |
| 163 | for (int i = 0; i < h; i++) |
| 164 | { |
| 165 | float* box = (float*)top_blob + i * w * num_prior * 4; |
| 166 | |
| 167 | float center_x = offset * step_w; |
| 168 | float center_y = offset * step_h + i * step_h; |
| 169 | if (center_mmdetection) |
| 170 | { |
| 171 | center_x = offset * (step_w - 1); |
| 172 | center_y = offset * (step_h - 1) + i * step_h; |
| 173 | } |
| 174 | |
| 175 | for (int j = 0; j < w; j++) |
| 176 | { |
| 177 | float box_w; |
| 178 | float box_h; |
| 179 | |
| 180 | for (int k = 0; k < num_min_size; k++) |
| 181 | { |
| 182 | float min_size = min_sizes[k]; |
| 183 | |
| 184 | // min size box |
| 185 | box_w = box_h = min_size; |
| 186 | |
| 187 | box[0] = (center_x - box_w * 0.5f) / image_w; |
| 188 | box[1] = (center_y - box_h * 0.5f) / image_h; |
| 189 | box[2] = (center_x + box_w * 0.5f) / image_w; |
| 190 | box[3] = (center_y + box_h * 0.5f) / image_h; |
| 191 | |
| 192 | box += 4; |
| 193 | |
| 194 | if (num_max_size > 0) |
| 195 | { |
| 196 | float max_size = max_sizes[k]; |
| 197 | |
| 198 | // max size box |
| 199 | box_w = box_h = sqrtf(min_size * max_size); |
| 200 | |
| 201 | box[0] = (center_x - box_w * 0.5f) / image_w; |
| 202 | box[1] = (center_y - box_h * 0.5f) / image_h; |
| 203 | box[2] = (center_x + box_w * 0.5f) / image_w; |
| 204 | box[3] = (center_y + box_h * 0.5f) / image_h; |
| 205 | |
| 206 | box += 4; |
| 207 | } |
| 208 | |
| 209 | // all aspect_ratios |
| 210 | for (int p = 0; p < num_aspect_ratio; p++) |
| 211 | { |
| 212 | float ar = aspect_ratios[p]; |
| 213 | |
| 214 | box_w = min_size * sqrtf(ar); |
| 215 | box_h = min_size / sqrtf(ar); |
| 216 | |
| 217 | box[0] = (center_x - box_w * 0.5f) / image_w; |
| 218 | box[1] = (center_y - box_h * 0.5f) / image_h; |
| 219 | box[2] = (center_x + box_w * 0.5f) / image_w; |
| 220 | box[3] = (center_y + box_h * 0.5f) / image_h; |
| 221 | |
| 222 | box += 4; |
| 223 | |
| 224 | if (flip) |
| 225 | { |
| 226 | box[0] = (center_x - box_h * 0.5f) / image_w; |
| 227 | box[1] = (center_y - box_w * 0.5f) / image_h; |
| 228 | box[2] = (center_x + box_h * 0.5f) / image_w; |
| 229 | box[3] = (center_y + box_w * 0.5f) / image_h; |
| 230 | |
| 231 | box += 4; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | center_x += step_w; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if (clip) |
| 241 | { |
| 242 | float* box = top_blob; |
| 243 | for (int i = 0; i < top_blob.w; i++) |
| 244 | { |
| 245 | box[i] = std::min(std::max(box[i], 0.f), 1.f); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // set variance |
| 250 | float* var = top_blob.row(1); |
| 251 | for (int i = 0; i < top_blob.w / 4; i++) |
| 252 | { |
| 253 | var[0] = variances[0]; |
| 254 | var[1] = variances[1]; |
| 255 | var[2] = variances[2]; |
| 256 | var[3] = variances[3]; |
| 257 | |
| 258 | var += 4; |
| 259 | } |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | } // namespace ncnn |