Skip to content

Commit

Permalink
Merge pull request #20746 from nluehr:merge_resize_bilinear
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 204947787
  • Loading branch information
tensorflower-gardener committed Jul 17, 2018
2 parents 6e5a78b + 0534435 commit aee128f
Show file tree
Hide file tree
Showing 4 changed files with 522 additions and 170 deletions.
11 changes: 9 additions & 2 deletions tensorflow/core/kernels/BUILD
Expand Up @@ -557,6 +557,12 @@ cc_header_only_library(
deps = [":image_resizer_state"],
)

cc_library(
name = "crop_resize_bilinear_core",
hdrs = ["crop_resize_bilinear_core.h"],
visibility = ["//visibility:private"],
)

# OpKernel libraries ----------------------------------------------------------

ARRAY_DEPS = [
Expand Down Expand Up @@ -2152,7 +2158,7 @@ tf_kernel_library(
tf_kernel_library(
name = "crop_and_resize_op",
prefix = "crop_and_resize_op",
deps = IMAGE_DEPS,
deps = IMAGE_DEPS + [":crop_resize_bilinear_core"],
)

tf_kernel_library(
Expand Down Expand Up @@ -2218,7 +2224,7 @@ tf_kernel_library(
tf_kernel_library(
name = "resize_bilinear_op",
prefix = "resize_bilinear_op",
deps = IMAGE_DEPS,
deps = IMAGE_DEPS + [":crop_resize_bilinear_core"],
)

tf_kernel_library(
Expand Down Expand Up @@ -4856,6 +4862,7 @@ filegroup(
"concat_op.cc",
"constant_op.cc",
"constant_op.h",
"crop_resize_bilinear_core.h",
"cwise_ops.h",
"cwise_ops_common.cc",
"cwise_ops_common.h",
Expand Down
64 changes: 32 additions & 32 deletions tensorflow/core/kernels/crop_and_resize_op.cc
Expand Up @@ -28,6 +28,7 @@ limitations under the License.
#include "tensorflow/core/framework/tensor_shape.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/kernels/bounds_check.h"
#include "tensorflow/core/kernels/crop_resize_bilinear_core.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/platform/logging.h"
Expand All @@ -42,6 +43,10 @@ limitations under the License.
using stream_executor::cuda::ScopedActivateExecutorContext;
#endif // GOOGLE_CUDA

using ::tensorflow::internal::CachedInterpolation;
using ::tensorflow::internal::compute_interpolation_weights;
using ::tensorflow::internal::crop_resize_single_image;

namespace tensorflow {
namespace {

Expand Down Expand Up @@ -249,39 +254,34 @@ struct CropAndResize<CPUDevice, T> {
continue;
}
if (method_name == "bilinear") {
const int top_y_index = floorf(in_y);
const int bottom_y_index = ceilf(in_y);
const float y_lerp = in_y - top_y_index;

for (int x = 0; x < crop_width; ++x) {
const float in_x = (crop_width > 1)
? x1 * (image_width - 1) + x * width_scale
: 0.5 * (x1 + x2) * (image_width - 1);
if (in_x < 0 || in_x > image_width - 1) {
for (int d = 0; d < depth; ++d) {
crops(b, y, x, d) = extrapolation_value;
}
continue;
}
const int left_x_index = floorf(in_x);
const int right_x_index = ceilf(in_x);
const float x_lerp = in_x - left_x_index;

for (int d = 0; d < depth; ++d) {
const float top_left(static_cast<float>(
image(b_in, top_y_index, left_x_index, d)));
const float top_right(static_cast<float>(
image(b_in, top_y_index, right_x_index, d)));
const float bottom_left(static_cast<float>(
image(b_in, bottom_y_index, left_x_index, d)));
const float bottom_right(static_cast<float>(
image(b_in, bottom_y_index, right_x_index, d)));
const float top = top_left + (top_right - top_left) * x_lerp;
const float bottom =
bottom_left + (bottom_right - bottom_left) * x_lerp;
crops(b, y, x, d) = top + (bottom - top) * y_lerp;
}
CachedInterpolation *interp_x = nullptr, *interp_y = nullptr;
int min_ix, max_ix, min_iy, max_iy;
compute_interpolation_weights(crop_width, image_width, x1, x2,
min_ix, max_ix, interp_x);
compute_interpolation_weights(crop_height, image_height, y1, y2,
min_iy, max_iy, interp_y);

// multiply by depth to avoid multiplication in resize_single_image.
for (int i = min_ix; i <= max_ix; ++i) {
interp_x[i - min_ix].lower *= depth;
interp_x[i - min_ix].upper *= depth;
}

crop_resize_single_image<T, float>(
image.data() + static_cast<int64>(b_in) *
static_cast<int64>(image_height) *
static_cast<int64>(image_width) *
static_cast<int64>(depth),
image_height, image_width, crop_height, crop_width, depth,
min_ix, max_ix, interp_x, min_iy, max_iy, interp_y,
extrapolation_value, false, false,
crops.data() + static_cast<int64>(b) *
static_cast<int64>(crop_height) *
static_cast<int64>(crop_width) *
static_cast<int64>(depth));

delete[] interp_y;
delete[] interp_x;
} else { // method == "nearest"
for (int x = 0; x < crop_width; ++x) {
const float in_x = (crop_width > 1)
Expand Down

0 comments on commit aee128f

Please sign in to comment.