Example #1
0
def extract_save_all_maps(config,
                          shuffle=1,
                          trainingsetindex=0,
                          comparisonbodyparts="all",
                          all_paf_in_one=True,
                          gputouse=None,
                          rescale=False,
                          Indices=None,
                          modelprefix="",
                          dest_folder=None):
    """
    Extracts the scoremap, location refinement field and part affinity field prediction of the model. The maps
    will be rescaled to the size of the input image and stored in the corresponding model folder in /evaluation-results.

    ----------
    config : string
        Full path of the config.yaml file as a string.

    shuffle: integer
        integers specifying shuffle index of the training dataset. The default is 1.

    trainingsetindex: int, optional
        Integer specifying which TrainingsetFraction to use. By default the first (note that TrainingFraction is a list in config.yaml). This
        variable can also be set to "all".

    comparisonbodyparts: list of bodyparts, Default is "all".
        The average error will be computed for those body parts only (Has to be a subset of the body parts).

    all_paf_in_one : bool
        By default, all part affinity fields are displayed on a single frame.
        If false, individual fields are shown on separate frames.

    Indices: default None
        For which images shall the scmap/locref and paf be computed? Give a list of images

    nplots_per_row: int, optional (default=None)
        Number of plots per row in grid plots. By default, calculated to approximate a squared grid of plots

    Examples
    --------
    Calculated maps for images 0, 1 and 33.
    >>> deeplabcut.extract_save_all_maps('/analysis/project/reaching-task/config.yaml', shuffle=1,Indices=[0,1,33])

    """

    from deeplabcut.utils.auxiliaryfunctions import (
        read_config, attempttomakefolder, GetEvaluationFolder,
        IntersectionofBodyPartsandOnesGivenbyUser)
    from tqdm import tqdm

    cfg = read_config(config)
    data = extract_maps(
        config,
        shuffle,
        trainingsetindex,
        gputouse,
        rescale,
        Indices,
        modelprefix,
    )

    comparisonbodyparts = IntersectionofBodyPartsandOnesGivenbyUser(
        cfg, comparisonbodyparts)

    print("Saving plots...")
    for frac, values in data.items():
        if not dest_folder:
            dest_folder = os.path.join(
                cfg["project_path"],
                str(
                    GetEvaluationFolder(frac,
                                        shuffle,
                                        cfg,
                                        modelprefix=modelprefix)),
                "maps",
            )
        attempttomakefolder(dest_folder)
        filepath = "{imname}_{map}_{label}_{shuffle}_{frac}_{snap}.png"
        dest_path = os.path.join(dest_folder, filepath)
        for snap, maps in values.items():
            for imagenr in tqdm(maps):
                (
                    image,
                    scmap,
                    locref,
                    paf,
                    bptnames,
                    pafgraph,
                    impath,
                    trainingframe,
                ) = maps[imagenr]
                label = "train" if trainingframe else "test"
                imname = os.path.split(os.path.splitext(impath)[0])[1]
                scmap, (locref_x, locref_y), paf = resize_all_maps(
                    image, scmap, locref, paf)
                to_plot = [
                    i for i, bpt in enumerate(bptnames)
                    if bpt in comparisonbodyparts
                ]
                list_of_inds = []
                for n, edge in enumerate(pafgraph):
                    if any(ind in to_plot for ind in edge):
                        list_of_inds.append([(2 * n, 2 * n + 1),
                                             (bptnames[edge[0]],
                                              bptnames[edge[1]])])
                map_ = scmap[:, :, to_plot].sum(axis=2)
                locref_x_ = locref_x[:, :, to_plot].sum(axis=2)
                locref_y_ = locref_y[:, :, to_plot].sum(axis=2)

                fig1, _ = visualize_scoremaps(image, map_)
                temp = dest_path.format(imname=imname,
                                        map='scmap',
                                        label=label,
                                        shuffle=shuffle,
                                        frac=frac,
                                        snap=snap)
                fig1.savefig(temp)

                fig2, _ = visualize_locrefs(image, map_, locref_x_, locref_y_)
                temp = dest_path.format(imname=imname,
                                        map='locref',
                                        label=label,
                                        shuffle=shuffle,
                                        frac=frac,
                                        snap=snap)
                fig2.savefig(temp)

                if paf is not None:
                    if not all_paf_in_one:
                        for inds, names in list_of_inds:
                            fig3, _ = visualize_paf(image, paf[:, :, [inds]])
                            temp = dest_path.format(
                                imname=imname,
                                map=f'paf_{"_".join(names)}',
                                label=label,
                                shuffle=shuffle,
                                frac=frac,
                                snap=snap)
                            fig3.savefig(temp)
                    else:
                        inds = [elem[0] for elem in list_of_inds]
                        n_inds = len(inds)
                        cmap = plt.cm.get_cmap(cfg['colormap'], n_inds)
                        colors = cmap(range(n_inds))
                        fig3, _ = visualize_paf(image,
                                                paf[:, :, inds],
                                                colors=colors)
                        temp = dest_path.format(imname=imname,
                                                map=f'paf',
                                                label=label,
                                                shuffle=shuffle,
                                                frac=frac,
                                                snap=snap)
                        fig3.savefig(temp)
                plt.close("all")
Example #2
0
def extract_save_all_maps(
    config,
    shuffle=1,
    trainingsetindex=0,
    comparisonbodyparts="all",
    gputouse=None,
    rescale=False,
    Indices=None,
    modelprefix="",
    dest_folder=None,
    nplots_per_row=None,
):
    """
    Extracts the scoremap, location refinement field and part affinity field prediction of the model. The maps
    will be rescaled to the size of the input image and stored in the corresponding model folder in /evaluation-results.

    ----------
    config : string
        Full path of the config.yaml file as a string.

    shuffle: integer
        integers specifying shuffle index of the training dataset. The default is 1.

    trainingsetindex: int, optional
        Integer specifying which TrainingsetFraction to use. By default the first (note that TrainingFraction is a list in config.yaml). This
        variable can also be set to "all".

    comparisonbodyparts: list of bodyparts, Default is "all".
        The average error will be computed for those body parts only (Has to be a subset of the body parts).

    Indices: default None
        For which images shall the scmap/locref and paf be computed? Give a list of images

    nplots_per_row: int, optional (default=None)
        Number of plots per row in grid plots. By default, calculated to approximate a squared grid of plots

    Examples
    --------
    Calculated maps for images 0, 1 and 33.
    >>> deeplabcut.extract_save_all_maps('/analysis/project/reaching-task/config.yaml', shuffle=1,Indices=[0,1,33])

    """

    from deeplabcut.utils.auxiliaryfunctions import (
        read_config,
        attempttomakefolder,
        GetEvaluationFolder,
    )
    from tqdm import tqdm

    cfg = read_config(config)
    data = extract_maps(
        config,
        shuffle,
        trainingsetindex,
        comparisonbodyparts,
        gputouse,
        rescale,
        Indices,
        modelprefix,
    )

    if not nplots_per_row:
        from deeplabcut.utils import auxiliaryfunctions

        bpts = auxiliaryfunctions.IntersectionofBodyPartsandOnesGivenbyUser(
            cfg, comparisonbodyparts)
        nplots_per_row = int(np.floor(np.sqrt(len(bpts))))

    print("Saving plots...")
    for frac, values in data.items():
        if not dest_folder:
            # dest_folder = os.path.join(cfg['project_path'], 'maps')
            dest_folder = os.path.join(
                cfg["project_path"],
                str(
                    GetEvaluationFolder(frac,
                                        shuffle,
                                        cfg,
                                        modelprefix=modelprefix)),
                "maps",
            )
        attempttomakefolder(dest_folder)
        dest_path = os.path.join(dest_folder, "{}_{}_{}_{}_{}_{}.png")

        for snap, maps in values.items():
            for imagenr in tqdm(maps):
                (
                    image,
                    scmap,
                    locref,
                    paf,
                    bptnames,
                    pafgraph,
                    impath,
                    trainingframe,
                ) = maps[imagenr]
                label = "train" if trainingframe else "test"
                imname = os.path.split(os.path.splitext(impath)[0])[1]
                if not os.path.isfile(
                        dest_path.format(imagenr, "scmap", label, shuffle,
                                         frac, snap)):
                    scmap, (locref_x, locref_y), paf = resize_all_maps(
                        image, scmap, locref, paf)
                    fig1, _ = visualize_scoremaps(
                        image,
                        scmap,
                        labels=bptnames,
                        nplots_per_row=nplots_per_row)
                    fig2, _ = visualize_locrefs(
                        image,
                        scmap,
                        locref_x,
                        locref_y,
                        labels=bptnames,
                        nplots_per_row=nplots_per_row,
                    )
                    fig3, _ = visualize_locrefs(
                        image,
                        scmap,
                        locref_x,
                        locref_y,
                        zoom_width=100,
                        labels=bptnames,
                        nplots_per_row=nplots_per_row,
                    )
                    if paf is not None:
                        fig4, _ = visualize_paf(
                            image,
                            paf,
                            pafgraph,
                            labels=bptnames,
                            nplots_per_row=nplots_per_row,
                        )

                    fig1.savefig(
                        dest_path.format(imname, "scmap", label, shuffle, frac,
                                         snap))
                    fig2.savefig(
                        dest_path.format(imname, "locref", label, shuffle,
                                         frac, snap))
                    fig3.savefig(
                        dest_path.format(imname, "locrefzoom", label, shuffle,
                                         frac, snap))
                    if paf is not None:
                        fig4.savefig(
                            dest_path.format(imname, "paf", label, shuffle,
                                             frac, snap))

                    plt.close("all")