Esempio n. 1
0
def compare_classifications(df):
    from Filters.screening import FileFilter

    ngridpts = int(np.sqrt(len(df)))
    fig, axs = plt.subplots(ngridpts, ngridpts)
    axs = axs.reshape((-1, ))

    for i, file in enumerate(df.iterrows()):
        filterer = FileFilter()
        filterer.assess_file(file[1]["File Path"])

        show_image(
            filterer.binarized_data,
            axis=axs[i],
            title=
            f"Steff = {file[1]['Regime']} | CNN = {file[1]['CNN Classification']}"
        )
Esempio n. 2
0
def pick_random_images(dir, n_ims):
    from Filters.screening import FileFilter

    df_summary = pd.DataFrame(columns=["File Name", "Classification"])
    all_files = [f for f in glob.glob(f"{dir}/**/*.ibw", recursive=True)]
    rand_files = np.random.choice(all_files, n_ims)

    for i, filepath in enumerate(tqdm(rand_files)):
        filterer = FileFilter()
        _, _, _, _, flattened_data, _, _, _ = filterer._load_and_preprocess(
            filepath=filepath, threshold_method="otsu")

        if flattened_data is not None:
            plt.imsave(f"Data/Random_Images/{os.path.basename(filepath)}.png",
                       flattened_data,
                       cmap="RdGy")
            df_summary.loc[i, ["File Name"]] = [os.path.basename(filepath)]

    return df_summary
Esempio n. 3
0
def minkowski_stability_test(filepath, window_size, save):
    from Filters.screening import FileFilter
    from skimage import measure
    from Analysis.plot_rabani import show_image

    filterer = FileFilter()
    _, _, _, _, _, data, _, _ = filterer._load_and_preprocess(
        filepath=filepath, threshold_method="multiotsu", nbins=1000)
    wrapped_arr = filterer._wrap_image_to_tensorflow(data,
                                                     window_size,
                                                     zigzag=True)

    fig, axs = plt.subplots(1, 3, figsize=(1000 / 96, 480 / 96))
    lims = [-0.00025, -0.001, -0.01, -0.03, -0.04]
    for lim in lims:
        axs[1].axhline(lim, color='k', linestyle='--')

    axs[2].set_ylim(0, 1)
    # axs[3].set_ylim(50, 250)

    axs[1].set_xlabel("Subimage Number")
    axs[2].set_xlabel("Subimage Number")

    axs[1].set_ylabel("Normalised Euler Number")
    axs[2].set_ylabel("Normalised Perimeter")

    show_image(data, axis=axs[0])
    for i, img in enumerate(wrapped_arr):
        region = measure.regionprops((img[:, :, 0] != 0) + 1)[0]
        euler_num = region["euler_number"] / np.sum(img == 1)
        eccentricity = region["perimeter"] / np.sum(img == 1)
        axs[1].plot(i, euler_num, 'rx')
        axs[2].plot(i, eccentricity, 'rx')

    plt.tight_layout()

    if save:
        savedir = '/'.join(filepath.split('/')[-2:])
        plt.savefig(
            f"/home/mltest1/tmp/pycharm_project_883/Data/Plots/minkowski_stability/{savedir}"
        )
        plt.close()
Esempio n. 4
0
def plot_random_classified_images(summary_csv, category=None, max_ims=25):
    """Plot a random selection of CNN classified images

    Parameters
    ----------
    summary_csv : str
        Path to a csv file containing classifications
    category : str or None, Optional
        If specified, only plot classifications of this category. Default None
    max_ims : int, Optional
        The maximum number of images to plot. Default 25

    See Also
    --------
    Filters.directory_screening
    """

    from Filters.screening import FileFilter

    # Read in file
    df_summary = pd.read_csv(summary_csv)
    df_classified = df_summary[pd.isnull(df_summary["Fail Reasons"])]

    # Only consider category if specified
    if category:
        df_classified = df_classified[df_classified["CNN Classification"] ==
                                      category]

    # Randomly plot up to max_ims binarized files
    ax_res = int(np.sqrt(max_ims))
    fig, ax = plt.subplots(ax_res, ax_res, sharex=True, sharey=True)
    ax = np.reshape(ax, -1)

    max_ims = np.min((max_ims, len(df_classified)))
    for i, file in enumerate(df_classified["File Path"].sample(max_ims)):
        filterer = FileFilter()
        _, _, _, _, _, binarized_data, _, _ = filterer._load_and_preprocess(
            file)

        show_image(binarized_data, axis=ax[i], title=file.split("/")[-1])
Esempio n. 5
0
def visualise_autoencoder_preds(model, *datadirs):
    """Show effect of denoising images

    Parameters
    ----------
    model : object of type tf.model
        Tensorflow model used for denoising
    datadirs : list of str
        List containing every directory containing files (e.g. "good" images, "bad" images, simulated images) to test
    """
    from Models.train_CNN import validate_CNN
    from Filters.screening import FileFilter

    # For each provided directory
    imsize = model.input_shape[1]
    for datadir in datadirs:
        is_ibw = len(glob.glob(f"{datadir}/*.ibw")) == 0

        if is_ibw:  # If real files, preprocess
            truth = np.zeros(
                (len(glob.glob(f"{datadir}/*.ibw")), imsize, imsize, 1))
            for i, file in enumerate(glob.glob(f"{datadir}/*.ibw")):
                filterer = FileFilter()
                _, _, _, _, _, binarized_data, _, _ = filterer._load_and_preprocess(
                    file)

                if binarized_data is not None:
                    truth[i, :, :, 0] = binarized_data[:imsize, :imsize]
                else:
                    warnings.warn(f"Failed to preprocess {file}")
        else:  # If simulated files, use datagen
            preds, truth = validate_CNN(
                model=model,
                validation_datadir=datadir,
                network_type="autoencoder",
                y_params=["kT", "mu"],
                y_cats=["liquid", "hole", "cellular", "labyrinth", "island"],
                batch_size=10,
                imsize=imsize,
                steps=1)

        # Predict
        preds = model.predict(truth)

        # Ensure binarisation
        truth = np.round(truth)
        preds = np.round(preds)

        # Calculate mse
        mse = np.squeeze(((preds - truth)**2).mean(axis=(1, 2)))

        # Plot stacks of images
        pred = np.reshape(preds, (-1, preds.shape[1]))
        true = np.reshape(truth, (-1, truth.shape[1]))
        img = np.concatenate((truth, preds), axis=1)

        fig, ax = plt.subplots(1, 1)
        fig.suptitle(f"Mean error: {np.mean(mse) :.2f}")
        ax.imshow(img, cmap=cmap_rabani)
        ax.axis("off")

        # Show mse for each image
        ax.text(0, -10, "Orig")
        ax.text(imsize, -10, "Recons")
        for i, j in enumerate(mse):
            ax.text((imsize * 2.5), (i * imsize) + (imsize // 2),
                    f"mse = {j:.2f}")
Esempio n. 6
0
def everything_test(filepath, window_size, num_steps, perc_noise):
    from Filters.screening import FileFilter
    from skimage import measure
    from Analysis.plot_rabani import show_image
    from Models.h5_iterator import h5RabaniDataGenerator
    from matplotlib.ticker import PercentFormatter
    from tqdm import tqdm

    # Load in file
    filterer = FileFilter()
    _, _, _, _, _, data, _, _ = filterer._load_and_preprocess(
        filepath=filepath, threshold_method="multiotsu", nbins=1000)
    wrapped_arr = filterer._wrap_image_to_tensorflow(data,
                                                     window_size,
                                                     zigzag=True)
    wrapped_arr_for_noise = wrapped_arr.copy()

    # Calculate stats as function of window num
    euler_nums = np.zeros(len(wrapped_arr))
    perimeters = np.zeros(len(wrapped_arr))
    for i, img in enumerate(tqdm(wrapped_arr)):
        region = measure.regionprops((img[:, :, 0] != 0) + 1)[1]
        euler_nums[i] = region["euler_number"] / np.sum(img == 1)
        perimeters[i] = region["perimeter"] / np.sum(img == 1)

    # Calculate stats as function of noise
    euler_nums_noise = np.zeros(num_steps)
    perimeters_noise = np.zeros(num_steps)
    euler_nums_noise_std = np.zeros(num_steps)
    perimeters_noise_std = np.zeros(num_steps)
    for i in tqdm(range(num_steps)):
        euler_nums_noise_step = np.zeros(len(wrapped_arr))
        perimeters_noise_step = np.zeros(len(wrapped_arr))
        for j, img in enumerate(wrapped_arr_for_noise):
            region = measure.regionprops((img[:, :, 0] != 0) + 1)[1]
            euler_nums_noise_step[j] = region["euler_number"] / np.sum(
                img == 1)
            perimeters_noise_step[j] = region["perimeter"] / np.sum(img == 1)

        euler_nums_noise[i] = np.mean(euler_nums_noise_step)
        euler_nums_noise_std[i] = np.std(euler_nums_noise_step)
        perimeters_noise[i] = np.mean(perimeters_noise_step)
        perimeters_noise_std[i] = np.std(perimeters_noise_step)

        wrapped_arr_for_noise = h5RabaniDataGenerator.speckle_noise(
            wrapped_arr_for_noise,
            perc_noise,
            perc_std=None,
            num_uniques=2,
            randomness="batchwise_flip",
            scaling=False)

    # Plot
    fig, axs = plt.subplots(1, 5, figsize=(1400 / 96, 500 / 96))

    [ax.set_xlabel("Subimage Number") for ax in axs[1:3]]
    [ax.set_xlabel("% Speckle Noise") for ax in axs[3:]]
    [ax.xaxis.set_major_formatter(PercentFormatter(xmax=1)) for ax in axs[3:]]

    [ax.set_ylabel("Normalised Euler Number") for ax in axs[1::2]]
    [ax.set_ylabel("Normalised Perimeter") for ax in axs[2::2]]

    lims = [-0.00025, -0.001, -0.01, -0.03, -0.04]
    for ax in axs[1::2]:
        for lim in lims:
            ax.axhline(lim, color='k', linestyle='--')

    show_image(data, axis=axs[0])
    axs[1].plot(euler_nums)
    axs[2].plot(perimeters)
    axs[3].errorbar(
        np.arange(num_steps) * perc_noise, euler_nums_noise,
        euler_nums_noise_std)
    axs[4].errorbar(
        np.arange(num_steps) * perc_noise, perimeters_noise,
        perimeters_noise_std)

    plt.tight_layout()
Esempio n. 7
0
    "CNN Classification", "CNN Mean", "CNN std", "Euler Classification",
    "Euler Mean", "Euler std", "Stats Regression Classification",
    "Stats Regression Mean", "Stats Regression std", "Manual Classification",
    "SIA", "SIP", "SIE"
])

# Filter every ibw file in the directory, and build up a dataframe
all_files = [
    f for f in glob.glob(f"{IMAGE_DIR}/**/*.ibw", recursive=SEARCH_RECURSIVE)
]

t = tqdm(total=len(all_files), smoothing=True)
for i, file in enumerate(all_files):
    t.set_description(f"...{file[-25:]}")

    filterer = FileFilter()
    filterer.assess_file(filepath=file,
                         threshold_method="multiotsu",
                         category_model=None,
                         denoising_model=denoiser_model,
                         minkowski_model=sklearn_model,
                         assess_euler=ASSESS_EULER,
                         nbins=1000)  #, savedir=f"{OUTPUT_DIR}/Filtered")

    df_summary.loc[i, ["File Path"]] = [file]
    df_summary.loc[i, ["Resolution"]] = [filterer.image_res]
    df_summary.loc[i, ["Size (m)"]] = [filterer.image_size]
    df_summary.loc[i, ["Fail Reasons"]] = [filterer.fail_reasons]
    df_summary.loc[i, ["Manual Classification"]] = [file.split("/")[-2]]

    if filterer.CNN_classification: