artus.inference package
Submodules
artus.inference.config module
Read a config file and return a cfg to use a model with detectron2 library for the inference process.
- artus.inference.config.add_config(config_path, device)
Add config for DL model coming from a yaml config file.
The config is read and a cfg suitable for detectron2 package is returned. This cfg uses a model already fine tuned with detectron2. The path to this model’sa checkpoint should be mentionned in the LOGS section of the config file.
- Parameters:
config_path (str) – the path to a config file in yaml format
device (str) – ‘cpu’ or ‘cuda’. Results of (Python(“cuda” if torch.cuda.is_available() else “cpu”))
- Returns:
a cfg to use a detectron model’s checkpoint on unlabeled images (inference)
- artus.inference.config.check_logs(config_path)
Check if the config file already contains path to a log directory (which include a checkpoint to a model)
- Parameters:
config_path (str) – the path to a config file in yaml format
- Returns:
- true if the config file includes path to a log directory,
false otherwise.
- Return type:
check_logs (bool)
artus.inference.deploy_unlabeled_dataset module
Create a fiftyone dataset with unlabeled or labeled images.
- artus.inference.deploy_unlabeled_dataset.create_or_load_dataset(dataset_name, dataset_type=['unlabeled', 'coco'], images_path=None, annotations_path=None, df_test=None, label_type=['segmentations', 'detections'])
Create a fiftyone labeled or unlabeled dataset.
If the dataset_name provided is in the local 51 database : the dataset is loaded from the database otherwise, the dataset is created
- Parameters:
dataset_name (str) – the name of the dataset that you want to create or load
dataset_type (str) – can be ‘unlabeled’ for images without annotations or ‘coco’ if images have coco annotations.
images_path (str) – a path to the directory containing the images (can be tif, png or jpg images)
annotations_path (str, optional) – a path to the COCO annotations files
df_test (str, optional) – a path to the COCO annotations files for test images
label_type – ‘segmentations’ for mask or ‘detections’ for bounding box annotations
- Returns :
A
fiftyone.core.datasetwith at least 6 fields : id, coco_id, filepath, ground_truth annotations (if provided), tags and basic metadata.
artus.inference.predict module
Module to use a model checkpoint and predict annotations on unlabeled images.
Annotations produced by the model (called predictions) are segmentation masks for instance segmentation tasks or bounding boxes for object detection tasks.
- artus.inference.predict.add_predictions_to_dataset(dataset, predictor, device, classes, predictions_field, tags=None, type_of_preds=['segm', 'bbox'], nms_threshold=1, confidence_thr=0)
Add predictions to
fiftyone.core.dataset.All the images
fiftyone.core.datasetthat have the tag indicated will be selected to be predicted by an AI model. This function uses the GPU is available or parallelize through multiple CPU to predict on all the images of a dataset.- Parameters:
dataset – a
fiftyone.core.datasetpredictor – the result of
artus.inference.build_predictor()device (str) – “cpu” or “cuda”. Results of “cuda” if torch.cuda.is_available() else “cpu”
classes (list) – a list of the classes predicted by the model
predictions_field (str) – the name of the fiftyone field that will be added to be dataset
tags (list or str) – a variable or a list of variables (optional) to filter the samples on which to predict
type_of_preds (str) – indicate if semgentation masks “segm” expected or bounding boxes “bbox”
confidence_thr (float) – a threshold between 0 and 1. Predictions with lower confidence than this threshold will be filtered out of the
fiftyone.core.dataset
- Returns:
a
fiftyone.core.datasetcontaining predictions for the tagged samples.
- artus.inference.predict.apply_nms(outputs, nms_threshold, type_of_preds=['segm', 'bbox'])
Apply non-maximum threshold on predictions made on a sample.
Non Maximum Suppression NMS is a technique to select one entity, i.e. bounding boxes or masks, out of many overlapping entities based on the IoU (intersection over union).The function applies the NMS technique for predictions of different class types, the default Detectron2 parameter MODEL.ROI_HEADS.NMS_THRESH_TEST applies NMS only for same class types instances https://github.com/facebookresearch/detectron2/issues/978
- Parameters:
outputs (list[dict]) – a list of dict in the “outputs” inference format of detectron2 lib
nms_threshold (float) – Overlap threshold used for non-maximum suppression, suppress boxes or masks with IoU >= threshold
type_of_preds (str) – indicate if predictions are semgentation masks “segm” or bounding boxes “bbox”
- Returns:
the “outputs” inference format of detectron2 lib
- Return type:
res (list[dict])
- artus.inference.predict.build_predictor(config_path, device)
Load a model in indeference mode.
- Parameters:
config_path (str) – the path to a config file in yaml format
device (str) – ‘cpu’ or ‘cuda’. Results of “cuda” if torch.cuda.is_available() else “cpu”
- Returns
predictor : the model loaded to predict unlabeled images
- artus.inference.predict.crop_mask(bbox, mask)
Crop the mask within a bounding box.
- Parameters:
bbox (list) – a list of the bounding box’s corners for the predicted object.
mask – the mask of the object predicted returned by detectron2. It is actually a mask for the whole image
bbox) (and 51 requires a mask within the) –
- Returns:
A cropped mask corresponding to the inside of the bbox for the predicted object.
- artus.inference.predict.filter_confidence_labels(dataset, fo_field, confidence_thr=0)
Filter labels that are smaller than the confidence threshold set.
- Parameters:
dataset – a
fiftyone.core.datasetfo_field (str) – a fiftyone field with model’s predictions
confidence_thr (float) – a threshold for confidence of model’s predictions
- Returns:
the
fiftyone.core.datasetwith labels smaller than the confidence threshold removed- Return type:
dataset
- artus.inference.predict.filter_small_predictions(dataset, fo_field)
Filter predictions covering less than 3 pixels in a dataset.
Predictions that are very small, i.e. smaller than 3 pixels, may trigger problems when exporting the fiftyone dataset. It is best practises to remove them.
- Parameters:
dataset – a
fiftyone.core.datasetfo_field (str) – a fiftyone field with model’s predictions
- Returns:
the
fiftyone.core.datasetwith small predictions area removed- Return type:
dataset
- artus.inference.predict.predict(predictor, filepath)
Predict masks or bounding boxes on an image.
- Parameters:
predictor – a detectron2 predictor returned by build_predictor()
filepath (str) – the absolute or relative path to the image
- Returns:
an ordered dict with the label predicted and the coordinates of the bbox or masks
- artus.inference.predict.predict_on_sample(sample_filepath, device, predictor, nms_threshold, classes, type_of_preds=['segm', 'bbox'])
Use a loaded AI model to predict segmentations mask or bounding boxes on an image.
- Parameters:
sample_filepath (str) – the filepath to an image (jpg, png or tif file)
predictor – the result of build_predictor()
device (str) – “cpu” or “cuda”. Results of “cuda” if torch.cuda.is_available() else “cpu”
nms_threshold (float) – the non-maximum-suppression threshold.
classes (list) – a list of the classes that can be predicted by the AI model.
type_of_preds (str) – indicate if semgentation masks “segm” expected or bounding boxes “bbox”
- Returns:
a fiftyone field :class:
fiftyone.core.detectionscontaining predictions for the sample