(batch_size, image_size, num_readers=1,
                    num_preprocess_threads=4)
Loads a batch of imagenet inputs.

  Used as a replacement for inception.image_processing.inputs in
  tensorflow/models in order to get around the use of hard-coded flags in the
  image_processing module.

  Args:
    batch_size: int, batch size.
    image_size: int. The images will be resized bilinearly to shape
        [image_size, image_size].
    num_readers: int, number of preprocessing threads per tower.  Must be a
        multiple of 4.
    num_preprocess_threads: int, number of parallel readers.

  Returns:
    4-D tensor of images of shape [batch_size, image_size, image_size, 3], with
    values in [0, 1].

  Raises:
    IOError: If ImageNet data files cannot be found.
    ValueError: If `num_preprocess_threads is not a multiple of 4 or
        `num_readers` is less than 1.
Loads a batch of imagenet inputs.
Loads a batch of imagenet inputs .
def imagenet_inputs(batch_size, image_size, num_readers=1,
                    num_preprocess_threads=4):
  """Loads a batch of imagenet inputs.

  Used as a replacement for inception.image_processing.inputs in
  tensorflow/models in order to get around the use of hard-coded flags in the
  image_processing module.

  Args:
    batch_size: int, batch size.
    image_size: int. The images will be resized bilinearly to shape
        [image_size, image_size].
    num_readers: int, number of preprocessing threads per tower.  Must be a
        multiple of 4.
    num_preprocess_threads: int, number of parallel readers.

  Returns:
    4-D tensor of images of shape [batch_size, image_size, image_size, 3], with
    values in [0, 1].

  Raises:
    IOError: If ImageNet data files cannot be found.
    ValueError: If `num_preprocess_threads is not a multiple of 4 or
        `num_readers` is less than 1.
  """
  imagenet = imagenet_data.ImagenetData('train')

  with tf.name_scope('batch_processing'):
    data_files = imagenet.data_files()
    if data_files is None:
      raise IOError('No ImageNet data files found')

    # Create filename_queue.
    filename_queue = tf.train.string_input_producer(data_files,
                                                    shuffle=True,
                                                    capacity=16)

    if num_preprocess_threads % 4:
      raise ValueError('Please make num_preprocess_threads a multiple '
                       'of 4 (%d %% 4 != 0).' % num_preprocess_threads)

    if num_readers < 1:
      raise ValueError('Please make num_readers at least 1')

    # Approximate number of examples per shard.
    examples_per_shard = 1024
    # Size the random shuffle queue to balance between good global
    # mixing (more examples) and memory use (fewer examples).
    # 1 image uses 299*299*3*4 bytes = 1MB
    # The default input_queue_memory_factor is 16 implying a shuffling queue
    # size: examples_per_shard * 16 * 1MB = 17.6GB
    input_queue_memory_factor = 16
    min_queue_examples = examples_per_shard * input_queue_memory_factor
    examples_queue = tf.RandomShuffleQueue(
        capacity=min_queue_examples + 3 * batch_size,
        min_after_dequeue=min_queue_examples,
        dtypes=[tf.string])

    # Create multiple readers to populate the queue of examples.
    enqueue_ops = []
    for _ in range(num_readers):
      reader = imagenet.reader()
      _, value = reader.read(filename_queue)
      enqueue_ops.append(examples_queue.enqueue([value]))

    tf.train.queue_runner.add_queue_runner(
        tf.train.queue_runner.QueueRunner(examples_queue, enqueue_ops))
    example_serialized = examples_queue.dequeue()

    images_and_labels = []
    for _ in range(num_preprocess_threads):
      # Parse a serialized Example proto to extract the image and metadata.
      image_buffer, label_index, _, _ = _parse_example_proto(
          example_serialized)
      image = tf.image.decode_jpeg(image_buffer, channels=3)

      # pylint: disable=protected-access
      image = _aspect_preserving_resize(image, image_size + 2)
      image = _central_crop([image], image_size, image_size)[0]
      # pylint: enable=protected-access
      image.set_shape([image_size, image_size, 3])
      image = tf.to_float(image) / 255.0

      images_and_labels.append([image, label_index])

    images, label_index_batch = tf.train.batch_join(
        images_and_labels,
        batch_size=batch_size,
        capacity=2 * num_preprocess_threads * batch_size)

    images = tf.reshape(images, shape=[batch_size, image_size, image_size, 3])

    # Display the training images in the visualizer.
    tf.summary.image('images', images)

    return images, tf.reshape(label_index_batch, [batch_size])
def imagenet_inputs ( batch_size , image_size , num_readers = 1 , num_preprocess_threads = 4 ) : imagenet = imagenet_data . ImagenetData ( 'train' ) with tf . name_scope ( 'batch_processing' ) : data_files = imagenet . data_files ( ) if data_files is None : raise IOError ( 'No ImageNet data files found' ) # Create filename_queue. filename_queue = tf . train . string_input_producer ( data_files , shuffle = True , capacity = 16 ) if num_preprocess_threads % 4 : raise ValueError ( 'Please make num_preprocess_threads a multiple ' 'of 4 (%d %% 4 != 0).' % num_preprocess_threads ) if num_readers < 1 : raise ValueError ( 'Please make num_readers at least 1' ) # Approximate number of examples per shard. examples_per_shard = 1024 # Size the random shuffle queue to balance between good global # mixing (more examples) and memory use (fewer examples). # 1 image uses 299*299*3*4 bytes = 1MB # The default input_queue_memory_factor is 16 implying a shuffling queue # size: examples_per_shard * 16 * 1MB = 17.6GB input_queue_memory_factor = 16 min_queue_examples = examples_per_shard * input_queue_memory_factor examples_queue = tf . RandomShuffleQueue ( capacity = min_queue_examples + 3 * batch_size , min_after_dequeue = min_queue_examples , dtypes = [ tf . string ] ) # Create multiple readers to populate the queue of examples. enqueue_ops = [ ] for _ in range ( num_readers ) : reader = imagenet . reader ( ) _ , value = reader . read ( filename_queue ) enqueue_ops . append ( examples_queue . enqueue ( [ value ] ) ) tf . train . queue_runner . add_queue_runner ( tf . train . queue_runner . QueueRunner ( examples_queue , enqueue_ops ) ) example_serialized = examples_queue . dequeue ( ) images_and_labels = [ ] for _ in range ( num_preprocess_threads ) : # Parse a serialized Example proto to extract the image and metadata. image_buffer , label_index , _ , _ = _parse_example_proto ( example_serialized ) image = tf . image . decode_jpeg ( image_buffer , channels = 3 ) # pylint: disable=protected-access image = _aspect_preserving_resize ( image , image_size + 2 ) image = _central_crop ( [ image ] , image_size , image_size ) [ 0 ] # pylint: enable=protected-access image . set_shape ( [ image_size , image_size , 3 ] ) image = tf . to_float ( image ) / 255.0 images_and_labels . append ( [ image , label_index ] ) images , label_index_batch = tf . train . batch_join ( images_and_labels , batch_size = batch_size , capacity = 2 * num_preprocess_threads * batch_size ) images = tf . reshape ( images , shape = [ batch_size , image_size , image_size , 3 ] ) # Display the training images in the visualizer. tf . summary . image ( 'images' , images ) return images , tf . reshape ( label_index_batch , [ batch_size ] )