-
Notifications
You must be signed in to change notification settings - Fork 617
Convert unknown rank image to 4D image #330
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
Merged
kyleabeauchamp
merged 10 commits into
tensorflow:master
from
facaiy:BUG/image_op_with_tf_dataset
Jul 7, 2019
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2196444
ENH: add *_4D_image method
facaiy b05b3b8
ENH: transform_ops use *_4D_image
facaiy 27de34d
TST: more test case
facaiy 60c3d34
CLN: simpler way to calcualte new_shape
facaiy 9001774
TST: static shape check
facaiy eaf339e
ENH: use tf.control_dependencies
facaiy e3b10c3
CLN: fix code style
facaiy bc097ff
CLN: rename original_shape to new_shape
facaiy fd9e020
CLN: fix typo
facaiy fa4577f
merge upstream/master
facaiy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| # Copyright 2019 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. | ||
| # ============================================================================== | ||
| """Image util ops.""" | ||
|
|
||
| from __future__ import absolute_import | ||
| from __future__ import division | ||
| from __future__ import print_function | ||
|
|
||
| import tensorflow as tf | ||
|
|
||
|
|
||
| def get_ndims(image): | ||
| return image.get_shape().ndims or tf.rank(image) | ||
|
|
||
|
|
||
| @tf.function | ||
| def to_4D_image(image): | ||
| """Convert 2/3/4D image to 4D image. | ||
|
|
||
| Args: | ||
| image: 2/3/4D tensor. | ||
|
|
||
| Returns: | ||
| 4D tensor with the same type. | ||
| """ | ||
| # yapf:disable | ||
| with tf.control_dependencies([ | ||
| tf.debugging.assert_rank_in( | ||
| image, [2, 3, 4], message='`image` must be 2/3/4D tensor') | ||
| ]): | ||
| # yapf: enable | ||
| ndims = image.get_shape().ndims | ||
| if ndims is None: | ||
| return _dynamic_to_4D_image(image) | ||
| elif ndims == 2: | ||
| return image[None, :, :, None] | ||
| elif ndims == 3: | ||
| return image[None, :, :, :] | ||
| else: | ||
| return image | ||
|
|
||
|
|
||
| def _dynamic_to_4D_image(image): | ||
| shape = tf.shape(image) | ||
| original_rank = tf.rank(image) | ||
| # 4D image => [N, H, W, C] or [N, C, H, W] | ||
| # 3D image => [1, H, W, C] or [1, C, H, W] | ||
| # 2D image => [1, H, W, 1] | ||
| left_pad = tf.cast(tf.less_equal(original_rank, 3), dtype=tf.int32) | ||
| right_pad = tf.cast(tf.equal(original_rank, 2), dtype=tf.int32) | ||
| # yapf: disable | ||
| new_shape = tf.concat( | ||
| [tf.ones(shape=left_pad, dtype=tf.int32), | ||
| shape, | ||
| tf.ones(shape=right_pad, dtype=tf.int32)], | ||
| axis=0) | ||
| # yapf: enable | ||
| return tf.reshape(image, new_shape) | ||
|
|
||
|
|
||
| @tf.function | ||
| def from_4D_image(image, ndims): | ||
| """Convert back to an image with `ndims` rank. | ||
|
|
||
| Args: | ||
| image: 4D tensor. | ||
facaiy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ndims: The original rank of the image. | ||
|
|
||
| Returns: | ||
| `ndims`-D tensor with the same type. | ||
| """ | ||
| # yapf:disable | ||
| with tf.control_dependencies([ | ||
| tf.debugging.assert_rank( | ||
| image, 4, message='`image` must be 4D tensor') | ||
| ]): | ||
| # yapf:enable | ||
| if isinstance(ndims, tf.Tensor): | ||
| return _dynamic_from_4D_image(image, ndims) | ||
| elif ndims == 2: | ||
| return tf.squeeze(image, [0, 3]) | ||
| elif ndims == 3: | ||
| return tf.squeeze(image, [0]) | ||
| else: | ||
| return image | ||
|
|
||
|
|
||
| def _dynamic_from_4D_image(image, original_rank): | ||
| shape = tf.shape(image) | ||
| # 4D image <= [N, H, W, C] or [N, C, H, W] | ||
| # 3D image <= [1, H, W, C] or [1, C, H, W] | ||
| # 2D image <= [1, H, W, 1] | ||
| begin = tf.cast(tf.less_equal(original_rank, 3), dtype=tf.int32) | ||
| end = 4 - tf.cast(tf.equal(original_rank, 2), dtype=tf.int32) | ||
| new_shape = shape[begin:end] | ||
| return tf.reshape(image, new_shape) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| # Copyright 2019 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. | ||
| # ============================================================================== | ||
| """Tests for util ops.""" | ||
|
|
||
| from __future__ import absolute_import | ||
| from __future__ import division | ||
| from __future__ import print_function | ||
|
|
||
| import tensorflow as tf | ||
|
|
||
| from tensorflow_addons.image import utils as img_utils | ||
| from tensorflow_addons.utils import test_utils | ||
|
|
||
|
|
||
| @test_utils.run_all_in_graph_and_eager_modes | ||
| class UtilsOpsTest(tf.test.TestCase): | ||
| def test_to_4D_image(self): | ||
| for shape in (2, 4), (2, 4, 1), (1, 2, 4, 1): | ||
| exp = tf.ones(shape=(1, 2, 4, 1)) | ||
| res = img_utils.to_4D_image(tf.ones(shape=shape)) | ||
| # static shape: | ||
| self.assertAllEqual(exp.get_shape(), res.get_shape()) | ||
| self.assertAllEqual(self.evaluate(exp), self.evaluate(res)) | ||
|
|
||
| def test_to_4D_image_with_unknown_shape(self): | ||
| fn = img_utils.to_4D_image.get_concrete_function( | ||
| tf.TensorSpec(shape=None, dtype=tf.float32)) | ||
| for shape in (2, 4), (2, 4, 1), (1, 2, 4, 1): | ||
| exp = tf.ones(shape=(1, 2, 4, 1)) | ||
| res = fn(tf.ones(shape=shape)) | ||
| self.assertAllEqual(self.evaluate(exp), self.evaluate(res)) | ||
|
|
||
| def test_to_4D_image_with_invalid_shape(self): | ||
| errors = (ValueError, tf.errors.InvalidArgumentError) | ||
| with self.assertRaisesRegexp(errors, '`image` must be 2/3/4D tensor'): | ||
| img_utils.to_4D_image(tf.ones(shape=(1,))) | ||
|
|
||
| with self.assertRaisesRegexp(errors, '`image` must be 2/3/4D tensor'): | ||
| img_utils.to_4D_image(tf.ones(shape=(1, 2, 4, 3, 2))) | ||
|
|
||
| def test_from_4D_image(self): | ||
| for shape in (2, 4), (2, 4, 1), (1, 2, 4, 1): | ||
| exp = tf.ones(shape=shape) | ||
| res = img_utils.from_4D_image( | ||
| tf.ones(shape=(1, 2, 4, 1)), len(shape)) | ||
| # static shape: | ||
| self.assertAllEqual(exp.get_shape(), res.get_shape()) | ||
| self.assertAllEqual(self.evaluate(exp), self.evaluate(res)) | ||
|
|
||
| def test_from_4D_image_with_unknown_shape(self): | ||
| for shape in (2, 4), (2, 4, 1), (1, 2, 4, 1): | ||
| exp = tf.ones(shape=shape) | ||
| fn = img_utils.from_4D_image.get_concrete_function( | ||
| tf.TensorSpec(shape=None, dtype=tf.float32), tf.size(shape)) | ||
| res = fn(tf.ones(shape=(1, 2, 4, 1)), tf.size(shape)) | ||
| self.assertAllEqual(self.evaluate(exp), self.evaluate(res)) | ||
|
|
||
| def test_from_4D_image_with_invalid_data(self): | ||
| with self.assertRaises(ValueError): | ||
| self.evaluate( | ||
| img_utils.from_4D_image(tf.ones(shape=(2, 2, 4, 1)), 2)) | ||
|
|
||
| with self.assertRaises(tf.errors.InvalidArgumentError): | ||
| self.evaluate( | ||
| img_utils.from_4D_image( | ||
| tf.ones(shape=(2, 2, 4, 1)), tf.constant(2))) | ||
|
|
||
| def test_from_4D_image_with_invalid_shape(self): | ||
| errors = (ValueError, tf.errors.InvalidArgumentError) | ||
| for rank in 2, tf.constant(2): | ||
| with self.subTest(rank=rank): | ||
| with self.assertRaisesRegexp(errors, | ||
| '`image` must be 4D tensor'): | ||
| img_utils.from_4D_image(tf.ones(shape=(2, 4)), rank) | ||
|
|
||
| with self.assertRaisesRegexp(errors, | ||
| '`image` must be 4D tensor'): | ||
| img_utils.from_4D_image(tf.ones(shape=(2, 4, 1)), rank) | ||
|
|
||
| with self.assertRaisesRegexp(errors, | ||
| '`image` must be 4D tensor'): | ||
| img_utils.from_4D_image( | ||
| tf.ones(shape=(1, 2, 4, 1, 1)), rank) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| tf.test.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.