Esempio n. 1
0
 def cfun(x):
     err = _cfun(tuple(x))
     if USE_OWN_PSEUDO_HUBER_LOSS:
         sqrt_phl_err = np.sqrt(tools.pseudo_huber_loss(
             err, huber_coef)) if huber_coef is not False else err
     else:
         sqrt_phl_err = err
     return sqrt_phl_err
Esempio n. 2
0
 def cost_fn(x, Y, X, K, iK):
     dx, dy, th, *dist_coefs = x
     A = np.array([
         [math.cos(th), -math.sin(th), dx],
         [math.sin(th), math.cos(th), dy],
     ])
     Xdot = Camera.distort(X.dot(A.T), dist_coefs, K, iK)
     return tools.pseudo_huber_loss(np.linalg.norm(Y - Xdot,
                                                   axis=1),
                                    delta=3)
Esempio n. 3
0
    def cfun(x, use_own_pseudo_huber_loss=USE_OWN_PSEUDO_HUBER_LOSS):
        err = _cfun(tuple(x))
        if use_own_pseudo_huber_loss:
            sqrt_phl_err = np.sqrt(weight * tools.pseudo_huber_loss(
                err, huber_coef)) if huber_coef is not False else err
        else:
            sqrt_phl_err = err

        if prior_x_idxs is not None:
            prior_r_current = (prior_r + prior_J.dot(x[prior_x_idxs] -
                                                     prior_x)) * prior_weight
            sqrt_phl_err = np.concatenate((sqrt_phl_err, prior_r_current),
                                          axis=0)

        return sqrt_phl_err
Esempio n. 4
0
    def jac(x):
        # apply pseudo huber loss derivative
        J = _jacobian(x, pose0, fixed_pt3d, n_cams, n_pts, cam_idxs, pt3d_idxs,
                      pts2d, K, px_err_sd)

        if USE_OWN_PSEUDO_HUBER_LOSS and huber_coef is not False:
            ## derivative of pseudo huber loss can be applied afterwards by multiplying with the err derivative
            # NOTE: need extra (.)**0.5 term as least_squares applies extra (.)**2 on top of our cost function
            # - d (weight * phl(e))**0.5/dksi = d (weight * phl(e))**0.5/d phl(e) * d phl(e)/de * de/dksi
            # - d (weight * phl(e))**0.5/d phl(e) = weight**0.5  * 0.5 * phl(e)**(-0.5)
            # - d phl(e)/de = e/sqrt(1+e**2/delta**2)
            # - de/dksi is solved next
            err = _cfun(tuple(x))
            phl_err = tools.pseudo_huber_loss(err, huber_coef)
            dhuber = 0.5 * np.sqrt(
                1 / phl_err) * err / np.sqrt(1 + err**2 / huber_coef**2)
            J = J.multiply(dhuber.reshape((-1, 1)))

        return J
Esempio n. 5
0
def costfn(p, lam_g, spec_g):
    from visnav.algo import tools

    mag_v, Teff, log_g, fe_h = p
    Teff, log_g = abs(Teff), abs(log_g)

    try:
        spec_fn = Stars.synthetic_radiation_fn(Teff,
                                               fe_h,
                                               log_g,
                                               mag_v=mag_v,
                                               model='ck04models')
    except AssertionError:
        return np.ones_like(spec_g)

    spec_m = spec_fn(lam_g * 1e-9)
    diff = tools.pseudo_huber_loss(0.1, np.log(spec_m) - np.log(spec_g))
    if np.any(np.isnan(diff)):
        diff = np.ones_like(spec_g)
    return diff
Esempio n. 6
0
    def jac(x, use_own_pseudo_huber_loss=USE_OWN_PSEUDO_HUBER_LOSS):
        # apply pseudo huber loss derivative
        J = _jacobian(x, pose0, fixed_pt3d, n_cams, n_pts, n_dist, n_cam_intr,
                      cam_idxs, pt3d_idxs, pts2d, v_pts2d, K, px_err_sd,
                      meas_r, meas_aa, meas_idxs, loc_err_sd, ori_err_sd,
                      huber_coef)

        if use_own_pseudo_huber_loss:
            ## derivative of pseudo huber loss can be applied afterwards by multiplying with the err derivative
            # NOTE: need extra (.)**0.5 term as least_squares applies extra (.)**2 on top of our cost function
            # - d (weight * phl(e))**0.5/dksi = d (weight * phl(e))**0.5/d phl(e) * d phl(e)/de * de/dksi
            # - d (weight * phl(e))**0.5/d phl(e) = weight**0.5  * 0.5 * phl(e)**(-0.5)
            # - d phl(e)/de = e/sqrt(1+e**2/delta**2)
            # - de/dksi is solved next
            err = _cfun(tuple(x))
            phl_err = tools.pseudo_huber_loss(err, huber_coef)
            dhuber = 0.5 * np.sqrt(
                weight / phl_err) * err / np.sqrt(1 + err**2 / huber_coef**2)
            J = J.multiply(dhuber.reshape((-1, 1)))

        if curr_prior_J is not None:
            J = sp.vstack((J, curr_prior_J))

        return J
Esempio n. 7
0
        def cost_fun(x, measures, prior_x, return_details=False, plot=False):
            c_qeff_coefs, f_gains, gain_adj, psf_coef = decode(x)

            band = []
            obj_ids = []
            measured_du = []
            expected_du = []
            weights = []
            for m in measures:
                if FRAME_GAINS == FRAME_GAIN_SAME:
                    pre_sat_gain = f_gains[0]
                elif FRAME_GAINS == FRAME_GAIN_INDIVIDUAL:
                    pre_sat_gain = f_gains[m.frame.id]
                elif FRAME_GAINS == FRAME_GAIN_STATIC:
                    if m.obj_id[0] == 'moon':
                        pre_sat_gain = MOON_GAIN_ADJ
                    else:
                        pre_sat_gain = STAR_GAIN_ADJUSTMENT if m.frame.cam[
                            0].emp_coef >= 1 else STAR_GAIN_ADJUSTMENT_TN
                else:
                    pre_sat_gain = 1

                edu = m.expected_du(pre_sat_gain=pre_sat_gain,
                                    post_sat_gain=gain_adj,
                                    qeff_coefs=c_qeff_coefs,
                                    psf_coef=psf_coef)

                if return_details or (m.obj_id[0],
                                      m.cam_i) not in IGNORE_MEASURES:
                    expected_du.append(edu)
                    measured_du.append(m.du_count)
                    weights.append(m.weight)
                    band.append(m.cam_i)
                    obj_ids.append(m.obj_id)

            measured_du, expected_du, band = map(
                np.array, (measured_du, expected_du, band))

            if plot:
                plt.rcParams.update({'font.size': 16})
                fig, ax = plt.subplots(1, 1, figsize=[6.4, 4.8])
                sb, = ax.plot(expected_du[band == 0] * 1e-3,
                              measured_du[band == 0] * 1e-3, 'bx')
                sg, = ax.plot(expected_du[band == 1] * 1e-3,
                              measured_du[band == 1] * 1e-3, 'gx')
                sr, = ax.plot(expected_du[band == 2] * 1e-3,
                              measured_du[band == 2] * 1e-3, 'rx')
                line = np.linspace(0, np.max(expected_du))
                ax.plot(line * 1e-3, line * 1e-3, 'k--', linewidth=0.5)
                ax.set_xlabel('Expected [1000 DNs]')
                ax.set_ylabel('Measured [1000 DNs]')
                names = Stars.get_catalog_id(
                    np.unique(list(s[0] for s in obj_ids if s[0] != 'moon')),
                    'simbad')
                names['moon'] = 'Moon'
                labels = np.array([names[id[0]] for id in obj_ids])
                tools.hover_annotate(fig, ax, sb, labels[band == 0])
                tools.hover_annotate(fig, ax, sg, labels[band == 1])
                tools.hover_annotate(fig, ax, sr, labels[band == 2])
                plt.tight_layout()
                plt.show()

            _, _, gain_adj0, _ = decode(prior_x)
            #            err = tuple(tools.pseudo_huber_loss(STAR_CALIB_HUBER_COEF, (measured_du - expected_du) * 2 / (expected_du + measured_du)) * np.array(weights))
            err = tuple(
                tools.pseudo_huber_loss(
                    STAR_CALIB_HUBER_COEF,
                    np.log10(expected_du) - np.log10(measured_du)) *
                np.array(weights))

            n = 3 * len(c_qeff_coefs[0])

            lab_dp = tuple()
            if STAR_LAB_DATAPOINT_WEIGHT > 0:
                c, lam = m.frame.cam, 557.7e-9
                g = Camera.sample_qeff(c_qeff_coefs[1], c[1].lambda_min,
                                       c[1].lambda_max, lam)
                eps = 1e-10
                r_g = (Camera.sample_qeff(c_qeff_coefs[2], c[2].lambda_min,
                                          c[2].lambda_max, lam) + eps) / (g +
                                                                          eps)
                b_g = (Camera.sample_qeff(c_qeff_coefs[0], c[0].lambda_min,
                                          c[0].lambda_max, lam) + eps) / (g +
                                                                          eps)
                lab_dp = tuple(STAR_LAB_DATAPOINT_WEIGHT * (np.log10(r_g) - np.log10(np.array((0.26, 0.25, 0.24, 0.24))))**2) \
                        +tuple(STAR_LAB_DATAPOINT_WEIGHT * (np.log10(b_g) - np.log10(np.array((0.23, 0.24, 0.21, 0.22))))**2)

            prior = tuple(STAR_CALIB_PRIOR_WEIGHT ** 2 * (np.array(x[:n]) - np.array(prior_x[:n])) ** 2) \
                if STAR_CALIB_PRIOR_WEIGHT > 0 else tuple()

            err_tuple = lab_dp + err + prior
            return (err_tuple, measured_du, expected_du) if return_details else \
                    err_tuple if opt_method == 'leastsq' else \
                    np.sum(err_tuple)
Esempio n. 8
0
 def costfn(p, x, y):
     gamma, gamma_break, max_val, scale = tuple(map(abs, p))
     diff = ImageProc.adjust_gamma(x, gamma, gamma_break,
                                   max_val=max_val) * scale - y
     diff = tools.pseudo_huber_loss(120, diff)
     return np.sum(diff) if _USE_BFGS else diff