forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add decode_image Op (tensorflow#4222)
* Add decode_image Op * Update decode_image to use tf.substr * Update JPEG magic numbers * Add GIF functionality * Add GIF tests, documentation * Rearrange imports to be alphabetical * Adjust decode_image, tests * Tests now use files in tensorflow/core/lib * decode_image now uses `cond` operations instead of a `case` * There is now only one substr Op for the header of the image contents It's not clean yet- we're still getting errors- just want to push this before I start making more changes. * Move from image_ops.py => image_ops_impl.py * Add gen_image_ops import * Clean implementation, add rgb_to_grayscale for gif * Prevent GIF channels being set to 1, add test * Add test for invalid channel value * Update args/returns documentation * Make changes to try and fix Windows build
- Loading branch information
1 parent
fb04d0f
commit db49056
Showing
5 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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,106 @@ | ||
# 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. | ||
# ============================================================================== | ||
"""Tests for decode_image.""" | ||
|
||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import os.path | ||
import numpy as np | ||
import tensorflow as tf | ||
|
||
|
||
class DecodeImageOpTest(tf.test.TestCase): | ||
|
||
def testGif(self): | ||
# Read some real GIFs | ||
path = os.path.join('tensorflow', 'core', 'lib', 'gif', 'testdata', | ||
'scan.gif') | ||
WIDTH = 20 | ||
HEIGHT = 40 | ||
STRIDE = 5 | ||
shape = (12, HEIGHT, WIDTH, 3) | ||
|
||
with self.test_session(use_gpu=True) as sess: | ||
gif0 = tf.read_file(path) | ||
image0 = tf.image.decode_image(gif0) | ||
image1 = tf.image.decode_gif(gif0) | ||
gif0, image0, image1 = sess.run([gif0, image0, image1]) | ||
|
||
self.assertEqual(image0.shape, shape) | ||
self.assertAllEqual(image0, image1) | ||
|
||
for frame_idx, frame in enumerate(image0): | ||
gt = np.zeros(shape[1:], dtype=np.uint8) | ||
start = frame_idx * STRIDE | ||
end = (frame_idx + 1) * STRIDE | ||
if end <= WIDTH: | ||
gt[:, start:end, :] = 255 | ||
else: | ||
start -= WIDTH | ||
end -= WIDTH | ||
gt[start:end, :, :] = 255 | ||
|
||
self.assertAllClose(frame, gt) | ||
|
||
bad_channels = tf.image.decode_image(gif0, channels=1) | ||
with self.assertRaises(tf.errors.InvalidArgumentError): | ||
bad_channels.eval() | ||
|
||
|
||
def testJpeg(self): | ||
# Read a real jpeg and verify shape | ||
path = os.path.join('tensorflow', 'core', 'lib', 'jpeg', 'testdata', | ||
'jpeg_merge_test1.jpg') | ||
with self.test_session(use_gpu=True) as sess: | ||
jpeg0 = tf.read_file(path) | ||
image0 = tf.image.decode_image(jpeg0) | ||
image1 = tf.image.decode_jpeg(jpeg0) | ||
jpeg0, image0, image1 = sess.run([jpeg0, image0, image1]) | ||
self.assertEqual(len(jpeg0), 3771) | ||
self.assertEqual(image0.shape, (256, 128, 3)) | ||
self.assertAllEqual(image0, image1) | ||
|
||
def testPng(self): | ||
# Read some real PNGs, converting to different channel numbers | ||
prefix = ['tensorflow', 'core', 'lib', 'png', 'testdata'] | ||
inputs = [(1, 'lena_gray.png')] | ||
for channels_in, filename in inputs: | ||
for channels in 0, 1, 3: | ||
with self.test_session(use_gpu=True) as sess: | ||
path = prefix + [filename] | ||
png0 = tf.read_file(os.path.join(*path)) | ||
image0 = tf.image.decode_image(png0, channels=channels) | ||
image1 = tf.image.decode_png(png0, channels=channels) | ||
png0, image0, image1 = sess.run([png0, image0, image1]) | ||
self.assertEqual(image0.shape, (26, 51, channels or channels_in)) | ||
self.assertAllEqual(image0, image1) | ||
|
||
def testInvalidBytes(self): | ||
image_bytes = b'ThisIsNotAnImage!' | ||
decode = tf.image.decode_image(image_bytes) | ||
with self.test_session(): | ||
with self.assertRaises(tf.errors.InvalidArgumentError): | ||
decode.eval() | ||
|
||
def testInvalidChannels(self): | ||
image_bytes = b'unused' | ||
with self.assertRaises(ValueError): | ||
decode = tf.image.decode_image(image_bytes, channels=4) | ||
|
||
|
||
if __name__ == "__main__": | ||
tf.test.main() |
This file contains 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 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