def train_step_fn(sess, train_op, global_step, train_step_kwargs):
    """Function that takes a gradient step and specifies whether to stop.
      (Wrapper function for train_step in learning.py)
  Args:
    sess: The current session.
    train_op: An `Operation` that evaluates the gradients and returns the
      total loss.
    global_step: A `Tensor` representing the global training step.
    train_step_kwargs: A dictionary of keyword arguments (using this here to 
    pass in my eval_op for validation set evaluation, so we can do evaluation
    during training).

  Returns:
    The total loss and a boolean indicating whether or not to stop training.

  """

    total_loss, should_stop = learning.train_step(sess, train_op, global_step,
                                                  train_step_kwargs)

    should_val = sess.run(train_step_kwargs['should_val'])
    global_step = sess.run(global_step)

    if should_val:
        # validate

        print('validating model on step %d' % global_step)
        sess.run([train_step_kwargs['eval_op']])


#  train_step_kwargs['curr_step'] += 1

    return [total_loss, should_stop]
Exemple #2
0
    def sequential_train_steps(sess, train_ops, global_step,
                               train_step_kwargs):
        """A thin wrapper around slim.learning.train_step, for GANs.

    Args:
      sess: A Tensorflow session.
      train_ops: A GANTrainOps tuple of train ops to run.
      global_step: The global step.
      train_step_kwargs: Dictionary controlling `train_step` behavior.

    Returns:
      A scalar final loss and a bool whether or not the train loop should stop.
    """
        # Only run `should_stop` at the end, if required. Make a local copy of
        # `train_step_kwargs`, if necessary, so as not to modify the caller's
        # dictionary.
        should_stop_op, train_kwargs = None, train_step_kwargs
        if 'should_stop' in train_step_kwargs:
            should_stop_op = train_step_kwargs['should_stop']
            train_kwargs = train_step_kwargs.copy()
            del train_kwargs['should_stop']

        # Run generator training steps.
        gen_loss = 0
        for _ in range(train_steps.generator_train_steps):
            cur_gen_loss, _ = slim_learning.train_step(
                sess, train_ops.generator_train_op, global_step, train_kwargs)
            gen_loss += cur_gen_loss

        # Run discriminator training steps.
        dis_loss = 0
        for _ in range(train_steps.discriminator_train_steps):
            cur_dis_loss, _ = slim_learning.train_step(
                sess, train_ops.discriminator_train_op, global_step,
                train_kwargs)
            dis_loss += cur_dis_loss

        sess.run(train_ops.global_step_inc_op)

        # Run the `should_stop` op after the global step has been incremented, so
        # that the `should_stop` aligns with the proper `global_step` count.
        if should_stop_op is not None:
            should_stop = sess.run(should_stop_op)
        else:
            should_stop = False

        return gen_loss + dis_loss, should_stop
Exemple #3
0
  def sequential_train_steps(sess, train_ops, global_step, train_step_kwargs):
    """A thin wrapper around slim.learning.train_step, for GANs.

    Args:
      sess: A Tensorflow session.
      train_ops: A GANTrainOps tuple of train ops to run.
      global_step: The global step.
      train_step_kwargs: Dictionary controlling `train_step` behavior.

    Returns:
      A scalar final loss and a bool whether or not the train loop should stop.
    """
    # Only run `should_stop` at the end, if required. Make a local copy of
    # `train_step_kwargs`, if necessary, so as not to modify the caller's
    # dictionary.
    should_stop_op, train_kwargs = None, train_step_kwargs
    if 'should_stop' in train_step_kwargs:
      should_stop_op = train_step_kwargs['should_stop']
      train_kwargs = train_step_kwargs.copy()
      del train_kwargs['should_stop']

    # Run generator training steps.
    gen_loss = 0
    for _ in range(train_steps.generator_train_steps):
      cur_gen_loss, _ = slim_learning.train_step(
          sess, train_ops.generator_train_op, global_step, train_kwargs)
      gen_loss += cur_gen_loss

    # Run discriminator training steps.
    dis_loss = 0
    for _ in range(train_steps.discriminator_train_steps):
      cur_dis_loss, _ = slim_learning.train_step(
          sess, train_ops.discriminator_train_op, global_step, train_kwargs)
      dis_loss += cur_dis_loss

    sess.run(train_ops.global_step_inc_op)

    # Run the `should_stop` op after the global step has been incremented, so
    # that the `should_stop` aligns with the proper `global_step` count.
    if should_stop_op is not None:
      should_stop = sess.run(should_stop_op)
    else:
      should_stop = False

    return gen_loss + dis_loss, should_stop
Exemple #4
0
    def train_step_fn(session, *args, **kwargs):
        total_loss, should_stop = train_step(session, *args, **kwargs)

        if session.run(train_step_fn.step) % num_per_valid == 0:
            accuracy = session.run(train_step_fn.accuracy_validation)
            step = session.run(train_step_fn.step)
            print('step', step, 'valid accuracy:', accuracy)

        return [total_loss, should_stop]
Exemple #5
0
def train_step_fn(session, *args, **kwargs):
    total_loss, should_stop = train_step(session, *args, **kwargs)

    if train_step_fn.step % train_step_fn.test_step == 0:
        session.run(train_step_fn.accuracy_test)

    train_step_fn.step += 1

    return [total_loss, should_stop]
Exemple #6
0
            def train_step_fn(session, *args, **kwargs):

                total_loss, should_stop = train_step(session, *args, **kwargs)

                if train_step_fn.step % FLAGS.validation_check == 0:
                    #accuracy = session.run(train_step_fn.accuracy_validation)
                    print('Step %s - Loss: %.2f ' %
                          (str(train_step_fn.step).rjust(6, '0'), total_loss))

                train_step_fn.step += 1

                return [total_loss, should_stop]
Exemple #7
0
        def train_step_fn(session, *args, **kwargs):
            # Decaying learning rate every epoch
            if train_step_fn.step % (nb_batches) == 0:
                lr_decay = decay_factor ** train_step_fn.epoch
                session.run(model.lr_rate_assign, feed_dict={model.lr_rate_placeholder: initial_lr * lr_decay})
                print('New learning rate: {0}'. format(initial_lr * lr_decay))
                train_step_fn.epoch += 1

            total_loss, should_stop = train_step(session, *args, **kwargs)
            
            train_step_fn.step += 1
            return [total_loss, should_stop]
        def train_step_fn(sess, *args, **kwargs):
            total_loss, should_stop = train_step(sess, *args, **kwargs)
            accuracy = sess.run([train_step_fn.accuracy])
            if train_step_fn.step % 50 == 0:
                # sess.run(assignment)
                accuracy_validation = sess.run(
                    [train_step_fn.accuracy_validation])
                # print('Step %s - Loss: %.2f Validation Accuracy: %.2f%%' %
                #       (str(train_step_fn.step).rjust(6, '0'), total_loss, accuracy * 100))

            train_step_fn.step += 1
            return [total_loss, should_stop]
Exemple #9
0
    def train_step_fn(session, *args, **kwargs):
        total_loss, should_stop = train_step(session, *args, **kwargs)

        if session.run(train_step_fn.step) % num_per_valid == 0:
            accuracy = session.run(train_step_fn.accuracy_validation)
            step = session.run(train_step_fn.step)
            with open("./test/test_" + str(step) + ".csv", "w") as datacsv:
                csvwriter = csv.writer(datacsv, delattr)
                for i in range(len(accuracy)):
                    csvwriter.writerow(accuracy[i])

        return [total_loss, should_stop]
Exemple #10
0
        def train_step_fn(sess, my_train_op, global_step, train_step_kwargs):
            total_loss, should_stop = train_step(sess, my_train_op,
                                                 global_step,
                                                 train_step_kwargs)
            step = sess.run(global_step)

            if step % FLAGS.log_every_n_steps == 0:
                accuracy = sess.run(accuracy_validation)
                print(
                    'Step %s - Loss: %.4f hold-out validation accuracy: %.4f' %
                    (str(step).rjust(6, '0'), total_loss, accuracy))

            return [total_loss, should_stop]
Exemple #11
0
        def train_step_fn(session, *args, **kwargs):
            # visualizer = Beholder(session=session, logdir=FLAGS.train_dir)
            total_loss, should_stop = train_step(session, *args, **kwargs)

            if train_step_fn.step % FLAGS.validation_check == 0:
                _mean_iou = session.run(train_step_fn.mean_iou)
                print('evaluation step %d - loss = %.4f mean_iou = %.2f%%' %\
                 (train_step_fn.step, total_loss, _mean_iou ))
            # evaluated_tensors = session.run([end_points['conv4'], end_points['up1']])
            # example_frame = session.run(end_points['up2'])
            # visualizer.update(arrays=evaluated_tensors, frame=example_frame)

            train_step_fn.step += 1
            return [total_loss, should_stop]
Exemple #12
0
def train_step_fn(session, *args, **kwargs):
    total_loss, should_stop = train_step(session, *args, **kwargs)

    if train_step_fn.step % validation_every_n_step == 0:
        accuracy = sess.run(train_step_fn.accuracy_validation)
        print("Validation Accuracy:", accuracy)
        if accuracy >= 0.993:
            print("准确率达到了99.3%,退出训练")

    if train_step_fn.step % test_every_n_step == 0:
        accuracy = session.run(train_step_fn.accuracy_test)
        print("Test Accuracy:", accuracy)

    train_step_fn.step += 1
    return [total_loss, should_stop]
Exemple #13
0
    def train_step_fn(session, *args, **kwargs):
        total_loss, should_stop = train_step(session, *args, **kwargs)

        if train_step_fn.step % FLAGS.validation_check == 0:
            pass
            # throws OutOfRange error after some time
         #   accuracy = session.run(train_step_fn.accuracy_validation)
          #  print('Step %s - Loss: %.2f Accuracy: %.2f%%' % (
          #  str(train_step_fn.step).rjust(6, '0'), total_loss, accuracy * 100))

     #   if train_step_fn.step == (FLAGS.max_steps - 1):
     #       accuracy = session.run(accuracy_test)
     #       print('%s - Loss: %.2f Accuracy: %.2f%%' % ('FINAL TEST', total_loss, accuracy * 100))

        train_step_fn.step += 1
        return [total_loss, should_stop]
    def train_step_fn(session, *args, **kwargs):
      def softmax(x):
        """Compute softmax values for each sets of scores in x."""
        x=np.transpose(x);
        return np.transpose(np.exp(x) / np.sum(np.exp(x), axis=0))
      total_loss, should_stop = train_step(session, *args, **kwargs)

      if train_step_fn.step % FLAGS.eval_every_n_steps == 0:
        start = time.time()
        tp = 0;
        fp = 0;
        tn = 0;
        fn = 0;
        y_true = [];
        y_scores = [];
        for eval_iter in range(num_batches_val):
#          pred, lab, logis = session.run([train_step_fn.predictions_val, train_step_fn.labels_val, train_step_fn.logits_val])
          pred, lab, logis, scs = session.run([train_step_fn.predictions_val, train_step_fn.labels_val, train_step_fn.logits_val, train_step_fn.scs_val])
#          scs = softmax(logis);
          scs = scs[:, 1];
          
          y_true = np.append(y_true,lab);
          y_scores = np.append(y_scores, scs);
          pos = np.equal(lab, 1);
          neg = np.invert(pos);
          pred_pos = np.equal(pred, 1);
          pred_neg = np.invert(pred_pos);
          tp += np.sum(np.multiply(pos, pred_pos));
          tn += np.sum(np.multiply(neg, pred_neg));
          fp += np.sum(np.multiply(neg, pred_pos));
          fn += np.sum(np.multiply(pos, pred_neg));
        auc = roc_auc_score(y_true, y_scores);
          
        accuracy = (tp+tn)/(tp+tn+fp+fn);
        logging.info('Step %s - Loss: %.2f Accuracy: %.2f%%, TP: %d, FP: %d, TN: %d, FN: %d, AUC: %.2f%%', str(train_step_fn.step).rjust(6, '0'), total_loss, accuracy * 100, tp, fp, tn, fn, auc*100)	
	
	of.write('Step %s - Loss: %.2f Accuracy: %.2f%%, TP: %d, FP: %d, TN: %d, FN: %d, AUC: %.2f%% \n'% (str(train_step_fn.step).rjust(6, '0'), total_loss, accuracy * 100, tp, fp, tn, fn, auc*100))


        end = time.time()
        logging.info('Took %f seconds for evaluation', end-start);
      train_step_fn.step += 1
      return [total_loss, should_stop]
        def train_step_fn(sess, train_tensor, global_step, train_step_kwargs):

            total_loss, should_stop = train_step(sess, train_tensor,
                                                 global_step,
                                                 train_step_kwargs)

            if train_step_fn.step % FLAGS.log_every_n_steps == 0:
                accuracy = sess.run(train_step_fn.accuracy)
                print('Accuracy: %.2f%%' % (accuracy * 100))
                csv_writerow(test_csv_file, [FLAGS.loss] +
                             [train_step_fn.step] + [accuracy * 100])

                #print('Accuracy: %.2f%%' % accuracy)
                #print('Accuracy: % g' % accuracy)

        # if train_step_fn.step == (FLAGS.max_steps - 1):
        #     accuracy = sess.run(accuracy_test)
        #     print('%s - Loss: %.2f Accuracy: %.2f%%' % ('FINAL TEST', total_loss, accuracy * 100))

            train_step_fn.step += 1
            return [total_loss, should_stop]
def train_step_fn(sess, train_op, global_step, train_step_kwargs):
  """Function that takes a gradient step and specifies whether to stop.
      (Wrapper function for train_step in learning.py)
  Args:
    sess: The current session.
    train_op: An `Operation` that evaluates the gradients and returns the
      total loss.
    global_step: A `Tensor` representing the global training step.
    train_step_kwargs: A dictionary of keyword arguments (using this here to 
    pass in my eval_op for validation set evaluation, so we can do evaluation
    during training).

  Returns:
    The total loss and a boolean indicating whether or not to stop training.

  """

  # first, run the normal training step (happens every single step)
  total_loss, should_stop = learning.train_step(sess, train_op, global_step, train_step_kwargs)

  # get global step as a value i can log 
  global_step = sess.run(global_step)
 
  # decide whether to reset the streaming evaluation metrics
  should_reset = sess.run(train_step_kwargs['should_reset_eval_metrics'])
  if should_reset:
      # reset counter and total
      logging.info('RESETTING STREAMING EVAL METRICS AT STEP %d\n'% (global_step))
      sess.run([train_step_kwargs['reset_op']])
  
  # decide whether to run evaluation
  should_val = sess.run(train_step_kwargs['should_val'])    
  if should_val:   
      # validate
      logging.info('EVALUATING MODEL AT STEP %d\n'% (global_step))
      sess.run([train_step_kwargs['eval_op']] )

  return [total_loss, should_stop]