reader.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright 2016 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. """Read image sequence."""
  16. import tensorflow as tf
  17. def SequenceToImageAndDiff(images):
  18. """Convert image sequence batch into image and diff batch.
  19. Each image pair is converted to the first image and their diff.
  20. Batch size will increase if sequence length is larger than 2.
  21. Args:
  22. images: Image sequence with shape
  23. [batch_size, seq_len, image_size, image_size, channel]
  24. Returns:
  25. the list of (image, diff) tuples with shape
  26. [batch_size2, image_size, image_size, channel]. image_sizes are
  27. [32, 64, 128, 256].
  28. """
  29. image_diff_list = []
  30. image_seq = tf.unstack(images, axis=1)
  31. for size in [32, 64, 128, 256]:
  32. resized_images = [
  33. tf.image.resize_images(i, [size, size]) for i in image_seq]
  34. diffs = []
  35. for i in xrange(0, len(resized_images)-1):
  36. diffs.append(resized_images[i+1] - resized_images[i])
  37. image_diff_list.append(
  38. (tf.concat(axis=0, values=resized_images[:-1]), tf.concat(axis=0, values=diffs)))
  39. return image_diff_list
  40. def ReadInput(data_filepattern, shuffle, params):
  41. """Read the tf.SequenceExample tfrecord files.
  42. Args:
  43. data_filepattern: tf.SequenceExample tfrecord filepattern.
  44. shuffle: Whether to shuffle the examples.
  45. params: parameter dict.
  46. Returns:
  47. image sequence batch [batch_size, seq_len, image_size, image_size, channel].
  48. """
  49. image_size = params['image_size']
  50. filenames = tf.gfile.Glob(data_filepattern)
  51. filename_queue = tf.train.string_input_producer(filenames, shuffle=shuffle)
  52. reader = tf.TFRecordReader()
  53. _, example = reader.read(filename_queue)
  54. feature_sepc = {
  55. 'moving_objs': tf.FixedLenSequenceFeature(
  56. shape=[image_size * image_size * 3], dtype=tf.float32)}
  57. _, features = tf.parse_single_sequence_example(
  58. example, sequence_features=feature_sepc)
  59. moving_objs = tf.reshape(
  60. features['moving_objs'], [params['seq_len'], image_size, image_size, 3])
  61. if shuffle:
  62. examples = tf.train.shuffle_batch(
  63. [moving_objs],
  64. batch_size=params['batch_size'],
  65. num_threads=64,
  66. capacity=params['batch_size'] * 100,
  67. min_after_dequeue=params['batch_size'] * 4)
  68. else:
  69. examples = tf.train.batch([moving_objs],
  70. batch_size=params['batch_size'],
  71. num_threads=16,
  72. capacity=params['batch_size'])
  73. examples /= params['norm_scale']
  74. return examples