| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2020 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 "roialign_x86.h" |
| 16 | |
| 17 | namespace ncnn { |
| 18 | |
| 19 | // adapted from detectron2 |
| 20 | // https://github.com/facebookresearch/detectron2/blob/master/detectron2/layers/csrc/ROIAlign/ROIAlign_cpu.cpp |
| 21 | template<typename T> |
| 22 | struct PreCalc |
| 23 | { |
| 24 | int pos1; |
| 25 | int pos2; |
| 26 | int pos3; |
| 27 | int pos4; |
| 28 | T w1; |
| 29 | T w2; |
| 30 | T w3; |
| 31 | T w4; |
| 32 | }; |
| 33 | |
| 34 | template<typename T> |
| 35 | void detectron2_pre_calc_for_bilinear_interpolate( |
| 36 | const int height, |
| 37 | const int width, |
| 38 | const int pooled_height, |
| 39 | const int pooled_width, |
| 40 | const int iy_upper, |
| 41 | const int ix_upper, |
| 42 | T roi_start_h, |
| 43 | T roi_start_w, |
| 44 | T bin_size_h, |
| 45 | T bin_size_w, |
| 46 | int roi_bin_grid_h, |
| 47 | int roi_bin_grid_w, |
| 48 | std::vector<PreCalc<T> >& pre_calc) |
| 49 | { |
| 50 | int pre_calc_index = 0; |
| 51 | for (int ph = 0; ph < pooled_height; ph++) |
| 52 | { |
| 53 | for (int pw = 0; pw < pooled_width; pw++) |
| 54 | { |
| 55 | for (int iy = 0; iy < iy_upper; iy++) |
| 56 | { |
| 57 | const T yy = roi_start_h + ph * bin_size_h + static_cast<T>(iy + .5f) * bin_size_h / static_cast<T>(roi_bin_grid_h); // e.g., 0.5, 1.5 |
| 58 | for (int ix = 0; ix < ix_upper; ix++) |
| 59 | { |
| 60 | const T xx = roi_start_w + pw * bin_size_w + static_cast<T>(ix + .5f) * bin_size_w / static_cast<T>(roi_bin_grid_w); |
| 61 | |
| 62 | T x = xx; |
| 63 | T y = yy; |
| 64 | // deal with: inverse elements are out of feature map boundary |
| 65 | if (y < -1.0 || y > height || x < -1.0 || x > width) |
| 66 | { |
| 67 | // empty |
| 68 | PreCalc<T> pc; |
| 69 | pc.pos1 = 0; |
| 70 | pc.pos2 = 0; |
| 71 | pc.pos3 = 0; |
| 72 | pc.pos4 = 0; |
| 73 | pc.w1 = 0; |
| 74 | pc.w2 = 0; |
| 75 | pc.w3 = 0; |
| 76 | pc.w4 = 0; |
| 77 | pre_calc[pre_calc_index++] = pc; |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | if (y <= 0) |
| 82 | { |
| 83 | y = 0; |
| 84 | } |
| 85 | if (x <= 0) |
| 86 | { |
| 87 | x = 0; |
| 88 | } |
| 89 | |
| 90 | int y_low = (int)y; |
| 91 | int x_low = (int)x; |
| 92 | int y_high; |
| 93 | int x_high; |
| 94 | |
| 95 | if (y_low >= height - 1) |
| 96 | { |
| 97 | y_high = y_low = height - 1; |
| 98 | y = (T)y_low; |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | y_high = y_low + 1; |
| 103 | } |
| 104 | |
| 105 | if (x_low >= width - 1) |
| 106 | { |
| 107 | x_high = x_low = width - 1; |
| 108 | x = (T)x_low; |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | x_high = x_low + 1; |
| 113 | } |
| 114 | |
| 115 | T ly = y - y_low; |
| 116 | T lx = x - x_low; |
| 117 | T hy = (T)(1. - ly), hx = (T)(1. - lx); |
| 118 | T w1 = hy * hx, w2 = hy * lx, w3 = ly * hx, w4 = ly * lx; |
| 119 | |
| 120 | // save weights and indices |
| 121 | PreCalc<T> pc; |
| 122 | pc.pos1 = y_low * width + x_low; |
| 123 | pc.pos2 = y_low * width + x_high; |
| 124 | pc.pos3 = y_high * width + x_low; |
| 125 | pc.pos4 = y_high * width + x_high; |
| 126 | pc.w1 = w1; |
| 127 | pc.w2 = w2; |
| 128 | pc.w3 = w3; |
| 129 | pc.w4 = w4; |
| 130 | pre_calc[pre_calc_index++] = pc; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | template<typename T> |
| 138 | void original_pre_calc_for_bilinear_interpolate( |
| 139 | const int height, |
| 140 | const int width, |
| 141 | const int pooled_height, |
| 142 | const int pooled_width, |
| 143 | T roi_start_h, |
| 144 | T roi_start_w, |
| 145 | T bin_size_h, |
| 146 | T bin_size_w, |
| 147 | int sampling_ratio, |
| 148 | std::vector<PreCalc<T> >& pre_calc) |
| 149 | { |
| 150 | int pre_calc_index = 0; |
| 151 | for (int ph = 0; ph < pooled_height; ph++) |
| 152 | { |
| 153 | for (int pw = 0; pw < pooled_width; pw++) |
| 154 | { |
| 155 | float hstart = roi_start_h + ph * bin_size_h; |
| 156 | float wstart = roi_start_w + pw * bin_size_w; |
| 157 | float hend = roi_start_h + (ph + 1) * bin_size_h; |
| 158 | float wend = roi_start_w + (pw + 1) * bin_size_w; |
| 159 | hstart = std::min(std::max(hstart, 0.f), (float)height); |
| 160 | wstart = std::min(std::max(wstart, 0.f), (float)width); |
| 161 | hend = std::min(std::max(hend, 0.f), (float)height); |
| 162 | wend = std::min(std::max(wend, 0.f), (float)width); |
| 163 | |
| 164 | int bin_grid_h = (int)(sampling_ratio > 0 ? sampling_ratio : ceil(hend - hstart)); |
| 165 | int bin_grid_w = (int)(sampling_ratio > 0 ? sampling_ratio : ceil(wend - wstart)); |
| 166 | |
| 167 | for (int by = 0; by < bin_grid_h; by++) |
| 168 | { |
| 169 | float y = hstart + (by + 0.5f) * bin_size_h / (float)bin_grid_h; |
| 170 | |
| 171 | for (int bx = 0; bx < bin_grid_w; bx++) |
| 172 | { |
| 173 | float x = wstart + (bx + 0.5f) * bin_size_w / (float)bin_grid_w; |
| 174 | int x0 = (int)x; |
| 175 | int x1 = x0 + 1; |
| 176 | int y0 = (int)y; |
| 177 | int y1 = y0 + 1; |
| 178 | |
| 179 | float a0 = x1 - x; |
| 180 | float a1 = x - x0; |
| 181 | float b0 = y1 - y; |
| 182 | float b1 = y - y0; |
| 183 | |
| 184 | if (x1 >= width) |
| 185 | { |
| 186 | x1 = width - 1; |
| 187 | a0 = 1.f; |
| 188 | a1 = 0.f; |
| 189 | } |
| 190 | if (y1 >= height) |
| 191 | { |
| 192 | y1 = height - 1; |
| 193 | b0 = 1.f; |
| 194 | b1 = 0.f; |
| 195 | } |
| 196 | // save weights and indices |
| 197 | PreCalc<T> pc; |
| 198 | pc.pos1 = y0 * width + x0; |
| 199 | pc.pos2 = y0 * width + x1; |
| 200 | pc.pos3 = y1 * width + x0; |
| 201 | pc.pos4 = y1 * width + x1; |
| 202 | pc.w1 = a0 * b0; |
| 203 | pc.w2 = a1 * b0; |
| 204 | pc.w3 = a0 * b1; |
| 205 | pc.w4 = a1 * b1; |
| 206 | pre_calc[pre_calc_index++] = pc; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | ROIAlign_x86::ROIAlign_x86() |
| 214 | { |
| 215 | } |
| 216 | |
| 217 | int ROIAlign_x86::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 218 | { |
| 219 | const Mat& bottom_blob = bottom_blobs[0]; |
| 220 | const int width = bottom_blob.w; |
| 221 | const int height = bottom_blob.h; |
| 222 | const size_t elemsize = bottom_blob.elemsize; |
| 223 | const int channels = bottom_blob.c; |
| 224 | |
| 225 | const Mat& roi_blob = bottom_blobs[1]; |
| 226 | |
| 227 | Mat& top_blob = top_blobs[0]; |
| 228 | top_blob.create(pooled_width, pooled_height, channels, elemsize, opt.blob_allocator); |
| 229 | if (top_blob.empty()) |
| 230 | return -100; |
| 231 | |
| 232 | // For each ROI R = [x y w h]: max pool over R |
| 233 | const float* roi_ptr = roi_blob; |
| 234 | |
| 235 | float roi_start_w = roi_ptr[0] * spatial_scale; |
| 236 | float roi_start_h = roi_ptr[1] * spatial_scale; |
| 237 | float roi_end_w = roi_ptr[2] * spatial_scale; |
| 238 | float roi_end_h = roi_ptr[3] * spatial_scale; |
| 239 | if (aligned) |
| 240 | { |
| 241 | roi_start_w -= 0.5f; |
| 242 | roi_start_h -= 0.5f; |
| 243 | roi_end_w -= 0.5f; |
| 244 | roi_end_h -= 0.5f; |
| 245 | } |
| 246 | |
| 247 | float roi_width = roi_end_w - roi_start_w; |
| 248 | float roi_height = roi_end_h - roi_start_h; |
| 249 | |
| 250 | if (!aligned) |
| 251 | { |
| 252 | roi_width = std::max(roi_width, 1.f); |
| 253 | roi_height = std::max(roi_height, 1.f); |
| 254 | } |
| 255 | |
| 256 | float bin_size_w = (float)roi_width / (float)pooled_width; |
| 257 | float bin_size_h = (float)roi_height / (float)pooled_height; |
| 258 | |
| 259 | if (version == 0) |
| 260 | { |
| 261 | // original version |
| 262 | int roi_bin_grid_h = (int)(sampling_ratio > 0 ? sampling_ratio : ceil(roi_height / pooled_height)); |
| 263 | int roi_bin_grid_w = (int)(sampling_ratio > 0 ? sampling_ratio : ceil(roi_width / pooled_width)); |
| 264 | std::vector<PreCalc<float> > pre_calc( |
| 265 | (size_t)roi_bin_grid_h * roi_bin_grid_w * pooled_width * pooled_height); |
| 266 | original_pre_calc_for_bilinear_interpolate( |
| 267 | height, |
| 268 | width, |
| 269 | pooled_height, |
| 270 | pooled_width, |
| 271 | roi_start_h, |
| 272 | roi_start_w, |
| 273 | bin_size_h, |
| 274 | bin_size_w, |
| 275 | sampling_ratio, |
| 276 | pre_calc); |
| 277 | |
| 278 | #pragma omp parallel for num_threads(opt.num_threads) |
| 279 | for (int q = 0; q < channels; q++) |
| 280 | { |
| 281 | const float* ptr = bottom_blob.channel(q); |
| 282 | float* outptr = top_blob.channel(q); |
| 283 | int pre_calc_index = 0; |
| 284 | |
| 285 | for (int ph = 0; ph < pooled_height; ph++) |
| 286 | { |
| 287 | for (int pw = 0; pw < pooled_width; pw++) |
| 288 | { |
| 289 | // Compute pooling region for this output unit: |
| 290 | // start (included) = ph * roi_height / pooled_height |
| 291 | // end (excluded) = (ph + 1) * roi_height / pooled_height |
| 292 | float hstart = roi_start_h + ph * bin_size_h; |
| 293 | float wstart = roi_start_w + pw * bin_size_w; |
| 294 | float hend = roi_start_h + (ph + 1) * bin_size_h; |
| 295 | float wend = roi_start_w + (pw + 1) * bin_size_w; |
| 296 | |
| 297 | hstart = std::min(std::max(hstart, 0.f), (float)height); |
| 298 | wstart = std::min(std::max(wstart, 0.f), (float)width); |
| 299 | hend = std::min(std::max(hend, 0.f), (float)height); |
| 300 | wend = std::min(std::max(wend, 0.f), (float)width); |
| 301 | |
| 302 | int bin_grid_h = (int)(sampling_ratio > 0 ? sampling_ratio : ceil(hend - hstart)); |
| 303 | int bin_grid_w = (int)(sampling_ratio > 0 ? sampling_ratio : ceil(wend - wstart)); |
| 304 | |
| 305 | bool is_empty = (hend <= hstart) || (wend <= wstart); |
| 306 | int area = bin_grid_h * bin_grid_w; |
| 307 | |
| 308 | float sum = 0.f; |
| 309 | for (int by = 0; by < bin_grid_h; by++) |
| 310 | { |
| 311 | for (int bx = 0; bx < bin_grid_w; bx++) |
| 312 | { |
| 313 | PreCalc<float>& pc = pre_calc[pre_calc_index++]; |
| 314 | // bilinear interpolate at (x,y) |
| 315 | sum += pc.w1 * ptr[pc.pos1] + pc.w2 * ptr[pc.pos2] + pc.w3 * ptr[pc.pos3] + pc.w4 * ptr[pc.pos4]; |
| 316 | } |
| 317 | } |
| 318 | outptr[pw] = is_empty ? 0.f : (sum / (float)area); |
| 319 | } |
| 320 | |
| 321 | outptr += pooled_width; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | else if (version == 1) |
| 326 | { |
| 327 | // the version in detectron 2 |
| 328 | int roi_bin_grid_h = (int)(sampling_ratio > 0 ? sampling_ratio : ceil(roi_height / pooled_height)); |
| 329 | int roi_bin_grid_w = (int)(sampling_ratio > 0 ? sampling_ratio : ceil(roi_width / pooled_width)); |
| 330 | |
| 331 | const float count = (float)std::max(roi_bin_grid_h * roi_bin_grid_w, 1); |
| 332 | |
| 333 | std::vector<PreCalc<float> > pre_calc( |
| 334 | (size_t)roi_bin_grid_h * roi_bin_grid_w * pooled_width * pooled_height); |
| 335 | detectron2_pre_calc_for_bilinear_interpolate( |
| 336 | height, |
| 337 | width, |
| 338 | pooled_height, |
| 339 | pooled_width, |
| 340 | roi_bin_grid_h, |
| 341 | roi_bin_grid_w, |
| 342 | roi_start_h, |
| 343 | roi_start_w, |
| 344 | bin_size_h, |
| 345 | bin_size_w, |
| 346 | roi_bin_grid_h, |
| 347 | roi_bin_grid_w, |
| 348 | pre_calc); |
| 349 | |
| 350 | #pragma omp parallel for num_threads(opt.num_threads) |
| 351 | for (int q = 0; q < channels; q++) |
| 352 | { |
| 353 | const float* ptr = bottom_blob.channel(q); |
| 354 | float* outptr = top_blob.channel(q); |
| 355 | int pre_calc_index = 0; |
| 356 | |
| 357 | for (int ph = 0; ph < pooled_height; ph++) |
| 358 | { |
| 359 | for (int pw = 0; pw < pooled_width; pw++) |
| 360 | { |
| 361 | float output_val = 0.f; |
| 362 | for (int iy = 0; iy < roi_bin_grid_h; iy++) |
| 363 | { |
| 364 | for (int ix = 0; ix < roi_bin_grid_w; ix++) |
| 365 | { |
| 366 | PreCalc<float>& pc = pre_calc[pre_calc_index++]; |
| 367 | |
| 368 | output_val += pc.w1 * ptr[pc.pos1] + pc.w2 * ptr[pc.pos2] + pc.w3 * ptr[pc.pos3] + pc.w4 * ptr[pc.pos4]; |
| 369 | } |
| 370 | } |
| 371 | output_val /= count; |
| 372 | outptr[pw] = output_val; |
| 373 | } |
| 374 | outptr += pooled_width; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | } // namespace ncnn |