ElaineBao commented on a change in pull request #16017: Add RROIAlign URL: https://github.com/apache/incubator-mxnet/pull/16017#discussion_r318891412
########## File path: src/operator/contrib/rroi_align.cc ########## @@ -0,0 +1,316 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * Copyright (c) 2015 by Contributors + * \file rroi_align.cc + * \brief rroi align operator + * \author Yixin Bao + * Forward pass adapted from Caffe2 + * link: https://github.com/pytorch/pytorch/blob/master/caffe2/operators/roi_align_rotated_op.cc + */ +#include "./rroi_align-inl.h" +#include <mshadow/tensor.h> +#include "math.h" + +using std::max; +using std::min; +using std::floor; +using std::ceil; + +namespace mxnet { +namespace op { + +template <typename DType> +struct position_for_bilinear_interpolate { + // 4 positions and corresponding weights for + // computing bilinear interpolation + int pos1, pos2, pos3, pos4; + DType w1, w2, w3, w4; +}; + +template <typename DType> +void pre_calc_for_bilinear_interpolate( + const int height, const int width, const int pooled_height, const int pooled_width, + const int iy_upper, const int ix_upper, DType roi_start_h, DType roi_start_w, + DType bin_size_h, DType bin_size_w, int roi_bin_grid_h, int roi_bin_grid_w, + DType roi_center_h, DType roi_center_w, DType theta, + std::vector<position_for_bilinear_interpolate<DType>> *pre_calc) { + int pre_calc_index = 0; + DType cosTheta = cos(theta); + DType sinTheta = sin(theta); + for (int ph = 0; ph < pooled_height; ph++) { + for (int pw = 0; pw < pooled_width; pw++) { + // calc bin grid position (xx,yy) + for (int iy = 0; iy < iy_upper; iy++) { + const DType yy = roi_start_h + ph * bin_size_h + + static_cast<DType>(iy + .5f) * bin_size_h / + static_cast<DType>(roi_bin_grid_h); // e.g., 0.5, 1.5 + for (int ix = 0; ix < ix_upper; ix++) { + const DType xx = roi_start_w + pw * bin_size_w + + static_cast<DType>(ix + .5f) * bin_size_w / + static_cast<DType>(roi_bin_grid_w); + + // Rotate by theta around the center and translate + DType x = xx * cosTheta + yy * sinTheta + roi_center_w; + DType y = yy * cosTheta - xx * sinTheta + roi_center_h; + + // deal with: inverse elements are out of feature map boundary + if (y < -1.0 || y > height || x < -1.0 || x > width) { + // empty + position_for_bilinear_interpolate<DType> pc; + pc.pos1 = 0; + pc.pos2 = 0; + pc.pos3 = 0; + pc.pos4 = 0; + pc.w1 = 0; + pc.w2 = 0; + pc.w3 = 0; + pc.w4 = 0; + pre_calc->at(pre_calc_index) = pc; + pre_calc_index += 1; + continue; + } + if (y <= 0) { + y = 0; + } + if (x <= 0) { + x = 0; + } + + // calc 4 points for interpolation + int y_low = static_cast<int>(y); + int x_low = static_cast<int>(x); + int y_high; + int x_high; + if (y_low >= height - 1) { + y_high = y_low = height - 1; + y = (DType)y_low; + } else { + y_high = y_low + 1; + } + if (x_low >= width - 1) { + x_high = x_low = width - 1; + x = (DType)x_low; + } else { + x_high = x_low + 1; + } + DType ly = y - y_low; + DType lx = x - x_low; + DType hy = 1. - ly, hx = 1. - lx; + DType w1 = hy * hx, w2 = hy * lx, w3 = ly * hx, w4 = ly * lx; + + // Save weights and indices + position_for_bilinear_interpolate<DType> pc; Review comment: thank you for your advices, @wkcn. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
