Пример #1
0
def valid_sam(test_loader, model):
    psnr_epoch = 0
    ssim_epoch = 0
    with torch.no_grad():
        for iteration, (HR_left, LR_left, LR_right,
                        LR_left_x4) in enumerate(test_loader):
            LR_left_RGB, HR_left_RGB = LR_left_x4 / 255, HR_left / 255
            LR_left, LR_right, HR_left = rgb2y(LR_left) / 255, rgb2y(
                LR_right) / 255, rgb2y(HR_left) / 255
            input_l, input_r, target = Variable(LR_left), Variable(
                LR_right), Variable(HR_left_RGB)
            if opt.cuda:
                input_l = input_l.cuda()
                input_r = input_r.cuda()
                target = target.cuda()
                LR_left_RGB = LR_left_RGB.cuda()
            HR, _, _, _ = model(input_l, input_r)
            HR = img_transfer(LR_left_RGB, HR)
            HR = torch.clamp(HR, 0, 1)
            SR_left_np = np.array(
                torch.squeeze(HR[:, :, :, 64:].data.cpu(), 0).permute(1, 2, 0))
            HR_left_np = np.array(
                torch.squeeze(target[:, :, :, 64:].data.cpu(),
                              0).permute(1, 2, 0))
            PSNR = measure.compare_psnr(HR_left_np, SR_left_np)
            SSIM = measure.compare_ssim(HR_left_np,
                                        SR_left_np,
                                        multichannel=True)
            psnr_epoch = psnr_epoch + PSNR
            ssim_epoch = ssim_epoch + SSIM
        print("===> SRDenseNet_SAM Avg. PSNR: {:.8f} dB, Avg. SSIM: {:.8f}".
              format(psnr_epoch / (iteration + 1),
                     ssim_epoch / (iteration + 1)))
Пример #2
0
def valid(test_loader, model):
    ssim_epoch = 0
    psnr_epoch = 0
    with torch.no_grad():
        for iteration, (HR_left, LR_left, _,
                        LR_left_x4) in enumerate(test_loader):
            LR_left_rgb = LR_left_x4 / 255
            LR_left, HR_left = rgb2y(LR_left) / 255, HR_left / 255
            input_l, target_l = Variable(LR_left), Variable(HR_left)
            if opt.cuda:
                input_l = input_l.cuda()
                target_l = target_l.cuda()
                LR_left_rgb = LR_left_rgb.cuda()
            _, HR_l = model(input_l)
            HR_l = img_transfer(LR_left_rgb, HR_l)
            SR_left_np = np.array(
                torch.squeeze(HR_l[:, :, :, 64:].data.cpu(),
                              0).permute(1, 2, 0))
            HR_left_np = np.array(
                torch.squeeze(target_l[:, :, :, 64:].data.cpu(),
                              0).permute(1, 2, 0))
            PSNR = measure.compare_psnr(HR_left_np, SR_left_np)
            SSIM = measure.compare_ssim(HR_left_np,
                                        SR_left_np,
                                        multichannel=True)
            psnr_epoch = psnr_epoch + PSNR
            ssim_epoch = ssim_epoch + SSIM
        print("===> LapSRN Avg. PSNR: {:.8f} dB, Avg. SSIM: {:.8f}".format(
            psnr_epoch / (iteration + 1), ssim_epoch / (iteration + 1)))
Пример #3
0
            y_ = tf.placeholder(tf.float32, [1, None, None, 3])
            h_ = tf.placeholder(tf.int32)
            w_ = tf.placeholder(tf.int32)

            # Remove boundaries (4px) from the produced and target images

            if RGB:
                output_crop_ = tf.clip_by_value(
                    tf.image.crop_to_bounding_box(out_, 4, 4, h_, w_), 0.0,
                    1.0)
                target_crop_ = tf.clip_by_value(
                    tf.image.crop_to_bounding_box(y_, 4, 4, h_, w_), 0.0, 1.0)
            else:
                output_crop_ = tf.clip_by_value(
                    tf.image.crop_to_bounding_box(utils.rgb2y(out_), 4, 4, h_,
                                                  w_), 0.0, 1.0)
                target_crop_ = tf.clip_by_value(
                    tf.image.crop_to_bounding_box(utils.rgb2y(y_), 4, 4, h_,
                                                  w_), 0.0, 1.0)

            psnr_ = tf.image.psnr(output_crop_, target_crop_,
                                  max_val=1.0)  # psnr
            ssim_ = tf.image.ssim(output_crop_, target_crop_,
                                  max_val=1.0)  # ssim

            print("Computing PSNR/SSIM scores....")

            ssim_score = 0.0
            psnr_score = 0.0
            validation_images = os.listdir(test_data_dir + dataset)
def run_testing():
    """Test denoising model."""
    params = {}
    params['kernel_size'] = FLAGS.kernel_size
    params['num_kernels'] = FLAGS.num_kernels
    params['num_layers'] = FLAGS.num_layers
    params['res'] = FLAGS.res
    params['remove_border_size'] = FLAGS.remove_border_size

    if FLAGS.noise_type == 'gaussian':
        if (FLAGS.noise_sigma is None):
            raise ValueError('please set noise sigma value')
        FLAGS.log_dir += 'gaussian_sigma_%d/' % (FLAGS.noise_sigma)
        ckpt = './models/model_sigma_%d.ckpt' % FLAGS.noise_sigma
    elif FLAGS.noise_type == 'poisson':
        if (FLAGS.noise_peak is None):
            raise ValueError('please set noise peak value')
        FLAGS.log_dir += 'poisson_peak_%d/' % (FLAGS.noise_peak)
        ckpt = './models/model_peak_%d.ckpt' % FLAGS.noise_peak
    else:
        print('Noise type not supported!!!')
        return

    if not os.path.isdir(FLAGS.log_dir):
        os.makedirs(FLAGS.log_dir)

    # Tell TensorFlow that the model will be built into the default Graph.
    with tf.Graph().as_default():

        gt_img = tf.placeholder(tf.float32,
                                shape=(FLAGS.batch_size, None, None, 1))
        noisy_img = tf.placeholder(tf.float32,
                                   shape=(FLAGS.batch_size, None, None, 1))

        data = {}
        data['gt_img'] = gt_img
        data['input_img'] = noisy_img

        # Pad the arrays so we can crop them later
        if FLAGS.pad_images:
            padding = [
                [0, 0],
                [params['remove_border_size'], params['remove_border_size']],
                [params['remove_border_size'], params['remove_border_size']],
                [0, 0]
            ]
            data['gt_img'] = tf.pad(data['gt_img'], padding, "SYMMETRIC")
            data['input_img'] = tf.pad(data['input_img'], padding, "SYMMETRIC")

        # Build a Graph that computes predictions from the inference model.
        model = models.basic_denoise_model(data=data, params=params)

        # The op for initializing the variables.
        init_op = tf.group(tf.initialize_all_variables(),
                           tf.initialize_local_variables())

        # Create a session for running operations in the Graph.
        sess = tf.Session()

        saver = tf.train.Saver()

        # Initialize the variables
        print('restoring model ' + ckpt)
        sess.run(init_op)
        saver.restore(sess, ckpt)

        path = FLAGS.data_dir
        print('Looking for images in ' + path)
        img_list = [
            f for f in listdir(path)
            if re.search(r'[.]*\.%s$' % FLAGS.data_type, f)
        ]
        print('found %d files' % len(img_list))

        try:
            mse_list = []
            psnr_list = []
            img_name_list = []

            for img in img_list:

                # Get the data.
                print(
                    '-------------------------------------------------------')

                path = FLAGS.data_dir + '/' + img
                print(path)
                rgb_img = Image.open(path)
                rgb_img = np.ndarray((rgb_img.size[1], rgb_img.size[0], 3),
                                     'u1', rgb_img.tobytes())
                rgb_img = rgb_img.astype(np.float32)
                y_img = utils.rgb2y(rgb_img)

                gt = y_img
                gt = gt[:, :, 0]

                if FLAGS.noise_type == 'gaussian':
                    gt = gt.astype(np.float32) * (1.0 / 255.0) - 0.5
                    noisy = gt + np.random.normal(size=gt.shape) * float(
                        FLAGS.noise_sigma) / 255.0
                else:
                    max_val = np.amax(np.amax(gt))
                    gt = gt.astype(np.float32) * (1.0 / float(max_val)) - 0.5
                    img_peak = (0.5 + gt) * float(FLAGS.noise_peak)
                    noisy = utils.add_poiss_noise_image(img_peak).astype(
                        np.float32)
                    noisy = (noisy / float(FLAGS.noise_peak)) - 0.5

                gt = np.expand_dims(a=gt, axis=2)
                gt = np.expand_dims(a=gt, axis=0)
                noisy = np.expand_dims(a=noisy, axis=2)
                noisy = np.expand_dims(a=noisy, axis=0)

                feed_dict = {gt_img: gt, noisy_img: noisy}

                start_time = time.time()

                # Run the network
                (gt_image, input_image, output_image, mse,
                 psnr) = sess.run([
                     model.net['gt_img'], model.net['input_img'],
                     model.net['gen_output'], model.mse, model.psnr
                 ],
                                  feed_dict=feed_dict)

                psnr_list.append(psnr)
                mse_list.append(mse)
                duration = time.time() - start_time
                print('PSNR=%f %f[sec]' % (psnr, duration))

                input_image = np.array(np.clip((input_image + 0.5) * 255.0, 0,
                                               255),
                                       dtype=np.uint8)
                output_image = np.array(np.clip((output_image + 0.5) * 255.0,
                                                0, 255),
                                        dtype=np.uint8)

                img_name = img[0:-(len(FLAGS.data_type) + 1)]

                utils.save_gray_img(img=input_image[0, :, :, :],
                                    path=FLAGS.log_dir +
                                    '%s_noisy.png' % img_name,
                                    bit_depth=8)
                utils.save_gray_img(img=output_image[0, :, :, :],
                                    path=FLAGS.log_dir +
                                    '%s_denoised.png' % img_name,
                                    bit_depth=8)
                sio.savemat(
                    FLAGS.log_dir + img_name + '.mat', {
                        'gt_img': gt_image,
                        'input_img': input_image,
                        'denoised_img': output_image,
                        'psnr': psnr,
                        'mse': mse
                    })
            print(
                '------------------ Overall performance ---------------------')
            print('mean PSNR=%f' % np.mean(psnr_list))
            print('mean MSE=%f' % np.mean(mse_list))

        except tf.errors.OutOfRangeError:
            print('Error')
        finally:
            pass