def alpha_from_angle(y_reco, y_true): zep, zet, azp, azt = y_reco[:, 1], y_true[:, 1], y_reco[:, 2], y_true[:, 2] cosalpha = abs(sin(zep)) * cos(azp) * sin(zet) * cos(azt) + abs( sin(zep)) * sin(azp) * sin(zet) * sin(azt) + cos(zep) * cos(zet) cosalpha -= tf.math.sign(cosalpha) * eps alpha = acos(cosalpha) return alpha
def energy_angle(y_reco, y_true): energy_metric = tfp.stats.percentile(tf.math.abs(tf.subtract(y_true[:, 0], y_reco[:, 0])), [50-34, 50, 50+34]) #for comparison classic_stat=tfp.stats.percentile(tf.subtract(y_true[:, 0], y_reco[:, 0]), [25, 75]) w_energy=tf.subtract(classic_stat[1],classic_stat[0])/1.349 alpha= acos(cos_angle(y_reco, y_true)) angle_resi = 180 / np.pi * alpha #degrees angle_metric = tfp.stats.percentile(angle_resi, [50-34,50,50+34]) w_angle = tfp.stats.percentile(angle_resi, [68]) return energy_metric.numpy(), angle_metric.numpy(), [float(w_energy), float(w_angle)]
def compute_angle_tensor(pts1, pts2): """ Compute the angle between pt1 and pt2 with respect to the origin Input: pts1: batch_size x 1 x 3 tensor pts2: batch_size x 1 x 3 tensor """ b = tf.constant([0.,0.,0.]) angle_diff = [] for pt1, pt2 in zip(pts1, pts2): ba = tf.subtract(pt1, b) bc = tf.subtract(pt2, b) cosine_angle = tm.divide(tf.tensordot(ba, bc, 1), tm.multiply(tf.norm(ba), tf.norm(bc))) angle = tm.acos(cosine_angle) angle_diff.append(tf.cast(angle, tf.float32)) return tf.stack(angle_diff, axis=0)
def angle_loss_fn_batch(y_true, y_pred): x_p = math.sin(y_pred[:, 0]) * math.cos(y_pred[:, 1]) y_p = math.sin(y_pred[:, 0]) * math.sin(y_pred[:, 1]) z_p = math.cos(y_pred[:, 0]) x_t = math.sin(y_true[:, 0]) * math.cos(y_true[:, 1]) y_t = math.sin(y_true[:, 0]) * math.sin(y_true[:, 1]) z_t = math.cos(y_true[:, 0]) norm_p = math.sqrt(x_p * x_p + y_p * y_p + z_p * z_p) norm_t = math.sqrt(x_t * x_t + y_t * y_t + z_t * z_t) dot_pt = x_p * x_t + y_p * y_t + z_p * z_t angle_value = dot_pt / (norm_p * norm_t) angle_value = tf.clip_by_value(angle_value, -0.99999, 0.99999) loss_val = (math.acos(angle_value)) return loss_val
def angle_loss_fn(self, y_true, y_pred): x_p = math.sin(y_pred[:, 0]) * math.cos(y_pred[:, 1]) y_p = math.sin(y_pred[:, 0]) * math.sin(y_pred[:, 1]) z_p = math.cos(y_pred[:, 0]) x_t = math.sin(y_true[:, 0]) * math.cos(y_true[:, 1]) y_t = math.sin(y_true[:, 0]) * math.sin(y_true[:, 1]) z_t = math.cos(y_true[:, 0]) norm_p = math.sqrt(x_p * x_p + y_p * y_p + z_p * z_p) norm_t = math.sqrt(x_t * x_t + y_t * y_t + z_t * z_t) dot_pt = x_p * x_t + y_p * y_t + z_p * z_t angle_value = dot_pt / (norm_p * norm_t) angle_value = tf.clip_by_value(angle_value, -0.99999, 0.99999) loss_val = (math.acos(angle_value)) # tf.debugging.check_numerics( # loss_val, "Vse propalo", name=None # ) # print(loss_val.shape) return loss_val