def tower_loss(iteration, scope, input_keep_prob, keep_prob):
  # make inputs
  x = ring_net.inputs(CURRICULUM_BATCH_SIZE[iteration], CURRICULUM_SEQ[iteration]) 
  # possible input dropout 
  x_drop = tf.nn.dropout(x, input_keep_prob)
  # create and unrap network
  output_t, output_g, output_f = ring_net.unwrap(x_drop, keep_prob, CURRICULUM_SEQ[iteration]) 
  # calc error
  _ = ring_net.loss(x, output_t, output_g, output_f)
  # error = tf.div(error, CURRICULUM_SEQ[iteration])
  losses = tf.get_collection('losses', scope)
  # calc total loss
  total_loss = tf.add_n(losses, name='total_loss')
  # Compute the moving average of all individual losses and the total loss.
  #loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg')
  #loss_averages_op = loss_averages.apply(losses + [total_loss])
  # Attach a scalar summary to all individual losses and the total loss; do the
  # same for the averaged version of the losses.
  #for l in losses + [total_loss]:
    # Remove 'tower_[0-9]/' from the name in case this is a multi-GPU training
    # session. This helps the clarity of presentation on tensorboard.
  #  loss_name = re.sub('%s_[0-9]*/' % 'tower', '', l.op.name)
    # Name each loss as '(raw)' and name the moving average version of the loss
    # as the original loss name.
  #  tf.scalar_summary(loss_name +' (raw)', l)
  #  tf.scalar_summary(loss_name, loss_averages.average(l))

  #with tf.control_dependencies([loss_averages_op]):
  #  total_loss = tf.identity(total_loss)

  return total_loss
def evaluate():
  """Train ring_net for a number of steps."""
  with tf.Graph().as_default():
    # make inputs
    state, reward, action = ring_net.inputs(1, 15) 

    # possible input dropout 
    input_keep_prob = tf.placeholder("float")
    state_drop = tf.nn.dropout(state, input_keep_prob)

    # possible dropout inside
    keep_prob_encoding = tf.placeholder("float")
    keep_prob_lstm = tf.placeholder("float")

    # unwrap
    reward_2_o = []
    # first step
    x_2, reward_2, hidden_state = ring_net.encode_compress_decode(state[:,0,:,:,:], action[:,1,:], None, keep_prob_encoding, keep_prob_lstm)
    tf.get_variable_scope().reuse_variables()
    # unroll for 9 more steps
    for i in xrange(9):
      x_2, reward_2,  hidden_state = ring_net.encode_compress_decode(state[:,i+1,:,:,:], action[:,i+2,:], hidden_state, keep_prob_encoding, keep_prob_lstm)
    # now collect values
    reward_2_o.append(reward_2)
    for i in xrange(4):
      x_2, reward_2, hidden_state = ring_net.encode_compress_decode(x_2, action[:,i+11,:], hidden_state, keep_prob_encoding, keep_prob_lstm)
      reward_2_o.append(reward_2)
    reward_2_o = tf.pack(reward_2_o)
    reward_2_o = tf.transpose(reward_2_o, perm=[1,0,2])

    # restore network
    variables_to_restore = tf.all_variables()
    saver = tf.train.Saver(variables_to_restore)
    sess = tf.Session()
    ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
    #ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
    if ckpt and ckpt.model_checkpoint_path:
      saver.restore(sess, ckpt.model_checkpoint_path)
      print("restored file from " + ckpt.model_checkpoint_path)
    else:
      print("no chekcpoint file found from " + FLAGS.checkpoint_dir + ", this is an error")

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

    reward_g_o, reward_o = sess.run([reward_2_o, reward],feed_dict={keep_prob_encoding:1.0, keep_prob_lstm:1.0, input_keep_prob:1.0})

    print(reward_g_o.shape)
    print(reward_o.shape)

    plt.figure(0)
    plt.plot(reward_g_o[0,:,0], label= "predicted reward") 
    plt.plot(reward_o[0,10:,0], label= "reward") 
    plt.title("reward vs step")
    plt.xlabel("step")
    plt.ylabel("reward")
    plt.legend()
    plt.savefig("paper_reward.png")
def evaluate():
  """ Eval the system"""
  with tf.Graph().as_default():
    # make inputs
    state_start, reward_start, action_start, = ring_net.inputs(1, 15)
    action_size = int(action_start.get_shape()[2])
    action = tf.placeholder(tf.float32, (1, action_size))

    # unwrap
    x_2_o = []
    # first step
    x_2, reward_2, hidden_state = ring_net.encode_compress_decode(state_start[:,0,:,:,:], action_start[:,1,:], None, 1.0, 1.0)
    tf.get_variable_scope().reuse_variables()
    # unroll for 9 more steps
    for i in xrange(9):
      x_2, reward_2,  hidden_state = ring_net.encode_compress_decode(state_start[:,i+1,:,:,:], action_start[:,i+2,:], hidden_state, 1.0, 1.0)

    # rename output_t
    x_1 = x_2
    print(hidden_state)
    hidden_state_1 = hidden_state
    x_2, reward_2, hidden_state_2 = ring_net.encode_compress_decode(x_1, action, hidden_state_1,  1.0, 1.0)

    # restore network
    variables_to_restore = tf.all_variables()
    saver = tf.train.Saver(variables_to_restore)
    sess = tf.Session()
    ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
    #ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
    if ckpt and ckpt.model_checkpoint_path:
      saver.restore(sess, ckpt.model_checkpoint_path)
      print("restored file from " + ckpt.model_checkpoint_path)
    else:
      print("no chekcpoint file found from " + FLAGS.checkpoint_dir + ", this is an error")

    # get frame
    tf.train.start_queue_runners(sess=sess)
    play_action = random_action(action_size)
    x_2_g, hidden_2_g = sess.run([x_1, hidden_state_1], feed_dict={})

    # Play!!!! 
    for step in xrange(100):
      print(step)
      #time.sleep(.5)
      # calc generated frame from t
      play_action = random_action(action_size)
      x_2_g, hidden_2_g = sess.run([x_2, hidden_state_2],feed_dict={x_1:x_2_g, hidden_state_1:hidden_2_g, action:play_action})
      frame = np.uint8(np.minimum(np.maximum(0, x_2_g*255.0), 255))
      frame = frame[0, :, :, :]
      video.write(frame)
      #frame = cv2.resize(frame, (500, 500))
      #cv2.imshow('frame', frame)
      #cv2.waitKey(0)
      #if cv2.waitKey(1) & 0xFF == ord('q'):
      #  break
    video.release()
    cv2.destroyAllWindows()
def evaluate():
  """ Eval the system"""
  with tf.Graph().as_default():
    # make inputs
    x = ring_net.inputs(1, NUM_FRAMES) 
    # unwrap it
    keep_prob = tf.placeholder("float")
    output_t, output_g, output_f = ring_net.unwrap(x, keep_prob, NUM_FRAMES) 

    # restore network
    variables_to_restore = tf.all_variables()
    saver = tf.train.Saver(variables_to_restore)
    sess = tf.Session()
    ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir + FLAGS.model + FLAGS.system)
    #ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
    if ckpt and ckpt.model_checkpoint_path:
      saver.restore(sess, ckpt.model_checkpoint_path)
      print("restored file from " + ckpt.model_checkpoint_path)
    else:
      print("no chekcpoint file found, this is an error")

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

    # eval ounce
    generated_seq, hidden_states, inputs = sess.run([output_g, output_f, x],feed_dict={keep_prob:1.0})
    generated_seq = generated_seq[0]
    inputs = inputs[0]
 
    # make video
    hidden_im = np.zeros((100,100,3))
    for step in xrange(NUM_FRAMES-1):
      # calc image from y_2
      print(step)
      #hidden_im = np.zeros((100,100,3))
      new_im = np.concatenate((generated_seq[step, :, :, 0:3].squeeze()/np.amax(generated_seq[step, :, :, 0:3]), inputs[step,:,:,0:3].squeeze()/np.amax(inputs[step,:,:,0:3]), generated_seq[step, :, :, 0:3].squeeze() - inputs[step, :, :, 0:3].squeeze()), axis=0)
      #hidden_im[0:32,0:32,0] = hidden_states[step,:].squeeze().reshape(32,32)
      #hidden_im[0:32,0:32,1] = hidden_states[step,:].squeeze().reshape(32,32)
      #hidden_im[0:32,0:32,2] = hidden_states[step,:].squeeze().reshape(32,32)
      new_im = np.uint8(np.abs(new_im * 255))
      #hidden_im = np.uint8(np.abs(hidden_im * 255))
      #print(new_im.shape)
      video.write(new_im)
      #video2.write(hidden_im)
    print('saved to ' + FLAGS.video_name)
    video.release()
    #video2.release()
    cv2.destroyAllWindows()
def evaluate():
  """ Eval the system"""
  with tf.Graph().as_default():
    # make inputs
    x = ring_net.inputs(1, 1)
    # unwrap it
    keep_prob = tf.placeholder("float")
    y_0 = unwrap_helper_test.encoding(x, keep_prob)
    x_1, y_1, hidden_state_1 = unwrap_helper_test.lstm_step(y_0, None,  keep_prob) 
    # set reuse to true 
    tf.get_variable_scope().reuse_variables()
    x_2, y_2, hidden_state_2 = unwrap_helper_test.lstm_step(y_1, hidden_state_1,  keep_prob) 

    # restore network
    variables_to_restore = tf.all_variables()
    saver = tf.train.Saver(variables_to_restore)
    sess = tf.Session()
    ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir + FLAGS.model + FLAGS.system)
    #ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
    if ckpt and ckpt.model_checkpoint_path:
      saver.restore(sess, ckpt.model_checkpoint_path)
      print("restored file from " + ckpt.model_checkpoint_path)
    else:
      print("no chekcpoint file found, this is an error")

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

    # eval ounce
    generated_x_1, generated_y_1, generated_hidden_state_1 = sess.run([x_1, y_1, hidden_state_1],feed_dict={keep_prob:1.0})
    new_im = np.uint8(np.abs(generated_x_1/np.amax(generated_x_1[0, :, :, :]) * 255))
    video.write(new_im[0,:,:,:])
 
    # make video
    for step in xrange(NUM_FRAMES-1):
      # continue to calc frames
      print(step)
      generated_x_1, generated_y_1, generated_hidden_state_1 = sess.run([x_2, y_2, hidden_state_2],feed_dict={keep_prob:1.0, y_1:generated_y_1, hidden_state_1:generated_hidden_state_1})
      new_im = np.uint8(np.abs(generated_x_1/np.amax(generated_x_1[0, :, :, :]) * 255))
      video.write(new_im[0,:,:,:])
    print('saved to ' + FLAGS.video_name)
    video.release()
    #video2.release()
    cv2.destroyAllWindows()
def evaluate():
  """ Eval the system"""
  with tf.Graph().as_default():
    # make inputs
    x = ring_net.inputs(1, NUM_FRAMES) 
    # unwrap it
    keep_prob = tf.placeholder("float")
    output_t, output_g, output_f = ring_net.unwrap(x, keep_prob, NUM_FRAMES) 

    # restore network
    variables_to_restore = tf.all_variables()
    saver = tf.train.Saver(variables_to_restore)
    sess = tf.Session()
    ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir + FLAGS.model + FLAGS.system)
    #ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
    if ckpt and ckpt.model_checkpoint_path:
      saver.restore(sess, ckpt.model_checkpoint_path)
      print("restored file from " + ckpt.model_checkpoint_path)
    else:
      print("no chekcpoint file found, this is an error")

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

    # eval ounce
    generated_seq, inputs = sess.run([output_g, x],feed_dict={keep_prob:1.0})
    generated_seq = generated_seq[0]
    inputs = inputs[0]
 
    # make video
    ims_generated = []
    fig = plt.figure()
    for step in xrange(NUM_FRAMES):
      # calc image from y_2
      print(step)
      new_im = np.concatenate((generated_seq[step, :, :, 2].squeeze()/np.amax(generated_seq[step, :, :, 2]), inputs[step,:,:,2].squeeze()/np.amax(inputs[step,:,:,2]), (generated_seq[step, :, :, 2].squeeze() - inputs[step,:,:,2].squeeze())), axis=0)
      ims_generated.append((plt.imshow(new_im),))
    m_ani = animation.ArtistAnimation(fig, ims_generated, interval= 5000, repeat_delay=3000, blit=True)
    print(FLAGS.video_name)
    m_ani.save(FLAGS.video_name, writer=writer)
def train():
  """Train ring_net for a number of steps."""
  with tf.Graph().as_default():
    # make inputs
    state, reward, action = ring_net.inputs(4, 15) 

    # possible input dropout 
    input_keep_prob = tf.placeholder("float")
    state_drop = tf.nn.dropout(state, input_keep_prob)

    # possible dropout inside
    keep_prob_encoding = tf.placeholder("float")
    keep_prob_lstm = tf.placeholder("float")

    # unwrap
    x_2_o = []
    # first step
    x_2, reward_2, hidden_state = ring_net.encode_compress_decode(state[:,0,:,:,:], action[:,1,:], None, keep_prob_encoding, keep_prob_lstm)
    tf.get_variable_scope().reuse_variables()
    # unroll for 9 more steps
    for i in xrange(9):
      x_2, reward_2,  hidden_state = ring_net.encode_compress_decode(state[:,i+1,:,:,:], action[:,i+2,:], hidden_state, keep_prob_encoding, keep_prob_lstm)
    # now collect values
    x_2_o.append(x_2)
    for i in xrange(4):
      x_2, reward_2, hidden_state = ring_net.encode_compress_decode(x_2, action[:,i+11,:], hidden_state, keep_prob_encoding, keep_prob_lstm)
      x_2_o.append(x_2)
      tf.image_summary('images_gen_' + str(i), x_2)
    x_2_o = tf.pack(x_2_o)
    x_2_o = tf.transpose(x_2_o, perm=[1,0,2,3,4])

    # error
    error = tf.nn.l2_loss(state[:,10:15,:,:,:] - x_2_o)
    tf.scalar_summary('loss', error)

    # train (hopefuly)
    train_op = ring_net.train(error, 1e-5)
    
    # List of all Variables
    variables = tf.all_variables()

    # Build a saver
    saver = tf.train.Saver(tf.all_variables(), max_to_keep=1)   

    # Summary op
    summary_op = tf.merge_all_summaries()
 
    # Start running operations on the Graph.
    sess = tf.Session()

    # init from seq 1 model
    print("init from " + RESTORE_DIR)
    saver_restore = tf.train.Saver(variables)
    ckpt = tf.train.get_checkpoint_state(RESTORE_DIR)
    saver_restore.restore(sess, ckpt.model_checkpoint_path)


    # 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.train.SummaryWriter(SAVE_DIR, graph_def=graph_def)

    for step in xrange(500000):
      t = time.time()
      _ , loss_value = sess.run([train_op, error],feed_dict={keep_prob_encoding:1.0, keep_prob_lstm:1.0, input_keep_prob:1.0})
      elapsed = time.time() - t

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

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

      if step%1000 == 0:
        checkpoint_path = os.path.join(SAVE_DIR, 'model.ckpt')
        saver.save(sess, checkpoint_path, global_step=step)  
        print("saved to " + SAVE_DIR)
Example #8
0
def evaluate():
    """Train ring_net for a number of steps."""
    with tf.Graph().as_default():
        # make inputs
        state, reward, action = ring_net.inputs(1, 15)

        # possible input dropout
        input_keep_prob = tf.placeholder("float")
        state_drop = tf.nn.dropout(state, input_keep_prob)

        # possible dropout inside
        keep_prob_encoding = tf.placeholder("float")
        keep_prob_lstm = tf.placeholder("float")

        # unwrap
        reward_2_o = []
        # first step
        x_2, reward_2, hidden_state = ring_net.encode_compress_decode(
            state[:, 0, :, :, :], action[:, 1, :], None, keep_prob_encoding,
            keep_prob_lstm)
        tf.get_variable_scope().reuse_variables()
        # unroll for 9 more steps
        for i in xrange(9):
            x_2, reward_2, hidden_state = ring_net.encode_compress_decode(
                state[:, i + 1, :, :, :], action[:, i + 2, :], hidden_state,
                keep_prob_encoding, keep_prob_lstm)
        # now collect values
        reward_2_o.append(reward_2)
        for i in xrange(4):
            x_2, reward_2, hidden_state = ring_net.encode_compress_decode(
                x_2, action[:, i + 11, :], hidden_state, keep_prob_encoding,
                keep_prob_lstm)
            reward_2_o.append(reward_2)
        reward_2_o = tf.pack(reward_2_o)
        reward_2_o = tf.transpose(reward_2_o, perm=[1, 0, 2])

        # restore network
        variables_to_restore = tf.all_variables()
        saver = tf.train.Saver(variables_to_restore)
        sess = tf.Session()
        ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
        #ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            print("restored file from " + ckpt.model_checkpoint_path)
        else:
            print("no chekcpoint file found from " + FLAGS.checkpoint_dir +
                  ", this is an error")

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

        reward_g_o, reward_o = sess.run([reward_2_o, reward],
                                        feed_dict={
                                            keep_prob_encoding: 1.0,
                                            keep_prob_lstm: 1.0,
                                            input_keep_prob: 1.0
                                        })

        print(reward_g_o.shape)
        print(reward_o.shape)

        plt.figure(0)
        plt.plot(reward_g_o[0, :, 0], label="predicted reward")
        plt.plot(reward_o[0, 10:, 0], label="reward")
        plt.title("reward vs step")
        plt.xlabel("step")
        plt.ylabel("reward")
        plt.legend()
        plt.savefig("paper_reward.png")
def train(iteration):
  """Train ring_net for a number of steps."""
  with tf.Graph().as_default():
    # make inputs
    state, reward, action = ring_net.inputs(CURRICULUM_BATCH_SIZE[iteration], CURRICULUM_SEQ[iteration]) 

    # possible input dropout 
    input_keep_prob = tf.placeholder("float")
    state_drop = tf.nn.dropout(state, input_keep_prob)

    # possible dropout inside
    keep_prob_encoding = tf.placeholder("float")
    keep_prob_lstm = tf.placeholder("float")

    # create and unrap network
    output_f, output_t, output_g, output_reward, output_autoencoder = ring_net.unwrap(state_drop, action, keep_prob_encoding, keep_prob_lstm, CURRICULUM_SEQ[iteration], CURRICULUM_TRAIN_PEICE[iteration])

    # calc error
    error = ring_net.loss(state, reward, output_f, output_t, output_g, output_reward, output_autoencoder, CURRICULUM_TRAIN_PEICE[iteration])
    error = tf.div(error, CURRICULUM_SEQ[iteration])

    # train (hopefuly)
    train_op = ring_net.train(error, CURRICULUM_LEARNING_RATE[iteration])
    
    # List of all Variables
    variables = tf.all_variables()
    #for i, variable in enumerate(variables):
    #  print '----------------------------------------------'
    #  print variable.name[:variable.name.index(':')]

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

    # Summary op
    summary_op = tf.merge_all_summaries()
 
    # Build an initialization operation to run below.
    if iteration == 0:
      init = tf.initialize_all_variables()

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

    # init if this is the very time training
    if iteration == 0: 
      print("init network from scratch")
      sess.run(init)

    # restore if iteration is not 0
    if iteration != 0:
      variables_to_restore = tf.all_variables()
      autoencoder_variables = [variable for i, variable in enumerate(variables_to_restore) if ("compress" not in variable.name[:variable.name.index(':')]) and ("RNN" not in variable.name[:variable.name.index(':')])]
      rnn_variables = [variable for i, variable in enumerate(variables_to_restore) if ("compress" in variable.name[:variable.name.index(':')]) or ("RNN" in variable.name[:variable.name.index(':')])]
     
      ckpt = tf.train.get_checkpoint_state(SAVE_DIR)
      autoencoder_saver = tf.train.Saver(autoencoder_variables)
      print("restoring autoencoder part of network form " + ckpt.model_checkpoint_path)
      autoencoder_saver.restore(sess, ckpt.model_checkpoint_path)
      print("restored file from " + ckpt.model_checkpoint_path)

      if CURRICULUM_SEQ[iteration-1] == 1:
        print("init compression part of network from scratch")
        rnn_init = tf.initialize_variables(rnn_variables)
        sess.run(rnn_init)
      else:
        rnn_saver = tf.train.Saver(rnn_variables)
        print("restoring compression part of network")
        rnn_saver.restore(sess, ckpt.model_checkpoint_path)
        print("restored file from " + ckpt.model_checkpoint_path)
        
    # 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.train.SummaryWriter(SAVE_DIR, graph_def=graph_def)

    for step in xrange(CURRICULUM_STEPS[iteration]):
      t = time.time()
      _ , loss_value = sess.run([train_op, error],feed_dict={keep_prob_encoding:1.0, keep_prob_lstm:1.0, input_keep_prob:.8})
      elapsed = time.time() - t

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

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

      if step%1000 == 0:
        checkpoint_path = os.path.join(SAVE_DIR, 'model.ckpt')
        saver.save(sess, checkpoint_path, global_step=step)  
        print("saved to " + SAVE_DIR)
Example #10
0
def train():
    """Train ring_net for a number of steps."""
    with tf.Graph().as_default():
        # make inputs
        state, reward, action = ring_net.inputs(4, 15)

        # possible input dropout
        input_keep_prob = tf.placeholder("float")
        state_drop = tf.nn.dropout(state, input_keep_prob)

        # possible dropout inside
        keep_prob_encoding = tf.placeholder("float")
        keep_prob_lstm = tf.placeholder("float")

        # unwrap
        x_2_o = []
        # first step
        x_2, reward_2, hidden_state = ring_net.encode_compress_decode(
            state[:, 0, :, :, :], action[:, 1, :], None, keep_prob_encoding,
            keep_prob_lstm)
        tf.get_variable_scope().reuse_variables()
        # unroll for 9 more steps
        for i in xrange(9):
            x_2, reward_2, hidden_state = ring_net.encode_compress_decode(
                state[:, i + 1, :, :, :], action[:, i + 2, :], hidden_state,
                keep_prob_encoding, keep_prob_lstm)
        # now collect values
        x_2_o.append(x_2)
        for i in xrange(4):
            x_2, reward_2, hidden_state = ring_net.encode_compress_decode(
                x_2, action[:, i + 11, :], hidden_state, keep_prob_encoding,
                keep_prob_lstm)
            x_2_o.append(x_2)
            tf.image_summary('images_gen_' + str(i), x_2)
        x_2_o = tf.pack(x_2_o)
        x_2_o = tf.transpose(x_2_o, perm=[1, 0, 2, 3, 4])

        # error
        error = tf.nn.l2_loss(state[:, 10:15, :, :, :] - x_2_o)
        tf.scalar_summary('loss', error)

        # train (hopefuly)
        train_op = ring_net.train(error, 1e-5)

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

        # Build a saver
        saver = tf.train.Saver(tf.all_variables(), max_to_keep=1)

        # Summary op
        summary_op = tf.merge_all_summaries()

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

        # init from seq 1 model
        print("init from " + RESTORE_DIR)
        saver_restore = tf.train.Saver(variables)
        ckpt = tf.train.get_checkpoint_state(RESTORE_DIR)
        saver_restore.restore(sess, ckpt.model_checkpoint_path)

        # 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.train.SummaryWriter(SAVE_DIR, graph_def=graph_def)

        for step in xrange(500000):
            t = time.time()
            _, loss_value = sess.run([train_op, error],
                                     feed_dict={
                                         keep_prob_encoding: 1.0,
                                         keep_prob_lstm: 1.0,
                                         input_keep_prob: 1.0
                                     })
            elapsed = time.time() - t

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

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

            if step % 1000 == 0:
                checkpoint_path = os.path.join(SAVE_DIR, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)
                print("saved to " + SAVE_DIR)
Example #11
0
def train(iteration):
  """Train ring_net for a number of steps."""
  with tf.Graph().as_default():
    # make inputs
    state, reward, action = ring_net.inputs(CURRICULUM_BATCH_SIZE[iteration], CURRICULUM_SEQ[iteration]) 

    # possible input dropout 
    input_keep_prob = tf.placeholder("float")
    state_drop = tf.nn.dropout(state, input_keep_prob)

    # possible dropout inside
    keep_prob_encoding = tf.placeholder("float")
    keep_prob_lstm = tf.placeholder("float")

    # create and unrap network
    output_f, output_t, output_g, output_reward, output_autoencoder = ring_net.unwrap(state_drop, action, keep_prob_encoding, keep_prob_lstm, CURRICULUM_SEQ[iteration], CURRICULUM_TRAIN_PEICE[iteration])

    # calc error
    error = ring_net.loss(state, reward, output_f, output_t, output_g, output_reward, output_autoencoder, CURRICULUM_TRAIN_PEICE[iteration])
    error = tf.div(error, CURRICULUM_SEQ[iteration])

    # train (hopefuly)
    train_op = ring_net.train(error, CURRICULUM_LEARNING_RATE[iteration])
    
    # List of all Variables
    variables = tf.all_variables()
    #for i, variable in enumerate(variables):
    #  print '----------------------------------------------'
    #  print variable.name[:variable.name.index(':')]

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

    # Summary op
    summary_op = tf.merge_all_summaries()
 
    # Start running operations on the Graph.
    sess = tf.Session()

    # restore if iteration is not 0
    variables_to_restore = tf.all_variables()
    autoencoder_variables = [variable for i, variable in enumerate(variables_to_restore) if ("compress" not in variable.name[:variable.name.index(':')]) and ("RNN" not in variable.name[:variable.name.index(':')])]
    rnn_variables = [variable for i, variable in enumerate(variables_to_restore) if ("compress" in variable.name[:variable.name.index(':')]) or ("RNN" in variable.name[:variable.name.index(':')])]
    if iteration == 0: 
      ckpt = tf.train.get_checkpoint_state(LOAD_DIR)
    else:
      ckpt = tf.train.get_checkpoint_state(SAVE_DIR)
    autoencoder_saver = tf.train.Saver(autoencoder_variables)
    print("restoring autoencoder part of network form " + ckpt.model_checkpoint_path)
    autoencoder_saver.restore(sess, ckpt.model_checkpoint_path)
    print("restored file from " + ckpt.model_checkpoint_path)

    if iteration == 0:
      print("init compression part of network from scratch")
      rnn_init = tf.initialize_variables(rnn_variables)
      sess.run(rnn_init)
    else:
      rnn_saver = tf.train.Saver(rnn_variables)
      print("restoring compression part of network")
      rnn_saver.restore(sess, ckpt.model_checkpoint_path)
      print("restored file from " + ckpt.model_checkpoint_path)
        
    # 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.train.SummaryWriter(SAVE_DIR, graph_def=graph_def)

    for step in xrange(CURRICULUM_STEPS[iteration]):
      t = time.time()
      _ , loss_value = sess.run([train_op, error],feed_dict={keep_prob_encoding:1.0, keep_prob_lstm:1.0, input_keep_prob:.8})
      elapsed = time.time() - t

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

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

      if step%1000 == 0:
        checkpoint_path = os.path.join(SAVE_DIR, 'model.ckpt')
        saver.save(sess, checkpoint_path, global_step=step)  
        print("saved to " + SAVE_DIR)
Example #12
0
def evaluate():
    """ Eval the system"""
    with tf.Graph().as_default():
        # make inputs
        state_start, reward_start, action_start, = ring_net.inputs(1, 15)
        action_size = int(action_start.get_shape()[2])
        action = tf.placeholder(tf.float32, (1, action_size))

        # unwrap
        x_2_o = []
        # first step
        x_2, reward_2, hidden_state = ring_net.encode_compress_decode(
            state_start[:, 0, :, :, :], action_start[:, 1, :], None, 1.0, 1.0)
        tf.get_variable_scope().reuse_variables()
        # unroll for 9 more steps
        for i in xrange(9):
            x_2, reward_2, hidden_state = ring_net.encode_compress_decode(
                state_start[:, i + 1, :, :, :], action_start[:, i + 2, :],
                hidden_state, 1.0, 1.0)

        # rename output_t
        x_1 = x_2
        print(hidden_state)
        hidden_state_1 = hidden_state
        x_2, reward_2, hidden_state_2 = ring_net.encode_compress_decode(
            x_1, action, hidden_state_1, 1.0, 1.0)

        # restore network
        variables_to_restore = tf.all_variables()
        saver = tf.train.Saver(variables_to_restore)
        sess = tf.Session()
        ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
        #ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            print("restored file from " + ckpt.model_checkpoint_path)
        else:
            print("no chekcpoint file found from " + FLAGS.checkpoint_dir +
                  ", this is an error")

        # get frame
        tf.train.start_queue_runners(sess=sess)
        play_action = random_action(action_size)
        x_2_g, hidden_2_g = sess.run([x_1, hidden_state_1], feed_dict={})

        # Play!!!!
        for step in xrange(100):
            print(step)
            #time.sleep(.5)
            # calc generated frame from t
            play_action = random_action(action_size)
            x_2_g, hidden_2_g = sess.run([x_2, hidden_state_2],
                                         feed_dict={
                                             x_1: x_2_g,
                                             hidden_state_1: hidden_2_g,
                                             action: play_action
                                         })
            frame = np.uint8(np.minimum(np.maximum(0, x_2_g * 255.0), 255))
            frame = frame[0, :, :, :]
            video.write(frame)
            #frame = cv2.resize(frame, (500, 500))
            #cv2.imshow('frame', frame)
            #cv2.waitKey(0)
            #if cv2.waitKey(1) & 0xFF == ord('q'):
            #  break
        video.release()
        cv2.destroyAllWindows()
Example #13
0
def train(iteration):
  """Train ring_net for a number of steps."""
  with tf.Graph().as_default():
    # make dynamic system
    #if FLAGS.system == "cannon":
    #  k = cn.Cannon()
    # make inputs
    x = ring_net.inputs(CURRICULUM_BATCH_SIZE[iteration], CURRICULUM_SEQ[iteration]) 
    # possible input dropout 
    input_keep_prob = tf.placeholder("float")
    x_drop = tf.nn.dropout(x, input_keep_prob)
    # possible dropout inside
    keep_prob = tf.placeholder("float")
    # create and unrap network
    output_t, output_g, output_f = ring_net.unwrap(x_drop, keep_prob, CURRICULUM_SEQ[iteration]) 
    # calc error
    error = ring_net.loss(x, output_t, output_g, output_f)
    error = tf.div(error, CURRICULUM_SEQ[iteration])
    # train hopefuly 
    train_op = ring_net.train(error, CURRICULUM_LEARNING_RATE[iteration])
    
    # List of all Variables
    variables = tf.all_variables()

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

    # Summary op
    summary_op = tf.merge_all_summaries()
 
    # Build an initialization operation to run below.
    if iteration == 0:
      init = tf.initialize_all_variables()

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

    # init if this is the very time training
    if iteration == 0: 
      print("init network from scratch")
      sess.run(init)

    # restore if iteration is not 0
    if iteration != 0:
      variables_to_restore = tf.all_variables()
      autoencoder_variables = [variable for i, variable in enumerate(variables_to_restore) if "RNNCell" not in variable.name[:variable.name.index(':')]]
      rnn_variables = [variable for i, variable in enumerate(variables_to_restore) if "RNNCell" in variable.name[:variable.name.index(':')]]
     
      ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir + FLAGS.model + FLAGS.system)
      autoencoder_saver = tf.train.Saver(autoencoder_variables)
      print("restoring autoencoder part of network form " + ckpt.model_checkpoint_path)
      autoencoder_saver.restore(sess, ckpt.model_checkpoint_path)
      print("restored file from " + ckpt.model_checkpoint_path)

      if CURRICULUM_SEQ[iteration-1] == 1:
        print("init rnn part of network from scratch")
        rnn_init = tf.initialize_variables(rnn_variables)
        sess.run(rnn_init)
      else:
        rnn_saver = tf.train.Saver(rnn_variables)
        print("restoring rnn part of network")
        rnn_saver.restore(sess, ckpt.model_checkpoint_path)
        print("restored file from " + ckpt.model_checkpoint_path)
        
    # 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.train.SummaryWriter(FLAGS.train_dir + FLAGS.model + FLAGS.system, graph_def=graph_def)

    for step in xrange(CURRICULUM_STEPS[iteration]):
      t = time.time()
      _ , loss_value = sess.run([train_op, error],feed_dict={keep_prob:0.9, input_keep_prob:.8})
      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={keep_prob:0.9, input_keep_prob:.8})
        summary_writer.add_summary(summary_str, step) 

      if step%1000 == 0:
        checkpoint_path = os.path.join(FLAGS.train_dir + FLAGS.model + FLAGS.system, 'model.ckpt')
        saver.save(sess, checkpoint_path, global_step=step)  
        print("saved to " + FLAGS.train_dir + FLAGS.model + FLAGS.system)
        print("loss value at " + str(loss_value))
        print("time per batch is " + str(elapsed))