def get_modified_cd_loss(pred, gt, forward_weight=1.0, threshold=None): """ pred: BxNxC, label: BxN, forward_weight: relative weight for forward_distance """ with tf.name_scope("cd_loss"): dists_forward, _, dists_backward, _ = tf_nndistance.nn_distance( gt, pred) if threshold is not None: forward_threshold = tf.reduce_mean( dists_forward, keepdims=True, axis=1) * threshold backward_threshold = tf.reduce_mean( dists_backward, keepdims=True, axis=1) * threshold # only care about distance within threshold (ignore strong outliers) dists_forward = tf.where(dists_forward < forward_threshold, dists_forward, tf.zeros_like(dists_forward)) dists_backward = tf.where(dists_backward < backward_threshold, dists_backward, tf.zeros_like(dists_backward)) # dists_forward is for each element in gt, the closest distance to this element dists_forward = tf.reduce_mean(dists_forward, axis=1) dists_backward = tf.reduce_mean(dists_backward, axis=1) CD_dist = forward_weight * dists_forward + dists_backward # CD_dist_norm = CD_dist/radius cd_loss = tf.reduce_mean(CD_dist) return cd_loss
def create_loss(self, coarse, fine, gt, alpha): gt_ds = gt[:, :coarse.shape[1], :] loss_coarse = 10 * earth_mover(coarse[:, :, 0:3], gt_ds[:, :, 0:3]) _, retb, _, retd = tf_nndistance.nn_distance(coarse[:, :, 0:3], gt_ds[:, :, 0:3]) for i in range(np.shape(gt_ds)[0]): index = tf.expand_dims(retb[i], -1) sem_feat = tf.nn.softmax(coarse[i, :, 3:], -1) sem_gt = tf.cast( tf.one_hot( tf.gather_nd(tf.cast(gt_ds[i, :, 3] * 80 * 12, tf.int32), index), 12), tf.float32) loss_sem_coarse = tf.reduce_mean(-tf.reduce_sum( 0.9 * sem_gt * tf.log(1e-6 + sem_feat) + (1 - 0.9) * (1 - sem_gt) * tf.log(1e-6 + 1 - sem_feat), [1])) loss_coarse += loss_sem_coarse add_train_summary('train/coarse_loss', loss_coarse) update_coarse = add_valid_summary('valid/coarse_loss', loss_coarse) loss_fine = 10 * chamfer(fine[:, :, 0:3], gt[:, :, 0:3]) _, retb, _, retd = tf_nndistance.nn_distance(fine[:, :, 0:3], gt[:, :, 0:3]) for i in range(np.shape(gt)[0]): index = tf.expand_dims(retb[i], -1) sem_feat = tf.nn.softmax(fine[i, :, 3:], -1) sem_gt = tf.cast( tf.one_hot( tf.gather_nd(tf.cast(gt[i, :, 3] * 80 * 12, tf.int32), index), 12), tf.float32) loss_sem_fine = tf.reduce_mean(-tf.reduce_sum( 0.9 * sem_gt * tf.log(1e-6 + sem_feat) + (1 - 0.9) * (1 - sem_gt) * tf.log(1e-6 + 1 - sem_feat), [1])) loss_fine += loss_sem_fine add_train_summary('train/fine_loss', loss_fine) update_fine = add_valid_summary('valid/fine_loss', loss_fine) loss = loss_coarse + alpha * loss_fine add_train_summary('train/loss', loss) update_loss = add_valid_summary('valid/loss', loss) return loss, [update_coarse, update_fine, update_loss]
def chamfer(pcd1, pcd2): dist1, _, dist2, _ = tf_nndistance.nn_distance(pcd1, pcd2) dist1 = tf.reduce_mean(tf.sqrt(dist1)) dist2 = tf.reduce_mean(tf.sqrt(dist2)) return (dist1 + dist2) / 2
while (True): line = f.readline().strip() if not line: break namelist.append(line) # eval_path eval_path = '../result/result_shapenet_ply_out_smooth_pt2466/' # Initialize session # xyz1:dataset_points * 3, xyz2:query_points * 3 xyz1 = tf.placeholder(tf.float32, shape=(None, 3)) xyz2 = tf.placeholder(tf.float32, shape=(None, 3)) xyz3 = tf.expand_dims(xyz1, 0) xyz4 = tf.expand_dims(xyz2, 0) # chamfer distance dist1, idx1, dist2, idx2 = tf_nndistance.nn_distance(xyz3, xyz4) # earth mover distance, notice that emd_dist return the sum of all distance match = tf_approxmatch.approx_match(xyz3, xyz4) emd_dist = tf_approxmatch.match_cost(xyz3, xyz4, match) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) ### class_name = { '02828884': 'bench', '03001627': 'chair', '03636649': 'lamp',
def chamfer_distance(pcd1, pcd2): dist1, _, dist2, _ = tf_nndistance.nn_distance(pcd1, pcd2) return dist1, dist2
def chamfer(pcd1, pcd2): """Normalised Chamfer Distance""" dist1, _, dist2, _ = tf_nndistance.nn_distance(pcd1, pcd2) dist1 = tf.reduce_mean(tf.sqrt(dist1)) dist2 = tf.reduce_mean(tf.sqrt(dist2)) return (dist1 + dist2) / 2
def dist_to_nearest(comp, gt): dist1, _, dist2, _ = tf_nndistance.nn_distance(comp, gt) dist1 = tf.sqrt(dist1) dist2 = tf.sqrt(dist2) return dist1, dist2
def chamfer(pcd1, pcd2): dist1, _, dist2, _ = tf_nndistance.nn_distance(pcd1, pcd2) mdist1 = tf.reduce_mean(dist1) mdist2 = tf.reduce_mean(dist2) return dist1, dist2, (mdist1 + mdist2)