def mesh_loss(pred, placeholders, block_id): gt_pt = placeholders['labels'][:, :3] # gt points gt_nm = placeholders['labels'][:, 3:] # gt normals # edge in graph nod1 = tf.gather(pred, placeholders['edges'][block_id - 1][:, 0]) nod2 = tf.gather(pred, placeholders['edges'][block_id - 1][:, 1]) edge = tf.subtract(nod1, nod2) # edge length loss edge_length = tf.reduce_sum(tf.square(edge), 1) edge_loss = tf.reduce_mean(edge_length) * 350 # chamfer distance sample_pt = sample(pred, placeholders, block_id) sample_pred = tf.concat([pred, sample_pt], axis=0) dist1, idx1, dist2, idx2 = nn_distance(gt_pt, sample_pred) point_loss = (tf.reduce_mean(dist1) + 0.55 * tf.reduce_mean(dist2)) * 3000 # normal cosine loss normal = tf.gather(gt_nm, tf.squeeze(idx2, 0)) normal = tf.gather(normal, placeholders['edges'][block_id - 1][:, 0]) cosine = tf.abs(tf.reduce_sum(tf.multiply(unit(normal), unit(edge)), 1)) normal_loss = tf.reduce_mean(cosine) * 0.5 total_loss = point_loss + edge_loss + normal_loss return total_loss
def main(): args = execute() os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id) pred_file_list = os.path.join(args.test_results_path, args.p2mpp_experiment_name, '*.xyz') xyz_list_path = glob.glob(pred_file_list) xyz1 = tf.placeholder(tf.float32, shape=(None, 3)) xyz2 = tf.placeholder(tf.float32, shape=(None, 3)) dist1, idx1, dist2, idx2 = nn_distance(xyz1, xyz2) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True sess = tf.Session(config=config) #A f1-score will be calculated for each threshold. threshold = [0.00005, 0.00010, 0.00015, 0.00020] name = { '02828884': 'bench', '03001627': 'chair', '03636649': 'lamp', '03691459': 'speaker', '04090263': 'firearm', '04379243': 'table', '04530566': 'watercraft', '02691156': 'plane', '02933112': 'cabinet', '02958343': 'car', '03211117': 'monitor', '04256520': 'couch', '04401088': 'cellphone' } length = { '02828884': 0, '03001627': 0, '03636649': 0, '03691459': 0, '04090263': 0, '04379243': 0, '04530566': 0, '02691156': 0, '02933112': 0, '02958343': 0, '03211117': 0, '04256520': 0, '04401088': 0 } sum_pred = { '02828884': np.zeros(4), '03001627': np.zeros(4), '03636649': np.zeros(4), '03691459': np.zeros(4), '04090263': np.zeros(4), '04379243': np.zeros(4), '04530566': np.zeros(4), '02691156': np.zeros(4), '02933112': np.zeros(4), '02958343': np.zeros(4), '03211117': np.zeros(4), '04256520': np.zeros(4), '04401088': np.zeros(4) } index = 0 for pred_path in xyz_list_path: filename = os.path.basename(pred_path) #Ground_thruth contains an image of the reconstructed object in [0] and the point cloud and normals on [1] ground_truth_path = os.path.join(args.test_models_path, filename.replace(".xyz", ".dat")) ground = pickle.load(open(ground_truth_path, 'rb'), encoding='bytes')[1][:, :3] predict = np.loadtxt(pred_path) class_id = pred_path.split('/')[-1].split('_')[0] length[class_id] += 1.0 d1, i1, d2, i2 = sess.run([dist1, idx1, dist2, idx2], feed_dict={ xyz1: predict, xyz2: ground }) sum_pred[class_id] += f_score(predict, ground, d1, i1, d2, i2, threshold) index += 1 log_dir = os.path.join(args.misc_results_path, args.p2mpp_experiment_name) if not os.path.exists(log_dir): os.makedirs(log_dir) log_path = os.path.join(log_dir, 'f1_score.log') with open(log_path, 'a+') as log: for item in length: number = length[item] + 1e-6 score = sum_pred[item] / number log.write(", ".join( [item, name[item], str(length[item]), str(score), "\n"])) sess.close()
import numpy as np import tensorflow as tf import glob import pickle from modules.chamfer import nn_distance from modules.config import execute if __name__ == '__main__': print('=> set config') args = execute() os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id) xyz1 = tf.placeholder(tf.float32, shape=(None, 3)) xyz2 = tf.placeholder(tf.float32, shape=(None, 3)) dist1, idx1, dist2, idx2 = nn_distance(xyz1, xyz2) config = tf.ConfigProto() #pylint: disable=no-member config.gpu_options.allow_growth = True config.allow_soft_placement = True sess = tf.Session(config=config) pred_file_list = os.path.join(args.test_results_path, args.p2mpp_experiment_name, '*.xyz') xyz_list_path = glob.glob(pred_file_list) log_dir = os.path.join(args.misc_results_path, args.p2mpp_experiment_name) if not os.path.exists(log_dir): os.makedirs(log_dir)