コード例 #1
0
def test_wavelet_denoising_nd():
    rstate = np.random.RandomState(1234)
    for method in ['VisuShrink', 'BayesShrink']:
        for ndim in range(1, 5):
            # Generate a very simple test image
            if ndim < 3:
                img = 0.2*np.ones((128, )*ndim)
            else:
                img = 0.2*np.ones((16, )*ndim)
            img[(slice(5, 13), ) * ndim] = 0.8

            sigma = 0.1
            noisy = img + sigma * rstate.randn(*(img.shape))
            noisy = np.clip(noisy, 0, 1)

            # Mark H. 2018.08:
            #   The issue arises because when ndim in [1, 2]
            #   ``waverecn`` calls ``_match_coeff_dims``
            #   Which includes a numpy 1.15 deprecation.
            #   for larger number of dimensions _match_coeff_dims isn't called
            #   for some reason.
            anticipated_warnings = (PYWAVELET_ND_INDEXING_WARNING
                                    if ndim < 3 else None)
            with expected_warnings([anticipated_warnings]):
                # Verify that SNR is improved with internally estimated sigma
                denoised = restoration.denoise_wavelet(noisy, method=method)
            psnr_noisy = compare_psnr(img, noisy)
            psnr_denoised = compare_psnr(img, denoised)
            assert_(psnr_denoised > psnr_noisy)
コード例 #2
0
ファイル: test_denoise.py プロジェクト: dfcollin/scikit-image
def test_wavelet_denoising():
    for img, multichannel in [(astro_gray, False), (astro, True)]:
        sigma = 0.1
        noisy = img + sigma * np.random.randn(*(img.shape))
        noisy = np.clip(noisy, 0, 1)

        # Verify that SNR is improved when true sigma is used
        denoised = restoration.denoise_wavelet(noisy, sigma=sigma,
                                               multichannel=multichannel)
        psnr_noisy = compare_psnr(img, noisy)
        psnr_denoised = compare_psnr(img, denoised)
        assert psnr_denoised > psnr_noisy

        # Verify that SNR is improved with internally estimated sigma
        denoised = restoration.denoise_wavelet(noisy,
                                               multichannel=multichannel)
        psnr_noisy = compare_psnr(img, noisy)
        psnr_denoised = compare_psnr(img, denoised)
        assert psnr_denoised > psnr_noisy

        # Test changing noise_std (higher threshold, so less energy in signal)
        res1 = restoration.denoise_wavelet(noisy, sigma=2*sigma,
                                           multichannel=multichannel)
        res2 = restoration.denoise_wavelet(noisy, sigma=sigma,
                                           multichannel=multichannel)
        assert (res1.sum()**2 <= res2.sum()**2)
コード例 #3
0
ファイル: test_denoise.py プロジェクト: ameya005/scikit-image
def test_wavelet_denoising_levels():
    rstate = np.random.RandomState(1234)
    ndim = 2
    N = 256
    wavelet = 'db1'
    # Generate a very simple test image
    img = 0.2*np.ones((N, )*ndim)
    img[[slice(5, 13), ] * ndim] = 0.8

    sigma = 0.1
    noisy = img + sigma * rstate.randn(*(img.shape))
    noisy = np.clip(noisy, 0, 1)

    denoised = restoration.denoise_wavelet(noisy, wavelet=wavelet)
    denoised_1 = restoration.denoise_wavelet(noisy, wavelet=wavelet,
                                             wavelet_levels=1)
    psnr_noisy = compare_psnr(img, noisy)
    psnr_denoised = compare_psnr(img, denoised)
    psnr_denoised_1 = compare_psnr(img, denoised_1)

    # multi-level case should outperform single level case
    assert_(psnr_denoised > psnr_denoised_1 > psnr_noisy)

    # invalid number of wavelet levels results in a ValueError
    max_level = pywt.dwt_max_level(np.min(img.shape),
                                   pywt.Wavelet(wavelet).dec_len)
    assert_raises(ValueError, restoration.denoise_wavelet, noisy,
                  wavelet=wavelet, wavelet_levels=max_level+1)
    assert_raises(ValueError, restoration.denoise_wavelet, noisy,
                  wavelet=wavelet, wavelet_levels=-1)
コード例 #4
0
def test_wavelet_threshold():
    rstate = np.random.RandomState(1234)

    img = astro_gray
    sigma = 0.1
    noisy = img + sigma * rstate.randn(*(img.shape))
    noisy = np.clip(noisy, 0, 1)

    # employ a single, user-specified threshold instead of BayesShrink sigmas
    with expected_warnings([PYWAVELET_ND_INDEXING_WARNING]):
        denoised = _wavelet_threshold(noisy, wavelet='db1', method=None,
                                      threshold=sigma)
    psnr_noisy = compare_psnr(img, noisy)
    psnr_denoised = compare_psnr(img, denoised)
    assert_(psnr_denoised > psnr_noisy)

    # either method or threshold must be defined
    with testing.raises(ValueError):
        _wavelet_threshold(noisy, wavelet='db1', method=None, threshold=None)

    # warns if a threshold is provided in a case where it would be ignored
    with expected_warnings(["Thresholding method ",
                            PYWAVELET_ND_INDEXING_WARNING]):
        _wavelet_threshold(noisy, wavelet='db1', method='BayesShrink',
                           threshold=sigma)
コード例 #5
0
def test_PSNR_dynamic_range_and_data_range():
    # Tests deprecation of "dynamic_range" in favor of "data_range"
    out1 = compare_psnr(cam/255., cam_noisy/255., data_range=1)
    with expected_warnings(
            '`dynamic_range` has been deprecated in favor of '
            '`data_range`. The `dynamic_range` keyword argument '
            'will be removed in v0.14'):
        out2 = compare_psnr(cam/255., cam_noisy/255., dynamic_range=1)
    assert_equal(out1, out2)
コード例 #6
0
ファイル: test_denoise.py プロジェクト: ameya005/scikit-image
def test_wavelet_threshold():
    rstate = np.random.RandomState(1234)

    img = astro_gray
    sigma = 0.1
    noisy = img + sigma * rstate.randn(*(img.shape))
    noisy = np.clip(noisy, 0, 1)

    # employ a single, uniform threshold instead of BayesShrink sigmas
    denoised = _wavelet_threshold(noisy, wavelet='db1', threshold=sigma)
    psnr_noisy = compare_psnr(img, noisy)
    psnr_denoised = compare_psnr(img, denoised)
    assert_(psnr_denoised > psnr_noisy)
コード例 #7
0
def test_denoise_nl_means_multichannel():
    # for true 3D data, 3D denoising is better than denoising as 2D+channels
    img = np.zeros((13, 10, 8))
    img[6, 4:6, 2:-2] = 1.
    sigma = 0.3
    imgn = img + sigma * np.random.randn(*img.shape)
    denoised_wrong_multichannel = restoration.denoise_nl_means(
        imgn, 3, 4, 0.6 * sigma, fast_mode=True, multichannel=True)
    denoised_ok_multichannel = restoration.denoise_nl_means(
        imgn, 3, 4, 0.6 * sigma, fast_mode=True, multichannel=False)
    psnr_wrong = compare_psnr(img, denoised_wrong_multichannel)
    psnr_ok = compare_psnr(img, denoised_ok_multichannel)
    assert_(psnr_ok > psnr_wrong)
コード例 #8
0
def test_wavelet_denoising():
    rstate = np.random.RandomState(1234)

    # version with one odd-sized dimension
    astro_gray_odd = astro_gray[:, :-1]
    astro_odd = astro[:, :-1]

    for img, multichannel, convert2ycbcr in [(astro_gray, False, False),
                                             (astro_gray_odd, False, False),
                                             (astro_odd, True, False),
                                             (astro_odd, True, True)]:
        sigma = 0.1
        noisy = img + sigma * rstate.randn(*(img.shape))
        noisy = np.clip(noisy, 0, 1)

        # Verify that SNR is improved when true sigma is used
        with expected_warnings([PYWAVELET_ND_INDEXING_WARNING]):
            denoised = restoration.denoise_wavelet(noisy, sigma=sigma,
                                                   multichannel=multichannel,
                                                   convert2ycbcr=convert2ycbcr)
        psnr_noisy = compare_psnr(img, noisy)
        psnr_denoised = compare_psnr(img, denoised)
        assert_(psnr_denoised > psnr_noisy)

        # Verify that SNR is improved with internally estimated sigma
        with expected_warnings([PYWAVELET_ND_INDEXING_WARNING]):
            denoised = restoration.denoise_wavelet(noisy,
                                                   multichannel=multichannel,
                                                   convert2ycbcr=convert2ycbcr)
        psnr_noisy = compare_psnr(img, noisy)
        psnr_denoised = compare_psnr(img, denoised)
        assert_(psnr_denoised > psnr_noisy)

        # SNR is improved less with 1 wavelet level than with the default.
        denoised_1 = restoration.denoise_wavelet(noisy,
                                                 multichannel=multichannel,
                                                 wavelet_levels=1,
                                                 convert2ycbcr=convert2ycbcr)
        psnr_denoised_1 = compare_psnr(img, denoised_1)
        assert_(psnr_denoised > psnr_denoised_1)
        assert_(psnr_denoised_1 > psnr_noisy)

        # Test changing noise_std (higher threshold, so less energy in signal)
        with expected_warnings([PYWAVELET_ND_INDEXING_WARNING]):
            res1 = restoration.denoise_wavelet(noisy, sigma=2 * sigma,
                                               multichannel=multichannel)
        with expected_warnings([PYWAVELET_ND_INDEXING_WARNING]):
            res2 = restoration.denoise_wavelet(noisy, sigma=sigma,
                                               multichannel=multichannel)
        assert_(np.sum(res1**2) <= np.sum(res2**2))
コード例 #9
0
ファイル: test_denoise.py プロジェクト: dfcollin/scikit-image
def test_wavelet_denoising_nd():
    for ndim in range(1, 5):
        # Generate a very simple test image
        img = 0.2*np.ones((16, )*ndim)
        img[[slice(5, 13), ] * ndim] = 0.8

        sigma = 0.1
        noisy = img + sigma * np.random.randn(*(img.shape))
        noisy = np.clip(noisy, 0, 1)

        # Verify that SNR is improved with internally estimated sigma
        denoised = restoration.denoise_wavelet(noisy)
        psnr_noisy = compare_psnr(img, noisy)
        psnr_denoised = compare_psnr(img, denoised)
        assert psnr_denoised > psnr_noisy
コード例 #10
0
def test_PSNR_float():
    p_uint8 = compare_psnr(cam, cam_noisy)
    p_float64 = compare_psnr(cam / 255., cam_noisy / 255.,
                             data_range=1)
    assert_almost_equal(p_uint8, p_float64, decimal=5)

    # mixed precision inputs
    p_mixed = compare_psnr(cam / 255., np.float32(cam_noisy / 255.),
                           data_range=1)
    assert_almost_equal(p_mixed, p_float64, decimal=5)

    # mismatched dtype results in a warning if data_range is unspecified
    with expected_warnings(['Inputs have mismatched dtype']):
        p_mixed = compare_psnr(cam / 255., np.float32(cam_noisy / 255.))
    assert_almost_equal(p_mixed, p_float64, decimal=5)
コード例 #11
0
def test_denoise_nl_means_3d():
    img = np.zeros((12, 12, 8))
    img[5:-5, 5:-5, 2:-2] = 1.
    sigma = 0.3
    imgn = img + sigma * np.random.randn(*img.shape)
    psnr_noisy = compare_psnr(img, imgn)
    for s in [sigma, 0]:
        denoised = restoration.denoise_nl_means(imgn, 3, 4, h=0.75 * sigma,
                                                fast_mode=True,
                                                multichannel=False, sigma=s)
        # make sure noise is reduced
        assert_(compare_psnr(img, denoised) > psnr_noisy)
        denoised = restoration.denoise_nl_means(imgn, 3, 4, h=0.75 * sigma,
                                                fast_mode=False,
                                                multichannel=False, sigma=s)
        # make sure noise is reduced
        assert_(compare_psnr(img, denoised) > psnr_noisy)
コード例 #12
0
def test_wavelet_denoising_levels():
    rstate = np.random.RandomState(1234)
    ndim = 2
    N = 256
    wavelet = 'db1'
    # Generate a very simple test image
    img = 0.2*np.ones((N, )*ndim)
    img[(slice(5, 13), ) * ndim] = 0.8

    sigma = 0.1
    noisy = img + sigma * rstate.randn(*(img.shape))
    noisy = np.clip(noisy, 0, 1)

    with expected_warnings([PYWAVELET_ND_INDEXING_WARNING]):
        denoised = restoration.denoise_wavelet(noisy, wavelet=wavelet)
    denoised_1 = restoration.denoise_wavelet(noisy, wavelet=wavelet,
                                             wavelet_levels=1)
    psnr_noisy = compare_psnr(img, noisy)
    psnr_denoised = compare_psnr(img, denoised)
    psnr_denoised_1 = compare_psnr(img, denoised_1)

    # multi-level case should outperform single level case
    assert_(psnr_denoised > psnr_denoised_1 > psnr_noisy)

    # invalid number of wavelet levels results in a ValueError or UserWarning
    max_level = pywt.dwt_max_level(np.min(img.shape),
                                   pywt.Wavelet(wavelet).dec_len)
    if Version(pywt.__version__) < '1.0.0':
        # exceeding max_level raises a ValueError in PyWavelets 0.4-0.5.2
        with testing.raises(ValueError):
            with expected_warnings([PYWAVELET_ND_INDEXING_WARNING]):
                restoration.denoise_wavelet(
                    noisy, wavelet=wavelet, wavelet_levels=max_level + 1)
    else:
        # exceeding max_level raises a UserWarning in PyWavelets >= 1.0.0
        with expected_warnings([
                'all coefficients will experience boundary effects']):
            restoration.denoise_wavelet(
                noisy, wavelet=wavelet, wavelet_levels=max_level + 1)

    with testing.raises(ValueError):
        with expected_warnings([PYWAVELET_ND_INDEXING_WARNING]):
            restoration.denoise_wavelet(
                noisy,
                wavelet=wavelet, wavelet_levels=-1)
コード例 #13
0
ファイル: main.py プロジェクト: bennjaminn64/DnCNN-keras
def test(model):
    
    print('Start to test on {}'.format(args.test_dir))
    out_dir = save_dir + args.test_dir.split('/')[-1] + '/'
    if not os.path.exists(out_dir):
            os.mkdir(out_dir)
            
    name = []
    psnr = []
    ssim = []
    file_list = glob.glob('{}/*.png'.format(args.test_dir))
    for file in file_list:
        # read image
        img_clean = np.array(Image.open(file), dtype='float32') / 255.0
        img_test = img_clean + np.random.normal(0, args.sigma/255.0, img_clean.shape)
        img_test = img_test.astype('float32')
        # predict
        x_test = img_test.reshape(1, img_test.shape[0], img_test.shape[1], 1) 
        y_predict = model.predict(x_test)
        # calculate numeric metrics
        img_out = y_predict.reshape(img_clean.shape)
        img_out = np.clip(img_out, 0, 1)
        psnr_noise, psnr_denoised = compare_psnr(img_clean, img_test), compare_psnr(img_clean, img_out)
        ssim_noise, ssim_denoised = compare_ssim(img_clean, img_test), compare_ssim(img_clean, img_out)
        psnr.append(psnr_denoised)
        ssim.append(ssim_denoised)
        # save images
        filename = file.split('/')[-1].split('.')[0]    # get the name of image file
        name.append(filename)
        img_test = Image.fromarray((img_test*255).astype('uint8'))
        img_test.save(out_dir+filename+'_sigma'+'{}_psnr{:.2f}.png'.format(args.sigma, psnr_noise))
        img_out = Image.fromarray((img_out*255).astype('uint8')) 
        img_out.save(out_dir+filename+'_psnr{:.2f}.png'.format(psnr_denoised))
    
    psnr_avg = sum(psnr)/len(psnr)
    ssim_avg = sum(ssim)/len(ssim)
    name.append('Average')
    psnr.append(psnr_avg)
    ssim.append(ssim_avg)
    print('Average PSNR = {0:.2f}, SSIM = {1:.2f}'.format(psnr_avg, ssim_avg))
    
    pd.DataFrame({'name':np.array(name), 'psnr':np.array(psnr), 'ssim':np.array(ssim)}).to_csv(out_dir+'/metrics.csv', index=True)
コード例 #14
0
ファイル: test_denoise.py プロジェクト: Cadair/scikit-image
def test_wavelet_denoising_nd():
    rstate = np.random.RandomState(1234)
    for method in ['VisuShrink', 'BayesShrink']:
        for ndim in range(1, 5):
            # Generate a very simple test image
            if ndim < 3:
                img = 0.2*np.ones((128, )*ndim)
            else:
                img = 0.2*np.ones((16, )*ndim)
            img[[slice(5, 13), ] * ndim] = 0.8

            sigma = 0.1
            noisy = img + sigma * rstate.randn(*(img.shape))
            noisy = np.clip(noisy, 0, 1)

            # Verify that SNR is improved with internally estimated sigma
            denoised = restoration.denoise_wavelet(noisy, method=method)
            psnr_noisy = compare_psnr(img, noisy)
            psnr_denoised = compare_psnr(img, denoised)
            assert_(psnr_denoised > psnr_noisy)
コード例 #15
0
def test_denoise_nl_means_2d_multichannel():
    # reduce image size because nl means is slow
    img = np.copy(astro[:50, :50])
    img = np.concatenate((img, ) * 2, )  # 6 channels

    # add some random noise
    sigma = 0.1
    imgn = img + sigma * np.random.standard_normal(img.shape)
    imgn = np.clip(imgn, 0, 1)
    for fast_mode in [True, False]:
        for s in [sigma, 0]:
            for n_channels in [2, 3, 6]:
                psnr_noisy = compare_psnr(img[..., :n_channels],
                                          imgn[..., :n_channels])
                denoised = restoration.denoise_nl_means(imgn[..., :n_channels],
                                                        3, 5, h=0.75 * sigma,
                                                        fast_mode=fast_mode,
                                                        multichannel=True,
                                                        sigma=s)
                psnr_denoised = compare_psnr(denoised[..., :n_channels],
                                             img[..., :n_channels])
                # make sure noise is reduced
                assert_(psnr_denoised > psnr_noisy)
コード例 #16
0
ファイル: image_metrics.py プロジェクト: echopen/kit-soft
def get_denoise_metrics(input, output, report):
    image_file_name1 = input
    image_file_name2 = output

    image_name1 = io.imread(image_file_name1)
    image_name2 = io.imread(image_file_name2)

    # estimate the standard deiviation of the images

    std_1 = numpy.std(numpy.std(numpy.array(image_name1)))
    std_2 = numpy.std(numpy.std(numpy.array(image_name2)))

    print("std is %2.10f" % std_1)

    # print ("Standard deviation of the images are"%(std_1,std_2))

    # estimate the peak signal to noise ratio (PSNR) between the image

    peak_signal_to_noise_ratio = measure.compare_psnr(image_name1, image_name2)

    print("Peak signal to noise ratio is %s" % peak_signal_to_noise_ratio)

    # estimate the mean square error between the images

    mse = measure.compare_mse(image_name1, image_name2)

    print("Mean square error between the images is %s" % mse)

    # estimate the normalised root mean square error between the images

    rmse = measure.compare_nrmse(image_name1, image_name2)

    print("Normalised root mean square error between the images is %s" % rmse)

    resp = open(report, 'w')
    resp.write("std1 is %2.10f \n" % std_1)
    resp.write("std2 is %2.10f \n" % std_2)
    resp.write(
        "Peak signal to noise ratio is %s \n" %
        peak_signal_to_noise_ratio)
    resp.write("Mean square error between the images is %s \n" % mse)
    resp.write(
        "Normalised root mean squre error between the images is %s \n" %
        rmse)
    resp.close()
コード例 #17
0
ファイル: metrics.py プロジェクト: echopen/kit-soft
def run_metrics(image_file_name1,image_file_name2 ):
    image_name1 = io.imread(image_file_name1)
    image_name2 = io.imread(image_file_name2)
    peak_signal_to_noise_ratio = measure.compare_psnr (image_name1,image_name2)
    print ("PSNR Peak signal to noise ratio is %s"%peak_signal_to_noise_ratio)
    mse = measure.compare_mse(image_name1,image_name2)
    print  ("MSE Mean square error between the images is %s"%mse)
    rmse = measure.compare_nrmse(image_name1,image_name2)
    print  ("RMSE Normalised root mean square error between the images is %s"%rmse)
    ssim = measure.compare_ssim(image_name1,image_name2, multichannel=True)
    print ("SSIM Structural Similarity Index is %s"%ssim)
    #[M3,M4] = minkowski_distance(image_name1,image_name2)
    #print ("Minkowski distance is %s %s"%(M3,M4))
    #AD = average_difference(image_name1,image_name2)
    #print ("AD Average difference is %s"%AD)
    #SC = structural_content(image_name1,image_name2)
    #print ("SC Structural Content is %s"%SC)
    #NK = normalised_cross_correlation(image_name1,image_name2)
    #print ("NK normalised cross correlation is %s"%NK)
    #MD = maximum_difference(image_name1,image_name2)
    #print ("Maximum difference is %s"%MD)
    return {'peaktonoise':peak_signal_to_noise_ratio ,'mse': mse, 'rmse': rmse, 'ssim':ssim,'score':peak_signal_to_noise_ratio}
コード例 #18
0
psnr_sum = 0
ssim_sum = 0
for i in range(14):
    image = scipy.misc.imread(
        '/home/wangyang/桌面/SRGAN/samples/evaluate/set14/%d/valid_gen%d.png' %
        (i, i))
    image_true = scipy.misc.imread(
        '/home/wangyang/桌面/SRGAN/samples/evaluate/set14/%d/valid_hr%d.png' %
        (i, i))
    y_image = convert_rgb_to_y(image, False)
    y_image_true = convert_rgb_to_y(image_true, False)
    print(np.array(y_image).shape)
    print(np.array(y_image_true).shape)
    ssim = measure.compare_ssim(np.array(y_image_true),
                                np.array(y_image),
                                win_size=11,
                                gradient=False,
                                multichannel=True,
                                gaussian_weights=True,
                                full=False,
                                dynamic_range=255)
    psnr = measure.compare_psnr(np.array(y_image_true), np.array(y_image),
                                True, 255)
    ssim_sum = ssim_sum + ssim
    psnr_sum = psnr_sum + psnr
    print(ssim)
    print(psnr)
print("average ssim is %4.4f" % (ssim_sum / 14.0))
print("average psnr is %4.4f" % (psnr_sum / 14.0))
コード例 #19
0
def test_PSNR_errors():
    # shape mismatch
    with testing.raises(ValueError):
        compare_psnr(cam, cam[:-1, :])
コード例 #20
0
    def train(self, config, load_model_epoch, indx_, original_img, psrns,
              ssims, ssims_with_data_range):

        self.load(config.checkpoint_dir, load_model_epoch)

        config.is_train = False

        nx, ny, original_shape = input_setup(config)

        #print(" nx, ny, original_shape:",  nx, ny, original_shape)

        data_dir = checkpoint_dir(config)

        #print("data_dir:", data_dir)
        input_, label_ = read_data(data_dir)

        print("Now Start Testing...")

        result = self.pred.eval({self.images: input_})

        #print("result:", result.shape)

        #print(label_[1] - result[1])
        image = merge(result, [nx, ny], self.c_dim)

        #print("image after merge:", image.shape)
        '''print("[nx, ny]:", [nx, ny])

        print("original_shape:", original_shape)
            
        print(type(image), type(original_shape[0]), type(original_shape[1]))
        '''
        cropped_img = crop_center(image, original_shape[0], original_shape[1])

        #print("cropped_img:", cropped_img.shape)

        #image_LR = merge(input_, [nx, ny], self.c_dim)
        #checkimage(image_LR)

        #imsave(image, config.result_dir + '/result.png', config)

        #imsave(cropped_img, config.result_dir + '/result_crop.png', config)

        imsave(
            cropped_img, config.result_dir + '/srcnn-' + str(indx_) +
            '-epoch-' + str(load_model_epoch) + '.png', config)

        cropped_img = cropped_img * 255.
        cropped_img = cropped_img.astype(np.uint8)

        print(original_img.shape, original_img.dtype)
        print(cropped_img.shape, cropped_img.dtype)

        try:
            psnr = measure.compare_psnr(original_img, cropped_img)
            psrns.append(psnr)
            print(indx_, ", psnr:", psnr)

            ssim = computeSSIM(
                config.test_img, config.result_dir + '/srcnn-' + str(indx_) +
                '-epoch-' + str(load_model_epoch) + '.png')

            print(indx_, ", ssim:", ssim)
            ssims.append(ssim)

            ssim_with_data_range = computeSSIM_WithDataRange(
                config.test_img, config.result_dir + '/srcnn-' + str(indx_) +
                '-epoch-' + str(load_model_epoch) + '.png')

            print(indx_, ", ssim_with_data_range:", ssim_with_data_range)
            ssims_with_data_range.append(ssim_with_data_range)

            #os.remove(config.result_dir + '/srcnn-' + str(indx_) + '-epoch-' + str(load_model_epoch) + '.png')

        except:
            print("indx_:", indx_)
            print("Unexpected error while computing psnr / ssim:",
                  sys.exc_info()[0])
コード例 #21
0
                np.arange(vol_dims[2])))
interpFunc = sciint.interpolate.RegularGridInterpolator((np.arange(
    vol_dims[0]), np.arange(vol_dims[1]), np.linspace(0, vol_dims[2], 3)),
                                                        lo_vol[:, :, 2::4])
samp_grid = np.moveaxis(samp_grid, 0, -1)
int_vol = interpFunc(samp_grid, method='linear')
int_vol = np.swapaxes(int_vol, 0, 1)

vol_L2 = np.sum(np.square(hi_vol - out_vol))
lo_MSE = sk.compare_mse(hi_vol, lo_vol)
out_MSE = sk.compare_mse(hi_vol, out_vol)
int_MSE = sk.compare_mse(hi_vol, int_vol)
# lo_L2 = np.sum(np.square(hi_vol - lo_vol))
# out_L2 = np.sum(np.square(hi_vol - out_vol))
# int_L2 = np.sum(np.square(hi_vol - int_vol))
lo_pSNR = sk.compare_psnr(hi_vol, lo_vol)
out_pSNR = sk.compare_psnr(hi_vol, out_vol)
int_pSNR = sk.compare_psnr(hi_vol, int_vol)
lo_SSIM = sk.compare_ssim(hi_vol, lo_vol)
out_SSIM = sk.compare_ssim(hi_vol, out_vol)
int_SSIM = sk.compare_ssim(hi_vol, int_vol)

for idx in range(0, vol_dims[2]):
    lo_L2 = np.sum(np.square(hi_vol[:, :, idx] - lo_vol[:, :, idx]))
    out_L2 = np.sum(np.square(hi_vol[:, :, idx] - out_vol[:, :, idx]))
    int_L2 = np.sum(np.square(hi_vol[:, :, idx] - int_vol[:, :, idx]))

    print(hi_vol[:, :, idx].max(), hi_vol[:, :, idx].min())
    print(lo_vol[:, :, idx].max(), lo_vol[:, :, idx].min())
    print(out_vol[:, :, idx].max(), out_vol[:, :, idx].min())
    print("\n")
コード例 #22
0
ファイル: train.py プロジェクト: juingzhou/SAM
def cal_psnr(img1, img2):
    img1 = img1.cpu()
    img2 = img2.cpu()
    img1_np = img1.detach().numpy()
    img2_np = img2.detach().numpy()
    return measure.compare_psnr(img1_np, img2_np)
コード例 #23
0
                lr_l = lr_loss(ds_in_tensor, lr_tensor)
                l2_l = l2_loss(in_tensor, org_tensor)
                l = lr_l + LAMBDA * l2_l
                l.backward()

                gradient = in_tensor.grad * LEARING_RATE
                if ATTENTION:
                    gradient = gradient * attentioner(torch.abs(gradient) / torch.max(torch.abs(gradient)))
                in_tensor = in_tensor.data.sub_(gradient)

                in_tensor.requires_grad = True

                sr_img = torch.clamp(torch.round(in_tensor), 0., 255.).detach().cpu().numpy().astype(np.uint8)
                dsr_img = torch.clamp(torch.round(ds_in_tensor), 0., 255.).detach().cpu().numpy().astype(np.uint8)
                # sr_img = np.moveaxis(sr_img.reshape(np_ds.shape[1:]), 0, -1)
                psnr = compare_psnr(sr_img[0], hr_img)
                lr_psnr = compare_psnr(dsr_img[0], lr_img)
                report = 'Image Name: {} | Epoch: {:03d}| PSNR vHR: {:.4f} | LR-PSNR: {:.4f} |Time: {:.4f}'.format(IMG_NAME, epoch, psnr, lr_psnr,  time() - begin_time)
                if epoch==0:
                    print(report)
                    psnr0 = psnr
                    lr_psnr0 = lr_psnr
                elif epoch==NUM_EPOCH-1:
                    print(report)
                    psnrF = psnr
                    lr_psnrF = lr_psnr
                f.write(report + "\n")
            # psnrs.append(float(psnr(hr_l)))
                # sr_img = torch.clamp(torch.round(in_tensor), 0., 255.).detach().cpu().numpy().astype(np.int16)
                # sr_img = rgb2ycbcr(sr_img.reshape(sr_img.shape[1:]))[CLIP:-CLIP, CLIP:-CLIP, 0]
                # psnr_dict[IMG_NAME].append(float(psnr(hr_l)))
コード例 #24
0
 else:
     result_RAISR = im_result[region] * 255
     result_RAISR = result_RAISR.astype('uint8')
 '''
 # Filtered result
 im_result = im_result * 255
 im_result = np.rint(im_result).astype('uint8')
 # Bicubic result
 im_bicubic = im_bicubic * 255
 im_bicubic = np.rint(im_bicubic).astype('uint8')
 # Blended result
 im_blending = im_blending * 255
 im_blending = np.rint(im_blending).astype('uint8')
 # Measure reconstruction quality
 # Calculate PSNR values
 PSNR_bicubic = compare_psnr(imHR[region], im_bicubic[region])
 PSNR_result = compare_psnr(imHR[region], im_result[region])
 PSNR_blending = compare_psnr(imHR[region], im_blending[region])
 PSNR_blending = max(PSNR_result, PSNR_blending)
 # Save RAISR reconstruction
 createFolder('./results/')
 #cv2.imwrite('results/' + os.path.splitext(os.path.basename(filelist[0][image]))[0] + '_result.bmp', result_RAISR)
 cv2.imwrite(
     'results/' +
     os.path.splitext(os.path.basename(filelist[0][image]))[0] +
     '_bicubic.bmp', im_bicubic[region])
 cv2.imwrite(
     'results/' +
     os.path.splitext(os.path.basename(filelist[0][image]))[0] +
     '_result.bmp', im_result[region])
 cv2.imwrite(
コード例 #25
0
def test_PSNR_errors():
    with pytest.raises(ValueError):
        compare_psnr(cam, cam.astype(np.float32))
    with pytest.raises(ValueError):
        compare_psnr(cam, cam[:-1, :])
コード例 #26
0
def func_enhance(dir_model_pre, QP, PreIndex_list, CmpIndex_list, SubIndex_list):
    """Enhance PQFs or non-PQFs, record dpsnr, dssim and enhanced frames."""
    #ywz
    global ywz_times

    global enhanced_list, sum_dpsnr, sum_dssim
    
    tf.reset_default_graph()

    ### Defind enhancement process
    x1 = tf.placeholder(tf.float32, [BATCH_SIZE, height, width, CHANNEL])  # previous
    x2 = tf.placeholder(tf.float32, [BATCH_SIZE, height, width, CHANNEL])  # current
    x3 = tf.placeholder(tf.float32, [BATCH_SIZE, height, width, CHANNEL])  # subsequent
        
    if QP in net1_list:
        is_training = tf.placeholder_with_default(False, shape=())
    
    x1to2 = warp_img(BATCH_SIZE, x2, x1, False)
    x3to2 = warp_img(BATCH_SIZE, x2, x3, True)
    
    if QP in net1_list:
        x2_enhanced = net_MFCNN.network(x1to2, x2, x3to2, is_training)
    else:
        x2_enhanced = net_MFCNN.network2(x1to2, x2, x3to2)
    
    saver = tf.train.Saver()
    
    with tf.Session(config = config) as sess:
        # ywz
        if ywz_times==0:
            options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
        #

        # Restore model
        # model_path = os.path.join(dir_model_pre, "model_step2.ckpt-" + str(QP))
        model_path = os.path.join(dir_model_pre, "model_step2.ckpt-0")

        # saver.restore(sess, model_path)
        module_file = tf.train.latest_checkpoint(model_path)
        # with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        if module_file is not None:
            saver.restore(sess, module_file)



        nfs = len(CmpIndex_list)
        
        sum_dpsnr_part = 0.0
        sum_dssim_part = 0.0
    
        for ite_frame in range(nfs):
          
            # Load frames
            pre_frame = y_import(CmpVideo_path, height, width, 1, PreIndex_list[ite_frame])[:,:,:,np.newaxis] / 255.0
            cmp_frame = y_import(CmpVideo_path, height, width, 1, CmpIndex_list[ite_frame])[:,:,:,np.newaxis] / 255.0
            sub_frame = y_import(CmpVideo_path, height, width, 1, SubIndex_list[ite_frame])[:,:,:,np.newaxis] / 255.0
            
            # if cmp frame is plane?
            if isplane(cmp_frame):
                continue

            # if PQF frames are plane?
            if isplane(pre_frame):
                 pre_frame = np.copy(cmp_frame)
            if isplane(sub_frame):
                 sub_frame = np.copy(cmp_frame)


            # ywz
            if ywz_times==0:
                run_metadata = tf.RunMetadata()
                # Enhance
                if QP in net1_list:
                    enhanced_frame = sess.run(x2_enhanced,options = options, feed_dict={x1:pre_frame, x2:cmp_frame, x3:sub_frame, is_training:False}, run_metadata=run_metadata)
                else:
                    enhanced_frame = sess.run(x2_enhanced,options = options, feed_dict={x1:pre_frame, x2:cmp_frame, x3:sub_frame}, run_metadata=run_metadata)

            else:
                # Enhance
                if QP in net1_list:
                    enhanced_frame = sess.run(x2_enhanced, feed_dict={x1: pre_frame, x2: cmp_frame, x3: sub_frame,is_training: False})
                else:
                    enhanced_frame = sess.run(x2_enhanced, feed_dict={x1: pre_frame, x2: cmp_frame, x3: sub_frame})

            #
            # ywz
            # timeline record
            # Create the Timeline object, and write it to a json file
            if ywz_times==0:
                fetched_timeline = timeline.Timeline(run_metadata.step_stats)
                chrome_trace = fetched_timeline.generate_chrome_trace_format()
                with open("timeline/timeline_" + str(ite_frame) + ".json", 'w') as f:
                    f.write(chrome_trace)
            #


            # Record for output video
            enhanced_list[CmpIndex_list[ite_frame]] = np.squeeze(enhanced_frame)
            
            # Evaluate and accumulate dpsnr
            raw_frame = np.squeeze(y_import(RawVideo_path, height, width, 1, CmpIndex_list[ite_frame])) / 255.0
            cmp_frame = np.squeeze(cmp_frame)
            enhanced_frame = np.squeeze(enhanced_frame)
            
            raw_frame = np.float32(raw_frame)
            cmp_frame = np.float32(cmp_frame)
            
            psnr_ori = compare_psnr(cmp_frame, raw_frame, data_range=1.0)
            psnr_aft = compare_psnr(enhanced_frame, raw_frame, data_range=1.0)

            ssim_ori = compare_ssim(cmp_frame, raw_frame, data_range=1.0)
            ssim_aft = compare_ssim(enhanced_frame, raw_frame, data_range=1.0)
            
            sum_dpsnr_part += psnr_aft - psnr_ori
            sum_dssim_part += ssim_aft - ssim_ori

            print("\r %d | %d at QP = %d" % (ite_frame + 1, nfs, QP), end="")

            #ywz
            ywz_times += 1

        print("              ", end="\r")
        
        sum_dpsnr += sum_dpsnr_part
        sum_dssim += sum_dssim_part
        
        average_dpsnr = sum_dpsnr_part / nfs
        average_dssim = sum_dssim_part / nfs
        print("dPSNR: %.3f - dSSIM: %.3f - nfs: %4d" % (average_dpsnr, average_dssim, nfs), flush=True)
        file_object.write("dPSNR: %.3f - dSSIM: %.3f - nfs: %4d\n" % (average_dpsnr, average_dssim, nfs))
        file_object.flush()
コード例 #27
0
def accuracy(outputs, labels):
    N, _, _, _ = outputs.shape
    psnr = 0
    for i in range(N):
        psnr += compare_psnr(labels[i], outputs[i])
    return psnr / N
コード例 #28
0
if len(img_clean.shape) == 2:
    net = Denoising_Net_gray()
    net.load_state_dict(t.load('./model/model_300.hdf5', 'cuda:0'))
    net.cuda()
    tensor_n = gray_tensor_generator(img_n)
    nlm = np.tile(sigma / 255.0,
                  tensor_n[0].shape).astype(np.float32)  # noise level map
    tensor_n.append(nlm)
    noisy = np.expand_dims(np.stack(tensor_n, axis=0), axis=0)
    noisy = t.from_numpy(noisy.copy())
    img_denoised = net(noisy.cuda())
    img_denoised = img_denoised.cpu().detach().numpy().squeeze()
    img_denoised = gray_reconstruction(img_denoised, wave_base)
    img_denoised = img_denoised[0:img_clean.shape[0], 0:img_clean.shape[1]]
    psnr = round(compare_psnr(img_denoised, img_clean, data_range=1), 3)
    ssim = round(
        compare_ssim(img_denoised, img_clean, data_range=1,
                     multichannel=False), 3)
    print('PSNR: %.4f, SSIM: %.4f' % (psnr, ssim))
    print('Test Image: ' + img_name +
          ' Noise Level: %d, PSNR: %.4f, SSIM: %.4f' % (sigma, psnr, ssim))
    if Dir != 'RNI6':
        psnr = round(compare_psnr(img_denoised, img_clean, data_range=1), 3)
        ssim = round(
            compare_ssim(img_denoised,
                         img_clean,
                         data_range=1,
                         multichannel=False), 3)
        print('PSNR: %.4f, SSIM: %.4f' % (psnr, ssim))
        print('Test Image: ' + img_name +
コード例 #29
0
ax[0, 1].set_title('non-local means\n(slow)')
ax[0, 2].imshow(denoise2)
ax[0, 2].axis('off')
ax[0, 2].set_title('non-local means\n(slow, using $\sigma_{est}$)')
ax[1, 0].imshow(astro)
ax[1, 0].axis('off')
ax[1, 0].set_title('original\n(noise free)')
ax[1, 1].imshow(denoise_fast)
ax[1, 1].axis('off')
ax[1, 1].set_title('non-local means\n(fast)')
ax[1, 2].imshow(denoise2_fast)
ax[1, 2].axis('off')
ax[1, 2].set_title('non-local means\n(fast, using $\sigma_{est}$)')

fig.tight_layout()

# print PSNR metric for each case
psnr_noisy = compare_psnr(astro, noisy)
psnr = compare_psnr(astro, denoise)
psnr2 = compare_psnr(astro, denoise2)
psnr_fast = compare_psnr(astro, denoise_fast)
psnr2_fast = compare_psnr(astro, denoise2_fast)

print("PSNR (noisy) = {:0.2f}".format(psnr_noisy))
print("PSNR (slow) = {:0.2f}".format(psnr))
print("PSNR (slow, using sigma) = {:0.2f}".format(psnr2))
print("PSNR (fast) = {:0.2f}".format(psnr_fast))
print("PSNR (fast, using sigma) = {:0.2f}".format(psnr2_fast))

plt.show()
コード例 #30
0
ファイル: mainimprovementcolor.py プロジェクト: zongjg/BRDNet
def test(model):

    print('Start to test on {}'.format(args.test_dir))
    out_dir = save_dir + args.test_dir.split('/')[-1] + '/'
    if not os.path.exists(out_dir):
        os.mkdir(out_dir)

    name = []
    psnr = []
    ssim = []
    file_list = glob.glob(
        '{}/*'.format(args.test_dir)
    )  #notice: it is easy to generate error  $201804101000tcw #notice; need to change the format
    for file in file_list:
        # read image
        img_clean = np.array(Image.open(file), dtype='float32') / 255.0
        np.random.seed(
            0
        )  #obtain the same random data when it is in the test phase tcw201804151350
        img_test = img_clean + np.random.normal(0, args.sigma / 255.0,
                                                img_clean.shape)
        img_test = img_test.astype('float32')
        # predict
        x_test = img_test.reshape(
            1, img_test.shape[0], img_test.shape[1], 3
        )  #if the last parameter is 1, the image is gray. If the last parameter is 3201807082123tcw
        y_predict = model.predict(x_test)  #tcw
        # calculate numeric metrics
        img_out = y_predict.reshape(img_clean.shape)
        img_out = np.clip(img_out, 0, 1)
        psnr_noise, psnr_denoised = compare_psnr(img_clean, img_test,
                                                 True), compare_psnr(
                                                     img_clean, img_out, True)
        ssim_noise, ssim_denoised = compare_ssim(
            img_clean, img_test,
            multichannel=True), compare_ssim(img_clean,
                                             img_out,
                                             multichannel=True)
        psnr.append(psnr_denoised)
        ssim.append(ssim_denoised)
        # save images
        filename = file.split('/')[-1].split('.')[
            0]  # get the name of image file
        name.append(filename)
        img_test = Image.fromarray((img_test * 255).astype('uint8'))
        img_test.save(out_dir + filename + '_sigma' +
                      '{}_psnr{:.2f}.png'.format(args.sigma, psnr_noise))
        img_out = Image.fromarray((img_out * 255).astype('uint8'))
        img_out.save(out_dir + filename +
                     '_psnr{:.2f}.png'.format(psnr_denoised))
    #    print psnr_denoised
    #	print len(psnr)
    #print sum(psnr)
    psnr_avg = sum(psnr) / len(psnr)
    ssim_avg = sum(ssim) / len(ssim)
    name.append('Average')
    psnr.append(psnr_avg)
    ssim.append(ssim_avg)
    print('Average PSNR = {0:.2f}, SSIM = {1:.2f}'.format(psnr_avg, ssim_avg))

    pd.DataFrame({
        'name': np.array(name),
        'psnr': np.array(psnr),
        'ssim': np.array(ssim)
    }).to_csv(out_dir + '/metrics.csv', index=True)
コード例 #31
0
from skimage.restoration import denoise_wavelet, cycle_spin
from skimage import data, img_as_float
from skimage.util import random_noise
from skimage.measure import compare_psnr


original = img_as_float(data.chelsea()[100:250, 50:300])

sigma = 0.155
noisy = random_noise(original, var=sigma**2)

fig, ax = plt.subplots(nrows=2, ncols=3, figsize=(10, 4),
                       sharex=False, sharey=False)
ax = ax.ravel()

psnr_noisy = compare_psnr(original, noisy)
ax[0].imshow(noisy)
ax[0].axis('off')
ax[0].set_title('Noisy\nPSNR={:0.4g}'.format(psnr_noisy))


# Repeat denosing with different amounts of cycle spinning.  e.g.
# max_shift = 0 -> no cycle spinning
# max_shift = 1 -> shifts of (0, 1) along each axis
# max_shift = 3 -> shifts of (0, 1, 2, 3) along each axis
# etc...

denoise_kwargs = dict(multichannel=True, convert2ycbcr=True, wavelet='db1')

all_psnr = []
max_shifts = [0, 1, 3, 5]
コード例 #32
0
ファイル: metrics.py プロジェクト: leehhtw/dldegibbs
 def psnr(gt, pred):
     """ Compute Peak Signal to Noise Ratio metric (PSNR) """
     return compare_psnr(gt, pred, data_range=gt.max())
コード例 #33
0
ファイル: task14.py プロジェクト: fakefeik/machine_learning

image = img_as_float(imread('parrots.jpg'))

l = []
for i in range(len(image)):
    for j in range(len(image[0])):
        l.append(image[i][j])
clf = KMeans(n_clusters=10, random_state=241)
clf.fit(l)
pred = clf.predict(l)

l = list(map(lambda x: x, l))
l1 = list(map(lambda x: clf.cluster_centers_[pred[x]], range(len(l))))
l2 = list(map(lambda x: to_rgb(clf.cluster_centers_[pred[x]]), range(len(l))))

print(compare_psnr(np.array(l), np.array(l1)))

count = 0
l3 = [None] * len(image)
for i in range(len(image)):
    l3[i] = [None] * len(image[i])
    for j in range(len(image[i])):
        l3[i][j] = l2[count]
        for k in range(3):
            l3[i][j][k] = 255 - l3[i][j][k]
        count += 1

l3 = np.array(l3, np.int32)
matplotlib.image.imsave('name.png', l3)
コード例 #34
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 = compare_psnr(cam, cam_noisy)
    assert_almost_equal(p, p_IPOL, decimal=4)
コード例 #35
0
ファイル: image-iqa.py プロジェクト: qjlv/your-face
def psnr(imageA, imageB):

    return compare_psnr(imageA, imageB)
コード例 #36
0
    for step in progress:
        optimizer.zero_grad()
        net_output = net(net_input)
        net_output_ds = ds_net(net_output)

        loss = MSE(net_output_ds, target)
        loss.backward()
        optimizer.step()

        train_loss = loss.data[0]

        if step % 100 == 0:
            if use_cuda:
                net_output = net_output.cpu()
            filename = None if args.output is None else '%s/%04d.png' % (
                args.output, step)
            img = tensor_to_image(net_output.data, filename)
            psnr = compare_psnr(truth, np.array(img))
            max_psnr = max(max_psnr, psnr)

        progress.set_description(
            'Loss: %.6f | PSNR: %.2f dB | Max PSNR: %.2f dB' %
            (train_loss, psnr, max_psnr))

        if use_cuda:
            noise_new = torch.cuda.FloatTensor(noise.shape).normal_(std=sigma)
        else:
            noise_new = torch.FloatTensor(noise.shape).normal_(std=sigma)

        net_input.data += noise_new
コード例 #37
0
                y = Q * np.random.poisson(im_lam).astype('float32') / 255.0
                y_ = torch.from_numpy(y).view(1, -1, y.shape[0], y.shape[1])

                torch.cuda.synchronize()
                start_time = time.time()
                y_ = y_.cuda()
                x_ = model(y_)[1]  # inference
                x_ = x_.view(y.shape[0], y.shape[1])
                x_ = x_.cpu()
                x_ = x_.detach().numpy().astype(np.float32)
                torch.cuda.synchronize()
                elapsed_time = time.time() - start_time
                print('%10s : %10s : %2.4f second' %
                      (set_cur, im, elapsed_time))

                psnr_x_ = compare_psnr(x, x_)
                ssim_x_ = compare_ssim(x, x_)
                if args.save_result:
                    name, ext = os.path.splitext(im)
                    if k < 10:
                        show(np.hstack((y, x_, y - x_)))  # show the image
                    k = k + 1
                    save_result(x_,
                                path=os.path.join(
                                    args.result_dir, set_cur, name + '_dncnn' +
                                    ext))  # save the denoised image
                psnrs.append(psnr_x_)
                ssims.append(ssim_x_)
        psnr_avg = np.mean(psnrs)
        ssim_avg = np.mean(ssims)
        psnrs.append(psnr_avg)
コード例 #38
0
def main(lr, prefix, K, T, gpu):
    data_path = "../data/UCF101/UCF-101/"
    f = open(data_path.rsplit("/", 2)[0] + "/testlist01.txt", "r")
    testfiles = f.readlines()
    image_size = [240, 320]
    c_dim = 3
    iters = 0

    if prefix == "paper_models":
        checkpoint_dir = "../models/" + prefix + "/S1M/"
        best_model = "MCNET.model-102502"
    else:
        checkpoint_dir = "../models/" + prefix + "/"
        best_model = None  # will pick last model

    with tf.device("/gpu:%d" % gpu[0]):
        model = MCNET(image_size=image_size,
                      batch_size=1,
                      K=K,
                      T=T,
                      c_dim=c_dim,
                      checkpoint_dir=checkpoint_dir,
                      is_train=False)

    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                          log_device_placement=False,
                                          gpu_options=gpu_options)) as sess:

        tf.global_variables_initializer().run()

        loaded, model_name = model.load(sess, checkpoint_dir, best_model)

        if loaded:
            print(" [*] Load SUCCESS")
        else:
            print(" [!] Load failed... exitting")
            return

        quant_dir = "../results/quantitative/UCF101/" + prefix + "/"
        save_path = quant_dir + "results_model=" + model_name + ".npz"
        if not exists(quant_dir):
            makedirs(quant_dir)

        vid_names = []
        psnr_err = np.zeros((0, T))
        ssim_err = np.zeros((0, T))
        for i in range(0, len(testfiles), 10):
            print(" Video " + str(i) + "/" + str(len(testfiles)))

            tokens = testfiles[i].split("/")[1].split()

            testfiles[i] = testfiles[i].replace("/HandStandPushups/",
                                                "/HandstandPushups/")

            vid_path = data_path + testfiles[i].split()[0]
            vid = imageio.get_reader(vid_path, "ffmpeg")
            folder_name = vid_path.split("/")[-1].split(".")[0]
            vid_names.append(folder_name)
            vid = imageio.get_reader(vid_path, "ffmpeg")
            savedir = "../results/images/UCF101/" + prefix + "/" + str(i + 1)

            seq_batch = np.zeros(
                (1, image_size[0], image_size[1], K + T, c_dim),
                dtype="float32")
            diff_batch = np.zeros((1, image_size[0], image_size[1], K - 1, 1),
                                  dtype="float32")
            for t in range(K + T):
                img = vid.get_data(t)[:, :, ::-1]
                seq_batch[0, :, :, t] = transform(img)

            for t in range(1, K):
                prev = inverse_transform(seq_batch[0, :, :, t - 1]) * 255
                prev = cv2.cvtColor(prev.astype("uint8"), cv2.COLOR_BGR2GRAY)
                next = inverse_transform(seq_batch[0, :, :, t]) * 255
                next = cv2.cvtColor(next.astype("uint8"), cv2.COLOR_BGR2GRAY)
                diff = next.astype("float32") - prev.astype("float32")
                diff_batch[0, :, :, t - 1] = diff[:, :, None] / 255.

            true_data = seq_batch[:, :, :, K:, :].copy()
            pred_data = np.zeros(true_data.shape, dtype="float32")
            xt = seq_batch[:, :, :, K - 1]
            pred_data[0] = sess.run(model.G,
                                    feed_dict={
                                        model.diff_in: diff_batch,
                                        model.xt: xt
                                    })

            if not os.path.exists(savedir):
                os.makedirs(savedir)

            cpsnr = np.zeros((K + T, ))
            cssim = np.zeros((K + T, ))
            pred_data = np.concatenate((seq_batch[:, :, :, :K], pred_data),
                                       axis=3)
            true_data = np.concatenate((seq_batch[:, :, :, :K], true_data),
                                       axis=3)
            for t in range(K + T):
                pred = (inverse_transform(pred_data[0, :, :, t]) *
                        255).astype("uint8")
                target = (inverse_transform(true_data[0, :, :, t]) *
                          255).astype("uint8")

                cpsnr[t] = measure.compare_psnr(pred, target)
                cssim[t] = ssim.compute_ssim(Image.fromarray(target),
                                             Image.fromarray(pred))

                pred = draw_frame(pred, t < K)
                target = draw_frame(target, t < K)

                cv2.imwrite(savedir + "/pred_" + "{0:04d}".format(t) + ".png",
                            pred)
                cv2.imwrite(savedir + "/gt_" + "{0:04d}".format(t) + ".png",
                            target)

            cmd1 = "rm " + savedir + "/pred.gif"
            cmd2 = ("ffmpeg -f image2 -framerate 3 -i " + savedir +
                    "/pred_%04d.png " + savedir + "/pred.gif")
            cmd3 = "rm " + savedir + "/pred*.png"

            # Comment out "system(cmd3)" if you want to keep the output images
            # Otherwise only the gifs will be kept
            system(cmd1)
            system(cmd2)
            system(cmd3)

            cmd1 = "rm " + savedir + "/gt.gif"
            cmd2 = ("ffmpeg -f image2 -framerate 3 -i " + savedir +
                    "/gt_%04d.png " + savedir + "/gt.gif")
            cmd3 = "rm " + savedir + "/gt*.png"

            # Comment out "system(cmd3)" if you want to keep the output images
            # Otherwise only the gifs will be kept
            system(cmd1)
            system(cmd2)
            system(cmd3)

            psnr_err = np.concatenate((psnr_err, cpsnr[None, K:]), axis=0)
            ssim_err = np.concatenate((ssim_err, cssim[None, K:]), axis=0)

        np.savez(save_path, psnr=psnr_err, ssim=ssim_err)
        print("Results saved to " + save_path)
    print("Done.")
コード例 #39
0
def main(args):
    image_number=0;
    model = SARGAN(img_size, BATCH_SIZE, img_channel=img_size[2])
    with tf.variable_scope("d_opt",reuse=tf.AUTO_REUSE):
        d_opt = tf.train.AdamOptimizer(learning_rate=LEARNING_RATE).minimize(model.d_loss, var_list=model.d_vars)
    with tf.variable_scope("g_opt",reuse=tf.AUTO_REUSE):
        g_opt = tf.train.AdamOptimizer(learning_rate=LEARNING_RATE).minimize(model.g_loss, var_list=model.g_vars)
    if(retrain==0):
        saver = tf.train.Saver(max_to_keep=20)
    else:
        saver = tf.train.import_meta_graph(trained_model_path+'/sargan_mnist.meta');
    
    gpu_options = tf.GPUOptions(allow_growth=True, visible_device_list=str(GPU_ID))
    config = tf.ConfigProto(gpu_options=gpu_options)
    
    progress_bar = tqdm(range(MAX_EPOCH), unit="epoch")
    #list of loss values each item is the loss value of one ieteration
    train_d_loss_values = []
    train_g_loss_values = []
    
    
    #test_imgs, test_classes = get_data(test_filename)
    #imgs, classes = get_data(train_filename)
    with tf.Session(config=config) as sess:
        if(retrain==0):
            sess.run(tf.global_variables_initializer())
        else:
            sess.run(tf.global_variables_initializer())
            saver.restore(sess,tf.train.latest_checkpoint(trained_model_path));
        #test_copies = test_imgs.astype('float32')
        for epoch in progress_bar:
            NUM_TEST_PER_EPOCH = 1
            counter = 0
            epoch_start_time = time.time()
            encoded_data, original_data, val_data, val_original=transfrom_data(NUM_TEST_PER_EPOCH)
            #shuffle(copies)
            #divide the images into equal sized batches
            #image_batches = np.array(list(chunks(copies, BATCH_SIZE)))
            for i in range (NUM_ITERATION):
                #getting a batch from the training data
                #one_batch_of_imgs = image_batches[i]                
                #copy the batch
                features=original_data[i]
                #corrupt the images
                corrupted_batch = encoded_data[i]
                _, m = sess.run([d_opt, model.d_loss], feed_dict={model.image:features, model.cond:corrupted_batch})
                _, M = sess.run([g_opt, model.g_loss], feed_dict={model.image:features, model.cond:corrupted_batch})
                train_d_loss_values.append(m)
                train_g_loss_values.append(M)
                #print some notifications
                counter += 1
                if counter % 25 == 0:
                    print("\rEpoch [%d], Iteration [%d]: time: %4.4f, d_loss: %.8f, g_loss: %.8f" \
                      % (epoch, counter, time.time() - epoch_start_time, m, M))
                
            # save the trained network
            if epoch % SAVE_EVERY_EPOCH == 0:
                save_path = saver.save(sess, (trained_model_path+"/sargan_mnist"))
                print("\n\nModel saved in file: %s\n\n" % save_path)
            ''' +\
                                   "%s_model_%s.ckpt" % ( experiment_name, epoch+1))'''
            
            
            ##### TESTING FOR CURRUNT EPOCH
            
           
            #test_batches = np.array(list(chunks(test_copies, BATCH_SIZE)))
            #test_images = test_batches[0]
            sum_psnr = 0
            list_images = []
            for j in range(NUM_TEST_PER_EPOCH):
                features=val_original[j]
                #corrupt the images
                corrupted_batch = val_data[j]
                gen_imgs = sess.run(model.gen_img, feed_dict={model.image:features, model.cond:corrupted_batch})
                print(features.shape, gen_imgs.shape)
                #if j %17 == 0: # only save 3 images 0, 17, 34
                list_images.append((features[0], corrupted_batch[0], gen_imgs[0]))
                list_images.append((features[17], corrupted_batch[17], gen_imgs[17]))                
                list_images.append((features[34], corrupted_batch[34], gen_imgs[34]))
                for i in range(len(gen_imgs)):
                    current_img = features[i]
                    recovered_img = gen_imgs[i]
                    sum_psnr += ski_me.compare_psnr(current_img, recovered_img, 1)
                #psnr_value = ski_mem.compare_psnr(test_img, gen_img, 1)
                #sum_psnr += psnr_value
            average_psnr = sum_psnr / 50
            
            epoch_running_time = time.time() - epoch_start_time
            ############### SEND EMAIL ##############
            rows = 1
            cols = 3
            display_mean = np.array([0.485, 0.456, 0.406])
            display_std = np.array([0.229, 0.224, 0.225])
            if epoch % SAVE_EVERY_EPOCH == 0: 
                #image = std * image + mean
                imgs_1 = list_images[0]
                imgs_2 = list_images[1]
                imgs_3 = list_images[2]
                imgs_1 = display_std * imgs_1 + display_mean
                imgs_2 = display_std * imgs_2 + display_mean
                imgs_3 = display_std * imgs_3 + display_mean
                fig = plt.figure(figsize=(14, 4))
                ax = fig.add_subplot(rows, cols, 1)
                ax.imshow(imgs_1[0])
                ax.set_title("Original", color='grey')
                ax = fig.add_subplot(rows, cols, 2)
                ax.imshow(imgs_1[1])
                ax.set_title("Corrupted", color='grey')
                ax = fig.add_subplot(rows, cols, 3)
                ax.imshow(imgs_1[2])
                ax.set_title("Recovered", color='grey')
                plt.tight_layout()
                #sample_test_file_1 = os.path.join(output_path, '%s_epoch_%s_batchsize_%s_1.jpg' % (experiment_name, epoch, BATCH_SIZE))
                sample_test_file_1 = os.path.join(output_path, 'image_%d_1.jpg' % image_number)
                plt.savefig(sample_test_file_1, dpi=300)
                
                fig = plt.figure(figsize=(14, 4))
                ax = fig.add_subplot(rows, cols, 1)
                ax.imshow(imgs_2[0])
                ax.set_title("Original", color='grey')
                ax = fig.add_subplot(rows, cols, 2)
                ax.imshow(imgs_2[1])
                ax.set_title("Corrupted", color='grey')
                ax = fig.add_subplot(rows, cols, 3)
                ax.imshow(imgs_2[2])
                ax.set_title("Recovered", color='grey')
                plt.tight_layout()
                #sample_test_file_2 = os.path.join(output_path, '%s_epoch_%s_batchsize_%s_2.jpg' % (experiment_name, epoch, BATCH_SIZE))
                sample_test_file_2 = os.path.join(output_path, 'image_%d_2.jpg' % image_number)
                plt.savefig(sample_test_file_2, dpi=300)
     
                fig = plt.figure(figsize=(14, 4))
                ax = fig.add_subplot(rows, cols, 1)
                ax.imshow(imgs_3[0])
                ax.set_title("Original", color='grey')
                ax = fig.add_subplot(rows, cols, 2)
                ax.imshow(imgs_3[1])
                ax.set_title("Corrupted", color='grey')
                ax = fig.add_subplot(rows, cols, 3)
                ax.imshow(imgs_3[2])
                ax.set_title("Recovered", color='grey')
                plt.tight_layout()
                #sample_test_file_3 = os.path.join(output_path, '%s_epoch_%s_batchsize_%s_3.jpg' % (experiment_name, epoch, BATCH_SIZE))
                sample_test_file_3 = os.path.join(output_path, 'image_%d_3.jpg' % image_number)
                image_number+=1
                plt.savefig(sample_test_file_3, dpi=300)
            plt.close("all")            
コード例 #40
0
 sr_ssim_total = 0.0
 sr_psnr_total = 0.0
 with open(RES_DIR + "qual.txt", 'w') as fp:
     fp.write("")
 for i in range(image_num):
     sk_io.imsave(RES_DIR + "sr_img_{0:02d}.png".format(i), sr_img[i])
     bc_ssim, sr_ssim = 0.0, 0.0
     for c in range(3):
         bc_ssim += sk_ms.compare_ssim(hr_img[i][:, :, c], bc_img[i][:, :,
                                                                     c])
         sr_ssim += sk_ms.compare_ssim(hr_img[i][:, :, c], sr_img[i][:, :,
                                                                     c])
     bc_ssim /= 3.0
     sr_ssim /= 3.0
     prog_bar(i * 2 + 1, image_num * 2)
     bc_psnr = sk_ms.compare_psnr(hr_img[i], bc_img[i])
     sr_psnr = sk_ms.compare_psnr(hr_img[i], sr_img[i])
     prog_bar(i * 2 + 2, image_num * 2)
     with open(RES_DIR + "qual.txt", 'a') as fp:
         fp.write("[Image {:d}]\n".format(i))
         fp.write("   bicubic: {0:f} {1:f}\n".format(bc_ssim, bc_psnr))
         fp.write("  superres: {0:f} {1:f}\n".format(sr_ssim, sr_psnr))
     bc_ssim_total += bc_ssim
     bc_psnr_total += bc_psnr
     sr_ssim_total += sr_ssim
     sr_psnr_total += sr_psnr
 bc_ssim_total /= image_num
 bc_psnr_total /= image_num
 sr_ssim_total /= image_num
 sr_psnr_total /= image_num
 with open(RES_DIR + "qual.txt", 'a') as fp:
コード例 #41
0
ファイル: dip.py プロジェクト: c1a1o1/dip-spectral
                num_ch=num_ch,
                reg_noise_std=args.reg_noise_std,
                n_ch_down=args.n_ch_down,
                n_ch_up=args.n_ch_up,
                skip_conn=args.skip_conn,
                depth=args.depth,
                act_fun=args.act_fun,
                upsample=args.upsample,
                linear=args.linear)
        traj_set.append(T)
    T1, T2 = traj_set

    # Find the best PSNR in the trajectory
    if not (args.clean_img is None):
        clean_img = utils.imread(args.clean_img)
        t1_psnr = [compare_psnr(clean_img, t1) for t1 in T1]
        best_psnr = np.max(t1_psnr)
        best_itr = np.argmax(t1_psnr)
        best_psnr_pred = T1[best_itr]
        print('Best PSNR: ' + str(best_psnr))

    # Show initial, best and final points on the trajectory
    if num_ch == 1:
        plt.imshow(T1[0], cmap='gray')
    else:
        plt.imshow(T1[0])
    plt.title('Initial output')
    plt.savefig(os.path.join(output_dir, 'init.png'))
    plt.close()
    if num_ch == 1:
        plt.imshow(T1[-1], cmap='gray')
コード例 #42
0
    img_pred = (imread(path_pred + '/' + basename(str(fn))) / 255.0).astype(
        np.float32)

    img_gt = rgb2gray(img_gt)
    img_pred = rgb2gray(img_pred)

    if args.debug != 0:
        plt.subplot('121')
        plt.imshow(img_gt)
        plt.title('Groud truth')
        plt.subplot('122')
        plt.imshow(img_pred)
        plt.title('Output')
        plt.show()

    psnr.append(compare_psnr(img_gt, img_pred, data_range=1))
    ssim.append(compare_ssim(img_gt, img_pred, data_range=1, win_size=51))
    mae.append(compare_mae(img_gt, img_pred))
    if np.mod(index, 100) == 0:
        print(
            str(index) + ' images processed',
            "PSNR: %.4f" % round(np.mean(psnr), 4),
            "SSIM: %.4f" % round(np.mean(ssim), 4),
            "MAE: %.4f" % round(np.mean(mae), 4),
        )
    index += 1

np.savez(args.output_path + '/metrics.npz',
         psnr=psnr,
         ssim=ssim,
         mae=mae,
コード例 #43
0
    final_preds = {b: None for b in baselines.keys()}

    traj_files = utils.traj_file_list(output_dir)
    for f in traj_files:
        hyp_str = utils.extract_hyp_str(f)

        noisy_output = utils.fname_with_hparams(output_dir, 'T_noisy.npz',
                                                hyp_str)
        noisy_T = utils.load_traj(noisy_output)

        # find pred. best iteration with different baselines
        for b in baselines.keys():
            best_iter_pred = baselines[b]['func'](noisy_im, noisy_T)
            best_rec_pred = noisy_T[best_iter_pred]
            best_err_pred = ((im - best_rec_pred)**2).sum()
            best_psnr_pred = compare_psnr(best_rec_pred.astype(im.dtype), im)

            if best_err_pred < baselines[b]['res']['pred_best_err']:
                baselines[b]['res']['pred_best_psnr'] = float(best_psnr_pred)
                baselines[b]['res']['pred_best_hyp'] = hyp_str
                baselines[b]['res']['pred_best_iter'] = int(
                    (best_iter_pred + 1) * traj_iter)
                final_preds[b] = best_rec_pred

    # save final results and best predicted denoised image
    for b in baselines.keys():
        save_dir = baselines[b]['save_dir']
        with open(os.path.join(save_dir, 'res.json'), 'w') as f:
            f.write(json.dumps(baselines[b]['res'], indent=2))
        f.close()
コード例 #44
0
    def eval(self, epoch, loss_epoch):
        print('Evaluating on the validation set...')
        psnr_all = 0
        ssim_all = 0
        count = 0
        for video_index in range(0, len(self.eval_frame_data_LR)):
            cur_video_LR = self.eval_frame_data_LR[video_index]
            path, _ = os.path.split(cur_video_LR[0])
            _, name = os.path.split(path)

            cur_video_HR = self.eval_frame_data_HR[video_index]
            max_frame = len(cur_video_LR)
            # temp_img = cv2_imread(cur_video_LR[0])
            # h,w,_ = temp_img.shape
            # L_eval = tf.placeholder(tf.float32, shape=[1, self.num_frames, h, w, 3], name='L_test')
            # SR_test = self.forward(L_eval)
            for i in range(max_frame):
                start_time = time.time()
                count += 1
                index = np.array([
                    i for i in range(i - self.num_frames // 2, i +
                                     self.num_frames // 2 + 1)
                ])
                index = np.clip(index, 0, max_frame - 1).tolist()
                lrs = np.array([cv2_imread(cur_video_LR[i])
                                for i in index]) / 255.0
                lrs = lrs.astype('float32')
                lrs = np.expand_dims(lrs, 0)

                sr = self.sess.run(self.SR_test, feed_dict={self.L_test: lrs})
                sr = sr * 255
                sr = np.squeeze(sr, axis=(0))
                sr = np.clip(sr, 0, 255)
                sr = np.round(sr, 0).astype(np.uint8)
                hr = cv2_imread(cur_video_HR[i])
                psnr = compare_psnr(sr, hr)
                ssim = compare_ssim(sr, hr, multichannel=True)
                psnr_all += psnr
                ssim_all += ssim
                cost_time = time.time() - start_time
                print(
                    '[*Epoch:{:05d}] val video:{} -> frame:{:05d}, psnr={:.4f}, ssim={:.4f}, cost time={:.2f} '
                    .format(epoch, name, i, psnr, ssim, cost_time))

        a_psnr = psnr_all / count
        a_ssim = ssim_all / count

        eval_ss = self.sess.run(self.merge_op_eval,
                                feed_dict={
                                    self.loss_epoch: loss_epoch,
                                    self.psnr_eval: a_psnr,
                                    self.ssim_eval: a_ssim
                                })
        self.writer.add_summary(eval_ss, epoch)

        print(
            '{' +
            '"Epoch": {:05d} , "Training Loss":{:.6f}, " Eval PSNR": {:.4f}, "Eval SSIM": {:.4f}'
            .format(epoch, loss_epoch, a_psnr, a_ssim) + '}')
        # write to log file
        with open(self.log_dir, 'a+') as f:
            f.write(
                '{' +
                '"Epoch": {:05d} , "Training Loss":{:.6f}, " Eval PSNR": {:.4f}, "Eval SSIM": {:.4f}'
                .format(epoch, loss_epoch, a_psnr, a_ssim) + '}\n')
コード例 #45
0
ファイル: resultsPSNR.py プロジェクト: sjameel98/Enhance
            if gencount >= args.images:
                continue
            generated[gencount] = np.array(io.imread(os.path.join(start_path, filename), as_grey = True))
            print(os.path.join(start_path, filename), gencount)
            gencount += 1
        elif (filename.startswith('interpolated') and filename.endswith('%03d.png'%(intcount))):
            if intcount >= args.images:
                continue
            interpolated[intcount] = np.array(io.imread(os.path.join(start_path, filename), as_grey = True))
            print(os.path.join(start_path, filename), intcount)
            intcount += 1

    psnrint = np.zeros(args.images)
    psnrgen = np.zeros(args.images)
    print(np.max(highres))
    print(np.max(interpolated)/255)
    print(np.max(generated))

    for i in range(args.images):
        psnrint[i] = compare_psnr(highres[i], interpolated[i]/255)
        psnrgen[i] = compare_psnr(highres[i], generated[i])

    fig = plt.figure()
    plt.plot(np.array([i+1 for i in range(args.images)]),psnrint)
    plt.plot(np.array([i+1 for i in range(args.images)]), psnrgen)
    plt.legend(['PSNR of Interpolated', 'PSNR of Generated'])
    plt.title('PSNR Value per Epoch for Select Sample')
    plt.xlabel('Epoch')
    plt.ylabel('PSNR (High is better image)')
    plt.show()
    fig.savefig('PSNROverEpochs.png', dpi = fig.dpi)
コード例 #46
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', '--continue', dest='continue_path', required=False)
    args = parser.parse_args()

    ## load dataset
    train_batch_gnr, train_set = get_dataset_batch(ds_name='train')

    test_gnr, test_set = get_dataset_batch(ds_name = 'test')
    ## build graph
    network = Model()
    placeholders, restored = network.build()
    gt = tf.placeholder(tf.float32, shape=(None, )+ (config.patch_size, config.patch_size)+ (config.nr_channel,), name = 'gt')

    loss_squared = squared_error_loss(gt, restored)
    loss_reg = tf.add_n(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))
    loss = loss_reg + loss_squared
    ## train config
    global_steps = tf.Variable(0, trainable=False)
    boundaries = [train_set.minibatchs_per_epoch*20, train_set.minibatchs_per_epoch*50]
    values = [0.001, 0.0001, 0.00005]
    lr = tf.train.piecewise_constant(global_steps, boundaries, values)
    opt = tf.train.AdamOptimizer(lr)
    # in order to update BN in every iter

    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    with tf.control_dependencies(update_ops):
        train = opt.minimize(loss)

    ## init tensorboard
    tf.summary.scalar('loss_regularization', loss_reg)
    tf.summary.scalar('loss_error', loss - loss_reg)
    tf.summary.scalar('loss', loss)
    tf.summary.scalar('learning_rate', lr)
    merged = tf.summary.merge_all()
    train_writer = tf.summary.FileWriter(os.path.join(config.log_dir, 'tf_log', 'train'),
                                         tf.get_default_graph())

    ## create a session
    tf.set_random_seed(12345) # ensure consistent results
    global_cnt = 0
    epoch_start = 0
    g_list = tf.global_variables()
    saver = tf.train.Saver(var_list=g_list)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        if args.continue_path:
            ckpt = tf.train.get_checkpoint_state(args.continue_path)
            saver.restore(sess, ckpt.model_checkpoint_path)
            epoch_start = int(ckpt.model_checkpoint_path.split('/')[-1].split('-')[1])
            global_cnt = epoch_start * train_set.minibatchs_per_epoch

        ## training
        for epoch in range(epoch_start+1, config.nr_epoch+1):
            for _ in range(train_set.minibatchs_per_epoch):
                global_cnt += 1
                images, gt_images = sess.run(train_batch_gnr)
                feed_dict = {
                    placeholders['data']: images,
                    gt: gt_images,
                    global_steps: global_cnt,
                    placeholders['is_training']: True,
                }
                _, loss_v, loss_reg_v, lr_v, summary = sess.run([train, loss, loss_reg,
                                                                 lr, merged],
                                                                       feed_dict=feed_dict)
                if global_cnt % config.show_interval == 0:
                    train_writer.add_summary(summary, global_cnt)
                    print(
                        "e:{},{}/{}".format(epoch, global_cnt % train_set.minibatchs_per_epoch,
                                            train_set.minibatchs_per_epoch),
                        'loss: {:.3f}'.format(loss_v),
                        'loss_reg: {:.3f}'.format(loss_reg_v),
                        'lr: {:.4f}'.format(lr_v),
                    )

            ## save model
            if epoch % config.snapshot_interval == 0:
                saver.save(sess, os.path.join(config.log_model_dir, 'epoch-{}'.format(epoch)),
                           global_step=global_cnt)

            if epoch % config.test_interval == 0:
                psnrs = []
                for _ in range(test_set.testing_minibatchs_per_epoch):
                    image, gt_image = sess.run(test_gnr)
                    feed_dict = {
                        placeholders['data']: image,
                        placeholders['is_training']: False,
                    }
                    restored_v = sess.run([restored],feed_dict = feed_dict)
                    psnr_x = compare_psnr(gt_image[0,:,:,::-1], restored_v[0][0, :, :, ::-1])
                    psnrs.append(psnr_x)
                print('average psnr is {:2.2f} dB'.format(np.mean(psnrs)))
        print('Training is done, exit.')
コード例 #47
0
im_visushrink = denoise_wavelet(noisy, multichannel=True, convert2ycbcr=True,
                                method='VisuShrink', mode='soft',
                                sigma=sigma_est)

# VisuShrink is designed to eliminate noise with high probability, but this
# results in a visually over-smooth appearance.  Repeat, specifying a reduction
# in the threshold by factors of 2 and 4.
im_visushrink2 = denoise_wavelet(noisy, multichannel=True, convert2ycbcr=True,
                                 method='VisuShrink', mode='soft',
                                 sigma=sigma_est/2)
im_visushrink4 = denoise_wavelet(noisy, multichannel=True, convert2ycbcr=True,
                                 method='VisuShrink', mode='soft',
                                 sigma=sigma_est/4)

# Compute PSNR as an indication of image quality
psnr_noisy = compare_psnr(original, noisy)
psnr_bayes = compare_psnr(original, im_bayes)
psnr_visushrink = compare_psnr(original, im_visushrink)
psnr_visushrink2 = compare_psnr(original, im_visushrink2)
psnr_visushrink4 = compare_psnr(original, im_visushrink4)

ax[0, 0].imshow(noisy)
ax[0, 0].axis('off')
ax[0, 0].set_title('Noisy\nPSNR={:0.4g}'.format(psnr_noisy))
ax[0, 1].imshow(im_bayes)
ax[0, 1].axis('off')
ax[0, 1].set_title(
    'Wavelet denoising\n(BayesShrink)\nPSNR={:0.4g}'.format(psnr_bayes))
ax[0, 2].imshow(im_visushrink)
ax[0, 2].axis('off')
ax[0, 2].set_title(
コード例 #48
0
def closure():

    global i, net_input, psnr_max, psnr_noisy_max, files_name, noisy_np, TEST_PLAN, test_torch_list, test_np_list
    global TRAIN_PLAN, noisy_np_norm, sigma_now, final_ssim, files_name, test_np_list, test_torch_list
    global psnr_2_5_max, final_ssim_max_5, psnr_2_10_max, final_ssim_max_10, psnr_2_15_max, final_ssim_max_15
    global psnr_2_20_max, final_ssim_max_20, psnr_2_25_max, final_ssim_max_25, img_aug_np,SAVE_MODEL_FLAG, multilevel_noise_train
    out_effect_np = []
    img_noisy_pil=[]
    img_noisy_np =[]
    img_noisy_torch=[]
    img_noisy_noisy_np = []
    img_noisy_noisy_pil =[]
    img_noisy_noisy_torch = []
    if multilevel_noise_train:
        min_log = np.log([0.0001])
        sigma_s = min_log + np.random.rand(1) * (np.log([sigma_now]) - min_log)
        sigma_s = np.exp(sigma_s)
        noisy_np = noisy_np_norm * sigma_s
        noisy_torch = np_to_torch(noisy_np)
    else:
        noisy_np = noisy_np_norm * sigma_now
        noisy_torch = np_to_torch(noisy_np)

    for idx in range(len(net_input)):
        img_noisy_pil_, img_noisy_np_, img_noisy_noisy_pil_, img_noisy_noisy_np_ = \
            get_noisy_noisy_image_with_noise(noisy_np, img_aug_np[idx])
        img_noisy_torch_ = np_to_torch(img_noisy_np_).type(dtype)
        img_noisy_noisy_torch_ = np_to_torch(img_noisy_noisy_np_).type(dtype)

        img_noisy_pil.append(img_noisy_pil_)
        img_noisy_np.append(img_noisy_np_)
        img_noisy_torch.append(img_noisy_torch_)
        img_noisy_noisy_np.append(img_noisy_noisy_np_)
        img_noisy_noisy_pil.append(img_noisy_noisy_pil_)
        img_noisy_noisy_torch.append(img_noisy_noisy_torch_)
    for aug in range(len(img_noisy_torch)):
        out = net(img_noisy_noisy_torch[aug])
        total_loss = mse(out, img_noisy_torch[aug])

        total_loss.backward()
        psrn_noisy = compare_psnr(np.clip(img_noisy_np[aug], 0, 1), out.detach().cpu().numpy()[0])
        do_i_learned_noise = img_noisy_noisy_np[aug] - out.detach().cpu().numpy()[0]
        mse_what_tf = MSE(noisy_np, do_i_learned_noise)

        if psnr_noisy_max == 0:
            psnr_noisy_max = psrn_noisy
        elif psnr_noisy_max < psrn_noisy:
            psnr_noisy_max = psrn_noisy

        if SAVE_DURING_TRAINING and i % save_every == 0:
            # output_dir
            out_test_np = torch_to_np(out)  # I +N1
            # out_test_name = f'{i}_test'
            # save_image(out_test_name, np.clip(out_test_np, 0, 1), output_path=output_dir)

        net.eval()
        with torch.no_grad():
            out_effect_np_ = torch_to_np(net(img_noisy_torch[aug]))
            out_effect_np.append(out_effect_np_)
            psnr_1 = compare_psnr(img_aug_np[aug], np.clip(out_effect_np_, 0, 1))

            test_do_i_learned_noise = img_noisy_np[aug] - out_effect_np_
            test_what_tf = MSE(noisy_np, test_do_i_learned_noise)
            writer.add_scalar('scalar_noisy_psnr', psrn_noisy, i)
            writer.add_scalar('scalar_test_psnr', psnr_1, i)
            if psnr_max == 0:
                psnr_max = psnr_1
            elif psnr_max < psnr_1:
                psnr_max = psnr_1

            # print('%s Iteration %05d lr: %f, Loss %f , PSNR_noisy: %f, PSNR_noisy_max: %f, noise mse: %f,'
            #     'test psnr: %f , test max psnr: %f, test noise mse: %f , current sigma: %f ' %
            #      (files_name, i, LR, total_loss.item(), psrn_noisy, psnr_noisy_max, mse_what_tf, psnr_1, psnr_max,
            #       test_what_tf, sigma_s * 255))


    if i % 10 == 0:
        SAVE_MODEL_FLAG = False
        net.eval()
        with torch.no_grad():
            real_test_effect_np = []
            for idxt in range(len(test_torch_list)):
                test_out_effect_np = torch_to_np(net(test_torch_list[idxt].type(dtype)))
                real_test_effect_np.append(test_out_effect_np)
                # psnr_1 = compare_psnr(img_np, np.clip(out_effect_np, 0, 1))
                # test_do_i_learned_noise = torch_to_np(img_noisy_np[aug]) - test_out_effect_np
        print('%s Iteration %05d lr: %f, Loss %f , PSNR_noisy: %f, PSNR_noisy_max: %f, noise mse: %f,'
              'test psnr: %f , test max psnr: %f, test noise mse: %f , current sigma: %f ' %
              (files_name, i, LR, total_loss.item(), psrn_noisy, psnr_noisy_max, mse_what_tf, psnr_1, psnr_max,
               test_what_tf, sigma_s * 255))


        # sigma = 5
        test_out_effect_np_5 = real_test_effect_np[0:8]
        test_out_effect_np_5[0] = test_out_effect_np_5[0].transpose(1, 2, 0)
        for aug in range(1, 8):
            if aug < 4:
               test_out_effect_np_5[aug] = np.rot90(test_out_effect_np_5[aug].transpose(1, 2, 0), 4-aug)
            else:
                test_out_effect_np_5[aug] = np.flipud(np.rot90(test_out_effect_np_5[aug].transpose(1, 2, 0), 8-aug))
        final_reuslt_5 = np.mean(test_out_effect_np_5, 0)

        psnr_2_5 = compare_psnr(torch_to_np(net_input[0]).transpose(1, 2, 0), np.clip(final_reuslt_5, 0, 1))
        final_ssim_5 = compare_ssim(img_aug_np[0].transpose(1, 2, 0), np.clip(final_reuslt_5, 0, 1), data_range=1, multichannel=True)

        if psnr_2_5_max==0:
            psnr_2_5_max = psnr_2_5
            final_ssim_max_5 = final_ssim_5
            SAVE_MODEL_FLAG = True
        elif psnr_2_5_max< psnr_2_5:
            psnr_2_5_max = psnr_2_5
            final_ssim_max_5 = final_ssim_5
            SAVE_MODEL_FLAG = True

        tmp_name_p_5 = f'{files_name[:-4]}_{TEST_PLAN[0]}_{psnr_2_5:.2f}_final_{final_ssim_5:.4f}'
        save_image(tmp_name_p_5, np.clip(final_reuslt_5.transpose(2, 0, 1), 0, 1), output_path=output_dir)

        print('psnr 2_5: %f, psnr 2_5 max: %f, final ssim_5 : %f, final ssim max_5: %f'
              %(psnr_2_5, psnr_2_5_max, final_ssim_5, final_ssim_max_5))

        # sigma = 10
        test_out_effect_np_10 = real_test_effect_np[8:16]
        test_out_effect_np_10[0] = test_out_effect_np_10[0].transpose(1, 2, 0)
        for aug in range(1, 8):
            if aug < 4:
                test_out_effect_np_10[aug] = np.rot90(test_out_effect_np_10[aug].transpose(1, 2, 0), 4 - aug)
            else:
                test_out_effect_np_10[aug] = np.flipud(np.rot90(test_out_effect_np_10[aug].transpose(1, 2, 0), 8 - aug))

        final_reuslt_10 = np.mean(test_out_effect_np_10, 0)

        psnr_2_10 = compare_psnr(img_aug_np[0].transpose(1, 2, 0), np.clip(final_reuslt_10, 0, 1))
        final_ssim_10 = compare_ssim(img_aug_np[0].transpose(1, 2, 0), np.clip(final_reuslt_10, 0, 1), data_range=1,
                                     multichannel=True)
        if psnr_2_10_max == 0:
            psnr_2_10_max = psnr_2_10
            final_ssim_max_10 = final_ssim_10
            SAVE_MODEL_FLAG = True
        elif psnr_2_10_max < psnr_2_10:
            psnr_2_10_max = psnr_2_10
            final_ssim_max_10 = final_ssim_10
            SAVE_MODEL_FLAG = True

        tmp_name_p_10 = f'{files_name[:-4]}_{TEST_PLAN[1]}_{psnr_2_10:.2f}_final_{final_ssim_10:.4f}'
        save_image(tmp_name_p_10, np.clip(final_reuslt_10.transpose(2, 0, 1), 0, 1), output_path=output_dir)

        print('psnr 2_10: %f, psnr 2_10 max: %f, final ssim_10 : %f, final ssim max_10: %f'
              % (psnr_2_10, psnr_2_10_max, final_ssim_10, final_ssim_max_10))

        # sigma = 15
        test_out_effect_np_15 = real_test_effect_np[16:24]
        test_out_effect_np_15[0] = test_out_effect_np_15[0].transpose(1, 2, 0)
        for aug in range(1, 8):
            if aug < 4:
                test_out_effect_np_15[aug] = np.rot90(test_out_effect_np_15[aug].transpose(1, 2, 0), 4 - aug)
            else:
                test_out_effect_np_15[aug] = np.flipud(np.rot90(test_out_effect_np_15[aug].transpose(1, 2, 0), 8 - aug))
        final_reuslt_15 = np.mean(test_out_effect_np_15, 0)

        psnr_2_15 = compare_psnr(img_aug_np[0].transpose(1, 2, 0), np.clip(final_reuslt_15, 0, 1))
        final_ssim_15 = compare_ssim(img_aug_np[0].transpose(1, 2, 0), np.clip(final_reuslt_15, 0, 1), data_range=1,
                                     multichannel=True)
        if psnr_2_15_max == 0:
            psnr_2_15_max = psnr_2_15
            final_ssim_max_15 = final_ssim_15
            SAVE_MODEL_FLAG = True
        elif psnr_2_15_max < psnr_2_15:
            psnr_2_15_max = psnr_2_15
            final_ssim_max_15 = final_ssim_15
            SAVE_MODEL_FLAG = True

        tmp_name_p_15 = f'{files_name[:-4]}_{TEST_PLAN[2]}_{psnr_2_15:.2f}_final_{final_ssim_15:.4f}'
        save_image(tmp_name_p_15, np.clip(final_reuslt_15.transpose(2, 0, 1), 0, 1), output_path=output_dir)

        print('psnr 2_15: %f, psnr 2_15 max: %f, final ssim_15 : %f, final ssim max_15: %f'
              % (psnr_2_15, psnr_2_15_max, final_ssim_15, final_ssim_max_15))

        # sigma = 20
        test_out_effect_np_20 = real_test_effect_np[24:32]
        test_out_effect_np_20[0] = test_out_effect_np_20[0].transpose(1, 2, 0)
        for aug in range(1, 8):
            if aug < 4:
                test_out_effect_np_20[aug] = np.rot90(test_out_effect_np_20[aug].transpose(1, 2, 0), 4 - aug)
            else:
                test_out_effect_np_20[aug] = np.flipud(np.rot90(test_out_effect_np_20[aug].transpose(1, 2, 0), 8 - aug))
        # final_reuslt = np.median(out_effect_np, 0)
        final_reuslt_20 = np.mean(test_out_effect_np_20, 0)

        psnr_2_20 = compare_psnr(img_aug_np[0].transpose(1, 2, 0), np.clip(final_reuslt_20, 0, 1))
        final_ssim_20 = compare_ssim(img_aug_np[0].transpose(1, 2, 0), np.clip(final_reuslt_20, 0, 1), data_range=1,
                                     multichannel=True)
        if psnr_2_20_max == 0:
            psnr_2_20_max = psnr_2_20
            final_ssim_max_20 = final_ssim_20
            SAVE_MODEL_FLAG = True
        elif psnr_2_20_max < psnr_2_20:
            psnr_2_20_max = psnr_2_20
            final_ssim_max_20 = final_ssim_20
            SAVE_MODEL_FLAG = True

        tmp_name_p_20 = f'{files_name[:-4]}_{TEST_PLAN[3]}_{psnr_2_20:.2f}_final_{final_ssim_20:.4f}'
        save_image(tmp_name_p_20, np.clip(final_reuslt_20.transpose(2, 0, 1), 0, 1), output_path=output_dir)

        print('psnr 2_20: %f, psnr 2_20 max: %f, final ssim_20 : %f, final ssim max_20: %f'
              % (psnr_2_20, psnr_2_20_max, final_ssim_20, final_ssim_max_20))

        # sigma = 25
        test_out_effect_np_25 = real_test_effect_np[32:40]
        test_out_effect_np_25[0] = test_out_effect_np_25[0].transpose(1, 2, 0)
        for aug in range(1, 8):
            if aug < 4:
                test_out_effect_np_25[aug] = np.rot90(test_out_effect_np_25[aug].transpose(1, 2, 0), 4 - aug)
            else:
                test_out_effect_np_25[aug] = np.flipud(np.rot90(test_out_effect_np_25[aug].transpose(1, 2, 0), 8 - aug))
        # final_reuslt = np.median(out_effect_np, 0)
        final_reuslt_25 = np.mean(test_out_effect_np_25, 0)

        psnr_2_25 = compare_psnr(img_aug_np[0].transpose(1, 2, 0), np.clip(final_reuslt_25, 0, 1))
        final_ssim_25 = compare_ssim(img_aug_np[0].transpose(1, 2, 0), np.clip(final_reuslt_25, 0, 1), data_range=1,
                                     multichannel=True)
        if psnr_2_25_max == 0:
            psnr_2_25_max = psnr_2_25
            final_ssim_max_25 = final_ssim_25
            SAVE_MODEL_FLAG = True
        elif psnr_2_25_max < psnr_2_25:
            psnr_2_25_max = psnr_2_25
            final_ssim_max_25 = final_ssim_25
            SAVE_MODEL_FLAG = True

        tmp_name_p_25 = f'{files_name[:-4]}_{TEST_PLAN[4]}_{psnr_2_25:.2f}_final_{final_ssim_25:.4f}'
        save_image(tmp_name_p_25, np.clip(final_reuslt_25.transpose(2, 0, 1), 0, 1), output_path=output_dir)

        print('psnr 2_25: %f, psnr 2_25 max: %f, final ssim_25 : %f, final ssim max_25: %f'
              % (psnr_2_25, psnr_2_25_max, final_ssim_25, final_ssim_max_25))

    i += 1

    return total_loss
コード例 #49
0
ファイル: image_metrics.py プロジェクト: benchoufi/kit-soft
print image_name1.shape
print image_name2.shape

#estimate the standard deiviation of the images

std_1 = numpy.std (numpy.std (numpy.array(image_name1)))
std_2 = numpy.std (numpy.std (numpy.array(image_name2)))

print ("std is %2.10f"%std_1)

#print ("Standard deviation of the images are"%(std_1,std_2))

#estimate the peak signal to noise ratio (PSNR) between the image

peak_signal_to_noise_ratio = measure.compare_psnr (image_name1,image_name2)

print ("Peak signal to noise ratio is %s"%peak_signal_to_noise_ratio)

# estimate the mean square error between the images

mse = measure.compare_mse(image_name1,image_name2)

print  ("Mean square error between the images is %s"%mse)

# estimate the normalised root mean square error between the images

rmse = measure.compare_nrmse(image_name1,image_name2)
ssim = measure.compare_ssim(image_name1,image_name2)

print  ("Normalised root mean squre error between the images is %s"%rmse)
コード例 #50
0
ファイル: psnr.py プロジェクト: xiesong521/remove_cloud
def psnr(img,gt):
    return compare_psnr(gt,img)
コード例 #51
0
        output = np.minimum(np.maximum(output, 0), 1)

        # print('**output shape', sess.run(tf.shape(output)))
        g_loss[ind] = G_current
        # if cnt % 20 == 0:
        train_writer.add_summary(summary, cnt)
        print(
            "%d %d Loss=%.3f Time=%.3f" %
            (epoch, cnt, np.mean(g_loss[np.where(g_loss)]), time.time() - st))

        if epoch % save_freq == 0:
            if not os.path.isdir(result_dir + '%04d' % epoch):
                os.makedirs(result_dir + '%04d' % epoch)

            psnr = compare_psnr(output[0, :, :, :],
                                gt_patch[0, :, :, :],
                                data_range=1.0)
            ssim = compare_ssim(output[0, :, :, :],
                                gt_patch[0, :, :, :],
                                multichannel=True)

            with open(os.path.join(log_dir, 'val.txt'), 'a+') as f:
                f.write('epoch: ' + str(epoch) + '  id: ' + str(train_id) +
                        '    psnr: ' + str(psnr) + 'ssim: ' + str(ssim) + '\n')
            temp = np.concatenate((gt_patch[0, :, :, :], output[0, :, :, :]),
                                  axis=1)
            # PIL.Image.fromarray((temp * 255).astype('uint8')).convert('RGB').save(
            #     result_dir + '%04d/%05d_00_train_%d_pil.jpg' % (epoch, train_id, ratio))
            scipy.misc.toimage(temp * 255, high=255, low=0, cmin=0,
                               cmax=255).save(result_dir +
                                              '%04d/%s_00_train.jpg' %
コード例 #52
0
def test_PSNR_float():
    p_uint8 = compare_psnr(cam, cam_noisy)
    p_float64 = compare_psnr(cam/255., cam_noisy/255., data_range=1)
    assert_almost_equal(p_uint8, p_float64, decimal=5)
コード例 #53
0
def test_cycle_spinning_multichannel():
    sigma = 0.1
    rstate = np.random.RandomState(1234)

    for multichannel in True, False:
        if multichannel:
            img = astro
            # can either omit or be 0 along the channels axis
            valid_shifts = [1, (0, 1), (1, 0), (1, 1), (1, 1, 0)]
            # can either omit or be 1 on channels axis.
            valid_steps = [1, 2, (1, 2), (1, 2, 1)]
            # too few or too many shifts or non-zero shift on channels
            invalid_shifts = [(1, 1, 2), (1, ), (1, 1, 0, 1)]
            # too few or too many shifts or any shifts <= 0
            invalid_steps = [(1, ), (1, 1, 1, 1), (0, 1), (-1, -1)]
        else:
            img = astro_gray
            valid_shifts = [1, (0, 1), (1, 0), (1, 1)]
            valid_steps = [1, 2, (1, 2)]
            invalid_shifts = [(1, 1, 2), (1, )]
            invalid_steps = [(1, ), (1, 1, 1), (0, 1), (-1, -1)]

        noisy = img.copy() + 0.1 * rstate.randn(*(img.shape))

        denoise_func = restoration.denoise_wavelet
        func_kw = dict(sigma=sigma, multichannel=multichannel)

        # max_shifts=0 is equivalent to just calling denoise_func
        with expected_warnings([PYWAVELET_ND_INDEXING_WARNING,
                                DASK_NOT_INSTALLED_WARNING]):
            dn_cc = restoration.cycle_spin(noisy, denoise_func, max_shifts=0,
                                           func_kw=func_kw,
                                           multichannel=multichannel)
            dn = denoise_func(noisy, **func_kw)
        assert_equal(dn, dn_cc)

        # denoising with cycle spinning will give better PSNR than without
        for max_shifts in valid_shifts:
            with expected_warnings([PYWAVELET_ND_INDEXING_WARNING,
                                    DASK_NOT_INSTALLED_WARNING]):
                dn_cc = restoration.cycle_spin(noisy, denoise_func,
                                               max_shifts=max_shifts,
                                               func_kw=func_kw,
                                               multichannel=multichannel)
            assert_(compare_psnr(img, dn_cc) > compare_psnr(img, dn))

        for shift_steps in valid_steps:
            with expected_warnings([PYWAVELET_ND_INDEXING_WARNING,
                                    DASK_NOT_INSTALLED_WARNING]):
                dn_cc = restoration.cycle_spin(noisy, denoise_func,
                                               max_shifts=2,
                                               shift_steps=shift_steps,
                                               func_kw=func_kw,
                                               multichannel=multichannel)
            assert_(compare_psnr(img, dn_cc) > compare_psnr(img, dn))

        for max_shifts in invalid_shifts:
            with testing.raises(ValueError):
                dn_cc = restoration.cycle_spin(noisy, denoise_func,
                                               max_shifts=max_shifts,
                                               func_kw=func_kw,
                                               multichannel=multichannel)
        for shift_steps in invalid_steps:
            with testing.raises(ValueError):
                dn_cc = restoration.cycle_spin(noisy, denoise_func,
                                               max_shifts=2,
                                               shift_steps=shift_steps,
                                               func_kw=func_kw,
                                               multichannel=multichannel)
コード例 #54
0
if len(sys.argv) > 1:
    model_location = str(sys.argv[1])
if len(sys.argv) > 2:
    img_location = str(sys.argv[2]) 
print('Inference on {} using model {}'.format(img_location, model_location))


model = load_model(
        model_location,
        custom_objects={
            'tf':tf, 
            'find_medians': find_medians,
            'merge': merge
            })


src_img = cv2.imread(img_location)
img = np.asarray(src_img / 255.0, np.float)
noisy_img = skimage.util.random_noise(img, mode='s&p', amount=0.7)
gx0 = np.reshape(noisy_img, (1, *noisy_img.shape))
Y = model.predict(gx0, verbose=1)
result = np.asarray(Y[0,:,:,:], np.float)
cv2.imshow('original', src_img)
cv2.imshow('bef', noisy_img)
cv2.imshow('aft', result)
print('psnr original', compare_psnr(img, noisy_img))
print('psnr smoothed', compare_psnr(img, result))
cv2.waitKey(0)


コード例 #55
-1
def test_wavelet_denoising():
    rstate = np.random.RandomState(1234)

    # version with one odd-sized dimension
    astro_gray_odd = astro_gray[:, :-1]
    astro_odd = astro[:, :-1]

    for img, multichannel in [(astro_gray, False), (astro_gray_odd, False),
                              (astro_odd, True)]:
        sigma = 0.1
        noisy = img + sigma * rstate.randn(*(img.shape))
        noisy = np.clip(noisy, 0, 1)

        # Verify that SNR is improved when true sigma is used
        denoised = restoration.denoise_wavelet(noisy, sigma=sigma,
                                               multichannel=multichannel)
        psnr_noisy = compare_psnr(img, noisy)
        psnr_denoised = compare_psnr(img, denoised)
        assert_(psnr_denoised > psnr_noisy)

        # Verify that SNR is improved with internally estimated sigma
        denoised = restoration.denoise_wavelet(noisy,
                                               multichannel=multichannel)
        psnr_noisy = compare_psnr(img, noisy)
        psnr_denoised = compare_psnr(img, denoised)
        assert_(psnr_denoised > psnr_noisy)

        # Test changing noise_std (higher threshold, so less energy in signal)
        res1 = restoration.denoise_wavelet(noisy, sigma=2*sigma,
                                           multichannel=multichannel)
        res2 = restoration.denoise_wavelet(noisy, sigma=sigma,
                                           multichannel=multichannel)
        assert_(np.sum(res1**2) <= np.sum(res2**2))