def deltaE(image1, image2):

    try:
        img1 = rgb2lab(image1)
        img2 = rgb2lab(image2)
        """ Very generalized view that deltaE generally is between 0-100:
            Normalize with that range to make this comparable with jaccard,
            and as we are trying to find the minimal for this, it doesn't actually
            matter if some value is over 100 """
        return (mean([
            deltaE_ciede94(img1, img2).mean(),
            deltaE_ciede94(img2, img1).mean()
        ]) / 100)
    except ValueError:
        return 1
def cie_de(
    reference: numpy.ndarray,
    distorted: numpy.ndarray,
    dE_function="2000",
    lightness_weight=1.0,
    chroma_weight=1.0,
    hue_weight=1.0,
) -> numpy.ndarray:
    assert reference.shape == distorted.shape, "Shapes do not match"

    if len(reference.shape) == 2:
        reference = reference[numpy.newaxis, ...]
        distorted = distorted[numpy.newaxis, ...]

    reference_lab = rgb2lab(reference)
    distorted_lab = rgb2lab(distorted)

    if dE_function == "2000":
        deltaE = deltaE_ciede2000(reference_lab, distorted_lab,
                                  lightness_weight, chroma_weight, hue_weight)
    elif dE_function == "1994":
        deltaE = deltaE_ciede94(reference_lab, distorted_lab, hue_weight,
                                chroma_weight, lightness_weight)
    elif dE_function == "1976":
        deltaE = deltaE_cie76(reference_lab, distorted_lab)
    else:
        raise NameError(
            "CIE dE function with name {} not found".format(dE_function))
    return deltaE
Example #3
0
def test_ciede94():
    data = load_ciede2000_data()
    N = len(data)
    lab1 = np.zeros((N, 3))
    lab1[:, 0] = data['L1']
    lab1[:, 1] = data['a1']
    lab1[:, 2] = data['b1']

    lab2 = np.zeros((N, 3))
    lab2[:, 0] = data['L2']
    lab2[:, 1] = data['a2']
    lab2[:, 2] = data['b2']

    dE2 = deltaE_ciede94(lab1, lab2)
    oracle = np.array([
        1.39503887, 1.93410055, 2.45433566, 0.68449187, 0.6695627, 0.69194527,
        2.23606798, 2.03163832, 4.80069441, 4.80069445, 4.80069449, 4.80069453,
        4.80069441, 4.80069445, 4.80069449, 3.40774352, 34.6891632,
        29.44137328, 27.91408781, 24.93766082, 0.82213163, 0.71658427,
        0.8048753, 0.75284394, 1.39099471, 1.24808929, 1.29795787, 1.82045088,
        2.55613309, 1.42491303, 1.41945261, 2.3225685, 0.93853308, 1.30654464
    ])
    assert_allclose(dE2, oracle, rtol=1.e-8)
Example #4
0
def test_ciede94():
    data = load_ciede2000_data()
    N = len(data)
    lab1 = np.zeros((N, 3))
    lab1[:, 0] = data['L1']
    lab1[:, 1] = data['a1']
    lab1[:, 2] = data['b1']

    lab2 = np.zeros((N, 3))
    lab2[:, 0] = data['L2']
    lab2[:, 1] = data['a2']
    lab2[:, 2] = data['b2']

    dE2 = deltaE_ciede94(lab1, lab2)
    oracle = np.array([
        1.39503887, 1.93410055, 2.45433566, 0.68449187, 0.6695627,
        0.69194527, 2.23606798, 2.03163832, 4.80069441, 4.80069445,
        4.80069449, 4.80069453, 4.80069441, 4.80069445, 4.80069449,
        3.40774352, 34.6891632, 29.44137328, 27.91408781, 24.93766082,
        0.82213163, 0.71658427, 0.8048753, 0.75284394, 1.39099471,
        1.24808929, 1.29795787, 1.82045088, 2.55613309, 1.42491303,
        1.41945261, 2.3225685, 0.93853308, 1.30654464
    ])
    assert_allclose(dE2, oracle, rtol=1.e-8)
Example #5
0
def test_single_color_ciede94():
    lab1 = (0.5, 0.5, 0.5)
    lab2 = (0.4, 0.4, 0.4)
    deltaE_ciede94(lab1, lab2)
Example #6
0
def get_delta(pic):
    print(color.deltaE_cmc(LabPic, pic)[0][0])
    print(color.deltaE_ciede94(LabPic, pic)[0][0])
    print(color.deltaE_ciede2000(LabPic, pic)[0][0])
Example #7
0
def distance_de94(src_lab, dst_lab):
    return color.deltaE_ciede94(src_lab, dst_lab)
Example #8
0
 def __is_similar_color(self, row, col):
     """Determine if the color at the given point is within the desired range."""
     candidate_color = self.img_values[row][col]
     delta_e = color.deltaE_ciede94(candidate_color, self.target_color)
     return delta_e <= self.COLOR_THRESHOLD
Example #9
0
def test_single_color_ciede94():
    lab1 = (0.5, 0.5, 0.5)
    lab2 = (0.4, 0.4, 0.4)
    deltaE_ciede94(lab1, lab2)
Example #10
0
def eval_test(config_path, model_path, show_image=False):
    model, test_dataloader, test_dataset, use_gpu = _load(
        config_path, model_path)

    total_processed = 0
    total_disc_real = 0
    total_disc_fake = 0
    total_ssim = 0
    total_psnr = 0
    total_psnr_count = 0  # fails to calculate if images are identical
    total_g_loss = 0
    total_deltaE76 = 0
    total_deltaE94 = 0
    total_deltaE2000 = 0

    total_real_real = 0
    total_fake_fake = 0

    for batch_index, (X_gray, X_color) in enumerate(test_dataloader):
        fake_label = torch.full((X_color.size(0), ), model.fake_label)
        real_label = torch.full((X_color.size(0), ), model.real_label)
        if use_gpu:
            real_label = real_label.cuda()
            fake_label = fake_label.cuda()
            X_gray = X_gray.cuda()
            X_color = X_color.cuda()

        X_fake, disc_fake = model.forward(X_gray,
                                          train='none',
                                          skip_generator=False)
        X_color, disc_real = model.forward(X_color,
                                           train='none',
                                           skip_generator=True)

        g_loss = model.Gcriterion(X_fake, X_color)

        # print(X_fake.shape)
        X_fake_not_norm = test_dataset.invert_transforms(X_fake.cpu()).cuda()
        X_color_not_norm = test_dataset.invert_transforms(X_color.cpu()).cuda()

        psnr_val = psnr(X_fake_not_norm, X_color_not_norm, 1.0, use_gpu)

        lab_fake = tensor_to_lab(X_fake_not_norm)
        lab_real = tensor_to_lab(X_color_not_norm)

        delta_e_76 = color.deltaE_cie76(lab_fake, lab_real)
        delta_e_94 = color.deltaE_ciede94(lab_fake, lab_real)
        delta_e_2000 = color.deltaE_ciede2000(lab_fake, lab_real)

        # TODO:  delta_e_94 has a (some?) nan pixels.
        #  Need to track down if this is due to floating point or what.
        mean_delta_76 = np.nanmean(delta_e_76, axis=(1, 2))
        mean_delta_94 = np.nanmean(delta_e_94, axis=(1, 2))
        mean_delta_2000 = np.nanmean(delta_e_2000, axis=(1, 2))

        # if np.isnan(mean_delta_94).any():
        #     print("ERROR, COMPUTED NAN FOR BATCH:", batch_index)
        #     img_name = test_dataset.data.iloc[batch_index, 0]
        #     print("Maybe on img_name???", img_name)
        #     return

        # print(lab_fake)

        # print("PSNR Tensor: ", psnr_val)
        # Note, if we go with only the py_ssim version of ssim,
        # we can batch and lose the for loop.
        for i in range(X_fake.shape[0]):
            fake_img = tensor_to_pil(X_fake_not_norm, i)
            real_img = tensor_to_pil(X_color_not_norm, i)

            if show_image:
                fake_img.show()
                real_img.show()
                input()

            # TODO:
            #  oh what a nightmare, py_ssim and pil_ssim don't agree.
            #  I'm taking PIL ssim since I know what inputs it expects.
            py_ssim_val = py_ssim(X_fake_not_norm[i:i + 1],
                                  X_color_not_norm[i:i + 1])
            pil_ssim_val = pil_ssim(fake_img, real_img)

            # print("Py ssim: ", py_ssim_val)
            # print("PIL ssim: ", pil_ssim_val)
            ssim_val = pil_ssim_val
            total_ssim += ssim_val

            # Computes exactly the same thing as the range 0-1 batch version, so psnr looks good.
            # psnr2_val = psnr(X_fake_not_norm[i:i+1] * 255, X_color_not_norm[i:i+1] * 255, 255, use_gpu)
            # print("PSNR_INDIV", psnr2_val)

        total_processed += X_fake.shape[0]
        total_disc_real += disc_real.sum()
        total_disc_fake += disc_fake.sum()
        total_g_loss += g_loss.sum()
        total_psnr_count += (psnr_val != 0).sum()
        total_psnr += psnr_val.sum()
        total_deltaE76 += mean_delta_76.sum()
        total_deltaE94 += mean_delta_94.sum()
        total_deltaE2000 += mean_delta_2000.sum()
        total_real_real += (disc_real > .5).sum()
        total_fake_fake += (disc_fake < .5).sum()

        # print(psnr_val)

        print("Avg Disc Sigmoid on REAL: ", total_disc_real / total_processed)
        print("Avg Disc Sigmoid on FAKE: ", total_disc_fake / total_processed)
        print("Avg PIL SSIM: ", total_ssim / total_processed)
        print("Avg psnr: ", total_psnr / total_psnr_count, "dB")
        print("Avg GLoss: ", total_g_loss / total_processed)
        print("Avg delta E 76: ", total_deltaE76 / total_processed)
        print("Avg delta E 94: ", total_deltaE94 / total_processed)
        print("Avg delta E 2000: ", total_deltaE2000 / total_processed)
        print("Correctly Identified Real: ", total_real_real, "/",
              total_processed)
        print("Correctly Identified Fake: ", total_fake_fake, "/",
              total_processed)

        if total_psnr_count != total_processed:
            print("Identical Images: ", total_processed - total_psnr_count)
        # Inception Score NOT DOING
        # Fix SSIM CHECK
        # PSNR CHECK
        # Delta E

        # Disc Real on Real
        # Disc Fake on Real
        # Disc Real on Fake
        # Disc Fake on Fake

        print(total_processed, batch_index, "/", len(test_dataloader))