Пример #1
0
def test_and_metrics(test, model, black=True, tolerance=1):
    if (test.shape[0] % 2 == 1):
        test = test[0:-1, :]
    if (test.shape[1] % 2 == 1):
        test = test[:, 0:-1]

    if (test.shape[0] < 40 or test.shape[1] < 40):
        return None

    timebef = time.time()

    baseline = generate_baseline(test)
    preds, numpreds, numiters = generate_prediction(test,
                                                    model,
                                                    black=black,
                                                    tolerance=tolerance)

    mse_base = mse(baseline, test)
    mse_pred = mse(preds, test)

    psnr_base = psnr(baseline, test)
    psnr_pred = psnr(preds, test)

    ssim_base = ssim(baseline, test)
    ssim_pred = ssim(preds, test)

    timeaft = time.time()
    print("w shape %d x %d, iteration took %f seconds" %
          (test.shape[0], test.shape[1], (timeaft - timebef)))
    return mse_base, psnr_base, ssim_base, mse_pred, psnr_pred, ssim_pred, numpreds, numiters
Пример #2
0
def ErrorMetrics(vol_s, vol_t):
    # calculate various error metrics.
    # vol_s should be the synthesized volume (a 3d numpy array) or an array of these volumes
    # vol_t should be the ground truth volume (a 3d numpy array) or an array of these volumes

    vol_s = np.squeeze(vol_s)
    vol_t = np.squeeze(vol_t)

    assert len(vol_s.shape) == len(vol_t.shape) == 3
    assert vol_s.shape[0] == vol_t.shape[0]
    assert vol_s.shape[1] == vol_t.shape[1]
    assert vol_s.shape[2] == vol_t.shape[2]

    vol_s[vol_t == 0] = 0
    vol_s[vol_s < 0] = 0

    errors = {}

    errors['MSE'] = np.mean((vol_s - vol_t) ** 2.)
    errors['SSIM'] = ssim(vol_t, vol_s)
    dr = np.max([vol_s.max(), vol_t.max()]) - np.min([vol_s.min(), vol_t.min()])
    errors['PSNR'] = psnr(vol_t, vol_s, dynamic_range=dr)

    # non background in both
    non_bg = (vol_t != vol_t[0, 0, 0])
    errors['SSIM_NBG'] = ssim(vol_t[non_bg], vol_s[non_bg])
    dr = np.max([vol_t[non_bg].max(), vol_s[non_bg].max()]) - np.min([vol_t[non_bg].min(), vol_s[non_bg].min()])
    errors['PSNR_NBG'] = psnr(vol_t[non_bg], vol_s[non_bg], dynamic_range=dr)

    vol_s_non_bg = vol_s[non_bg].flatten()
    vol_t_non_bg = vol_t[non_bg].flatten()
    errors['MSE_NBG'] = np.mean((vol_s_non_bg - vol_t_non_bg) ** 2.)

    return errors
Пример #3
0
def psnr_ssim_from_sci(img1, img2, padding=4, y_channels = False):
    '''
    Calculate PSNR and SSIM on Y channels for image super resolution
    :param img1: numpy array
    :param img2: numpy array
    :param padding: padding before calculate
    :return: psnr, ssim
    '''

    img1 = Image.fromarray(np.uint8(img1), mode='RGB')
    img2 = Image.fromarray(np.uint8(img2), mode='RGB')
    if y_channels:
        img1 = img1.convert('YCbCr')
        img1 = np.ndarray((img1.size[1], img1.size[0], 3), 'u1', img1.tobytes())

        img2 = img2.convert('YCbCr')
        img2 = np.ndarray((img2.size[1], img2.size[0], 3), 'u1', img2.tobytes())
        # get channel Y
        img1 = img1[:, :, 0]
        img2 = img2[:, :, 0]
        # padding
        img1 = img1[padding: -padding, padding:-padding]
        img2 = img2[padding: -padding, padding:-padding]
        ss = ssim(img1, img2)
        ps = psnr(img1, img2)
    else:
        # padding
        img1 = np.array(img1)
        img2 = np.array(img2)
        # img1 = img1[padding: -padding, padding:-padding,:]
        # img2 = img2[padding: -padding, padding:-padding,:]
        ps = psnr(img1,img2,255)
        ss = ssim(img1,img2,multichannel=True)

    return (ps, ss)
def test(downsampled_img, img):
    downsampled_img = downsampled_img[np.newaxis, :, :, :]
    downsampled = tf.placeholder(tf.float32, [None, None, None, 3])
    G = generator("generator")
    SR = G(downsampled)
    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.restore(sess, "./save_para/.\\para.ckpt")
    SR_img = sess.run(SR, feed_dict={downsampled: downsampled_img / 127.5 - 1})
    Image.fromarray(np.uint8((SR_img[0, :, :, :] + 1) * 127.5)).show()
    Image.fromarray(np.uint8((SR_img[0, :, :, :] + 1) * 127.5)).save(
        "C://Users//asus//Desktop//imageFigure8-12//srGAN97531.jpg")
    Image.fromarray(np.uint8((downsampled_img[0, :, :, :]))).show()
    h = img.shape[0]
    w = img.shape[1]
    bic_img = misc.imresize(downsampled_img[0, :, :, :], [h, w])
    Image.fromarray(np.uint8((bic_img))).show()
    SR_img = misc.imresize(SR_img[0, :, :, :], [h, w])
    p = psnr(img, SR_img)
    s = ssim(img, SR_img, multichannel=True)
    p1 = psnr(img, bic_img)
    s1 = ssim(img, bic_img, multichannel=True)
    print("SR PSNR: %f, SR SSIM:%f, BIC PSNR: %f, BIC SSIM: %f" %
          (p, s, p1, s1))
    sess.close()
Пример #5
0
    def eval(self):
        ''' Testing Function'''
        print("Testing the results")
        self.input_setup_adni(input_path)
        self.model_setup()
        saver = tf.train.Saver()
        init = (tf.global_variables_initializer(), tf.local_variables_initializer())
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True

        with tf.Session() as sess:
            sess.run(init)
            print(chkpt_fname)
            saver.restore(sess, chkpt_fname)
            if not os.path.exists(outputAB_path):
                os.makedirs(outputAB_path)
            MAE = []; SSIM = []; PSNR = []
            for ptr in range(min(len(self.imdb_test), max_images)):
                inputA, inputB = self.inputAB(self.imdb_test[ptr], cycload=False)
                if (inputB is None) | (inputA is None): continue
                filename = self.imdb_test[ptr][1]+self.imdb_test[ptr][0]
                print(filename)
                fake_A, fake_B = sess.run([self.fake_A, self.fake_B], feed_dict={self.input_A: inputA[0:1], self.input_B: inputB[0:1]})
                print([np.mean(np.abs(fake_A-inputA)), np.mean(np.abs(fake_B-inputB))])
                MAE.append([np.mean(np.abs(fake_A-inputA)), np.mean(np.abs(fake_B-inputB))])
                SSIM.append([ssim(inputA[0], fake_A[0], multichannel=True), ssim(inputB[0], fake_B[0], multichannel=True)])
                PSNR.append([psnr(inputA[0]/2, fake_A[0]/2), psnr(inputB[0]/2, fake_B[0]/2)])
                imsave(os.path.join(outputAB_path, filename + '.bmp'), np.concatenate(
                    (np.array((inputA[:, :, :, 72, :]) * 100 + 100).astype(np.uint8).reshape([img_width, img_height]),
                    np.array((inputB[:, :, :, 72, :]) * 100 + 100).astype(np.uint8).reshape([img_width, img_height]),
                    np.array((fake_A[:, :, :, 72, :]) * 100 + 100).astype(np.uint8).reshape([img_width, img_height]),
                    np.array((fake_B[:, :, :, 72, :]) * 100 + 100).astype(np.uint8).reshape([img_width, img_height])), axis=1), 'bmp')
            print(np.mean(MAE, axis=0), np.mean(SSIM, axis=0), np.mean(PSNR, axis=0))
            print(np.std(MAE, axis=0), np.std(SSIM, axis=0), np.std(PSNR, axis=0))
Пример #6
0
def get_metrics(images, metric):
    height, width = images.shape[0], images.shape[1]

    width_cutoff = width // 2
    s1 = images[..., :width_cutoff, :]
    s2 = images[..., width_cutoff:, :]

    s1[s1 == np.nan] = 0
    s2[s2 == np.nan] = 0

    sum = 0
    s1 = np.expand_dims(s1, 0)
    s2 = np.expand_dims(s2, 0)

    for left, right in zip(s1, s2):
        if (metric == "ssim"):
            sum += ssim(left,
                        right,
                        data_range=right.max() - right.min(),
                        multichannel=True)
        elif (metric == "psnr"):
            sum += psnr(left, right)
        elif (metric == "mse"):
            sum += mse(left, right)
        else:
            print("Metric not recognized")
            exit()

    return sum
Пример #7
0
def run_evaluation_metrics():
    # run_replace()
    # run_eval()
    a_org = cv2.imread('eval_replace_ground.jpg')
    a_rep = cv2.imread('eval_replace_out_aer.jpg')
    a_dic = cv2.imread('eval_dict.jpg')
    a_gan = cv2.imread('eval_gan_out_ground.jpg')

    a_mask = cv2.imread('eval_replace_out_cld.jpg')
    a_mask[a_mask > 1] = 1

    from skimage.measure import compare_psnr as psnr
    from skimage.measure import compare_mse as mse
    from skimage.measure import compare_ssim as ssim

    for a in (a_rep, a_dic, a_gan):  # ssim
        print("| PSNR %e MSE %e SSIM: %e" % (psnr(a_org, a),
                                             mse(a_org, a),
                                             ssim(a_org, a, multichannel=True)))
    # a_mask = cv2.imread('eval_replace_out_cld.jpg')
    # print(np.sum(a_mask // 255), a_mask.shape[0] * a_mask.shape[1])
    # 11205 262144
    """
    | PSNR 1.805008e+01 MSE 1.018760e+03 SSIM: 8.988292e-01
    | PSNR 2.858750e+01 MSE 9.001815e+01 SSIM: 9.069278e-01
    | PSNR 3.486142e+01 MSE 2.122945e+01 SSIM: 9.710586e-01
    """
    # mask_num = np.sum(a_mask)
    # print("| mask num:", mask_num)
    # for a in (a_rep, a_dic, a_gan):  # EER
    #     print("|", np.sum((a_org / 255.0 - a / 255.0) ** 2 / (a_org / 255.0)) / mask_num)
    """
Пример #8
0
def get_avg_metrics(images, split, save_dir_root):
    height, width = images.shape[1], images.shape[2]

    width_cutoff = width // 2
    s1 = images[..., :width_cutoff, :]
    s2 = images[..., width_cutoff:, :]

    i = 0
    ssim_sum = 0
    psnr_sum = 0
    for left, right in tqdm(zip(s1, s2), total=MAX_IMAGES):
        i += 1

        if split:
            imageio.imsave(save_dir_root + "/sharp/" + str(i) + ".png", left)
            imageio.imsave(save_dir_root + "/blur/" + str(i) + ".png", right)

        ssim_sum += ssim(left,
                         right,
                         data_range=right.max() - right.min(),
                         multichannel=True)
        psnr_sum += psnr(left, right)

        if (i % 50 == 0) and i != 0:
            print("[+]: Iteration {}".format(i))
            print_metrics(ssim_sum, psnr_sum, i)

        if i == MAX_IMAGES:
            break

    return ssim_sum / i, psnr_sum / i
Пример #9
0
def calculate_score(output_dir="./data/output/",
                    gt_dir="./data/input/fattal_db/true/",
                    key1="im0",
                    key2="im0"):
    img_files = os.listdir(gt_dir)

    Score = []
    print("computing  Score")
    s = []
    for i in range(len(img_files)):
        gt_img = misc.imread(gt_dir + img_files[i]) / 255.0

        out_f = img_files[i]
        out_f = out_f.replace(key1, key2)

        out_img = misc.imread(output_dir + out_f) / 255.0
        #gt_img = read_resize_image(gt_dir+img_files[i])

        t1 = psnr(out_img, gt_img)
        t2 = ssim(out_img, gt_img, multichannel=True)
        t3 = skimage.color.deltaE_ciede2000(rgb2lab(out_img), rgb2lab(gt_img))
        t3 = np.mean(t3, axis=(0, 1))
        print t3.shape

        Score.append([t1, t2, t3])
        s.append([out_f, round(t1, 2), round(t2, 2)])
    Score = np.array(Score)
    mean = np.mean(Score, axis=0)

    return s, Score, mean
Пример #10
0
def evaluation(root_dir):
    mse_total = 0
    psnr_total = 0
    fmse_total = 0
    count = 0
    for comp_name in os.listdir(root_dir):
        comp = Image.open(comp_name)
        comp = comp.resize(IMAGE_SIZE, Image.BICUBIC)
        comp = np.array(comp, dtype=np.float32)

        real = Image.open(real_name(comp_name))
        real = real.resize(IMAGE_SIZE, Image.BICUBIC)
        real = np.array(real, dtype=np.float32)

        mask = Image.open(mask_name(comp_name))
        mask = mask.convert('1')
        mask = mask.resize(IMAGE_SIZE, Image.BICUBIC)
        mask = np.array(mask, dtype=np.uint8)
        fore_area = np.sum(np.sum(mask, axis=0), axis=0)
        mask = mask[..., np.newaxis]

        mse_total += mse(comp, real)
        psnr_total += psnr(real, comp, data_range=comp.max() - comp.min())
        fmse_total += mse(comp * mask, real * mask) * 256 * 256 / fore_area
        count += 1
    print(
        "%s MSE %0.2f | PSNR %0.2f | fMSE %0.2f" %
        (comp_name, mse_total / count, psnr_total / count, fmse_total / count))
Пример #11
0
    def PSNR(self, img, nimg):
        '''
		:param img:  original image
		:param nimg: noised image
		:return: return value of PSNR
		'''
        return psnr(img, nimg, data_range=255)
Пример #12
0
def run_eval_metrics():
    from skimage.measure import compare_psnr as psnr
    from skimage.measure import compare_mse as mse
    from skimage.measure import compare_ssim as ssim

    grounds = np.load('grounds.npy')

    for dir in ('eval_low_rank', 'eval_dict', 'eval_two_stage1', 'eval_ground'):
        eval_matrics = list()
        for i, ground in enumerate(grounds):
            if i == 3 or 8 <= i <= 10:
                continue
            # ground = cv2.imread('%s/%02d.jpg' % ('eval_ground', i))
            result = cv2.imread('%s/%02d.jpg' % (dir, i))
            # print("| PSNR %e MSE %e SSIM: %e" %
            #       (psnr(ground, result),
            #        mse(ground, result),
            #        ssim(ground, result, multichannel=True)))
            eval_matrics.append((
                ssim(ground, result, multichannel=True),
                psnr(ground, result),
                mse(ground, result),
                np.mean((ground - result) ** 2),
            ))

        eval_matrics = np.array(eval_matrics)
        np.save('%s/eval.npy' % dir, eval_matrics)

    for dir in ('eval_low_rank', 'eval_dict', 'eval_two_stage1', 'eval_ground'):
        print("%16s SSIM PSNR MSE L2:" % dir,
              np.average(np.load('%s/eval.npy' % dir), axis=0))
Пример #13
0
    def forward(self, input_data, gt, is_train=True, is_test=False):
        '''
      input_data: raindrops image
      gt: ground truth
      attention_map: attention map
      frame1: autoencoder last 5th
      frame2: autoencoder last 3rd
      encoder_output: autoencoder last 1st
      O_map_2d: D(encoder_output)
      R_map_2d: D(ground truth)
    '''
        input_data = self.variable_grad(input_data, is_train)
        gt = self.variable_grad(gt, is_train)
        attention_map, frame1, frame2, encoder_output = self.generator(
            input_data)

        # error
        batch_size = input_data.shape[0]
        error = self.mse_func(encoder_output, gt).item()
        PSNR = 0
        SSIM = 0
        for i in range(batch_size):
            a = self.handle_tensor(encoder_output[i])
            b = self.handle_tensor(gt[i])
            PSNR += psnr(a, b)
            SSIM += ssim(a, b, multichannel=True)
        PSNR /= batch_size
        SSIM /= batch_size

        if is_train:
            # calculate loss of generator
            binary_mask = []
            for i in range(batch_size):
                binary_mask.append(self.get_binary_mask(gt[i], input_data[i]))
            binary_mask = torch.stack(binary_mask)
            binary_mask = self.variable_grad(binary_mask, is_train)

            attention_loss = self.attention_loss_func(attention_map,
                                                      binary_mask)
            S = [frame1, frame2, encoder_output]
            multi_scale_loss = self.multi_scale_loss_func(S, gt)
            perceptual_loss = self.perceptual_loss_func(encoder_output, gt)

            O_map_2d, O_prob = self.discriminator(encoder_output)
            gan_loss = self.gan_loss_func(O_prob, is_real=False)
            generator_loss = -0.01 * gan_loss + attention_loss + multi_scale_loss + perceptual_loss

            # calculate loss of discriminator
            R_map_2d, R_prob = self.discriminator(gt)
            map_loss = self.map_loss_func(O_map_2d, R_map_2d,
                                          attention_map[-1])
            gt_gan_loss = self.gan_loss_func(R_prob, is_real=True)
            discriminator_loss = gt_gan_loss + gan_loss + map_loss

            return generator_loss, discriminator_loss, error, PSNR, SSIM, map_loss, attention_loss, multi_scale_loss, perceptual_loss, attention_map[
                -1]
        elif is_test:
            return encoder_output, error, PSNR, SSIM
        else:
            return encoder_output, error, PSNR, SSIM
Пример #14
0
def compare_img(source_path, target_path):
    src_img = cv2.imread(source_path)
    tar_img = cv2.imread(target_path)
    ssim_const = ssim(src_img, tar_img, multichannel=True)
    psnr_const = psnr(src_img, tar_img)
    print('ssim : ', ssim_const)
    print('psnr : ', psnr_const)
 def test(self, cleaned_path="./crop_color_test//img24901_1.png"):
     cleaned_img = np.reshape(
         np.array(Image.open(cleaned_path), dtype=np.float32),
         [1, 256, 256, 3])
     noised_img = cleaned_img + np.random.normal(0, SIGMA,
                                                 cleaned_img.shape)
     [denoised_img] = self.sess.run(
         [self.denoised_img],
         feed_dict={
             self.clean_img: cleaned_img,
             self.noised_img: noised_img,
             self.train_phase: False
         })
     # PSNR = psnr(np.uint8(cleaned_img[0, :, :, :]), np.uint8(denoised_img[0, :, :, :]))
     # SSIM = ssim(np.uint8(cleaned_img[0, :, :, :]), np.uint8(denoised_img[0, :, :, :]))
     PSNR = SSIM = 0
     for i in range(3):
         PSNR += psnr(np.uint8(cleaned_img[0, :, :, i]),
                      np.uint8(denoised_img[0, :, :, i]))
         SSIM += ssim(np.uint8(cleaned_img[0, :, :, i]),
                      np.uint8(denoised_img[0, :, :, i]))
     PSNR /= 3.0
     SSIM /= 3.0
     print("psnr: %g, ssim: %g" % (PSNR, SSIM))
     compared = np.concatenate(
         (cleaned_img[0, :, :, :], noised_img[0, :, :, :],
          denoised_img[0, :, :, :]), 1)
     Image.fromarray(np.uint8(compared)).show()
Пример #16
0
def calc_psnr(gt, img):
    """
    Calculate peak to signal noise ratio
    @param gt: ground truth
    @param: img: input image
    """
    psnr_const = psnr(gt, img, dynamic_range=255)
    return psnr_const
Пример #17
0
def evaluate_image(mode,image,image_golden):

    if mode == "PSNR":
        cost = psnr(image,image_golden,255.0)
    elif mode == "SSIM": 
        cost = ssim(image,image_golden,data_range=255.0)
    
    return cost
Пример #18
0
    def test(self, mode, label, mask):
        y = label.numpy()
        netLabel = Variable(label).type(self.dtype)
        if (self.mode != 'inNetDC'):
            assert False, 'only for inNetDC mode'
        else:
            mask_var = Variable(mask).type(self.dtype)
            subF = kspace_subsampling_pytorch(netLabel, mask_var)
            complexFlag = (mode[0] == 'complex')
            netInput = imgFromSubF_pytorch(subF, complexFlag)

        y = y[0]

        netOutput = self.net(netInput, subF, mask_var)
        loss = self.lossForward(netOutput, netLabel)
        netOutput_np = netOutput.cpu().data.numpy()
        img1 = netOutput_np[0, 0:1].astype('float64')
        if (netOutput_np.shape[1] == 2):
            netOutput_np = abs(netOutput_np[:, 0:1] +
                               netOutput_np[:, 1:2] * 1j)
        img2 = netOutput_np[0].astype('float64')
        y2 = y[0:1]

        img1 = np.clip(img1, 0, 1)
        img2 = np.clip(img2, 0, 1)

        if (self.isFastMRI):
            psnrBefore = psnr(y2, img1, 12)
            psnrAfter = psnr(y2, img2, 12)
        else:
            psnrBefore = psnr(y2, img1)
            psnrAfter = psnr(y2, img2)

        ssimBefore = ssim(y2[0], img1[0])
        ssimAfter = ssim(y2[0], img2[0])

        return {
            "loss": loss.item(),
            "psnr1": psnrBefore,
            "psnr2": psnrAfter,
            "ssim1": ssimBefore,
            "ssim2": ssimAfter,
            "result1": img1,
            "result2": img2,
            'label': y2
        }
def calculate_PSNR(gt, rc):
    num_images = rc.shape[0]
    PSNR_values = np.zeros([num_images])

    for i in range(num_images):
        PSNR = psnr(gt[i], rc[i])
        PSNR_values[i] = PSNR
    return np.mean(PSNR_values)
Пример #20
0
def test_quality(gt, pred):
    shape = gt.shape
    if len(shape) == 3:
        psnr_s = []
        ssim_s = []
        for i in range(shape[0]):
            qr = psnr(gt[i,:,:].astype(np.uint8), pred[i,:,:].astype(np.uint8))
            sm = ssim(gt[i,:,:].astype(np.uint8), pred[i,:,:].astype(np.uint8), multichannel = True)
            psnr_s.append(qr)
            ssim_s.append(sm)
        # Here we will return the mean of the psnrs and ssims
        return np.mean(psnr_s), np.mean(ssim_s)

    elif len(shape) == 2:
        qr = psnr(gt.astype(np.uint8), pred.astype(np.uint8))
        sm = ssim(gt.astype(np.uint8), pred.astype(np.uint8), multichannel = True)
        return qr, sm
Пример #21
0
def compare_images_silent(imageA, imageB):
    imageA = cv2.cvtColor(imageA, cv2.COLOR_RGB2GRAY)
    imageB = cv2.cvtColor(imageB, cv2.COLOR_RGB2GRAY)
    m = mse(imageA, imageB)
    s = ssim(imageA, imageB)
    p = psnr(imageA, imageB)

    print("PSNR: %.2f MSE: %.2f SSIM: %.2f" % (p, m, s))
    return m
Пример #22
0
def compare_imgs(generated_imgs, target_imgs):
    s_sim = 0.0
    p_snr = 0.0

    for i in range(len(generated_imgs)):
        s_sim += ssim(generated_imgs[i], target_imgs[i], multichannel=True)
        p_snr += psnr(target_imgs[i], generated_imgs[i])

    return p_snr / len(generated_imgs), s_sim / len(generated_imgs)
Пример #23
0
def evaluate(model, dataloader, epoch, writer, logger, data_name='val'):

    save_root = os.path.join(opt.result_dir, opt.tag, str(epoch), data_name)

    utils.try_make_dir(save_root)

    total_psnr = 0.0
    total_ssim = 0.0
    ct_num = 0
    # print('Start testing ' + tag + '...')
    for i, sample in enumerate(dataloader):
        utils.progress_bar(i, len(dataloader), 'Eva... ')

        path = sample['path']
        with torch.no_grad():
            recovered = model(sample)

        if data_name == 'val':
            label = sample['label']
            label = tensor2im(label)
            recovered = tensor2im(recovered)

            ct_num += 1

            total_psnr += psnr(recovered, label, data_range=255)
            total_ssim += ski_ssim(recovered,
                                   label,
                                   data_range=255,
                                   multichannel=True)

            save_dst = os.path.join(save_root,
                                    utils.get_file_name(path[0]) + '.png')
            Image.fromarray(recovered).save(save_dst)

        elif data_name == 'test':
            pass

        else:
            raise Exception('Unknown dataset name: %s.' % data_name)

        # 保存结果
        save_dst = os.path.join(save_root,
                                utils.get_file_name(path[0]) + '.png')
        Image.fromarray(recovered).save(save_dst)

    if data_name == 'val':
        ave_psnr = total_psnr / float(ct_num)
        ave_ssim = total_ssim / float(ct_num)
        # write_loss(writer, f'val/{data_name}', 'psnr', total_psnr / float(ct_num), epochs)

        logger.info(f'Eva({data_name}) epoch {epoch}, psnr: {ave_psnr}.')
        logger.info(f'Eva({data_name}) epoch {epoch}, ssim: {ave_ssim}.')

        return f'{ave_ssim: .3f}'
    else:
        return ''
 def test_all(self, cleaned_path="./BSD500_256//"):
     filenames = os.listdir(cleaned_path)
     csvname = 'sigma50_bsd_6000.csv'
     csvfile = open(csvname, 'w', newline="")
     writer = csv.writer(csvfile,dialect='excel')
     writer.writerow(["num","PSNR","n_PSNR","c_SSIM","n_SSIM"])
     idx = 0
     tn_PSNR=tn_SSIM=tc_PSNR=tc_SSIM=0
     for f in filenames:
         idx+=1
         path = cleaned_path+f
         cleaned_img = np.reshape(np.array(Image.open(path), dtype=np.float32), [1, 256, 256, 3])
         noised_img = cleaned_img + np.random.normal(0, SIGMA, cleaned_img.shape)
         noised_img = np.clip(noised_img,0,255)
         # c_batch = cleaned_img.astype('float32')/255.
         # noised_img=util.random_noise(c_batch,mode='poisson')
         # noised_img = noised_img.astype('float32')*255.
         n_PSNR=n_SSIM=0
         for i in range(3):
             n_PSNR += psnr(np.uint8(cleaned_img[0, :, :, i]), np.uint8(noised_img[0, :, :, i]))
             n_SSIM += ssim(np.uint8(cleaned_img[0, :, :, i]), np.uint8(noised_img[0, :, :, i]))
         n_PSNR/=3.0
         n_SSIM/=3.0
         tn_PSNR+=n_PSNR
         tn_SSIM+=n_SSIM
         # print("n_psnr: %g, n_ssim: %g" % (n_PSNR, n_SSIM))
         [denoised_img] = self.sess.run([self.denoised_img], feed_dict={self.clean_img: cleaned_img, self.noised_img: noised_img, self.train_phase: False})
         PSNR=SSIM=0
         for i in range(3):
             PSNR += psnr(np.uint8(cleaned_img[0, :, :, i]), np.uint8(denoised_img[0, :, :, i]))
             SSIM += ssim(np.uint8(cleaned_img[0, :, :, i]), np.uint8(denoised_img[0, :, :, i]))
         PSNR/=3.0
         SSIM/=3.0
         tc_PSNR+=PSNR
         tc_SSIM+=SSIM
         # print("psnr: %g, ssim: %g" % (PSNR, SSIM))
         writer.writerow([idx,PSNR,n_PSNR,SSIM,n_SSIM])
     tn_PSNR/=idx
     tn_SSIM/=idx
     tc_PSNR/=idx
     tc_SSIM/=idx
     writer.writerow(["average",tc_PSNR,tn_PSNR,tc_SSIM,tn_SSIM])
def calculate_score(Y_out,Y_gt):

    Score=[]
    print("computing  Score")
    for i in range(Y_out.shape[0]):
        t1=psnr(Y_out[i],Y_gt[i])
        t2=ssim(Y_out[i],Y_gt[i],multichannel=True)
        Score.append([t1,t2])

    Score=np.array(Score)
    Score=np.mean(Score,axis=0)
    return  Score   
    def train(self):
        filepath = "./TrainingSet//"
        filenames = os.listdir(filepath)
        saver = tf.train.Saver()
        csvname = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + '.csv'
        csvfile = open(csvname, 'w', newline="")
        writer = csv.writer(csvfile, dialect='excel')
        writer.writerow(["Epoch", "Step", "Loss", "PSNR", "SSIM"])

        for epoch in range(EPOCHS):
            for i in range(filenames.__len__() // BATCH_SIZE):
                cleaned_batch = np.zeros([BATCH_SIZE, IMG_H, IMG_W, IMG_C],
                                         dtype=np.float32)
                for idx, filename in enumerate(
                        filenames[i * BATCH_SIZE:i * BATCH_SIZE + BATCH_SIZE]):
                    cleaned_batch[idx, :, :, :] = np.array(
                        Image.open(filepath + filename))
                # print(cleaned_batch.shape)
                noised_batch = cleaned_batch + np.random.normal(
                    0, SIGMA, cleaned_batch.shape)
                self.sess.run(self.Opt,
                              feed_dict={
                                  self.clean_img: cleaned_batch,
                                  self.noised_img: noised_batch,
                                  self.train_phase: True
                              })
                if i % 100 == 0:
                    [loss, denoised_img] = self.sess.run(
                        [self.L_cross, self.denoised_img],
                        feed_dict={
                            self.clean_img: cleaned_batch,
                            self.noised_img: noised_batch,
                            self.train_phase: False
                        })
                    PSNR = psnr(np.uint8(cleaned_batch[0, :, :, 0]),
                                np.uint8(denoised_img[0, :, :, 0]))
                    SSIM = ssim(np.uint8(cleaned_batch[0, :, :, 0]),
                                np.uint8(denoised_img[0, :, :, 0]))
                    print("Epoch: %d, Step: %d, Loss: %g, psnr: %g, ssim: %g" %
                          (epoch, i, loss, PSNR, SSIM))
                    data = [epoch, i, loss, PSNR, SSIM]
                    writer.writerow(data)
                    compared = np.concatenate(
                        (cleaned_batch[0, :, :, 0], noised_batch[0, :, :, 0],
                         denoised_img[0, :, :, 0]), 1)
                    Image.fromarray(
                        np.uint8(compared)).save("./TrainingResults//" +
                                                 str(epoch) + "_" + str(i) +
                                                 ".jpg")
                if i % 500 == 0:
                    saver.save(self.sess, "./save_para//FormResNet.ckpt")
            np.random.shuffle(filenames)
Пример #27
0
def compare(original, restored):
    """
        Side by side comparison of the original image and reconstruction effort with MSE, PSNR, and SSIM labels
    """
    if original.dtype != restored.dtype:
        warnings.warn(
            "The images are different data types. Converting both images to floats within 0-1 range"
        )
        original = normalize(original.astype(float), 0, 1)
        restored = normalize(restored.astype(float), 0, 1)

    fig, axes = plt.subplots(nrows=1,
                             ncols=2,
                             sharex=True,
                             sharey=True,
                             subplot_kw={'adjustable': 'box-forced'})
    ax = axes.ravel()

    mse_original = mse(original, original)
    psnr_original = psnr(original, original)
    ssim_original = ssim(original, original)

    mse_restored = mse(original, restored)
    psnr_restored = psnr(original, restored)
    ssim_restored = ssim(original, restored)

    label = 'MSE: {:.2f}, PSNR: {:.2F}, SSIM: {:.2f}'

    ax[0].imshow(original, cmap='gray', vmin=0, vmax=1)
    ax[0].set_xlabel(label.format(mse_original, psnr_original, ssim_original))
    ax[0].set_title('Original image')

    ax[1].imshow(restored, cmap='gray', vmin=0, vmax=1)
    ax[1].set_xlabel(label.format(mse_restored, psnr_restored, ssim_restored))
    ax[1].set_title('Restored image')

    plt.tight_layout()
    plt.show()
Пример #28
0
def calc_test_psnr(imGT, imSR, scale):
    if len(imGT.shape) > 2 and imGT.shape[2] > 1:
        imGT = sc.rgb2ycbcr(imGT)[..., 0]
    if len(imSR.shape) > 2 and imSR.shape[2] > 1:
        imSR = sc.rgb2ycbcr(imSR)[..., 0]

    imGT = shave(imGT, [scale, scale])
    imSR = shave(imSR, [scale, scale])

    imGT = imGT / 255.0
    imSR = imSR / 255.0
    cur_psnr = psnr(imGT, imSR)

    return cur_psnr
Пример #29
0
def calculate_psnr(X_test, Y_test):
    score = 0.0
    counter = 0

    for i in range(X_test.shape[0]):
        current_score = psnr(255 * normalize(np.float64(X_test[i, :, :, 0])),
                             255 * normalize(np.float64(Y_test[i, :, :, 0])),
                             data_range=255)
        if (np.isnan(current_score) == False):
            score += current_score
            counter += 1

    score = score / counter
    return score
Пример #30
0
    def inference_2(self, testset):

        np.random.seed(seed=0)  #### for reproduce

        total_psnr = 0
        total_ssim = 0
        test_total_count = 0
        start = time.time()
        im_list = glob('TestData/%s/*.png' % (testset))
        im_list = sorted(im_list)
        for i in range(len(im_list)):
            ###### convert to float [0, 1]
            im_path = im_list[i]
            im_gt = imageio.imread(im_path) / 255.0  ###### range[0,1]
            im_noise = im_gt + np.random.normal(0, self.args.sigma / 255.0,
                                                im_gt.shape)

            batch_images = im_noise[np.newaxis, :, :, np.newaxis]
            test_output_eval = self.sess.run(
                self.test_output, feed_dict={self.test_input:
                                             batch_images})  ## range [0,1]
            test_output_eval = test_output_eval[0, :, :, 0]
            im_out = np.clip(test_output_eval, 0, 1)

            ###### convert back to uint8 [0 255]
            im_noise = np.uint8(np.clip(im_noise, 0, 1) * 255)
            im_gt = np.uint8(im_gt * 255)
            im_out = np.uint8(im_out * 255)

            ### save noise
            temp_dir = '%s/%s' % (self.args.noise_dir, testset)
            if not os.path.exists(temp_dir):
                os.makedirs(temp_dir)
            save_path = '%s/%s' % (temp_dir, os.path.basename(im_path))
            imageio.imsave(save_path, im_noise)
            #### save output
            temp_dir = '%s/%s' % (self.args.results_dir, testset)
            if not os.path.exists(temp_dir):
                os.makedirs(temp_dir)
            save_path = '%s/%s' % (temp_dir, os.path.basename(im_path))
            imageio.imsave(save_path, im_out)

            total_psnr = total_psnr + psnr(im_gt, im_out)
            total_ssim = total_ssim + ssim(im_gt, im_out)

        print("average run time: ", (time.time() - start) / len(im_list))
        print('%s, %d ,psnr: %.2f, ssim: %.4f' %
              (testset, self.args.sigma, total_psnr / len(im_list),
               total_ssim / len(im_list)))
def metric_psnr(input_img, output_image, reference_image, **kwargs):
    r"""Compute the score of ``output_image`` regarding ``reference_image``
    with the *Peak Signal-to-Noise Ratio* (PSNR) metric.

    See [5]_ and [6]_ for more information.
    
    Parameters
    ----------
    input_img: 2D ndarray
        The RAW original image.
    output_image: 2D ndarray
        The cleaned image returned by the image cleanning algorithm to assess.
    reference_image: 2D ndarray
        The actual clean image (the best result that can be expected for the
        image cleaning algorithm).
    kwargs: dict
        Additional options.

    Returns
    -------
    float
        The score of the image cleaning algorithm for the given image.

    References
    ----------
    .. [5] http://scikit-image.org/docs/dev/api/skimage.measure.html#skimage.measure.compare_psnr
    .. [6] https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio
    """

    # Copy and cast images to prevent tricky bugs
    # See https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.astype.html#numpy-ndarray-astype
    output_image = output_image.astype('float64', copy=True)
    reference_image = reference_image.astype('float64', copy=True)

    # TODO: the following two lines may be wrong...
    output_image[np.isnan(output_image)] = 0
    reference_image[np.isnan(reference_image)] = 0

    #psnr_val = psnr(output_image, reference_image, dynamic_range=1e3)
    psnr_val = psnr(output_image, reference_image, data_range=1e3)

    return float(psnr_val)
Пример #32
0
     # x_target_gray = rgb2gray((x_target_list[i]/127.5-1).clip(min=-1,max=1))
     # gray image, [0,1]
     # G_gray = rgb2gray((G_list[i]).clip(min=0,max=255))
     # x_target_gray = rgb2gray((x_target_list[i]).clip(min=0,max=255))
     # ssim_G_x.append(ssim(G_gray, x_target_gray, data_range=x_target_gray.max()-x_target_gray.min(), multichannel=False))
     # psnr_G_x.append(psnr(im_true=x_target_gray, im_test=G_gray, data_range=x_target_gray.max()-x_target_gray.min())) 
     # L1_mean_G_x.append(l1_mean_dist(G_gray, x_target_gray))
     # L2_mean_G_x.append(l2_mean_dist(G_gray, x_target_gray))
     
     # color image
     # ssim_G_x.append(ssim(G_list[i], x_target_list[i], multichannel=True))
     masked_G_array = np.uint8(mask_target_list[i][:,:,np.newaxis]/255.*G_list[i])
     masked_x_target_array = np.uint8(mask_target_list[i][:,:,np.newaxis]/255.*x_target_list[i])
     ssim_G_x.append(ssim(masked_G_array, masked_x_target_array, multichannel=True))
     
     psnr_G_x.append(psnr(im_true=masked_x_target_array, im_test=masked_G_array))
     L1_mean_G_x.append(l1_mean_dist(masked_G_array, masked_x_target_array))
     L2_mean_G_x.append(l2_mean_dist(masked_G_array, masked_x_target_array))
 # pdb.set_trace()
 ssim_G_x_mean = np.mean(ssim_G_x)
 ssim_G_x_std = np.std(ssim_G_x)
 psnr_G_x_mean = np.mean(psnr_G_x)
 psnr_G_x_std = np.std(psnr_G_x)
 L1_G_x_mean = np.mean(L1_mean_G_x)
 L1_G_x_std = np.std(L1_mean_G_x)
 L2_G_x_mean = np.mean(L2_mean_G_x)
 L2_G_x_std = np.std(L2_mean_G_x)
 print('ssim_G_x_mean: %f\n' % ssim_G_x_mean)
 print('ssim_G_x_std: %f\n' % ssim_G_x_std)
 print('psnr_G_x_mean: %f\n' % psnr_G_x_mean)
 print('psnr_G_x_std: %f\n' % psnr_G_x_std)
Пример #33
0
def test_PSNR_vs_IPOL():
    # Tests vs. imdiff result from the following IPOL article and code:
    # http://www.ipol.im/pub/art/2011/g_lmii/
    p_IPOL = 22.4497
    p = psnr(cam, cam_noisy)
    assert_almost_equal(p, p_IPOL, decimal=4)
Пример #34
0
 L2_mean_G_x = []
 # x_0_255 = utils_wgan.unprocess_image(x_fixed, 127.5, 127.5)
 x_0_255 = x_target_list
 for i in xrange(N):
     # G_gray = rgb2gray((G_list[i]/127.5-1).clip(min=-1,max=1))
     # x_target_gray = rgb2gray((x_target_list[i]/127.5-1).clip(min=-1,max=1))
     # gray image, [0,1]
     # G_gray = rgb2gray((G_list[i]).clip(min=0,max=255))
     # x_target_gray = rgb2gray((x_target_list[i]).clip(min=0,max=255))
     # ssim_G_x.append(ssim(G_gray, x_target_gray, data_range=x_target_gray.max()-x_target_gray.min(), multichannel=False))
     # psnr_G_x.append(psnr(im_true=x_target_gray, im_test=G_gray, data_range=x_target_gray.max()-x_target_gray.min()))
     # color image
     G_gray = G_list[i]
     x_target_gray = x_target_list[i]
     ssim_G_x.append(ssim(G_list[i], x_target_list[i], multichannel=True))
     psnr_G_x.append(psnr(im_true=x_target_gray, im_test=G_gray))
     L1_mean_G_x.append(l1_mean_dist(G_gray, x_target_gray))
     L2_mean_G_x.append(l2_mean_dist(G_gray, x_target_gray))
 # pdb.set_trace()
 ssim_G_x_mean = np.mean(ssim_G_x)
 ssim_G_x_std = np.std(ssim_G_x)
 psnr_G_x_mean = np.mean(psnr_G_x)
 psnr_G_x_std = np.std(psnr_G_x)
 L1_G_x_mean = np.mean(L1_mean_G_x)
 L1_G_x_std = np.std(L1_mean_G_x)
 L2_G_x_mean = np.mean(L2_mean_G_x)
 L2_G_x_std = np.std(L2_mean_G_x)
 print('ssim_G_x_mean: %f\n' % ssim_G_x_mean)
 print('ssim_G_x_std: %f\n' % ssim_G_x_std)
 print('psnr_G_x_mean: %f\n' % psnr_G_x_mean)
 print('psnr_G_x_std: %f\n' % psnr_G_x_std)
Пример #35
0
def test_PSNR_float():
    p_uint8 = psnr(cam, cam_noisy)
    p_float64 = psnr(cam/255., cam_noisy/255., dynamic_range=1)
    assert_almost_equal(p_uint8, p_float64, decimal=5)