aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/kernel_tests/decode_image_op_test.py
blob: 51cdedcf0fb58a9ee6e66c4dd802cc8657f089fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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

# Double-quote usage here is intentional to make internal path rewriting easier.
prefix_path = os.path.join("tensorflow", "core", "lib")

class DecodeImageOpTest(tf.test.TestCase):

  def testGif(self):
    # Read some real GIFs
    path = os.path.join(prefix_path, '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(prefix_path, '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
    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 = os.path.join(prefix_path, 'png', 'testdata', filename)
          png0 = tf.read_file(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()