示例#1
0
    def train(self,
              sess,
              audio_processor,
              test_set,
              train_set,
              steps_per_checkpoint,
              checkpoint_dir,
              max_epoch=None):
        # Create a queue for each dataset and a coordinator
        # Shuffle queue for the train set, but we don't shuffle too much in order to keep the benefit from
        # having homogeneous sizes in a given batch (files are ordered by size ascending)
        capacity = min(self.batch_size * 10, len(train_set))
        min_after_dequeue = min(self.batch_size * 7,
                                len(train_set) - self.batch_size)
        train_queue = tf.RandomShuffleQueue(
            capacity,
            min_after_dequeue, [tf.int32, tf.int32, tf.int32, tf.int32],
            shapes=[[self.max_input_seq_length, self.input_dim], [],
                    [self.max_target_seq_length], []])
        # Simple FIFO queue for the test set because we don't care to test always in the same order
        capacity = min(self.batch_size * 10, len(test_set))
        test_queue = tf.FIFOQueue(
            capacity, [tf.int32, tf.int32, tf.int32, tf.int32],
            shapes=[[self.max_input_seq_length, self.input_dim], [],
                    [self.max_target_seq_length], []])
        coord = tf.train.Coordinator()

        # Define the enqueue operation for training data
        train_mfcc_input = tf.placeholder(
            tf.int32, shape=[self.max_input_seq_length, self.input_dim])
        train_mfcc_input_length = tf.placeholder(tf.int32, shape=[])
        train_label = tf.placeholder(tf.int32,
                                     shape=[self.max_target_seq_length])
        train_label_length = tf.placeholder(tf.int32, shape=[])
        train_enqueue_op = train_queue.enqueue([
            train_mfcc_input, train_mfcc_input_length, train_label,
            train_label_length
        ])
        train_dequeue_op = train_queue.dequeue_many(self.batch_size)

        # Define the enqueue operation for test data
        test_mfcc_input = tf.placeholder(
            tf.int32, shape=[self.max_input_seq_length, self.input_dim])
        test_mfcc_input_length = tf.placeholder(tf.int32, shape=[])
        test_label = tf.placeholder(tf.int32,
                                    shape=[self.max_target_seq_length])
        test_label_length = tf.placeholder(tf.int32, shape=[])
        test_enqueue_op = test_queue.enqueue([
            test_mfcc_input, test_mfcc_input_length, test_label,
            test_label_length
        ])
        test_dequeue_op = test_queue.dequeue_many(self.batch_size)

        # Calculate approximate position for learning batch, allow to keep consistency between multiple iterations
        # of the same training job (will default to 0 if it is the first launch because global_step will be 0)
        start_from = self.global_step.eval() * self.batch_size
        logging.info("Start training from file number : %d", start_from)

        # Create the threads
        thread_local_data = threading.local()
        threads = [
            threading.Thread(name="train_enqueue",
                             target=self.enqueue_data,
                             args=(coord, sess, audio_processor,
                                   thread_local_data, train_enqueue_op,
                                   train_set, train_mfcc_input,
                                   train_mfcc_input_length, train_label,
                                   train_label_length, start_from)),
            threading.Thread(
                name="test_enqueue",
                target=self.enqueue_data,
                args=(coord, sess, audio_processor, thread_local_data,
                      test_enqueue_op, test_set, test_mfcc_input,
                      test_mfcc_input_length, test_label, test_label_length))
        ]
        for t in threads:
            t.start()

        previous_loss = 0
        no_improvement_since = 0
        step_time, mean_loss = 0.0, 0.0
        current_step = 1

        # Main training loop
        while True:
            if coord.should_stop():
                break

            start_time = time.time()

            input_feat_vecs, mfcc_lengths_batch, label_values_batch, label_indices_batch =\
                self.dequeue_data(sess, train_dequeue_op)

            step_loss = self.step(sess,
                                  input_feat_vecs,
                                  mfcc_lengths_batch,
                                  label_values_batch,
                                  label_indices_batch,
                                  forward_only=False)

            # Decrease learning rate if no improvement was seen over last 6 steps
            if step_loss >= previous_loss:
                no_improvement_since += 1
                if no_improvement_since == 6:
                    sess.run(self.learning_rate_decay_op)
                    no_improvement_since = 0
                    if self.learning_rate.eval() < 1e-7:
                        # End learning process
                        break
            else:
                no_improvement_since = 0
            previous_loss = step_loss

            # Step result
            logging.info("Step %d with loss %.4f", current_step, step_loss)
            step_time += (time.time() - start_time) / steps_per_checkpoint
            mean_loss += step_loss / steps_per_checkpoint

            # Check if we are at a checkpoint
            if current_step % steps_per_checkpoint == 0:
                logging.info(
                    "Global step %d / learning rate %.4f / step-time %.2f / loss %.2f",
                    self.global_step.eval(), self.learning_rate.eval(),
                    step_time, mean_loss)
                num_test_batches = self.get_num_batches(test_set)
                self.run_checkpoint(sess, checkpoint_dir, num_test_batches,
                                    test_dequeue_op)
                step_time, mean_loss = 0.0, 0.0

            current_step += 1
            if (max_epoch is not None) and (current_step > max_epoch):
                # We have reached the maximum allowed, we should exit at the end of this run
                break

        # Ask the threads to stop.
        coord.request_stop()
        # And wait for them to actually do it.
        coord.join(threads)
示例#2
0
def prefetch_input_data(reader,
                        file_pattern,
                        is_training,
                        batch_size,
                        values_per_shard,
                        input_queue_capacity_factor=16,
                        num_reader_threads=1,
                        shard_queue_name="filename_queue",
                        value_queue_name="input_queue"):
    """Prefetches string values from disk into an input queue.

	In training the capacity of the queue is important because a larger queue
	means better mixing of training examples between shards. The minimum number of
	values kept in the queue is values_per_shard * input_queue_capacity_factor,
	where input_queue_memory factor should be chosen to trade-off better mixing
	with memory usage.

	Args:
		reader: Instance of tf.ReaderBase.
		file_pattern: Comma-separated list of file patterns (e.g.
				/tmp/train_data-?????-of-00100).
		is_training: Boolean; whether prefetching for training or eval.
		batch_size: Model batch size used to determine queue capacity.
		values_per_shard: Approximate number of values per shard.
		input_queue_capacity_factor: Minimum number of values to keep in the queue
			in multiples of values_per_shard. See comments above.
		num_reader_threads: Number of reader threads to fill the queue.
		shard_queue_name: Name for the shards filename queue.
		value_queue_name: Name for the values input queue.

	Returns:
		A Queue containing prefetched string values.
	"""
    data_files = []
    for pattern in file_pattern.split(","):
        data_files.extend(tf.gfile.Glob(pattern))
    if not data_files:
        tf.logging.fatal("Found no input files matching %s", file_pattern)
    else:
        tf.logging.info("Prefetching values from %d files matching %s",
                        len(data_files), file_pattern)

    if is_training:
        filename_queue = tf.train.string_input_producer(data_files,
                                                        shuffle=True,
                                                        capacity=16,
                                                        name=shard_queue_name)
        min_queue_examples = values_per_shard * input_queue_capacity_factor
        capacity = min_queue_examples + 100 * batch_size
        values_queue = tf.RandomShuffleQueue(
            capacity=capacity,
            min_after_dequeue=min_queue_examples,
            dtypes=[tf.string],
            name="random_" + value_queue_name)
    else:
        filename_queue = tf.train.string_input_producer(data_files,
                                                        shuffle=False,
                                                        capacity=1,
                                                        name=shard_queue_name)
        capacity = values_per_shard + 3 * batch_size
        values_queue = tf.FIFOQueue(capacity=capacity,
                                    dtypes=[tf.string],
                                    name="fifo_" + value_queue_name)

    enqueue_ops = []
    for _ in range(num_reader_threads):
        _, value = reader.read(filename_queue)
        enqueue_ops.append(values_queue.enqueue([value]))
    tf.train.queue_runner.add_queue_runner(
        tf.train.queue_runner.QueueRunner(values_queue, enqueue_ops))
    tf.summary.scalar(
        "queue/%s/fraction_of_%d_full" % (values_queue.name, capacity),
        tf.cast(values_queue.size(), tf.float32) * (1. / capacity))

    return values_queue
示例#3
0
from __future__ import print_function
import tensorflow as tf

sess = tf.InteractiveSession()

gen_random_normal = tf.random_normal(shape=())
queue = tf.RandomShuffleQueue(capacity=100, dtypes=[tf.float32], min_after_dequeue=1)
enqueue_op = queue.enqueue(gen_random_normal)

qr = tf.train.QueueRunner(queue, [enqueue_op] * 4)
coord = tf.train.Coordinator()
enqueue_threads = qr.create_threads(sess, coord=coord, start=True)
coord.request_stop()
coord.join(enqueue_threads)
示例#4
0
def build_input(dataset, data_path, batch_size, mode):
    """Build CIFAR image and labels.

  Args:
    dataset: Either 'cifar10' or 'cifar100'.
    data_path: Filename for data.
    batch_size: Input batch size.
    mode: Either 'train' or 'eval'.
  Returns:
    images: Batches of images. [batch_size, image_size, image_size, 3]
    labels: Batches of labels. [batch_size, num_classes]
  Raises:
    ValueError: when the specified dataset is not supported.
  """
    image_size = 32
    if dataset == 'cifar10':
        label_bytes = 1
        label_offset = 0
        num_classes = 10
    elif dataset == 'cifar100':
        label_bytes = 1
        label_offset = 1
        num_classes = 100
    else:
        raise ValueError('Not supported dataset %s', dataset)

    depth = 3
    image_bytes = image_size * image_size * depth
    record_bytes = label_bytes + label_offset + image_bytes

    data_files = tf.gfile.Glob(data_path)
    file_queue = tf.train.string_input_producer(data_files, shuffle=True)
    # Read examples from files in the filename queue.
    reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
    _, value = reader.read(file_queue)

    # Convert these examples to dense labels and processed images.
    record = tf.reshape(tf.decode_raw(value, tf.uint8), [record_bytes])
    label = tf.cast(tf.slice(record, [label_offset], [label_bytes]), tf.int32)
    # Convert from string to [depth * height * width] to [depth, height, width].
    depth_major = tf.reshape(
        tf.slice(record, [label_offset + label_bytes], [image_bytes]),
        [depth, image_size, image_size])
    # Convert from [depth, height, width] to [height, width, depth].
    image = tf.cast(tf.transpose(depth_major, [1, 2, 0]), tf.float32)

    if mode == 'train':
        image = tf.image.resize_image_with_crop_or_pad(image, image_size + 4,
                                                       image_size + 4)
        image = tf.random_crop(image, [image_size, image_size, 3])
        image = tf.image.random_flip_left_right(image)
        # Brightness/saturation/constrast provides small gains .2%~.5% on cifar.
        # image = tf.image.random_brightness(image, max_delta=63. / 255.)
        # image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
        # image = tf.image.random_contrast(image, lower=0.2, upper=1.8)
        image = tf.image.per_image_standardization(image)

        example_queue = tf.RandomShuffleQueue(
            capacity=16 * batch_size,
            min_after_dequeue=8 * batch_size,
            dtypes=[tf.float32, tf.int32],
            shapes=[[image_size, image_size, depth], [1]])
        num_threads = 16
    else:
        image = tf.image.resize_image_with_crop_or_pad(image, image_size,
                                                       image_size)
        image = tf.image.per_image_standardization(image)

        example_queue = tf.FIFOQueue(3 * batch_size,
                                     dtypes=[tf.float32, tf.int32],
                                     shapes=[[image_size, image_size, depth],
                                             [1]])
        num_threads = 1

    example_enqueue_op = example_queue.enqueue([image, label])
    tf.train.add_queue_runner(
        tf.train.queue_runner.QueueRunner(example_queue,
                                          [example_enqueue_op] * num_threads))

    # Read 'batch' labels + images from the example queue.
    images, labels = example_queue.dequeue_many(batch_size)
    labels = tf.reshape(labels, [batch_size, 1])
    indices = tf.reshape(tf.range(0, batch_size, 1), [batch_size, 1])
    labels = tf.sparse_to_dense(tf.concat(values=[indices, labels], axis=1),
                                [batch_size, num_classes], 1.0, 0.0)

    assert len(images.get_shape()) == 4
    assert images.get_shape()[0] == batch_size
    assert images.get_shape()[-1] == 3
    assert len(labels.get_shape()) == 2
    assert labels.get_shape()[0] == batch_size
    assert labels.get_shape()[1] == num_classes

    # Display the training images in the visualizer.
    tf.summary.image('images', images)
    return images, labels
    def __init__(self,
                 dataset1,
                 dataset2,
                 shuffle=True,
                 num_epochs=None,
                 common_queue_capacity=4096,
                 common_queue_min=1024,
                 seed=None):

        if seed is None:
            seed = np.random.randint(10e8)

        _, data_source = parallel_reader.parallel_read(
            dataset1.data_sources,
            reader_class=dataset1.reader,
            num_epochs=num_epochs,
            num_readers=1,
            shuffle=False,
            capacity=common_queue_capacity,
            min_after_dequeue=common_queue_min,
            seed=seed)

        data_target = ""
        if dataset2 is not None:
            _, data_target = parallel_reader.parallel_read(
                dataset2.data_sources,
                reader_class=dataset2.reader,
                num_epochs=num_epochs,
                num_readers=1,
                shuffle=False,
                capacity=common_queue_capacity,
                min_after_dequeue=common_queue_min,
                seed=seed)

        # Optionally shuffle the data
        if shuffle:
            shuffle_queue = tf.RandomShuffleQueue(
                capacity=common_queue_capacity,
                min_after_dequeue=common_queue_min,
                dtypes=[tf.string, tf.string],
                seed=seed)
            enqueue_ops = []
            enqueue_ops.append(
                shuffle_queue.enqueue([data_source, data_target]))
            tf.train.add_queue_runner(
                tf.train.QueueRunner(shuffle_queue, enqueue_ops))
            data_source, data_target = shuffle_queue.dequeue()

        # Decode source items
        items = dataset1.decoder.list_items()
        tensors = dataset1.decoder.decode(data_source, items)

        if dataset2 is not None:
            # Decode target items
            items2 = dataset2.decoder.list_items()
            tensors2 = dataset2.decoder.decode(data_target, items2)

            # Merge items and results
            items = items + items2
            tensors = tensors + tensors2

        super(ParallelDataProvider,
              self).__init__(items_to_tensors=dict(zip(items, tensors)),
                             num_samples=dataset1.num_samples)
示例#6
0
def batch_inputs(data_dir, batch_size, image_size, train, num_preprocess_threads=4,
                 num_readers=1, input_queue_memory_factor=16):
  with tf.name_scope('batch_processing'):

    if train:
        files = data_files(data_dir, 'train')
        filename_queue = tf.train.string_input_producer(files,
                                                        shuffle=True,
                                                        capacity=16)
    else:
        files = data_files(data_dir, 'validation')
        filename_queue = tf.train.string_input_producer(files,
                                                        shuffle=False,
                                                        capacity=1)
    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
    min_queue_examples = examples_per_shard * input_queue_memory_factor
    if train:
      examples_queue = tf.RandomShuffleQueue(
          capacity=min_queue_examples + 3 * batch_size,
          min_after_dequeue=min_queue_examples,
          dtypes=[tf.string])
    else:
      examples_queue = tf.FIFOQueue(
          capacity=examples_per_shard + 3 * batch_size,
          dtypes=[tf.string])

    # Create multiple readers to populate the queue of examples.
    if num_readers > 1:
      enqueue_ops = []
      for _ in range(num_readers):
        reader = tf.TFRecordReader()
        _, 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()
    else:
      reader = tf.TFRecordReader()
      _, example_serialized = reader.read(filename_queue)

    images_labels_fnames = []
    for thread_id in range(num_preprocess_threads):
      # Parse a serialized Example proto to extract the image and metadata.
      image_buffer, label_index, fname = parse_example_proto(example_serialized)
          
      image = image_preprocessing(image_buffer, image_size, train, thread_id)
      images_labels_fnames.append([image, label_index, fname])

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

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

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

    return images, tf.reshape(label_index_batch, [batch_size]), fnames
示例#7
0
文件: _queues.py 项目: fishsey/code
#==============================================================================
#create some threads manipulate  queues
#==============================================================================

import tensorflow as tf
import numpy as np
seed = 10
batchSize = 1
threadNums = 10

#get a random sample with shape(3, )
sample = tf.random_normal([5], seed=seed, dtype=tf.float32)

queue = tf.RandomShuffleQueue(capacity=30,
                              min_after_dequeue=1,
                              dtypes=[tf.float32] * 3)
#queue = tf.FIFOQueue(100, [tf.float32] * 2)

initQueue = queue.enqueue_many([sample * i for i in range(1, 11)])
x = queue.dequeue()
dequeueOp = queue.dequeue(2)  #out, each batchSize sample

#create a queueRunner with some threads enqueue
#enqueueRunners = tf.train.QueueRunner(queue, enqueue_ops=[enqueueOp]*threadNums)

#begin session
sess = tf.Session()

#coord = tf.train.Coordinator()
#threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    def train(self):
        flags = self.FLAGS
        block_shape = self.block_shape
        record_dir = self.record_dir
        record_dir_test = self.record_dir_test
        batch_size_train = self.batch_size_train
        batch_size_test = self.batch_size_test
        test_step = self.test_step
        LEARNING_RATE_BASE = flags.training_rate_base
        LEARNING_RATE_DECAY = flags.training_rate_decay
        X = tf.placeholder(dtype=tf.float32,
                           shape=[
                               batch_size_train, block_shape[0],
                               block_shape[1], block_shape[2]
                           ])
        training = tf.placeholder(tf.bool)
        with tf.variable_scope('generator'):
            airway_pred, airway_sig, artery_pred, artery_sig = self.Dense_Net(
                X, training, flags.batch_size_train, flags.accept_threshold)

        # lung_pred = tf.reshape(lung_pred,[batch_size_train,block_shape[0],block_shape[1],block_shape[2]])
        airway_pred = tf.reshape(
            airway_pred,
            [batch_size_train, block_shape[0], block_shape[1], block_shape[2]])
        artery_pred = tf.reshape(
            artery_pred,
            [batch_size_train, block_shape[0], block_shape[1], block_shape[2]])

        # binary predict mask
        # lung_pred_mask = tf.cast((lung_pred>0.01),tf.float32)
        airway_pred_mask = tf.cast((airway_pred > 0.01), tf.float32)
        artery_pred_mask = tf.cast((artery_pred > 0.01), tf.float32)

        # labels
        # lung_lable = tf.placeholder(dtype=tf.float32,shape=[batch_size_train,block_shape[0],block_shape[1],block_shape[2]])
        airway_lable = tf.placeholder(dtype=tf.float32,
                                      shape=[
                                          batch_size_train, block_shape[0],
                                          block_shape[1], block_shape[2]
                                      ])
        artery_lable = tf.placeholder(dtype=tf.float32,
                                      shape=[
                                          batch_size_train, block_shape[0],
                                          block_shape[1], block_shape[2]
                                      ])

        # discriminator output
        with tf.variable_scope('discriminator'):
            XY_real_pair = self.Dis(X, artery_lable, training,
                                    flags.batch_size_train)
        with tf.variable_scope('discriminator', reuse=True):
            XY_fake_pair = self.Dis(X, artery_sig, training,
                                    flags.batch_size_train)

        # accuracy
        # lung_acc = 2*tf.reduce_sum(lung_lable*lung_pred_mask)/(tf.reduce_sum(lung_lable+lung_pred_mask)+1e-6)
        # tf.summary.scalar('lung_acc', lung_acc)
        airway_acc = 2 * tf.reduce_sum(airway_lable * airway_pred_mask) / (
            tf.reduce_sum(airway_lable + airway_pred_mask) + 1e-6)
        tf.summary.scalar('airway_acc', airway_acc)
        artery_acc = 2 * tf.reduce_sum(artery_lable * artery_pred_mask) / (
            tf.reduce_sum(artery_lable + artery_pred_mask) + 1e-6)
        tf.summary.scalar('artery_acc', artery_acc)

        # generator cross entropy loss
        # w_fore_lung = flags.lung_fore_weight
        w_fore_airway = flags.airway_fore_weight
        w_fore_artery = flags.artery_fore_weight
        # lung loss

        airway_lable_ = tf.reshape(airway_lable, shape=[batch_size_train, -1])
        artery_lable_ = tf.reshape(artery_lable, shape=[batch_size_train, -1])
        airway_pred_ = tf.reshape(airway_pred, shape=[batch_size_train, -1])
        artery_pred_ = tf.reshape(artery_pred, shape=[batch_size_train, -1])
        airway_loss = flags.airway_weight * tf.reduce_mean(-tf.reduce_mean(
            w_fore_airway * airway_lable_ * tf.log(airway_pred_ + 1e-8),
            reduction_indices=[1]) - tf.reduce_mean(
                (1 - w_fore_airway) *
                (1 - airway_lable_) * tf.log(1 - airway_pred_ + 1e-8),
                reduction_indices=[1]))
        tf.summary.scalar('airway_loss_cross_entropy', airway_loss)
        # predict_mean_lung = tf.reduce_mean(tf.log(1-lung_pred + 1e-8),reduction_indices=[1])
        # mask_mean_lung = tf.reduce_mean((1-lung_lable))
        # artery loss
        artery_loss = flags.artery_weight * tf.reduce_mean(-tf.reduce_mean(
            w_fore_artery * artery_lable_ * tf.log(artery_pred_ + 1e-8),
            reduction_indices=[1]) - tf.reduce_mean(
                (1 - w_fore_artery) *
                (1 - artery_lable_) * tf.log(1 - artery_pred_ + 1e-8),
                reduction_indices=[1]))
        tf.summary.scalar('artery_loss_cross_entropy', artery_loss)
        # predict_mean_artery = tf.reduce_mean(tf.log(1 - artery_pred + 1e-8), reduction_indices=[1])
        # mask_mean_artery = tf.reduce_mean((1 - artery_lable))

        # generator cross entropy loss
        ge_loss = airway_loss + artery_loss
        tf.summary.scalar('generator_cross_entropy_loss', ge_loss)

        # discriminator and gan loss
        gan_g_loss = -tf.reduce_mean(XY_fake_pair)
        gan_d_loss = tf.reduce_mean(XY_fake_pair) - tf.reduce_mean(
            XY_real_pair)
        tf.summary.scalar('dis_g_loss', gan_g_loss)
        tf.summary.scalar('dis_d_loss', gan_d_loss)
        alpha = tf.random_uniform(shape=[
            batch_size_train, block_shape[0] * block_shape[1] * block_shape[2]
        ],
                                  minval=0.0,
                                  maxval=1.0)
        artery_sig_ = tf.reshape(artery_sig, shape=[batch_size_train, -1])
        diffenences_ = artery_sig_ - artery_lable_
        interpolates = artery_lable_ + alpha * diffenences_
        with tf.variable_scope('discriminator', reuse=True):
            XY_fake_intep = self.Dis(X, interpolates, training,
                                     batch_size_train)
        gradients = tf.gradients(XY_fake_intep, [interpolates])[0]
        slopes = tf.sqrt(
            tf.reduce_sum(tf.square(gradients), reduction_indices=[1]))
        gradient_penalty = tf.reduce_mean((slopes - 1.0)**2)
        gan_d_loss += 10 * gradient_penalty

        # total generator loss
        gan_g_w = 5
        ge_w = 100 - gan_g_w
        total_g_loss = ge_w * ge_loss + gan_g_w * gan_g_loss
        tf.summary.scalar('total_g_loss', total_g_loss)

        # set training step and learning rate into tensors to save
        global_step = tf.Variable(0, trainable=False)
        learning_rate = tf.maximum(
            tf.train.exponential_decay(LEARNING_RATE_BASE,
                                       global_step,
                                       15000 / flags.batch_size_train,
                                       LEARNING_RATE_DECAY,
                                       staircase=True), 1e-9)

        # merge operation for tensorboard summary
        merge_summary_op = tf.summary.merge_all()
        # trainer
        ge_var = [
            var for var in tf.trainable_variables()
            if var.name.startswith('generator')
        ]
        dis_var = [
            var for var in tf.trainable_variables()
            if var.name.startswith('discriminator')
        ]
        ge_train_op = tf.train.AdamOptimizer(learning_rate=learning_rate,
                                             beta1=0.9,
                                             beta2=0.999,
                                             epsilon=1e-8).minimize(
                                                 total_g_loss, global_step,
                                                 ge_var)
        dis_train_op = tf.train.AdamOptimizer(learning_rate=learning_rate,
                                              beta1=0.9,
                                              beta2=0.999,
                                              epsilon=1e-8).minimize(
                                                  gan_d_loss, global_step,
                                                  dis_var)

        # data part
        records = ut.get_records(record_dir)
        records_processor = TF_Records(records, block_shape)
        single_blocks = records_processor.read_records()
        queue = tf.RandomShuffleQueue(capacity=8,
                                      min_after_dequeue=4,
                                      dtypes=(
                                          single_blocks['airway'].dtype,
                                          single_blocks['artery'].dtype,
                                          single_blocks['lung'].dtype,
                                          single_blocks['original'].dtype,
                                      ))
        enqueue_op = queue.enqueue((
            single_blocks['airway'],
            single_blocks['artery'],
            single_blocks['lung'],
            single_blocks['original'],
        ))
        (airway_block, artery_block, lung_block,
         original_block) = queue.dequeue()
        qr = tf.train.QueueRunner(queue, [enqueue_op] * 2)

        # test data part
        records_test = ut.get_records(record_dir_test)
        records_processor_test = TF_Records(records_test, block_shape)
        single_blocks_test = records_processor_test.read_records()
        queue_test = tf.RandomShuffleQueue(
            capacity=8,
            min_after_dequeue=4,
            dtypes=(
                single_blocks_test['airway'].dtype,
                single_blocks_test['artery'].dtype,
                single_blocks_test['lung'].dtype,
                single_blocks_test['original'].dtype,
            ))
        enqueue_op_test = queue_test.enqueue((
            single_blocks_test['airway'],
            single_blocks_test['artery'],
            single_blocks_test['lung'],
            single_blocks_test['original'],
        ))
        (airway_block_test, artery_block_test, lung_block_test,
         original_block_test) = queue_test.dequeue()
        qr_test = tf.train.QueueRunner(queue, [enqueue_op_test] * 2)

        saver = tf.train.Saver(max_to_keep=1)
        config = tf.ConfigProto(allow_soft_placement=True)

        with tf.Session(config=config) as sess:

            # load variables if saved before
            if len(os.listdir(self.train_models_dir)) > 0:
                print "load saved model"
                sess.run(
                    tf.group(tf.global_variables_initializer(),
                             tf.local_variables_initializer()))
                saver.restore(sess,
                              self.train_models_dir + "train_models.ckpt")
            else:
                sess.run(
                    tf.group(tf.global_variables_initializer(),
                             tf.local_variables_initializer()))

            # coord for the reading threads
            coord = tf.train.Coordinator()
            enqueue_threads = qr.create_threads(sess, coord=coord, start=True)
            enqueue_threads_test = qr_test.create_threads(sess,
                                                          coord=coord,
                                                          start=True)
            tf.train.start_queue_runners(sess=sess)

            summary_writer_test = tf.summary.FileWriter(
                self.test_sum_dir, sess.graph)
            summary_writer_train = tf.summary.FileWriter(
                self.train_sum_dir, sess.graph)

            # main train loop
            # for i in range(flags.max_iteration_num):
            try:
                for i in range(flags.max_iteration_num):
                    # organize a batch of data for training
                    airway_np = np.zeros([
                        batch_size_train, block_shape[0], block_shape[1],
                        block_shape[2]
                    ], np.int16)
                    artery_np = np.zeros([
                        batch_size_train, block_shape[0], block_shape[1],
                        block_shape[2]
                    ], np.int16)
                    # lung_np = np.zeros([batch_size_train,block_shape[0],block_shape[1],block_shape[2]], np.int16)
                    original_np = np.zeros([
                        batch_size_train, block_shape[0], block_shape[1],
                        block_shape[2]
                    ], np.int16)

                    # store values into data block
                    for m in range(flags.batch_size_train):
                        artery_data, airway_data, original_data = \
                            sess.run([artery_block, airway_block, original_block])
                        airway_np[m, :, :, :] += airway_data
                        artery_np[m, :, :, :] += artery_data
                        # lung_np[m, :, :, :] += lung_data
                        original_np[m, :, :, :] += original_data

                    train_ge_, train_dis_, step_num = sess.run(
                        [ge_train_op, dis_train_op, global_step],
                        feed_dict={
                            X: original_np,
                            airway_lable: artery_np,
                            artery_lable: artery_np,
                            training: True
                        })

                    if i % 10 == 0:
                        sum_train, accuracy_artery, accuracy_airway, \
                        artery_l_val, total_l_val,total_l_dis \
                            = sess.run([merge_summary_op, artery_acc, airway_acc,
                                        artery_loss, total_g_loss,gan_d_loss],
                                       feed_dict={X: original_np, airway_lable: artery_np,
                                                                artery_lable: artery_np, training: False})
                        summary_writer_train.add_summary(
                            sum_train, global_step=int(step_num))
                        print "train :\nstep %d , artery loss = %f total generator loss = %f total discriminator loss= %f \n\t\t\tairway accuracy = %f , artery accuracy = %f\n =====================" \
                              % (int(step_num), artery_l_val, total_l_val,total_l_dis
                                 , accuracy_airway, accuracy_artery)

                    if i % test_step == 0 and i > 0:
                        # block testing part
                        airway_np_test = np.zeros([
                            batch_size_train, block_shape[0], block_shape[1],
                            block_shape[2]
                        ], np.int16)
                        artery_np_test = np.zeros([
                            batch_size_train, block_shape[0], block_shape[1],
                            block_shape[2]
                        ], np.int16)
                        # lung_np_test = np.zeros([batch_size_train, block_shape[0], block_shape[1], block_shape[2]], np.int16)
                        original_np_test = np.zeros([
                            batch_size_train, block_shape[0], block_shape[1],
                            block_shape[2]
                        ], np.int16)

                        # store values into data block
                        for m in range(flags.batch_size_train):
                            artery_data_test, airway_data_test, original_data_test = \
                                sess.run([artery_block_test, airway_block_test, original_block_test])
                            airway_np_test[m, :, :, :] += airway_data_test
                            artery_np_test[m, :, :, :] += artery_data_test
                            # lung_np_test[m, :, :, :] += lung_data_test
                            original_np_test[m, :, :, :] += original_data_test

                        sum_test, accuracy_artery, accuracy_airway, \
                        artery_l_val, total_l_val, \
                        artery_np_pred,artery_np_sig\
                            = sess.run([merge_summary_op, artery_acc, airway_acc,
                                        artery_loss, total_g_loss,
                                        artery_pred,artery_sig],
                                       feed_dict={X: original_np_test, airway_lable: artery_np_test,
                                                  artery_lable: artery_np_test, training: False})

                        summary_writer_test.add_summary(
                            sum_test, global_step=int(step_num))
                        print "\ntest :\nstep %d , artery loss = %f total loss = %f \n\t   airway accuracy = %f , artery accuracy = %f\n=====================" \
                              % (int(step_num), artery_l_val, total_l_val
                                 , accuracy_airway, accuracy_artery)
                        # print "airway percentage : ",str(np.float32(np.sum(np.float32(airway_np_test))/(flags.batch_size_train*block_shape[0]*block_shape[1]*block_shape[2])))
                        print "artery percentage : ", str(
                            np.float32(
                                np.sum(np.float32(artery_np_test)) /
                                (flags.batch_size_train * block_shape[0] *
                                 block_shape[1] * block_shape[2])))
                        # print "prediction of airway : maximum = ",np.max(airway_np_sig)," minimum = ",np.min(airway_np_sig)
                        print "prediction of artery : maximum = ", np.max(
                            artery_np_sig), " minimum = ", np.min(
                                artery_np_sig), '\n'
                        # print 'airway_log_mean = ',airway_log_mean,' airway_mask_mean = ',airway_mask_mean
                        # print 'lung_log_mean = ',lung_log_mean,' lung_mask_mean = ',lung_mask_mean
                        # print 'artery_log_mean = ',artery_log_mean,' artery_mask_mean = ',artery_mask_mean
                    if i % 100 == 0:
                        saver.save(sess,
                                   self.train_models_dir + "train_models.ckpt")
                        print "regular model saved! step count : ", step_num
            except Exception, e:
                print e
                # exit(2)
                coord.request_stop(e)
            coord.request_stop()
            coord.join(enqueue_threads)
            coord.join(enqueue_threads_test)
示例#9
0
def batch_inputs(dataset, log_annotated_images, train, num_readers,
                 filter_blacklist, scope_name):
    with tf.device('/cpu:0'), tf.name_scope(scope_name):
        data_files = dataset.data_files()

        filename_queue = tf.train.string_input_producer(
            data_files,
            shuffle=train,
            capacity=INPUT_QUEUE_MEMORY_FACTOR * 2 if train else 1)

        num_preprocess_threads = PREPROCESS_THREADS
        examples_per_shard = EXAMPLES_PER_SHARD
        min_queue_examples = examples_per_shard * INPUT_QUEUE_MEMORY_FACTOR

        if train:
            examples_queue = tf.RandomShuffleQueue(
                capacity=min_queue_examples + 3 * dataset.batch_size,
                min_after_dequeue=min_queue_examples,
                dtypes=[tf.string])
        else:
            examples_queue = tf.FIFOQueue(capacity=examples_per_shard +
                                          3 * dataset.batch_size,
                                          dtypes=[tf.string])

        if num_readers > 1:
            enqueue_ops = []
            for _ in range(num_readers):
                reader = tf.TFRecordReader()
                _, 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()
        else:
            reader = tf.TFRecordReader()
            _, example_serialized = reader.read(filename_queue)

        images_and_labels = []
        for thread_id in range(num_preprocess_threads):
            image_buffer, label_index, bbox, text, synset, filename =\
                parse_example_proto(example_serialized)
            if not filter_blacklist or \
                filename != ["ILSVRC2012_val_"+("00000000"+str(bli))[:8]+".JPG"
                            for bli in dataset.bl_images]:
                image = image_preprocessing(image_buffer, dataset.image_size,
                                            dataset.image_size, bbox,
                                            label_index, text, synset,
                                            log_annotated_images, train,
                                            thread_id)
                images_and_labels.append([image, label_index, text, synset])
        images, labels, texts, synsets = tf.train.batch_join(
            images_and_labels,
            batch_size=dataset.batch_size // dataset.num_gpus,
            capacity=2 * num_preprocess_threads * dataset.batch_size)

        images = tf.cast(images, tf.float32)
        images = tf.reshape(images,
                            shape=[
                                dataset.batch_size // dataset.num_gpus,
                                dataset.image_size, dataset.image_size, 3
                            ])

        labels = tf.reshape(labels, [dataset.batch_size // dataset.num_gpus])

        return images, tf.one_hot(labels, dataset.num_classes())
示例#10
0
def train():
    """The main function that runs training"""

    ## data
    image, ih, iw, gt_boxes, gt_masks, num_instances, img_id = \
        datasets.get_dataset(FLAGS.dataset_name,
                             FLAGS.dataset_split_name,
                             FLAGS.dataset_dir,
                             FLAGS.im_batch,
                             is_training=True)

    data_queue = tf.RandomShuffleQueue(
        capacity=32,
        min_after_dequeue=16,
        dtypes=(image.dtype, ih.dtype, iw.dtype, gt_boxes.dtype,
                gt_masks.dtype, num_instances.dtype, img_id.dtype))
    enqueue_op = data_queue.enqueue(
        (image, ih, iw, gt_boxes, gt_masks, num_instances, img_id))
    data_queue_runner = tf.train.QueueRunner(data_queue, [enqueue_op] * 4)
    tf.add_to_collection(tf.GraphKeys.QUEUE_RUNNERS, data_queue_runner)
    (image, ih, iw, gt_boxes, gt_masks, num_instances,
     img_id) = data_queue.dequeue()
    im_shape = tf.shape(image)
    image = tf.reshape(image, (im_shape[0], im_shape[1], im_shape[2], 3))

    ## network
    logits, end_points, pyramid_map = network.get_network(
        FLAGS.network, image, weight_decay=FLAGS.weight_decay)
    outputs = pyramid_network.build(end_points,
                                    ih,
                                    iw,
                                    pyramid_map,
                                    num_classes=81,
                                    base_anchors=9,
                                    is_training=True,
                                    gt_boxes=gt_boxes,
                                    gt_masks=gt_masks,
                                    loss_weights=[0.2, 0.2, 1.0, 0.2, 1.0])

    total_loss = outputs['total_loss']
    losses = outputs['losses']
    batch_info = outputs['batch_info']
    regular_loss = tf.add_n(
        tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))

    ## solvers
    global_step = slim.create_global_step()
    update_op = solve(global_step)

    cropped_rois = tf.get_collection('__CROPPED__')[0]
    transposed = tf.get_collection('__TRANSPOSED__')[0]

    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.8)
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())
    sess.run(init_op)

    summary_op = tf.summary.merge_all()
    logdir = os.path.join(FLAGS.train_dir, strftime('%Y%m%d%H%M%S', gmtime()))
    if not os.path.exists(logdir):
        os.makedirs(logdir)
    summary_writer = tf.summary.FileWriter(logdir, graph=sess.graph)

    ## restore
    restore(sess)

    ## main loop
    coord = tf.train.Coordinator()
    threads = []
    # print (tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS))
    for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS):
        threads.extend(
            qr.create_threads(sess, coord=coord, daemon=True, start=True))

    tf.train.start_queue_runners(sess=sess, coord=coord)
    saver = tf.train.Saver(max_to_keep=20)

    for step in range(FLAGS.max_iters):

        start_time = time.time()

        s_, tot_loss, reg_lossnp, img_id_str, \
        rpn_box_loss, rpn_cls_loss, refined_box_loss, refined_cls_loss, mask_loss, \
        gt_boxesnp, \
        rpn_batch_pos, rpn_batch, refine_batch_pos, refine_batch, mask_batch_pos, mask_batch = \
                     sess.run([update_op, total_loss, regular_loss, img_id] +
                              losses +
                              [gt_boxes] +
                              batch_info )

        duration_time = time.time() - start_time
        if step % 1 == 0:
            print(
                """iter %d: image-id:%07d, time:%.3f(sec), regular_loss: %.6f, """
                """total-loss %.4f(%.4f, %.4f, %.6f, %.4f, %.4f), """
                """instances: %d, """
                """batch:(%d|%d, %d|%d, %d|%d)""" %
                (step, img_id_str, duration_time, reg_lossnp, tot_loss,
                 rpn_box_loss, rpn_cls_loss, refined_box_loss,
                 refined_cls_loss, mask_loss, gt_boxesnp.shape[0],
                 rpn_batch_pos, rpn_batch, refine_batch_pos, refine_batch,
                 mask_batch_pos, mask_batch))

            if np.isnan(tot_loss) or np.isinf(tot_loss):
                print(gt_boxesnp)
                raise

        if step % 100 == 0:
            summary_str = sess.run(summary_op)
            summary_writer.add_summary(summary_str, step)

        if (step % 10000 == 0 or step + 1 == FLAGS.max_iters) and step != 0:
            checkpoint_path = os.path.join(
                FLAGS.train_dir,
                FLAGS.dataset_name + '_' + FLAGS.network + '_model.ckpt')
            saver.save(sess, checkpoint_path, global_step=step)

        if coord.should_stop():
            coord.request_stop()
            coord.join(threads)
示例#11
0
def cifar10(path=pathcifar,
            activation="sigmoid",
            conv_channels=(16, 16, 16),
            linear_layers=32,
            batch_size=128,
            num_threads=4,
            min_queue_examples=1000,
            mode="train"):
    """Cifar10 classification with a convolutional network."""

    # Data.
    _open_cifar10(path)

    if activation == "sigmoid":
        activation_op = tf.sigmoid
    elif activation == "relu":
        activation_op = tf.nn.relu
    else:
        raise ValueError("{} activation not supported".format(activation))
# Read images and labels from disk.
    if mode == "train":
        filenames = [
            os.path.join(path, CIFAR10_FOLDER, "data_batch_{}.bin".format(i))
            for i in range(1, 6)
        ]
    elif mode == "test":
        filenames = [os.path.join(path, "test_batch.bin")]
    else:
        raise ValueError("Mode {} not recognised".format(mode))

    depth = 3
    height = 32
    width = 32
    label_bytes = 1
    image_bytes = depth * height * width
    record_bytes = label_bytes + image_bytes
    reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
    _, record = reader.read(tf.train.string_input_producer(filenames))
    record_bytes = tf.decode_raw(record, tf.uint8)

    label = tf.cast(tf.slice(record_bytes, [0], [label_bytes]), tf.int32)
    raw_image = tf.slice(record_bytes, [label_bytes], [image_bytes])
    image = tf.cast(tf.reshape(raw_image, [depth, height, width]), tf.float32)
    # height x width x depth.
    image = tf.transpose(image, [1, 2, 0])
    image = tf.div(image, 255)

    queue = tf.RandomShuffleQueue(
        capacity=min_queue_examples + 3 * batch_size,
        min_after_dequeue=min_queue_examples,
        dtypes=[tf.float32, tf.int32],
        shapes=[image.get_shape(), label.get_shape()])
    enqueue_ops = [queue.enqueue([image, label]) for _ in range(num_threads)]
    tf.train.add_queue_runner(tf.train.QueueRunner(queue, enqueue_ops))

    with tf.name_scope('Optimizee_loss'):

        def compute_loss():
            image_batch, label_batch = queue.dequeue_many(batch_size)
            label_batch = tf.reshape(label_batch, [batch_size])
            output = image_batch
            with tf.variable_scope('ConvMLP', reuse=tf.AUTO_REUSE):
                conv1_w = tf.get_variable(
                    "conv1_w",
                    shape=[5, 5, depth, conv_channels[0]],
                    dtype=tf.float32,
                    initializer=tf.random_normal_initializer(stddev=0.01))
                conv1_b = tf.get_variable(
                    "conv1_b",
                    shape=[
                        conv_channels[0],
                    ],
                    dtype=tf.float32,
                    initializer=tf.random_normal_initializer(stddev=0.01))
                conv1_beta = tf.get_variable(
                    "conv1_beta",
                    shape=[1, 1, 1, conv_channels[0]],
                    dtype=tf.float32,
                    initializer=tf.random_normal_initializer(stddev=0.01))
                conv2_w = tf.get_variable(
                    "conv2_w",
                    shape=[5, 5, conv_channels[0], conv_channels[1]],
                    dtype=tf.float32,
                    initializer=tf.random_normal_initializer(stddev=0.01))
                conv2_b = tf.get_variable(
                    "conv2_b",
                    shape=[
                        conv_channels[1],
                    ],
                    dtype=tf.float32,
                    initializer=tf.random_normal_initializer(stddev=0.01))
                conv2_beta = tf.get_variable(
                    "conv2_beta",
                    shape=[1, 1, 1, conv_channels[1]],
                    dtype=tf.float32,
                    initializer=tf.random_normal_initializer(stddev=0.01))
                conv3_w = tf.get_variable(
                    "conv3_w",
                    shape=[5, 5, conv_channels[1], conv_channels[2]],
                    dtype=tf.float32,
                    initializer=tf.random_normal_initializer(stddev=0.01))
                conv3_b = tf.get_variable(
                    "conv3_b",
                    shape=[
                        conv_channels[2],
                    ],
                    dtype=tf.float32,
                    initializer=tf.random_normal_initializer(stddev=0.01))
                conv3_beta = tf.get_variable(
                    "conv3_beta",
                    shape=[1, 1, 1, conv_channels[2]],
                    dtype=tf.float32,
                    initializer=tf.random_normal_initializer(stddev=0.01))
                output = tf.nn.convolution(output,
                                           conv1_w,
                                           padding='SAME',
                                           strides=[1, 1])
                output = tf.nn.relu(tf.nn.bias_add(output, conv1_b))
                output = tf.nn.max_pool(output,
                                        ksize=[1, 2, 2, 1],
                                        strides=[1, 2, 2, 1],
                                        padding='SAME')
                b_m_1, b_v_1 = tf.nn.moments(output, axes=[0, 1, 2])
                output = tf.nn.batch_normalization(output,
                                                   b_m_1,
                                                   b_v_1,
                                                   conv1_beta,
                                                   scale=None,
                                                   variance_epsilon=1e-8)
                output = tf.nn.convolution(output,
                                           conv2_w,
                                           padding='SAME',
                                           strides=[1, 1])
                output = tf.nn.relu(tf.nn.bias_add(output, conv2_b))
                output = tf.nn.max_pool(output,
                                        ksize=[1, 2, 2, 1],
                                        strides=[1, 2, 2, 1],
                                        padding='SAME')
                b_m_2, b_v_2 = tf.nn.moments(output, [0, 1, 2])
                output = tf.nn.batch_normalization(output,
                                                   b_m_2,
                                                   b_v_2,
                                                   conv2_beta,
                                                   scale=None,
                                                   variance_epsilon=1e-8)
                output = tf.nn.convolution(output,
                                           conv3_w,
                                           padding='SAME',
                                           strides=[1, 1])
                output = tf.nn.relu(tf.nn.bias_add(output, conv3_b))
                output = tf.nn.max_pool(output,
                                        ksize=[1, 2, 2, 1],
                                        strides=[1, 2, 2, 1],
                                        padding='SAME')
                b_m_3, b_v_3 = tf.nn.moments(output, [0, 1, 2])
                output = tf.nn.batch_normalization(output,
                                                   b_m_3,
                                                   b_v_3,
                                                   conv3_beta,
                                                   scale=None,
                                                   variance_epsilon=1e-8)
                output = tf.layers.flatten(output)
                W_in = tf.get_variable(
                    "W_in",
                    shape=[output.shape[1], linear_layers],
                    dtype=tf.float32,
                    initializer=tf.random_normal_initializer(stddev=0.01))
                b_in = tf.get_variable(
                    "b_in",
                    shape=[
                        linear_layers,
                    ],
                    dtype=tf.float32,
                    initializer=tf.random_normal_initializer(stddev=0.01))
                W_out = tf.get_variable(
                    "W_out",
                    shape=[linear_layers, 10],
                    dtype=tf.float32,
                    initializer=tf.random_normal_initializer(stddev=0.01))
                b_out = tf.get_variable(
                    "b_out",
                    shape=[
                        10,
                    ],
                    dtype=tf.float32,
                    initializer=tf.random_normal_initializer(stddev=0.01))
            layer_out = activation_op(tf.add(tf.matmul(output, W_in), b_in))
            output = tf.add(tf.matmul(layer_out, W_out), b_out)
            loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
                logits=output, labels=label_batch)
            return tf.reduce_mean(loss)

    with tf.name_scope('Convex_loss'):

        def convex_loss():
            with tf.variable_scope('conv_var', reuse=tf.AUTO_REUSE):
                v = tf.get_variable(
                    "v",
                    shape=[1, 10],
                    dtype=tf.float32,
                    initializer=tf.random_normal_initializer(stddev=0.01))

                # Non-trainable variables.
                target = tf.get_variable(
                    "target",
                    shape=[1, 10],
                    dtype=tf.float32,
                    initializer=tf.random_uniform_initializer(),
                    trainable=False)

            return tf.reduce_mean(
                tf.clip_by_value(tf.square(v - target), 0, 10))

    return collections.OrderedDict([('Opt_loss', compute_loss),
                                    ('Aux_loss', convex_loss)])
示例#12
0
    def _create_queue_and_ops(self, window, enqueue_size=1, dequeue_size=1):
        """
        Create a shuffled queue or FIFO queue, and create queue
        operations. This should be called before ``tf.Graph.finalize``.
        """
        self._window = window
        try:
            is_dynamic_window = window.has_dynamic_shapes
        except AttributeError:
            tf.logging.fatal(
                "unrecognised window format, expecting a"
                "niftynet.engine.image_window.ImageWindow instance")
            raise
        if is_dynamic_window and enqueue_size > 1:
            tf.logging.warning(
                "using dynamic window size, buffer input size is set to 1")
        if is_dynamic_window and dequeue_size > 1:
            tf.logging.warning(
                "using dynamic window size, network batch size is set to 1")
        _enqueue_size = 1 if is_dynamic_window else enqueue_size

        # batch_size is 1 if is_dynamic_window because
        # RandomShuffleQueue's DequeueMany and DequeueUpTo require
        # the components to have specified shapes
        self._batch_size = 1 if is_dynamic_window else dequeue_size

        self.capacity = int(max(self.capacity, round(self._batch_size * 2.5)))
        assert self._batch_size <= self.capacity, \
            "batch size {} is larger than the buffer size {}, " \
            "please increase the queue capacity " \
            "or decrease the batch size".format(
                self._batch_size, self.capacity)
        tf.logging.info('buffering with %s windows', self.capacity)
        try:
            self.placeholders_dict = window.placeholders_dict(_enqueue_size)
        except AttributeError:
            tf.logging.fatal(
                "unrecognised window format, expecting a"
                "niftynet.engine.image_window.ImageWindow instance")
            raise

        names = list(self.placeholders_dict)
        placeholders = list(self.placeholders_dict.values())
        input_dtypes = [holder.dtype for holder in placeholders]
        input_shapes = [holder.shape[1:] for holder in placeholders] \
            if not is_dynamic_window else None

        # create a queue
        # pylint: disable=redefined-variable-type
        if self.shuffle:
            self._queue = tf.RandomShuffleQueue(
                capacity=self.capacity,
                min_after_dequeue=self.capacity // 2,
                dtypes=input_dtypes,
                shapes=input_shapes,
                names=names,
                name="shuffled_queue")
            assert (self.capacity - self.capacity // 2) >= self._batch_size, \
                "batch size larger than the largest possible dequeue size" \
                "of the current queue capacity"
        else:
            self._queue = tf.FIFOQueue(capacity=self.capacity,
                                       dtypes=input_dtypes,
                                       shapes=input_shapes,
                                       names=names,
                                       name="FIFO_queue")

        # create queue operations
        if is_dynamic_window:
            self._enqueue_op = self._queue.enqueue(self.placeholders_dict)
            self._dequeue_func = self._queue.dequeue
        else:
            self._enqueue_op = self._queue.enqueue_many(self.placeholders_dict)
            self._dequeue_func = self._queue.dequeue_many
        self._query_queue_size_op = self._queue.size()
        self._close_queue_op = self._queue.close(cancel_pending_enqueues=True)
示例#13
0
#生成三个样本文件,每个文件包含5列,假设前4列为特征,最后1列为标签
data = np.zeros([20, 5])
np.savetxt('./file0.csv', data, fmt='%d', delimiter=',')
data += 1
np.savetxt('./file1.csv', data, fmt='%d', delimiter=',')
data += 1
np.savetxt('./file2.csv', data, fmt='%d', delimiter=',')

# 创建pipeline数据流。

#定义FilenameQueue
filename_queue = tf.train.string_input_producer(
    ["file%d.csv" % i for i in range(3)])
#定义ExampleQueue
example_queue = tf.RandomShuffleQueue(capacity=1000,
                                      min_after_dequeue=0,
                                      dtypes=[tf.int32, tf.int32],
                                      shapes=[[4], [1]])
#读取CSV文件,每次读一行
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
#对一行数据进行解码
record_defaults = [[1], [1], [1], [1], [1]]
col1, col2, col3, col4, col5 = tf.decode_csv(value,
                                             record_defaults=record_defaults)
features = tf.stack([col1, col2, col3, col4])
#将特征和标签push进ExampleQueue
enq_op = example_queue.enqueue([features, [col5]])
#使用QueueRunner创建两个进程加载数据到ExampleQueue
qr = tf.train.QueueRunner(example_queue, [enq_op] * 2)
#使用此方法方便后面tf.train.start_queue_runner统一开始进程
tf.train.add_queue_runner(qr)
示例#14
0
def prefetch_input_data(reader,
                        file_pattern,
                        shuffle,
                        capacity,
                        dataset,
                        num_reader_threads=1):
    """Prefetches string values from disk into an input queue.

  Args:
    reader: Instance of tf.ReaderBase.
    file_pattern: Comma-separated list of file patterns (e.g.
        "/tmp/train_data-?????-of-00100", where '?' acts as a wildcard that
        matches any character).
    shuffle: Boolean; whether to randomly shuffle the input data.
    capacity: Queue capacity (number of records).
    num_reader_threads: Number of reader threads feeding into the queue.

  Returns:
    A Queue containing prefetched string values.
  """
    data_files = []
    print("file_pattern is ", file_pattern)
    for pattern in file_pattern.split(","):
        data_files.extend(tf.gfile.Glob(pattern))
    if not data_files:
        tf.logging.fatal("Found no input files matching %s", file_pattern)
    else:
        tf.logging.info("Prefetching values from %d files matching %s",
                        len(data_files), file_pattern)

    filename_queue = tf.train.string_input_producer(data_files,
                                                    shuffle=shuffle,
                                                    capacity=16,
                                                    name="filename_queue" +
                                                    dataset)

    if shuffle:
        min_after_dequeue = int(0.6 * capacity)
        values_queue = tf.RandomShuffleQueue(
            capacity=capacity,
            min_after_dequeue=min_after_dequeue,
            dtypes=[tf.string],
            shapes=[[]],
            name="random_input_queue" + dataset)
    else:
        values_queue = tf.FIFOQueue(capacity=capacity,
                                    dtypes=[tf.string],
                                    shapes=[[]],
                                    name="fifo_input_queue" + dataset)

    enqueue_ops = []
    for _ in range(num_reader_threads):
        key, value = reader.read(filename_queue)
        enqueue_ops.append(values_queue.enqueue([value]))

    tf.train.queue_runner.add_queue_runner(
        tf.train.queue_runner.QueueRunner(values_queue, enqueue_ops))
    #tf.train.queue_runner.add_queue_runner(video_queue)       ### MAYBE IMPORTANT
    #tf.summary.scalar("queue/%s/fraction_of_%d_full" % (values_queue.name,capacity),tf.cast(values_queue.size(), tf.float32) * (1.0 / capacity))

    return values_queue
def batch_inputs(data_dir,
                 batch_size,
                 image_size,
                 train,
                 preprocess_operation,
                 num_preprocess_threads=None,
                 num_readers=1,
                 examples_per_shard=1024,
                 input_queue_memory_factor=16):
    """Contruct batches of training or evaluation examples from the image dataset.
    Args:
      dataset: instance of Dataset class specifying the dataset.
        See dataset.py for details.
      batch_size: integer
      train: boolean
      num_preprocess_threads: integer, total number of preprocessing threads
      num_readers: integer, number of parallel readers
    Returns:
      images: 4-D float Tensor of a batch of images
      labels: 1-D integer Tensor of [batch_size].
    Raises:
      ValueError: if data is not found
    """
    with tf.name_scope('batch_processing'):
        if train == True:
            subset = 'train'
        else:
            subset = 'validation'

        # Reshape images into these desired dimensions.
        if len(image_size) == 1:
            if isinstance(image_size, list) or isinstance(image_size, tuple):
                image_size = image_size[0]
            height = image_size
            width = image_size
            depth = 3
        elif len(image_size) == 2:
            height = image_size[0]
            width = image_size[1]
            depth = 3
        elif len(image_size) == 3:
            height = image_size[0]
            width = image_size[1]
            depth = image_size[2]
        else:
            print('Wrong image_size dimension %d' % len(image_size))
            exit(-1)

        tf_record_pattern = os.path.join(data_dir, '%s-*' % subset)
        data_files = tf.gfile.Glob(tf_record_pattern)
        if not data_files:
            print('No files found for dataset %s at %s' % (subset, data_dir))
            exit(-1)
        if data_files is None:
            raise ValueError('No data files found for this dataset')

        # Create filename_queue
        if train:
            filename_queue = tf.train.string_input_producer(data_files,
                                                            shuffle=True,
                                                            capacity=16)
        else:
            filename_queue = tf.train.string_input_producer(data_files,
                                                            shuffle=False,
                                                            capacity=1)
        if num_preprocess_threads is None:
            num_preprocess_threads = 4

        if num_readers is None:
            num_readers = 1

        # Approximate number of examples per shard.
        if examples_per_shard is None:
            examples_per_shard = 1024
        if input_queue_memory_factor is None:
            input_queue_memory_factor = 16
        # 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
        min_queue_examples = examples_per_shard * input_queue_memory_factor
        if train:
            examples_queue = tf.RandomShuffleQueue(
                capacity=min_queue_examples + 3 * batch_size,
                min_after_dequeue=min_queue_examples,
                dtypes=[tf.string])
        else:
            examples_queue = tf.FIFOQueue(capacity=examples_per_shard +
                                          3 * batch_size,
                                          dtypes=[tf.string])

        # Create multiple readers to populate the queue of examples.
        if num_readers > 1:
            enqueue_ops = []
            for _ in range(num_readers):
                reader = tf.TFRecordReader()
                _, 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()
        else:
            reader = tf.TFRecordReader()
            _, example_serialized = reader.read(filename_queue)

        images_and_labels = []
        for thread_id 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_preprocessing'''
            image = image_preprocessing(image_buffer, preprocess_operation,
                                        train, height, width, depth, thread_id)
            images_and_labels.append([image, label_index, _])

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

        images = tf.cast(images, tf.float32)
        images = tf.reshape(images, shape=[batch_size, height, width, depth])

        # Display the training images in the visualizer.
        tf.image_summary('images', images)
        return images, tf.reshape(label_index_batch, [batch_size]), tf.reshape(
            label_text, [batch_size])
示例#16
0
    def _build(self):
        """Returns a tuple containing image, image metadata and label.

        Does not receive any input since it doesn't depend on anything inside
        the graph and it's the starting point of it.

        Returns:
            dequeue_dict ({}): Dequeued dict returning and image, bounding
                boxes, filename and the scaling factor used.

        TODO: Join filename, scaling_factor (and possible other fields) into a
        metadata.
        """
        # Find split file from which we are going to read.
        split_path = os.path.join(
            self._dataset_dir, '{}.tfrecords'.format(self._split)
        )
        if not tf.gfile.Exists(split_path):
            raise InvalidDataDirectory(
                '"{}" does not exist.'.format(split_path)
            )
        # String input producer allows for a variable number of files to read
        # from. We just know we have a single file.
        filename_queue = tf.train.string_input_producer(
            [split_path], num_epochs=self._num_epochs, seed=self._seed
        )

        # Define reader to parse records.
        reader = tf.TFRecordReader()
        _, raw_record = reader.read(filename_queue)

        # We parse variable length features (bboxes in a image) as sequence
        # features
        context_example, sequence_example = tf.parse_single_sequence_example(
            raw_record,
            context_features=self._context_features,
            sequence_features=self._sequence_features
        )

        # Decode and preprocess the example (crop, adjust mean and variance).
        # image_jpeg = tf.decode_raw(example['image_raw'], tf.string)
        image_raw = tf.image.decode_jpeg(
            context_example['image_raw'], channels=3
        )
        # tf.summary.image('image_raw', image_raw, max_outputs=20)

        # Do we need per_image_standardization? Do it depend on pretrained?
        image = tf.cast(image_raw, tf.float32)
        height = tf.cast(context_example['height'], tf.int32)
        width = tf.cast(context_example['width'], tf.int32)
        image_shape = tf.stack([height, width, 3])
        image = tf.reshape(image, image_shape)

        label = self.sparse_to_tensor(sequence_example['label'])
        xmin = self.sparse_to_tensor(sequence_example['xmin'])
        xmax = self.sparse_to_tensor(sequence_example['xmax'])
        ymin = self.sparse_to_tensor(sequence_example['ymin'])
        ymax = self.sparse_to_tensor(sequence_example['ymax'])

        # Stack parsed tensors to define bounding boxes of shape (num_boxes, 5)
        bboxes = tf.stack([xmin, ymin, xmax, ymax, label], axis=1)

        # Resize images (if needed)
        image, bboxes, scale_factor = self._resize_image(image, bboxes)

        image, bboxes, applied_augmentations = self._augment(image, bboxes)

        filename = tf.cast(context_example['filename'], tf.string)

        # TODO: Send additional metadata through the queue (scale_factor,
        # applied_augmentations)

        queue_dtypes = [tf.float32, tf.int32, tf.string]
        queue_names = ['image', 'bboxes', 'filename']

        if self._random_shuffle:
            queue = tf.RandomShuffleQueue(
                capacity=100,
                min_after_dequeue=0,
                dtypes=queue_dtypes,
                names=queue_names,
                name='tfrecord_random_queue',
                seed=self._seed
            )
        else:
            queue = tf.FIFOQueue(
                capacity=100,
                dtypes=queue_dtypes,
                names=queue_names,
                name='tfrecord_fifo_queue'
            )

        # Generate queueing ops for QueueRunner.
        enqueue_ops = [queue.enqueue({
            'image': image,
            'bboxes': bboxes,
            'filename': filename,
        })] * 20

        tf.train.add_queue_runner(tf.train.QueueRunner(queue, enqueue_ops))

        return queue.dequeue()
示例#17
0
    def __init__(self, num_actions, observation_size):

        self.num_actions = num_actions
        self.observation_size = observation_size

        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True

        self.graph = tf.get_default_graph()
        self.sess = tf.Session(config=config)
        self.logger = tf.summary.FileWriter("logs")

        self.batch_size = 64
        self.max_experience_size = 1000000
        self.start_learning_after = 5000
        self.store_every_nth = 5

        self.inp_rewards = tf.placeholder(tf.float32, (None, ))
        self.inp_actions = tf.placeholder(tf.float32, (None, num_actions))
        self.inp_prev_states = tf.placeholder(tf.float32,
                                              (None, observation_size))
        self.inp_next_states = tf.placeholder(tf.float32,
                                              (None, observation_size))

        all_experience = tf.RandomShuffleQueue(
            capacity=self.max_experience_size,
            min_after_dequeue=self.start_learning_after,
            dtypes=[tf.float32, tf.float32, tf.float32, tf.float32],
            shapes=[(), (num_actions, ), (observation_size, ),
                    (observation_size, )])

        exp_fifo_queue = tf.FIFOQueue(
            capacity=self.max_experience_size,
            dtypes=[tf.float32, tf.float32, tf.float32, tf.float32],
            shapes=[(), (num_actions, ), (observation_size, ),
                    (observation_size, )])

        self.exp_enqueue_op = all_experience.enqueue_many([
            self.inp_rewards, self.inp_actions, self.inp_prev_states,
            self.inp_next_states
        ])

        self.exp_fifo_enqueue_op = exp_fifo_queue.enqueue_many([
            self.inp_rewards, self.inp_actions, self.inp_prev_states,
            self.inp_next_states
        ])

        self.exp_size_op = all_experience.size()

        [rewards, actions, prev_states,
         next_states] = all_experience.dequeue_many(self.batch_size)
        self.dequeued_rewards = rewards
        self.dequeued_actions = actions
        self.dequeued_prev_states = prev_states
        self.dequeued_next_states = next_states

        [fifo_rewards, fifo_actions, fifo_prev_states,
         fifo_next_states] = exp_fifo_queue.dequeue_many(self.batch_size)
        self.dequeued_fifo_rewards = fifo_rewards
        self.dequeued_fifo_actions = fifo_actions
        self.dequeued_fifo_prev_states = fifo_prev_states
        self.dequeued_fifo_next_states = fifo_next_states
        self.exp_fifo_size_op = exp_fifo_queue.size()

        self.sum_rewards = 0
        self.store_index = 0
        self.stored_count = 0

        self.train_ops = []
        self.store_ops = []

        self.train_listener = None
        self.store_listener = None
def batch_inputs(dataset, batch_size, train, num_preprocess_threads=None,
                 num_readers=1, regular=True):
  """Contruct batches of training or evaluation examples from the image dataset.

  Args:
    dataset: instance of Dataset class specifying the dataset.
      See dataset.py for details.
    batch_size: integer
    train: boolean
    num_preprocess_threads: integer, total number of preprocessing threads
    num_readers: integer, number of parallel readers

  Returns:
    images: 4-D float Tensor of a batch of images
    labels: 1-D integer Tensor of [batch_size].

  Raises:
    ValueError: if data is not found
  """
  with tf.name_scope('batch_processing'):
    data_files = dataset.data_files()
    if data_files is None:
      raise ValueError('No data files found for this dataset')

    # Create filename_queue
    if train:
      filename_queue = tf.train.string_input_producer(data_files,
                                                      shuffle=True,
                                                      capacity=16)
    else:
      filename_queue = tf.train.string_input_producer(data_files,
                                                      shuffle=False,
                                                      capacity=1)
    if num_preprocess_threads is None:
      num_preprocess_threads = FLAGS.num_preprocess_threads

    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 is None:
      num_readers = FLAGS.num_readers

    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
    min_queue_examples = examples_per_shard * FLAGS.input_queue_memory_factor
    if train:
      examples_queue = tf.RandomShuffleQueue(
          capacity=min_queue_examples + 3 * batch_size,
          min_after_dequeue=min_queue_examples,
          dtypes=[tf.string])
    else:
      examples_queue = tf.FIFOQueue(
          capacity=examples_per_shard + 3 * batch_size,
          dtypes=[tf.string])

    # Create multiple readers to populate the queue of examples.
    if num_readers > 1:
      enqueue_ops = []
      for _ in range(num_readers):
        reader = dataset.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()
    else:
      reader = dataset.reader()
      _, example_serialized = reader.read(filename_queue)

    images_and_labels = []
    for thread_id in range(num_preprocess_threads):
      # Parse a serialized Example proto to extract the image and metadata.
      image_buffer, label_index, bbox, _ = parse_example_proto(
          example_serialized)
      image = image_preprocessing(image_buffer, bbox, train, thread_id, regular=regular)
      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)

    # Reshape images into these desired dimensions.
    height = FLAGS.image_size
    width = FLAGS.image_size
    depth = 3

    images = tf.cast(images, tf.float32)
    images = tf.reshape(images, shape=[batch_size, height, width, depth])

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

    return images, tf.reshape(label_index_batch, [batch_size])
示例#19
0
def prefetch_input_data(reader,
                        file_pattern,
                        is_training,
                        batch_size,
                        values_per_shard,
                        input_queue_capacity_factor=16,
                        num_reader_threads=1,
                        shard_queue_name="filename_queue",
                        value_queue_name="input_queue"):
  """Prefetches string values from disk into an input queue.

  In training the capacity of the queue is important because a larger queue
  means better mixing of training examples between shards. The minimum number of
  values kept in the queue is values_per_shard * input_queue_capacity_factor,
  where input_queue_memory factor should be chosen to trade-off better mixing
  with memory usage.

  Args:
    reader: Instance of tf.ReaderBase.
    file_pattern: Comma-separated list of file patterns (e.g.
        /tmp/train_data-?????-of-00100).
    is_training: Boolean; whether prefetching for training or eval.
    batch_size: Model batch size used to determine queue capacity.
    values_per_shard: Approximate number of values per shard.
    input_queue_capacity_factor: Minimum number of values to keep in the queue
      in multiples of values_per_shard. See comments above.
    num_reader_threads: Number of reader threads to fill the queue.
    shard_queue_name: Name for the shards filename queue.
    value_queue_name: Name for the values input queue.

  Returns:
    A Queue containing prefetched string values.
  """
  # check if dataset file exist
  data_files = []
  for pattern in file_pattern.split(","):
    data_files.extend(tf.gfile.Glob(pattern))
  if not data_files:
    tf.logging.fatal("Found no input files matching %s", file_pattern)
  else:
    tf.logging.info("Prefetching values from %d files matching %s",
                    len(data_files), file_pattern)

  """
  Queues are a convenient TensorFlow mechanism to compute tensors 
  asynchronously using multiple threads. For example in the canonical 
  'Input Reader' setup one set of threads generates filenames in a queue; 
  a second set of threads read records from the files, processes them, 
  and enqueues tensors on a second queue; a third set of threads dequeues 
  these input records to construct batches and runs them through training 
  operations.
  """
  
  if is_training:
    # create a queue to store filenames (string)
    filename_queue = tf.train.string_input_producer(
        data_files, shuffle=True, capacity=16, name=shard_queue_name)
        
    # calculate min capacity and capacity
    min_queue_examples = values_per_shard * input_queue_capacity_factor
    capacity = min_queue_examples + 100 * batch_size
    # create a queue to store raw data
    values_queue = tf.RandomShuffleQueue(
        capacity=capacity,
        min_after_dequeue=min_queue_examples,
        dtypes=[tf.string],
        name="random_" + value_queue_name)
  else:
    filename_queue = tf.train.string_input_producer(
        data_files, shuffle=False, capacity=1, name=shard_queue_name)
    capacity = values_per_shard + 3 * batch_size
    values_queue = tf.FIFOQueue(
        capacity=capacity, dtypes=[tf.string], name="fifo_" + value_queue_name)

  enqueue_ops = []
  for _ in range(num_reader_threads):
    # read file (value) from filename queue
    # ps: tf reader is also tf graph operation
    _, value = reader.read(filename_queue)
    # values_queue.enqueue is a enqueue operation put value into values_queue
    enqueue_ops.append(values_queue.enqueue([value]))
    
  # values_queue: a queue, enqueue_ops: is a operation on values_queue
  tf.train.queue_runner.add_queue_runner(tf.train.queue_runner.QueueRunner(
      values_queue, enqueue_ops))
  tf.summary.scalar(
      "queue/%s/fraction_of_%d_full" % (values_queue.name, capacity),
      tf.cast(values_queue.size(), tf.float32) * (1. / capacity))

  return values_queue
示例#20
0
    records = get_records(FLAGS.dataset_name,
                          FLAGS.dataset_split_name,
                          FLAGS.dataset_dir,
                          FLAGS.im_batch,
                          is_training=False)

    image, ih, iw, gt_boxes, gt_masks, num_instances, img_id = \
        coco.read(records)

    image, gt_boxes, gt_masks = \
        preprocess_coco.preprocess_image(image, gt_boxes, gt_masks)

    # using queue to input
    queue = tf.RandomShuffleQueue(capacity=12,
                                  min_after_dequeue=6,
                                  dtypes=(image.dtype, ih.dtype, iw.dtype,
                                          gt_boxes.dtype, gt_masks.dtype,
                                          num_instances.dtype, img_id.dtype))
    enqueue_op = queue.enqueue(
        (image, ih, iw, gt_boxes, gt_masks, num_instances, img_id))
    (image_, ih_, iw_, gt_boxes_, gt_masks_, num_instances_,
     img_id_) = queue.dequeue()
    qr = tf.train.QueueRunner(queue, [enqueue_op] * 4)

    sess = tf.Session()
    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())
    # init_op = tf.initialize_all_variables()

    coord = tf.train.Coordinator()
    enqueue_threads = qr.create_threads(sess, coord=coord, start=True)
示例#21
0
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2019/12/18 17:19
# @Author  : Leslee

import tensorflow as tf

q = tf.RandomShuffleQueue(capacity=10,min_after_dequeue=2,dtypes="float")

sess = tf.Session()

for i in range(0,10):
    sess.run(q.enqueue(i))

# for i in range(0,8):
#     print(sess.run(q.dequeue()))

run_options = tf.RunOptions(timeout_in_ms = 10000)
try:
    print(sess.run(q.dequeue(),options=run_options))
except tf.errors.DeadlineExceededError:
    print('out of range')






示例#22
0
def image_processing_module(dataset, train):
    """Contruct batches of training or evaluation examples from the image dataset.
    """

    batch_size = FLAGS.batch_size
    num_readers = FLAGS.num_readers
    num_preprocess_threads = FLAGS.num_preprocess_threads

    with tf.device('/cpu:0'):
        with tf.name_scope('batch_processing'):
            if train:
                filename_queue = tf.train.string_input_producer(dataset,
                                                                shuffle=True,
                                                                capacity=2)
            else:
                filename_queue = tf.train.string_input_producer(dataset,
                                                                shuffle=False,
                                                                capacity=1)
            examples_per_shard = 128
            # Shuffling queue size
            min_queue_examples = examples_per_shard * FLAGS.input_queue_memory_factor

            if train:
                examples_queue = tf.RandomShuffleQueue(
                    capacity=min_queue_examples + 3 * batch_size,
                    min_after_dequeue=min_queue_examples,
                    dtypes=[tf.string])
            else:
                examples_queue = tf.FIFOQueue(capacity=examples_per_shard +
                                              3 * batch_size,
                                              dtypes=[tf.string])

            # Create multiple readers to populate the queue of examples.
            if num_readers > 1:
                enqueue_ops = []
                for _ in range(num_readers):
                    reader = tf.TFRecordReader()
                    _, 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()
            else:
                reader = tf.TFRecordReader()
                _, example_serialized = reader.read(filename_queue)

            images_and_labels = []

            for thread_id in range(num_preprocess_threads):
                # Parse a serialized Examle proto to extract the image and metadata.
                image_buffer, labels = parse_example_proto(example_serialized)

                image = image_transform(image_buffer, train, thread_id)
                if image is not None:
                    images_and_labels.append([image, labels])

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

            #Reshape images into desired dimentions
            height = FLAGS.image_size
            width = FLAGS.image_size
            depth = 3

            images = tf.cast(images, tf.float32)
            images = tf.reshape(images,
                                shape=[batch_size, height, width, depth])

            return images, tf.reshape(label_index_batch, [batch_size])
示例#23
0
def cifar10(path,  # pylint: disable=invalid-name
            conv_channels=None,
            linear_layers=None,
            batch_norm=True,
            batch_size=128,
            num_threads=4,
            min_queue_examples=1000,
            mode="train"):
  """Cifar10 classification with a convolutional network."""

  # Data.
  _maybe_download_cifar10(path)

  # Read images and labels from disk.
  if mode == "train":
    filenames = [os.path.join(path,
                              CIFAR10_FOLDER,
                              "data_batch_{}.bin".format(i))
                 for i in xrange(1, 6)]
  elif mode == "test":
    filenames = [os.path.join(path, "test_batch.bin")]
  else:
    raise ValueError("Mode {} not recognised".format(mode))

  depth = 3
  height = 32
  width = 32
  label_bytes = 1
  image_bytes = depth * height * width
  record_bytes = label_bytes + image_bytes
  reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
  _, record = reader.read(tf.train.string_input_producer(filenames))
  record_bytes = tf.decode_raw(record, tf.uint8)

  label = tf.cast(tf.slice(record_bytes, [0], [label_bytes]), tf.int32)
  raw_image = tf.slice(record_bytes, [label_bytes], [image_bytes])
  image = tf.cast(tf.reshape(raw_image, [depth, height, width]), tf.float32)
  # height x width x depth.
  image = tf.transpose(image, [1, 2, 0])
  image = tf.div(image, 255)

  queue = tf.RandomShuffleQueue(capacity=min_queue_examples + 3 * batch_size,
                                min_after_dequeue=min_queue_examples,
                                dtypes=[tf.float32, tf.int32],
                                shapes=[image.get_shape(), label.get_shape()])
  enqueue_ops = [queue.enqueue([image, label]) for _ in xrange(num_threads)]
  tf.train.add_queue_runner(tf.train.QueueRunner(queue, enqueue_ops))

  # Network.
  def _conv_activation(x):  # pylint: disable=invalid-name
    return tf.nn.max_pool(tf.nn.relu(x),
                          ksize=[1, 2, 2, 1],
                          strides=[1, 2, 2, 1],
                          padding="SAME")

  conv = nn.ConvNet2D(output_channels=conv_channels,
                      kernel_shapes=[5],
                      strides=[1],
                      paddings=[nn.SAME],
                      activation=_conv_activation,
                      activate_final=True,
                      initializers=_nn_initializers,
                      use_batch_norm=batch_norm)

  if batch_norm:
    linear_activation = lambda x: tf.nn.relu(nn.BatchNorm()(x))
  else:
    linear_activation = tf.nn.relu

  mlp = nn.MLP(list(linear_layers) + [10],
               activation=linear_activation,
               initializers=_nn_initializers)
  network = nn.Sequential([conv, nn.BatchFlatten(), mlp])

  def build():
    image_batch, label_batch = queue.dequeue_many(batch_size)
    label_batch = tf.reshape(label_batch, [batch_size])

    output = network(image_batch)
    return _xent_loss(output, label_batch)

  return build
示例#24
0
def enqueue_data(data,
                 capacity,
                 shuffle=False,
                 min_after_dequeue=None,
                 seed=None):
  """Creates a queue filled from a numpy array or pandas `DataFrame`.

    Returns a queue filled with the rows of the given array or `DataFrame`. In
    the case of a pandas `DataFrame`, the first enqueued `Tensor` corresponds to
    the index of the `DataFrame`. For numpy arrays, the first enqueued `Tensor`
    contains the row number.

  Args:
    data: a numpy `ndarray or` pandas `DataFrame` that will be read into the
      queue.
    capacity: the capacity of the queue.
    shuffle: whether or not to shuffle the rows of the array.
    min_after_dequeue: minimum number of elements that can remain in the queue
    after a dequeue operation. Only used when `shuffle` is true. If not set,
    defaults to `capacity` / 4.
    seed: used to seed RandomShuffleQueue. Only used when `shuffle` is True.

  Returns:
    A queue filled with the rows of the given array or `DataFrame`.

  Raises:
    TypeError: `data` is not a Pandas `DataFrame` or a numpy `ndarray`.
  """
  # TODO(jamieas): create multithreaded version of enqueue_data.
  if isinstance(data, np.ndarray):
    dtypes = [tf.int64, tf.as_dtype(data.dtype)]
    shapes = [(), data.shape[1:]]
    get_feed_fn = _ArrayFeedFn
  elif HAS_PANDAS and isinstance(data, pd.DataFrame):
    dtypes = [tf.as_dtype(dt) for dt in [data.index.dtype] + list(data.dtypes)]
    shapes = [() for _ in dtypes]
    get_feed_fn = _PandasFeedFn
  else:
    raise TypeError(
        "data must be either a numpy array or pandas DataFrame if pandas is "
        "installed; got {}".format(
            type(data).__name__))

  placeholders = [tf.placeholder(*type_and_shape)
                  for type_and_shape in zip(dtypes, shapes)]
  if shuffle:
    min_after_dequeue = (capacity / 4 if min_after_dequeue is None else
                         min_after_dequeue)
    queue = tf.RandomShuffleQueue(capacity,
                                  min_after_dequeue,
                                  dtypes=dtypes,
                                  shapes=shapes,
                                  seed=seed)
  else:
    queue = tf.FIFOQueue(capacity, dtypes=dtypes, shapes=shapes)
  enqueue_op = queue.enqueue(placeholders)
  feed_fn = get_feed_fn(placeholders, data)
  queue_runner = fqr.FeedingQueueRunner(queue=queue,
                                        enqueue_ops=[enqueue_op],
                                        feed_fn=feed_fn)
  tf.train.add_queue_runner(queue_runner)
  return queue
示例#25
0
文件: train.py 项目: xu-cpp/Mask-RCNN
def train():
    """The main function that runs training"""

    ## data
    image, ih, iw, gt_boxes, gt_masks, num_instances, img_id = \
        datasets.get_dataset(FLAGS.dataset_name,
                             FLAGS.dataset_split_name,
                             FLAGS.dataset_dir,
                             FLAGS.im_batch,
                             is_training=True)

    data_queue = tf.RandomShuffleQueue(
        capacity=32,
        min_after_dequeue=16,
        dtypes=(image.dtype, ih.dtype, iw.dtype, gt_boxes.dtype,
                gt_masks.dtype, num_instances.dtype, img_id.dtype))
    enqueue_op = data_queue.enqueue(
        (image, ih, iw, gt_boxes, gt_masks, num_instances, img_id))
    data_queue_runner = tf.train.QueueRunner(data_queue, [enqueue_op] * 4)
    tf.add_to_collection(tf.GraphKeys.QUEUE_RUNNERS, data_queue_runner)
    (image, ih, iw, gt_boxes, gt_masks, num_instances,
     img_id) = data_queue.dequeue()
    im_shape = tf.shape(image)
    image = tf.reshape(image, (im_shape[0], im_shape[1], im_shape[2], 3))

    ## network
    logits, end_points, pyramid_map = network.get_network(
        FLAGS.network,
        image,
        weight_decay=FLAGS.weight_decay,
        is_training=True)
    outputs = pyramid_network.build(end_points,
                                    im_shape[1],
                                    im_shape[2],
                                    pyramid_map,
                                    num_classes=81,
                                    base_anchors=9,
                                    is_training=True,
                                    gt_boxes=gt_boxes,
                                    gt_masks=gt_masks,
                                    loss_weights=[0.2, 0.2, 1.0, 0.2, 1.0])

    total_loss = outputs['total_loss']
    losses = outputs['losses']
    batch_info = outputs['batch_info']
    regular_loss = tf.add_n(
        tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))

    input_image = end_points['input']
    final_box = outputs['final_boxes']['box']
    final_cls = outputs['final_boxes']['cls']
    final_prob = outputs['final_boxes']['prob']
    final_gt_cls = outputs['final_boxes']['gt_cls']
    gt = outputs['gt']

    #############################
    tmp_0 = outputs['losses']
    tmp_1 = outputs['losses']
    tmp_2 = outputs['losses']
    tmp_3 = outputs['losses']
    tmp_4 = outputs['losses']

    # tmp_0 = outputs['tmp_0']
    # tmp_1 = outputs['tmp_1']
    # tmp_2 = outputs['tmp_2']
    tmp_3 = outputs['tmp_3']
    tmp_4 = outputs['tmp_4']
    ############################

    ## solvers
    global_step = slim.create_global_step()
    update_op = solve(global_step)

    cropped_rois = tf.get_collection('__CROPPED__')[0]
    transposed = tf.get_collection('__TRANSPOSED__')[0]

    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.95)
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())
    sess.run(init_op)

    summary_op = tf.summary.merge_all()
    logdir = os.path.join(FLAGS.train_dir, strftime('%Y%m%d%H%M%S', gmtime()))
    if not os.path.exists(logdir):
        os.makedirs(logdir)
    summary_writer = tf.summary.FileWriter(logdir, graph=sess.graph)

    ## restore
    restore(sess)

    ## main loop
    coord = tf.train.Coordinator()
    threads = []
    # print (tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS))
    for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS):
        threads.extend(
            qr.create_threads(sess, coord=coord, daemon=True, start=True))

    tf.train.start_queue_runners(sess=sess, coord=coord)
    saver = tf.train.Saver(max_to_keep=20)

    for step in range(FLAGS.max_iters):

        start_time = time.time()

        s_, tot_loss, reg_lossnp, img_id_str, \
        rpn_box_loss, rpn_cls_loss, refined_box_loss, refined_cls_loss, mask_loss, \
        gt_boxesnp, \
        rpn_batch_pos, rpn_batch, refine_batch_pos, refine_batch, mask_batch_pos, mask_batch, \
        input_imagenp, final_boxnp, final_clsnp, final_probnp, final_gt_clsnp, gtnp, tmp_0np, tmp_1np, tmp_2np, tmp_3np, tmp_4np= \
                     sess.run([update_op, total_loss, regular_loss, img_id] +
                              losses +
                              [gt_boxes] +
                              batch_info +
                              [input_image] + [final_box] + [final_cls] + [final_prob] + [final_gt_cls] + [gt] + [tmp_0] + [tmp_1] + [tmp_2] + [tmp_3] + [tmp_4])

        duration_time = time.time() - start_time
        if step % 1 == 0:
            print(
                """iter %d: image-id:%07d, time:%.3f(sec), regular_loss: %.6f, """
                """total-loss %.4f(%.4f, %.4f, %.6f, %.4f, %.4f), """
                """instances: %d, """
                """batch:(%d|%d, %d|%d, %d|%d)""" %
                (step, img_id_str, duration_time, reg_lossnp, tot_loss,
                 rpn_box_loss, rpn_cls_loss, refined_box_loss,
                 refined_cls_loss, mask_loss, gt_boxesnp.shape[0],
                 rpn_batch_pos, rpn_batch, refine_batch_pos, refine_batch,
                 mask_batch_pos, mask_batch))

            # draw_bbox(step,
            #           np.uint8((np.array(input_imagenp[0])/2.0+0.5)*255.0),
            #           name='est',
            #           bbox=final_boxnp,
            #           label=final_clsnp,
            #           prob=final_probnp,
            #           gt_label=np.argmax(np.asarray(final_gt_clsnp),axis=1),
            #           )

            # draw_bbox(step,
            #           np.uint8((np.array(input_imagenp[0])/2.0+0.5)*255.0),
            #           name='gt',
            #           bbox=gtnp[:,0:4],
            #           label=np.asarray(gtnp[:,4], dtype=np.uint8),
            #           )

            print("labels")
            # print (cat_id_to_cls_name(np.unique(np.argmax(np.asarray(final_gt_clsnp),axis=1)))[1:])
            # print (cat_id_to_cls_name(np.unique(np.asarray(gt_boxesnp, dtype=np.uint8)[:,4])))
            print(
                cat_id_to_cls_name(
                    np.unique(np.argmax(np.asarray(tmp_3np), axis=1)))[1:])
            #print (cat_id_to_cls_name(np.unique(np.argmax(np.asarray(gt_boxesnp)[:,4],axis=1))))
            print("classes")
            print(
                cat_id_to_cls_name(
                    np.unique(np.argmax(np.array(tmp_4np), axis=1))))
            # print (np.asanyarray(tmp_3np))

            #print ("ordered rois")
            #print (np.asarray(tmp_0np)[0])
            #print ("pyramid_feature")
            #print ()
            #print(np.unique(np.argmax(np.array(final_probnp),axis=1)))
            #for var, val in zip(tmp_2, tmp_2np):
            #    print(var.name)
            #print(np.argmax(np.array(tmp_0np),axis=1))

            if np.isnan(tot_loss) or np.isinf(tot_loss):
                print(gt_boxesnp)
                raise

        if step % 100 == 0:
            summary_str = sess.run(summary_op)
            summary_writer.add_summary(summary_str, step)
            summary_writer.flush()

        if (step % 10000 == 0 or step + 1 == FLAGS.max_iters) and step != 0:
            checkpoint_path = os.path.join(
                FLAGS.train_dir,
                FLAGS.dataset_name + '_' + FLAGS.network + '_model.ckpt')
            saver.save(sess, checkpoint_path, global_step=step)

        if coord.should_stop():
            coord.request_stop()
            coord.join(threads)
示例#26
0
def style_image_inputs(style_dataset_file, batch_size=None, image_size=None,
                       square_crop=False, shuffle=True):
  """Loads a style image at random.

  Args:
    style_dataset_file: str, path to the tfrecord dataset of style files.
        The dataset is produced via the create_style_dataset.py script and is
        made of Example protobufs with the following features:
        * 'image_raw': byte encoding of the JPEG string of the style image.
        * 'label': integer identifier of the style image in [0, N - 1], where
              N is the number of examples in the dataset.
        * 'vgg_16/<LAYER_NAME>': Gram matrix at layer <LAYER_NAME> of the VGG-16
              network (<LAYER_NAME> in {conv,pool}{1,2,3,4,5}) for the style
              image.
    batch_size: int. If provided, batches style images. Defaults to None.
    image_size: int. The images will be resized bilinearly so that the smallest
        side has size image_size. Defaults to None.
    square_crop: bool. If True, square-crops to [image_size, image_size].
        Defaults to False.
    shuffle: bool, whether to shuffle style files at random. Defaults to True.

  Returns:
    If batch_size is defined, a 4-D tensor of shape [batch_size, ?, ?, 3] with
    values in [0, 1] for the style image, and 1-D tensor for the style label.

  Raises:
    ValueError: if center cropping is requested but no image size is provided,
        or if batch size is specified but center-cropping is not requested.
  """
  vgg_layers = ['vgg_16/conv1', 'vgg_16/pool1', 'vgg_16/conv2', 'vgg_16/pool2',
                'vgg_16/conv3', 'vgg_16/pool3', 'vgg_16/conv4', 'vgg_16/pool4',
                'vgg_16/conv5', 'vgg_16/pool5']

  if square_crop and image_size is None:
    raise ValueError('center-cropping requires specifying the image size.')
  if batch_size is not None and not square_crop:
    raise ValueError('batching requires center-cropping.')

  with tf.name_scope('style_image_processing'):
    filename_queue = tf.train.string_input_producer(
        [style_dataset_file], shuffle=False, capacity=1,
        name='filename_queue')
    if shuffle:
      examples_queue = tf.RandomShuffleQueue(
          capacity=64,
          min_after_dequeue=32,
          dtypes=[tf.string], name='random_examples_queue')
    else:
      examples_queue = tf.FIFOQueue(
          capacity=64,
          dtypes=[tf.string], name='fifo_examples_queue')
    reader = tf.TFRecordReader()
    _, value = reader.read(filename_queue)
    enqueue_ops = [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()
    features = tf.parse_single_example(
        example_serialized,
        features={'label': tf.FixedLenFeature([], tf.int64),
                  'image_raw': tf.FixedLenFeature([], tf.string),
                  'vgg_16/conv1': tf.FixedLenFeature([64, 64], tf.float32),
                  'vgg_16/pool1': tf.FixedLenFeature([64, 64], tf.float32),
                  'vgg_16/conv2': tf.FixedLenFeature([128, 128], tf.float32),
                  'vgg_16/pool2': tf.FixedLenFeature([128, 128], tf.float32),
                  'vgg_16/conv3': tf.FixedLenFeature([256, 256], tf.float32),
                  'vgg_16/pool3': tf.FixedLenFeature([256, 256], tf.float32),
                  'vgg_16/conv4': tf.FixedLenFeature([512, 512], tf.float32),
                  'vgg_16/pool4': tf.FixedLenFeature([512, 512], tf.float32),
                  'vgg_16/conv5': tf.FixedLenFeature([512, 512], tf.float32),
                  'vgg_16/pool5': tf.FixedLenFeature([512, 512], tf.float32)})
    image = tf.image.decode_jpeg(features['image_raw'])
    label = features['label']
    gram_matrices = [features[vgg_layer] for vgg_layer in vgg_layers]
    image.set_shape([None, None, 3])

    if image_size:
      if square_crop:
        image = _aspect_preserving_resize(image, image_size + 2)
        image = _central_crop([image], image_size, image_size)[0]
        image.set_shape([image_size, image_size, 3])
      else:
        image = _aspect_preserving_resize(image, image_size)

    image = tf.to_float(image) / 255.0

    if batch_size is None:
      image = tf.expand_dims(image, 0)
    else:
      image_label_gram_matrices = tf.train.batch(
          [image, label] + gram_matrices, batch_size=batch_size)
      image, label = image_label_gram_matrices[:2]
      gram_matrices = image_label_gram_matrices[2:]

    gram_matrices = dict([(vgg_layer, gram_matrix)
                          for vgg_layer, gram_matrix
                          in zip(vgg_layers, gram_matrices)])
    return image, label, gram_matrices
示例#27
0
    def get_batch_input_tensors(self):
        with tf.name_scope('batch_processing'):
            data_files = self.dataset.data_files()
            if data_files is None:
                raise ValueError('No data files found for this dataset')

            # Create filename_queue
            if self.mode == 'train' or self.need_shuffle:
                filename_queue = tf.train.string_input_producer(data_files,
                                                                shuffle=True,
                                                                capacity=16)
            else:
                filename_queue = tf.train.string_input_producer(data_files,
                                                                shuffle=False,
                                                                capacity=1)

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

            if self.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
            min_queue_examples = examples_per_shard * INPUT_QUEUE_MEMORY_FACTOR
            if self.mode == 'train' or self.need_shuffle:
                print('use random shuffle example queue')
                examples_queue = tf.RandomShuffleQueue(
                    capacity=min_queue_examples + 3 * self.batch_size,
                    min_after_dequeue=min_queue_examples,
                    dtypes=[tf.string])
            else:
                print('use FIFO example queue')
                examples_queue = tf.FIFOQueue(
                    capacity=examples_per_shard + 3 * self.batch_size,
                    dtypes=[tf.string])

            # Create multiple readers to populate the queue of examples.
            if self.num_readers > 1:
                enqueue_ops = []
                for _ in range(self.num_readers):
                    reader = self.dataset.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()
            else:
                reader = self.dataset.reader()
                _, example_serialized = reader.read(filename_queue)

            images_and_labels = []
            for thread_id in range(self.num_preprocess_threads):
                # Parse a serialized Example proto to extract the image and metadata.
                image, label_index = self.cifar10_parse_example_proto(example_serialized)
                # image = self.cifar10_preprocess(image_buffer)
                images_and_labels.append([image, label_index])

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

            # Reshape images into these desired dimensions.
            height = self.image_size
            width = self.image_size
            depth = 3

            images = tf.cast(images, tf.float32)
            images = tf.reshape(images, shape=[self.batch_size, height, width, depth])

            # Display the training images in the visualizer.
            tf.summary.image('images', images)
            return images, tf.reshape(label_index_batch, [self.batch_size])
示例#28
0
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.
    if num_readers > 1:
      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()
    else:
      reader = imagenet.reader()
      _, example_serialized = reader.read(filename_queue)

    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 = _decode_jpeg(image_buffer)

      # 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.image_summary('images', images)

    return images, tf.reshape(label_index_batch, [batch_size])
示例#29
0
def batch_inputs(dataset,
                 batch_size,
                 train,
                 num_preprocess_threads=None,
                 num_readers=1):
    """Contruct batches of training or evaluation examples from the image dataset.

  Args:
    dataset: instance of Dataset class specifying the dataset.
      See dataset.py for details.
    batch_size: integer
    train: boolean
    num_preprocess_threads: integer, total number of preprocessing threads
    num_readers: integer, number of parallel readers

  Returns:
    images: 4-D float Tensor of a batch of images
    labels: 1-D integer Tensor of [batch_size].

  Raises:
    ValueError: if data is not found
  """
    with tf.name_scope('batch_processing'):
        data_files = dataset.data_files()
        if data_files is None:
            raise ValueError('No data files found for this dataset')

        # Create filename_queue
        if train:
            filename_queue = tf.train.string_input_producer(data_files,
                                                            shuffle=True,
                                                            capacity=64)
        else:
            filename_queue = tf.train.string_input_producer(data_files,
                                                            shuffle=False,
                                                            capacity=1)
        if num_preprocess_threads is None:
            num_preprocess_threads = FLAGS.num_preprocess_threads

        # to reduce the num of preprocessing threads, no longer require this
        #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 is None:
            num_readers = FLAGS.num_readers

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

        # Approximate number of examples per shard.
        examples_per_shard = FLAGS.examples_per_shard
        # 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
        if train:
            min_queue_examples = examples_per_shard * FLAGS.input_queue_memory_factor
            examples_queue = tf.RandomShuffleQueue(
                capacity=min_queue_examples + 3 * batch_size,
                min_after_dequeue=min_queue_examples,
                dtypes=[tf.string])
        else:
            examples_queue = tf.FIFOQueue(capacity=examples_per_shard +
                                          3 * batch_size,
                                          dtypes=[tf.string])

        # Create multiple readers to populate the queue of examples.
        if num_readers > 1:
            enqueue_ops = []
            for _ in range(num_readers):
                reader = dataset.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()
        else:
            reader = dataset.reader()
            _, example_serialized = reader.read(filename_queue)

        if FLAGS.use_MIMO_inputs_pipeline:
            # The new convention for images and labels is a generalized one
            # images: all inputs; labels: all outputs that needs to be predicted
            datan = []
            for thread_id in range(num_preprocess_threads):
                # 1. this function returns multiple input data (could include both labels and images).
                # This will enable more complex models such as LRCN with egomotion inputs to be able
                # to run with this framework
                # 2. The parse_example_proto function could return more than 1 input for one
                # example_serialized.
                # 3. the returned format is a list of tensors [Tensor1, Tensor2,...., Tensor_n],
                # each of the tensor denotes a small batch of one variable.
                # The tensors for the video might be 5-dim, [batch_size, nframes, H, W, C]
                # 4. We expect future data augmentation code to appear in parse_example_proto
                # itself, since inheriently the augmentation is highly dataset dependent.
                # 5. the parse_example_proto return net_input and net_output as two seperate tensor lists
                net_inputs, net_outputs = dataset.parse_example_proto(
                    example_serialized)
                net_inputs, net_outputs = dataset.augmentation(
                    train, net_inputs, net_outputs)
                datan.append(net_inputs + net_outputs)

            # the single thread batch_join dequeue_many operation might be the bottleneck.
            if net_inputs[0].get_shape()[0].value == batch_size:
                print(
                    "output batch of parse_example_proto == required batchsize (%d), no batching needed"
                    % batch_size)
                print("Omitting the batch_join queue")
                joins = datan
                one_joined = datan[-1]
            else:
                # this is quite slow, avoid using this
                joins = []
                for i in range(FLAGS.num_batch_join):
                    reduced_factor = max(
                        math.ceil(1.0 * num_preprocess_threads /
                                  FLAGS.num_batch_join), 2)
                    one_joined = tf.train.batch_join(tensors_list=datan,
                                                     batch_size=batch_size,
                                                     capacity=reduced_factor *
                                                     batch_size,
                                                     enqueue_many=True)
                    joins.append(one_joined)
                print(FLAGS.num_batch_join,
                      " batch_joins, each of them capacity is, ",
                      reduced_factor * batch_size, " instances")

            # add a buffering queue to remove the dequeue_many time
            print("buffer queue capacity is: ", FLAGS.num_batch_join,
                  " batches")
            capacity = FLAGS.num_batch_join
            buffer_queue = tf.FIFOQueue(
                capacity=capacity,
                dtypes=[x.dtype for x in one_joined],
                shapes=[x.get_shape() for x in one_joined],
                name="buffer_queue")

            tf.summary.scalar(
                "queue/%s/fraction_of_%d_full" % (buffer_queue.name, capacity),
                tf.cast(buffer_queue.size(), tf.float32) * (1. / capacity))

            buffer_ops = [buffer_queue.enqueue(join) for join in joins]

            tf.train.queue_runner.add_queue_runner(
                tf.train.queue_runner.QueueRunner(buffer_queue, buffer_ops))
            data_joined = buffer_queue.dequeue()
            # end of buffer queue

            # The CPU to GPU memory transfer still not resolved

            # recover the inputs and outputs
            joined_inputs = data_joined[:len(net_inputs)]
            joined_outputs = data_joined[len(net_inputs):]

            # dataset's visualizer
            # since only the dataset knows how to visualize the data, let the dataset to provide such method
            dataset.visualize(joined_inputs, joined_outputs)
            return joined_inputs, joined_outputs

        else:
            raise ValueError("have to use MIMO input pipeline")
示例#30
0
def build_input(dataset, batch_size, mode):
  if mode == 'train':
    data_path = '../cifar10/data_batch*'
  elif mode == 'test':
    data_path = '../cifar10/test_batch.bin'

  tf.set_random_seed(2)
  image_size = 32
  if dataset == 'cifar10':
    label_bytes = 1
    label_offset = 0
    num_classes = 10
  elif dataset == 'cifar100':
    label_bytes = 1
    label_offset = 1
    num_classes = 100
  else:
    raise ValueError('Not supported dataset %s', dataset)

  depth = 3
  image_bytes = image_size * image_size * depth
  record_bytes = label_bytes + label_offset + image_bytes

  data_files = tf.gfile.Glob(data_path)
  file_queue = tf.train.string_input_producer(data_files, shuffle=True)
  # Read examples from files in the filename queue.
  reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
  _, value = reader.read(file_queue)

  # Convert these examples to dense labels and processed images.
  record = tf.reshape(tf.decode_raw(value, tf.uint8), [record_bytes])
  label = tf.cast(tf.slice(record, [label_offset], [label_bytes]), tf.int32)
  # Convert from string to [depth * height * width] to [depth, height, width].
  depth_major = tf.reshape(tf.slice(record, [label_offset + label_bytes], [image_bytes]),
                           [depth, image_size, image_size])
  # Convert from [depth, height, width] to [height, width, depth].
  image = tf.cast(tf.transpose(depth_major, [1, 2, 0]), tf.float32)

  if mode == 'train':
    image = tf.image.resize_image_with_crop_or_pad(
        image, image_size+4, image_size+4)
    image = tf.random_crop(image, [image_size, image_size, 3], seed=3)
    image = tf.image.random_flip_left_right(image, seed=5)
    # Brightness/saturation/constrast provides small gains .2%~.5% on cifar.
    # image = tf.image.random_brightness(image, max_delta=63. / 255.)
    # image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
    # image = tf.image.random_contrast(image, lower=0.2, upper=1.8)
    image = tf.image.per_image_standardization(image)

    example_queue = tf.RandomShuffleQueue(
        capacity=16 * batch_size,
        min_after_dequeue=8 * batch_size,
        dtypes=[tf.float32, tf.int32],
        shapes=[[image_size, image_size, depth], [1]],
        seed=7)
    num_threads = 16
  else:
    image = tf.image.resize_image_with_crop_or_pad(
        image, image_size, image_size)
    image = tf.image.per_image_standardization(image)

    example_queue = tf.FIFOQueue(
        3 * batch_size,
        dtypes=[tf.float32, tf.int32],
        shapes=[[image_size, image_size, depth], [1]])
    num_threads = 1

  example_enqueue_op = example_queue.enqueue([image, label])
  tf.train.add_queue_runner(tf.train.queue_runner.QueueRunner(
      example_queue, [example_enqueue_op] * num_threads))

  # Read 'batch' labels + images from the example queue.
  images, labels = example_queue.dequeue_many(batch_size)
  labels = tf.squeeze(labels)
  assert len(images.get_shape()) == 4
  assert images.get_shape()[0] == batch_size
  assert images.get_shape()[-1] == 3
  assert len(labels.get_shape()) == 1

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