Пример #1
0
def test_compute_IoU(download_release):
    m = main.deepforest()
    m.use_release(check_release=False)
    csv_file = get_data("OSBS_029.csv")
    predictions = m.predict_file(csv_file=csv_file,
                                 root_dir=os.path.dirname(csv_file))
    ground_truth = pd.read_csv(csv_file)

    predictions['geometry'] = predictions.apply(
        lambda x: shapely.geometry.box(x.xmin, x.ymin, x.xmax, x.ymax), axis=1)
    predictions = gpd.GeoDataFrame(predictions, geometry='geometry')

    ground_truth['geometry'] = ground_truth.apply(
        lambda x: shapely.geometry.box(x.xmin, x.ymin, x.xmax, x.ymax), axis=1)
    ground_truth = gpd.GeoDataFrame(ground_truth, geometry='geometry')

    ground_truth.label = 0
    predictions.label = 0
    visualize.plot_prediction_dataframe(df=predictions,
                                        ground_truth=ground_truth,
                                        root_dir=os.path.dirname(csv_file))

    result = IoU.compute_IoU(ground_truth, predictions)
    assert result.shape[0] == ground_truth.shape[0]
    assert sum(result.IoU) > 10
Пример #2
0
def test_plot_prediction_dataframe(m, tmpdir):
    ds = m.val_dataloader()
    batch = next(iter(ds))
    paths, images, targets = batch
    for path, image, target in zip(paths, images, targets):
        target_df = visualize.format_boxes(target, scores=False)
        target_df["image_path"] = path
        visualize.plot_prediction_dataframe(
            df=target_df,
            savedir=tmpdir,
            root_dir=m.config["validation"]["root_dir"])
Пример #3
0
def evaluate_image(predictions, ground_df, show_plot, root_dir, savedir):
    """
    Compute intersection-over-union matching among prediction and ground truth boxes for one image
    Args:
        df: a pandas dataframe with columns name, xmin, xmax, ymin, ymax, label. The 'name' column should be the path relative to the location of the file.
        show: Whether to show boxes as they are plotted
        summarize: Whether to group statistics by plot and overall score
        image_coordinates: Whether the current boxes are in coordinate system of the image, e.g. origin (0,0) upper left.
        root_dir: Where to search for image names in df
    Returns:
        result: pandas dataframe with crown ids of prediciton and ground truth and the IoU score.
    """

    plot_names = predictions["image_path"].unique()
    if len(plot_names) > 1:
        raise ValueError(
            "More than one plot passed to image crown: {}".format(plot_name))
    else:
        plot_name = plot_names[0]

    predictions['geometry'] = predictions.apply(
        lambda x: shapely.geometry.box(x.xmin, x.ymin, x.xmax, x.ymax), axis=1)
    predictions = gpd.GeoDataFrame(predictions, geometry='geometry')

    ground_df['geometry'] = ground_df.apply(
        lambda x: shapely.geometry.box(x.xmin, x.ymin, x.xmax, x.ymax), axis=1)
    ground_df = gpd.GeoDataFrame(ground_df, geometry='geometry')

    if savedir:
        visualize.plot_prediction_dataframe(df=predictions,
                                            ground_truth=ground_df,
                                            root_dir=root_dir,
                                            savedir=savedir)
    else:
        if show_plot:
            visualize.plot_prediction_dataframe(df=predictions,
                                                ground_truth=ground_df,
                                                root_dir=root_dir,
                                                savedir=savedir,
                                                show=True)

    # match
    result = IoU.compute_IoU(ground_df, predictions)

    #add the label classes
    result["predicted_label"] = result.prediction_id.apply(
        lambda x: predictions.label.loc[x] if pd.notnull(x) else x)
    result["true_label"] = result.truth_id.apply(
        lambda x: ground_df.label.loc[x])

    return result
Пример #4
0
def test_confirm_backend(m, tmpdir, show):
    """When a use call a visualize function we are using matplotlib to save figures, this should not interfere with plotting in global scope"""
    ds = m.val_dataloader()
    batch = next(iter(ds))
    paths, images, targets = batch

    original_backend = matplotlib.get_backend()

    for path, image, target in zip(paths, images, targets):
        target_df = visualize.format_boxes(target, scores=False)
        target_df["image_path"] = path
        visualize.plot_prediction_dataframe(
            df=target_df,
            savedir=tmpdir,
            root_dir=m.config["validation"]["root_dir"],
            show=show)

    post_backend = matplotlib.get_backend()

    assert original_backend == post_backend