コード例 #1
0
def test_NRMSE_no_int_overflow():
    camf = cam.astype(np.float32)
    cam_noisyf = cam_noisy.astype(np.float32)
    assert_almost_equal(mean_squared_error(cam, cam_noisy),
                        mean_squared_error(camf, cam_noisyf))
    assert_almost_equal(normalized_root_mse(cam, cam_noisy),
                        normalized_root_mse(camf, cam_noisyf))
コード例 #2
0
def test_NRMSE_errors():
    x = np.ones(4)
    # shape mismatch
    with pytest.raises(ValueError):
        normalized_root_mse(x[:-1], x)
    # invalid normalization name
    with pytest.raises(ValueError):
        normalized_root_mse(x, x, normalization='foo')
コード例 #3
0
def test_NRMSE():
    x = np.ones(4)
    y = np.asarray([0., 2., 2., 2.])
    assert_equal(normalized_root_mse(y, x, 'mean'), 1 / np.mean(y))
    assert_equal(normalized_root_mse(y, x, 'Euclidean'), 1 / np.sqrt(3))
    assert_equal(normalized_root_mse(y, x, 'min-max'), 1 / (y.max() - y.min()))

    # mixed precision inputs are allowed
    assert_almost_equal(normalized_root_mse(y, np.float32(x), 'min-max'),
                        1 / (y.max() - y.min()))
コード例 #4
0
ファイル: test_inpaint.py プロジェクト: rfezzani/scikit-image
def test_inpaint_nrmse(dtype, order, channel_axis, split_into_regions):
    image_orig = data.astronaut()[:, :200]
    float_dtype = np.float32 if dtype == np.float32 else np.float64
    image_orig = image_orig.astype(float_dtype, copy=False)

    # Create mask with six block defect regions
    mask = np.zeros(image_orig.shape[:-1], dtype=bool)
    mask[20:50, 3:20] = 1
    mask[165:180, 90:155] = 1
    mask[40:60, 170:195] = 1
    mask[-60:-40, 170:195] = 1
    mask[-180:-165, 90:155] = 1
    mask[-50:-20, :20] = 1

    # add a few long, narrow defects
    mask[200:205, -200:] = 1
    mask[150:255, 20:22] = 1
    mask[365:368, 60:130] = 1

    # add randomly positioned small point-like defects
    rstate = np.random.default_rng(0)
    for radius in [0, 2, 4]:
        # larger defects are less common
        thresh = 3.25 + 0.25 * radius  # larger defects less commmon
        tmp_mask = rstate.standard_normal(image_orig.shape[:-1]) > thresh
        if radius > 0:
            tmp_mask = binary_dilation(tmp_mask, disk(radius, dtype=bool))
        mask[tmp_mask] = 1

    # Defect image over the same region in each color channel
    image_defect = image_orig.copy()
    for layer in range(image_defect.shape[-1]):
        image_defect[np.where(mask)] = 0

    if channel_axis is None:
        image_orig = rgb2gray(image_orig)
        image_defect = rgb2gray(image_defect)

    image_orig = image_orig.astype(dtype, copy=False)
    image_defect = image_defect.astype(dtype, copy=False)

    image_defect = np.asarray(image_defect, order=order)
    image_result = inpaint.inpaint_biharmonic(
        image_defect,
        mask,
        channel_axis=channel_axis,
        split_into_regions=split_into_regions)
    assert image_result.dtype == float_dtype

    nrmse_defect = normalized_root_mse(image_orig, image_defect)
    nrmse_result = normalized_root_mse(img_as_float(image_orig), image_result)
    assert nrmse_result < 0.2 * nrmse_defect
コード例 #5
0
ファイル: plots.py プロジェクト: michaelmendoza/ssfp
def plot_planet_results(mask, T1, T1est, T2, T2est, df, dfest):

    nx, ny = 3, 3
    plt.subplot(nx, ny, 1)
    plt.imshow(T1*mask)
    plt.title('T1 Truth')
    plt.axis('off')

    plt.subplot(nx, ny, 2)
    plt.imshow(T1est)
    plt.title('T1 est')
    plt.axis('off')

    plt.subplot(nx, ny, 3)
    plt.imshow(T1*mask - T1est)
    plt.title('NRMSE: %g' % normalized_root_mse(T1, T1est))
    plt.axis('off')

    plt.subplot(nx, ny, 4)
    plt.imshow(T2*mask)
    plt.title('T2 Truth')
    plt.axis('off')

    plt.subplot(nx, ny, 5)
    plt.imshow(T2est)
    plt.title('T2 est')
    plt.axis('off')

    plt.subplot(nx, ny, 6)
    plt.imshow(T2*mask - T2est)
    plt.title('NRMSE: %g' % normalized_root_mse(T2, T2est))
    plt.axis('off')

    plt.subplot(nx, ny, 7)
    plt.imshow(df*mask)
    plt.title('df Truth')
    plt.axis('off')

    plt.subplot(nx, ny, 8)
    plt.imshow(dfest)
    plt.title('df est')
    plt.axis('off')

    plt.subplot(nx, ny, 9)
    plt.imshow(df*mask - dfest)
    plt.title('NRMSE: %g' % normalized_root_mse(df*mask, dfest))
    plt.axis('off')

    plt.show()
コード例 #6
0
def test_NRMSE(dtype):
    x = np.ones(4, dtype=dtype)
    y = np.asarray([0., 2., 2., 2.], dtype=dtype)
    nrmse = normalized_root_mse(y, x, normalization='mean')
    assert nrmse.dtype == np.float64
    assert_equal(nrmse, 1 / np.mean(y))
    assert_equal(normalized_root_mse(y, x, normalization='euclidean'),
                 1 / np.sqrt(3))
    assert_equal(normalized_root_mse(y, x, normalization='min-max'),
                 1 / (y.max() - y.min()))

    # mixed precision inputs are allowed
    assert_almost_equal(
        normalized_root_mse(y, np.float32(x), normalization='min-max'),
        1 / (y.max() - y.min()))
コード例 #7
0
 def __call__(img1, img2, tensor=True):
     if tensor:
         img1 = convert_tensor_to_nparray(img1)
         img2 = convert_tensor_to_nparray(img2)
     img1 = remove_outliers_eval(img1)
     img2 = remove_outliers_eval(img2)
     return normalized_root_mse(img1, img2)
コード例 #8
0
ファイル: net_old.py プロジェクト: GoldiolBack/TFG
def test(args, test_loader, model, device):
    model.eval()
    test_loss = 0
    rmse = 0
    psnr = 0
    ssim = 0
    with torch.no_grad():
        for hr, lr, target in test_loader:
            lr, hr, target = lr.to(device), hr.to(device), target.to(device)
            output = model(hr, lr)
            test_loss_function = nn.L1Loss(reduction='sum')
            test_loss = test_loss_function(output, target).item()
            real = np.moveaxis(target.numpy(), 1, 3)
            predicted = np.moveaxis(output.numpy(), 1, 3)
            rmse += skm.normalized_root_mse(real, predicted)
            psnr += skm.peak_signal_noise_ratio(real,
                                                predicted,
                                                data_range=real.max() -
                                                real.min())
            for i in range(5):
                ssim += skm.structural_similarity(real[i],
                                                  predicted[i],
                                                  multichannel=True,
                                                  data_range=real.max() -
                                                  real.min())

    test_loss /= len(test_loader.dataset)
    rmse /= len(test_loader.dataset)
    psnr /= len(test_loader.dataset)
    ssim /= len(test_loader.dataset)

    print(
        '\nTest set: Average values --> Loss: {:.4f}, RMSE: ({:.2f}), PSNR: ({:.2f}dB),'
        ' SSIM: ({:.2f})\n'.format(test_loss, rmse, psnr, ssim))
コード例 #9
0
    def test_image_align_precise(self, precise, instance, image_ref,
                                 image_translated):
        tolerance = 0.1 if precise else 0.12
        result = instance.align(image_translated, precise=precise)

        assert result.shape == image_ref.shape
        assert round(normalized_root_mse(image_ref, result), 5) < tolerance
コード例 #10
0
def evaluate(individual: List[float], ref_img: np.ndarray) -> Tuple[float]:
    im_shape = ref_img.shape
    gen_img = express_genome_to_image(np.array(individual), im_shape)
    gen_array = np.array(gen_img)
    gen_array[:, :, 3] = 255
    error = normalized_root_mse(ref_img, gen_array)
    return error.item(),
コード例 #11
0
ファイル: sandbox_planet.py プロジェクト: michaelmendoza/ssfp
def planet_phantom_example():

    TR, alpha = 24e-3, np.deg2rad(70)

    sig, df = load_data()
    coil_index = 0
    sig = sig[:, :, coil_index, :]
    df = df[:, :, coil_index]

    sig = sig.transpose((2, 0, 1))  # Move pc to 0 index
    sig = sig[..., None]

    # Do T1, T2 mapping for each pixel
    mask = np.abs(sig[1, :, :, :]) > 5e-8

    print('-------')
    print(sig.shape)
    print(df.shape)
    print(mask.shape)

    # Do the thing
    t0 = perf_counter()
    Mmap, T1est, T2est, dfest = planet(sig, alpha, TR, pc_axis=0)
    print('Took %g sec to run PLANET' % (perf_counter() - t0))

    print(sig.shape)
    print(df.shape)
    print(dfest.shape)

    # Simple phase unwrapping of off-resonance estimate
    dfest = unwrap_phase(dfest * 2 * np.pi * TR) / (2 * np.pi * TR)

    nx, ny = 3, 3

    plt.subplot(nx, ny, 2)
    plt.imshow(T1est)
    plt.title('T1 est')
    plt.axis('off')

    plt.subplot(nx, ny, 5)
    plt.imshow(T2est)
    plt.title('T2 est')
    plt.axis('off')

    plt.subplot(nx, ny, 7)
    plt.imshow(np.abs(df))
    plt.title('df Truth')
    plt.axis('off')

    plt.subplot(nx, ny, 8)
    plt.imshow(np.abs(dfest))
    plt.title('df est')
    plt.axis('off')

    plt.subplot(nx, ny, 9)
    plt.imshow(np.abs(df - dfest[:, :, 0]))
    plt.title('NRMSE: %g' % normalized_root_mse(df, dfest[:, :, 0]))
    plt.axis('off')

    plt.show()
コード例 #12
0
ファイル: Compare.py プロジェクト: elbrandt/CS766_Project
def compare_imgs(im1, im2, fil_out):
    with warnings.catch_warnings():
        warnings.simplefilter(
            "ignore")  # ignore skimage's deprecation warinings.

        # structural similarity index
        # [ss, im] = metrics.structural_similarity(im1,im2,multichannel=True)
        [ss, im] = metrics.structural_similarity(im1,
                                                 im2,
                                                 gaussian_weights=True,
                                                 sigma=1.5,
                                                 use_sample_covariance=False,
                                                 multichannel=True,
                                                 full=True)
        if fil_out:
            im8 = skimage.img_as_ubyte(np.clip(im, -1.0, 1.0))
            save_img(fil_out, im8)

        # mean square error
        mse = metrics.mean_squared_error(im1, im2)

        # normalized root mean squared error
        nrmse = metrics.normalized_root_mse(im1, im2)

        # peak signal-to-noise ratio
        psnr = metrics.peak_signal_noise_ratio(im1, im2)

        return [ss, mse, nrmse, psnr]
コード例 #13
0
def compute_metrics(seriesX, seriesY, metric, window):
    # Smooth the data if needed
    if window != 'none':
        seriesX = smooth(x=seriesX, window_len=5, window=window)
        seriesY = smooth(x=seriesY, window_len=5, window=window)

    if seriesX.shape != seriesY.shape:
        raise ValueError('Only accepts signals that have the same shape.')

    # Compute the similarity metrics
    if metric == 'zncc': return getZNCC(seriesX, seriesY)
    if metric == 'ssim':
        return ski_metrics.structural_similarity(seriesX, seriesY)
    if metric == 'psnr': return getPSNR(seriesX, seriesY)
    if metric == 'f-test': return stats.f_oneway(seriesX, seriesY)[0]

    seriesX, seriesY = pd.Series(seriesX), pd.Series(seriesY)

    if metric == 'pearsonr': return seriesX.corr(seriesY, method='pearson')
    if metric == 'spearmanr': return seriesX.corr(seriesY, method='spearman')
    if metric == 'kendalltau': return seriesX.corr(seriesY, method='kendall')

    # Compute dissimilarity metrics
    if metric == 'mse': return ski_metrics.mean_squared_error(seriesX, seriesY)
    if metric == 'nrmse':
        return ski_metrics.normalized_root_mse(seriesX, seriesY)
    if metric == 'me': return getME(seriesX, seriesY)
    if metric == 'mae': return getMAE(seriesX, seriesY)
    if metric == 'msle': return getMSLE(seriesX, seriesY)
    if metric == 'medae': return getMedAE(seriesX, seriesY)
コード例 #14
0
def compute_metrics(df1, df2, metrics, window):
    number_of_columns1 = len(df1.columns)
    number_of_columns2 = len(df2.columns)

    # { var1_names, var2_names, metric1: 2d array, metric2: ... }}
    result = {
        'var1_names': df1.columns[1:].tolist(),
        'var2_names': df2.columns[1:].tolist()
    }
    for m in metrics:
        result[m] = np.zeros((number_of_columns1 - 1, number_of_columns2 - 1))

    # Using nested for loops to create a 2-D matrix
    for i in range(number_of_columns1 - 1):
        for j in range(number_of_columns2 - 1):
            X = df1.columns[i + 1]
            Y = df2.columns[j + 1]

            # Remove missing data if needed
            seriesX = removeMissingData(df1[X])
            seriesY = removeMissingData(df2[Y])

            # Smooth the data if needed
            if window != 'none':
                seriesX = smooth(x=seriesX, window_len=5, window=window)
                seriesY = smooth(x=seriesY, window_len=5, window=window)

            if seriesX.shape != seriesY.shape:
                raise ValueError('Only accepts signals that have the same shape.')

            # Compute the similarity metrics
            if 'zncc' in metrics: result['zncc'][i][j] = getZNCC(seriesX, seriesY)
            if 'ssim' in metrics: result['ssim'][i][j] = ski_metrics.structural_similarity(seriesX, seriesY)
            if 'psnr' in metrics: result['psnr'][i][j] = getPSNR(seriesX, seriesY)

            # Note: added by Phong
            if 'f-test' in metrics: result['f-test'][i][j] = stats.f_oneway(seriesX, seriesY)[0]

            # Use pandas corr to ingnore inf and nan.
            seriesX, seriesY = pd.Series(seriesX), pd.Series(seriesY)

            if 'pearsonr' in metrics: result['pearsonr'][i][j] = seriesX.corr(seriesY, method='pearson')
            if 'spearmanr' in metrics: result['spearmanr'][i][j] = seriesX.corr(seriesY, method='spearman')
            if 'kendalltau' in metrics: result['kendalltau'][i][j] = seriesX.corr(seriesY, method='kendall')

            # Compute dissimilarity metrics
            if 'mse' in metrics: result['mse'][i][j] = ski_metrics.mean_squared_error(seriesX, seriesY)
            if 'nrmse' in metrics: result['nrmse'][i][j] = ski_metrics.normalized_root_mse(seriesX, seriesY)
            if 'me' in metrics: result['me'][i][j] = getME(seriesX, seriesY)
            if 'mae' in metrics: result['mae'][i][j] = getMAE(seriesX, seriesY)
            if 'msle' in metrics: result['msle'][i][j] = getMSLE(seriesX, seriesY)
            if 'medae' in metrics: result['medae'][i][j] = getMedAE(seriesX, seriesY)

    # Note: added by Phong
    # Replace NaN with 0 for serialisation
    for m in metrics:
        result[m] = np.nan_to_num(result[m])

    return result
コード例 #15
0
def parallel_comparison(args):
    orig_path, comp_path = args
    orig_img = io.imread(orig_path)
    comp_img = io.imread(comp_path)
    comparisons = []
    comparisons.append(
        metrics.structural_similarity(orig_img, comp_img, multichannel=True))
    comparisons.append(metrics.peak_signal_noise_ratio(orig_img, comp_img))
    comparisons.append(metrics.mean_squared_error(orig_img, comp_img))
    comparisons.append(metrics.normalized_root_mse(orig_img, comp_img))
    return comparisons
コード例 #16
0
def calc_metircs(gt, out, tag):
    ssim, ssimMAP = structural_similarity(gt, out, data_range=1, full=True)
    nrmse = normalized_root_mse(gt, out)
    psnr = peak_signal_noise_ratio(gt, out, data_range=1)
    uqi = UQICalc(gt, out)
    metrics = {
        "SSIM" + tag: ssim,
        "NRMSE" + tag: nrmse,
        "PSNR" + tag: psnr,
        "UQI" + tag: uqi
    }
    return metrics, ssimMAP
コード例 #17
0
ファイル: metrics.py プロジェクト: JonasFreibur/ledebruiteur
def metrics_example(dataframe, noise_class_list):
    """Show metrics example 

    Arguments:
        dataframe {Dataframe} -- Dataframe that contains images path
        noise_class_list {List} -- List of noise type

    Returns:
        Dataframe -- Dataframe that contain metrics example
    """
    path = dataframe.iloc[0, 0]
    orignal_img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    orignal_img = np.array(orignal_img, np.float32)

    images = [orignal_img]

    df_error = pd.DataFrame({
        "Noise": [],
        "MSE": [],
        "NRMSE": [],
        "PSNR": [],
        "SSIM": []
    })

    for noise in noise_class_list:
        noised_img = noise.add(orignal_img)
        images.append(noised_img)

        noise_name = noise.__class__.__name__
        mse = metrics.mean_squared_error(orignal_img, noised_img)
        nrmse = metrics.normalized_root_mse(orignal_img, noised_img)
        psnr = metrics.peak_signal_noise_ratio(orignal_img,
                                               noised_img,
                                               data_range=255)
        ssim = metrics.structural_similarity(orignal_img, noised_img)

        df_error = df_error.append(
            {
                "Noise": noise_name,
                "MSE": mse,
                "NRMSE": nrmse,
                "PSNR": psnr,
                "SSIM": ssim
            },
            ignore_index=True)

    plot_im_grid_from_list(images, 5, 2)
    df_error.head(len(noise_class_list))
    return df_error
コード例 #18
0
def calc_metircs(gt, out, tag, norm4diff=False):
    ssim, ssimMAP = structural_similarity(gt, out, data_range=1, full=True)
    nrmse = normalized_root_mse(gt, out)
    psnr = peak_signal_noise_ratio(gt, out, data_range=1)
    uqi = UQICalc(gt, out)
    if norm4diff:
        gt = minmax(gt)
        out = minmax(out)
    diff = gt - out
    dif_std = np.std(diff)
    metrics = {
        "SSIM"+tag: ssim,
        "NRMSE"+tag: nrmse,
        "PSNR"+tag: psnr,
        "UQI"+tag: uqi,
        "SDofDiff"+tag: dif_std
    }
    return metrics, ssimMAP, abs(diff)
コード例 #19
0
ファイル: metrics.py プロジェクト: JonasFreibur/ledebruiteur
def compare_images(original_img, transformed_img):
    """Compares original image to transformed image 

    Arguments:
        orignal_img {Array} -- Numpy like array of image [Non-normalize (0-255)]
        transformed_img {Array} -- Numpy like array of image [Non-normalize (0-255)]

    Returns:
        dict -- {"MSE", "NRMSE", "PSNR", "SSIM"}
    """
    original_img = np.array(original_img, np.float32)
    transformed_img = np.array(transformed_img, np.float32)

    mse = metrics.mean_squared_error(original_img, transformed_img)
    nrmse = metrics.normalized_root_mse(original_img, transformed_img)
    ssim = metrics.structural_similarity(original_img, transformed_img)
    psnr = metrics.peak_signal_noise_ratio(original_img,
                                           transformed_img,
                                           data_range=255)

    return {"MSE": mse, "NRMSE": nrmse, "PSNR": psnr, "SSIM": ssim}
コード例 #20
0
ファイル: Gamma.py プロジェクト: BishopWolf/GammaIndex
    def skimage_metrics(self, im1, im2):
        ## Metric 1: MSE (Mean Squared Error)
        mse = compare.mean_squared_error(im1, im2)
        ## Metric 1.1: NRMSE (Normalized Root Mean Squared Error)
        nrmse = compare.normalized_root_mse(im1,
                                            im2,
                                            normalization='euclidean')
        ## Metric 2: SSIM (Structural Similarity Image Matrix)
        ssim = compare.structural_similarity(im1,
                                             im2,
                                             data_range=im2.max() - im2.min())
        ## Metric 3: Peak Signal-to-Noise ratio
        if mse > 0:
            psnr = compare.peak_signal_noise_ratio(im1,
                                                   im2,
                                                   data_range=im2.max() -
                                                   im2.min())
        else:
            psnr = -1

        return mse, nrmse, ssim, psnr
コード例 #21
0
def test(args, test_loader, model, device, epoch):
    model.eval()
    test_loss = 0
    rmse = 0
    psnr = 0
    ssim = 0
    with torch.no_grad():
        for hr, lr, target in test_loader:
            hr_crop, lr_crop, target_crop = crop(hr, lr, target, 128, 64, 128)
            lr_crop, hr_crop, target_crop = lr_crop.to(device), hr_crop.to(
                device), target_crop.to(device)
            output = model(hr_crop, lr_crop)
            test_loss_function = nn.L1Loss(reduction='sum')
            test_loss = test_loss_function(output, target_crop).item()
            real = np.moveaxis(target_crop.cpu().numpy(), 1, 3)
            predicted = np.moveaxis(output.cpu().numpy(), 1, 3)
            for i in range(args.test_batch_size):
                rmse += skm.normalized_root_mse(real[i], predicted[i])
                psnr += skm.peak_signal_noise_ratio(real[i],
                                                    predicted[i],
                                                    data_range=real.max() -
                                                    real.min())
                ssim += skm.structural_similarity(real[i],
                                                  predicted[i],
                                                  multichannel=True,
                                                  data_range=real.max() -
                                                  real.min())
            # tb.add_scalar("Loss/test", test_loss, epoch)
            # tb.add_scalar("RMSE/test", rmse/5, epoch)
            # tb.add_scalar("PSNR/test", psnr/5, epoch)
            # tb.add_scalar("SSIM/test", ssim/5, epoch)

    test_loss /= len(test_loader.dataset)
    rmse /= len(test_loader.dataset)
    psnr /= len(test_loader.dataset)
    ssim /= len(test_loader.dataset)

    print(
        '\nTest set: Average values --> Loss: {:.4f}, RMSE: ({:.2f}), PSNR: ({:.2f}dB),'
        ' SSIM: ({:.2f})\n'.format(test_loss, rmse, psnr, ssim))
コード例 #22
0
def validation(args, val_loader, model, device):
    model.eval()
    val_loss = 0
    rmse = 0
    psnr = 0
    ssim = 0
    with torch.no_grad():
        for hr, lr, target in val_loader:
            hr_crop, lr_crop, target_crop = crop(hr, lr, target, 128, 64, 128)
            lr_crop, hr_crop, target_crop = lr_crop.to(device), hr_crop.to(
                device), target_crop.to(device)
            output = model(hr_crop, lr_crop)
            val_loss_function = nn.L1Loss(reduction='sum')
            val_loss = val_loss_function(output, target_crop).item()
            real = np.moveaxis(target_crop.cpu().numpy(), 1, 3)
            predicted = np.moveaxis(output.cpu().numpy(), 1, 3)
            for i in range(args.test_batch_size):
                rmse += skm.normalized_root_mse(real[i], predicted[i])
                psnr += skm.peak_signal_noise_ratio(real[i],
                                                    predicted[i],
                                                    data_range=real.max() -
                                                    real.min())
                ssim += skm.structural_similarity(real[i],
                                                  predicted[i],
                                                  multichannel=True,
                                                  data_range=real.max() -
                                                  real.min())

    val_loss /= len(val_loader.dataset)
    rmse /= len(val_loader.dataset)
    psnr /= len(val_loader.dataset)
    ssim /= len(val_loader.dataset)

    print(
        '\nValidation set: Average values --> Loss: {:.4f}, RMSE: ({:.2f}), PSNR: ({:.2f}dB),'
        ' SSIM: ({:.2f})\n'.format(val_loss, rmse, psnr, ssim))

    np.save('val_input.npy', (np.moveaxis(lr_crop.cpu().numpy(), 1, 3)))
    np.save('val_real.npy', real)
    np.save('val_output.npy', predicted)
コード例 #23
0
	def __call__(self, input, target):
		input, target = convert_to_numpy(input, target)
		return normalized_root_mse(target, input)
コード例 #24
0
grayA = cv2.imread(fst_file, 0)
grayB = cv2.imread(snd_file, 0)
grayBG = cv2.imread(bg_file, 0)

# Remove background and threshold to remove shadow effects
threshold = 20

diffA = cv2.absdiff(grayA, grayBG)
thresA = cv2.threshold(diffA, threshold, 255, cv2.THRESH_BINARY)[1]

diffB = cv2.absdiff(grayB, grayBG)
thresB = cv2.threshold(diffB, threshold, 255, cv2.THRESH_BINARY)[1]

# compute the Normalised Root Mean-Squared Error (NRMSE) between the two
# images
score = normalized_root_mse(thresA, thresB)

# Compare the current image with the image from 5 layers ago
# This is used to check for filament runout or huge deviance
deviance = 1.0
scr_diff = 0.0
dev_diff = 0.0
if curr > 5:
    trd_file = fst_file[:-10] + str(curr - 5).rjust(6, '0') + fst_file[-4:]
    #imageC = cv2.imread(trd_file)
    grayC = cv2.imread(trd_file, 0)
    diffC = cv2.absdiff(grayC, grayBG)
    thresC = cv2.threshold(diffC, threshold, 255, cv2.THRESH_BINARY)[1]
    deviance = normalized_root_mse(thresA, thresC)

    # Calculate difference compared with previous layer score and deviance
コード例 #25
0
ファイル: basic_fimtre.py プロジェクト: mckib2/ssfp
        Ip8_hi += sigma * (np.random.normal(0, 1, Ip8_hi.shape) +
                           1j * np.random.normal(0, 1, Ip8_hi.shape))
        Ip6_lo += sigma * (np.random.normal(0, 1, Ip6_lo.shape) +
                           1j * np.random.normal(0, 1, Ip6_lo.shape))
        Ip8_lo += sigma * (np.random.normal(0, 1, Ip8_lo.shape) +
                           1j * np.random.normal(0, 1, Ip8_lo.shape))

    # Do the thing
    if fimtre_results:
        theta6_hi = fimtre(I0_hi, I1_6_hi, TR0, TR1, rad=False) * mask
        theta8_hi = fimtre(I0_hi, I1_8_hi, TR0, TR1, rad=False) * mask
        theta6_lo = fimtre(I0_lo, I1_6_lo, TR0, TR1, rad=False) * mask
        theta8_lo = fimtre(I0_lo, I1_8_lo, TR0, TR1, rad=False) * mask

        # reverse polarity if it makes sense
        if normalized_root_mse(theta6_hi, df) > normalized_root_mse(
                -1 * theta6_hi, df):
            theta6_hi *= -1
        if normalized_root_mse(theta8_hi, df) > normalized_root_mse(
                -1 * theta8_hi, df):
            theta8_hi *= -1
        if normalized_root_mse(theta6_lo, df) > normalized_root_mse(
                -1 * theta6_lo, df):
            theta6_lo *= -1
        if normalized_root_mse(theta8_lo, df) > normalized_root_mse(
                -1 * theta8_lo, df):
            theta8_lo *= -1

    if planet_results:
        _Meff, _T1, _T2, theta_planet6_hi = planet(
            Ip6_hi, TR=TR0, alpha=alpha, pc_axis=-1) * mask
コード例 #26
0
ファイル: sandbox_planet.py プロジェクト: michaelmendoza/ssfp
def planet_shepp_logan_example():

    # Shepp-Logan
    N, nslices, npcs = 128, 1, 8  # 2 slices just to show we can
    M0, T1, T2 = shepp_logan((N, N, nslices), MR=True, zlims=(-.25, 0))

    # Simulate bSSFP acquisition with linear off-resonance
    TR, alpha = 3e-3, np.deg2rad(15)
    pcs = np.linspace(0, 2 * np.pi, npcs, endpoint=False)
    df, _ = np.meshgrid(np.linspace(-1 / TR, 1 / TR, N),
                        np.linspace(-1 / TR, 1 / TR, N))
    sig = np.empty((npcs, ) + T1.shape, dtype='complex')
    for sl in range(nslices):
        sig[..., sl] = bssfp(T1[..., sl],
                             T2[..., sl],
                             TR,
                             alpha,
                             field_map=df,
                             phase_cyc=pcs,
                             M0=M0[..., sl])

    # Do T1, T2 mapping for each pixel
    mask = np.abs(M0) > 1e-8

    # Make it noisy
    np.random.seed(0)
    sig += 1e-5 * (np.random.normal(0, 1, sig.shape) +
                   1j * np.random.normal(0, 1, sig.shape)) * mask

    print(sig.shape, alpha, TR, mask.shape)

    # Show the phase-cycled images
    nx, ny = 2, 4
    plt.figure()
    for ii in range(nx * ny):
        plt.subplot(nx, ny, ii + 1)
        plt.imshow(np.abs(sig[ii, :, :, 0]))
        plt.title('%d deg PC' % (ii * (360 / npcs)))
    plt.show()

    # Do the thing
    t0 = perf_counter()
    Mmap, T1est, T2est, dfest = planet(sig, alpha, TR, mask=mask, pc_axis=0)
    print('Took %g sec to run PLANET' % (perf_counter() - t0))

    print(T1est.shape, T2est.shape, dfest.shape, T1.shape, T2.shape,
          mask.shape)

    # Look at a single slice
    sl = 0
    T1est = T1est[..., sl]
    T2est = T2est[..., sl]
    dfest = dfest[..., sl]
    T1 = T1[..., sl]
    T2 = T2[..., sl]
    mask = mask[..., sl]

    # Simple phase unwrapping of off-resonance estimate
    dfest = unwrap_phase(dfest * 2 * np.pi * TR) / (2 * np.pi * TR)

    print('t1, mask:', T1.shape, mask.shape)

    nx, ny = 3, 3
    plt.subplot(nx, ny, 1)
    plt.imshow(T1 * mask)
    plt.title('T1 Truth')
    plt.axis('off')

    plt.subplot(nx, ny, 2)
    plt.imshow(T1est)
    plt.title('T1 est')
    plt.axis('off')

    plt.subplot(nx, ny, 3)
    plt.imshow(T1 * mask - T1est)
    plt.title('NRMSE: %g' % normalized_root_mse(T1, T1est))
    plt.axis('off')

    plt.subplot(nx, ny, 4)
    plt.imshow(T2 * mask)
    plt.title('T2 Truth')
    plt.axis('off')

    plt.subplot(nx, ny, 5)
    plt.imshow(T2est)
    plt.title('T2 est')
    plt.axis('off')

    plt.subplot(nx, ny, 6)
    plt.imshow(T2 * mask - T2est)
    plt.title('NRMSE: %g' % normalized_root_mse(T2, T2est))
    plt.axis('off')

    plt.subplot(nx, ny, 7)
    plt.imshow(df * mask)
    plt.title('df Truth')
    plt.axis('off')

    plt.subplot(nx, ny, 8)
    plt.imshow(dfest)
    plt.title('df est')
    plt.axis('off')

    plt.subplot(nx, ny, 9)
    plt.imshow(df * mask - dfest)
    plt.title('NRMSE: %g' % normalized_root_mse(df * mask, dfest))
    plt.axis('off')

    plt.show()
コード例 #27
0
def normalized_rmse(img_a, img_b):
    return sk_metrics.normalized_root_mse(img_a, img_b)
コード例 #28
0
def brain_planet_virtual_ellipse_example():
    N = 128
    filepath = './data'
    data = load_dataslice(filepath, image_index=1, slice_index=150)

    freq = 1 / 3e-3
    offres = generate_offres(N, f=freq, rotate=True, deform=True)

    # alpha = flip angle
    alpha = np.deg2rad(100)

    #Create brain phantom
    phantom = generate_phantom(data, alpha, offres=offres)

    #Get phantom parameter
    M0, T1, T2, _alpha, df, _sample = get_phantom_parameters(phantom)

    # Generate phase-cycled images
    TR = 3e-3
    TE = TR / 2
    pcs = np.linspace(0, 2 * np.pi, 4, endpoint=False)
    M = ma_ssfp(T1, T2, TR, TE, alpha, f0=-df, dphi=pcs, M0=M0)
    #M = add_noise_gaussian(M, sigma=0.0)
    M = np.transpose(M, (2, 0, 1))
    M = M[..., None]

    TR1 = 3.45e-3
    TE1 = TR / 2
    pcs = np.linspace(0, 2 * np.pi, 4, endpoint=False)
    M1 = ma_ssfp(T1, T2, TR1, TE1, alpha, f0=-df, dphi=pcs, M0=M0)
    M1 = np.transpose(M1, (2, 0, 1))
    M1 = M1[..., None]

    # Do T1, T2 mapping for each pixel
    mask = np.abs(M0) > 1e-8
    mask = mask[..., None]
    print(M.shape, alpha, TR, mask.shape)

    # Look at a single slice
    sl = 0
    mask = mask[..., sl]

    print(M.shape, M1.shape, M.shape[-1], M1.shape[-1])
    phi = ormtre(M, M1, mask, TR, TR1, pc_axis=0, rad=True)

    plt.figure()
    plt.imshow(phi)
    plt.show()

    print('M, phi', M.shape, phi[..., None].shape)
    v = np.empty(M.shape, dtype=M.dtype)
    v[..., :4] = M
    v[..., 4:] = M1 * np.exp(1j * phi[None, ...])

    print('v', v.shape)

    t0 = perf_counter()
    Mmap, T1est, T2est, dfest = planet(v,
                                       alpha=alpha,
                                       TR=(TR + TR1) / 2,
                                       mask=mask,
                                       pc_axis=0)
    print('Took %g sec to run PLANET' % (perf_counter() - t0))

    # Look at a single slice
    sl = 0
    T1est = T1est[..., sl]
    T2est = T2est[..., sl]
    dfest = dfest[..., sl]
    #T1 = T1[..., sl]
    #T2 = T2[..., sl]
    mask = mask[..., sl]

    # Simple phase unwrapping of off-resonance estimate
    dfest = unwrap_phase(dfest * 2 * np.pi * TR) / (2 * np.pi * TR)

    print('t1, mask:', T1.shape, mask.shape)

    nx, ny = 3, 3
    plt.subplot(nx, ny, 1)
    plt.imshow(T1 * mask)
    plt.title('T1 Truth')
    plt.axis('off')

    plt.subplot(nx, ny, 2)
    plt.imshow(T1est)
    plt.title('T1 est')
    plt.axis('off')

    plt.subplot(nx, ny, 3)
    plt.imshow(T1 * mask - T1est)
    plt.title('NRMSE: %g' % normalized_root_mse(T1, T1est))
    plt.axis('off')

    plt.subplot(nx, ny, 4)
    plt.imshow(T2 * mask)
    plt.title('T2 Truth')
    plt.axis('off')

    plt.subplot(nx, ny, 5)
    plt.imshow(T2est)
    plt.title('T2 est')
    plt.axis('off')

    plt.subplot(nx, ny, 6)
    plt.imshow(T2 * mask - T2est)
    plt.title('NRMSE: %g' % normalized_root_mse(T2, T2est))
    plt.axis('off')

    plt.subplot(nx, ny, 7)
    plt.imshow(df * mask)
    plt.title('df Truth')
    plt.axis('off')

    plt.subplot(nx, ny, 8)
    plt.imshow(dfest)
    plt.title('df est')
    plt.axis('off')

    plt.subplot(nx, ny, 9)
    plt.imshow(df * mask - dfest)
    plt.title('NRMSE: %g' % normalized_root_mse(df * mask, dfest))
    plt.axis('off')

    plt.show()
コード例 #29
0
def evaluation(model_name, weights, data_loader, use_cuda=False):
    '''
    Evaluate a model.

    Parameters
    ----------
    model_name : str
        Name of the model to be evaluated.
    weights : str
        Path to weights for the initialisation of the model. If None, weights
        are randomly initialized.
    data_loader : torch.utils.data.dataloader.DataLoader
        Data loader of the dataset.
    use_cuda : bool, optional
        Whether or not to use CUDA. The default is False.

    Raises
    ------
    NameError
        If the model is not recognised.

    Returns
    -------
    df : pandas.DataFrame
        Data frame with the evaluation data.

    '''
    if model_name == "Zhang16":
        model = Zhang16(weights=weights)
        resize = transforms.Resize((256, 256))
        process_output = lambda output, data: lab2rgb(
            torch.cat((data.cpu(), resize(z2ab(output.cpu()))), dim=1))
    elif model_name == "Su20":
        model = Su20(weights=weights)
        resize = transforms.Resize((256, 256))
        process_output = lambda output, data: lab2rgb(
            torch.cat((data.cpu(), resize(z2ab(output.cpu()))), dim=1))
    elif model_name == "Collage":
        model = Collage(weights=weights)
        process_output = lambda output, data: lab2rgb(
            torch.cat((data, output), dim=1).cpu())
    else:
        raise NameError(model_name)
    if use_cuda:
        print("Using GPU.")
        model.cuda()
    else:
        print("Using CPU.")
    model.eval()

    df = pd.DataFrame(columns=['name', 'nrmse', 'ssim', 'psnr', 'lpips'],
                      index=range(len(data_loader.dataset)))
    idx = 0
    lpips_loss = LPIPS(net='alex')
    for ite, (names, (data, target)) in enumerate(tqdm(data_loader)):
        if use_cuda:
            data, target = data.cuda(), target.cuda()
        output = model(data)
        images_true = lab2rgb(torch.cat((data, target), axis=1).cpu())
        images_test = process_output(output, data)
        lpips_values = lpips_loss(images_true * 2. - 1., images_test * 2. - 1.)
        for i, name in enumerate(names):
            image_true = images_true[i].permute(1, 2, 0).numpy()
            image_test = images_test[i].permute(1, 2, 0).numpy()
            nrmse = normalized_root_mse(image_true, image_test)
            ssim = structural_similarity(image_true,
                                         image_test,
                                         data_range=1.,
                                         multichannel=True)
            psnr = peak_signal_noise_ratio(image_true,
                                           image_test,
                                           data_range=1.)
            df.loc[idx, 'name'] = name
            df.loc[idx, 'nrmse'] = nrmse
            df.loc[idx, 'ssim'] = ssim
            df.loc[idx, 'psnr'] = psnr
            df.loc[idx, 'lpips'] = lpips_values[i].item()
            idx += 1

    return df
コード例 #30
0
def brain_planet_example():
    N = 128
    npcs = 8
    filepath = './data'
    data = load_dataslice(filepath, image_index=1, slice_index=150)

    freq = 1 / 3e-3
    offres = generate_offres(N, f=freq, rotate=True, deform=True)

    # alpha = flip angle
    alpha = np.deg2rad(15)

    #Create brain phantom
    phantom = generate_phantom(data, alpha, offres=offres)

    #Get phantom parameter
    M0, T1, T2, _alpha, df, _sample = get_phantom_parameters(phantom)

    # Generate phase-cycled images
    TR = 3e-3
    TE = TR / 2
    pcs = np.linspace(0, 2 * np.pi, npcs, endpoint=False)
    M = ma_ssfp(T1, T2, TR, TE, alpha, f0=-df, dphi=pcs, M0=M0)
    #M = add_noise_gaussian(M, sigma=0.0)
    M = np.transpose(M, (2, 0, 1))
    M = M[..., None]

    # Do T1, T2 mapping for each pixel
    mask = np.abs(M0) > 1e-8
    mask = mask[..., None]

    print(M.shape, alpha, TR, mask.shape)

    # Show the phase-cycled images
    nx, ny = 2, 4
    plt.figure()
    for ii in range(nx * ny):
        plt.subplot(nx, ny, ii + 1)
        plt.imshow(np.abs(M[ii, :, :, 0]))
        plt.title('%d deg PC' % (ii * (360 / npcs)))
    plt.show()

    # Do the thing
    t0 = perf_counter()
    Mmap, T1est, T2est, dfest = planet(M, alpha, TR, mask=mask, pc_axis=0)
    print('Took %g sec to run PLANET' % (perf_counter() - t0))

    # Look at a single slice

    print(T1est.shape, T2est.shape, dfest.shape, T1.shape, T2.shape,
          mask.shape)
    sl = 0
    T1est = T1est[..., sl]
    T2est = T2est[..., sl]
    dfest = dfest[..., sl]
    #T1 = T1[..., sl]
    #T2 = T2[..., sl]
    mask = mask[..., sl]

    # Simple phase unwrapping of off-resonance estimate
    dfest = unwrap_phase(dfest * 2 * np.pi * TR) / (2 * np.pi * TR)

    print('t1, mask:', T1.shape, mask.shape)

    nx, ny = 3, 3
    plt.subplot(nx, ny, 1)
    plt.imshow(T1 * mask)
    plt.title('T1 Truth')
    plt.axis('off')

    plt.subplot(nx, ny, 2)
    plt.imshow(T1est)
    plt.title('T1 est')
    plt.axis('off')

    plt.subplot(nx, ny, 3)
    plt.imshow(T1 * mask - T1est)
    plt.title('NRMSE: %g' % normalized_root_mse(T1, T1est))
    plt.axis('off')

    plt.subplot(nx, ny, 4)
    plt.imshow(T2 * mask)
    plt.title('T2 Truth')
    plt.axis('off')

    plt.subplot(nx, ny, 5)
    plt.imshow(T2est)
    plt.title('T2 est')
    plt.axis('off')

    plt.subplot(nx, ny, 6)
    plt.imshow(T2 * mask - T2est)
    plt.title('NRMSE: %g' % normalized_root_mse(T2, T2est))
    plt.axis('off')

    plt.subplot(nx, ny, 7)
    plt.imshow(df * mask)
    plt.title('df Truth')
    plt.axis('off')

    plt.subplot(nx, ny, 8)
    plt.imshow(dfest)
    plt.title('df est')
    plt.axis('off')

    plt.subplot(nx, ny, 9)
    plt.imshow(df * mask - dfest)
    plt.title('NRMSE: %g' % normalized_root_mse(df * mask, dfest))
    plt.axis('off')

    plt.show()