Skip to content

Commit

Permalink
Add ops that perform color transforms (including changing value, satu…
Browse files Browse the repository at this point in the history
…ration and hue) in YIQ space.

PiperOrigin-RevId: 168897736
  • Loading branch information
tensorflower-gardener committed Sep 15, 2017
1 parent 30868ef commit bc68dc8
Show file tree
Hide file tree
Showing 7 changed files with 792 additions and 1 deletion.
1 change: 1 addition & 0 deletions tensorflow/contrib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ py_library(
"//tensorflow/contrib/graph_editor:graph_editor_py",
"//tensorflow/contrib/grid_rnn:grid_rnn_py",
"//tensorflow/contrib/hooks",
"//tensorflow/contrib/image:distort_image_py",
"//tensorflow/contrib/image:image_py",
"//tensorflow/contrib/image:single_image_random_dot_stereograms_py",
"//tensorflow/contrib/imperative",
Expand Down
75 changes: 75 additions & 0 deletions tensorflow/contrib/image/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ cuda_py_test(
size = "medium",
srcs = ["python/kernel_tests/image_ops_test.py"],
additional_deps = [
":distort_image_py",
":image_py",
":single_image_random_dot_stereograms_py",
"//third_party/py/numpy",
Expand All @@ -99,6 +100,80 @@ cuda_py_test(
],
)

tf_custom_op_library(
name = "python/ops/_distort_image_ops.so",
srcs = [
"kernels/adjust_hsv_in_yiq_op.cc",
"ops/distort_image_ops.cc",
],
deps = [
"@protobuf_archive//:protobuf",
],
)

tf_gen_op_libs(
op_lib_names = ["distort_image_ops"],
)

tf_gen_op_wrapper_py(
name = "distort_image_ops",
deps = [":distort_image_ops_op_lib"],
)

cc_library(
name = "distort_image_ops_cc",
srcs = [
"kernels/adjust_hsv_in_yiq_op.cc",
],
deps = [
"//tensorflow/core:framework",
"//tensorflow/core:lib",
"//third_party/eigen3",
],
alwayslink = 1,
)

py_library(
name = "distort_image_py",
srcs = [
"__init__.py",
"python/ops/distort_image_ops.py",
],
data = [":python/ops/_distort_image_ops.so"],
srcs_version = "PY2AND3",
deps = [
":distort_image_ops",
"//tensorflow/contrib/util:util_py",
"//tensorflow/python:framework",
"//tensorflow/python:framework_for_generated_wrappers",
"//tensorflow/python:image_ops",
"//tensorflow/python:platform",
"//tensorflow/python:random_ops",
],
)

cuda_py_test(
name = "distort_image_ops_test",
size = "medium",
srcs = ["python/kernel_tests/distort_image_ops_test.py"],
additional_deps = [
":distort_image_py",
":image_py",
":single_image_random_dot_stereograms_py",
"//third_party/py/numpy",
"//tensorflow/python:client",
"//tensorflow/python:client_testlib",
"//tensorflow/python:control_flow_ops",
"//tensorflow/python:framework_for_generated_wrappers",
"//tensorflow/python:framework_test_lib",
"//tensorflow/python:math_ops",
"//tensorflow/python:platform_test",
"//tensorflow/python:random_ops",
"//tensorflow/python:variables",
"//tensorflow/core:protos_all_py",
],
)

tf_custom_op_library(
name = "python/ops/_single_image_random_dot_stereograms.so",
srcs = [
Expand Down
9 changes: 8 additions & 1 deletion tensorflow/contrib/image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
### API
This module provides functions for image manipulation; currently, only
This module provides functions for image manipulation; currently, chrominance
transformas (including changing saturation and hue) in YIQ space and
projective transforms (including rotation) are supported.
@@angles_to_projective_transforms
@@compose_transforms
@@adjust_yiq_hsv
@@random_yiq_hsv
@@rotate
@@transform
@@bipartite_match
Expand All @@ -31,6 +34,9 @@
from __future__ import print_function

# pylint: disable=line-too-long
from tensorflow.contrib.image.python.ops.distort_image_ops import adjust_hsv_in_yiq
from tensorflow.contrib.image.python.ops.distort_image_ops import random_hsv_in_yiq

from tensorflow.contrib.image.python.ops.image_ops import angles_to_projective_transforms
from tensorflow.contrib.image.python.ops.image_ops import compose_transforms
from tensorflow.contrib.image.python.ops.image_ops import rotate
Expand All @@ -39,5 +45,6 @@

from tensorflow.python.util.all_util import remove_undocumented

# pylint: enable=line-too-long

remove_undocumented(__name__)
172 changes: 172 additions & 0 deletions tensorflow/contrib/image/kernels/adjust_hsv_in_yiq_op.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
Licensed 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.
==============================================================================*/
#include <cmath>
#include <memory>
#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/register_types.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_shape.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/util/work_sharder.h"

namespace tensorflow {

typedef Eigen::ThreadPoolDevice CPUDevice;
typedef Eigen::GpuDevice GPUDevice;

class AdjustHsvInYiqOpBase : public OpKernel {
protected:
explicit AdjustHsvInYiqOpBase(OpKernelConstruction* context)
: OpKernel(context) {}

struct ComputeOptions {
const Tensor* input = nullptr;
const Tensor* delta_h = nullptr;
const Tensor* scale_s = nullptr;
const Tensor* scale_v = nullptr;
Tensor* output = nullptr;
int64 channel_count = 0;
};

virtual void DoCompute(OpKernelContext* context,
const ComputeOptions& options) = 0;

void Compute(OpKernelContext* context) override {
const Tensor& input = context->input(0);
const Tensor& delta_h = context->input(1);
const Tensor& scale_s = context->input(2);
const Tensor& scale_v = context->input(3);
OP_REQUIRES(context, input.dims() >= 3,
errors::InvalidArgument("input must be at least 3-D, got shape",
input.shape().DebugString()));
OP_REQUIRES(context, TensorShapeUtils::IsScalar(delta_h.shape()),
errors::InvalidArgument("delta_h must be scalar: ",
delta_h.shape().DebugString()));
OP_REQUIRES(context, TensorShapeUtils::IsScalar(scale_s.shape()),
errors::InvalidArgument("scale_s must be scalar: ",
scale_s.shape().DebugString()));
OP_REQUIRES(context, TensorShapeUtils::IsScalar(scale_v.shape()),
errors::InvalidArgument("scale_v must be scalar: ",
scale_v.shape().DebugString()));
auto channels = input.dim_size(input.dims() - 1);
OP_REQUIRES(
context, channels == 3,
errors::InvalidArgument("input must have 3 channels but instead has ",
channels, " channels."));

Tensor* output = nullptr;
OP_REQUIRES_OK(context,
context->allocate_output(0, input.shape(), &output));

if (input.NumElements() > 0) {
const int64 channel_count = input.NumElements() / channels;
ComputeOptions options;
options.input = &input;
options.delta_h = &delta_h;
options.scale_s = &scale_s;
options.scale_v = &scale_v;
options.output = output;
options.channel_count = channel_count;
DoCompute(context, options);
}
}
};

template <class Device>
class AdjustHsvInYiqOp;

template <>
class AdjustHsvInYiqOp<CPUDevice> : public AdjustHsvInYiqOpBase {
public:
explicit AdjustHsvInYiqOp(OpKernelConstruction* context)
: AdjustHsvInYiqOpBase(context) {}

void DoCompute(OpKernelContext* context,
const ComputeOptions& options) override {
const Tensor* input = options.input;
Tensor* output = options.output;
const int64 channel_count = options.channel_count;
static const int kChannelSize = 3;
auto input_data = input->shaped<float, 2>({channel_count, kChannelSize});
const float delta_h = options.delta_h->scalar<float>()();
const float scale_s = options.scale_s->scalar<float>()();
const float scale_v = options.scale_v->scalar<float>()();
auto output_data = output->shaped<float, 2>({channel_count, kChannelSize});
const int kCostPerChannel = 10;
const DeviceBase::CpuWorkerThreads& worker_threads =
*context->device()->tensorflow_cpu_worker_threads();
Shard(worker_threads.num_threads, worker_threads.workers, channel_count,
kCostPerChannel,
[channel_count, &input_data, &output_data, delta_h, scale_s, scale_v](
int64 start_channel, int64 end_channel) {
// Using approximate linear transfomation described in:
// https://beesbuzz.biz/code/hsv_color_transforms.php
/** Get the constants from sympy
from sympy import Matrix
from sympy.abc import u, w
# Projection matrix to YIQ. http://en.wikipedia.org/wiki/YIQ
tyiq = Matrix([[0.299, 0.587, 0.114],
[0.596, -0.274, -0.322],
[0.211, -0.523, 0.312]])
# Hue rotation matrix in YIQ space.
hue_proj = Matrix(3,3, [v, 0, 0, 0, vsu, -vsw, 0, vsw, vsu])
m = tyiq.inv() * hue_proj * tyiq
**/
// TODO(huangyp): directly compute the projection matrix from tyiq.
static const float t[kChannelSize][kChannelSize][kChannelSize] = {
{{.299, .701, .16862179492229},
{.587, -.587, .329804745287403},
{.114, -.114, -0.498426540209694}},
{{.299, -.299, -.327963394172371},
{.587, .413, .0346106879248821},
{.114, -.114, .293352706247489}},
{{.299, -.299, 1.24646136576682},
{.587, -.587, -1.04322888291964},
{.114, .886, -.203232482847173}}};
float m[kChannelSize][kChannelSize] = {{0.}};
float su = scale_s * std::cos(delta_h);
float sw = scale_s * std::sin(delta_h);
for (int q_index = 0; q_index < kChannelSize; q_index++) {
for (int p_index = 0; p_index < kChannelSize; p_index++) {
m[q_index][p_index] = scale_v * (t[q_index][p_index][0] +
t[q_index][p_index][1] * su +
t[q_index][p_index][2] * sw);
}
}
// Applying projection matrix to input RGB vectors.
const float* p = input_data.data() + start_channel * kChannelSize;
float* q = output_data.data() + start_channel * kChannelSize;
for (int i = start_channel; i < end_channel; i++) {
for (int q_index = 0; q_index < kChannelSize; q_index++) {
q[q_index] = 0;
for (int p_index = 0; p_index < kChannelSize; p_index++) {
q[q_index] += m[q_index][p_index] * p[p_index];
}
}
p += kChannelSize;
q += kChannelSize;
}
});
}
};

REGISTER_KERNEL_BUILDER(Name("AdjustHsvInYiq").Device(DEVICE_CPU),
AdjustHsvInYiqOp<CPUDevice>);

// TODO(huangyp): add the GPU kernel
} // namespace tensorflow
60 changes: 60 additions & 0 deletions tensorflow/contrib/image/ops/distort_image_ops.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
Licensed 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.
==============================================================================*/

#include "tensorflow/core/framework/common_shape_fns.h"
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/shape_inference.h"

namespace tensorflow {

using shape_inference::InferenceContext;

// --------------------------------------------------------------------------
REGISTER_OP("AdjustHsvInYiq")
.Input("images: T")
.Input("delta_h: float")
.Input("scale_s: float")
.Input("scale_v: float")
.Output("output: T")
.Attr("T: {uint8, int8, int16, int32, int64, half, float, double}")
.SetShapeFn([](InferenceContext* c) {
return shape_inference::UnchangedShapeWithRankAtLeast(c, 3);
})
.Doc(R"Doc(
Adjust the YIQ hue of one or more images.
`images` is a tensor of at least 3 dimensions. The last dimension is
interpretted as channels, and must be three.
We used linear transfomation described in:
beesbuzz.biz/code/hsv_color_transforms.php
The input image is considered in the RGB colorspace. Conceptually, the RGB
colors are first mapped into YIQ space, rotated around the Y channel by
delta_h in radians, multiplying the chrominance channels (I, Q) by scale_s,
multiplying all channels (Y, I, Q) by scale_v, and then remapped back to RGB
colorspace. Each operation described above is a linear transformation.
images: Images to adjust. At least 3-D.
delta_h: A float scale that represents the hue rotation amount, in radians.
Although delta_h can be any float value.
scale_s: A float scale that represents the factor to multiply the saturation by.
scale_s needs to be non-negative.
scale_v: A float scale that represents the factor to multiply the value by.
scale_v needs to be non-negative.
output: The hsv-adjusted image or images. No clipping will be done in this op.
The client can clip them using additional ops in their graph.
)Doc");

} // namespace tensorflow
Loading

0 comments on commit bc68dc8

Please sign in to comment.