Skip to content

deeplabcut.generate_training_dataset.trainingsetmanipulation

Functions:

Name Description
SplitTrials

Split a trial index into train and test sets.

adddatasetstovideolistandviceversa

First run comparevideolistsanddatafolders(config) to compare the folders in

boxitintoacell

Auxiliary function for creating matfile.

check_labels

Check the labeled frames.

comparevideolistsanddatafolders

Auxiliary function that compares the folders in labeled-data and the ones listed

create_training_dataset

Creates a training dataset.

create_training_dataset_from_existing_split

Labels from all the extracted frames are merged into a single .h5 file. Only the

create_training_model_comparison

Creates a training dataset to compare networks and augmentation types.

drop_likelihood_columns

Drop any columns whose coord level is named 'likelihood'.

dropannotationfileentriesduetodeletedimages

Drop entries for all deleted images in annotation files, i.e. for folders of the

dropduplicatesinannotatinfiles

Drop duplicate entries (of images) in annotation files (this should no longer

dropimagesduetolackofannotation

Drop images from corresponding folder for not annotated images: /labeled-data/folder/CollectedData_scorer.h5

dropunlabeledframes

Drop entries such that all the bodyparts are not labeled from the annotation

get_existing_shuffle_indices

Get the existing shuffle indices.

get_largestshuffle_index

Returns the largest shuffle for all dlc-models in the current iteration.

merge_annotateddatasets

Merges all the h5 files for all labeled-datasets (from individual videos).

mergeandsplit

This function allows additional control over "create_training_dataset".

parse_video_filenames

Parses the names of all videos listed in a project's config.yaml file.

SplitTrials

SplitTrials(trialindex, trainFraction=0.8, enforce_train_fraction=False)

Split a trial index into train and test sets.

Also checks that the trainFraction is a two digit number between 0 an 1. The reason is that the folders contain the trainfraction as int(100*trainFraction). If enforce_train_fraction is True, train and test indices are padded with -1 such that the ratio of their lengths is exactly the desired train fraction.

Source code in deeplabcut/generate_training_dataset/trainingsetmanipulation.py
def SplitTrials(
    trialindex,
    trainFraction=0.8,
    enforce_train_fraction=False,
):
    """Split a trial index into train and test sets.

    Also checks that the trainFraction is a two digit number between 0 an 1. The reason
    is that the folders contain the trainfraction as int(100*trainFraction). If
    enforce_train_fraction is True, train and test indices are padded with -1 such that
    the ratio of their lengths is exactly the desired train fraction.
    """
    if trainFraction > 1 or trainFraction < 0:
        print(
            "The training fraction should be a two digit number between 0 and 1; i.e. 0.95. Please change accordingly."
        )
        return ([], [])

    if abs(trainFraction - round(trainFraction, 2)) > 0:
        print(
            "The training fraction should be a two digit number between 0 and 1; i.e. 0.95. Please change accordingly."
        )
        return ([], [])
    else:
        index_len = len(trialindex)
        train_fraction = round(trainFraction, 2)
        train_size = index_len * train_fraction
        shuffle = np.random.permutation(trialindex)
        test_indices = shuffle[int(train_size) :]
        train_indices = shuffle[: int(train_size)]
        if enforce_train_fraction and not train_size.is_integer():
            train_indices, test_indices = pad_train_test_indices(
                train_indices,
                test_indices,
                train_fraction,
            )

        return train_indices, test_indices

adddatasetstovideolistandviceversa

adddatasetstovideolistandviceversa(config: str | Path)

First run comparevideolistsanddatafolders(config) to compare the folders in labeled-data and the ones listed under video_sets (in the config file). If you detect differences this function can be used to maker sure each folder has a video entry & vice versa.

It corrects this problem in the following way:

If a video entry in the config file does not contain a folder in labeled-data, then the entry is removed. If a folder in labeled-data does not contain a video entry in the config file then the prefix path will be added in front of the name of the labeled-data folder and combined with the suffix variable as an ending. Width and height will be added as cropping variables as passed on.

Handle with care!

Parameters:

Name Type Description Default

config

string

String containing the full path of the config file in the project.

required
Source code in deeplabcut/generate_training_dataset/trainingsetmanipulation.py
def adddatasetstovideolistandviceversa(config: str | Path):
    """First run comparevideolistsanddatafolders(config) to compare the folders in
    labeled-data and the ones listed under video_sets (in the config file). If you
    detect differences this function can be used to maker sure each folder has a video
    entry & vice versa.

    It corrects this problem in the following way:

    If a video entry in the config file does not contain a folder in labeled-data, then the entry is removed.
    If a folder in labeled-data does not contain a video entry in the config file then
    the prefix path will be added in front of the name of the labeled-data folder and combined
    with the suffix variable as an ending. Width and height will be added as cropping variables as passed on.

    Handle with care!

    Args:
        config (string): String containing the full path of the config file in the project.
    """
    cfg = read_config(config)
    videos = cfg["video_sets"]
    video_names = [Path(i).stem for i in videos]

    labeled_data_dir = Path(config).parent / "labeled-data"
    alldatafolders = [
        f.name for f in labeled_data_dir.iterdir() if "_labeled" not in f.name and not f.name.startswith(".")
    ]

    print("Config file contains:", len(video_names))
    print("Labeled-data contains:", len(alldatafolders))

    toberemoved = []
    for vn in video_names:
        if vn not in alldatafolders:
            print(vn, " is missing as a labeled folder >> removing key!")
            for fullvideo in videos:
                if vn in fullvideo:
                    toberemoved.append(fullvideo)

    for vid in toberemoved:
        del videos[vid]

    # Load updated lists:
    video_names = [Path(i).stem for i in videos]
    for vn in alldatafolders:
        if vn not in video_names:
            print(vn, " is missing in config file >> adding it!")
            # Find the corresponding video file
            found = False
            for file in (Path(cfg["project_path"]) / "videos").iterdir():
                if file.stem == vn:
                    found = True
                    break
            if found:
                video_path = str(file)
                clip = VideoReader(video_path)
                videos.update({video_path: {"crop": ", ".join(map(str, clip.get_bbox()))}})

    auxiliaryfunctions.write_config(config, cfg)

boxitintoacell

boxitintoacell(joints)

Auxiliary function for creating matfile.

Source code in deeplabcut/generate_training_dataset/trainingsetmanipulation.py
def boxitintoacell(joints):
    """Auxiliary function for creating matfile."""
    outer = np.array([[None]], dtype=object)
    outer[0, 0] = np.array(joints, dtype="int64")
    return outer

check_labels

check_labels(config: str | Path, Labels=None, scale=1, dpi=100, draw_skeleton=True, visualizeindividuals=True)

Check the labeled frames.

Double check if the labels were at the correct locations and stored in the proper file format.

This creates a new subdirectory for each video under the 'labeled-data' and all the frames are plotted with the labels.

Make sure that these labels are fine.

Parameters:

Name Type Description Default

config

string

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

required

Labels

list

List of at least 3 matplotlib markers. The first one will be used to indicate the human ground truth location. Defaults to '+'.

None

scale

float

Change the relative size of the output images. Defaults to 1.

1

dpi

int

Output resolution in dpi. Defaults to 100.

100

draw_skeleton

bool

Plot skeleton overlaid over body parts. Defaults to True.

True

visualizeindividuals

bool

For a multianimal project, if True, the different individuals have different colors (and all bodyparts the same). If False, the colors change over bodyparts rather than individuals. Defaults to True.

True

Returns:

Type Description

None

Examples:

deeplabcut.check_labels("/analysis/project/reaching-task/config.yaml")

Source code in deeplabcut/generate_training_dataset/trainingsetmanipulation.py
def check_labels(
    config: str | Path,
    Labels=None,
    scale=1,
    dpi=100,
    draw_skeleton=True,
    visualizeindividuals=True,
):
    """Check the labeled frames.

    Double check if the labels were at the correct locations and stored in the proper
    file format.

    This creates a new subdirectory for each video under the 'labeled-data' and all the
    frames are plotted with the labels.

    Make sure that these labels are fine.

    Args:
        config (string): Full path of the config.yaml file as a string.
        Labels (list, optional): List of at least 3 matplotlib markers. The first one
            will be used to indicate the human ground truth location. Defaults to '+'.
        scale (float, optional): Change the relative size of the output images.
            Defaults to 1.
        dpi (int, optional): Output resolution in dpi. Defaults to 100.
        draw_skeleton (bool, optional): Plot skeleton overlaid over body parts.
            Defaults to True.
        visualizeindividuals (bool, optional): For a multianimal project, if True, the
            different individuals have different colors (and all bodyparts the same).
            If False, the colors change over bodyparts rather than individuals.
            Defaults to True.

    Returns:
        None

    Examples:
            deeplabcut.check_labels("/analysis/project/reaching-task/config.yaml")
    """
    from deeplabcut.utils import visualization

    if Labels is None:
        Labels = ["+", ".", "x"]
    cfg = read_config(config)
    videos = cfg["video_sets"].keys()
    video_names = [Path(video).stem for video in videos]

    folders = [Path(cfg["project_path"]) / "labeled-data" / Path(i) for i in video_names]
    print("Creating images with labels by {}.".format(cfg["scorer"]))
    for folder in folders:
        try:
            DataCombined = pd.read_hdf(folder / ("CollectedData_" + cfg["scorer"] + ".h5"))
            conversioncode.guarantee_multiindex_rows(DataCombined)
            if cfg.get("multianimalproject", False):
                color_by = "individual" if visualizeindividuals else "bodypart"
            else:  # for single animal projects
                color_by = "bodypart"

            visualization.make_labeled_images_from_dataframe(
                DataCombined,
                cfg,
                folder,
                scale,
                dpi=dpi,
                keypoint=Labels[0],
                draw_skeleton=draw_skeleton,
                color_by=color_by,
            )
        except FileNotFoundError:
            print("Attention:", folder, "does not appear to have labeled data!")

    print("If all the labels are ok, then use the function 'create_training_dataset' to create the training dataset!")

comparevideolistsanddatafolders

comparevideolistsanddatafolders(config: str | Path)

Auxiliary function that compares the folders in labeled-data and the ones listed under video_sets (in the config file).

Parameters:

Name Type Description Default

config

string

String containing the full path of the config file in the project.

required
Source code in deeplabcut/generate_training_dataset/trainingsetmanipulation.py
def comparevideolistsanddatafolders(config: str | Path):
    """Auxiliary function that compares the folders in labeled-data and the ones listed
    under video_sets (in the config file).

    Args:
        config (string): String containing the full path of the config file in the project.
    """
    cfg = read_config(config)
    videos = cfg["video_sets"].keys()
    video_names = [Path(i).stem for i in videos]
    alldatafolders = [f.name for f in (Path(config).parent / "labeled-data").iterdir() if "_labeled" not in f.name]

    print("Config file contains:", len(video_names))
    print("Labeled-data contains:", len(alldatafolders))

    for vn in video_names:
        if vn not in alldatafolders:
            print(vn, " is missing as a folder!")

    for vn in alldatafolders:
        if vn not in video_names:
            print(vn, " is missing in config file!")

create_training_dataset

create_training_dataset(
    config: str | Path | ProjectConfig | dict,
    num_shuffles=1,
    Shuffles=None,
    windows2linux=False,
    userfeedback=True,
    trainIndices=None,
    testIndices=None,
    net_type=None,
    detector_type=None,
    augmenter_type=None,
    posecfg_template=None,
    superanimal_name="",
    weight_init: WeightInitialization | None = None,
    engine: Engine | None = None,
    ctd_conditions: int | str | Path | tuple[int, str] | tuple[int, int] | None = None,
)

Creates a training dataset.

Labels from all the extracted frames are merged into a single .h5 file. Only the videos included in the config file are used to create this dataset.

Parameters:

Name Type Description Default

config

str | Path | ProjectConfig | dict

Full path of the config.yaml file. Alternatively, a ProjectConfig object or a dictionary can be passed.

required

num_shuffles

int

Number of shuffles of training dataset to create, i.e. [1,2,3] for num_shuffles=3. Defaults to 1.

1

Shuffles

list[int]

Alternatively the user can also give a list of shuffles.

None

userfeedback

bool

If False, all requested train/test splits are created (no matter if they already exist). If you want to assure that previous splits etc. are not overwritten, set this to True and you will be asked for each split. Defaults to True.

True

trainIndices

list of lists

List of one or multiple lists containing train indexes. A list containing two lists of training indexes will produce two splits. Defaults to None.

None

testIndices

list of lists

List of one or multiple lists containing test indexes. Defaults to None.

None

net_type

list

Type of networks. The options available depend on which engine is used. Currently supported options are: TensorFlow * resnet_50 * resnet_101 * resnet_152 * mobilenet_v2_1.0 * mobilenet_v2_0.75 * mobilenet_v2_0.5 * mobilenet_v2_0.35 * efficientnet-b0 * efficientnet-b1 * efficientnet-b2 * efficientnet-b3 * efficientnet-b4 * efficientnet-b5 * efficientnet-b6 PyTorch (call deeplabcut.pose_estimation_pytorch.available_models() for a complete list) * animaltokenpose_base * cspnext_m * cspnext_s * cspnext_x * ctd_coam_w32 * ctd_coam_w48 * ctd_prenet_cspnext_m * ctd_prenet_cspnext_x * ctd_prenet_rtmpose_x_human * ctd_prenet_hrnet_w32 * ctd_prenet_hrnet_w48 * ctd_prenet_rtmpose_m * ctd_prenet_rtmpose_x * ctd_prenet_rtmpose_x_human * dekr_w18 * dekr_w32 * dekr_w48 * dlcrnet_stride16_ms5 * dlcrnet_stride32_ms5 * hrnet_w18 * hrnet_w32 * hrnet_w48 * resnet_101 * resnet_50 * rtmpose_m * rtmpose_s * rtmpose_x * top_down_cspnext_m * top_down_cspnext_s * top_down_cspnext_x * top_down_hrnet_w18 * top_down_hrnet_w32 * top_down_hrnet_w48 * top_down_resnet_101 * top_down_resnet_50 Defaults to None.

None

detector_type

string

Only for the PyTorch engine. When passing creating shuffles for top-down models, you can specify which detector you want. If the detector_type is None, the ssdlite will be used. The list of all available detectors can be obtained by calling deeplabcut.pose_estimation_pytorch.available_detectors(). Supported options: * ssdlite * fasterrcnn_mobilenet_v3_large_fpn * fasterrcnn_resnet50_fpn_v2 Defaults to None.

None

augmenter_type

string

Type of augmenter. The options available depend on which engine is used. Currently supported options are: TensorFlow * default * scalecrop * imgaug * tensorpack * deterministic PyTorch * albumentations Defaults to None.

None

posecfg_template

string

Only for the TensorFlow engine. Path to a pose_cfg.yaml file to use as a template for generating the new one for the current iteration. Useful if you would like to start with the same parameters a previous training iteration. None uses the default pose_cfg.yaml. Defaults to None.

None

superanimal_name

string

Only for the TensorFlow engine. For the PyTorch engine, use the weight_init parameter. Specify the superanimal name is transfer learning with superanimal is desired. This makes sure the pose config template uses superanimal configs as template. Defaults to "".

''

weight_init

WeightInitialisation

PyTorch engine only. Specify how model weights should be initialized. The default mode uses transfer learning from ImageNet weights. Defaults to None.

None

engine

Engine

Whether to create a pose config for a Tensorflow or PyTorch model. Defaults to the value specified in the project configuration file. If no engine is specified for the project, defaults to deeplabcut.compat.DEFAULT_ENGINE.

None

Returns:

Type Description

list(tuple) or None: If training dataset was successfully created, a list of tuples is returned. The first two elements in each tuple represent the training fraction and the shuffle value. The last two elements in each tuple are arrays of integers representing the training and test indices.

Returns None if training dataset could not be created.
Note

Use the function add_new_videos at any stage of the project to add more videos to the project.

Examples:

Linux/MacOS: deeplabcut.create_training_dataset( '/analysis/project/reaching-task/config.yaml', num_shuffles=1, )

deeplabcut.create_training_dataset(
    '/analysis/project/reaching-task/config.yaml', Shuffles=[2], engine=deeplabcut.Engine.TF,
)

Windows:

deeplabcut.create_training_dataset(
    "C:\Users\Ulf\looming-task\config.yaml",
    Shuffles=[3, 17, 5],
)
Source code in deeplabcut/generate_training_dataset/trainingsetmanipulation.py
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
def create_training_dataset(
    config: str | Path | ProjectConfig | dict,
    num_shuffles=1,
    Shuffles=None,
    windows2linux=False,
    userfeedback=True,
    trainIndices=None,
    testIndices=None,
    net_type=None,
    detector_type=None,
    augmenter_type=None,
    posecfg_template=None,
    superanimal_name="",
    weight_init: WeightInitialization | None = None,
    engine: Engine | None = None,
    ctd_conditions: int | str | Path | tuple[int, str] | tuple[int, int] | None = None,
):
    """Creates a training dataset.

    Labels from all the extracted frames are merged into a single .h5 file.
    Only the videos included in the config file are used to create this dataset.

    Args:
        config (str | Path | ProjectConfig | dict): Full path of the ``config.yaml``
            file. Alternatively, a ProjectConfig object or a dictionary can be passed.
        num_shuffles (int, optional): Number of shuffles of training dataset to create,
            i.e. ``[1,2,3]`` for ``num_shuffles=3``. Defaults to 1.
        Shuffles (list[int], optional): Alternatively the user can also give a list of
            shuffles.
        userfeedback (bool, optional): If ``False``, all requested train/test splits are
            created (no matter if they already exist). If you want to assure that
            previous splits etc. are not overwritten, set this to ``True`` and you will
            be asked for each split. Defaults to True.
        trainIndices (list of lists, optional): List of one or multiple lists containing
            train indexes. A list containing two lists of training indexes will produce
            two splits. Defaults to None.
        testIndices (list of lists, optional): List of one or multiple lists containing
            test indexes. Defaults to None.
        net_type (list, optional): Type of networks. The options available depend on which
            engine is used. Currently supported options are:
            TensorFlow
                * ``resnet_50``
                * ``resnet_101``
                * ``resnet_152``
                * ``mobilenet_v2_1.0``
                * ``mobilenet_v2_0.75``
                * ``mobilenet_v2_0.5``
                * ``mobilenet_v2_0.35``
                * ``efficientnet-b0``
                * ``efficientnet-b1``
                * ``efficientnet-b2``
                * ``efficientnet-b3``
                * ``efficientnet-b4``
                * ``efficientnet-b5``
                * ``efficientnet-b6``
            PyTorch (call ``deeplabcut.pose_estimation_pytorch.available_models()`` for
            a complete list)
                * ``animaltokenpose_base``
                * ``cspnext_m``
                * ``cspnext_s``
                * ``cspnext_x``
                * ``ctd_coam_w32``
                * ``ctd_coam_w48``
                * ``ctd_prenet_cspnext_m``
                * ``ctd_prenet_cspnext_x``
                * ``ctd_prenet_rtmpose_x_human``
                * ``ctd_prenet_hrnet_w32``
                * ``ctd_prenet_hrnet_w48``
                * ``ctd_prenet_rtmpose_m``
                * ``ctd_prenet_rtmpose_x``
                * ``ctd_prenet_rtmpose_x_human``
                * ``dekr_w18``
                * ``dekr_w32``
                * ``dekr_w48``
                * ``dlcrnet_stride16_ms5``
                * ``dlcrnet_stride32_ms5``
                * ``hrnet_w18``
                * ``hrnet_w32``
                * ``hrnet_w48``
                * ``resnet_101``
                * ``resnet_50``
                * ``rtmpose_m``
                * ``rtmpose_s``
                * ``rtmpose_x``
                * ``top_down_cspnext_m``
                * ``top_down_cspnext_s``
                * ``top_down_cspnext_x``
                * ``top_down_hrnet_w18``
                * ``top_down_hrnet_w32``
                * ``top_down_hrnet_w48``
                * ``top_down_resnet_101``
                * ``top_down_resnet_50``
            Defaults to None.

        detector_type (string, optional): Only for the PyTorch engine. When passing
            creating shuffles for top-down models, you can specify which detector you
            want. If the detector_type is None, the ```ssdlite``` will be used. The list
            of all available detectors can be obtained by calling
            ``deeplabcut.pose_estimation_pytorch.available_detectors()``. Supported
            options:
            * ``ssdlite``
            * ``fasterrcnn_mobilenet_v3_large_fpn``
            * ``fasterrcnn_resnet50_fpn_v2``
            Defaults to None.

        augmenter_type (string, optional): Type of augmenter. The options available
            depend on which engine is used. Currently supported options are:
            TensorFlow
                * ``default``
                * ``scalecrop``
                * ``imgaug``
                * ``tensorpack``
                * ``deterministic``
            PyTorch
                * ``albumentations``
            Defaults to None.

        posecfg_template (string, optional): Only for the TensorFlow engine. Path to a
            ``pose_cfg.yaml`` file to use as a template for generating the new one for
            the current iteration. Useful if you would like to start with the same
            parameters a previous training iteration. None uses the default
            ``pose_cfg.yaml``. Defaults to None.

        superanimal_name (string, optional): Only for the TensorFlow engine. For the
            PyTorch engine, use the ``weight_init`` parameter. Specify the superanimal
            name is transfer learning with superanimal is desired. This makes sure the
            pose config template uses superanimal configs as template. Defaults to "".

        weight_init (WeightInitialisation, optional): PyTorch engine only. Specify how
            model weights should be initialized. The default mode uses transfer learning
            from ImageNet weights. Defaults to None.

        engine (Engine, optional): Whether to create a pose config for a Tensorflow or
            PyTorch model. Defaults to the value specified in the project configuration
            file. If no engine is specified for the project, defaults to
            ``deeplabcut.compat.DEFAULT_ENGINE``.

        ctd_conditions (int | str | Path | tuple[int, str] | tuple[int, int] | None,
            optional): If using a conditional-top-down (CTD) net_type, this argument
            should be specified. It defines the conditions that will be used with the CTD
            model. It can be either:
            * A shuffle number (ctd_conditions: int), which must correspond to a
                bottom-up (BU) network type. Valid for both evaluation and live
                analyze.
            * A predictions file path (ctd_conditions: string | Path), which must
                correspond to a .json or .h5 predictions file. Evaluation-only —
                not valid for ``analyze_images`` / ``analyze_videos``.
            * A shuffle number and a particular snapshot
                (ctd_conditions: tuple[int, str] | tuple[int, int]), which respectively
                correspond to a bottom-up (BU) network type and a particular snapshot
                name or index. Defaults to None.

    Returns:
        list(tuple) or None: If training dataset was successfully created, a list of
            tuples is returned. The first two elements in each tuple represent the
            training fraction and the shuffle value. The last two elements in each tuple
            are arrays of integers representing the training and test indices.

            Returns None if training dataset could not be created.

    Note:
        Use the function ``add_new_videos`` at any stage of the project to add more
        videos to the project.

    Examples:
        Linux/MacOS:
            deeplabcut.create_training_dataset(
                '/analysis/project/reaching-task/config.yaml', num_shuffles=1,
            )

            deeplabcut.create_training_dataset(
                '/analysis/project/reaching-task/config.yaml', Shuffles=[2], engine=deeplabcut.Engine.TF,
            )

        Windows:

            deeplabcut.create_training_dataset(
                "C:\\Users\\Ulf\\looming-task\\config.yaml",
                Shuffles=[3, 17, 5],
            )
    """
    import scipy.io as sio

    if windows2linux:
        # DeprecationWarnings are silenced since Python 3.2 unless triggered in __main__
        warnings.warn(
            "`windows2linux` has no effect since 2.2.0.4 and will be removed in 2.2.1.",
            FutureWarning,
            stacklevel=2,
        )

    # Loading metadata from config file:
    cfg = ProjectConfig.from_any(config, repair_path=True)
    cfg_path = config if isinstance(config, (str, Path)) else cfg.config_yaml_path

    auxiliaryfunctions.get_deeplabcut_path()

    if superanimal_name != "":
        raise ValueError(
            "Invalid argument superanimal_name. This functionality has been "
            "removed. Please use modelzoo.build_weight_init() instead."
        )

    if posecfg_template:
        posecfg_template = Path(posecfg_template)
        if posecfg_template.name not in {"pose_cfg.yaml", "superquadruped.yaml", "supertopview.yaml"}:
            raise ValueError("posecfg_template argument must contain path to a pose_cfg.yaml file")
        else:
            print(f"Reloading pose_cfg parameters from {posecfg_template}\n")
            from deeplabcut.utils.auxiliaryfunctions import read_plainconfig

        prior_cfg = read_plainconfig(posecfg_template)
    if cfg.get("multianimalproject", False):
        from deeplabcut.generate_training_dataset.multiple_individuals_trainingsetmanipulation import (
            create_multianimaltraining_dataset,
        )

        create_multianimaltraining_dataset(
            cfg,
            num_shuffles,
            Shuffles,
            net_type=net_type,
            detector_type=detector_type,
            trainIndices=trainIndices,
            testIndices=testIndices,
            userfeedback=userfeedback,
            engine=engine,
            weight_init=weight_init,
            ctd_conditions=ctd_conditions,
        )
    else:
        scorer = cfg["scorer"]
        project_path = cfg["project_path"]
        if engine is None:
            engine = compat.get_project_engine(cfg)

        # Create path for training sets & store data there
        trainingsetfolder = auxiliaryfunctions.get_training_set_folder(
            cfg
        )  # Path concatenation OS platform independent
        auxiliaryfunctions.attempt_to_make_folder(Path(project_path) / str(trainingsetfolder), recursive=True)

        # Create the trainset metadata file, if it doesn't yet exist
        if not metadata.TrainingDatasetMetadata.path(cfg).exists():
            trainset_metadata = metadata.TrainingDatasetMetadata.create(cfg)
            trainset_metadata.save()

        Data = merge_annotateddatasets(
            cfg,
            Path(project_path) / trainingsetfolder,
        )
        if Data is None:
            return
        Data = Data[scorer]  # extract labeled data

        # loading & linking pretrained models
        if net_type is None:  # loading & linking pretrained models
            net_type = cfg.get("default_net_type", "resnet_50")
        elif engine == Engine.PYTORCH:
            pass
        else:
            if "resnet" in net_type or "mobilenet" in net_type or "efficientnet" in net_type or "dlcrnet" in net_type:
                pass
            else:
                raise ValueError("Invalid network type:", net_type)

        top_down = False
        if engine == Engine.PYTORCH:
            if net_type.startswith("top_down_"):
                top_down = True
                net_type = net_type[len("top_down_") :]

        augmenters = compat.get_available_aug_methods(engine)
        default_augmenter = augmenters[0]
        if augmenter_type is None:
            augmenter_type = cfg.get("default_augmenter", default_augmenter)

            if augmenter_type is None:  # this could be in config.yaml for old projects!
                # updating variable if null/None! #backwardscompatability
                augmenter_type = default_augmenter
                cfg.default_augmenter = augmenter_type
                cfg.to_yaml(cfg_path, log_changes=True, mark_clean=True)
            elif augmenter_type not in augmenters:
                # as the default augmenter might not be available for the given engine
                augmenter_type = default_augmenter
                logging.info(
                    f"Default augmenter {augmenter_type} not available for engine "
                    f"{engine}: using {default_augmenter} instead"
                )

        if augmenter_type not in augmenters:
            if engine != Engine.PYTORCH:
                raise ValueError(
                    f"Invalid augmenter type: {augmenter_type} (available: for engine={engine}: {augmenters})"
                )

            logging.info(f"Switching augmentation to {default_augmenter} for PyTorch")
            augmenter_type = default_augmenter

        if posecfg_template:
            if net_type != prior_cfg["net_type"]:
                print(
                    "WARNING: Specified net_type does not match net_type from "
                    "posecfg_template path entered. Proceed with caution."
                )
            if augmenter_type != prior_cfg["dataset_type"]:
                print(
                    "WARNING: Specified augmenter_type does not match dataset_type "
                    "from posecfg_template path entered. Proceed with caution."
                )

        # Loading the encoder (if necessary downloading from TF)
        dlcparent_path = auxiliaryfunctions.get_deeplabcut_path()
        if not posecfg_template:
            defaultconfigfile = dlcparent_path / "pose_cfg.yaml"
        elif posecfg_template:
            defaultconfigfile = posecfg_template

        if engine == Engine.PYTORCH:
            model_path = dlcparent_path
        else:
            model_path = auxfun_models.check_for_weights(net_type, dlcparent_path)

        Shuffles = validate_shuffles(cfg, Shuffles, num_shuffles, userfeedback)

        if trainIndices is None and testIndices is None:
            splits = [
                (
                    trainFraction,
                    shuffle,
                    SplitTrials(range(len(Data.index)), trainFraction),
                )
                for trainFraction in cfg["TrainingFraction"]
                for shuffle in Shuffles
            ]
        else:
            if len(trainIndices) != len(testIndices) != len(Shuffles):
                raise ValueError("Number of Shuffles and train and test indexes should be equal.")
            splits = []
            for shuffle, (train_inds, test_inds) in enumerate(zip(trainIndices, testIndices, strict=False)):
                trainFraction = round(len(train_inds) * 1.0 / (len(train_inds) + len(test_inds)), 2)
                print(f"You passed a split with the following fraction: {int(100 * trainFraction)}%")
                # Now that the training fraction is guaranteed to be correct,
                # the values added to pad the indices are removed.
                train_inds = np.asarray(train_inds)
                train_inds = train_inds[train_inds != -1]
                test_inds = np.asarray(test_inds)
                test_inds = test_inds[test_inds != -1]
                splits.append((trainFraction, Shuffles[shuffle], (train_inds, test_inds)))

        bodyparts = auxiliaryfunctions.get_bodyparts(cfg)
        nbodyparts = len(bodyparts)
        for trainFraction, shuffle, (trainIndices, testIndices) in splits:
            if len(trainIndices) > 0:
                if userfeedback:
                    trainposeconfigfile, _, _ = compat.return_train_network_path(
                        cfg_path,
                        shuffle=shuffle,
                        trainingsetindex=cfg["TrainingFraction"].index(trainFraction),
                        engine=engine,
                    )
                    if trainposeconfigfile.is_file():
                        askuser = input(
                            "The model folder is already present. "
                            "If you continue, it will overwrite the existing model (split). "
                            "Do you want to continue?(yes/no): "
                        )
                        if askuser == "no" or askuser == "No" or askuser == "N" or askuser == "No":
                            raise Exception(
                                "Use the Shuffles argument as a list to specify a different shuffle index. "
                                "Check out the help for more details."
                            )

                ####################################################
                # Generating data structure with labeled information & frame metadata (for deep cut)
                ####################################################
                # Make training file!
                (
                    datafilename,
                    metadatafilename,
                ) = auxiliaryfunctions.get_data_and_metadata_filenames(trainingsetfolder, trainFraction, shuffle, cfg)

                ################################################################################
                # Saving data file (convert to training file for deeper cut (*.mat))
                ################################################################################
                data, MatlabData = format_training_data(Data, trainIndices, nbodyparts, project_path)
                sio.savemat(str(Path(project_path) / datafilename), {"dataset": MatlabData})

                ################################################################################
                # Saving metadata (Pickle file)
                ################################################################################
                auxiliaryfunctions.save_metadata(
                    Path(project_path) / metadatafilename,
                    data,
                    trainIndices,
                    testIndices,
                    trainFraction,
                )
                metadata.update_metadata(
                    cfg=cfg,
                    train_fraction=trainFraction,
                    shuffle=shuffle,
                    engine=engine,
                    train_indices=trainIndices,
                    test_indices=testIndices,
                    overwrite=not userfeedback,
                )

                ################################################################################
                # Creating file structure for training &
                # Test files as well as pose_yaml files (containing training and testing information)
                #################################################################################
                modelfoldername = auxiliaryfunctions.get_model_folder(
                    trainFraction,
                    shuffle,
                    cfg,
                    engine=engine,
                )
                auxiliaryfunctions.attempt_to_make_folder(cfg.project_path / modelfoldername, recursive=True)
                auxiliaryfunctions.attempt_to_make_folder(cfg.project_path / modelfoldername / "train")
                auxiliaryfunctions.attempt_to_make_folder(cfg.project_path / modelfoldername / "test")

                path_train_config = str(Path(cfg["project_path"]) / modelfoldername / "train" / engine.pose_cfg_name)
                path_test_config = str(Path(cfg["project_path"]) / modelfoldername / "test" / "pose_cfg.yaml")
                if engine == Engine.TF:
                    if weight_init is not None:
                        raise ValueError(
                            "Weight initialization is not supported for TensorFlow engine. "
                            "Pretrained weights are automatically downloaded."
                        )
                    items2change = {
                        "dataset": datafilename,
                        "engine": engine.aliases[0],
                        "metadataset": metadatafilename,
                        "num_joints": len(bodyparts),
                        "all_joints": [[i] for i in range(len(bodyparts))],
                        "all_joints_names": [str(bpt) for bpt in bodyparts],
                        "init_weights": model_path,
                        "project_path": str(cfg["project_path"]),
                        "net_type": net_type,
                        "dataset_type": augmenter_type,
                    }

                    items2drop = {}
                    if augmenter_type == "scalecrop":
                        # these values are dropped as scalecrop
                        # doesn't have rotation implemented
                        items2drop = {"rotation": 0, "rotratio": 0.0}
                    # Also drop maDLC smart cropping augmentation parameters
                    for key in [
                        "pre_resize",
                        "crop_size",
                        "max_shift",
                        "crop_sampling",
                    ]:
                        items2drop[key] = None

                    trainingdata = MakeTrain_pose_yaml(
                        items2change,
                        path_train_config,
                        defaultconfigfile,
                        items2drop,
                        save=(engine == Engine.TF),
                    )

                    keys2save = [
                        "dataset",
                        "num_joints",
                        "all_joints",
                        "all_joints_names",
                        "net_type",
                        "init_weights",
                        "global_scale",
                        "location_refinement",
                        "locref_stdev",
                    ]
                    MakeTest_pose_yaml(trainingdata, keys2save, path_test_config)
                    print(
                        "The training dataset is successfully created. Use the function"
                        "'train_network' to start training. Happy training!"
                    )
                elif engine == Engine.PYTORCH:
                    from deeplabcut.pose_estimation_pytorch.config.make_pose_config import (
                        make_pytorch_pose_config,
                        make_pytorch_test_config,
                    )
                    from deeplabcut.pose_estimation_pytorch.modelzoo.config import (
                        make_super_animal_finetune_config,
                    )

                    if weight_init is not None and weight_init.with_decoder:
                        pytorch_cfg = make_super_animal_finetune_config(
                            project_config=cfg,
                            pose_config_path=path_train_config,
                            model_name=net_type,
                            detector_name=detector_type,
                            weight_init=weight_init,
                            save=True,
                        )
                    else:
                        pytorch_cfg = make_pytorch_pose_config(
                            project_config=cfg,
                            pose_config_path=path_train_config,
                            net_type=net_type,
                            top_down=top_down,
                            detector_type=detector_type,
                            weight_init=weight_init,
                            save=True,
                            ctd_conditions=ctd_conditions,
                        )

                    make_pytorch_test_config(pytorch_cfg, path_test_config, save=True)

        return splits

create_training_dataset_from_existing_split

create_training_dataset_from_existing_split(
    config: str | Path | ProjectConfig | dict,
    from_shuffle: int,
    from_trainsetindex: int = 0,
    num_shuffles: int = 1,
    shuffles: list[int] | None = None,
    userfeedback: bool = True,
    net_type: str | None = None,
    detector_type: str | None = None,
    augmenter_type: str | None = None,
    ctd_conditions: int | str | Path | tuple[int, str] | tuple[int, int] | None = None,
    posecfg_template: dict | None = None,
    superanimal_name: str = "",
    weight_init: WeightInitialization | None = None,
    engine: Engine | None = None,
) -> None | list[int]

Labels from all the extracted frames are merged into a single .h5 file. Only the videos included in the config file are used to create this dataset.

Parameters:

Name Type Description Default

config

str | Path | ProjectConfig | dict
required

from_shuffle

int

The index of the shuffle from which to copy the train/test split.

required

from_trainsetindex

int

The trainset index of the shuffle from which to use the data split. Default is 0.

0

num_shuffles

int

Number of shuffles of training dataset to create, used if shuffles is None.

1

shuffles

list[int] | None

If defined, num_shuffles is ignored and a shuffle is created for each index given in the list.

None

userfeedback

bool

If False, all requested train/test splits are created (no matter if they already exist). If you want to assure that previous splits etc. are not overwritten, set this to True and you will be asked for each existing split if you want to overwrite it.

True

net_type

str | None

The type of network to create the shuffle for. Currently supported options for engine=Engine.TF are: * resnet_50 * resnet_101 * resnet_152 * mobilenet_v2_1.0 * mobilenet_v2_0.75 * mobilenet_v2_0.5 * mobilenet_v2_0.35 * efficientnet-b0 * efficientnet-b1 * efficientnet-b2 * efficientnet-b3 * efficientnet-b4 * efficientnet-b5 * efficientnet-b6 Currently supported options for engine=Engine.TF can be obtained by calling deeplabcut.pose_estimation_pytorch.available_models().

None

detector_type

str | None

string, optional, default=None Only for the PyTorch engine. When passing creating shuffles for top-down models, you can specify which detector you want. If the detector_type is None, the ssdlite will be used. The list of all available detectors can be obtained by calling deeplabcut.pose_estimation_pytorch.available_detectors(). Supported options: * ssdlite * fasterrcnn_mobilenet_v3_large_fpn * fasterrcnn_resnet50_fpn_v2

None

augmenter_type

str | None

Type of augmenter. Currently supported augmenters for engine=Engine.TF are * default * scalecrop * imgaug * tensorpack * deterministic The only supported augmenter for Engine.PYTORCH is albumentations.

None

posecfg_template

dict | None

Only for Engine.TF. Path to a pose_cfg.yaml file to use as a template for generating the new one for the current iteration. Useful if you would like to start with the same parameters a previous training iteration. None uses the default pose_cfg.yaml.

None

superanimal_name

str

Specify the superanimal name is transfer learning with superanimal is desired. This makes sure the pose config template uses superanimal configs as template.

''

weight_init

WeightInitialization | None

Only for Engine.PYTORCH. Specify how model weights should be initialized. The default mode uses transfer learning from ImageNet weights.

None

engine

Engine | None

Whether to create a pose config for a Tensorflow or PyTorch model. Defaults to the value specified in the project configuration file. If no engine is specified for the project, defaults to deeplabcut.compat.DEFAULT_ENGINE.

None

ctd_conditions

int | str | Path | tuple[int, str] | tuple[int, int] | None

int | str | Path | tuple[int, str] | tuple[int, int] | None, default = None, If using a conditional-top-down (CTD) net_type, this argument should be specified. It defines the conditions that will be used with the CTD model. It can be either: * A shuffle number (ctd_conditions: int), which must correspond to a bottom-up (BU) network type. Valid for both evaluation and live analyze. * A predictions file path (ctd_conditions: string | Path), which must correspond to a .json or .h5 predictions file. Evaluation-only — not valid for analyze_images / analyze_videos. * A shuffle number and a particular snapshot (ctd_conditions: tuple[int, str] | tuple[int, int]), which respectively correspond to a bottom-up (BU) network type and a particular snapshot name or index.

None

Returns:

Type Description
None | list[int]

If training dataset was successfully created, a list of tuples is returned. The first two elements in each tuple represent the training fraction and the shuffle value. The last two elements in each tuple are arrays of integers representing the training and test indices.

Returns None if training dataset could not be created.

Raises:

Type Description
ValueError

If the shuffle from which to copy the data split doesn't exist.

Source code in deeplabcut/generate_training_dataset/trainingsetmanipulation.py
def create_training_dataset_from_existing_split(
    config: str | Path | ProjectConfig | dict,
    from_shuffle: int,
    from_trainsetindex: int = 0,
    num_shuffles: int = 1,
    shuffles: list[int] | None = None,
    userfeedback: bool = True,
    net_type: str | None = None,
    detector_type: str | None = None,
    augmenter_type: str | None = None,
    ctd_conditions: int | str | Path | tuple[int, str] | tuple[int, int] | None = None,
    posecfg_template: dict | None = None,
    superanimal_name: str = "",
    weight_init: WeightInitialization | None = None,
    engine: Engine | None = None,
) -> None | list[int]:
    """Labels from all the extracted frames are merged into a single .h5 file. Only the
    videos included in the config file are used to create this dataset.

    Args:
        config (str | Path | ProjectConfig | dict):
        Full path of the ``config.yaml`` file. Alternatively, a ProjectConfig object or a dictionary can be passed.

        from_shuffle: The index of the shuffle from which to copy the train/test split.

        from_trainsetindex: The trainset index of the shuffle from which to use the data
            split. Default is 0.

        num_shuffles: Number of shuffles of training dataset to create, used if
            ``shuffles`` is None.

        shuffles: If defined, ``num_shuffles`` is ignored and a shuffle is created for
            each index given in the list.

        userfeedback: If ``False``, all requested train/test splits are created (no
            matter if they already exist). If you want to assure that previous splits
            etc. are not overwritten, set this to ``True`` and you will be asked for
            each existing split if you want to overwrite it.

        net_type: The type of network to create the shuffle for. Currently supported
            options for engine=Engine.TF are:
                * ``resnet_50``
                * ``resnet_101``
                * ``resnet_152``
                * ``mobilenet_v2_1.0``
                * ``mobilenet_v2_0.75``
                * ``mobilenet_v2_0.5``
                * ``mobilenet_v2_0.35``
                * ``efficientnet-b0``
                * ``efficientnet-b1``
                * ``efficientnet-b2``
                * ``efficientnet-b3``
                * ``efficientnet-b4``
                * ``efficientnet-b5``
                * ``efficientnet-b6``
            Currently supported  options for engine=Engine.TF can be obtained by calling
            ``deeplabcut.pose_estimation_pytorch.available_models()``.

        detector_type: string, optional, default=None
            Only for the PyTorch engine.
            When passing creating shuffles for top-down models, you can specify which
            detector you want. If the detector_type is None, the ```ssdlite``` will be
            used. The list of all available detectors can be obtained by calling
            ``deeplabcut.pose_estimation_pytorch.available_detectors()``. Supported
            options:
                * ``ssdlite``
                * ``fasterrcnn_mobilenet_v3_large_fpn``
                * ``fasterrcnn_resnet50_fpn_v2``

        augmenter_type: Type of augmenter. Currently supported augmenters for
            engine=Engine.TF are
                * ``default``
                * ``scalecrop``
                * ``imgaug``
                * ``tensorpack``
                * ``deterministic``
            The only supported augmenter for Engine.PYTORCH is ``albumentations``.

        posecfg_template: Only for Engine.TF. Path to a ``pose_cfg.yaml`` file to use as
            a template for generating the new one for the current iteration. Useful if
            you would like to start with the same parameters a previous training
            iteration. None uses the default ``pose_cfg.yaml``.

        superanimal_name: Specify the superanimal name is transfer learning with
            superanimal is desired. This makes sure the pose config template uses
            superanimal configs as template.

        weight_init: Only for Engine.PYTORCH. Specify how model weights should be
            initialized. The default mode uses transfer learning from ImageNet weights.

        engine: Whether to create a pose config for a Tensorflow or PyTorch model.
            Defaults to the value specified in the project configuration file. If no
            engine is specified for the project, defaults to
            ``deeplabcut.compat.DEFAULT_ENGINE``.

        ctd_conditions: int | str | Path | tuple[int, str] | tuple[int, int] | None, default = None,
            If using a conditional-top-down (CTD) net_type, this argument should be
            specified. It defines the conditions that will be used with the CTD model.
            It can be either:
                * A shuffle number (ctd_conditions: int), which must correspond to a
                  bottom-up (BU) network type. Valid for both evaluation and live
                  analyze.
                * A predictions file path (ctd_conditions: string | Path), which must
                  correspond to a .json or .h5 predictions file. Evaluation-only —
                  not valid for ``analyze_images`` / ``analyze_videos``.
                * A shuffle number and a particular snapshot
                  (ctd_conditions: tuple[int, str] | tuple[int, int]), which
                  respectively correspond to a bottom-up (BU) network type and a
                  particular snapshot name or index.

    Returns:
        If training dataset was successfully created, a list of tuples is returned.
        The first two elements in each tuple represent the training fraction and the
        shuffle value. The last two elements in each tuple are arrays of integers
        representing the training and test indices.

        Returns None if training dataset could not be created.

    Raises:
        ValueError: If the shuffle from which to copy the data split doesn't exist.
    """
    cfg = ProjectConfig.from_any(config, repair_path=True)
    trainset_meta_path = metadata.TrainingDatasetMetadata.path(cfg)
    if not trainset_meta_path.exists():
        meta = metadata.TrainingDatasetMetadata.create(cfg)
        meta.save()
    else:
        meta = metadata.TrainingDatasetMetadata.load(cfg, load_splits=False)

    shuffle = meta.get(trainset_index=from_trainsetindex, index=from_shuffle)
    shuffle = shuffle.load_split(cfg, trainset_path=trainset_meta_path.parent)

    num_copies = num_shuffles
    if shuffles is not None:
        num_copies = len(shuffles)

    # pad the train and test indices with -1s so the training fraction is exact
    train_idx = list(shuffle.split.train_indices)
    test_idx = list(shuffle.split.test_indices)
    n_train, n_test = len(train_idx), len(test_idx)

    train_fraction = round(cfg["TrainingFraction"][from_trainsetindex], 2)
    if round(n_train / (n_train + n_test), 2) != train_fraction:
        train_padding, test_padding = _compute_padding(train_fraction, n_train, n_test)
        train_idx = train_idx + (train_padding * [-1])
        test_idx = test_idx + (test_padding * [-1])

    return create_training_dataset(
        config=cfg,
        num_shuffles=num_shuffles,
        Shuffles=shuffles,
        userfeedback=userfeedback,
        trainIndices=[train_idx for _ in range(num_copies)],
        testIndices=[test_idx for _ in range(num_copies)],
        net_type=net_type,
        detector_type=detector_type,
        augmenter_type=augmenter_type,
        posecfg_template=posecfg_template,
        superanimal_name=superanimal_name,
        weight_init=weight_init,
        engine=engine,
        ctd_conditions=ctd_conditions,
    )

create_training_model_comparison

create_training_model_comparison(
    config: str | Path | ProjectConfig | dict,
    trainindex=0,
    num_shuffles=1,
    net_types=None,
    augmenter_types=None,
    userfeedback=False,
    windows2linux=False,
)

Creates a training dataset to compare networks and augmentation types.

The datasets are created such that the shuffles have same training and testing indices. Therefore, this function is useful for benchmarking the performance of different network and augmentation types on the same training/testdata.

Parameters:

Name Type Description Default

config

str | Path | ProjectConfig | dict

Full path of the config.yaml file. Alternatively, a ProjectConfig object or a dictionary can be passed.

required

trainindex

int

Either (in case uniform = True) indexes which element of TrainingFraction in the config file should be used (note it is a list!). Alternatively (uniform = False) indexes which folder is dropped, i.e. the first if trainindex=0, the second if trainindex=1, etc. Defaults to 0.

0

num_shuffles

int

Number of shuffles of training dataset to create, i.e. [1,2,3] for num_shuffles=3. Defaults to 1.

1

net_types

list[str]

Currently supported networks are

  • "resnet_50"
  • "resnet_101"
  • "resnet_152"
  • "mobilenet_v2_1.0"
  • "mobilenet_v2_0.75"
  • "mobilenet_v2_0.5"
  • "mobilenet_v2_0.35"
  • "efficientnet-b0"
  • "efficientnet-b1"
  • "efficientnet-b2"
  • "efficientnet-b3"
  • "efficientnet-b4"
  • "efficientnet-b5"
  • "efficientnet-b6"

Defaults to ["resnet_50"].

None

augmenter_types

list[str]

Currently supported augmenters are

  • "default"
  • "imgaug"
  • "tensorpack"
  • "deterministic"

Defaults to ["imgaug"].

None

userfeedback

bool

If False, then all requested train/test splits are created, no matter if they already exist. If you want to assure that previous splits etc. are not overwritten, then set this to True and you will be asked for each split. Defaults to False.

False

windows2linux

..deprecated:: Has no effect since 2.2.0.4 and will be removed in 2.2.1.

False

Returns:

Name Type Description
shuffle_list list

List of indices corresponding to the trainingsplits/models that were created.

Examples:

On Linux/MacOS

shuffle_list = deeplabcut.create_training_model_comparison(
    '/analysis/project/reaching-task/config.yaml',
    num_shuffles=1,
    net_types=['resnet_50','resnet_152'],
    augmenter_types=['tensorpack','deterministic'],
)

On Windows

shuffle_list = deeplabcut.create_training_model_comparison(
    'C:\Users\Ulf\looming-task\config.yaml',
    num_shuffles=1,
    net_types=['resnet_50','resnet_152'],
    augmenter_types=['tensorpack','deterministic'],
)

See examples/testscript_openfielddata_augmentationcomparison.py for an example of how to use shuffle_list.

Source code in deeplabcut/generate_training_dataset/trainingsetmanipulation.py
def create_training_model_comparison(
    config: str | Path | ProjectConfig | dict,
    trainindex=0,
    num_shuffles=1,
    net_types=None,
    augmenter_types=None,
    userfeedback=False,
    windows2linux=False,
):
    """Creates a training dataset to compare networks and augmentation types.

    The datasets are created such that the shuffles have same training and testing
    indices. Therefore, this function is useful for benchmarking the performance of
    different network and augmentation types on the same training/testdata.

    Args:
        config (str | Path | ProjectConfig | dict): Full path of the config.yaml file.
            Alternatively, a ProjectConfig object or a dictionary can be passed.
        trainindex (int, optional): Either (in case uniform = True) indexes which element
            of TrainingFraction in the config file should be used (note it is a list!).
            Alternatively (uniform = False) indexes which folder is dropped, i.e. the
            first if trainindex=0, the second if trainindex=1, etc. Defaults to 0.
        num_shuffles (int, optional): Number of shuffles of training dataset to create,
            i.e. [1,2,3] for num_shuffles=3. Defaults to 1.
        net_types (list[str], optional): Currently supported networks are

            * ``"resnet_50"``
            * ``"resnet_101"``
            * ``"resnet_152"``
            * ``"mobilenet_v2_1.0"``
            * ``"mobilenet_v2_0.75"``
            * ``"mobilenet_v2_0.5"``
            * ``"mobilenet_v2_0.35"``
            * ``"efficientnet-b0"``
            * ``"efficientnet-b1"``
            * ``"efficientnet-b2"``
            * ``"efficientnet-b3"``
            * ``"efficientnet-b4"``
            * ``"efficientnet-b5"``
            * ``"efficientnet-b6"``

            Defaults to ["resnet_50"].

        augmenter_types (list[str], optional): Currently supported augmenters are

            * ``"default"``
            * ``"imgaug"``
            * ``"tensorpack"``
            * ``"deterministic"``

            Defaults to ["imgaug"].

        userfeedback (bool, optional): If ``False``, then all requested train/test splits
            are created, no matter if they already exist. If you want to assure that
            previous splits etc. are not overwritten, then set this to True and you will
            be asked for each split. Defaults to False.
        windows2linux: ..deprecated:: Has no effect since 2.2.0.4 and will be removed in
            2.2.1.

    Returns:
        shuffle_list (list): List of indices corresponding to the trainingsplits/models
            that were created.

    Examples:
        On Linux/MacOS

            shuffle_list = deeplabcut.create_training_model_comparison(
                '/analysis/project/reaching-task/config.yaml',
                num_shuffles=1,
                net_types=['resnet_50','resnet_152'],
                augmenter_types=['tensorpack','deterministic'],
            )

        On Windows

            shuffle_list = deeplabcut.create_training_model_comparison(
                'C:\\Users\\Ulf\\looming-task\\config.yaml',
                num_shuffles=1,
                net_types=['resnet_50','resnet_152'],
                augmenter_types=['tensorpack','deterministic'],
            )

        See ``examples/testscript_openfielddata_augmentationcomparison.py`` for an
        example of how to use ``shuffle_list``.
    """
    # read cfg file
    if augmenter_types is None:
        augmenter_types = ["imgaug"]
    if net_types is None:
        net_types = ["resnet_50"]
    cfg = ProjectConfig.from_any(config, repair_path=True)

    if windows2linux:
        warnings.warn(
            "`windows2linux` has no effect since 2.2.0.4 and will be removed in 2.2.1.",
            FutureWarning,
            stacklevel=2,
        )

    # create log file
    log_file_name = str(Path(cfg["project_path"]) / "training_model_comparison.log")
    logger = logging.getLogger("training_model_comparison")
    if not logger.handlers:
        logger = logging.getLogger("training_model_comparison")
        hdlr = logging.FileHandler(log_file_name)
        formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
        hdlr.setFormatter(formatter)
        logger.addHandler(hdlr)
        logger.setLevel(logging.INFO)
    else:
        pass

    existing_shuffles = get_existing_shuffle_indices(cfg)
    if len(existing_shuffles) == 0:
        largestshuffleindex = 0
    else:
        largestshuffleindex = existing_shuffles[-1] + 1

    shuffle_list = []
    for shuffle in range(num_shuffles):
        trainIndices, testIndices = mergeandsplit(cfg, trainindex=trainindex, uniform=True)
        for idx_net, net in enumerate(net_types):
            for idx_aug, aug in enumerate(augmenter_types):
                get_max_shuffle_idx = (
                    largestshuffleindex
                    + idx_aug
                    + idx_net * len(augmenter_types)
                    + shuffle * len(augmenter_types) * len(net_types)
                )

                shuffle_list.append(get_max_shuffle_idx)
                log_info = str(
                    "Shuffle index:"
                    + str(get_max_shuffle_idx)
                    + ", net_type:"
                    + net
                    + ", augmenter_type:"
                    + aug
                    + ", trainsetindex:"
                    + str(trainindex)
                    + ", frozen shuffle ID:"
                    + str(shuffle)
                )
                create_training_dataset(
                    cfg,
                    Shuffles=[get_max_shuffle_idx],
                    net_type=net,
                    trainIndices=[trainIndices],
                    testIndices=[testIndices],
                    augmenter_type=aug,
                    userfeedback=userfeedback,
                )
                logger.info(log_info)

    return shuffle_list

drop_likelihood_columns

drop_likelihood_columns(df: DataFrame) -> pd.DataFrame

Drop any columns whose coord level is named 'likelihood'.

This sanitizes annotation DataFrames coming from h5/csv files before they are used for training dataset generation.

NOTE @C-Achard 2026-05-18: This is used in several places as a guard

Most call sites using this should instead go through a canonical, validated project loading function
AND THEN do any custom local processing they require. The current design is hard to maintain and error prone,
and lacks a clearly documented, centralized project I/O interface.
Source code in deeplabcut/generate_training_dataset/trainingsetmanipulation.py
def drop_likelihood_columns(df: pd.DataFrame) -> pd.DataFrame:
    """Drop any columns whose coord level is named 'likelihood'.

    This sanitizes annotation DataFrames coming from h5/csv files before they are
    used for training dataset generation.

    # NOTE @C-Achard 2026-05-18: This is used in several places as a guard
        Most call sites using this should instead go through a canonical, validated project loading function
        AND THEN do any custom local processing they require. The current design is hard to maintain and error prone,
        and lacks a clearly documented, centralized project I/O interface.
    """
    if not isinstance(df.columns, pd.MultiIndex):
        return df

    coord_level = "coords" if "coords" in df.columns.names else df.columns.names[-1]
    coord_values = df.columns.get_level_values(coord_level)

    likelihood_mask = coord_values == "likelihood"
    if likelihood_mask.any():
        logging.warning("Detected likelihood columns in annotation data; dropping them.", stacklevel=2)
        df = df.drop(columns=df.columns[likelihood_mask])

    return df

dropannotationfileentriesduetodeletedimages

dropannotationfileentriesduetodeletedimages(config: str | Path)

Drop entries for all deleted images in annotation files, i.e. for folders of the type: /labeled-data/folder/CollectedData_scorer.h5 Will be carried out iteratively for all folders in labeled-data.

Parameters:

Name Type Description Default

config

string

String containing the full path of the config file in the project.

required
Source code in deeplabcut/generate_training_dataset/trainingsetmanipulation.py
def dropannotationfileentriesduetodeletedimages(config: str | Path):
    """Drop entries for all deleted images in annotation files, i.e. for folders of the
    type: /labeled-data/*folder*/CollectedData_*scorer*.h5 Will be carried out
    iteratively for all *folders* in labeled-data.

    Args:
        config (string): String containing the full path of the config file in the project.
    """
    cfg = read_config(config)
    videos = cfg["video_sets"].keys()
    video_names = [Path(i).stem for i in videos]
    folders = [Path(config).parent / "labeled-data" / Path(i) for i in video_names]

    for folder in folders:
        fn = folder / ("CollectedData_" + cfg["scorer"] + ".h5")
        try:
            DC = pd.read_hdf(fn)
        except FileNotFoundError:
            print("Attention:", folder, "does not appear to have labeled data!")
            continue
        dropped = False
        for imagename in DC.index:
            if Path(cfg["project_path"]).joinpath(*imagename).is_file():
                pass
            else:
                print("Dropping...", imagename)
                DC = DC.drop(imagename)
                dropped = True
        if dropped:
            DC.to_hdf(fn, key="df_with_missing", mode="w")
            DC.to_csv(folder / ("CollectedData_" + cfg["scorer"] + ".csv"))

dropduplicatesinannotatinfiles

dropduplicatesinannotatinfiles(config: str | Path)

Drop duplicate entries (of images) in annotation files (this should no longer happen, but might be useful).

Parameters:

Name Type Description Default

config

string

String containing the full path of the config file in the project.

required
Source code in deeplabcut/generate_training_dataset/trainingsetmanipulation.py
def dropduplicatesinannotatinfiles(config: str | Path):
    """Drop duplicate entries (of images) in annotation files (this should no longer
    happen, but might be useful).

    Args:
        config (string): String containing the full path of the config file in the project.
    """
    cfg = read_config(config)
    videos = cfg["video_sets"].keys()
    video_names = [Path(i).stem for i in videos]
    folders = [Path(config).parent / "labeled-data" / Path(i) for i in video_names]

    for folder in folders:
        try:
            fn = folder / ("CollectedData_" + cfg["scorer"] + ".h5")
            DC = pd.read_hdf(fn)
            numimages = len(DC.index)
            DC = DC[~DC.index.duplicated(keep="first")]
            if len(DC.index) < numimages:
                print("Dropped", numimages - len(DC.index))
                DC.to_hdf(fn, key="df_with_missing", mode="w")
                DC.to_csv(folder / ("CollectedData_" + cfg["scorer"] + ".csv"))

        except FileNotFoundError:
            print("Attention:", folder, "does not appear to have labeled data!")

dropimagesduetolackofannotation

dropimagesduetolackofannotation(config: str | Path)

Drop images from corresponding folder for not annotated images: /labeled-data/folder/CollectedData_scorer.h5 Will be carried out iteratively for all folders in labeled-data.

Parameters:

Name Type Description Default

config

string

String containing the full path of the config file in the project.

required
Source code in deeplabcut/generate_training_dataset/trainingsetmanipulation.py
def dropimagesduetolackofannotation(config: str | Path):
    """
    Drop images from corresponding folder for not annotated images: /labeled-data/*folder*/CollectedData_*scorer*.h5
    Will be carried out iteratively for all *folders* in labeled-data.

    Args:
        config (string): String containing the full path of the config file in the project.
    """
    cfg = read_config(config)
    videos = cfg["video_sets"].keys()
    video_names = [Path(i).stem for i in videos]
    folders = [Path(config).parent / "labeled-data" / Path(i) for i in video_names]

    for folder in folders:
        h5file = folder / ("CollectedData_" + cfg["scorer"] + ".h5")
        try:
            DC = pd.read_hdf(h5file)
        except FileNotFoundError:
            print("Attention:", folder, "does not appear to have labeled data!")
            continue
        conversioncode.guarantee_multiindex_rows(DC)
        annotatedimages = [fn[-1] for fn in DC.index]
        imagelist = [f.name for f in folder.iterdir() if ".png" in f.name]
        print("Annotated images: ", len(annotatedimages), " In folder:", len(imagelist))
        for imagename in imagelist:
            if imagename in annotatedimages:
                pass
            else:
                fullpath = folder / imagename
                if fullpath.is_file():
                    print("Deleting", fullpath)
                    fullpath.unlink()

        annotatedimages = [fn[-1] for fn in DC.index]
        imagelist = [f.name for f in folder.iterdir() if ".png" in f.name]
        print(
            "PROCESSED:",
            folder,
            " now # of annotated images: ",
            len(annotatedimages),
            " in folder:",
            len(imagelist),
        )

dropunlabeledframes

dropunlabeledframes(config: str | Path)

Drop entries such that all the bodyparts are not labeled from the annotation files, i.e. h5 and csv files Will be carried out iteratively for all folders in labeled-data.

Parameters:

Name Type Description Default

config

string

String containing the full path of the config file in the project.

required
Source code in deeplabcut/generate_training_dataset/trainingsetmanipulation.py
def dropunlabeledframes(config: str | Path):
    """Drop entries such that all the bodyparts are not labeled from the annotation
    files, i.e. h5 and csv files Will be carried out iteratively for all *folders* in
    labeled-data.

    Args:
        config (string): String containing the full path of the config file in the project.
    """
    cfg = read_config(config)
    videos = cfg["video_sets"].keys()
    video_names = [Path(i).stem for i in videos]
    folders = [Path(config).parent / "labeled-data" / Path(i) for i in video_names]

    for folder in folders:
        h5file = folder / ("CollectedData_" + cfg["scorer"] + ".h5")
        try:
            DC = pd.read_hdf(h5file)
        except FileNotFoundError:
            print("Skipping ", folder, "...")
            continue
        before_len = len(DC.index)
        DC = DC.dropna(how="all")  # drop rows where all values are missing(NaN)
        after_len = len(DC.index)
        dropped = before_len - after_len
        if dropped:
            DC.to_hdf(h5file, key="df_with_missing", mode="w")
            DC.to_csv(folder / ("CollectedData_" + cfg["scorer"] + ".csv"))

            print("Dropped ", dropped, "entries in ", folder)

    print("Done.")

get_existing_shuffle_indices

get_existing_shuffle_indices(
    cfg: dict | str | Path, train_fraction: float | None = None, engine: Engine | None = None
) -> list[int]

Get the existing shuffle indices.

Parameters:

Name Type Description Default

cfg

dict | str | Path

The content of a project configuration file, or the path to the project configuration file.

required

train_fraction

float | None

If defined, only get the indices of shuffles with this train fraction.

None

engine

Engine | None

If specified, returns only the shuffle indices that were created with the given engine. Can only be used when train_fraction is also defined.

None

Returns:

Type Description
list[int]

the indices of existing shuffles for this iteration of the project, sorted by ascending index

Source code in deeplabcut/generate_training_dataset/trainingsetmanipulation.py
def get_existing_shuffle_indices(
    cfg: dict | str | Path,
    train_fraction: float | None = None,
    engine: Engine | None = None,
) -> list[int]:
    """Get the existing shuffle indices.

    Args:
        cfg: The content of a project configuration file, or the path to the project
            configuration file.
        train_fraction: If defined, only get the indices of shuffles with this train
            fraction.
        engine: If specified, returns only the shuffle indices that were created with
            the given engine. Can only be used when train_fraction is also defined.

    Returns:
        the indices of existing shuffles for this iteration of the project, sorted by
        ascending index
    """

    def is_valid_data_stem(stem: str) -> bool:
        if len(stem) == 0:
            return False
        suffix = stem.split("_")[-1]
        if len(suffix) == 0:
            return False
        info = suffix.split("shuffle")
        if len(info) != 2:
            return False
        train_frac, idx = info
        return (
            train_frac.isdigit()
            and idx.isdigit()
            and (train_fraction is None or int(train_frac) == int(100 * train_fraction))
        )

    if isinstance(cfg, (str, Path)):
        cfg = read_config(cfg)

    project = Path(cfg["project_path"])
    trainset_folder = project / auxiliaryfunctions.get_training_set_folder(cfg)
    if not trainset_folder.exists():
        return []

    shuffle_indices = [
        int(p.stem.split("shuffle")[-1])
        for p in trainset_folder.iterdir()
        if (p.stem.startswith("Documentation_data") and p.suffix == ".pickle" and is_valid_data_stem(p.stem))
    ]
    if engine is not None:
        if train_fraction is None:
            raise ValueError(f"Must select {train_fraction} to filter shuffles by engine")

        shuffle_indices = [
            idx
            for idx in shuffle_indices
            if (
                project
                / auxiliaryfunctions.get_model_folder(
                    trainFraction=train_fraction,
                    shuffle=idx,
                    cfg=cfg,
                    engine=engine,
                )
            ).exists()
        ]

    return sorted(shuffle_indices)

get_largestshuffle_index

get_largestshuffle_index(config: str | Path)

Returns the largest shuffle for all dlc-models in the current iteration.

Source code in deeplabcut/generate_training_dataset/trainingsetmanipulation.py
def get_largestshuffle_index(config: str | Path):
    """Returns the largest shuffle for all dlc-models in the current iteration."""
    shuffle_indices = get_existing_shuffle_indices(config)
    if len(shuffle_indices) > 0:
        return shuffle_indices[-1]

    return None

merge_annotateddatasets

merge_annotateddatasets(cfg, trainingsetfolder_full)

Merges all the h5 files for all labeled-datasets (from individual videos).

This is a bit of a mess because of cross platform compatibility.

Within platform comp. is straightforward. But if someone labels on windows and wants to train on a unix cluster or colab...

Source code in deeplabcut/generate_training_dataset/trainingsetmanipulation.py
def merge_annotateddatasets(cfg, trainingsetfolder_full):
    """Merges all the h5 files for all labeled-datasets (from individual videos).

    This is a bit of a mess because of cross platform compatibility.

    Within platform comp. is straightforward.
    But if someone labels on windows and wants to train on a unix cluster or colab...
    """
    AnnotationData = []
    data_path = Path(cfg["project_path"]) / "labeled-data"
    videos = cfg["video_sets"].keys()
    video_filenames = parse_video_filenames(videos)
    for filename in video_filenames:
        file_path = data_path / filename / f"CollectedData_{cfg['scorer']}.h5"
        try:
            data = pd.read_hdf(file_path)
            conversioncode.guarantee_multiindex_rows(data)
            if data.columns.levels[0][0] != cfg["scorer"]:
                print(
                    f"{file_path} labeled by a different scorer. "
                    "This data will not be utilized in training dataset creation."
                    "If you need to merge datasets across scorers, see "
                    "https://github.com/DeepLabCut/DeepLabCut/wiki/Using-labeled-data-in\
                        -DeepLabCut-that-was-annotated-elsewhere-(or-merge-across-labelers)"
                )
                continue
            AnnotationData.append(data)
        except FileNotFoundError:
            print(file_path, " not found (perhaps not annotated).")

    if not len(AnnotationData):
        print(
            "Annotation data was not found by splitting video paths (from config['video_sets']). "
            "An alternative route is taken..."
        )
        AnnotationData = conversioncode.merge_windowsannotationdataONlinuxsystem(cfg)
        if not len(AnnotationData):
            print("No data was found!")
            return

    AnnotationData = pd.concat(AnnotationData).sort_index()
    # When concatenating DataFrames with misaligned column labels,
    # all sorts of reordering may happen (mainly depending on 'sort' and 'join')
    # Ensure the 'bodyparts' level agrees with the order in the config file.
    if cfg.get("multianimalproject", False):
        (
            _,
            uniquebodyparts,
            multianimalbodyparts,
        ) = auxfun_multianimal.extractindividualsandbodyparts(cfg)
        bodyparts = multianimalbodyparts + uniquebodyparts
    else:
        bodyparts = cfg["bodyparts"]
    AnnotationData = AnnotationData.reindex(bodyparts, axis=1, level=AnnotationData.columns.names.index("bodyparts"))
    # Filter out any stray likelihood columns that may have been concatenated in
    # see napari-deeplabcut #204 and DeepLabCut #3319
    AnnotationData = drop_likelihood_columns(AnnotationData)

    if AnnotationData.empty:
        logging.warning(
            "The annotated dataframe is empty after reindexing using config. "
            "Hint: are bodyparts correctly listed in the configuration?"
        )

    filename = trainingsetfolder_full / f"CollectedData_{cfg['scorer']}"
    AnnotationData.to_hdf(str(filename) + ".h5", key="df_with_missing", mode="w")
    AnnotationData.to_csv(str(filename) + ".csv")  # human readable.
    return AnnotationData

mergeandsplit

mergeandsplit(config: str | Path | ProjectConfig | dict, trainindex=0, uniform=True)

This function allows additional control over "create_training_dataset".

Merge annotated data sets (from different folders) and split data in a specific way, returns the split variables (train/test indices). Importantly, this allows one to freeze a split.

One can also either create a uniform split (uniform = True; thereby indexing TrainingFraction in config file) or leave-one-folder out split by passing the index of the corresponding video from the config.yaml file as variable trainindex.

Parameters:

Name Type Description Default

config

str | Path | ProjectConfig | dict

Full path of the config.yaml file. Alternatively, a ProjectConfig object or a dictionary can be passed.

required

trainindex

int

Either (in case uniform = True) indexes which element of TrainingFraction in the config file should be used (note it is a list!). Alternatively (uniform = False) indexes which folder is dropped, i.e. the first if trainindex=0, the second if trainindex =1, etc.

0

uniform

bool

Perform uniform split (disregarding folder structure in labeled data), or (if False) leave one folder out.

True

Examples:

To create a leave-one-folder-out model:

trainIndices, testIndices = deeplabcut.mergeandsplit(config, trainindex=0, uniform=False)

Returns the indices for the first video folder (as defined in config file) as testIndices and all others as trainIndices. You can then create the training set by calling (e.g. defining it as Shuffle 3):

deeplabcut.create_training_dataset(
    config,
    Shuffles=[3],
    trainIndices=trainIndices,
    testIndices=testIndices,
)

To freeze a (uniform) split (i.e. iid sampled from all the data):

trainIndices, testIndices = deeplabcut.mergeandsplit(config, trainindex=0, uniform=True)

You can then create two model instances that have the identical trainingset. Thereby you can assess the role of various parameters on the performance of DLC.

deeplabcut.create_training_dataset(
    config,
    Shuffles=[0, 1],
    trainIndices=[trainIndices, trainIndices],
    testIndices=[testIndices, testIndices],
)
Source code in deeplabcut/generate_training_dataset/trainingsetmanipulation.py
def mergeandsplit(config: str | Path | ProjectConfig | dict, trainindex=0, uniform=True):
    """This function allows additional control over "create_training_dataset".

    Merge annotated data sets (from different folders) and split data in a specific way,
    returns the split variables (train/test indices).
    Importantly, this allows one to freeze a split.

    One can also either create a uniform split (uniform = True; thereby indexing TrainingFraction in config file)
    or leave-one-folder out split
    by passing the index of the corresponding video from the config.yaml file as variable trainindex.

    Args:
        config (str | Path | ProjectConfig | dict): Full path of the config.yaml file.
            Alternatively, a ProjectConfig object or a dictionary can be passed.
        trainindex (int, optional): Either (in case uniform = True) indexes which element
            of TrainingFraction in the config file should be used (note it is a list!).
            Alternatively (uniform = False) indexes which folder is dropped, i.e. the
            first if trainindex=0, the second if trainindex =1, etc.
        uniform (bool, optional): Perform uniform split (disregarding folder structure in
            labeled data), or (if False) leave one folder out.

    Examples:
        To create a leave-one-folder-out model:

            trainIndices, testIndices = deeplabcut.mergeandsplit(config, trainindex=0, uniform=False)

        Returns the indices for the first video folder (as defined in config file) as
        testIndices and all others as trainIndices. You can then create the training set
        by calling (e.g. defining it as Shuffle 3):

            deeplabcut.create_training_dataset(
                config,
                Shuffles=[3],
                trainIndices=trainIndices,
                testIndices=testIndices,
            )

        To freeze a (uniform) split (i.e. iid sampled from all the data):

            trainIndices, testIndices = deeplabcut.mergeandsplit(config, trainindex=0, uniform=True)

        You can then create two model instances that have the identical trainingset.
        Thereby you can assess the role of various parameters on the performance of DLC.

            deeplabcut.create_training_dataset(
                config,
                Shuffles=[0, 1],
                trainIndices=[trainIndices, trainIndices],
                testIndices=[testIndices, testIndices],
            )
    """
    # Loading metadata from config file:
    cfg = ProjectConfig.from_any(config, repair_path=True)
    scorer = cfg["scorer"]
    project_path = cfg["project_path"]
    # Create path for training sets & store data there
    trainingsetfolder = auxiliaryfunctions.get_training_set_folder(cfg)  # Path concatenation OS platform independent
    auxiliaryfunctions.attempt_to_make_folder(Path(project_path) / str(trainingsetfolder), recursive=True)
    fn = str(Path(project_path) / trainingsetfolder / ("CollectedData_" + cfg["scorer"]))

    try:
        data = pd.read_hdf(fn + ".h5")
        data = drop_likelihood_columns(data)
    except FileNotFoundError:
        data = merge_annotateddatasets(
            cfg,
            Path(project_path) / trainingsetfolder,
        )
        if data is None:
            return [], []

    conversioncode.guarantee_multiindex_rows(data)
    data = data[scorer]  # extract labeled data

    if uniform:
        TrainingFraction = cfg["TrainingFraction"]
        trainFraction = TrainingFraction[trainindex]
        trainIndices, testIndices = SplitTrials(
            range(len(data.index)),
            trainFraction,
            True,
        )
    else:  # leave one folder out split
        videos = cfg["video_sets"].keys()
        test_video_name = [Path(i).stem for i in videos][trainindex]
        print("Excluding the following folder (from training):", test_video_name)
        trainIndices, testIndices = [], []
        for index, name in enumerate(data.index):
            if test_video_name == name[1]:  # this is the video name
                # print(name,test_video_name)
                testIndices.append(index)
            else:
                trainIndices.append(index)

    return trainIndices, testIndices

parse_video_filenames

parse_video_filenames(videos: list[str]) -> list[str]

Parses the names of all videos listed in a project's config.yaml file.

Goes through the paths all videos listed for a project, and removes entries with a duplicate video name (e.g. if a video is listed twice, once with the path /data/video-1.mov and once with the path /my-dlc-project/videos/video-1.mov, then video-1 will only be returned once). The order of videos listed is preserved.

This prevents the same labeled-data to be added multiple times when merging annotated datasets.

Prints a warning for each filename with duplicate video paths.

Parameters:

Name Type Description Default

videos

list[str]

the videos listed in the project's config.yaml file

required

Returns:

Type Description
list[str]

the filenames of videos listed in the project's config.yaml file, with duplicate entries removed

Source code in deeplabcut/generate_training_dataset/trainingsetmanipulation.py
def parse_video_filenames(videos: list[str]) -> list[str]:
    """Parses the names of all videos listed in a project's ``config.yaml`` file.

    Goes through the paths all videos listed for a project, and removes entries with a
    duplicate video name (e.g. if a video is listed twice, once with the path
    ``/data/video-1.mov`` and once with the path ``/my-dlc-project/videos/video-1.mov``,
    then ``video-1`` will only be returned once). The order of videos listed is
    preserved.

    This prevents the same labeled-data to be added multiple times when merging
    annotated datasets.

    Prints a warning for each filename with duplicate video paths.

    Args:
        videos: the videos listed in the project's config.yaml file

    Returns:
        the filenames of videos listed in the project's config.yaml file, with duplicate
        entries removed
    """
    filenames = []
    filename_to_videos = {}
    for video in videos:
        filename = Path(video).stem
        videos_with_filename = filename_to_videos.get(filename, [])
        if len(videos_with_filename) == 0:
            filenames.append(filename)

        videos_with_filename.append(video)
        filename_to_videos[filename] = videos_with_filename

    for filename, videos in filename_to_videos.items():
        if len(videos) > 1:
            video_str = "\n  * " + "\n  * ".join(videos)
            logging.warning(
                f"Found multiple videos with the same filename (``{filename}``). To "
                f"avoid issues, please edit your project's `config.yaml` file to have "
                f"each video added only once.\nDuplicate entries: {video_str}"
            )

    return filenames