darknet#
Functions
Convert dataframe of yolo bboxes to a list of strings to be written in a file readable by darknet |
|
Save given dataset to darknet. |
|
Get dataset path and data file, deduce one from another if needed. |
|
Creates dataset object from a darknet dataset. |
|
Generic function to load a darknet like dataset by only giving it folders, class names and optionally file list instead of a data file. |
|
Same as from_darknet, expect the data file replaced with a json file containing directly annotations information. |
|
Creates dataset object from a darknet dataset. |
|
Open a |
|
Read lines coming from a darknet txt annotation file and convert it to a list of annotation dictionaries |
|
Save a dictionary to a |
|
Convert the path of an image to its corresponding label |
- bbox_to_txt(bbox_df: DataFrame) list[str][source]#
Convert dataframe of yolo bboxes to a list of strings to be written in a file readable by darknet
- Parameters:
bbox_df – bboxes dataframe. Must be in yolo format (see function above)
- Returns:
list of string describing bboxes according to darknet format
- dataset_to_darknet(dataset: Dataset, output_path: Path | str, copy_images: bool = False, overwrite_images: bool = True, overwrite_labels: bool = True, yolo_version: int = 1, data_yaml_name: str = 'data.yaml', split_name_mapping: dict[str, str] | None = None, create_split_folder: bool = False) None[source]#
Save given dataset to darknet. Will recreate folder structure of dataset_path + image relative path. Will also create metadata files needed by darknet, i.e. names file, json label maps, list of train, validation and evaluation images (depending on available splits) and train and eval data files
- Parameters:
dataset – dataset object to save as darknet
output_path – root folder where images, corresponding text files describing bboxes will be saved along metadata files described above
copy_images – If set to False, will create a symbolic link instead of copying. Much faster, but needs to keep original images in the same relative path. Defaults to False.
overwrite_images – if set to False, will skip images that are already copied. Defaults to True.
overwrite_labels – if set to False, will skip annotation that are already created. Defaults to True.
yolo_version – version number for yolo. Officially supported versions are
1to4(regular darknet),5(Ultralytics),7(Chien-Yao Wang). Versions6and8are NOT officially supported, and will be assumed to all be version5.data_yaml_name – if
yolo_versionis > 4, will be used as filename to save the yaml file at the root ofoutput_pathsplit_name_mapping – mapping dict to replace split names to other ones. split names not present in mapping will not be modified. If set to None, will apply yolov5 conventional mapping, i.e.
{'valid': 'val', 'validation': 'val', 'eval': 'test'}. Defaults to Nonecreate_split_folder – if set to True, will create a dedicated folder for each split and will save images in it. Image paths in {split}.txt will be changed accordingly. Note that this changes the dataset structure. Defaults to False
- deduce_data_file_and_dataset_folder(dataset_path: Path | str | None = None, data_file: Path | str | None = None, default_data_file: str = 'train_job.data') tuple[Path, Path][source]#
Get dataset path and data file, deduce one from another if needed.
- from_darknet(dataset_path: Path | str | None = None, data_file: Path | str | None = None, ids_map: dict[int, dict[str, Any]] | str | Path | None = None, image_info: DataFrame | None = None) Dataset[source]#
Creates dataset object from a darknet dataset. Note that category ids and image ids are not given in the dataset format and thus can only be sequential As such, if we want to convert the dataset back to another format that keeps track of image and category ids, we need to give image_info and class mapping from an external source. Here we expect it to be contained in a json label map for annotations and a DataFrame with similar columns as in the final dataset’s images DataFrame for images
- Parameters:
dataset_path – folder containing the dataset, from which the relative path are given. If not set, will use
data_file’s parent directory. Defaults to Nonedata_file – data file containing info about names, lists of train and validation images. Can be either a .data file or a .yml file (for yolov5). If not set, will use the file
train_job.dataat the root ofdataset_path. Defaults to None.ids_map – Optional dictionary containing the id_remapping that was initially applied to create the darknet dataset. Will reverse it to get back to the original class mapping. The dictionary must have darknet dataset’s category ids (in sequential order then) as keys and with corresponding values that are dictionaries containing
nameandidkeys relative to this class. Note that this can also be a path to a json file containing the dictionary. Defaults to None.image_info – Optional DataFrame containing image information. Must contain at least the following columns :
relative_path,id,width,height. Defaults to None
- Raises:
ValueError – Errors when neither
dataset_pathnordata_fileis specified- Returns:
Loaded dataset object
- from_darknet_generic(images_root: Path | str, labels_root: Path | str, names: Iterable[str], image_files_list: Iterable[str | Path] | None = None, split: str | None = None, ids_map: dict[int, dict[str, Any]] | str | Path | None = None, image_info: DataFrame | None = None) Dataset[source]#
Generic function to load a darknet like dataset by only giving it folders, class names and optionally file list instead of a data file.
Note
Unlike the darknet and yolov5 loaders, this function does only accept a single split value.
Note
If no file list is given, the function will simply glob all image files in
images_rootfolder.- Parameters:
images_root – Folder containing the image files to load
labels_root – Folder containing the txt annotations files. Each annotation file is obtained by using the same relative path as the image equivalent and replacing the extension by txt. If no such file exist, it will not error but will assume there is no annotation
names – list of classes used in the label map when saving this darknet dataset.
image_files_list – list of images to read. Their path must be relative to
images_root. If set to None, will glob all files inimages_rootsplit – Split name of the constructed dataset. Defaults to None.
ids_map – Optional dictionary containing the id_remapping that was initially applied to create the darknet dataset. Will reverse it to get back to the original class mapping. The dictionary must have darknet dataset’s category ids (in sequential order then) as keys and with corresponding values that are dictionaries containing
nameandidkeys relative to this class. Note that this can also be a path to a json file containing the dictionary. Defaults to None.image_info – Optional DataFrame containing image information. Must contain at least the following columns :
relative_path,id,width,height. Defaults to None
- Raises:
FileNotFoundError – when the annotation file corresponding to a particular image file can not be found, be it using usual darknet convention or yolov5 one.
- Returns:
Loaded dataset object
- from_darknet_json(dataset_path: Path, json_path: Path, ids_map: dict[int, dict[str, Any]], image_info: DataFrame | None, split_name: str = 'eval') Dataset[source]#
Same as from_darknet, expect the data file replaced with a json file containing directly annotations information. This is typically the format of predictions done by darknet’s detector.
- Parameters:
dataset_path – folder containing the dataset, from which the relative path are given
json_path – json file containing a list of predictions as dictionaries. Each dictionary will have bbox info as well ad image path, which will be used to retrieve the original image id thanks to image_info DataFrame
ids_map – dictionary containing the id_remapping that was initially applied to create the darknet dataset. Will reverse it to get back to the original class mapping. The dictionary must have darknet dataset’s category ids (in sequential order then) as keys and with corresponding values that are dictionaries containing
nameandidkeys relative to this class.image_info – DataFrame containing image information. Must contain at least the following columns :
relative_path,id,width,heightsplit_name – Name of the split that will be assigned to the
splitcolumn of the resulting dataset’s annotation dataframe.
- Returns:
Loaded dataset object
- from_darknet_yolov5(dataset_path: Path | str | None = None, data_file: Path | str | None = None, splits: Iterable[str] | None = None, split_name_mapping: Mapping[str, str] | None = None, ids_map: dict[int, dict[str, Any]] | str | Path | None = None, image_info: DataFrame | None = None) Dataset[source]#
Creates dataset object from a darknet dataset. Note that category ids and image ids are not given in the dataset format and thus can only be sequential As such, if we want to convert the dataset back to another format that keeps track of image and category ids, we need to give image_info and class mapping from an external source. Here we expect it to be contained in a json label map for annotations and a DataFrame with similar columns as in the final dataset’s images DataFrame for images
- Parameters:
dataset_path – folder containing the dataset, from which the relative path are given. If not set, will use
data_file’s parent directory. Defaults to Nonedata_file – data file containing info about names, lists of train and validation images. Must be a valid path to a Yaml file (either .yml or .yaml). If set to None, will use the file
data.yamlat the root ofdataset_path. Defaults to None.splits – name of splits to load. if set to None, will consider every key in
data_filethat is neithernamesnorpathto be a split. Every value in said keys must be either a valid folder or a valid text file, both relative todataset_path. Defaults to None.split_name_mapping – mapping dict to replace split names to other ones. split names not present in mapping will not be modified. If set to None, will apply yolov5 conventional mapping, i.e. ‘val’ -> ‘valid’ and ‘test’ -> ‘eval’. Defaults to None
ids_map – Optional dictionary containing the id_remapping that was initially applied to create the darknet dataset. Will reverse it to get back to the original class mapping. The dictionary must have darknet dataset’s category ids (in sequential order then) as keys and with corresponding values that are dictionaries containing
nameandidkeys relative to this class. Note that this can also be a path to a json file containing the dictionary. Defaults to None.image_info – Optional DataFrame containing image information. Must contain at least the following columns :
relative_path,id,width,height. Defaults to None
- Raises:
ValueError – Errors when neither
dataset_pathnordata_fileis specified- Returns:
Loaded dataset object
- open_data_file(data_file: Path) dict[str, str | int | float][source]#
Open a
.datafile typically used in darknet.- Parameters:
data_file – path to
.datafile- Returns:
dictionary of values contained in the .data file
- txt_to_bbox(text_lines: Iterable[str], image_id: int) list[dict[str, Any]][source]#
Read lines coming from a darknet txt annotation file and convert it to a list of annotation dictionaries
- Parameters:
text_lines – list of text lines in a particular folder all given lines are assumed to come from the same image
image_id – image index of corresponding image
- Returns:
list of annotations for the particular image. bboxes are in the darknet format, i.e. with relative coordinates
- write_data_file(data: dict[str, str | int], data_file: Path) None[source]#
Save a dictionary to a
.datafile typically used in darknet. Note that only the typical keys expected by darknet are considered, i.e. classes, train, valid, names and backup- Parameters:
data – content to save to the
.datafiledata_file – path where to save the given data dictionary
- yolov5_img_path_to_label_path(img_path: Path) Path[source]#
Convert the path of an image to its corresponding label
That is, if the path has a folder named “images”, rename that folder into “labels” and change the image extension to “txt”
See Yolov5’s image2labels .
- Parameters:
img_path – path to an image file
- Returns:
corresponding label file as it would have been searched for by yolov5