ParticleDetection.modelling.export
- Functions to export a Detectron2 model as a pure pytorch model and functions to
export its output to other formats.
Author: Adrian Niemann (adrian.niemann@ovgu.de)
Date: 31.10.2022
- annotation_to_json(prediction: DetectionResult, image: Path | str, classes: dict | None = None, output_dir: Path | str = Path(), *, filename: str = 'extracted_meta_data.json', **_)[source]
Saves detected object masks in the metadata format used for model training.
Hint
This function is intended to be used as a saving function with
detect().- Parameters:
prediction (
DetectionResult) – Prediction output of a Detectron2 network. It can also be given asprediction["instances"]asdetectron2.structures.Instancesordict, as long as the resultingdictcontains at least the same keys asDetectionResult.image (Union[Path, str]) – Path to image that prediction was created from.
classes (dict, optional) –
Dictionary of classes detectable by the model with
{key}-> Index of class in the model{value}-> Name of the classBy default
None.output_dir (Path | str, optional) –
Path to a folder the output file will be written to.
By default
Path().filename (str, optional) –
Name of the
*.jsonfile the annotation data should be saved in. Already existing data for an image in this file will be overwritten.By default
"extracted_meta_data.json".
- export_model(config_path: Path, weights_path: Path, sample_img: Path, option: Literal['cpu', 'cuda'] = 'cuda') None[source]
Exports a Detectron2 model to be usable with just pytorch.
- Parameters:
config_path (Path) – File that holds the model’s configuration in yaml format.
weights_path (Path) – File that holds the trained model’s weights.
sample_img (Path) – Image to be used to trace the model.
option (EXPORT_OPTIONS, optional) –
Option whether to restrict the exported model to be used on the CPU or to also allow the use of a GPU.
By default
"cuda".
Note
The GPU version then requires the pytorch GPU version to be installed. The CPU version can be run with both, pytorch’s CPU and GPU version.
- get_sample_img(sample: Path) Tensor[source]
Loads an image into the format necessary for inference by a Detectron2 model.
- rods_to_csv(prediction: DetectionResult, image: Path | str | ndarray, classes: dict | None = None, output_dir: Path | str = Path(), *, filename: str = 'extracted_rods.csv', **kwargs) None[source]
Extract rod enpoint positions from detected object masks and save them to
*.csvfiles.For each detected object, two enpoints are determined from the detected segmentation mask. These endpoints are then saved in
*.csvformat described byDEFAULT_COLUMNS. The data is either saved into a new file, given byfilenameandoutput_diror integrated into this file, if it already exists.Hint
This function is intended to be used as a saving function with
detect().- Parameters:
prediction (
DetectionResult) – Prediction output of a Detectron2 network. It can also be given asprediction["instances"]asdetectron2.structures.Instancesordict, as long as the resultingdictcontains at least the same keys asDetectionResult.image (Union[Path, str, np.ndarray]) – (Path to ) the image that prediction was created from.
classes (dict, optional) –
_description_
By default
None.output_dir (Union[Path, str], optional) –
Path to a folder the output file will be written to.
By default
Path().filename (str, optional) –
Name of the
*.csvfile the rod position data should be saved in. Already existing data for an image in this file might get overwritten.By default
"extracted_rods.csv".**kwargs (dict, optional) –
The following keywords are used to determine the used frame-camera combination of the image used to create the
prediction. This allows the proper saving when a dataset format is given to thedetect()function instead of a list of files.cam1_name : str
cam2_name : str
frames : Iterable[int]
dataset_format : str
The following keyword arguments are passed to
rod_endpoints():method: Literal[“simple”, “advanced”]
expected_particles : Union[int, Dict[int, int], None]
- rods_to_mat(prediction: DetectionResult, image: Path | str, classes: dict | None = None, output_dir: Path | str = Path(), *_, **kwargs) None[source]
Extract rod enpoint positions from detected object masks and save them to
*.matfiles.The generated
*.matcontain one variable rod_data_links with each rod being represented by a Point1 and Point2.Hint
This function is intended to be used as a saving function with
detect().- Parameters:
prediction (
DetectionResult) – Prediction output of a Detectron2 network. It can also be given asprediction["instances"]asdetectron2.structures.Instancesordict, as long as the resultingdictcontains at least the same keys asDetectionResult.image (Union[Path, str]) – Path to image that prediction was created from.
classes (dict, optional) –
Dictionary of classes detectable by the model with
{key}-> Index of class in the model{value}-> Name of the classBy default
None.output_dir (Union[Path, str]) –
Path to a folder the output file will be written to.
By default
Path().**kwargs (dict, optional) –
Keywords, that are propagated to
rod_endpoints():method: Literal[“simple”, “advanced”]
expected_particles : Union[int, Dict[int, int], None]
See also