提交 a0c63f11 编写于 作者: T tink2123

add align_flag

test=develop
上级 b64cdaf6
...@@ -110,7 +110,7 @@ class InterpolateOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -110,7 +110,7 @@ class InterpolateOpMaker : public framework::OpProtoAndCheckerMaker {
to perform linear interpolation first in one direction, and then to perform linear interpolation first in one direction, and then
again in the other direction. again in the other direction.
Align_corners and align_mode are optinal parameters,The calculation method Align_corners and align_mode are optinal parameters,the calculation method
of interpolation can be selected by them. of interpolation can be selected by them.
Example: Example:
......
...@@ -94,6 +94,7 @@ __global__ void KeBilinearInterpFw( ...@@ -94,6 +94,7 @@ __global__ void KeBilinearInterpFw(
int nthreads = output_h * output_w; int nthreads = output_h * output_w;
int tid = blockIdx.x * blockDim.x + threadIdx.x; int tid = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x * gridDim.x; int stride = blockDim.x * gridDim.x;
bool align_flag = (align_mode == 0 && !align_corners);
for (; tid < nthreads; tid += stride) { for (; tid < nthreads; tid += stride) {
int out_id_h = tid / output_w; int out_id_h = tid / output_w;
int out_id_w = tid % output_w; int out_id_w = tid % output_w;
...@@ -102,24 +103,22 @@ __global__ void KeBilinearInterpFw( ...@@ -102,24 +103,22 @@ __global__ void KeBilinearInterpFw(
int channel_id = out_id_w / out_img_size; int channel_id = out_id_w / out_img_size;
int out_img_idy = (out_id_w % out_img_size) / out_img_w; int out_img_idy = (out_id_w % out_img_size) / out_img_w;
int in_img_idy = (align_mode == 0 && !align_corners) int in_img_idy = align_flag
? static_cast<int>(ratio_h * (out_img_idy + 0.5) - 0.5) ? static_cast<int>(ratio_h * (out_img_idy + 0.5) - 0.5)
: static_cast<int>(ratio_h * out_img_idy); : static_cast<int>(ratio_h * out_img_idy);
in_img_idy = (in_img_idy > 0) ? in_img_idy : 0; in_img_idy = (in_img_idy > 0) ? in_img_idy : 0;
int h_id = (in_img_idy < in_img_h - 1) ? 1 : 0; int h_id = (in_img_idy < in_img_h - 1) ? 1 : 0;
T h1lambda = (align_mode == 0 && !align_corners) T h1lambda = align_flag ? ratio_h * (out_img_idy + 0.5) - 0.5 - in_img_idy
? ratio_h * (out_img_idy + 0.5) - 0.5 - in_img_idy
: ratio_h * out_img_idy - in_img_idy; : ratio_h * out_img_idy - in_img_idy;
T h2lambda = 1.f - h1lambda; T h2lambda = 1.f - h1lambda;
int out_img_idx = tid % out_img_w; int out_img_idx = tid % out_img_w;
int in_img_idx = (align_mode == 0 && !align_corners) int in_img_idx = align_flag
? static_cast<int>(ratio_w * (out_img_idx + 0.5) - 0.5) ? static_cast<int>(ratio_w * (out_img_idx + 0.5) - 0.5)
: static_cast<int>(ratio_w * out_img_idx); : static_cast<int>(ratio_w * out_img_idx);
in_img_idx = (in_img_idx > 0) ? in_img_idx : 0; in_img_idx = (in_img_idx > 0) ? in_img_idx : 0;
int w_id = (in_img_idx < in_img_w - 1) ? 1 : 0; int w_id = (in_img_idx < in_img_w - 1) ? 1 : 0;
T w1lambda = (align_mode == 0 && !align_corners) T w1lambda = align_flag ? ratio_w * (out_img_idx + 0.5) - 0.5 - in_img_idx
? ratio_w * (out_img_idx + 0.5) - 0.5 - in_img_idx
: ratio_w * out_img_idx - in_img_idx; : ratio_w * out_img_idx - in_img_idx;
T w2lambda = 1.f - w1lambda; T w2lambda = 1.f - w1lambda;
...@@ -144,6 +143,7 @@ __global__ void KeBilinearInterpBw( ...@@ -144,6 +143,7 @@ __global__ void KeBilinearInterpBw(
int nthreads = output_h * output_w; int nthreads = output_h * output_w;
int tid = blockIdx.x * blockDim.x + threadIdx.x; int tid = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x * gridDim.x; int stride = blockDim.x * gridDim.x;
bool align_flag = (align_mode == 0 && !align_corners);
for (; tid < nthreads; tid += stride) { for (; tid < nthreads; tid += stride) {
int out_id_h = tid / output_w; int out_id_h = tid / output_w;
int out_id_w = tid % output_w; int out_id_w = tid % output_w;
...@@ -152,25 +152,21 @@ __global__ void KeBilinearInterpBw( ...@@ -152,25 +152,21 @@ __global__ void KeBilinearInterpBw(
int channel_id = out_id_w / out_img_size; int channel_id = out_id_w / out_img_size;
int out_img_idy = (out_id_w % out_img_size) / out_img_w; int out_img_idy = (out_id_w % out_img_size) / out_img_w;
int in_img_idy = (align_mode == 0 && !align_corners) int in_img_idy = align_flag ? ratio_h * (out_img_idy + 0.5) - 0.5
? ratio_h * (out_img_idy + 0.5) - 0.5
: ratio_h * out_img_idy; : ratio_h * out_img_idy;
in_img_idy = (in_img_idy > 0) ? in_img_idy : 0; in_img_idy = (in_img_idy > 0) ? in_img_idy : 0;
int h_id = (in_img_idy < in_img_h - 1) ? 1 : 0; int h_id = (in_img_idy < in_img_h - 1) ? 1 : 0;
T h1lambda = (align_mode == 0 && !align_corners) T h1lambda = align_flag ? ratio_h * (out_img_idy + 0.5) - 0.5 - in_img_idy
? ratio_h * (out_img_idy + 0.5) - 0.5 - in_img_idy
: ratio_h * out_img_idy - in_img_idy; : ratio_h * out_img_idy - in_img_idy;
T h2lambda = 1.f - h1lambda; T h2lambda = 1.f - h1lambda;
int out_img_idx = tid % out_img_w; int out_img_idx = tid % out_img_w;
int in_img_idx = (align_mode == 0 && !align_corners) int in_img_idx = align_flag ? ratio_w * (out_img_idx + 0.5) - 0.5
? ratio_w * (out_img_idx + 0.5) - 0.5
: ratio_w * out_img_idx; : ratio_w * out_img_idx;
in_img_idx = (in_img_idx > 0) ? in_img_idx : 0; in_img_idx = (in_img_idx > 0) ? in_img_idx : 0;
int w_id = (in_img_idx < in_img_w - 1) ? 1 : 0; int w_id = (in_img_idx < in_img_w - 1) ? 1 : 0;
T w1lambda = (align_mode == 0 && !align_corners) T w1lambda = align_flag ? ratio_w * (out_img_idx + 0.5) - 0.5 - in_img_idx
? ratio_w * (out_img_idx + 0.5) - 0.5 - in_img_idx
: ratio_w * out_img_idx - in_img_idx; : ratio_w * out_img_idx - in_img_idx;
T w2lambda = 1.f - w1lambda; T w2lambda = 1.f - w1lambda;
......
...@@ -56,15 +56,14 @@ static void BilinearInterpolation(const Tensor& input, Tensor* output, ...@@ -56,15 +56,14 @@ static void BilinearInterpolation(const Tensor& input, Tensor* output,
const bool align_mode) { const bool align_mode) {
auto input_t = EigenTensor<T, 4>::From(input); auto input_t = EigenTensor<T, 4>::From(input);
auto output_t = EigenTensor<T, 4>::From(*output); auto output_t = EigenTensor<T, 4>::From(*output);
bool align_flag = (align_mode == 0 && !align_corners);
for (int k = 0; k < out_h; k++) { // loop for images for (int k = 0; k < out_h; k++) { // loop for images
int y_n = (align_mode == 0 && !align_corners) int y_n = align_flag ? static_cast<int>(ratio_h * (k + 0.5) - 0.5)
? static_cast<int>(ratio_h * (k + 0.5) - 0.5)
: static_cast<int>(ratio_h * k); : static_cast<int>(ratio_h * k);
y_n = (y_n > 0) ? y_n : 0; y_n = (y_n > 0) ? y_n : 0;
int y_s = (y_n + 1) < (in_h - 1) ? (y_n + 1) : (in_h - 1); int y_s = (y_n + 1) < (in_h - 1) ? (y_n + 1) : (in_h - 1);
float d_n = (align_mode == 0 && !align_corners) float d_n =
? ratio_h * (k + 0.5) - 0.5 - y_n align_flag ? ratio_h * (k + 0.5) - 0.5 - y_n : ratio_h * k - y_n;
: ratio_h * k - y_n;
float d_s = 1.f - d_n; float d_s = 1.f - d_n;
for (int l = 0; l < out_w; l++) { for (int l = 0; l < out_w; l++) {
...@@ -73,9 +72,8 @@ static void BilinearInterpolation(const Tensor& input, Tensor* output, ...@@ -73,9 +72,8 @@ static void BilinearInterpolation(const Tensor& input, Tensor* output,
: static_cast<int>(ratio_w * l); : static_cast<int>(ratio_w * l);
x_w = (x_w > 0) ? x_w : 0; x_w = (x_w > 0) ? x_w : 0;
int x_e = (x_w + 1) < (in_w - 1) ? (x_w + 1) : (in_w - 1); int x_e = (x_w + 1) < (in_w - 1) ? (x_w + 1) : (in_w - 1);
float d_w = (align_mode == 0 && !align_corners) float d_w =
? ratio_w * (l + 0.5) - 0.5 - x_w align_flag ? ratio_w * (l + 0.5) - 0.5 - x_w : ratio_w * l - x_w;
: ratio_w * l - x_w;
float d_e = 1.f - d_w; float d_e = 1.f - d_w;
for (int i = 0; i < n; i++) { // loop for batches for (int i = 0; i < n; i++) { // loop for batches
...@@ -126,26 +124,23 @@ static void BilinearInterpolationGrad(const Tensor& output_grad, ...@@ -126,26 +124,23 @@ static void BilinearInterpolationGrad(const Tensor& output_grad,
const int align_mode) { const int align_mode) {
auto input_grad_t = EigenTensor<T, 4>::From(*input_grad); auto input_grad_t = EigenTensor<T, 4>::From(*input_grad);
auto output_grad_t = EigenTensor<T, 4>::From(output_grad); auto output_grad_t = EigenTensor<T, 4>::From(output_grad);
bool align_flag = (align_mode == 0 && !align_corners);
for (int k = 0; k < out_h; k++) { // loop for images for (int k = 0; k < out_h; k++) { // loop for images
int y_n = (align_mode == 0 && !align_corners) int y_n = align_flag ? static_cast<int>(ratio_h * (k + 0.5) - 0.5)
? static_cast<int>(ratio_h * (k + 0.5) - 0.5)
: static_cast<int>(ratio_h * k); : static_cast<int>(ratio_h * k);
y_n = (y_n > 0) ? y_n : 0; y_n = (y_n > 0) ? y_n : 0;
int y_s = (y_n + 1) < (in_h - 1) ? (y_n + 1) : (in_h - 1); int y_s = (y_n + 1) < (in_h - 1) ? (y_n + 1) : (in_h - 1);
float d_n = (align_mode == 0 && !align_corners) float d_n =
? ratio_h * (k + 0.5) - 0.5 - y_n align_flag ? ratio_h * (k + 0.5) - 0.5 - y_n : ratio_h * k - y_n;
: ratio_h * k - y_n;
float d_s = 1.f - d_n; float d_s = 1.f - d_n;
for (int l = 0; l < out_w; l++) { for (int l = 0; l < out_w; l++) {
int x_w = (align_mode == 0 && !align_corners) int x_w = align_flag ? static_cast<int>(ratio_w * (l + 0.5) - 0.5)
? static_cast<int>(ratio_w * (l + 0.5) - 0.5)
: static_cast<int>(ratio_w * l); : static_cast<int>(ratio_w * l);
x_w = (x_w > 0) ? x_w : 0; x_w = (x_w > 0) ? x_w : 0;
int x_e = (x_w + 1) < (in_w - 1) ? (x_w + 1) : (in_w - 1); int x_e = (x_w + 1) < (in_w - 1) ? (x_w + 1) : (in_w - 1);
float d_w = (align_mode == 0 && !align_corners) float d_w =
? ratio_w * (l + 0.5) - 0.5 - x_w align_flag ? ratio_w * (l + 0.5) - 0.5 - x_w : ratio_w * l - x_w;
: ratio_w * l - x_w;
float d_e = 1.f - d_w; float d_e = 1.f - d_w;
for (int i = 0; i < n; i++) { // loop for batches for (int i = 0; i < n; i++) { // loop for batches
......
...@@ -6552,7 +6552,7 @@ def image_resize(input, ...@@ -6552,7 +6552,7 @@ def image_resize(input,
to perform linear interpolation first in one direction, and then to perform linear interpolation first in one direction, and then
again in the other direction. again in the other direction.
Align_corners and align_mode are optinal parameters,The calculation method Align_corners and align_mode are optinal parameters,the calculation method
of interpolation can be selected by them. of interpolation can be selected by them.
Example: Example:
...@@ -6758,11 +6758,11 @@ def resize_bilinear(input, ...@@ -6758,11 +6758,11 @@ def resize_bilinear(input,
For details of bilinear interpolation, please refer to Wikipedia: For details of bilinear interpolation, please refer to Wikipedia:
https://en.wikipedia.org/wiki/Bilinear_interpolation https://en.wikipedia.org/wiki/Bilinear_interpolation
Align_corners and align_mode are optinal parameters,The calculation Align_corners and align_mode are optinal parameters,the calculation
method of interpolation can be selected by them. method of interpolation can be selected by them.
Align_corners and align_mode are optinal parameters,The calculation method Align_corners and align_mode are optinal parameters,the calculation method
of interpolation can be selected by them. of interpolation can be selected by them.
Example: Example:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册