Пример #1
0
def main():
  images = tf.placeholder(tf.float32, [batch_size, 224, 224, 3])
  poses_x = tf.placeholder(tf.float32, [batch_size, 3])
  poses_q = tf.placeholder(tf.float32, [batch_size, 4])
  datasource = get_data()

  net = PoseNet({'data': images})

  p1_x = net.layers['cls1_fc_pose_xyz']
  p1_q = net.layers['cls1_fc_pose_wpqr']
  p2_x = net.layers['cls2_fc_pose_xyz']
  p2_q = net.layers['cls2_fc_pose_wpqr']
  p3_x = net.layers['cls3_fc_pose_xyz']
  p3_q = net.layers['cls3_fc_pose_wpqr']

  l1_x = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p1_x, poses_x)))) * 0.3
  l1_q = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p1_q, poses_q)))) * 150
  l2_x = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p2_x, poses_x)))) * 0.3
  l2_q = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p2_q, poses_q)))) * 150
  l3_x = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p3_x, poses_x)))) * 1
  l3_q = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p3_q, poses_q)))) * 500

  loss = l1_x + l1_q + l2_x + l2_q + l3_x + l3_q
  opt = tf.train.AdamOptimizer(learning_rate=0.0001, beta1=0.9, beta2=0.999, epsilon=0.00000001, use_locking=False, name='Adam').minimize(loss)

  # Set GPU options
  gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6833)

  init = tf.global_variables_initializer()
  saver = tf.train.Saver()
  outputFile = "/home/prasenjit/posenet/trainingresult/PoseNet.ckpt"

  with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
    # Load the data
    sess.run(init)
    net.load('posenet.npy', sess)

    data_gen = gen_data_batch(datasource)
    for i in range(max_iterations):
      np_images, np_poses_x, np_poses_q = next(data_gen)
      feed = {images: np_images, poses_x: np_poses_x, poses_q: np_poses_q}

      sess.run(opt, feed_dict=feed)
      np_loss = sess.run(loss, feed_dict=feed)
      if i % 20 == 0:
        print("iteration: " + str(i) + "\n\t" + "Loss is: " + str(np_loss))
      if i % 5000 == 0:
        saver.save(sess, outputFile)
        print("Intermediate file saved at: " + outputFile)
    saver.save(sess, outputFile)
    print("Intermediate file saved at: " + outputFile)
Пример #2
0
def main():
    today = datetime.date.today()
    formatted_today = today.strftime('%y%m%d')
    outputFoldPath = path + formatted_today + '/'
    folder = os.path.exists(outputFoldPath)
    if not folder:
        os.makedirs(outputFoldPath)
    curFoldPath = os.getcwd()
    formatted_today_1 = today.strftime('%m%d')
    resFile = curFoldPath + '/' + formatted_today_1
    #	print curFoldPath

    images = tf.placeholder(tf.float32, [batch_size, 224, 224, 3])
    poses_x = tf.placeholder(tf.float32, [batch_size, 3])
    poses_q = tf.placeholder(tf.float32, [batch_size, 4])
    datasource = get_data()

    net = PoseNet({'data': images})

    #	p1_x = net.layers['cls1_fc_pose_xyz']
    p1_q = net.layers['cls1_fc_pose_wpqr']
    #	p2_x = net.layers['cls2_fc_pose_xyz']
    p2_q = net.layers['cls2_fc_pose_wpqr']
    #	p3_x = net.layers['cls3_fc_pose_xyz']
    p3_q = net.layers['cls3_fc_pose_wpqr']

    #	l1_x = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p1_x, poses_x)))) * 0.3
    l1_q = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p1_q, poses_q)))) * 0.3
    #	l2_x = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p2_x, poses_x)))) * 0.3
    l2_q = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p2_q, poses_q)))) * 0.3
    #	l3_x = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p3_x, poses_x)))) * 1
    l3_q = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p3_q, poses_q))))

    #	loss = l1_x + l1_q + l2_x + l2_q + l3_x + l3_q
    loss = l1_q + l2_q + l3_q
    opt = tf.train.AdamOptimizer(learning_rate=0.0001,
                                 beta1=0.9,
                                 beta2=0.999,
                                 epsilon=0.00000001,
                                 use_locking=False,
                                 name='Adam').minimize(loss)

    # Set GPU options
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6833)

    init = tf.global_variables_initializer()
    saver = tf.train.Saver()
    #outputFile = path+'PoseNet.ckpt'

    with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
        # Load the data
        sess.run(init)
        net.load(path + 'posenet.npy', sess)

        resfo = open(resFile, 'w')
        resfo.write('load initial weight file:' + path + 'posenet.npy\n')
        resfo.close()

        data_gen = gen_data_batch(datasource)
        for i in range(max_iterations):
            np_images, np_poses_x, np_poses_q = next(data_gen)
            feed = {
                images: np_images,
                poses_x: np_poses_x,
                poses_q: np_poses_q
            }

            sess.run(opt, feed_dict=feed)
            np_loss = sess.run(loss, feed_dict=feed)

            #			outputFile = outputFoldPath + 'PoseNet_' + str(i) + '.ckpt'
            time_str = time.strftime('%m-%d %H:%M:%S',
                                     time.localtime(time.time()))
            if i % 20 == 0:
                resfo = open(resFile, 'a')
                print(time_str + "\titeration: " + str(i) + "\n\t" +
                      "Loss is: " + str(np_loss))
                resfo.write(time_str + "\titeration: " + str(i) + "\n\t" +
                            "Loss is: " + str(np_loss) + '\n')
                resfo.close()
            if i > 0 and i % 5000 == 0:
                resfo = open(resFile, 'a')
                outputFile = outputFoldPath + 'PoseNet_' + str(i) + '.ckpt'
                saver.save(sess, outputFile)
                print("Intermediate file saved at: " + outputFile)
                resfo.write("Intermediate file saved at: " + outputFile + '\n')
                resfo.close()

        saver.save(sess, outputFile)
        print("Intermediate file saved at: " + outputFile)
Пример #3
0
def train():
    batch_size = 75
    max_iterations = 3000

    images = tf.placeholder(tf.float32, [batch_size, 224, 224, 3])
    poses_x = tf.placeholder(tf.float32, [batch_size, 3])
    poses_q = tf.placeholder(tf.float32, [batch_size, 4])
    datasource = utils.get_data("train")

    net = PoseNet({'data': images})

    p1_x = net.layers['cls1_fc_pose_xyz']
    p1_q = net.layers['cls1_fc_pose_wpqr']
    p2_x = net.layers['cls2_fc_pose_xyz']
    p2_q = net.layers['cls2_fc_pose_wpqr']
    p3_x = net.layers['cls3_fc_pose_xyz']
    p3_q = net.layers['cls3_fc_pose_wpqr']

    l1_x = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p1_x, poses_x)))) * 0.3
    l1_q = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p1_q, poses_q)))) * 150
    l2_x = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p2_x, poses_x)))) * 0.3
    l2_q = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p2_q, poses_q)))) * 150
    l3_x = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p3_x, poses_x)))) * 1
    l3_q = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p3_q, poses_q)))) * 500

    loss = l1_x + l1_q + l2_x + l2_q + l3_x + l3_q
    opt = tf.train.AdamOptimizer(learning_rate=0.0001,
                                 beta1=0.9,
                                 beta2=0.999,
                                 epsilon=0.00000001,
                                 use_locking=False,
                                 name='Adam').minimize(loss)

    # ---- create a summary to monitor cost tensor
    tf.summary.scalar("loss", loss)
    merged_summary_op = tf.summary.merge_all(
    )  # merge all summaries into a single op
    logs_path = './logs'  # op to write logs to Tensorboard
    summary_writer = tf.summary.FileWriter(logs_path,
                                           graph=tf.get_default_graph())
    print("Run the command line: --> tensorboard --logdir=./logs " \
      "\nThen open http://0.0.0.0:6006/ into your web browser")

    # ---- Set GPU options
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6833)

    init = tf.global_variables_initializer()
    saver = tf.train.Saver()

    with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
        # Load the data
        sess.run(init)
        net.load('posenet.npy', sess)

        data_gen = utils.gen_data_batch(datasource, batch_size)
        for i in range(max_iterations):
            np_images, np_poses_x, np_poses_q = next(data_gen)
            feed = {
                images: np_images,
                poses_x: np_poses_x,
                poses_q: np_poses_q
            }

            sess.run(opt, feed_dict=feed)  # run the optimizer
            np_loss = sess.run(loss, feed_dict=feed)  #get the loss

            # ---- print the logs
            if i % 20 == 0:
                print("iteration: " + str(i) + "\n\t" + "Loss is: " +
                      str(np_loss))
            if i % 100 == 0:
                saver.save(sess, path_ckpt)
                print("Intermediate file saved at: " + path_ckpt)

            # ---- write logs at every iteration
            summary = merged_summary_op.eval(feed_dict=feed)
            summary_writer.add_summary(summary, i)

        saver.save(sess, path_ckpt)
        print("Intermediate file saved at: " + path_ckpt)
Пример #4
0
def main():
    random.seed(27)

    images = tf.placeholder(tf.float32, [batch_size, 224, 224, 3])
    poses_x = tf.placeholder(tf.float32, [batch_size, 3])
    poses_q = tf.placeholder(tf.float32, [batch_size, 4])

    net = PoseNet({'data': images})

    p1_x = net.layers['cls1_fc_pose_xyz']
    p1_q = net.layers['cls1_fc_pose_wpqr']
    p2_x = net.layers['cls2_fc_pose_xyz']
    p2_q = net.layers['cls2_fc_pose_wpqr']
    p3_x = net.layers['cls3_fc_pose_xyz']
    p3_q = net.layers['cls3_fc_pose_wpqr']

    l1_x = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p1_x, poses_x)))) * 0.3
    l1_q = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p1_q, poses_q)))) * 225
    l2_x = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p2_x, poses_x)))) * 0.3
    l2_q = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p2_q, poses_q)))) * 225
    l3_x = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p3_x, poses_x)))) * 1
    l3_q = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(p3_q, poses_q)))) * 500

    WEIGHT_DECAY_FACTOR = 0.005

    # Create your variables
    #weights = tf.get_variable('weights', collections=['variables'])

    with tf.variable_scope('weights_norm') as scope:
        weights_norm = tf.reduce_sum(
            input_tensor=WEIGHT_DECAY_FACTOR *
            tf.stack([tf.nn.l2_loss(i) for i in tf.get_collection('weights')]),
            name='weights_norm')

    # Add the weight decay loss to another collection called losses
    #tf.add_to_collection('losses', weights_norm)

    # To calculate your total loss
    #tf.add_n(tf.get_collection('losses'), name='total_loss')

    loss = l1_x + l1_q + l2_x + l2_q + l3_x + l3_q
    losses = loss + weights_norm
    opt = tf.train.AdamOptimizer(learning_rate=0.0001,
                                 beta1=0.9,
                                 beta2=0.999,
                                 epsilon=0.00000001,
                                 use_locking=False,
                                 name='Adam').minimize(losses)

    # Set GPU options
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6833)

    init = tf.global_variables_initializer()
    saver = tf.train.Saver()
    outputFile = directory + "PoseNet.ckpt"

    #f = open('D:/PythonWorkSpace/posenet/PoseNet_AllDatasets_Closer/practiceCurves.txt','w')

    with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
        # Load the data
        sess.run(init)
        net.load('.../weights/posenet.npy', sess)
        #saver.restore(sess, directory + "PoseNet.ckpt")

        datasource = get_data(directory + dataset)
        datasource_test = get_data(directory + dataset_test)

        data_gen = gen_data_batch(datasource)
        data_gen_test = gen_data_batch(datasource_test)

        for i in range(max_iterations):
            np_images, np_poses_x, np_poses_q = next(data_gen)
            feed = {
                images: np_images,
                poses_x: np_poses_x,
                poses_q: np_poses_q
            }

            sess.run(opt, feed_dict=feed)

            if i % 500 == 0:
                np_loss = sess.run(loss, feed_dict=feed)

                np_images_test, np_poses_x_test, np_poses_q_test = next(
                    data_gen_test)
                feed_test = {
                    images: np_images_test,
                    poses_x: np_poses_x_test,
                    poses_q: np_poses_q_test
                }

                np_loss_test = sess.run(loss, feed_dict=feed_test)

                print("iteration: " + str(i) + "\n\t" + "Loss is: " +
                      str(np_loss) + "\n\t" + "test Loss is: " +
                      str(np_loss_test))
                f = open(directory + 'practiceCurves.txt', 'a')
                f.write(
                    str(i) + " " + str(np_loss) + " " + str(np_loss_test) +
                    "\n")
                f.close()
            if i % 2000 == 0:
                saver.save(sess, outputFile, global_step=i)
                print("Intermediate file saved at: " + outputFile)
        saver.save(sess, outputFile)
        print("Intermediate file saved at: " + outputFile)