Skip to main content

DOI CodeFactor PyPI tests

LAPiX DL - Utils for Computer Vision Deep Learning research

This package contains utilitary functions to support train and evaluation of Deep Learning models applied to images.

Three computer vision approaches are covered: Segmentation, Detection and Classification.

How to use

For Model Evaluation

This module exports the following functions for model evaluation:

from lapixdl.evaluation.evaluate import evaluate_segmentation
from lapixdl.evaluation.evaluate import evaluate_detection
from lapixdl.evaluation.evaluate import evaluate_classification

All model evaluation methods need two iterators: one for the ground truth itens and one for the predictions.

These iterators must be sorted equaly, assuring that the ground truth and the prediction of the same sample are at the same position.

Example of segmentation model evaluation using PyTorch:

from lapixdl.evaluation.evaluate import evaluate_segmentation

classes = ['background', 'object']

# Iterator for GT masks
# `dl` is a PyTorch DataLoader
def gt_mask_iterator_from_dl(dl):
  for imgs, masks in iter(dl):
    for mask in masks:
      yield mask

# Iterator for prediction masks
# `predict` a function that, given an image, predicts the mask.
def pred_mask_iterator_from_dl(dl, predict):
  for imgs, masks in iter(dl):
    for img in imgs:
      yield predict(img)

gt_masks = gt_mask_iterator_from_dl(validation_dl)
pred_masks = pred_mask_iterator_from_dl(validation_dl, prediction_function)

# Calculates and shows metrics
eval = evaluate_segmentation(gt_masks, pred_masks, classes)

# Shows confusion matrix and returns its Figure and Axes
fig, axes = eval.show_confusion_matrix()

Examples with third libraries

How to log the results of LAPiX DL evaluations in the Weights & Biases platform

About Weights & Biases.

from lapixdl.evaluation.evaluate import evaluate_segmentation
import wandb

# init wandb ...
...

eval_test = evaluate_segmentation(gt_masks, pred_masks, categories)

...

# If you want to log everything
wandb.log({'test_evaluation':  eval_test.to_dict()['By Class']})

# If you want to choose specific categories to log
selected_cats = ['A', 'B', 'C']
metrics_by_cat = {k: v for k, v in eval_test.to_dict()['By Class'].items() if k in selected_cats}
wandb.log({'test_evaluation': metrics_by_cat})
Computing using GPU with torchmetrics

About torchmetrics.

The lapixdl package calculates the confusion matrix first (on the CPU), which this will be slower than calculating using torchmetrics which uses pytorch tensors. So a trick here, to not calculate each metric separately in torchmetrics, is to calculate a confusion matrix using torchmetrics and then calculate all the metrics at once using lapixdl.

A simple example for a Segmentation case:

import torchmetrics
from lapixdl.evaluation.model import SegmentationMetrics

classes = ['background', 'object']

confMat = torchmetrics.ConfusionMatrix(
    reduce="macro", mdmc_reduce="global", num_classes=len(classes)
)

confusion_matrix = confMat(pred, target)
confusion_matrix = confusion_matrix.numpy()

metrics = SegmentationMetrics(
    classes=classes, confusion_matrix=confusion_matrix
)

For Results Visualization

This module exports the following functions for results visualization:

from lapixdl.evaluation.visualize import show_segmentations
from lapixdl.evaluation.visualize import show_classifications
from lapixdl.evaluation.visualize import show_detections

The available color maps are the ones from matplotlib.

For Data Conversion

This module exports the functions for data conversion.

from lapixdl.convert import labelbox_to_lapix
from lapixdl.convert import labelbox_to_coco

Example of conversion from Labelbox to COCO labels format:

import json

from lapixdl.formats import labelbox_to_coco

# A map categories between labelbox schematic id and category ID
map_categories = {
  '<schematic id from labelbox>': 1 # category id
}

# The categories section in the COCO format
categories_coco = [{
  'supercategory': None,
  'name': 'example_category',
  'id': 1
}]

# Convert it and create the COCO OD data
coco_dict = labelbox_to_coco(
  'labelbox_export_file.json',
  map_categories,
  categories_coco,
  target = 'object detection',
  image_shape = (1200, 1600)
)

# Saves converted json
with open('./coco.json', 'w') as out_file:
    json.dump(coco_dict, out_file)

Metadata

Release files for lapixdl 0.11.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lapixdl 0.11.0
File Size Uploaded
lapixdl-0.11.0.tar.gz 24.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lapixdl 0.11.0
File Interpreter ABI Platform
lapixdl-0.11.0-py3-none-any.whl Python 3 none any Details

Total release size: 52.3 kB

Release files / lapixdl-0.11.0.tar.gz

Download URL lapixdl-0.11.0.tar.gz
Size 24.9 kB
Tags Source
SHA-256 checksum
How to use checksums
dd515d9e87c29362011c92538c3664a0d28d5696dd51e34ec93b43a1f9b24f99
BLAKE2b-256 checksum
How to use checksums
b4dea263ac2d92463391dacecd1beac47b390fcd5881f202d5e9760d521aab1e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release files / lapixdl-0.11.0-py3-none-any.whl

Download URL lapixdl-0.11.0-py3-none-any.whl
Size 27.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b79338beb1177b918bd81de12cd0f00bcdb6a9853a31a1d6c10e7fba3e767e11
BLAKE2b-256 checksum
How to use checksums
c7ebe7104a461f67a0a748cc15af6f2f5f347ec3838a34ae7d366c19d3cd9c4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release history Release notifications | RSS feed

This release

0.11.0 This release

2 release files

0.10.0

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.12

2 release files

0.8.1

2 release files

0.7.17

2 release files

0.7.16

2 release files

0.7.15

2 release files

0.7.14

2 release files

0.7.13

2 release files

0.7.10

2 release files

0.7.9

2 release files

0.7.8

2 release files

0.7.7

2 release files

0.7.6

2 release files

0.7.5

2 release files

0.7.4

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7

2 release files

0.6.2

2 release files

0.6

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page