def evaluate():
    """Run Eval once.
  """
    with tf.Session() as sess:
        # Make image placeholder
        input_dims = FLAGS.nr_boundary_params
        input_vector, true_boundary = flow_net.inputs_boundary(
            input_dims, batch_size=FLAGS.batch_size, shape=shape)

        # Build a Graph that computes the logits predictions from the
        predicted_boundary = flow_net.inference_boundary(FLAGS.batch_size,
                                                         FLAGS.dims *
                                                         [FLAGS.obj_size],
                                                         input_vector,
                                                         full_shape=shape)

        # Restore for eval
        init = tf.global_variables_initializer()
        sess.run(init)

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

        # make one input and run on it again and again
        #input_batch, boundary_batch = flow_net.feed_dict_boundary(input_dims, FLAGS.batch_size, shape)

        sess.run(
            predicted_boundary,
            feed_dict={input_vector: np.zeros((FLAGS.batch_size, input_dims))})
        t = time.time()
        for i in tqdm(xrange(run_steps)):
            sess.run(predicted_boundary,
                     feed_dict={
                         input_vector: np.zeros((FLAGS.batch_size, input_dims))
                     })
        elapsed = time.time() - t

        filename = "./figs/boundary_network_shape_" + FLAGS.shape + "_batch_size_" + str(
            FLAGS.batch_size) + ".txt"
        with open(filename, "w") as f:
            f.write(str(elapsed / float(FLAGS.batch_size * run_steps)))
Example #2
0
def evaluate():
  """Run Eval once.
  """
  with tf.Session() as sess:
    # Make image placeholder
    param_inputs, _ = flow_net.inputs_boundary(FLAGS.nr_boundary_params, 1, shape)

    # Make boundary
    boundary = flow_net.inference_boundary(1, FLAGS.dims*[FLAGS.obj_size], param_inputs, full_shape=shape)
    boundary = tf.round(boundary)

    # inference model.
    predicted_flow = flow_net.inference_network(boundary)

    # init graph
    init = tf.global_variables_initializer()
    sess.run(init)

    # Restore variables
    variables_to_restore = tf.all_variables()
    variables_to_restore_boundary = [variable for i, variable in enumerate(variables_to_restore) if "boundary_network" in variable.name[:variable.name.index(':')]]
    variables_to_restore_flow = [variable for i, variable in enumerate(variables_to_restore) if "flow_network" in variable.name[:variable.name.index(':')]]
    saver_boundary = tf.train.Saver(variables_to_restore_boundary)
    saver_flow = tf.train.Saver(variables_to_restore_flow)
    ckpt_boundary = tf.train.get_checkpoint_state(BOUNDARY_DIR)
    ckpt_flow = tf.train.get_checkpoint_state(FLOW_DIR)
    saver_boundary.restore(sess, ckpt_boundary.model_checkpoint_path)
    saver_flow.restore(sess, ckpt_flow.model_checkpoint_path)
    global_step = 1
    
    # make graph def 
    graph_def = tf.get_default_graph().as_graph_def(add_shapes=True)


    for i in xrange(10):
      # random params
      rand_param = np.expand_dims(get_random_params(FLAGS.nr_boundary_params, 3), axis=0)
      rand_param[:,0] = 0
      rand_param[:,1] = 0
      rand_param[:,2] = 0.5
      rand_param[:,3] = 1.0

      rand_param = np.load("figs/3d_wing_params_op.npy")
      rand_param = np.expand_dims(rand_param, axis=0)

      # calc flow 
      p_flow, p_boundary = sess.run([predicted_flow, boundary],feed_dict={param_inputs: rand_param})
      p_flow = p_flow * ((-p_boundary) + 1.0)
      dim=3

      # plt boundary
      #plt.imshow(p_boundary[0,:,:,72,0])
      #plt.show()

      # save vtk file of it
      image_vtk = tvtk.ImageData(spacing=(1, 1, 1), origin=(0, 0, 0))
      image_vtk.point_data.vectors = p_flow[0,:,:,:,0:3].reshape([shape[0]*shape[1]*shape[2], 3])
      image_vtk.point_data.scalars = p_flow[0,:,:,:,3].reshape([shape[0]*shape[1]*shape[2]])
      image_vtk.point_data.scalars.name = "pressure"
      image_vtk.dimensions = shape
      write_data(image_vtk, "figs/vtk_flow_wing_3d")

      image_vtk = tvtk.ImageData(spacing=(1, 1, 1), origin=(0, 0, 0))
      image_vtk.point_data.scalars = p_boundary[0,:,:,:,0].reshape([shape[0]*shape[1]*shape[2]])
      image_vtk.point_data.scalars.name = "boundary"
      image_vtk.dimensions = shape
      write_data(image_vtk, "figs/vtk_boundary_wing_3d")
def evaluate():
    """Run Eval once.

  Args:
    saver: Saver.
    summary_writer: Summary writer.
    top_k_op: Top K op.
    summary_op: Summary op.
  """

    with tf.Graph().as_default():
        # Make image placeholder
        params_op, params_op_init, params_op_set, squeeze_loss = flow_net.inputs_boundary_learn(
            batch_size, network_type="heat", noise_std=0.01)

        # Make boundary
        boundary = flow_net.inference_boundary(batch_size, 2 * [128],
                                               params_op)

        # predict steady flow on boundary
        predicted_heat = flow_net.inference_network(boundary,
                                                    network_type="heat",
                                                    keep_prob=1.0)

        # loss
        #loss = tf.reduce_sum(predicted_heat)
        loss = tf.reduce_max(predicted_heat)
        loss += squeeze_loss

        # train_op
        variables_to_train = tf.all_variables()
        variables_to_train = [
            variable for i, variable in enumerate(variables_to_train)
            if "params" in variable.name[:variable.name.index(':')]
        ]
        train_step = flow_net.train(loss,
                                    FLAGS.boundary_learn_lr,
                                    train_type="boundary_params",
                                    variables=variables_to_train)

        # init graph
        init = tf.global_variables_initializer()

        # Restore the moving average version of the learned variables for eval.
        variables_to_restore = tf.all_variables()
        variables_to_restore_boundary = [
            variable for i, variable in enumerate(variables_to_restore)
            if "boundary_network" in variable.name[:variable.name.index(':')]
        ]
        variables_to_restore_heat = [
            variable for i, variable in enumerate(variables_to_restore)
            if "heat_network" in variable.name[:variable.name.index(':')]
        ]
        saver_boundary = tf.train.Saver(variables_to_restore_boundary)
        saver_heat = tf.train.Saver(variables_to_restore_heat)

        # start ses and init
        sess = tf.Session()
        sess.run(init)
        ckpt_boundary = tf.train.get_checkpoint_state(BOUNDARY_DIR)
        ckpt_heat = tf.train.get_checkpoint_state(FLOW_DIR)
        saver_boundary.restore(sess, ckpt_boundary.model_checkpoint_path)
        saver_heat.restore(sess, ckpt_heat.model_checkpoint_path)

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

        params_np = (np.random.rand(1, FLAGS.nr_boundary_params) - .5) / 2.0

        sess.run(params_op_init, feed_dict={params_op_set: params_np})
        run_time = FLAGS.boundary_learn_steps

        # make store vectors for values
        plot_error = []

        # make store dir
        os.system("mkdir ./figs/boundary_learn_image_store")
        for i in tqdm(xrange(run_time)):
            l, _, = sess.run([loss, train_step], feed_dict={})
            print(l)
            plot_error.append(np.sum(l))
            if ((i + 1) % 1 == 0) or i == run_time - 1:
                # make video with opencv
                p_heat, p_boundary = sess.run([predicted_heat, boundary])

                # save plot image to make video
                fig = plt.figure()
                fig.set_size_inches(15, 5)
                a = fig.add_subplot(1, 3, 2)
                plt.imshow(p_heat[0, :, :, 0])
                #plt.title("Heat Dissipation", fontsize="x-large")
                plt.title("Heat Dissipation", fontsize=16)
                a = fig.add_subplot(1, 3, 3)
                plt.imshow(p_boundary[0, :, :, 0])
                plt.title("Heat Sink Geometry", fontsize=16)
                a = fig.add_subplot(1, 3, 1)
                plt.plot(np.array(plot_error), label="Temp at Source")
                plt.xlim(-10, 510)
                plt.xlabel("Step")
                plt.ylabel("Temp")
                plt.legend()
                plt.suptitle("Heat Sink Optimization Using Gradient Descent",
                             fontsize=20)
                plt.savefig("./figs/boundary_learn_image_store/plot_" +
                            str(i).zfill(5) + ".png")
                if run_time - i <= 100:
                    plt.savefig("./figs/" + FLAGS.boundary_learn_loss +
                                "_plot.png")
                if i == run_time - 1:
                    plt.savefig("./figs/heat_learn_gradient_decent.pdf")
                    plt.show()
                plt.close(fig)

        # generate video of plots
        os.system("rm ./figs/heat_learn_video.mp4")
        os.system(
            "cat ./figs/boundary_learn_image_store/*.png | ffmpeg -f image2pipe -r 30 -vcodec png -i - -vcodec libx264 ./figs/heat_learn_video.mp4"
        )
        os.system("rm -r ./figs/boundary_learn_image_store")
Example #4
0
def evaluate():
    """Run Eval once.

  Args:
    saver: Saver.
    summary_writer: Summary writer.
    top_k_op: Top K op.
    summary_op: Summary op.
  """
    with tf.Graph().as_default():
        # Make image placeholder
        inputs_vector, true_boundary = flow_net.inputs_boundary(
            FLAGS.nr_boundary_params, batch_size, shape)

        # Build a Graph that computes the logits predictions from the
        # inference model.
        #inputs_vector_noise = inputs_vector + tf.random_normal(shape=tf.shape(inputs_vector), mean=0.0, stddev=0.0001, dtype=tf.float32)
        boundary = flow_net.inference_boundary(1,
                                               FLAGS.dims * [FLAGS.obj_size],
                                               inputs=inputs_vector,
                                               full_shape=shape)
        #boundary = tf.round(boundary)
        predicted_flow = flow_net.inference_network(boundary, keep_prob=1.0)

        # quantities to optimize
        force = calc_force(boundary, predicted_flow[:, :, :, 2:3])
        drag_x = tf.reduce_sum(force[:, :, :, 0])
        drag_y = tf.reduce_sum(force[:, :, :, 1])
        drag_ratio = (drag_y / drag_x)

        # init graph
        init = tf.global_variables_initializer()

        # Restore the moving average version of the learned variables for eval.
        variables_to_restore = tf.all_variables()
        variables_to_restore_boundary = [
            variable for i, variable in enumerate(variables_to_restore)
            if "boundary_network" in variable.name[:variable.name.index(':')]
        ]
        variables_to_restore_flow = [
            variable for i, variable in enumerate(variables_to_restore)
            if "flow_network" in variable.name[:variable.name.index(':')]
        ]
        saver_boundary = tf.train.Saver(variables_to_restore_boundary)
        saver_flow = tf.train.Saver(variables_to_restore_flow)

        # start ses and init
        sess = tf.Session()
        sess.run(init)
        ckpt_boundary = tf.train.get_checkpoint_state(BOUNDARY_DIR)
        ckpt_flow = tf.train.get_checkpoint_state(FLOW_DIR)
        saver_boundary.restore(sess, ckpt_boundary.model_checkpoint_path)
        saver_flow.restore(sess, ckpt_flow.model_checkpoint_path)

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

        params_np = get_random_params(FLAGS.nr_boundary_params, 2)
        params_np = np.expand_dims(params_np, axis=0)
        params_np[0, 0] = 0.0
        params_np[0, 1] = 0.5
        params_np[0, 2] = 1.0
        params_np[0, 4] = 0.0

        # make store vectors for values
        resolution = 320
        loss_val = np.zeros((resolution))
        max_d_ratio = np.zeros((resolution))
        d_ratio_store = None
        boundary_frame_store = []
        store_freq = int(resolution / nr_frame_saves)

        # make store dir
        for i in tqdm(xrange(resolution)):
            params_np[0, 4] += (0.3) / resolution
            velocity_norm_g = sess.run(drag_ratio,
                                       feed_dict={
                                           inputs_vector:
                                           np.concatenate(batch_size *
                                                          [params_np],
                                                          axis=0)
                                       })
            if i % store_freq == 0:
                boundary_frame_store.append(
                    sess.run(
                        boundary,
                        feed_dict={
                            inputs_vector:
                            np.concatenate(batch_size * [params_np], axis=0)
                        })[0,
                           int(FLAGS.obj_size / 2):int(3 * FLAGS.obj_size / 2),
                           int(FLAGS.obj_size / 2):int(3 * FLAGS.obj_size / 2),
                           0])
            loss_val[i] = velocity_norm_g

        fig = plt.figure(figsize=(10, 5))
        a = fig.add_subplot(1, 2, 1)
        plt.title("Boundary from Parameter Change", fontsize=16)
        boundary_frame_store = tile_frames(boundary_frame_store)
        plt.imshow(boundary_frame_store)
        #plt.tick_params(axis='both', top="off", bottom="off")
        plt.axis('off')
        a = fig.add_subplot(1, 2, 2)
        #plt.imshow(np.concatenate(boundary_frame_store, axis = 0))
        plt.plot(np.arange(resolution) / float(resolution) - .5, loss_val)
        plt.ylabel("Lift/Drag")
        plt.xlabel("Parameter Value")
        plt.title("Loss vs Parameter Value", fontsize=16)
        plt.savefig("./figs/boundary_space_explore.pdf")
        plt.show()
def evaluate():
    """Run Eval once.

  Args:
    saver: Saver.
    summary_writer: Summary writer.
    top_k_op: Top K op.
    summary_op: Summary op.
  """

    num_angles = 9
    max_angle = 0.10
    min_angle = -0.30
    set_params = np.array(num_angles * [FLAGS.nr_boundary_params * [0.0]])
    set_params[:, :] = 0.0
    set_params_pos = np.array(num_angles * [FLAGS.nr_boundary_params * [0.0]])
    set_params_pos[:, :] = 1.0

    for i in xrange(num_angles):
        set_params[i, 0] = -i
    set_params[:, 0] = ((max_angle - min_angle) *
                        (set_params[:, 0] / (num_angles - 1))) - min_angle
    print(set_params[:, 0])

    set_params[:, 2] = 0.5
    set_params[:, 3] = 1.0
    set_params[:, -1] = 0.0

    set_params_pos[:, 0] = 0.0  # set angle to 0.0
    set_params_pos[:, 1] = 0.0  # set angle to 0.0
    set_params_pos[:, 2] = 0.0  # set n_1 to .5
    set_params_pos[:, 3] = 0.0  # set n_2 to 1.0
    set_params_pos[:, -1] = 0.0  # set tail hieght to 0.0

    with tf.Graph().as_default():
        # Make image placeholder
        params_op, params_op_init, params_op_set, squeeze_loss = flow_net.inputs_boundary_learn(
            batch_size,
            set_params=set_params,
            set_params_pos=set_params_pos,
            noise_std=0.01)

        # Make placeholder for flow computed by lattice boltzmann solver
        solver_boundary, solver_flow = flow_net.inputs_flow(
            1, shape, FLAGS.dims)
        sharp_boundary, blaa = flow_net.inputs_flow(
            batch_size * set_params.shape[0], shape, FLAGS.dims)

        # Make boundary
        params_op_0, params_op_1, params_op_2 = tf.split(params_op, 3, axis=0)
        with tf.device('/gpu:0'):
            boundary_0 = flow_net.inference_boundary(
                batch_size * int(set_params.shape[0] / 3),
                FLAGS.dims * [FLAGS.obj_size],
                params_op_0,
                full_shape=shape)
        with tf.device('/gpu:1'):
            boundary_1 = flow_net.inference_boundary(
                batch_size * int(set_params.shape[0] / 3),
                FLAGS.dims * [FLAGS.obj_size],
                params_op_1,
                full_shape=shape)
        with tf.device('/gpu:2'):
            boundary_2 = flow_net.inference_boundary(
                batch_size * int(set_params.shape[0] / 3),
                FLAGS.dims * [FLAGS.obj_size],
                params_op_2,
                full_shape=shape)

        # predict steady flow on boundary
        grads = []
        loss_gen = []
        with tf.device('/gpu:0'):
            predicted_flow_0 = flow_net.inference_network(
                boundary_0, network_type="flow", keep_prob=FLAGS.keep_prob)
            force_0 = calc_force(boundary_0, predicted_flow_0[..., -1:])
            drag_x_0 = tf.reduce_sum(force_0[..., 0], axis=[1, 2, 3
                                                            ]) / batch_size
            drag_y_0 = tf.reduce_sum(force_0[..., 1], axis=[1, 2, 3
                                                            ]) / batch_size
            drag_z_0 = tf.reduce_sum(force_0[..., 2], axis=[1, 2, 3
                                                            ]) / batch_size
            drag_lift_ratio_0 = -(drag_x_0 / drag_z_0)
            loss_0 = -tf.reduce_sum(drag_lift_ratio_0)
            loss_0 += squeeze_loss
            variables_to_train = tf.all_variables()
            variables_to_train = [
                variable for i, variable in enumerate(variables_to_train)
                if "params" in variable.name[:variable.name.index(':')]
            ]
            loss_gen.append(loss_0)
            # store grads
            grads.append(tf.gradients(loss_gen[0], variables_to_train))
        with tf.device('/gpu:1'):
            predicted_flow_1 = flow_net.inference_network(
                boundary_1, network_type="flow", keep_prob=FLAGS.keep_prob)
            force_1 = calc_force(boundary_1, predicted_flow_1[..., -1:])
            drag_x_1 = tf.reduce_sum(force_1[..., 0], axis=[1, 2, 3
                                                            ]) / batch_size
            drag_y_1 = tf.reduce_sum(force_1[..., 1], axis=[1, 2, 3
                                                            ]) / batch_size
            drag_z_1 = tf.reduce_sum(force_1[..., 2], axis=[1, 2, 3
                                                            ]) / batch_size
            drag_lift_ratio_1 = -(drag_x_1 / drag_z_1)
            loss_1 = -tf.reduce_sum(drag_lift_ratio_1)
            loss_1 += squeeze_loss
            variables_to_train = tf.all_variables()
            variables_to_train = [
                variable for i, variable in enumerate(variables_to_train)
                if "params" in variable.name[:variable.name.index(':')]
            ]
            loss_gen.append(loss_1)
            # store grads
            grads.append(tf.gradients(loss_gen[1], variables_to_train))
        with tf.device('/gpu:2'):
            predicted_flow_2 = flow_net.inference_network(
                boundary_2, network_type="flow", keep_prob=FLAGS.keep_prob)
            force_2 = calc_force(boundary_2, predicted_flow_2[..., -1:])
            drag_x_2 = tf.reduce_sum(force_2[..., 0], axis=[1, 2, 3
                                                            ]) / batch_size
            drag_y_2 = tf.reduce_sum(force_2[..., 1], axis=[1, 2, 3
                                                            ]) / batch_size
            drag_z_2 = tf.reduce_sum(force_2[..., 2], axis=[1, 2, 3
                                                            ]) / batch_size
            drag_lift_ratio_2 = -(drag_x_2 / drag_z_2)
            loss_2 = -tf.reduce_sum(drag_lift_ratio_2)
            loss_2 += squeeze_loss
            variables_to_train = tf.all_variables()
            variables_to_train = [
                variable for i, variable in enumerate(variables_to_train)
                if "params" in variable.name[:variable.name.index(':')]
            ]
            loss_gen.append(loss_2)
            # store grads
            grads.append(tf.gradients(loss_gen[2], variables_to_train))

        # store up the loss and gradients on gpu:0
        with tf.device('/gpu:0'):
            for i in range(1, 3):
                loss_gen[0] += loss_gen[i]
                for j in range(len(grads[0])):
                    grads[0][j] += grads[i][j]

            train_step = tf.group(
                adam_updates(variables_to_train,
                             grads[0],
                             lr=FLAGS.boundary_learn_lr,
                             mom1=0.95,
                             mom2=0.9995))

        #with tf.device('/cpu:0'):
        #  sharp_predicted_flow = flow_net.inference_network(sharp_boundary, network_type="flow", keep_prob=FLAGS.keep_prob)

        # quantities to optimize
        #force = calc_force(boundary, predicted_flow[...,-1:])
        #sharp_force = calc_force(sharp_boundary, sharp_predicted_flow[...,-1:])
        #solver_force = calc_force(solver_boundary, solver_flow[...,-1:])
        #drag_x = tf.reduce_sum(force[...,0], axis=[1,2,3])/batch_size
        #drag_y = tf.reduce_sum(force[...,1], axis=[1,2,3])/batch_size
        #drag_z = tf.reduce_sum(force[...,2], axis=[1,2,3])/batch_size
        """
    sharp_drag_x = tf.reduce_sum(sharp_force[...,0], axis=[1,2,3])/batch_size
    sharp_drag_y = tf.reduce_sum(sharp_force[...,1], axis=[1,2,3])/batch_size
    sharp_drag_z = tf.reduce_sum(sharp_force[...,2], axis=[1,2,3])/batch_size
    solver_drag_x = tf.reduce_sum(solver_force[...,0], axis=[1,2,3])/batch_size
    solver_drag_y = tf.reduce_sum(solver_force[...,1], axis=[1,2,3])/batch_size
    solver_drag_z = tf.reduce_sum(solver_force[...,2], axis=[1,2,3])/batch_size
    """

        #drag_x = tf.concat([drag_x_0, drag_x_1, drag_x_2], axis=0)
        #drag_y = tf.concat([drag_y_0, drag_y_1, drag_y_2], axis=0)
        #drag_z = tf.concat([drag_z_0, drag_z_1, drag_z_2], axis=0)
        #drag_lift_ratio        = -(drag_x/drag_z)
        #sharp_drag_lift_ratio  = -(sharp_drag_x/sharp_drag_z)
        #solver_drag_lift_ratio = -(solver_drag_x/solver_drag_z)

        # loss
        #loss = - tf.abs(tf.constant([-25.0, 100.0, 200.0]) - drag_x) - drag_z
        #loss = drag_x - drag_z/2.0
        #loss = -tf.reduce_sum(drag_lift_ratio)
        #loss += squeeze_loss

        # train_op
        #variables_to_train = tf.all_variables()
        #variables_to_train = [variable for i, variable in enumerate(variables_to_train) if "params" in variable.name[:variable.name.index(':')]]
        #train_step = flow_net.train(loss, FLAGS.boundary_learn_lr, train_type="boundary_params", variables=variables_to_train)

        # init graph
        init = tf.global_variables_initializer()

        # Restore the moving average version of the learned variables for eval.
        variables_to_restore = tf.all_variables()
        variables_to_restore_boundary = [
            variable for i, variable in enumerate(variables_to_restore)
            if "boundary_network" in variable.name[:variable.name.index(':')]
        ]
        variables_to_restore_flow = [
            variable for i, variable in enumerate(variables_to_restore)
            if "flow_network" in variable.name[:variable.name.index(':')]
        ]
        saver_boundary = tf.train.Saver(variables_to_restore_boundary)
        saver_flow = tf.train.Saver(variables_to_restore_flow)

        # start ses and init
        sess = tf.Session()
        sess.run(init)
        ckpt_boundary = tf.train.get_checkpoint_state(BOUNDARY_DIR)
        ckpt_flow = tf.train.get_checkpoint_state(FLOW_DIR)
        saver_boundary.restore(sess, ckpt_boundary.model_checkpoint_path)
        saver_flow.restore(sess, ckpt_flow.model_checkpoint_path)

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

        params_np = (np.random.rand(1, FLAGS.nr_boundary_params) - .5) / 2.0
        #params_np = (np.random.rand(1,FLAGS.nr_boundary_params) - .5)/2.0
        #params_np = np.zeros((1,FLAGS.nr_boundary_params-1))

        sess.run(params_op_init, feed_dict={params_op_set: params_np})
        run_time = FLAGS.boundary_learn_steps

        # make store vectors for values
        plot_error = np.zeros((run_time))
        plot_drag_x = np.zeros((run_time))
        plot_drag_y = np.zeros((run_time))
        plot_drag_z = np.zeros((run_time))

        # make store dir
        os.system("mkdir ./figs/boundary_learn_image_store")
        for i in tqdm(xrange(run_time)):
            #l_0, _ = sess.run([loss_0, train_step_0], feed_dict={})
            l, _, d_x, d_y, d_z = sess.run(
                [loss_gen[0], train_step, drag_x_2, drag_y_2, drag_z_2],
                feed_dict={})
            #l_2, _ = sess.run([loss_2, train_step_2], feed_dict={})
            plot_error[i] = np.sum(l)
            plot_drag_x[i] = np.sum(d_x[fig_pos])
            plot_drag_y[i] = np.sum(d_y[fig_pos])
            plot_drag_z[i] = np.sum(d_z[fig_pos])
            if ((i + 1) % 20 == 0) or i == run_time - 1:
                # make video with opencv
                """
        s_params = sess.run(params_op)
        wing_boundary = []
        for p in xrange(s_params.shape[0]):
          wing_boundary.append(wing_boundary_3d(s_params[p,0], s_params[p,1], s_params[p,2], 
                                                s_params[p,3], s_params[p,4], s_params[p,5],
                                                s_params[p,6:int((FLAGS.nr_boundary_params-7)/3+6)],
                                                s_params[p,int((FLAGS.nr_boundary_params-7)/3+6):int(2*(FLAGS.nr_boundary_params-7)/3+6)],
                                                s_params[p,int(2*(FLAGS.nr_boundary_params-7)/3+6):-1],
                                                s_params[p,-1], FLAGS.dims*[FLAGS.obj_size]))
        wing_boundary = np.stack(wing_boundary)
        wing_boundary = np.pad(wing_boundary, [[0,0],[24,24],[24,24],[24,24],[0,0]], 'constant', constant_values=0.0)
        #print(sharp_boundary.get_shape())
        #print(wing_boundary.shape)
        p_flow, p_boundary, d_l_ratio, sharp_d_l_ratio = sess.run([sharp_predicted_flow, boundary, drag_lift_ratio, sharp_drag_lift_ratio],feed_dict={sharp_boundary: wing_boundary})
        """
                p_flow, p_boundary, d_l_ratio_0, d_l_ratio_1, d_l_ratio_2 = sess.run(
                    [
                        predicted_flow_2, boundary_2, drag_lift_ratio_0,
                        drag_lift_ratio_1, drag_lift_ratio_2
                    ])
                d_l_ratio = np.concatenate(
                    [d_l_ratio_0, d_l_ratio_1, d_l_ratio_2], axis=0)

                # save plot image to make video
                #p_pressure = p_flow[fig_pos,:,:,72,2]
                p_boundary = np.concatenate([
                    p_boundary[fig_pos, :, 71, :, 0], p_boundary[fig_pos, :, :,
                                                                 71, 0]
                ],
                                            axis=0)
                p_pressure = np.concatenate([
                    p_flow[fig_pos, :, 71, :, 3], p_flow[fig_pos, :, :, 71, 3]
                ],
                                            axis=0)
                #p_pressure = p_flow[:,:,76,:,3].reshape((p_boundary.shape[0]*p_boundary.shape[1], p_boundary.shape[2]))
                #p_boundary = p_boundary[:,:,76,:,0].reshape((p_boundary.shape[0]*p_boundary.shape[1], p_boundary.shape[2]))

                fig = plt.figure()
                fig.set_size_inches(20, 5)
                #a = fig.add_subplot(1,5,1)
                #plt.imshow(p_pressure)
                a = fig.add_subplot(1, 4, 1)
                plt.imshow(p_boundary)
                a = fig.add_subplot(1, 4, 2)
                plt.plot(plot_error, label="Sum(Lift/Drag)")
                plt.xlabel("Step")
                plt.legend()
                a = fig.add_subplot(1, 4, 3)
                plt.plot(plot_drag_x, label="Lift Angle 0")
                plt.plot(-plot_drag_z, label="Drag Angle 0")
                plt.ylim(0.0, np.max(plot_drag_x) + 30.0)
                plt.xlabel("Step")
                plt.legend()
                a = fig.add_subplot(1, 4, 4)
                plt.plot(np.degrees(set_params[:, 0]),
                         d_l_ratio,
                         'bo',
                         label="Lift/Drag Network")
                #plt.plot(-np.degrees(set_params[3:6,0]), sharp_d_l_ratio, 'ro', label="Lift/Drag Sharp")
                #if i == run_time-1:
                #  solver_d_l_ratio = run_flow_solver(sess.run(params_op), solver_boundary, solver_flow, sess, solver_drag_lift_ratio)
                #  plt.plot(-np.degrees(set_params[:,0]), solver_d_l_ratio, 'go', label="Lift/Drag Solver")
                plt.xlabel("Angle of Attack (Degrees)")
                plt.xlim(
                    min(np.degrees(set_params[:, 0])) - 3,
                    max(np.degrees(set_params[:, 0])) + 3)
                plt.ylim(np.min(d_l_ratio) - 1.0, np.max(d_l_ratio) + 1.0)
                plt.legend()
                plt.suptitle("3D Wing Optimization Using Gradient Descent",
                             fontsize=20)
                plt.savefig("./figs/boundary_learn_image_store/plot_" +
                            str(i).zfill(5) + ".png")
                if run_time - i <= 100:
                    plt.savefig("./figs/" + FLAGS.boundary_learn_loss +
                                "_plot.png")
                if i == run_time - 1:
                    plt.savefig("./figs/learn_gradient_descent.pdf")
                    plt.show()
                plt.show()
                plt.close(fig)

        np.save("figs/3d_wing_params_op", sess.run(params_op[6]))
        print(sess.run(params_op[6]))
def evaluate():
  """Run Eval once.

  Args:
    saver: Saver.
    summary_writer: Summary writer.
    top_k_op: Top K op.
    summary_op: Summary op.
  """
  num_angles = 4
  max_angle =  0.2
  min_angle = -0.1
  set_params          = np.array(num_angles*[FLAGS.nr_boundary_params*[0.0]])
  set_params[:,:]     = 0.0
  set_params_pos      = np.array(num_angles*[FLAGS.nr_boundary_params*[0.0]])
  set_params_pos[:,:] = 1.0

  for i in xrange(num_angles):
    set_params[i,0]      = -i 
  set_params[:,0] = ((max_angle - min_angle) * (set_params[:,0]/num_angles)) - min_angle

  set_params[:,1]      = 0.5
  set_params[:,2]      = 1.0
  set_params[:,-1]     = 0.0

  set_params_pos[:,0]  = 0.0 # set angle to 0.0
  set_params_pos[:,1]  = 0.0 # set n_1 to .5
  set_params_pos[:,2]  = 0.0 # set n_2 to 1.0
  set_params_pos[:,-1] = 0.0 # set tail hieght to 0.0

  with tf.Graph().as_default():
    # Make image placeholder
    params_op, params_op_init, params_op_set, squeeze_loss = flow_net.inputs_boundary_learn(batch_size, set_params=set_params, set_params_pos=set_params_pos, noise_std=0.001)

    # Make boundary
    boundary = flow_net.inference_boundary(batch_size*set_params.shape[0], FLAGS.dims*[FLAGS.obj_size], params_op, full_shape=shape)
    sharp_boundary = tf.round(boundary)

    # predict steady flow on boundary
    predicted_flow = flow_net.inference_network(boundary)
    predicted_sharp_flow = flow_net.inference_network(sharp_boundary)

    # quantities to optimize
    force = calc_force(boundary, predicted_flow[:,:,:,2:3])
    sharp_force = calc_force(sharp_boundary, predicted_sharp_flow[:,:,:,2:3])
    drag_x = tf.reduce_sum(force[:,:,:,0], axis=[1,2])/batch_size
    drag_y = tf.reduce_sum(force[:,:,:,1], axis=[1,2])/batch_size
    sharp_drag_x = tf.reduce_sum(sharp_force[:,:,:,0], axis=[1,2])/batch_size
    sharp_drag_y = tf.reduce_sum(sharp_force[:,:,:,1], axis=[1,2])/batch_size
    
    drag_lift_ratio = (drag_x/drag_y)
    sharp_drag_lift_ratio = (sharp_drag_x/sharp_drag_y)

    # loss
    loss = -tf.reduce_sum(drag_lift_ratio)

    # init graph
    init = tf.global_variables_initializer()

    # Restore the moving average version of the learned variables for eval.
    variables_to_restore = tf.all_variables()
    variables_to_restore_boundary = [variable for i, variable in enumerate(variables_to_restore) if "boundary_network" in variable.name[:variable.name.index(':')]]
    variables_to_restore_flow = [variable for i, variable in enumerate(variables_to_restore) if "flow_network" in variable.name[:variable.name.index(':')]]
    saver_boundary = tf.train.Saver(variables_to_restore_boundary)
    saver_flow = tf.train.Saver(variables_to_restore_flow)

    # start ses and init
    sess = tf.Session()
    sess.run(init)
    ckpt_boundary = tf.train.get_checkpoint_state(BOUNDARY_DIR)
    ckpt_flow = tf.train.get_checkpoint_state(FLOW_DIR)
    saver_boundary.restore(sess, ckpt_boundary.model_checkpoint_path)
    saver_flow.restore(sess, ckpt_flow.model_checkpoint_path)
    
    graph_def = tf.get_default_graph().as_graph_def(add_shapes=True)

    params_np = (np.random.rand(1,FLAGS.nr_boundary_params) - .5)
 
    sess.run(params_op_init, feed_dict={params_op_set: params_np})
    run_time = FLAGS.boundary_learn_steps

    # make store vectors for values
    plot_error = np.zeros((run_time))
    plot_drag_y = np.zeros((run_time))
    plot_drag_x = np.zeros((run_time))

    # make store dir
    os.system("mkdir ./figs/boundary_learn_image_store")

    # simulated annealing params
    temp = 0.1
    param_old = params_np 
    param_new = distort_param(params_np, std)
    fittness_old = sess.run(loss)
    fittness_new = 0.0

    for i in tqdm(xrange(run_time)):
      sess.run(params_op_init, feed_dict={params_op_set: param_new})
      fittness_new = sess.run(loss)
      print(fittness_new)
      print(fittness_old)
      param_old, fittness_old, temp = simulated_annealing_step(param_old, fittness_old, param_new, fittness_new, temp=temp)
      print(temp)
      param_new = distort_param(param_old, std)

      l, d_y, d_x, p_o = sess.run([loss, sharp_drag_y, sharp_drag_x, params_op], feed_dict={})
      plot_error[i] = np.sum(l)
      plot_drag_x[i] = np.sum(d_x[2])
      plot_drag_y[i] = np.sum(d_y[2])
      if (i+1) % 400 == 0:
        # make video with opencv
        velocity_norm_g, boundary_g = sess.run([predicted_sharp_flow, sharp_boundary],feed_dict={})
        d_y, d_x, l_c, p_o = sess.run([sharp_drag_y, sharp_drag_x, sharp_drag_lift_ratio, params_op], feed_dict={})
        #velocity_norm_g, boundary_g = sess.run([force, boundary],feed_dict={})
        #sflow_plot = np.concatenate([ 5.0*velocity_norm_g[0], boundary_g[0]], axis=1)
        #sflow_plot = np.uint8(grey_to_short_rainbow(sflow_plot))
        #sflow_plot = cv2.applyColorMap(sflow_plot
        #video.write(sflow_plot)
    
        # save plot image to make video
        velocity_norm_g = velocity_norm_g[2,:,:,2]
        boundary_g = boundary_g[2,:,:,0]
        fig = plt.figure()
        fig.set_size_inches(15.5, 7.5)
        a = fig.add_subplot(1,5,1)
        plt.imshow(velocity_norm_g)
        a = fig.add_subplot(1,5,2)
        plt.imshow(boundary_g)
        a = fig.add_subplot(1,5,3)
        plt.plot(plot_error, label="lift/drag")
        plt.xlabel("step")
        plt.legend()
        a = fig.add_subplot(1,5,4)
        plt.plot(plot_drag_x, label="drag_x")
        plt.plot(plot_drag_y, label="drag_y")
        plt.xlabel("step")
        plt.legend()
        a = fig.add_subplot(1,5,5)
        plt.plot(set_params[:,0], l_c, 'bo', label="lift/drag")
        plt.xlabel("angle of attack")
        plt.xlim(min(set_params[:,0])-0.03, max(set_params[:,0])+0.03)
        #plt.legend()
        plt.suptitle("Using Gradient Decent")
        plt.savefig("./figs/boundary_learn_image_store/plot_" + str(i).zfill(5) + ".png")
        if run_time - i <= 100:
          plt.savefig("./figs/" + FLAGS.boundary_learn_loss + "_plot.png")
          #plt.show()
        plt.close(fig)

    # close cv video
    video.release()
    cv2.destroyAllWindows()

    # generate video of plots
    os.system("rm ./figs/" + FLAGS.boundary_learn_loss + "_plot_video.mp4")
    os.system("cat ./figs/boundary_learn_image_store/*.png | ffmpeg -f image2pipe -r 30 -vcodec png -i - -vcodec libx264 ./figs/" + FLAGS.boundary_learn_loss + "_plot_video.mp4")
    os.system("rm -r ./figs/boundary_learn_image_store")
Example #7
0
def train():
    """Train ring_net for a number of steps."""
    with tf.Graph().as_default():
        # global step counter
        global_step = tf.get_variable('global_step', [],
                                      initializer=tf.constant_initializer(0),
                                      trainable=False)

        # make inputs
        input_dims = FLAGS.nr_boundary_params
        inputs_vector, true_boundary = flow_net.inputs_boundary(
            input_dims, FLAGS.batch_size, shape)

        # create and unrap network
        predicted_boundary = flow_net.inference_boundary(
            FLAGS.batch_size, shape, inputs_vector)

        # calc error
        error = flow_net.loss_boundary(true_boundary, predicted_boundary)

        # train hopefuly
        train_op = flow_net.train(error,
                                  FLAGS.lr,
                                  train_type="boundary_network",
                                  global_step=global_step)

        # List of all Variables
        variables = tf.global_variables()

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

        # 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
        variables_to_restore = tf.all_variables()
        variables_to_restore_flow = [
            variable for i, variable in enumerate(variables_to_restore)
            if ("boundary_network" in variable.name[:variable.name.index(':')])
            or ("global_step" in variable.name[:variable.name.index(':')])
        ]
        saver_restore = tf.train.Saver(variables_to_restore_flow)
        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)

        # make boundary dataset
        dataset = Boundary_data("../../data/",
                                size=FLAGS.obj_size,
                                dim=FLAGS.dims,
                                num_params=input_dims)
        dataset.parse_data()

        # calc number of steps left to run
        run_steps = FLAGS.max_steps - int(sess.run(global_step))
        print(sess.run(global_step))
        for step in xrange(run_steps):
            current_step = sess.run(global_step)
            t = time.time()
            batch_params, batch_boundary = dataset.minibatch(
                batch_size=FLAGS.batch_size,
                signed_distance_function=FLAGS.sdf)
            _, loss_value, gen_boundary = sess.run(
                [train_op, error, predicted_boundary],
                feed_dict={
                    inputs_vector: batch_params,
                    true_boundary: batch_boundary
                })
            elapsed = time.time() - t

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

            if current_step % 100 == 0:
                print("loss value at " + str(loss_value))
                print("time per batch is " + str(elapsed))

            if current_step % 1000 == 0:
                summary_str = sess.run(summary_op,
                                       feed_dict={
                                           inputs_vector: batch_params,
                                           true_boundary: batch_boundary
                                       })
                summary_writer.add_summary(summary_str, current_step)
                checkpoint_path = os.path.join(TRAIN_DIR, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=global_step)
                print("saved to " + TRAIN_DIR)
def evaluate():
    """Run Eval once.

  Args:
    saver: Saver.
    summary_writer: Summary writer.
    top_k_op: Top K op.
    summary_op: Summary op.
  """
    num_angles = 1
    max_angle = 0.0
    min_angle = 0.0
    set_params = np.array(num_angles * [FLAGS.nr_boundary_params * [0.0]])
    set_params[:, :] = 0.0
    set_params_pos = np.array(num_angles * [FLAGS.nr_boundary_params * [0.0]])
    set_params_pos[:, :] = 1.0

    for i in xrange(num_angles):
        set_params[i, 0] = -i
    set_params[:, 0] = ((max_angle - min_angle) *
                        (set_params[:, 0] / num_angles)) - min_angle

    set_params[:, 1] = 0.5
    set_params[:, 2] = 1.0
    set_params[:, -1] = 0.0

    set_params_pos[:, 0] = 0.0  # set angle to 0.0
    set_params_pos[:, 1] = 0.0  # set n_1 to .5
    set_params_pos[:, 2] = 0.0  # set n_2 to 1.0
    set_params_pos[:, -1] = 0.0  # set tail hieght to 0.0

    with tf.Graph().as_default():
        # Make vector placeholder
        params_op, params_op_init, params_op_set, squeeze_loss = flow_net.inputs_boundary_learn(
            batch_size, set_params=set_params, set_params_pos=set_params_pos)

        # Compute boundary
        boundary = flow_net.inference_boundary(batch_size * num_angles,
                                               FLAGS.dims * [FLAGS.obj_size],
                                               params_op,
                                               full_shape=shape)
        boundary = tf.round(boundary)

        # Build a Graph that computes the logits predictions from the
        # inference model.
        predicted_flow = flow_net.inference_network(boundary)

        # quantities to optimize
        force = calc_force(boundary, predicted_flow[:, :, :, 2:3])
        drag_x = tf.reduce_sum(force[:, :, :, 0], axis=[0, 1, 2])
        drag_y = tf.reduce_sum(force[:, :, :, 1], axis=[0, 1, 2])
        #drag_ratio = -(drag_x/drag_y)
        #drag_ratio = (drag_y/drag_x)
        drag_ratio = drag_y

        # init graph
        init = tf.global_variables_initializer()

        # Restore the moving average version of the learned variables for eval.
        variables_to_restore = tf.all_variables()
        variables_to_restore_boundary = [
            variable for i, variable in enumerate(variables_to_restore)
            if "boundary_network" in variable.name[:variable.name.index(':')]
        ]
        variables_to_restore_flow = [
            variable for i, variable in enumerate(variables_to_restore)
            if "flow_network" in variable.name[:variable.name.index(':')]
        ]
        saver_boundary = tf.train.Saver(variables_to_restore_boundary)
        saver_flow = tf.train.Saver(variables_to_restore_flow)

        # start ses and init
        sess = tf.Session()
        sess.run(init)
        ckpt_boundary = tf.train.get_checkpoint_state(BOUNDARY_DIR)
        ckpt_flow = tf.train.get_checkpoint_state(FLOW_DIR)
        saver_boundary.restore(sess, ckpt_boundary.model_checkpoint_path)
        saver_flow.restore(sess, ckpt_flow.model_checkpoint_path)

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

        run_time = FLAGS.boundary_learn_steps

        # make store vectors for values
        best_boundary = []
        max_d_ratio = np.zeros((run_time))
        iteration = np.arange(run_time) * batch_size
        d_ratio_store = []

        # make store dir
        os.system("mkdir ./figs/boundary_random_image_store")
        for i in tqdm(xrange(run_time)):
            input_batch = (
                np.random.rand(batch_size, FLAGS.nr_boundary_params) - .5)
            sess.run(params_op_init, feed_dict={params_op_set: input_batch})
            d_ratio, boundary_batch = sess.run([drag_ratio, boundary])
            #plt.imshow(1.0*sess.run(force)[0,:,:,1] - boundary_batch[0,3:-3,3:-3,0])
            #plt.imshow(1.0*sess.run(force)[0,:,:,1])
            #plt.show()
            d_ratio_store.append(d_ratio)
            if np.max(np.array(d_ratio_store)) <= d_ratio:
                best_boundary = boundary_batch
                best_input = input_batch
            max_d_ratio[i] = np.max(np.array(d_ratio_store))
            if i % 100 == 0:
                # make video with opencv
                sess.run(params_op_init, feed_dict={params_op_set: best_input})
                velocity_norm_g = sess.run(predicted_flow)
                #velocity_norm_g, boundary_g = sess.run([force, boundary],feed_dict={})
                #sflow_plot = np.concatenate([ 5.0*velocity_norm_g[0], boundary_g[0]], axis=1)
                #sflow_plot = np.uint8(grey_to_short_rainbow(sflow_plot))
                #sflow_plot = cv2.applyColorMap(sflow_plot
                #video.write(sflow_plot)

                # save plot image to make video
                velocity_norm_g = velocity_norm_g[fig_pos, :, :, 2]
                boundary_g = best_boundary[fig_pos, :, :, 0]
                fig = plt.figure()
                fig.set_size_inches(25.5, 7.5)
                a = fig.add_subplot(1, 4, 1)
                plt.imshow(velocity_norm_g)
                a = fig.add_subplot(1, 4, 2)
                plt.imshow(boundary_g)
                a = fig.add_subplot(1, 4, 3)
                plt.plot(iteration, max_d_ratio, label="best lift/drag")
                plt.legend(loc=4)
                a = fig.add_subplot(1, 4, 4)
                # the histogram of the data
                n, bins, patches = plt.hist(np.array(d_ratio_store),
                                            50,
                                            normed=1,
                                            facecolor='green')
                #plt.hist(d_ratio_store, 10, normed=1, facecolor='green')
                plt.xlabel("lift/drag")
                plt.ylabel("frequency")
                plt.legend()
                plt.suptitle("Using Random Search")
                plt.savefig("./figs/boundary_random_image_store/plot_" +
                            str(i).zfill(5) + ".png")
                if run_time - i <= 100:
                    plt.savefig("./figs/" + FLAGS.boundary_learn_loss +
                                "_plot.png")
                    #plt.show()
                plt.close(fig)

        # close cv video
        video.release()
        cv2.destroyAllWindows()

        # generate video of plots
        os.system("rm ./figs/random_plot_video.mp4")
        os.system(
            "cat ./figs/boundary_random_image_store/*.png | ffmpeg -f image2pipe -r 30 -vcodec png -i - -vcodec libx264 ./figs/random_plot_video.mp4"
        )
        os.system("rm -r ./figs/boundary_random_image_store")
def evaluate():
    """Run Eval once.

  Args:
    saver: Saver.
    summary_writer: Summary writer.
    top_k_op: Top K op.
    summary_op: Summary op.
  """

    with tf.Graph().as_default():
        # Make image placeholder
        params_op, params_op_init, params_op_set, squeeze_loss = flow_net.inputs_boundary_learn(
            batch_size, network_type="heat", noise_std=0.01)

        # Make boundary
        boundary = flow_net.inference_boundary(batch_size, 2 * [128],
                                               params_op)

        # predict steady flow on boundary
        predicted_heat = flow_net.inference_network(boundary,
                                                    network_type="heat")

        # loss
        loss = tf.reduce_max(predicted_heat)
        loss += squeeze_loss

        # train_op
        variables_to_train = tf.all_variables()
        variables_to_train = [
            variable for i, variable in enumerate(variables_to_train)
            if "params" in variable.name[:variable.name.index(':')]
        ]
        train_step = flow_net.train(loss,
                                    FLAGS.boundary_learn_lr,
                                    train_type="boundary_params",
                                    variables=variables_to_train)

        # init graph
        init = tf.global_variables_initializer()

        # Restore the moving average version of the learned variables for eval.
        variables_to_restore = tf.all_variables()
        variables_to_restore_boundary = [
            variable for i, variable in enumerate(variables_to_restore)
            if "boundary_network" in variable.name[:variable.name.index(':')]
        ]
        variables_to_restore_heat = [
            variable for i, variable in enumerate(variables_to_restore)
            if "heat_network" in variable.name[:variable.name.index(':')]
        ]
        saver_boundary = tf.train.Saver(variables_to_restore_boundary)
        saver_heat = tf.train.Saver(variables_to_restore_heat)

        # start ses and init
        sess = tf.Session()
        sess.run(init)
        ckpt_boundary = tf.train.get_checkpoint_state(BOUNDARY_DIR)
        ckpt_heat = tf.train.get_checkpoint_state(FLOW_DIR)
        saver_boundary.restore(sess, ckpt_boundary.model_checkpoint_path)
        saver_heat.restore(sess, ckpt_heat.model_checkpoint_path)

        # make graph
        graph_def = tf.get_default_graph().as_graph_def(add_shapes=True)

        # total run time
        run_time = FLAGS.boundary_learn_steps

        # use same start for comparison
        start_params_np = (
            np.random.rand(batch_size, FLAGS.nr_boundary_params) - .5) / 2.0

        # gradient decent
        plot_error_gradient_decent = np.zeros((num_runs, run_time))
        for sim in tqdm(xrange(num_runs)):
            sess.run(params_op_init,
                     feed_dict={params_op_set: start_params_np})
            for i in tqdm(xrange(run_time)):
                plot_error_gradient_decent[sim, i] = run_heat_sink_simulation(
                    np.minimum(np.maximum(sess.run(params_op) - 0.5, -.5),
                               0.5))
                sess.run(train_step, feed_dict={})
        gradient_descent_boundary = heat_sink_boundary_2d(
            sess.run(params_op)[0], [128, 128])

        # simulated annealing
        plot_error_simulated_annealing = np.zeros(
            (len(temps), num_runs, run_time))
        for t in tqdm(xrange(len(temps))):
            for sim in tqdm(xrange(num_runs)):
                temp = temps[t]
                param_old = start_params_np
                fittness_old = run_heat_sink_simulation(param_old)
                param_new = distort_param(start_params_np, std)
                fittness_new = 0.0
                for i in tqdm(xrange(run_time)):
                    plot_error_simulated_annealing[t, sim, i] = fittness_old
                    fittness_new = run_heat_sink_simulation(param_new)
                    param_old, fittness_old, temp = simulated_annealing_step(
                        param_old,
                        fittness_old,
                        param_new,
                        fittness_new,
                        temp=temp)
                    param_new = distort_param(param_old, std)

        simulated_annealing_boundary = heat_sink_boundary_2d(
            param_old[0] + .5, [128, 128])

        x = np.arange(run_time)

        plot_error_gradient_decent_mean, plot_error_gradient_decent_std = calc_mean_and_std(
            plot_error_gradient_decent)

        fig = plt.figure()
        fig.set_size_inches(10, 5)
        a = fig.add_subplot(1, 2, 1)
        plt.imshow((simulated_annealing_boundary -
                    .5 * gradient_descent_boundary)[:, :, 0])
        plt.title("Difference in Heat Sink Design", fontsize=16)
        a = fig.add_subplot(1, 2, 2)
        plt.errorbar(x,
                     plot_error_gradient_decent_mean,
                     yerr=plot_error_gradient_decent_std,
                     lw=1.0,
                     label="Gradient Descent")
        for t in tqdm(xrange(len(temps))):
            plot_error_simulated_annealing_mean, plot_error_simulated_annealing_std = calc_mean_and_std(
                plot_error_simulated_annealing[t])
            plt.errorbar(x,
                         plot_error_simulated_annealing_mean,
                         yerr=plot_error_simulated_annealing_std,
                         lw=1.0,
                         label="Simulated Annealing temp = " + str(temps[t]))
        plt.xlabel('Step')
        plt.ylabel('Temp at Source')
        plt.suptitle("Gradient Descent vs Simulated Annealing", fontsize=20)
        plt.legend(loc="upper_left")
        plt.savefig("./figs/heat_learn_comparison.pdf")
        plt.show()
def evaluate():
    """Run Eval once.

  Args:
    saver: Saver.
    summary_writer: Summary writer.
    top_k_op: Top K op.
    summary_op: Summary op.
  """

    num_angles = 9
    max_angle = 0.30
    min_angle = -0.10
    set_params = np.array(num_angles * [FLAGS.nr_boundary_params * [0.0]])
    set_params[:, :] = 0.0
    set_params_pos = np.array(num_angles * [FLAGS.nr_boundary_params * [0.0]])
    set_params_pos[:, :] = 1.0

    for i in xrange(num_angles):
        set_params[i, 0] = -i
    set_params[:, 0] = ((max_angle - min_angle) *
                        (set_params[:, 0] / (num_angles - 1))) - min_angle

    set_params[:, 1] = 0.5
    set_params[:, 2] = 1.0
    set_params[:, -1] = 0.0

    set_params_pos[:, 0] = 0.0  # set angle to 0.0
    set_params_pos[:, 1] = 0.0  # set n_1 to .5
    set_params_pos[:, 2] = 0.0  # set n_2 to 1.0
    set_params_pos[:, -1] = 0.0  # set tail hieght to 0.0

    with tf.Graph().as_default():
        # Make image placeholder
        params_op, params_op_init, params_op_set, squeeze_loss = flow_net.inputs_boundary_learn(
            batch_size,
            set_params=set_params,
            set_params_pos=set_params_pos,
            noise_std=0.01)

        # Make boundary
        boundary = flow_net.inference_boundary(batch_size *
                                               set_params.shape[0],
                                               FLAGS.dims * [FLAGS.obj_size],
                                               params_op,
                                               full_shape=shape)
        #sharp_boundary =

        # predict steady flow on boundary
        predicted_flow = flow_net.inference_network(boundary)

        # quantities to optimize
        force = calc_force(boundary, predicted_flow[:, :, :, 2:3])
        drag_x = tf.reduce_sum(force[:, :, :, 0], axis=[1, 2]) / batch_size
        drag_y = tf.reduce_sum(force[:, :, :, 1], axis=[1, 2]) / batch_size

        drag_lift_ratio = -(drag_y / drag_x)

        # loss
        loss = -tf.reduce_sum(drag_lift_ratio)
        loss += squeeze_loss

        # train_op
        variables_to_train = tf.all_variables()
        variables_to_train = [
            variable for i, variable in enumerate(variables_to_train)
            if "params" in variable.name[:variable.name.index(':')]
        ]
        train_step = flow_net.train(loss,
                                    FLAGS.boundary_learn_lr,
                                    train_type="boundary_params",
                                    variables=variables_to_train)

        # init graph
        init = tf.global_variables_initializer()

        # Restore the moving average version of the learned variables for eval.
        variables_to_restore = tf.all_variables()
        variables_to_restore_boundary = [
            variable for i, variable in enumerate(variables_to_restore)
            if "boundary_network" in variable.name[:variable.name.index(':')]
        ]
        variables_to_restore_flow = [
            variable for i, variable in enumerate(variables_to_restore)
            if "flow_network" in variable.name[:variable.name.index(':')]
        ]
        saver_boundary = tf.train.Saver(variables_to_restore_boundary)
        saver_flow = tf.train.Saver(variables_to_restore_flow)

        # start ses and init
        sess = tf.Session()
        sess.run(init)
        ckpt_boundary = tf.train.get_checkpoint_state(BOUNDARY_DIR)
        ckpt_flow = tf.train.get_checkpoint_state(FLOW_DIR)
        saver_boundary.restore(sess, ckpt_boundary.model_checkpoint_path)
        saver_flow.restore(sess, ckpt_flow.model_checkpoint_path)

        # make graph
        graph_def = tf.get_default_graph().as_graph_def(add_shapes=True)

        # total run time
        run_time = FLAGS.boundary_learn_steps

        # use same start for comparison
        start_params_np = (
            np.random.rand(batch_size, FLAGS.nr_boundary_params) - .5)

        # gradient decent
        plot_error_gradient_decent = np.zeros((num_runs, run_time))
        for sim in tqdm(xrange(num_runs)):
            sess.run(params_op_init,
                     feed_dict={params_op_set: start_params_np})
            for i in tqdm(xrange(run_time)):
                l, _ = sess.run([loss, train_step], feed_dict={})
                if i == run_time - 1:
                    d_l_ratio = sess.run(drag_lift_ratio)
                plot_error_gradient_decent[sim, i] = np.sum(l)

        # simulated annealing
        plot_error_simulated_annealing = np.zeros(
            (len(temps), num_runs, run_time))
        for t in tqdm(xrange(len(temps))):
            for sim in tqdm(xrange(num_runs)):
                sess.run(params_op_init,
                         feed_dict={params_op_set: start_params_np})
                temp = temps[t]
                param_old = start_params_np
                param_new = distort_param(start_params_np, std)
                fittness_old = sess.run(loss)
                fittness_new = 0.0
                for i in tqdm(xrange(run_time)):
                    sess.run(params_op_init,
                             feed_dict={params_op_set: param_new})
                    fittness_new = sess.run(loss)
                    param_old, fittness_old, temp = simulated_annealing_step(
                        param_old,
                        fittness_old,
                        param_new,
                        fittness_new,
                        temp=temp)
                    param_new = distort_param(param_old, std)
                    plot_error_simulated_annealing[t, sim, i] = fittness_old

        x = np.arange(run_time)

        fig = plt.figure()
        fig.set_size_inches(5, 5)

        plot_error_gradient_decent_mean, plot_error_gradient_decent_std = calc_mean_and_std(
            plot_error_gradient_decent)
        plt.errorbar(x,
                     plot_error_gradient_decent_mean,
                     yerr=plot_error_gradient_decent_std,
                     lw=1.0,
                     label="Gradient Descent")

        for t in tqdm(xrange(len(temps))):
            plot_error_simulated_annealing_mean, plot_error_simulated_annealing_std = calc_mean_and_std(
                plot_error_simulated_annealing[t])
            #plt.errorbar(x, plot_error_simulated_annealing_mean, yerr=plot_error_simulated_annealing_std, c='g', lw=1.0, label="Simulated Annealing temp = " + str(temps[t]))
            plt.errorbar(x,
                         plot_error_simulated_annealing_mean,
                         yerr=plot_error_simulated_annealing_std,
                         lw=1.0,
                         label="Simulated Annealing temp = " + str(temps[t]))

        plt.xlabel('Step')
        plt.ylabel('Loss')
        plt.title("Optimization", fontsize=20)
        plt.legend(loc="upper_left")
        plt.savefig("./figs/learn_comparison.pdf")
        plt.show()
def evaluate():
    """Run Eval once.

  Args:
    saver: Saver.
    summary_writer: Summary writer.
    top_k_op: Top K op.
    summary_op: Summary op.
  """

    num_angles = 9
    max_angle = 0.30
    min_angle = -0.10
    set_params = np.array(num_angles * [FLAGS.nr_boundary_params * [0.0]])
    set_params[:, :] = 0.0
    set_params_pos = np.array(num_angles * [FLAGS.nr_boundary_params * [0.0]])
    set_params_pos[:, :] = 1.0

    for i in xrange(num_angles):
        set_params[i, 0] = -i
    set_params[:, 0] = ((max_angle - min_angle) *
                        (set_params[:, 0] / (num_angles - 1))) - min_angle

    set_params[:, 1] = 0.5
    set_params[:, 2] = 1.0
    set_params[:, -1] = 0.0

    set_params_pos[:, 0] = 0.0  # set angle to 0.0
    set_params_pos[:, 1] = 0.0  # set n_1 to .5
    set_params_pos[:, 2] = 0.0  # set n_2 to 1.0
    set_params_pos[:, -1] = 0.0  # set tail hieght to 0.0

    with tf.Graph().as_default():
        # Make image placeholder
        params_op, params_op_init, params_op_set, squeeze_loss = flow_net.inputs_boundary_learn(
            batch_size,
            set_params=set_params,
            set_params_pos=set_params_pos,
            noise_std=0.01)

        # Make placeholder for flow computed by lattice boltzmann solver
        solver_boundary, solver_flow = flow_net.inputs_flow(
            1, shape, FLAGS.dims)
        sharp_boundary, blaa = flow_net.inputs_flow(
            batch_size * set_params.shape[0], shape, FLAGS.dims)

        # Make boundary
        boundary = flow_net.inference_boundary(batch_size *
                                               set_params.shape[0],
                                               FLAGS.dims * [FLAGS.obj_size],
                                               params_op,
                                               full_shape=shape)

        # predict steady flow on boundary
        predicted_flow = flow_net.inference_network(boundary,
                                                    network_type="flow",
                                                    keep_prob=FLAGS.keep_prob)
        sharp_predicted_flow = flow_net.inference_network(
            sharp_boundary, network_type="flow", keep_prob=FLAGS.keep_prob)

        # quantities to optimize
        force = calc_force(boundary, predicted_flow[..., -1:])
        sharp_force = calc_force(sharp_boundary, sharp_predicted_flow[...,
                                                                      -1:])
        solver_force = calc_force(solver_boundary, solver_flow[..., -1:])
        drag_x = tf.reduce_sum(force[..., 0], axis=[1, 2]) / batch_size
        drag_y = tf.reduce_sum(force[..., 1], axis=[1, 2]) / batch_size
        sharp_drag_x = tf.reduce_sum(sharp_force[..., 0], axis=[1, 2
                                                                ]) / batch_size
        sharp_drag_y = tf.reduce_sum(sharp_force[..., 1], axis=[1, 2
                                                                ]) / batch_size
        solver_drag_x = tf.reduce_sum(solver_force[..., 0],
                                      axis=[1, 2]) / batch_size
        solver_drag_y = tf.reduce_sum(solver_force[..., 1],
                                      axis=[1, 2]) / batch_size

        drag_lift_ratio = -(drag_y / drag_x)
        sharp_drag_lift_ratio = -(sharp_drag_y / sharp_drag_x)
        solver_drag_lift_ratio = -(solver_drag_y / solver_drag_x)

        # loss
        loss = -tf.reduce_sum(drag_lift_ratio)
        #loss = -drag_y + drag_x
        #loss = -tf.reduce_sum(drag_x)
        loss += squeeze_loss

        # train_op
        variables_to_train = tf.all_variables()
        variables_to_train = [
            variable for i, variable in enumerate(variables_to_train)
            if "params" in variable.name[:variable.name.index(':')]
        ]
        train_step = flow_net.train(loss,
                                    FLAGS.boundary_learn_lr,
                                    train_type="boundary_params",
                                    variables=variables_to_train)

        # init graph
        init = tf.global_variables_initializer()

        # Restore the moving average version of the learned variables for eval.
        variables_to_restore = tf.all_variables()
        variables_to_restore_boundary = [
            variable for i, variable in enumerate(variables_to_restore)
            if "boundary_network" in variable.name[:variable.name.index(':')]
        ]
        variables_to_restore_flow = [
            variable for i, variable in enumerate(variables_to_restore)
            if "flow_network" in variable.name[:variable.name.index(':')]
        ]
        saver_boundary = tf.train.Saver(variables_to_restore_boundary)
        saver_flow = tf.train.Saver(variables_to_restore_flow)

        # start ses and init
        sess = tf.Session()
        sess.run(init)
        ckpt_boundary = tf.train.get_checkpoint_state(BOUNDARY_DIR)
        ckpt_flow = tf.train.get_checkpoint_state(FLOW_DIR)
        saver_boundary.restore(sess, ckpt_boundary.model_checkpoint_path)
        saver_flow.restore(sess, ckpt_flow.model_checkpoint_path)

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

        params_np = (np.random.rand(1, FLAGS.nr_boundary_params) - .5)
        #params_np = np.zeros((1,FLAGS.nr_boundary_params-1))

        sess.run(params_op_init, feed_dict={params_op_set: params_np})
        run_time = FLAGS.boundary_learn_steps

        # make store vectors for values
        plot_error = np.zeros((run_time))
        plot_drag_y = np.zeros((run_time))
        plot_drag_x = np.zeros((run_time))

        # make store dir
        os.system("mkdir ./figs/boundary_learn_image_store")
        for i in tqdm(xrange(run_time)):
            l, _, d_y, d_x = sess.run([loss, train_step, drag_y, drag_x],
                                      feed_dict={})
            plot_error[i] = np.sum(l)
            plot_drag_x[i] = np.sum(d_x[fig_pos])
            plot_drag_y[i] = np.sum(d_y[fig_pos])
            if ((i + 1) % 1 == 0) or i == run_time - 1:
                # make video with opencv
                s_params = sess.run(params_op)
                wing_boundary = []
                for p in xrange(s_params.shape[0]):
                    wing_boundary.append(
                        wing_boundary_2d(
                            s_params[p, 0], s_params[p, 1], s_params[p, 2],
                            s_params[p, 3:int((FLAGS.nr_boundary_params - 4) /
                                              2)],
                            s_params[p,
                                     int((FLAGS.nr_boundary_params - 4) /
                                         2):-1], s_params[p, -1],
                            FLAGS.dims * [FLAGS.obj_size]))
                wing_boundary = np.stack(wing_boundary)
                wing_boundary = np.pad(
                    wing_boundary, [[0, 0], [128, 128], [128, 128], [0, 0]],
                    'constant',
                    constant_values=0.0)
                #print(sharp_boundary.get_shape())
                #print(wing_boundary.shape)
                p_flow, p_boundary, d_l_ratio, sharp_d_l_ratio = sess.run(
                    [
                        sharp_predicted_flow, boundary, drag_lift_ratio,
                        sharp_drag_lift_ratio
                    ],
                    feed_dict={sharp_boundary: wing_boundary})

                # save plot image to make video
                p_pressure = p_flow[fig_pos, :, :, 2]
                p_boundary = p_boundary[fig_pos, :, :, 0]
                fig = plt.figure()
                fig.set_size_inches(15, 10)
                a = fig.add_subplot(2, 3, 1)
                plt.imshow(p_pressure)
                plt.title("Pressure", fontsize=16)
                a = fig.add_subplot(2, 3, 2)
                plt.imshow(p_boundary)
                plt.title("Boundary", fontsize=16)
                a = fig.add_subplot(2, 3, 3)
                plt.plot(plot_error, label="Sum(Lift/Drag)")
                plt.xlabel("Step")
                plt.legend()
                a = fig.add_subplot(2, 3, 4)
                plt.plot(-plot_drag_x, label="Drag Angle 0")
                plt.plot(plot_drag_y, label="Lift Angle 0")
                plt.ylim(-1.0, np.max(plot_drag_y) + 2.0)
                plt.xlabel("Step")
                plt.legend()
                a = fig.add_subplot(2, 3, 5)
                plt.plot(-np.degrees(set_params[:, 0]),
                         d_l_ratio,
                         'bo',
                         label="Lift/Drag Network")
                #plt.plot(-np.degrees(set_params[:,0]), sharp_d_l_ratio, 'ro', label="Lift/Drag Sharp")
                #if i == run_time-1:
                #  solver_d_l_ratio = run_flow_solver(sess.run(params_op), solver_boundary, solver_flow, sess, solver_drag_lift_ratio)
                #  plt.plot(-np.degrees(set_params[:,0]), solver_d_l_ratio, 'go', label="Lift/Drag Solver")
                plt.xlabel("Angle of Attack (Degrees)")
                plt.xlim(
                    min(-np.degrees(set_params[:, 0])) - 3,
                    max(-np.degrees(set_params[:, 0])) + 3)
                plt.ylim(np.min(d_l_ratio) - 1, np.max(d_l_ratio) + 2)
                plt.legend()
                plt.suptitle("2D Wing Optimization Using Gradient Descent",
                             fontsize=20)
                plt.savefig("./figs/boundary_learn_image_store/plot_" +
                            str(i).zfill(5) + ".png")
                if run_time - i <= 100:
                    plt.savefig("./figs/" + FLAGS.boundary_learn_loss +
                                "_plot.pdf")
                if i == run_time - 1:
                    plt.savefig("./figs/learn_gradient_descent.pdf")
                    plt.show()
                #plt.show()
                plt.close(fig)

        # generate video of plots
        os.system("rm ./figs/airfoil_2d_video.mp4")
        os.system(
            "cat ./figs/boundary_learn_image_store/*.png | ffmpeg -f image2pipe -r 30 -vcodec png -i - -vcodec libx264 ./figs/airfoil_2d_video.mp4"
        )
        os.system("rm -r ./figs/boundary_learn_image_store")
Example #12
0
def evaluate():
    """Run Eval once.
  """
    # get a list of image filenames
    filenames = glb('../data/computed_car_flow/*')
    filenames.sort(key=alphanum_key)
    filename_len = len(filenames)
    batch_size = 1
    #shape = [128, 512]
    #shape = [256, 1024]
    #shape = [128, 256]
    #shape = [64, 256]
    #shape = [16, 64]

    with tf.Session() as sess:
        # Make image placeholder
        input_dims = FLAGS.nr_boundary_params
        input_vector, true_boundary = flow_net.inputs_boundary(input_dims,
                                                               batch_size=1,
                                                               shape=shape)

        # Build a Graph that computes the logits predictions from the
        # inference model.
        #mean, stddev, x_sampled, predicted_boundary = flow_net.inference_boundary(true_boundary, shape)
        predicted_boundary = flow_net.inference_boundary(1,
                                                         FLAGS.dims *
                                                         [FLAGS.obj_size],
                                                         input_vector,
                                                         full_shape=shape)

        # Restore for eval
        init = tf.global_variables_initializer()
        sess.run(init)
        variables_to_restore = tf.all_variables()
        variables_to_restore_boundary = [
            variable for i, variable in enumerate(variables_to_restore)
            if "boundary_network" in variable.name[:variable.name.index(':')]
        ]
        saver = tf.train.Saver(variables_to_restore_boundary)
        print(BOUNDARY_DIR)
        ckpt = tf.train.get_checkpoint_state(BOUNDARY_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:
        for i in xrange(1000):
            # read in boundary
            #batch_boundary, batch_flow = dataset.minibatch(train=False, batch_size=1)

            # calc flow
            input_batch, boundary_batch = flow_net.feed_dict_boundary(
                input_dims, 1, shape)
            """
      #print(input_batch)
      input_batch[:,1] = .5
      input_batch[:,2] = 1.0
      input_batch = [[ 0.,         0.50044733, 1.0007602,  0.04609993, 0.02800636, 0.17423785, 0.32387334, 0.02092246, 0.06054832, 0.10314581, 0.00666449, 0.09281126]]
      """
            p_boundary = sess.run(predicted_boundary,
                                  feed_dict={input_vector: input_batch})
            #p_boundary = sess.run(predicted_boundary,feed_dict={true_boundary: batch_boundary})
            dim = 0
            #sflow_plot = np.concatenate([p_boundary, batch_boundary], axis=1)
            sflow_plot = p_boundary
            sflow_plot = sflow_plot[0, :, :, 0]
            #sflow_plot = sflow_plot[0,:,:,2]

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