Training and inference templates based on the D-FINE architecture.
Project description
Sinapsis D-FINE
Templates for training and inference with the D-FINE model
🐍 Installation • 🚀 Features • 📚 Usage example • 🌐 Webapp • 📙 Documentation • 🔍 License
The Sinapsis D-FINE module provides templates for training and inference with the D-FINE model, enabling advanced object detection tasks.
🐍 Installation
Install using your package manager of choice. We encourage the use of uv
Example with uv:
uv pip install sinapsis-dfine --extra-index-url https://pypi.sinapsis.tech
or with raw pip:
pip install sinapsis-dfine --extra-index-url https://pypi.sinapsis.tech
🚀 Features
Templates Supported
The Sinapsis D-FINE module provides two main templates for inference and training:
- DFINETraining: This module implements the training pipeline for the D-FINE model. It includes logic for initializing configuration, downloading weights and setting up the training solver.
- DFINEInference: Template designed to perform inference on a set of images using the different D-FINE architectures available.
🌍 General Attributes
Both templates share the following attributes:
config_file(str, required): Path to the model configuration file. Refer to the original repo for detailed instructions on using, creating and customizing these configuration files.pretrained_model(dict | None, optional): Specifies the size and variant of the pretrained model.device(Literal["cpu", "cuda"], required): Defines whether to run inference on CPU or CUDA.weights_path(str | None, optional): Path to a custom weights file, if provided. Defaults toNone.output_dir(str, optional): Directory where downloaded weights will be stored. Defaults to SINAPSIS_CACHE_DIR.
Specific Attributes
There are some attributes specific to the templates used:
DFINEInferencehas four additional attributes:threshold(float, required): Confidence score threshold for filtering detections.batch_inference(bool, optional): Whether to perform batch inference. Defaults toFalse.warmup_iterations(int, optional): Number of warm-up iterations to optimize model performance. Defaults to10.id2label(dict[int, str] | None, optional): Mapping of class indices to label strings. Required if using custom weights. Defaults toNone.
DFINETraininghas five additional attributes:training_mode(Literal["scratch", "tune"], required):"scratch"trains the model from scratch, while"tune"is meant to be used to fine-tune the model with provided or downloaded weights.seed(int | None, optional): Random seed for reproducibility. Defaults toNone.use_amp(bool, optional): Enables Automatic Mixed Precision (AMP) for improved performance. Defaults toFalse.print_rank(int, optional): Rank of the process for logging in distributed training. Defaults to0.print_method(Literal["builtin", "rich"], optional): Defines the logging method while training. Defaults to"builtin".
[!TIP] Use CLI command
sinapsis info --example-template-config TEMPLATE_NAMEto produce an example Agent config for the Template specified in TEMPLATE_NAME.
For example, for DFINEInference use sinapsis info --example-template-config DFINEInference to produce an example config like:
agent:
name: my_test_agent
templates:
- template_name: InputTemplate
class_name: InputTemplate
attributes: {}
- template_name: DFINEInference
class_name: DFINEInference
template_input: InputTemplate
attributes:
config_file: '/path/to/config.yml'
pretrained_model: null
device: 'cuda'
weights_path: null
output_dir: '/path/to/sinapsis/cache'
threshold: 0.5
warmup_iterations: 10
id2label: null
📚 Usage example
The following example demonstrates how to use the DFINEInference template for object detection. This setup processes a folder of images, runs inference using the D-FINE model, and saves the results, including detected bounding boxes.
Config
agent:
name: dfine_inference
description: "run inferences with D-FINE"
templates:
- template_name: InputTemplate
class_name: InputTemplate
attributes: {}
- template_name: FolderImageDatasetCV2
class_name: FolderImageDatasetCV2
template_input: InputTemplate
attributes:
data_dir: datasets/coco
- template_name: DFINEInference
class_name: DFINEInference
template_input: FolderImageDatasetCV2
attributes:
threshold: 0.5
config_file: artifacts/configs/dfine/dfine_hgnetv2_n_coco.yml
device: cuda
output_dir: ./artifacts/dfine_hgnetv2_n_coco
pretrained_model:
size: n
variant: coco
- template_name: BBoxDrawer
class_name: BBoxDrawer
template_input: DFINEInference
attributes:
overwrite: true
randomized_color: false
- template_name: ImageSaver
class_name: ImageSaver
template_input: BBoxDrawer
attributes:
root_dir: datasets
save_dir: output
extension: png
This configuration defines an agent and a sequence of templates to run object detection with D-FINE.
[!IMPORTANT] The FolderImageDatasetCV2, BBoxDrawer and ImageSaver correspond to sinapsis-data-readers, sinapsis-data-visualization and sinapsis-data-writers. If you want to use the example, please make sure you install the packages.
To run the config, use the CLI:
sinapsis run name_of_config.yml
🌐 Webapp
The webapps included in this project demonstrate the modularity of the templates, showcasing the capabilities of various object detection models for different tasks.
[!IMPORTANT] To run the app, you first need to clone this repository:
git clone git@github.com:Sinapsis-ai/sinapsis-object-detection.git
cd sinapsis-object-detection
[!NOTE] If you'd like to enable external app sharing in Gradio,
export GRADIO_SHARE_APP=True
[!NOTE] Agent configuration can be changed through the
AGENT_CONFIG_PATHenv var. You can check the available configurations in each package configs folder.
[!NOTE] When running the app with the D-FINE model, it defaults to a confidence threshold of
0.5, uses CUDA for acceleration, and employs the nano-sized D-FINE model trained on the COCO dataset. These settings can be customized by modifying thedemo.ymlfile inside theconfigsdirectory of thesinapsis-dfinepackage and restarting the webapp.
🐳 Docker
IMPORTANT This docker image depends on the sinapsis-nvidia:base image. Please refer to the official sinapsis instructions to Build with Docker.
- Build the sinapsis-object-detection image:
docker compose -f docker/compose.yaml build
- Start the app container:
docker compose -f docker/compose_apps.yaml up sinapsis-dfine-gradio -d
- Check the status:
docker logs -f sinapsis-dfine-gradio
- The logs will display the URL to access the webapp, e.g.:
Running on local URL: http://127.0.0.1:7860
NOTE: The url can be different, check the output of logs.
- To stop the app:
docker compose -f docker/compose_apps.yaml down
💻 UV
To run the webapp using the uv package manager, follow these steps:
- Create the virtual environment and sync the dependencies:
uv sync --frozen
- Install the sinapsis-object-detection package:
uv pip install sinapsis-object-detection[all] --extra-index-url https://pypi.sinapsis.tech
- Run the webapp:
uv run webapps/detection_demo.py
- The terminal will display the URL to access the webapp, e.g.:
Running on local URL: http://127.0.0.1:7860
NOTE: The url can be different, check the output of the terminal.
📙 Documentation
Documentation for this and other sinapsis packages is available on the sinapsis website
Tutorials for different projects within sinapsis are available at sinapsis tutorials page
🔍 License
This project is licensed under the AGPLv3 license, which encourages open collaboration and sharing. For more details, please refer to the LICENSE file.
For commercial use, please refer to our official Sinapsis website for information on obtaining a commercial license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sinapsis_dfine-0.1.4.tar.gz.
File metadata
- Download URL: sinapsis_dfine-0.1.4.tar.gz
- Upload date:
- Size: 27.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15d840f90c9cd56fdadfb56955d0ad0dd72c1ee52b3c6c9c23057c8a9366cc2d
|
|
| MD5 |
b50a8db7d180a13c73cbc04c685dff8b
|
|
| BLAKE2b-256 |
4c3ab6ad24db232788c68066e743f1c66505a054ac74c3b8f28814972fbcfb11
|
File details
Details for the file sinapsis_dfine-0.1.4-py3-none-any.whl.
File metadata
- Download URL: sinapsis_dfine-0.1.4-py3-none-any.whl
- Upload date:
- Size: 26.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c960e3a0ac40e36e7931cf1035822e0c3bc21af993c3976e1c8ed4c886a073a1
|
|
| MD5 |
103e2a5732418ff91a72689edc1ed5db
|
|
| BLAKE2b-256 |
41b28f8ae5b3f3dfbc397cb4a22efdfb07b0011e3453e005137965bedd8fc660
|