| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2018 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.h" |
| 16 | |
| 17 | #include <assert.h> |
| 18 | |
| 19 | namespace ncnn { |
| 20 | |
| 21 | ROIAlign::ROIAlign() |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | int ROIAlign::load_param(const ParamDict& pd) |
| 26 | { |
| 27 | pooled_width = pd.get(0, 0); |
| 28 | pooled_height = pd.get(1, 0); |
| 29 | spatial_scale = pd.get(2, 1.f); |
| 30 | sampling_ratio = pd.get(3, 0); |
| 31 | aligned = pd.get(4, false); |
| 32 | version = pd.get(5, 0); |
| 33 | /* |
| 34 | * version 0: |
| 35 | * the original version of ROIAlign in ncnn |
| 36 | * version 1: |
| 37 | * the version in detectron2 |
| 38 | */ |
| 39 | assert(version >= 0 && version <= 1); |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static inline float bilinear_interpolate(const float* ptr, int w, int h, float x, float y) |
| 45 | { |
| 46 | int x0 = (int)x; |
| 47 | int x1 = x0 + 1; |
| 48 | int y0 = (int)y; |
| 49 | int y1 = y0 + 1; |
| 50 | |
| 51 | float a0 = x1 - x; |
| 52 | float a1 = x - x0; |
| 53 | float b0 = y1 - y; |
| 54 | float b1 = y - y0; |
| 55 | |
| 56 | if (x1 >= w) |
| 57 | { |
| 58 | x1 = w - 1; |
| 59 | a0 = 1.f; |
| 60 | a1 = 0.f; |
| 61 | } |
| 62 | if (y1 >= h) |
| 63 | { |
| 64 | y1 = h - 1; |
| 65 | b0 = 1.f; |
| 66 | b1 = 0.f; |
| 67 | } |
| 68 | |
| 69 | float r0 = ptr[y0 * w + x0] * a0 + ptr[y0 * w + x1] * a1; |
| 70 | float r1 = ptr[y1 * w + x0] * a0 + ptr[y1 * w + x1] * a1; |
| 71 | |
| 72 | float v = r0 * b0 + r1 * b1; |
| 73 | |
| 74 | return v; |
| 75 | } |
| 76 | |
| 77 | int ROIAlign::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 78 | { |
| 79 | const Mat& bottom_blob = bottom_blobs[0]; |
| 80 | int w = bottom_blob.w; |
| 81 | int h = bottom_blob.h; |
| 82 | size_t elemsize = bottom_blob.elemsize; |
| 83 | int channels = bottom_blob.c; |
| 84 | |
| 85 | const Mat& roi_blob = bottom_blobs[1]; |
| 86 | |
| 87 | Mat& top_blob = top_blobs[0]; |
| 88 | top_blob.create(pooled_width, pooled_height, channels, elemsize, opt.blob_allocator); |
| 89 | if (top_blob.empty()) |
| 90 | return -100; |
| 91 | |
| 92 | // For each ROI R = [x y w h]: avg pool over R |
| 93 | const float* roi_ptr = roi_blob; |
| 94 | |
| 95 | float roi_x1 = roi_ptr[0] * spatial_scale; |
| 96 | float roi_y1 = roi_ptr[1] * spatial_scale; |
| 97 | float roi_x2 = roi_ptr[2] * spatial_scale; |
| 98 | float roi_y2 = roi_ptr[3] * spatial_scale; |
| 99 | if (aligned) |
| 100 | { |
| 101 | roi_x1 -= 0.5f; |
| 102 | roi_y1 -= 0.5f; |
| 103 | roi_x2 -= 0.5f; |
| 104 | roi_y2 -= 0.5f; |
| 105 | } |
| 106 | |
| 107 | float roi_w = roi_x2 - roi_x1; |
| 108 | float roi_h = roi_y2 - roi_y1; |
| 109 | |
| 110 | if (!aligned) |
| 111 | { |
| 112 | roi_w = std::max(roi_w, 1.f); |
| 113 | roi_h = std::max(roi_h, 1.f); |
| 114 | } |
| 115 | |
| 116 | float bin_size_w = roi_w / (float)pooled_width; |
| 117 | float bin_size_h = roi_h / (float)pooled_height; |
| 118 | |
| 119 | if (version == 0) |
| 120 | { |
| 121 | // original version |
| 122 | #pragma omp parallel for num_threads(opt.num_threads) |
| 123 | for (int q = 0; q < channels; q++) |
| 124 | { |
| 125 | const float* ptr = bottom_blob.channel(q); |
| 126 | float* outptr = top_blob.channel(q); |
| 127 | |
| 128 | for (int ph = 0; ph < pooled_height; ph++) |
| 129 | { |
| 130 | for (int pw = 0; pw < pooled_width; pw++) |
| 131 | { |
| 132 | // Compute pooling region for this output unit: |
| 133 | // start (included) = ph * roi_height / pooled_height |
| 134 | // end (excluded) = (ph + 1) * roi_height / pooled_height |
| 135 | float hstart = roi_y1 + ph * bin_size_h; |
| 136 | float wstart = roi_x1 + pw * bin_size_w; |
| 137 | float hend = roi_y1 + (ph + 1) * bin_size_h; |
| 138 | float wend = roi_x1 + (pw + 1) * bin_size_w; |
| 139 | |
| 140 | hstart = std::min(std::max(hstart, 0.f), (float)h); |
| 141 | wstart = std::min(std::max(wstart, 0.f), (float)w); |
| 142 | hend = std::min(std::max(hend, 0.f), (float)h); |
| 143 | wend = std::min(std::max(wend, 0.f), (float)w); |
| 144 | |
| 145 | int bin_grid_h = (int)(sampling_ratio > 0 ? sampling_ratio : ceil(hend - hstart)); |
| 146 | int bin_grid_w = (int)(sampling_ratio > 0 ? sampling_ratio : ceil(wend - wstart)); |
| 147 | |
| 148 | bool is_empty = (hend <= hstart) || (wend <= wstart); |
| 149 | int area = bin_grid_h * bin_grid_w; |
| 150 | |
| 151 | float sum = 0.f; |
| 152 | for (int by = 0; by < bin_grid_h; by++) |
| 153 | { |
| 154 | float y = hstart + (by + 0.5f) * bin_size_h / (float)bin_grid_h; |
| 155 | |
| 156 | for (int bx = 0; bx < bin_grid_w; bx++) |
| 157 | { |
| 158 | float x = wstart + (bx + 0.5f) * bin_size_w / (float)bin_grid_w; |
| 159 | |
| 160 | // bilinear interpolate at (x,y) |
| 161 | float v = bilinear_interpolate(ptr, w, h, x, y); |
| 162 | |
| 163 | sum += v; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | outptr[pw] = is_empty ? 0.f : (sum / (float)area); |
| 168 | } |
| 169 | |
| 170 | outptr += pooled_width; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | else if (version == 1) |
| 175 | { |
| 176 | // the version in detectron 2 |
| 177 | int roi_bin_grid_h = (int)(sampling_ratio > 0 ? sampling_ratio : ceil(roi_h / pooled_height)); |
| 178 | int roi_bin_grid_w = (int)(sampling_ratio > 0 ? sampling_ratio : ceil(roi_w / pooled_width)); |
| 179 | |
| 180 | const float count = (float)std::max(roi_bin_grid_h * roi_bin_grid_w, 1); |
| 181 | |
| 182 | #pragma omp parallel for num_threads(opt.num_threads) |
| 183 | for (int q = 0; q < channels; q++) |
| 184 | { |
| 185 | const float* ptr = bottom_blob.channel(q); |
| 186 | float* outptr = top_blob.channel(q); |
| 187 | |
| 188 | for (int ph = 0; ph < pooled_height; ph++) |
| 189 | { |
| 190 | for (int pw = 0; pw < pooled_width; pw++) |
| 191 | { |
| 192 | float sum = 0.f; |
| 193 | for (int by = 0; by < roi_bin_grid_h; by++) |
| 194 | { |
| 195 | float y = roi_y1 + ph * bin_size_h + (by + 0.5f) * bin_size_h / (float)roi_bin_grid_h; |
| 196 | |
| 197 | for (int bx = 0; bx < roi_bin_grid_w; bx++) |
| 198 | { |
| 199 | float x = roi_x1 + pw * bin_size_w + (bx + 0.5f) * bin_size_w / (float)roi_bin_grid_w; |
| 200 | |
| 201 | if (y < -1.0 || y > h || x < -1.0 || x > w) |
| 202 | { |
| 203 | // empty |
| 204 | continue; |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | if (y <= 0) y = 0; |
| 209 | if (x <= 0) x = 0; |
| 210 | |
| 211 | // bilinear interpolate at (x,y) |
| 212 | float v = bilinear_interpolate(ptr, w, h, x, y); |
| 213 | sum += v; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | outptr[pw] = sum / count; |
| 218 | } |
| 219 | |
| 220 | outptr += pooled_width; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | } // namespace ncnn |