ml Package
data_utils Module
- ml.data_utils.coco_image_data(coco, img_id)
Extract image data and annotations from COCO object.
- Args:
coco: COCO object. img_id: Image ID.
- Returns:
Tuple[str, dict]: Tuple containing image path and annotation dictionary.
- ml.data_utils.image_predictions_to_id(image_path, model_version, image_shape, prediction, classes, dataset_id)
Convert image predictions to a specific format.
- Args:
image_path: Path to the image. model_version: Model version. image_shape: Shape of the image. prediction: Model prediction. classes: List of classes. dataset_id: id of label studio project with data
- Returns:
dict: Serialized prediction.
- ml.data_utils.load_coco_from_json(path)
Load COCO object from a JSON file.
- Args:
path (str): Path to the JSON file.
- Returns:
pycocotools.coco.COCO: COCO object.
- ml.data_utils.load_coco_from_prediction(prediction)
Load COCO object from a prediction.
- Args:
prediction: Prediction data. categories: List of categories. image_data: Image data.
- Returns:
pycocotools.coco.COCO: COCO object.
- ml.data_utils.parse_image_json(label_data)
Parse JSON data for image information.
- Args:
label_data: JSON data containing image information.
- Returns:
Tuple[list, list]: Tuple containing lists of image paths and annotations.
- ml.data_utils.parse_predictions(image_shape, prediction, classes_by_id)
Parse model predictions.
- Args:
image_shape: Shape of the image. prediction: Model prediction. classes_by_id: Mapping of class IDs to class names.
- Returns:
List[dict]: List of parsed predictions.
- ml.data_utils.sample_prediction_to_dict(image_shape, bbox, label, classes_by_id)
Convert a sample prediction to a dictionary.
- Args:
image_shape: Shape of the image. bbox: Bounding box coordinates. label: Label of the prediction. classes_by_id: Mapping of class IDs to class names.
- Returns:
dict: Serialized sample prediction.
- ml.data_utils.serialize_predictions(image_shapes, predictions, image_paths, model_version, classes, dataset_id)
Serialize predictions into a specific format.
- Args:
image_shapes: Shapes of the images. predictions: Model predictions. image_paths: Paths to the images. model_version: Model version. classes: List of classes. dataset_id: id of label studio project with data
- Returns:
List: List of serialized predictions.
detector_dataset Module
- class ml.detector_dataset.DetectorDataset(dataset_path, annotation_path, transforms=None, indices=None)
Dataset class for object detection.
- Args:
dataset_path (str): Path to the dataset. annotation_path (str): Path to the annotation file. transforms (callable, optional): Optional transforms to be applied to the data. indices (list, optional): List of indices to subset the dataset.
- Attributes:
indices (list): List of indices to subset the dataset. dataset_path (str): Path to the dataset. annotation_path (str): Path to the annotation file. transforms (callable, optional): Optional transforms to be applied to the data. img_paths (list): List of image paths. targets (list): List of targets. coco (pycocotools.coco.COCO): COCO object for indexing.
- Methods:
__getitem__: Get a specific item from the dataset. __len__: Get the length of the dataset.
detector_module Module
- class ml.detector_module.CocoDetectorABC(num_classes, iou_thresh, **kwargs)
Abstract base class for the COCO Detector.
- Args:
num_classes (int): Number of classes in the dataset. iou_thresh (float): IoU threshold for non-maximum suppression. **kwargs: Additional keyword arguments.
- Attributes:
num_classes (int): Number of classes. iou_thresh (float): IoU threshold for NMS. model: Model to be initialized in subclasses.
- forward(x, y)
Forward pass of the model.
- Args:
x: Input tensor. y: Ground truth tensor.
- Returns:
Model output.
- predict(x)
Make predictions using the model.
- Args:
x: Input tensor.
- Returns:
list: List of final predictions after NMS.
- class ml.detector_module.CocoDetectorModule(model: CocoDetectorABC)
Lightning Module for training, validation, and testing of the COCO Detector.
- Args:
model (CocoDetectorABC): COCO Detector model.
- Attributes:
model (CocoDetectorABC): COCO Detector model.
- configure_optimizers()
Configure the optimizer for the Lightning Module.
- Returns:
torch.optim.Optimizer: The optimizer for the model training.
- predict(x) Any
Perform predictions using the model.
- Args:
x: Input data.
- Returns:
List[dict]: List of final predictions after NMS.
- test_step(batch, batch_idx) Tensor | Mapping[str, Any] | None
Test step for Lightning Module.
- Args:
batch: Batch of test data. batch_idx (int): Batch index.
- Returns:
torch.Tensor: Loss value for testing.
- training_step(batch, batch_idx)
Training step for Lightning Module.
- Args:
batch: Batch of training data. batch_idx (int): Batch index.
- Returns:
torch.Tensor: Loss value for training.
- validation_step(batch, batch_idx) Tensor | Mapping[str, Any] | None
Validation step for Lightning Module.
- Args:
batch: Batch of validation data. batch_idx (int): Batch index.
- Returns:
torch.Tensor: Loss value for validation.
- class ml.detector_module.CocoDetectorResnet(num_classes, iou_thresh, **kwargs)
Subclass of CocoDetectorABC for ResNet-based object detection model.
- Args:
num_classes (int): Number of classes in the dataset. iou_thresh (float): IoU threshold for non-maximum suppression. **kwargs: Additional keyword arguments.
model_endpoint Module
- ml.model_endpoint.annotation_path_from_id(model_id: int)
Get the annotation path based on the model ID.
- Args:
model_id (int): ID of the model.
- Returns:
str: Annotation path.
- ml.model_endpoint.dataset_path_from_id(project_id: int)
Get the dataset path based on the project ID.
- Args:
project_id (int): ID of the project.
- Returns:
str: Dataset path.
- ml.model_endpoint.evaluate_detector(model_id: int, model: CocoDetectorModule, dataset: DetectorDataset) list[float]
Evaluate the object detection model using COCO evaluation.
- Args:
model_id (int): ID of the model. model (CocoDetectorModule): Object detection model. dataset (DetectorDataset): COCO dataset for evaluation.
- Returns:
list: COCO evaluation statistics.
- ml.model_endpoint.image_paths_for_prediction(project_id: int, model_id: int)
Get unlabeled image paths for prediction.
- Args:
project_id (int): ID of the project. model_id (int): ID of the model.
- Returns:
set[str]: Unlabeled image paths.
- ml.model_endpoint.initialize_coco_loader(dataset_path: str, annotation_path: str, batch_size: int, is_shuffled: bool = False, indices: list | None = None)
Initialize COCO DataLoader for the object detection dataset.
- Args:
dataset_path (str): Path to the dataset. annotation_path (str): Path to the annotation file. batch_size (int): Batch size. is_shuffled (bool): Flag for shuffling the dataset. indices (list): List of indices to subset the dataset.
- Returns:
DataLoader: COCO DataLoader.
- ml.model_endpoint.initialize_model(model_id: int)
Initialize the object detection model.
- Args:
model_id (int): ID of the model.
- Returns:
CocoDetectorResnet: Initialized object detection model.
- ml.model_endpoint.initialize_optimizer(model: CocoDetectorABC, model_id: int)
Initialize the optimizer for the object detection model.
- Args:
model: Object detection model. model_id (int): ID of the model.
- Returns:
torch.optim.AdamW: Initialized optimizer.
- ml.model_endpoint.load_label_studio_project(project_id: int)
Load label studio project id from the database.
- Args:
project_id (int): ID of the project.
- Returns:
Int: Label studio project id.
- ml.model_endpoint.load_model_params(model_id: int)
Load model parameters from the database.
- Args:
model_id (int): ID of the model.
- Returns:
LearningModel: Model parameters.
- ml.model_endpoint.predict(project_id: int, model_id: int) dict
Train the object detection model.
- Args:
project_id (int): ID of the project. model_id (int): ID of the model.
- Returns:
dict: COCO prediction dictionary
- ml.model_endpoint.project_classes(model_id: int)
Get the list of classes from the annotation file of the model.
- Args:
model_id (int): ID of the model.
- Returns:
list: List of class names.
- ml.model_endpoint.train(project_id: int, model_id: int) list
Train the object detection model.
- Args:
project_id (int): ID of the project. model_id (int): ID of the model.
- Returns:
list: COCO evaluation statistics.