コード例 #1
0
def evaluate():
    """Run Eval once.
  """
    # get a list of image filenames
    filenames = glb('/data/fluid_flow_steady_state_128x128_test/*')
    filenames.sort(key=alphanum_key)
    filename_len = len(filenames)
    shape = [128, 256]

    with tf.Graph().as_default():
        # Make image placeholder
        boundary_op = tf.placeholder(tf.float32, [1, shape[0], shape[1], 1])

        # Build a Graph that computes the logits predictions from the
        # inference model.
        sflow_p = flow_net.inference(boundary_op, 1.0)

        # Restore the moving average version of the learned variables for eval.
        variables_to_restore = tf.all_variables()
        saver = tf.train.Saver(variables_to_restore)

        sess = tf.Session()

        ckpt = tf.train.get_checkpoint_state(TEST_DIR)

        saver.restore(sess, ckpt.model_checkpoint_path)
        global_step = 1

        graph_def = tf.get_default_graph().as_graph_def(add_shapes=True)

        for run in filenames:
            # read in boundary
            flow_name = run + '/fluid_flow_0002.h5'
            boundary_np = load_boundary(flow_name, shape).reshape(
                [1, shape[0], shape[1], 1])
            sflow_true = load_flow(flow_name, shape)

            # calc logits
            sflow_generated = sess.run(sflow_p,
                                       feed_dict={boundary_op: boundary_np})[0]

            if FLAGS.display_test:
                # convert to display
                sflow_plot = np.concatenate([
                    sflow_true, sflow_generated, sflow_true - sflow_generated
                ],
                                            axis=1)
                boundary_concat = np.concatenate(3 * [boundary_np], axis=2)
                sflow_plot = np.sqrt(
                    np.square(sflow_plot[:, :, 0]) +
                    np.square(sflow_plot[:, :, 1])
                ) - .05 * boundary_concat[0, :, :, 0]

                # display it
                plt.imshow(sflow_plot)
                plt.colorbar()
                plt.show()

        print("the percent error on " + FLAGS.test_set + " is")
        print(p_error)
コード例 #2
0
def evaluate():
    """Run Eval once.

  Args:
    saver: Saver.
    summary_writer: Summary writer.
    top_k_op: Top K op.
    summary_op: Summary op.
  """
    shape = [128, 256]

    with tf.Graph().as_default():
        # Make image placeholder
        boundary_op = tf.placeholder(tf.float32, [8, shape[0], shape[1], 1])

        # Build a Graph that computes the logits predictions from the
        # inference model.
        sflow_p = flow_net.inference(boundary_op, 1.0)

        # Restore the moving average version of the learned variables for eval.
        variables_to_restore = tf.all_variables()
        saver = tf.train.Saver(variables_to_restore)

        sess = tf.Session()

        ckpt = tf.train.get_checkpoint_state(TEST_DIR)

        saver.restore(sess, ckpt.model_checkpoint_path)
        global_step = 1

        graph_def = tf.get_default_graph().as_graph_def(add_shapes=True)

        num_runs = 1000
        boundary_np = np.zeros([8, shape[0], shape[1], 1])
        _ = sess.run(sflow_p, feed_dict={boundary_op: boundary_np})[0]
        t = time.time()
        for i in xrange(num_runs):
            # make boundary
            # calc logits
            _ = sess.run(sflow_p, feed_dict={boundary_op: boundary_np})[0]

        elapsed = time.time() - t
        print("time per inpu is ")
        print(elapsed / (num_runs * 8.))
コード例 #3
0
def train():
  """Train ring_net for a number of steps."""
  with tf.Graph().as_default():
    # make inputs
    boundary, sflow = flow_net.inputs(FLAGS.batch_size) 
    # create and unrap network
    sflow_p = flow_net.inference(boundary, FLAGS.keep_prob) 
    # calc error
    error = flow_net.loss_image(sflow_p, sflow) 
    # train hopefuly 
    train_op = flow_net.train(error, FLAGS.learning_rate)
    # List of all Variables
    variables = tf.global_variables()

    # Build a saver
    saver = tf.train.Saver(tf.global_variables())   
    #for i, variable in enumerate(variables):
    #  print '----------------------------------------------'
    #  print variable.name[:variable.name.index(':')]

    # Summary op
    summary_op = tf.summary.merge_all()
 
    # Build an initialization operation to run below.
    init = tf.global_variables_initializer()

    # Start running operations on the Graph.
    sess = tf.Session()

    # init if this is the very time training
    sess.run(init)
 
    # init from checkpoint
    saver_restore = tf.train.Saver(variables)
    ckpt = tf.train.get_checkpoint_state(TRAIN_DIR)
    if ckpt is not None:
      print("init from " + TRAIN_DIR)
      try:
         saver_restore.restore(sess, ckpt.model_checkpoint_path)
      except:
         tf.gfile.DeleteRecursively(TRAIN_DIR)
         tf.gfile.MakeDirs(TRAIN_DIR)
         print("there was a problem using variables in checkpoint, random init will be used instead")

    # Start que runner
    tf.train.start_queue_runners(sess=sess)

    # Summary op
    graph_def = sess.graph.as_graph_def(add_shapes=True)
    summary_writer = tf.summary.FileWriter(TRAIN_DIR, graph_def=graph_def)

    for step in range(FLAGS.max_steps):
      t = time.time()
      _ , loss_value = sess.run([train_op, error],feed_dict={})
      elapsed = time.time() - t

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

      if step%100 == 0:
        summary_str = sess.run(summary_op, feed_dict={})
        summary_writer.add_summary(summary_str, step) 
        print("loss value at " + str(loss_value))
        print("time per batch is " + str(elapsed))

      if step%1000 == 0:
        checkpoint_path = os.path.join(TRAIN_DIR, 'model.ckpt')
        saver.save(sess, checkpoint_path, global_step=step)  
        print("saved to " + TRAIN_DIR)