def tower_loss(scope):
    """Calculate the total loss on a single tower running the CIFAR model.

  Args:
    scope: unique prefix string identifying the CIFAR tower, e.g. 'tower_0'

  Returns:
     Tensor of shape [] containing the total loss for a batch of data
  """
    # Get images and labels for CIFAR-10.
    images, labels = svhn.distorted_inputs()

    # Build inference Graph.
    logits = svhn.inference(images)

    # Build the portion of the Graph calculating the losses. Note that we will
    # assemble the total_loss using a custom function below.
    _ = svhn.loss(logits, labels)

    # Assemble all of the losses for the current tower only.
    losses = tf.get_collection('losses', scope)

    # Calculate the total loss for the current tower.
    total_loss = tf.add_n(losses, name='total_loss')

    # Attach a scalar summary to all individual losses and the total loss; do the
    # same for the averaged version of the losses.
    for l in losses + [total_loss]:
        # Remove 'tower_[0-9]/' from the name in case this is a multi-GPU training
        # session. This helps the clarity of presentation on tensorboard.
        loss_name = re.sub('%s_[0-9]*/' % svhn.TOWER_NAME, '', l.op.name)
        tf.summary.scalar(loss_name, l)

    return total_loss
Exemple #2
0
def train():
    with tf.Graph().as_default():
        global_step = tf.Variable(0, trainable=False)

        # Get images and labels for CIFAR-10.
        images, labels = svhn.distorted_inputs()

        # Build a Graph that computes the logits predictions from the
        # inference model.
        logits = svhn.inference(images)

        # Calculate loss.
        loss = svhn.loss(logits, labels)

        # Build a Graph that trains the model with one batch of examples and
        # updates the model parameters.
        train_op = svhn.train(loss, global_step)

        # Create a saver.
        saver = tf.train.Saver(tf.all_variables())

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.merge_all_summaries()

        # Build an initialization operation to run below.
        init = tf.initialize_all_variables()

        # Start running operations on the Graph.
        sess = tf.Session(config=tf.ConfigProto(log_device_placement=FLAGS.log_device_placement))
        sess.run(init)

        # Start the queue runners.
        tf.train.start_queue_runners(sess=sess)

        summary_writer = tf.train.SummaryWriter(FLAGS.train_dir, graph_def=sess.graph_def)
        for step in xrange(FLAGS.max_steps):
            start_time = time.time()
            _, loss_value = sess.run([train_op, loss])
            duration = time.time() - start_time

            assert not np.isnan(loss_value), "Model diverged with loss = NaN"

            if step % 10 == 0:
                num_examples_per_step = FLAGS.batch_size
                examples_per_sec = num_examples_per_step / duration
                sec_per_batch = float(duration)

                format_str = "%s: step %d, loss = %.2f (%.1f examples/sec; %.3f " "sec/batch)"
                print(format_str % (datetime.now(), step, loss_value, examples_per_sec, sec_per_batch))

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

            # Save the model checkpoint periodically.
            if step % 1000 == 0 or (step + 1) == FLAGS.max_steps:
                checkpoint_path = os.path.join(FLAGS.train_dir, "model.ckpt")
                saver.save(sess, checkpoint_path, global_step=step)
def train():
    """Train CIFAR-10 for a number of steps."""
    with tf.Graph().as_default():
        global_step = tf.contrib.framework.get_or_create_global_step()

        # Force input pipeline to CPU:0 to avoid operations sometimes ending up on
        # GPU and resulting in a slow down.
        with tf.device('/cpu:0'):
            images, labels = svhn.distorted_inputs()

        # Build a Graph that computes the logits predictions from the
        # inference model.
        logits = svhn.inference(images)

        # Calculate loss.
        loss = svhn.loss(logits, labels)

        # Build a Graph that trains the model with one batch of examples and
        # updates the model parameters.
        train_op = svhn.train(loss, global_step)

        class _LoggerHook(tf.train.SessionRunHook):
            """Logs loss and runtime."""
            def begin(self):
                self._step = -1
                self._start_time = time.time()

            def before_run(self, run_context):
                self._step += 1
                return tf.train.SessionRunArgs(loss)  # Asks for loss value.

            def after_run(self, run_context, run_values):
                if self._step % FLAGS.log_frequency == 0:
                    current_time = time.time()
                    duration = current_time - self._start_time
                    self._start_time = current_time

                    loss_value = run_values.results
                    examples_per_sec = FLAGS.log_frequency * FLAGS.batch_size / duration
                    sec_per_batch = float(duration / FLAGS.log_frequency)

                    format_str = (
                        '%s: step %d, loss = %.2f (%.1f examples/sec; %.3f '
                        'sec/batch)')
                    print(format_str % (datetime.now(), self._step, loss_value,
                                        examples_per_sec, sec_per_batch))

        with tf.train.MonitoredTrainingSession(
                checkpoint_dir=FLAGS.train_dir,
                hooks=[
                    tf.train.StopAtStepHook(last_step=FLAGS.max_steps),
                    tf.train.NanTensorHook(loss),
                    _LoggerHook()
                ],
                config=tf.ConfigProto(log_device_placement=FLAGS.
                                      log_device_placement)) as mon_sess:
            while not mon_sess.should_stop():
                mon_sess.run(train_op)
def train():
    """Train SVHN for a number of steps."""
    with tf.Graph().as_default():
        global_step = tf.Variable(0, trainable=False)
        # Get images and labels for SVHN with mat file
        images, labels = svhn.distorted_inputs()
        # Build a Graph that computes the logits predictions from
        # inference model.
        logits = svhn.inference(images)

        # Calculate loss.
        loss = svhn.loss(logits, labels)

        # Build a Graph that trains the model with one batch of examples
        # and updates the model parm
        train_op = svhn.train(loss, global_step)

        # Create a saver.
        saver = tf.train.Saver(tf.all_variables())

        # Build an initialization operation to run.
        init = tf.initialize_all_variables()

        # Start running operations on the Graph.
        sess = tf.Session(config=tf.ConfigProto(
            log_device_placement=FLAGS.log_device_placement))
        sess.run(init)

        # Start the queue runners.
        tf.train.start_queue_runners(sess=sess)

        # summary_writer = tf.train.SummaryWriter(FLAGS.train_dir, sess.graph)

        for step in xrange(FLAGS.max_steps):
            start_time = time.time()
            _, loss_value = sess.run([train_op, loss])
            duration = time.time() - start_time

            assert not np.isnan(loss_value), 'Model diverged with loss = NaN'

            if step % 10 == 0:
                num_examples_per_step = FLAGS.batch_size
                examples_per_sec = num_examples_per_step / duration
                sec_per_batch = float(duration)
                format_str = (
                    '%s: step %d, loss = %.2f (%.1f examples/sec; %.3f '
                    'sec/batch)')
                print(format_str % (datetime.now(), step, loss_value,
                                    examples_per_sec, sec_per_batch))

            # Save the model checkpoint periodically.
            if step % 1000 == 0 or (step + 1) == FLAGS.max_steps:
                checkpoint_path = os.path.join(FLAGS.train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)
Exemple #5
0
def get_data(FLAGS, dataset):
    tr_data = None
    tr_label = None
    image_size = None
    channel_num = None
    output_num = None
    if dataset == 'cifar10':
        tr_data, tr_label = cifar10_input.distorted_inputs(
            FLAGS.cifar_data_dir, FLAGS.batch_size)
        image_size = cifar10_input.IMAGE_SIZE
        channel_num = 3
        output_num = 10
    elif dataset == 'svhn':
        tr_data, tr_label = svhn.distorted_inputs(FLAGS.svhn_data_dir,
                                                  FLAGS.batch_size)
        image_size = svhn.IMAGE_SIZE
        channel_num = 3
        output_num = 10
    elif dataset == 'cifar20':
        tr_data, tr_label = cifar100_input.distorted_inputs(
            20, FLAGS.cifar100_data_dir, FLAGS.batch_size)
        image_size = cifar100_input.IMAGE_SIZE
        channel_num = 3
        output_num = 20
    elif dataset == 'mnist1':
        tr_data, tr_label = binary_mnist_input.read_train_data(
            FLAGS, FLAGS.a1, FLAGS.a2)
        image_size = 28
        channel_num = 1
        output_num = 2
    elif dataset == 'mnist2':
        tr_data, tr_label = binary_mnist_input.read_train_data(
            FLAGS, FLAGS.b1, FLAGS.b2)
        image_size = 28
        channel_num = 1
        output_num = 2
    elif dataset == 'mnist3':
        tr_data, tr_label = binary_mnist_input.read_train_data(
            FLAGS, FLAGS.c1, FLAGS.c2)
        image_size = 28
        channel_num = 1
        output_num = 2
    elif dataset == 'mnist4':
        tr_data, tr_label = binary_mnist_input.read_train_data(
            FLAGS, FLAGS.d1, FLAGS.d2)
        image_size = 28
        channel_num = 1
        output_num = 2
    else:
        raise ValueError("No such dataset")

    return tr_data, tr_label, image_size, channel_num, output_num
def train():
  """Train CIFAR-10 for a number of steps."""
  with tf.Graph().as_default(), tf.device('/cpu:0'):
    # Create a variable to count the number of train() calls. This equals the
    # number of batches processed * FLAGS.num_gpus.
    global_step = tf.get_variable(
        'global_step', [],
        initializer=tf.constant_initializer(0), trainable=False)

    # Calculate the learning rate schedule.
    num_batches_per_epoch = (svhn.NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN /
                             FLAGS.batch_size)
    decay_steps = int(num_batches_per_epoch * svhn.NUM_EPOCHS_PER_DECAY)

    # Decay the learning rate exponentially based on the number of steps.
    lr = tf.train.exponential_decay(svhn.INITIAL_LEARNING_RATE,
                                    global_step,
                                    decay_steps,
                                    svhn.LEARNING_RATE_DECAY_FACTOR,
                                    staircase=True)

    # Create an optimizer that performs gradient descent.
    opt = tf.train.GradientDescentOptimizer(lr)

    # Get images and labels for CIFAR-10.
    images, labels = svhn.distorted_inputs()
    batch_queue = tf.contrib.slim.prefetch_queue.prefetch_queue(
          [images, labels], capacity=2 * FLAGS.num_gpus)
    # Calculate the gradients for each model tower.
    tower_grads = []
    with tf.variable_scope(tf.get_variable_scope()):
      for i in xrange(FLAGS.num_gpus):
        with tf.device('/gpu:%d' % i):
          with tf.name_scope('%s_%d' % (svhn.TOWER_NAME, i)) as scope:
            # Dequeues one batch for the GPU
            image_batch, label_batch = batch_queue.dequeue()
            # Calculate the loss for one tower of the CIFAR model. This function
            # constructs the entire CIFAR model but shares the variables across
            # all towers.
            loss = tower_loss(scope, image_batch, label_batch)

            # Reuse variables for the next tower.
            tf.get_variable_scope().reuse_variables()

            # Retain the summaries from the final tower.
            summaries = tf.get_collection(tf.GraphKeys.SUMMARIES, scope)

            # Calculate the gradients for the batch of data on this CIFAR tower.
            grads = opt.compute_gradients(loss)

            # Keep track of the gradients across all towers.
            tower_grads.append(grads)

    # We must calculate the mean of each gradient. Note that this is the
    # synchronization point across all towers.
    grads = average_gradients(tower_grads)

    # Add a summary to track the learning rate.
    summaries.append(tf.summary.scalar('learning_rate', lr))

    # Add histograms for gradients.
    for grad, var in grads:
      if grad is not None:
        summaries.append(tf.summary.histogram(var.op.name + '/gradients', grad))

    # Apply the gradients to adjust the shared variables.
    apply_gradient_op = opt.apply_gradients(grads, global_step=global_step)

    # Add histograms for trainable variables.
    for var in tf.trainable_variables():
      summaries.append(tf.summary.histogram(var.op.name, var))

    # Track the moving averages of all trainable variables.
    variable_averages = tf.train.ExponentialMovingAverage(
        svhn.MOVING_AVERAGE_DECAY, global_step)
    variables_averages_op = variable_averages.apply(tf.trainable_variables())

    # Group all updates to into a single train op.
    train_op = tf.group(apply_gradient_op, variables_averages_op)

    # Create a saver.
    saver = tf.train.Saver(tf.global_variables())

    # Build the summary operation from the last tower summaries.
    summary_op = tf.summary.merge(summaries)

    # Build an initialization operation to run below.
    init = tf.global_variables_initializer()

    # Start running operations on the Graph. allow_soft_placement must be set to
    # True to build towers on GPU, as some of the ops do not have GPU
    # implementations.
    sess = tf.Session(config=tf.ConfigProto(
        allow_soft_placement=True,
        log_device_placement=FLAGS.log_device_placement))
    sess.run(init)

    # Start the queue runners.
    tf.train.start_queue_runners(sess=sess)

    summary_writer = tf.summary.FileWriter(FLAGS.train_dir, sess.graph)

    for step in xrange(FLAGS.max_steps):
      start_time = time.time()
      _, loss_value = sess.run([train_op, loss])
      duration = time.time() - start_time

      assert not np.isnan(loss_value), 'Model diverged with loss = NaN'

      if step % 10 == 0:
        num_examples_per_step = FLAGS.batch_size * FLAGS.num_gpus
        examples_per_sec = num_examples_per_step / duration
        sec_per_batch = duration / FLAGS.num_gpus

        format_str = ('%s: step %d, loss = %.2f (%.1f examples/sec; %.3f '
                      'sec/batch)')
        print (format_str % (datetime.now(), step, loss_value,
                             examples_per_sec, sec_per_batch))

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

      # Save the model checkpoint periodically.
      if step % 1000 == 0 or (step + 1) == FLAGS.max_steps:
        checkpoint_path = os.path.join(FLAGS.train_dir, 'model.ckpt')
        saver.save(sess, checkpoint_path, global_step=step)
def train():
    with tf.Graph().as_default() as graph:
        global_step = tf.Variable(0, trainable=False)

        # Get images and labels for CIFAR-10.
        images, labels = svhn.distorted_inputs()

        logits1, logits2, logits3, logits4, logits5, logits6 = svhn.net_1(
            images, 0.71)

        loss = svhn.loss(logits1, logits2, logits3, logits4, logits5, logits6,
                         labels)

        pred = tf.stack([tf.argmax(tf.nn.softmax(logits1), 1),\
          tf.argmax(tf.nn.softmax(logits2), 1),\
          tf.argmax(tf.nn.softmax(logits3), 1),\
          tf.argmax(tf.nn.softmax(logits4), 1),\
          tf.argmax(tf.nn.softmax(logits5), 1),\
          tf.argmax(tf.nn.softmax(logits6), 1)], axis=1)

        # Build a Graph that trains the model with one batch of examples and
        # updates the model parameters.
        train_op = svhn.train(loss, global_step)

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.summary.merge_all()

        # Build an initialization operation to run below.
        init = tf.global_variables_initializer()

        with tf.Session() as sess:
            sess.run(init)
            variable_averages = tf.train.ExponentialMovingAverage(
                svhn.MOVING_AVERAGE_DECAY)
            variables_to_restore = variable_averages.variables_to_restore()

            saver = tf.train.Saver(variables_to_restore)
            ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)

            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
            """ Save Whole Model to model.pb
      for v in tf.trainable_variables():
        # assign the ExponentialMovingAverage value to the real variable
        name = v.name.split(':')[0]+'/ExponentialMovingAverage'
        sess.run(tf.assign(v, variables_to_restore[name]))
      out_graph_def = graph_util.convert_variables_to_constants(sess, graph.as_graph_def(), ['out1/Add','out2/Add','out3/Add','out4/Add','out5/Add','out6/Add', 'shuffle_batch'])
      with tf.gfile.GFile("model.pb", "wb") as f:
        f.write(out_graph_def.SerializeToString())
      """

            tf.train.start_queue_runners(sess=sess)

            summary_writer = tf.summary.FileWriter(FLAGS.train_dir,
                                                   graph=sess.graph)

            for step in xrange(FLAGS.max_steps):
                start_time = time.time()
                _, loss_value, prediction, label = sess.run(
                    [train_op, loss, pred, labels])
                duration = time.time() - start_time
                assert not np.isnan(
                    loss_value), 'Model diverged with loss = NaN'

                if step % 100 == 0:
                    num_examples_per_step = FLAGS.batch_size
                    examples_per_sec = num_examples_per_step / duration
                    sec_per_batch = float(duration)
                    true_count = 0
                    for x in range(num_examples_per_step):
                        current_pred = np.array(
                            prediction[x]).astype(int).tostring()
                        correct_pred = np.array(
                            label[x]).astype(int).tostring()
                        if current_pred == correct_pred:
                            true_count += 1

                    format_str = (
                        '%s: step %d, loss = %.6f, acc = %.6f%% (%.1f examples/sec; %.3f '
                        'sec/batch)')
                    print(format_str % (datetime.now(), step, loss_value, 100 *
                                        (true_count / num_examples_per_step),
                                        examples_per_sec, sec_per_batch))
                    # print(prediction)

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

                # Save the model checkpoint periodically.
                if step % 1000 == 0 or (step + 1) == FLAGS.max_steps:
                    checkpoint_path = os.path.join(FLAGS.train_dir,
                                                   'model.ckpt')
                    saver.save(sess, checkpoint_path, global_step=step)