cifar10_input_test.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright 2015 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """Tests for cifar10 input."""
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import os
  20. import tensorflow as tf
  21. import cifar10_input
  22. class CIFAR10InputTest(tf.test.TestCase):
  23. def _record(self, label, red, green, blue):
  24. image_size = 32 * 32
  25. record = bytes(bytearray([label] + [red] * image_size +
  26. [green] * image_size + [blue] * image_size))
  27. expected = [[[red, green, blue]] * 32] * 32
  28. return record, expected
  29. def testSimple(self):
  30. labels = [9, 3, 0]
  31. records = [self._record(labels[0], 0, 128, 255),
  32. self._record(labels[1], 255, 0, 1),
  33. self._record(labels[2], 254, 255, 0)]
  34. contents = b"".join([record for record, _ in records])
  35. expected = [expected for _, expected in records]
  36. filename = os.path.join(self.get_temp_dir(), "cifar")
  37. open(filename, "wb").write(contents)
  38. with self.test_session() as sess:
  39. q = tf.FIFOQueue(99, [tf.string], shapes=())
  40. q.enqueue([filename]).run()
  41. q.close().run()
  42. result = cifar10_input.read_cifar10(q)
  43. for i in range(3):
  44. key, label, uint8image = sess.run([
  45. result.key, result.label, result.uint8image])
  46. self.assertEqual("%s:%d" % (filename, i), tf.compat.as_text(key))
  47. self.assertEqual(labels[i], label)
  48. self.assertAllEqual(expected[i], uint8image)
  49. with self.assertRaises(tf.errors.OutOfRangeError):
  50. sess.run([result.key, result.uint8image])
  51. if __name__ == "__main__":
  52. tf.test.main()