def generate_stat_data(self, filename): eval_poses = helper.read_poses(FLAGS.data_dict, FLAGS.eval_poses) template_data = self.templates[self.template_idx,:,:].reshape((1,MAX_NUM_POINT,3)) TIME, ITR, Trans_Err, Rot_Err = [], [], [], [] for pose in eval_poses: source_data = helper.apply_transformation(self.templates[self.template_idx,:,:],pose.reshape((-1,6))) final_pose, _, _, _, _, elapsed_time, itr = self.test_one_case(source_data,template_data) translation_error, rotational_error = self.find_errors(pose.reshape((-1,6)), final_pose) TIME.append(elapsed_time) ITR.append(itr) Trans_Err.append(translation_error) Rot_Err.append(rotational_error) TIME_mean, ITR_mean, Trans_Err_mean, Rot_Err_mean = sum(TIME)/len(TIME), sum(ITR)/len(ITR), sum(Trans_Err)/len(Trans_Err), sum(Rot_Err)/len(Rot_Err) TIME_var, ITR_var, Trans_Err_var, Rot_Err_var = np.var(np.array(TIME)), np.var(np.array(ITR)), np.var(np.array(Trans_Err)), np.var(np.array(Rot_Err)) import csv with open(filename + '.csv','w') as csvfile: csvwriter = csv.writer(csvfile) for i in range(len(TIME)): csvwriter.writerow([i, TIME[i], ITR[i], Trans_Err[i], Rot_Err[i]]) with open(filename+'.txt','w') as file: file.write("Mean of Time: {}".format(TIME_mean)) file.write("Mean of Iterations: {}".format(ITR_mean)) file.write("Mean of Translation Error: {}".format(Trans_Err_mean)) file.write("Mean of Rotation Error: {}".format(Rot_Err_mean)) file.write("Variance in Time: {}".format(TIME_var)) file.write("Variance in Iterations: {}".format(ITR_var)) file.write("Variance in Translation Error: {}".format(Trans_Err_var)) file.write("Variance in Rotation Error: {}".format(Rot_Err_var))
def train(): if not torch.cuda.is_available(): args.device = 'cpu' args.device = torch.device(args.device) action = Action() model = action.create_model() model.to(args.device) model.cuda() min_loss = float('inf') learnable_params = filter(lambda p: p.requires_grad, model.parameters()) if args.optimizer == 'adam': optimizer = torch.optim.Adam(learnable_params) else: optimizer = torch.optim.SGD(learnable_params, lr=0.1) templates = helper.loadData(args.data_dict) poses = helper.read_poses(args.data_dict, args.train_poses) eval_poses = helper.read_poses(args.data_dict, args.eval_poses) print(templates.shape, poses.shape, eval_poses.shape) for epoch in range(args.max_epoch): log_string("############## EPOCH: %0.4d ##############" % epoch) train_loss = action.train_one_epoch(model, poses, templates, optimizer) eval_loss = action.eval_one_epoch(model, eval_poses, templates) log_string("Training Loss: {} and Evaluation Loss: {}".format( train_loss, eval_loss)) is_best = eval_loss < min_loss snap = { 'epoch': epoch + 1, 'model': model.state_dict(), 'min_loss': min_loss, 'optimizer': optimizer.state_dict() } if is_best: save_checkpoint(snap, os.path.join(LOG_DIR, 'model'), 'snap_best') save_checkpoint(model.state_dict(), os.path.join(LOG_DIR, 'model'), 'model_best') log_string("Best Evaluation Loss: {}".format(eval_loss)) save_checkpoint(snap, os.path.join(LOG_DIR, 'model'), 'snap_last') save_checkpoint(model.state_dict(), os.path.join(LOG_DIR, 'model'), 'model_last')
def train(): with tf.Graph().as_default(): with tf.device('/cpu:0'): batch = tf.Variable(0) # That tells the optimizer to helpfully increment the 'batch' parameter for you every time it trains. with tf.device('/gpu:'+str(GPU_INDEX)): is_training_pl = tf.placeholder(tf.bool, shape=()) # Flag for dropouts. bn_decay = get_bn_decay(batch) # Calculate BN decay. learning_rate = get_learning_rate(batch) # Calculate Learning Rate at each step. # Define a network to backpropagate the using final pose prediction. with tf.variable_scope('Network') as _: # Get the placeholders. source_pointclouds_pl, template_pointclouds_pl = MODEL.placeholder_inputs(BATCH_SIZE, NUM_POINT) # Extract Features. source_global_feature, template_global_feature = MODEL.get_model(source_pointclouds_pl, template_pointclouds_pl, is_training_pl, bn_decay=bn_decay,PN=POINTNET,POOL=FLAGS.pn_pool,out_features=out_features) # Find the predicted transformation. predicted_transformation = MODEL.get_pose(source_global_feature,template_global_feature,is_training_pl, bn_decay=bn_decay,lim_rot=FLAGS.lim_rot) # Find the loss using source and transformed template point cloud. # loss = MODEL.get_loss(predicted_transformation, BATCH_SIZE, template_pointclouds_pl, source_pointclouds_pl) loss = 0 with tf.device('/cpu:0'): # Add ops to save and restore all the variables. saver = tf.train.Saver() # Create a session config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True config.log_device_placement = False sess = tf.Session(config=config) # Init variables init = tf.global_variables_initializer() sess.run(init, {is_training_pl: True}) saver.restore(sess, FLAGS.model_path) # Create a dictionary to pass the tensors and placeholders in train and eval function for Network. ops = {'source_pointclouds_pl': source_pointclouds_pl, 'template_pointclouds_pl': template_pointclouds_pl, 'is_training_pl': is_training_pl, 'predicted_transformation': predicted_transformation, 'loss': loss, 'step': batch} templates = helper.loadData(FLAGS.data_dict) # pairs = helper.read_pairs(FLAGS.data_dict, FLAGS.pairs_file) eval_poses = helper.read_poses(FLAGS.data_dict, FLAGS.eval_poses) # Read all the poses data for evaluation. eval_network(sess, ops, templates, eval_poses)
def train(): with tf.Graph().as_default(): with tf.device('/cpu:0'): batch = tf.Variable(0) # That tells the optimizer to helpfully increment the 'batch' parameter for you every time it trains. with tf.device('/gpu:'+str(GPU_INDEX)): is_training_pl = tf.placeholder(tf.bool, shape=()) # Flag for dropouts. learning_rate = get_learning_rate(batch) # Calculate Learning Rate at each step. # Define a network to backpropagate the using final pose prediction. with tf.variable_scope('Network') as _: # Get the placeholders. source_pointclouds_pl, template_pointclouds_pl, transformation_pl, gt_transformation_pl = MODEL.placeholder_inputs(BATCH_SIZE, NUM_VOXEL) # Extract Features. source_global_feature, template_global_feature = MODEL.get_model(source_pointclouds_pl, template_pointclouds_pl, is_training_pl, bn_decay=None) # Find the predicted transformation. predicted_transformation = MODEL.get_pose(source_global_feature,template_global_feature,is_training_pl, bn_decay=None) # Find the loss using source and transformed template point cloud. loss = MODEL.get_loss_v1(predicted_transformation, transformation_pl, gt_transformation_pl, BATCH_SIZE) # Add the loss in tensorboard. # Get training optimization algorithm. if OPTIMIZER == 'momentum': optimizer = tf.train.MomentumOptimizer(learning_rate, momentum=MOMENTUM) elif OPTIMIZER == 'adam': optimizer = tf.train.AdamOptimizer(learning_rate) train_op = optimizer.minimize(loss, global_step=batch) with tf.device('/cpu:0'): # Add ops to save and restore all the variables. saver = tf.train.Saver() tf.summary.scalar('loss', loss) tf.summary.scalar('learning_rate', learning_rate) # Create a session config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True config.log_device_placement = False sess = tf.Session(config=config) # Add summary writers merged = tf.summary.merge_all() if FLAGS.mode == 'train': # Create summary writers only for train mode. train_writer = tf.summary.FileWriter(os.path.join(LOG_DIR, 'train'), sess.graph) eval_writer = tf.summary.FileWriter(os.path.join(LOG_DIR, 'eval')) # Init variables init = tf.global_variables_initializer() sess.run(init, {is_training_pl: True}) # Just to initialize weights with pretrained model. if FLAGS.use_pretrained_model: saver.restore(sess,os.path.join('log_512pts_1024feat_6itr_180deg_random_poses','model250.ckpt')) # Create a dictionary to pass the tensors and placeholders in train and eval function for Network. ops = {'source_pointclouds_pl': source_pointclouds_pl, 'template_pointclouds_pl': template_pointclouds_pl, 'transformation_pl': transformation_pl, 'gt_transformation_pl': gt_transformation_pl, 'is_training_pl': is_training_pl, 'predicted_transformation': predicted_transformation, 'loss': loss, 'train_op': train_op, 'merged': merged, 'step': batch} templates = helper.loadData(FLAGS.data_dict) poses = helper.read_poses(FLAGS.data_dict, TRAIN_POSES) # Read all the poses data for training. eval_poses = helper.read_poses(FLAGS.data_dict, EVAL_POSES) # Read all the poses data for evaluation. if FLAGS.mode == 'train': # For actual training. for epoch in range(MAX_EPOCH): log_string('**** EPOCH %03d ****' % (epoch)) sys.stdout.flush() # Train for all triaining poses. train_one_epoch(sess, ops, train_writer, templates, poses) save_path = saver.save(sess, os.path.join(LOG_DIR, FLAGS.results+".ckpt")) if epoch % 10 == 0: # Evaluate the trained network after 50 epochs. eval_one_epoch(sess, ops, eval_writer, templates, eval_poses) # Save the variables to disk. if epoch % 50 == 0: # Store the Trained weights in log directory. save_path = saver.save(sess, os.path.join(LOG_DIR, "models", "model"+str(epoch)+".ckpt")) log_string("Model saved in file: %s" % save_path)
def run(): NUM_POINT = FLAGS.num_point if not use_noise_data: templates = helper.loadData(FLAGS.data_dict) pairs = helper.read_pairs(FLAGS.data_dict, FLAGS.pairs_file) else: templates, sources = helper.read_noise_data(FLAGS.data_dict) # templates = helper.loadData(FLAGS.data_dict) eval_poses = helper.read_poses( FLAGS.data_dict, FLAGS.eval_poses) # Read all the poses data for evaluation. eval_poses = eval_poses[0:1, :] num_batches = eval_poses.shape[0] TIME, ITR, Trans_Err, Rot_Err = [], [], [], [] idxs_5_5, idxs_10_1, idxs_20_2 = [], [], [] counter = 0 for fn, gt_pose in enumerate(eval_poses): if fn > 0: break if not use_noise_data: # template_idx = pairs[fn,1] template_idx = 0 M_given = templates[template_idx, :, :] S_given = helper.apply_transformation(M_given.reshape((1, -1, 3)), gt_pose.reshape((1, 6)))[0] else: M_given = templates[fn, :, :] S_given = sources[fn, :, :] # M_given = np.loadtxt('template_car_itr.txt') # S_given = np.loadtxt('source_car_itr.txt') # helper.display_clouds_data(M_given) # helper.display_clouds_data(S_given) # To generate point cloud for Xueqian: For CAD model figures # gt_pose = np.array([[0.5,0.2,0.4,40*(np.pi/180),20*(np.pi/180),30*(np.pi/180)]]) # templates = helper.loadData('unseen_data') # gt_pose = np.array([[-0.3,-0.7,0.4,-34*(np.pi/180),31*(np.pi/180),-27*(np.pi/180)]]) # gt_pose = np.array([[0.5929,-0.0643,-0.961,0.4638,-0.3767,-0.6253]]) # M_given = templates[48,:,:] # S_given = helper.apply_transformation(M_given.reshape(1,-1,3),gt_pose) # S_given = helper.add_noise(S_given) # S_given = S_given[0] M_given = M_given[0:NUM_POINT, :] # template data S_given = S_given[0:NUM_POINT, :] # source data tree_M = KDTree(M_given) tree_M_sampled = KDTree(M_given[0:100, :]) final_pose, model_data, sensor_data, predicted_data, _, time_elapsed, itr = icp.icp_test( S_given[0:100, :], M_given, tree_M, M_given[0:100, :], tree_M_sampled, S_given, gt_pose.reshape((1, 6)), 100, FLAGS.threshold) translation_error, rotational_error = find_errors( gt_pose[0], final_pose[0]) print(translation_error, rotational_error) TIME.append(time_elapsed) ITR.append(itr) Trans_Err.append(translation_error) Rot_Err.append(rotational_error) if rotational_error < 20 and translation_error < 0.2: if rotational_error < 10 and translation_error < 0.1: if rotational_error < 5 and translation_error < 0.05: idxs_5_5.append(fn) idxs_10_1.append(fn) idxs_20_2.append(fn) print('Batch: {}, Iterations: {}, Time: {}'.format( counter, itr, time_elapsed)) # counter += 1 # helper.display_three_clouds(M_given, S_given, predicted_data, "") # np.savetxt('template_piano.txt',M_given) # np.savetxt('source_piano.txt',S_given) # np.savetxt('predicted_piano.txt',predicted_data) log = { 'TIME': TIME, 'ITR': ITR, 'Trans_Err': Trans_Err, 'Rot_Err': Rot_Err, 'idxs_5_5': idxs_5_5, 'idxs_10_1': idxs_10_1, 'idxs_20_2': idxs_20_2, 'num_batches': num_batches } helper.log_test_results(FLAGS.log_dir, FLAGS.filename, log)
def train(): graph = tf.Graph() with graph.as_default(): with tf.device('/cpu:0'): batch = tf.Variable( 0 ) # That tells the optimizer to helpfully increment the 'batch' parameter for you every time it trains. with tf.device('/gpu:' + str(GPU_INDEX)): is_training_pl = tf.placeholder(tf.bool, shape=()) # Flag for dropouts. learning_rate = get_learning_rate( batch) # Calculate Learning Rate at each step. # Define a network to backpropagate the using final pose prediction. with tf.variable_scope('Network') as _: # Get the placeholders. source_pointclouds_pl, template_pointclouds_pl = MODEL.placeholder_inputs( BATCH_SIZE, NUM_POINT) # Extract Features. source_global_feature, template_global_feature = MODEL.get_model( source_pointclouds_pl, template_pointclouds_pl, is_training_pl, bn_decay=None, PN=POINTNET, POOL=FLAGS.pn_pool, out_features=out_features) # Find the predicted transformation. predicted_transformation = MODEL.get_pose( source_global_feature, template_global_feature, is_training_pl, bn_decay=None, lim_rot=FLAGS.lim_rot) # Find the loss using source and transformed template point cloud. # loss = MODEL.get_loss(predicted_transformation, BATCH_SIZE, template_pointclouds_pl, source_pointclouds_pl) # Add the loss in tensorboard. # loss - ours: batch_size = BATCH_SIZE predicted_position = tf.slice(predicted_transformation, [0, 0], [batch_size, 3]) predicted_quat = tf.slice(predicted_transformation, [0, 3], [batch_size, 4]) norm_predicted_quat = tf.reduce_sum(tf.square(predicted_quat), 1) norm_predicted_quat = tf.sqrt(norm_predicted_quat) norm_predicted_quat = tf.reshape(norm_predicted_quat, (batch_size, 1)) const = tf.constant(0.0000001, shape=(batch_size, 1), dtype=tf.float32) norm_predicted_quat = tf.add(norm_predicted_quat, const) predicted_norm_quat = tf.divide(predicted_quat, norm_predicted_quat) transformed_predicted_point_cloud = helper.transformation_quat_tensor( source_pointclouds_pl, predicted_norm_quat, predicted_position) is_training_pl_1 = tf.placeholder(tf.bool, shape=()) # flag for batch norm # load our model as loss function: saver31 = tf.train.import_meta_graph( os.path.join(LOG_DIR, 'model.ckpt.meta'), import_scope='g1', input_map={ 'input1': transformed_predicted_point_cloud, 'input2': template_pointclouds_pl, 'Placeholder': is_training_pl_1 }, clear_devices=True) labels12 = graph.get_tensor_by_name('g1/labels12:0') if ADD_NOISE_MODEL: add_noise = graph.get_tensor_by_name('g1/add_noise:0') # set optimizer: if OPTIMIZER == 'momentum': optimizer3 = tf.train.MomentumOptimizer(learning_rate, momentum=MOMENTUM) elif OPTIMIZER == 'adam': optimizer3 = tf.train.AdamOptimizer(learning_rate, name='Adam2') # optimizer only on g2 variables. # get loss: # pred_AB3 = (graph.get_tensor_by_name('g1/pc_compare/output1:0') + 1) / 2.0 # pred_BA3 = (graph.get_tensor_by_name('g1/pc_compare/output2:0') + 1) / 2.0 pred_AB3 = (graph.get_tensor_by_name('g1/pc_compare/output1:0')) pred_BA3 = (graph.get_tensor_by_name('g1/pc_compare/output2:0')) loss = (tf.reduce_mean(pred_AB3[:, :, :, 0]) + tf.reduce_mean(pred_BA3[:, :, :, 0])) / 2.0 loss_c = chmafer_dist(transformed_predicted_point_cloud, template_pointclouds_pl) train_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='Network') grads = optimizer3.compute_gradients(loss, train_vars) train_op = optimizer3.apply_gradients(grads, global_step=batch) # train_op = optimizer.minimize(loss, global_step=batch) with tf.device('/cpu:0'): # Add ops to save and restore all the variables. saver = tf.train.Saver() sum_lr = tf.summary.scalar('learning_rate', learning_rate) sum_loss = tf.summary.scalar('loss', loss) chamfer_loss = tf.summary.scalar('loss_chamfer', loss_c) # Create a session config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True config.log_device_placement = False sess = tf.Session(config=config) # Add summary writers # merged = tf.summary.merge_all() merged = tf.summary.merge([sum_loss, sum_lr, chamfer_loss]) if FLAGS.mode == 'train': # Create summary writers only for train mode. train_writer = tf.summary.FileWriter( os.path.join(LOG_DIR, 'train'), sess.graph) eval_writer = tf.summary.FileWriter(os.path.join(LOG_DIR, 'eval')) # Init variables init = tf.global_variables_initializer() # sess.run(init, {is_training_pl: True}) MODEL_PATH1 = os.path.join(LOG_DIR, 'model.ckpt') saver31.restore(sess, MODEL_PATH1) initialize_uninitialized_vars(sess) print('models restored') # Just to initialize weights with pretrained model. if FLAGS.use_pretrained_model: saver.restore( sess, os.path.join('log_512pts_1024feat_6itr_180deg_random_poses', 'model250.ckpt')) if ADD_NOISE_MODEL: # Create a dictionary to pass the tensors and placeholders in train and eval function for Network. ops = { 'source_pointclouds_pl': source_pointclouds_pl, 'template_pointclouds_pl': template_pointclouds_pl, 'is_training_pl': is_training_pl, 'predicted_transformation': predicted_transformation, 'loss': loss, 'train_op': train_op, 'merged': merged, 'step': batch, 'labels12': labels12, 'is_training_pl_1': is_training_pl_1, 'add_noise': add_noise } else: # Create a dictionary to pass the tensors and placeholders in train and eval function for Network. ops = { 'source_pointclouds_pl': source_pointclouds_pl, 'template_pointclouds_pl': template_pointclouds_pl, 'is_training_pl': is_training_pl, 'predicted_transformation': predicted_transformation, 'loss': loss, 'train_op': train_op, 'merged': merged, 'step': batch, 'labels12': labels12, 'is_training_pl_1': is_training_pl_1 } templates = helper.loadData(FLAGS.data_dict) poses = helper.read_poses( FLAGS.data_dict, TRAIN_POSES) # Read all the poses data for training. eval_poses = helper.read_poses( FLAGS.data_dict, EVAL_POSES) # Read all the poses data for evaluation. if FLAGS.mode == 'train': print_('Training Started!', color='r', style='bold') # For actual training. for epoch in range(MAX_EPOCH): log_string('**** EPOCH %03d ****' % (epoch)) sys.stdout.flush() # Train for all triaining poses. train_one_epoch(sess, ops, train_writer, templates, poses) save_path = saver.save( sess, os.path.join(LOG_DIR, FLAGS.results + ".ckpt")) if epoch % 10 == 0: # Evaluate the trained network after 50 epochs. eval_one_epoch(sess, ops, eval_writer, templates, eval_poses) # Save the variables to disk. if epoch % 50 == 0: # Store the Trained weights in log directory. save_path = saver.save( sess, os.path.join(LOG_DIR, "models", "model" + str(epoch) + ".ckpt")) log_string("Model saved in file: %s" % save_path) print_('Training Successful!!', color='r', style='bold')
def train(): with tf.Graph().as_default(): with tf.device('/cpu:0'): batch = tf.Variable( 0 ) # That tells the optimizer to helpfully increment the 'batch' parameter for you every time it trains. with tf.device('/gpu:' + str(GPU_INDEX)): is_training_pl = tf.placeholder(tf.bool, shape=()) # Flag for dropouts. learning_rate = get_learning_rate( batch) # Calculate Learning Rate at each step. # Define a network to backpropagate the using final pose prediction. with tf.variable_scope('Network_L') as _: # Object of network class. network_L = MODEL.Network() # Get the placeholders. source_pointclouds_pl_L, template_pointclouds_pl_L = network_L.placeholder_inputs( BATCH_SIZE, NUM_POINT) # Extract Features. source_global_feature_L, template_global_feature_L = network_L.get_model( source_pointclouds_pl_L, template_pointclouds_pl_L, FLAGS.feature_size, is_training_pl, bn_decay=None) # Find the predicted transformation. predicted_transformation_L = network_L.get_pose( source_global_feature_L, template_global_feature_L, is_training_pl, bn_decay=None) loss = 0 with tf.device('/cpu:0'): # Add ops to save and restore all the variables. saver = tf.train.Saver() # Create a session config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True config.log_device_placement = False sess = tf.Session(config=config) # Init variables init = tf.global_variables_initializer() sess.run(init, {is_training_pl: True}) # Just to initialize weights with pretrained model. saver.restore(sess, FLAGS.model_path) # Create a dictionary to pass the tensors and placeholders in train and eval function for Network_L. ops_L = { 'source_pointclouds_pl': source_pointclouds_pl_L, 'template_pointclouds_pl': template_pointclouds_pl_L, 'is_training_pl': is_training_pl, 'predicted_transformation': predicted_transformation_L, 'loss': loss, 'step': batch } #templates = helper.process_templates(FLAGS.data_dict) # Read all the templates. templates = helper.loadData(FLAGS.data_dict) eval_poses = helper.read_poses( FLAGS.data_dict, FLAGS.eval_poses) # Read all the poses data for evaluation. eval_poses = eval_poses[0:10, :] eval_network(sess, ops_L, templates, eval_poses)