Skip to content

Solarize_ops for Tensorflow Addons #1340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 21 commits into from
Closed
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
204 changes: 85 additions & 119 deletions tensorflow_addons/image/BUILD
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
licenses(["notice"]) # Apache 2.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not how the BUILD is formatted. Please look at Development tips to know how to auto-format your code.

If you're on Windows, you can use docker to do the same.


package(default_visibility = ["//visibility:public"])
package(default_visibility=["//visibility:public"])

py_library(
name = "image",
srcs = ([
"__init__.py",
"dense_image_warp.py",
"distance_transform.py",
"distort_image_ops.py",
"filters.py",
"transform_ops.py",
"translate_ops.py",
"utils.py",
"sparse_image_warp.py",
"interpolate_spline.py",
"connected_components.py",
"resampler_ops.py",
"compose_ops.py",
]),

name="image",
srcs=(
[
"__init__.py",
"dense_image_warp.py",
"distance_transform.py",
"distort_image_ops.py",
"filters.py",
"transform_ops.py",
"translate_ops.py",
"utils.py",
"sparse_image_warp.py",
"interpolate_spline.py",
"connected_components.py",
"resampler_ops.py",
"solarize_ops.py",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compose_ops.py is missing here.

]),
data = [

":sparse_image_warp_test_data",
"//tensorflow_addons/custom_ops/image:_distort_image_ops.so",
"//tensorflow_addons/custom_ops/image:_image_ops.so",
Expand All @@ -29,141 +32,104 @@ py_library(
)

filegroup(
name = "sparse_image_warp_test_data",
srcs = glob(["test_data/*.png"]),
name="sparse_image_warp_test_data", srcs=glob(["test_data/*.png"]),
)

py_test(
name = "dense_image_warp_test",
size = "small",
srcs = [
"dense_image_warp_test.py",
],
main = "dense_image_warp_test.py",
deps = [
":image",
],
name="dense_image_warp_test",
size="small",
srcs=["dense_image_warp_test.py",],
main="dense_image_warp_test.py",
deps=[":image",],
)

py_test(
name = "distance_transform_ops_test",
size = "small",
srcs = [
"distance_transform_test.py",
],
main = "distance_transform_test.py",
deps = [
":image",
],
name="distance_transform_ops_test",
size="small",
srcs=["distance_transform_test.py",],
main="distance_transform_test.py",
deps=[":image",],
)

py_test(
name = "distort_image_ops_test",
size = "small",
srcs = [
"distort_image_ops_test.py",
],
main = "distort_image_ops_test.py",
deps = [
":image",
],
name="distort_image_ops_test",
size="small",
srcs=["distort_image_ops_test.py",],
main="distort_image_ops_test.py",
deps=[":image",],
)

py_test(
name = "filters_test",
size = "medium",
srcs = [
"filters_test.py",
],
flaky = True,
main = "filters_test.py",
deps = [
":image",
],
name="filters_test",
size="medium",
srcs=["filters_test.py",],
flaky=True,
main="filters_test.py",
deps=[":image",],
)

py_test(
name = "transform_ops_test",
size = "medium",
srcs = [
"transform_ops_test.py",
],
main = "transform_ops_test.py",
deps = [
":image",
],
name="transform_ops_test",
size="medium",
srcs=["transform_ops_test.py",],
main="transform_ops_test.py",
deps=[":image",],
)

py_test(
name = "translate_ops_test",
size = "medium",
srcs = [
"translate_ops_test.py",
],
main = "translate_ops_test.py",
deps = [
":image",
],
name="translate_ops_test",
size="medium",
srcs=["translate_ops_test.py",],
main="translate_ops_test.py",
deps=[":image",],
)

py_test(
name = "utils_test",
size = "small",
srcs = [
"utils_test.py",
],
main = "utils_test.py",
deps = [
":image",
],
name="utils_test",
size="small",
srcs=["utils_test.py",],
main="utils_test.py",
deps=[":image",],
)

py_test(
name = "sparse_image_warp_test",
size = "medium",
srcs = [
"sparse_image_warp_test.py",
],
main = "sparse_image_warp_test.py",
deps = [
":image",
],
name="sparse_image_warp_test",
size="medium",
srcs=["sparse_image_warp_test.py",],
main="sparse_image_warp_test.py",
deps=[":image",],
)

py_test(
name = "interpolate_spline_test",
size = "medium",
srcs = [
"interpolate_spline_test.py",
],
main = "interpolate_spline_test.py",
deps = [
":image",
],
name="interpolate_spline_test",
size="medium",
srcs=["interpolate_spline_test.py",],
main="interpolate_spline_test.py",
deps=[":image",],
)

py_test(
name = "connected_components_test",
size = "medium",
srcs = [
"connected_components_test.py",
],
main = "connected_components_test.py",
deps = [
":image",
],
name="connected_components_test",
size="medium",
srcs=["connected_components_test.py",],
main="connected_components_test.py",
deps=[":image",],
)

py_test(
name = "resampler_ops_test",
size = "medium",
srcs = [
"resampler_ops_test.py",
],
main = "resampler_ops_test.py",
deps = [
":image",
],
name="resampler_ops_test",
size="medium",
srcs=["resampler_ops_test.py",],
main="resampler_ops_test.py",
deps=[":image",],
)

py_test(
name="solarize_ops_test",
size="medium",
srcs=["solarize_ops_test.py",],
main="solarize_ops_test.py",
deps=[":image",],
)

py_test(
Expand Down
2 changes: 2 additions & 0 deletions tensorflow_addons/image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@
from tensorflow_addons.image.transform_ops import rotate
from tensorflow_addons.image.transform_ops import transform
from tensorflow_addons.image.translate_ops import translate
from tensorflow_addons.image.solarize_ops import solarize
from tensorflow_addons.image.solarize_ops import solarize_add
from tensorflow_addons.image.compose_ops import blend
30 changes: 30 additions & 0 deletions tensorflow_addons/image/solarize_ops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
""" This module is used to invert all pixel values above a threshold
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing LICENSE, see any other source file to get the license.

which simply means segmentation. """

import tensorflow as tf


def solarize(image, threshold=128):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing type.

"""Method to solarize the image
image: input image
threshold: threshold value to solarize the image
"""
Comment on lines +8 to +11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use proper indents.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at other source files to see how docstrings are written.

# For each pixel in the image, select the pixel
# if the value is less than the threshold.
# Otherwise, subtract 255 from the pixel.
return tf.where(image < threshold, image, 255 - image)


def solarize_add(image, addition=0, threshold=128):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing type info.

"""Method to add solarize to the image
image: input image
addition: addition amount to add in image
threshold: threshold value to solarize the image
"""
Comment on lines +19 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent properly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at other source files to see how docstrings are written.

# For each pixel in the image less than threshold
# we add 'addition' amount to it and then clip the
# pixel value to be between 0 and 255. The value
# of 'addition' is between -128 and 128.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a check to ensure addition is between -128 and 128.

added_image = tf.cast(image, tf.int64) + addition
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the cast to int64 instead of int32?

added_image = tf.cast(tf.clip_by_value(added_image, 0, 255), tf.uint8)
return tf.where(image < threshold, added_image, image)
30 changes: 30 additions & 0 deletions tensorflow_addons/image/solarize_ops_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Test of solarize_ops"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing LICENSE.

import sys
import pytest
import tensorflow as tf
from tensorflow_addons.image import solarize_ops
from tensorflow_addons.utils import test_utils
from absl.testing import parameterized


@test_utils.run_all_in_graph_and_eager_modes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #1328

class SolarizeOPSTest(tf.test.TestCase, parameterized.TestCase):
"""SolarizeOPSTest class to test the solarize images"""

def test_solarize(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test for solarize_add

if tf.executing_eagerly():
image2 = tf.constant(
[
[255, 255, 255, 255],
[255, 255, 255, 255],
[255, 255, 255, 255],
[255, 255, 255, 255],
],
dtype=tf.uint8,
)
threshold = 10
solarize_img = solarize_ops.solarize(image2, threshold)
self.assertAllEqual(tf.shape(solarize_img), tf.shape(image2))

if __name__ == "__main__":
sys.exit(pytest.main([__file__]))