Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions source/device/cpu/op/interp/cortex-a/interp_kernel_arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@

#define MIN(a, b) ((a) < (b) ? (a) : (b))

static void linear_coeffs(int w, int outw, int* xofs, float* alpha)
static void linear_coeffs(int w, int outw, int* xofs, float* alpha, int align_corner)
{
double scale = (double)w / outw;

if (align_corner)
{
scale = (double)(w - 1) / (outw - 1);
}
for (int dx = 0; dx < outw; dx++)
{
float fx = (float)((dx)*scale);
float fx = (float)((dx + 0.5) * scale - 0.5);
if (align_corner)
{
fx = (float)(dx * scale);
}

int sx = floor(fx);
fx -= sx;

Expand Down Expand Up @@ -498,7 +506,7 @@ int interp_run(struct tensor* output_tensor, struct tensor* input_tensor, struct
}
}
}
else if (resize_type == 2) // bilinear
else if (resize_type == 2 || resize_type == 4) // bilinear
{
int* buf = (int*)sys_malloc((out_w + out_h + out_w * 2 + out_h * 2) * sizeof(int));

Expand All @@ -508,8 +516,9 @@ int interp_run(struct tensor* output_tensor, struct tensor* input_tensor, struct
float* alpha = (float*)(buf + out_w + out_h); // new float[ow * 2];
float* beta = (float*)(buf + out_w + out_h + out_w * 2); // new float[oh * 2];

linear_coeffs(in_w, out_w, xofs, alpha);
linear_coeffs(in_h, out_h, yofs, beta);
int align_corner = interp_param->resize_type == 2 ? 0 : 1;
linear_coeffs(in_w, out_w, xofs, alpha, align_corner);
linear_coeffs(in_h, out_h, yofs, beta, align_corner);

#pragma omp parallel for num_threads(num_thread)
for (int q = 0; q < in_c; ++q)
Expand Down
35 changes: 23 additions & 12 deletions source/device/cpu/op/interp/interp_ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@

#define INTERP_MIN(a, b) ((a) < (b) ? (a) : (b))

void linear_coeffs(int w, int outw, int* xofs, float* alpha)
static void linear_coeffs(int w, int outw, int* xofs, float* alpha, int align_corner)
{
double scale = (double)w / outw;

if (align_corner)
{
scale = (double)(w - 1) / (outw - 1);
}
for (int dx = 0; dx < outw; dx++)
{
float fx = (float)((dx)*scale);
float fx = (float)((dx + 0.5) * scale - 0.5);
if (align_corner)
{
fx = (float)(dx * scale);
}

int sx = floor(fx);
fx -= sx;

Expand Down Expand Up @@ -193,7 +201,7 @@ int ref_interp_fp32(struct tensor* input_tensor, struct tensor* output_tensor, s
}
}
}
else if (param->resize_type == 2)
else if (param->resize_type == 2 || param->resize_type == 4)
{
float* input = (float*)input_tensor->data;
float* output = (float*)output_tensor->data;
Expand Down Expand Up @@ -222,8 +230,9 @@ int ref_interp_fp32(struct tensor* input_tensor, struct tensor* output_tensor, s
float* alpha = (float*)(buf + param->output_width + param->output_height); //new float[ow * 2];
float* beta = (float*)(buf + param->output_width + param->output_height + param->output_width * 2); //new float[oh * 2];

linear_coeffs(in_w, out_w, xofs, alpha);
linear_coeffs(in_h, out_h, yofs, beta);
int align_corner = param->resize_type == 2 ? 0 : 1;
linear_coeffs(in_w, out_w, xofs, alpha, align_corner);
linear_coeffs(in_h, out_h, yofs, beta, align_corner);

for (int q = 0; q < channel; ++q)
{
Expand Down Expand Up @@ -290,7 +299,7 @@ int ref_interp_int8(struct tensor* input_tensor, struct tensor* output_tensor, s
}
}
}
else if (param->resize_type == 2)
else if (param->resize_type == 2 || param->resize_type == 4)
{
int batch = input_tensor->dims[0];
int channel = input_tensor->dims[1];
Expand All @@ -316,8 +325,9 @@ int ref_interp_int8(struct tensor* input_tensor, struct tensor* output_tensor, s
float* alpha = (float*)(buf + param->output_width + param->output_height); //new float[ow * 2];
float* beta = (float*)(buf + param->output_width + param->output_height + param->output_width * 2); //new float[oh * 2];

linear_coeffs(in_w, out_w, xofs, alpha);
linear_coeffs(in_h, out_h, yofs, beta);
int align_corner = param->resize_type == 2 ? 0 : 1;
linear_coeffs(in_w, out_w, xofs, alpha, align_corner);
linear_coeffs(in_h, out_h, yofs, beta, align_corner);

for (int q = 0; q < channel; ++q)
{
Expand Down Expand Up @@ -398,7 +408,7 @@ int ref_interp_uint8(struct tensor* input_tensor, struct tensor* output_tensor,
}
}
}
else if (param->resize_type == 2)
else if (param->resize_type == 2 || param->resize_type == 4)
{
int batch = input_tensor->dims[0];
int channel = input_tensor->dims[1];
Expand All @@ -424,8 +434,9 @@ int ref_interp_uint8(struct tensor* input_tensor, struct tensor* output_tensor,
float* alpha = (float*)(buf + param->output_width + param->output_height); //new float[ow * 2];
float* beta = (float*)(buf + param->output_width + param->output_height + param->output_width * 2); //new float[oh * 2];

linear_coeffs(in_w, out_w, xofs, alpha);
linear_coeffs(in_h, out_h, yofs, beta);
int align_corner = param->resize_type == 2 ? 0 : 1;
linear_coeffs(in_w, out_w, xofs, alpha, align_corner);
linear_coeffs(in_h, out_h, yofs, beta, align_corner);

for (int q = 0; q < channel; ++q)
{
Expand Down
13 changes: 12 additions & 1 deletion tools/convert_tool/onnx/onnx2tengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2136,6 +2136,17 @@ int load_resize(ir_graph_t* graph, ir_node_t* node, const onnx::NodeProto& onnx_
interp_param->height_scale = 0;
interp_param->width_scale = 0;

int align_corner = 0;
for (int k = 0; k < onnx_node.attribute_size(); k++)
{
const onnx::AttributeProto& attr = onnx_node.attribute(k);
if (attr.name() == "coordinate_transformation_mode")
{
if (attr.s() == "align_corners")
align_corner = 1;
}
}

if (onnx_node.input_size() == 1)
{
for (int k = 0; k < onnx_node.attribute_size(); k++)
Expand Down Expand Up @@ -2198,7 +2209,7 @@ int load_resize(ir_graph_t* graph, ir_node_t* node, const onnx::NodeProto& onnx_
}
else if (mode == "bilinear" || mode == "linear")
{
interp_param->resize_type = 2;
interp_param->resize_type = align_corner == 0 ? 2 : 4;
}

return 0;
Expand Down