Skip to content

Commit 9b5ba3c

Browse files
authored
Computer vision example with FiftyOne and Ultralytics (#4112)
* Added a computer vision example * Update agent framework integrations * Add computer vision example files to .gitignore * With fiftyone * Update computer vision README with ZenML, Ultralytics, FiftyOne * Update Computer Vision link in table of contentsand README.md * Delete annotated_image.jpg from predictions folder * Add predictions folder to computer vision gitignore * Add new typo "fo" to the typos list * Update YOLO model materializer code file name * Fix fileio.exists check and copy source_weights_path * Refactor handling of inference results and image processing * Add COCO class mapping for YOLO format export * Add computer vision example to .gitignore * Remove unused 'required_integrations' from DockerSettings * Update AWS Secrets Manager authentication URLs * Update launch_fiftyone to accept custom port * Add exception for missing dataset.yaml file check * Add support for multiple image input formats in run.py * Add new directories to .gitignoreIgnore test_fixed_training and test_runs dirs * Remove unnecessary f-string formatting * Add FiftyOne annotator class and fix COCO class mapping * Update object detection results loading in run.py * Refactor FiftyOneAnnotator class in computer_vision * Update port number validation logic to only convert to int * Update launch_dataset function to include port argument
1 parent cfe3e2c commit 9b5ba3c

26 files changed

+3608
-4
lines changed

.gitignore

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ test_terraform/
189189
# feast registry database
190190
examples/feast_feature_store/feast_feature_repo/data/registry.db
191191

192+
# Computer vision example
193+
examples/computer_vision/*.jpg
194+
examples/computer_vision/*.jpeg
195+
examples/computer_vision/*.pt
196+
examples/computer_vision/data
197+
examples/computer_vision/runs
198+
examples/computer_vision/predictions
199+
examples/computer_vision/test_fixed_training
200+
examples/computer_vision/test_runs
201+
192202
#neptune
193203
*.neptune/
194204

@@ -200,5 +210,4 @@ zenml_tutorial/
200210
.claude/
201211

202212
.local/
203-
# PLEASE KEEP THIS LINE AT THE EOF: never include here src/zenml/zen_server/dashboard, since it is affecting release flow
204-
213+
# PLEASE KEEP THIS LINE AT THE EOF: never include here src/zenml/zen_server/dashboard, since it is affecting release flow

.typos.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ cachable = "cachable"
3838
OT = "OT"
3939
cll = "cll"
4040
ser = "ser"
41+
fo = "fo"
4142

4243
[default]
4344
locale = "en-us"

docs/book/user-guide/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ Complete end-to-end implementations that showcase ZenML in real-world scenarios.
2424
Focused code snippets and templates that address specific ML workflow challenges.\
2525
[See all examples in GitHub →](https://github.com/zenml-io/zenml-projects)
2626

27-
<table data-view="cards"><thead><tr><th></th><th></th><th data-hidden data-card-cover data-type="files"></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><strong>Quickstart</strong></td><td>Bridging Local Development and Cloud Deployment</td><td><a href=".gitbook/assets/example-01.png">example-01.png</a></td><td><a href="https://github.com/zenml-io/zenml/blob/main/examples/quickstart">https://github.com/zenml-io/zenml/blob/main/examples/quickstart</a></td></tr><tr><td><strong>End-to-End Batch Inference</strong></td><td>Supervised ML project built with the ZenML framework and its integration.</td><td><a href=".gitbook/assets/example-02.png">example-02.png</a></td><td><a href="https://github.com/zenml-io/zenml/tree/main/examples/e2e">https://github.com/zenml-io/zenml/tree/main/examples/e2e</a></td></tr><tr><td><strong>Agent Architecture Comparison</strong></td><td>Compare AI agents with LangGraph workflows, LiteLLM integration, and automatic visualizations.</td><td><a href=".gitbook/assets/example-06.png">example-06.png</a></td><td><a href="https://github.com/zenml-io/zenml/blob/main/examples/agent_comparison">https://github.com/zenml-io/zenml/blob/main/examples/agent_comparison</a></td></tr><tr><td><strong>Agent Framework Integrations</strong></td><td>Production-ready integrations for 11 popular agent frameworks including LangChain, CrewAI, AutoGen, and more.</td><td><a href=".gitbook/assets/example-06.png">example-06.png</a></td><td><a href="https://github.com/zenml-io/zenml/tree/main/examples/agent_framework_integrations">https://github.com/zenml-io/zenml/tree/main/examples/agent_framework_integrations</a></td></tr><tr><td><strong>Deploying Agents</strong></td><td>Document analysis service with pipelines, evaluation, and embedded web UI.</td><td><a href=".gitbook/assets/example-07.png">example-07.png</a></td><td><a href="https://github.com/zenml-io/zenml/blob/main/examples/deploying_agent">https://github.com/zenml-io/zenml/blob/main/examples/deploying_agent</a></td></tr><tr><td><strong>Agent Outer Loop</strong></td><td>Agent training and evaluation loop: evolve a generic agent into a specialized support system through intent classification and model training.</td><td><a href=".gitbook/assets/example-07.png">example-07.png</a></td><td><a href="https://github.com/zenml-io/zenml/blob/main/examples/agent_outer_loop">https://github.com/zenml-io/zenml/blob/main/examples/agent_outer_loop</a></td></tr><tr><td><strong>Basic NLP with BERT</strong></td><td>Build NLP models with production-ready ML pipeline framework</td><td><a href=".gitbook/assets/example-03.png">example-03.png</a></td><td><a href="https://github.com/zenml-io/zenml/tree/main/examples/e2e_nlp">https://github.com/zenml-io/zenml/tree/main/examples/e2e_nlp</a></td></tr><tr><td><strong>Computer Vision with YoloV8</strong></td><td>End-to-end computer vision pipeline with modular design</td><td><a href=".gitbook/assets/example-04.png">example-04.png</a></td><td><a href="https://github.com/zenml-io/zenml-projects/tree/main/end-to-end-computer-vision">https://github.com/zenml-io/zenml-projects/tree/main/end-to-end-computer-vision</a></td></tr><tr><td><strong>LLM Finetuning</strong></td><td>LLM fine-tuning pipeline with PEFT approach</td><td><a href=".gitbook/assets/example-05.png">example-05.png</a></td><td><a href="https://github.com/zenml-io/zenml/tree/main/examples/llm_finetuning">https://github.com/zenml-io/zenml/tree/main/examples/llm_finetuning</a></td></tr></tbody></table>
27+
<table data-view="cards"><thead><tr><th></th><th></th><th data-hidden data-card-cover data-type="files"></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><strong>Quickstart</strong></td><td>Bridging Local Development and Cloud Deployment</td><td><a href=".gitbook/assets/example-01.png">example-01.png</a></td><td><a href="https://github.com/zenml-io/zenml/blob/main/examples/quickstart">https://github.com/zenml-io/zenml/blob/main/examples/quickstart</a></td></tr><tr><td><strong>End-to-End Batch Inference</strong></td><td>Supervised ML project built with the ZenML framework and its integration.</td><td><a href=".gitbook/assets/example-02.png">example-02.png</a></td><td><a href="https://github.com/zenml-io/zenml/tree/main/examples/e2e">https://github.com/zenml-io/zenml/tree/main/examples/e2e</a></td></tr><tr><td><strong>Agent Architecture Comparison</strong></td><td>Compare AI agents with LangGraph workflows, LiteLLM integration, and automatic visualizations.</td><td><a href=".gitbook/assets/example-06.png">example-06.png</a></td><td><a href="https://github.com/zenml-io/zenml/blob/main/examples/agent_comparison">https://github.com/zenml-io/zenml/blob/main/examples/agent_comparison</a></td></tr><tr><td><strong>Agent Framework Integrations</strong></td><td>Production-ready integrations for 11 popular agent frameworks including LangChain, CrewAI, AutoGen, and more.</td><td><a href=".gitbook/assets/example-06.png">example-06.png</a></td><td><a href="https://github.com/zenml-io/zenml/tree/main/examples/agent_framework_integrations">https://github.com/zenml-io/zenml/tree/main/examples/agent_framework_integrations</a></td></tr><tr><td><strong>Deploying Agents</strong></td><td>Document analysis service with pipelines, evaluation, and embedded web UI.</td><td><a href=".gitbook/assets/example-07.png">example-07.png</a></td><td><a href="https://github.com/zenml-io/zenml/blob/main/examples/deploying_agent">https://github.com/zenml-io/zenml/blob/main/examples/deploying_agent</a></td></tr><tr><td><strong>Agent Outer Loop</strong></td><td>Agent training and evaluation loop: evolve a generic agent into a specialized support system through intent classification and model training.</td><td><a href=".gitbook/assets/example-07.png">example-07.png</a></td><td><a href="https://github.com/zenml-io/zenml/blob/main/examples/agent_outer_loop">https://github.com/zenml-io/zenml/blob/main/examples/agent_outer_loop</a></td></tr><tr><td><strong>Basic NLP with BERT</strong></td><td>Build NLP models with production-ready ML pipeline framework</td><td><a href=".gitbook/assets/example-03.png">example-03.png</a></td><td><a href="https://github.com/zenml-io/zenml/tree/main/examples/e2e_nlp">https://github.com/zenml-io/zenml/tree/main/examples/e2e_nlp</a></td></tr><tr><td><strong>Computer Vision with YoloV8</strong></td><td>End-to-end computer vision pipeline with modular design</td><td><a href=".gitbook/assets/example-04.png">example-04.png</a></td><td><a href="https://github.com/zenml-io/zenml/tree/main/examples/computer_vision">https://github.com/zenml-io/zenml/tree/main/examples/computer_vision</a></td></tr><tr><td><strong>LLM Finetuning</strong></td><td>LLM fine-tuning pipeline with PEFT approach</td><td><a href=".gitbook/assets/example-05.png">example-05.png</a></td><td><a href="https://github.com/zenml-io/zenml/tree/main/examples/llm_finetuning">https://github.com/zenml-io/zenml/tree/main/examples/llm_finetuning</a></td></tr></tbody></table>

docs/book/user-guide/toc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@
8282
* [End-to-End Batch Inference](https://github.com/zenml-io/zenml/tree/main/examples/e2e)
8383
* [Forecasting time-series prediction](https://github.com/zenml-io/zenml-projects/tree/main/floracasts)
8484
* [ML classification](https://github.com/zenml-io/zenml-projects/tree/main/oncoclear)
85-
* [Computer Vision with YoloV8](https://github.com/zenml-io/zenml-projects/tree/main/end-to-end-computer-vision)
85+
* [Computer Vision with YoloV8](https://github.com/zenml-io/zenml/tree/main/examples/computer_vision)
8686
* [Deep research agentic workflow](https://github.com/zenml-io/zenml-projects/tree/main/deep_research)
8787
* [More Projects...](https://github.com/zenml-io/zenml-projects)

examples/computer_vision/README.md

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
# Train and deploy YOLO Object Detection with ZenML, Ultralytics, and FiftyOne
2+
3+
Learn how to build a production-ready computer vision pipeline with YOLOv8, FiftyOne dataset management, and deploy it as a warm HTTP service with an interactive web interface. This example showcases the complete **FiftyOne annotation workflow loop**: export → train → predict → import → analyze → visualize.
4+
5+
## 🎯 What You'll Build
6+
7+
![Interactive Web UI](assets/app.png)
8+
9+
- **Complete FiftyOne Annotation Workflow**: Export COCO data → Train YOLO → Run inference on original FiftyOne dataset → Import predictions back → Analyze performance → Interactive dashboard visualization
10+
- **Production-Ready Training**: YOLOv8 model training with automatic artifact versioning and performance tracking via ZenML
11+
- **Real-Time Inference Service**: Deploy as warm HTTP service with sub-second latency using ZenML's deployment system
12+
- **Interactive Web UI**: Upload images or use URLs for instant object detection testing with visual results
13+
- **Dataset-Model Lineage**: Full traceability linking FiftyOne datasets to ZenML model artifacts and predictions
14+
- **Visual Performance Analysis**: Side-by-side comparison of predictions vs ground truth in FiftyOne's interactive dashboard
15+
16+
## 🏃 Quickstart
17+
18+
```bash
19+
pip install -r requirements.txt
20+
zenml init
21+
zenml login
22+
```
23+
24+
**Train with FiftyOne analysis** ([see code](pipelines/training_pipeline.py)):
25+
26+
```bash
27+
# Full workflow: training + FiftyOne analysis
28+
python run.py --train --samples 50 --epochs 3
29+
30+
# Fast training (skip FiftyOne analysis)
31+
python run.py --train --samples 50 --epochs 3 --disable-fiftyone-analysis
32+
```
33+
34+
This downloads 50 COCO validation images via FiftyOne, trains a YOLOv8 nano model for 3 epochs, runs inference on the original FiftyOne dataset, and provides interactive analysis capabilities.
35+
36+
**Deploy as a real-time service** ([see code](pipelines/inference_pipeline.py)):
37+
38+
```bash
39+
zenml pipeline deploy pipelines.inference_pipeline.object_detection_inference_pipeline
40+
```
41+
42+
Visit `http://localhost:8000` for the interactive UI ([see code](ui/index.html)).
43+
44+
**Test batch inference locally**:
45+
46+
The inference step supports multiple image input formats:
47+
48+
```bash
49+
# Using image URL
50+
python run.py --predict --image https://ultralytics.com/images/bus.jpg
51+
52+
# Using local file path
53+
python run.py --predict --image /path/to/local/image.jpg
54+
55+
# Using base64 data URI (useful for programmatic usage)
56+
python run.py --predict --image "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEA..."
57+
```
58+
59+
**Make predictions via API**:
60+
61+
```bash
62+
# Using image URL
63+
curl -X POST http://localhost:8000/invoke \
64+
-H "Content-Type: application/json" \
65+
-d '{
66+
"parameters": {
67+
"image_path": "https://ultralytics.com/images/bus.jpg",
68+
"confidence_threshold": 0.25
69+
}
70+
}'
71+
```
72+
73+
**Explore with FiftyOne Dashboard**:
74+
75+
![FiftyOne dashboard](assets/fiftyone_dashboard.png)
76+
77+
After training, the pipeline creates persistent FiftyOne datasets linked to your ZenML model artifacts. Launch the dashboard to analyze results:
78+
79+
```bash
80+
# Easy way - automatically finds datasets with predictions
81+
python launch_fiftyone.py
82+
83+
# Launch specific dataset with custom port
84+
python launch_fiftyone.py coco-2017-validation-50samples --port 8080
85+
86+
# Manual way
87+
fiftyone app launch
88+
# Then in Python: fo.load_dataset('coco-2017-validation-50samples')
89+
```
90+
91+
**Dataset-Artifact Connection**: Each FiftyOne dataset (named by parameters like `coco-2017-validation-50samples`) stores predictions from your latest ZenML run. Re-running pipelines overwrites predictions while preserving ground truth, so the dashboard always shows your current model's performance.
92+
93+
In the FiftyOne dashboard you can:
94+
- Compare predictions vs ground truth side-by-side
95+
- Filter by confidence levels and object classes
96+
- Analyze per-class performance metrics
97+
- Identify false positives and negatives
98+
- Export problematic samples for retraining
99+
100+
**Use the ZenML Deployment Playground**
101+
102+
The ZenML dashboard includes a built-in playground for deployed pipelines, allowing you to test your service directly from the UI. Navigate to your deployment in the dashboard, fill in the image URL and confidence threshold, and see real-time detection results with visualizations.
103+
104+
## 🏗️ What's Inside
105+
106+
```
107+
computer_vision/
108+
├── pipelines/
109+
│ ├── training_pipeline.py - Train YOLO + optional FiftyOne analysis
110+
│ ├── inference_pipeline.py - Real-time detection service
111+
│ └── hooks.py - Warm model loading at startup/shutdown
112+
├── steps/
113+
│ ├── data_loader.py - COCO dataset loading via FiftyOne
114+
│ ├── model_trainer.py - YOLO model training
115+
│ ├── evaluate.py - Model evaluation metrics
116+
│ ├── inference.py - Fast object detection (supports base64 uploads)
117+
│ └── fiftyone_analysis.py - Complete annotation workflow loop
118+
├── annotators/
119+
│ ├── __init__.py - Annotator package initialization
120+
│ └── fiftyone_annotator.py - FiftyOne annotator class (ZenML-style)
121+
├── materializers/
122+
│ └── ultralytics_materializer.py - Custom YOLO model serialization
123+
├── ui/
124+
│ └── index.html - Interactive web interface with image upload
125+
├── run.py - CLI for training and testing
126+
├── launch_fiftyone.py - Easy FiftyOne dashboard launcher (uses annotator)
127+
└── requirements.txt - Dependencies
128+
```
129+
130+
## 🔑 Important Notes
131+
132+
### **FiftyOne Annotator Class**
133+
134+
This example includes a `FiftyOneAnnotator` class that encapsulates all FiftyOne functionality in a clean, ZenML-style interface.
135+
136+
- 📄 [View FiftyOne annotator documentation](./annotators/README.md)
137+
- 🔧 [View annotator implementation](./annotators/fiftyone_annotator.py)
138+
- 🎯 **Key fix**: Proper COCO 80-class mapping ensures successful training (mAP@50: 0.799) vs broken training (0 mAP)
139+
140+
### **Custom Materializers**
141+
142+
This example features a [custom ZenML materializer](https://docs.zenml.io/how-to/types-and-materializers/materializers) for YOLO models that handles model weight serialization and artifact versioning, ensuring seamless model tracking across pipeline runs. ZenML automatically manages and version-controls model artifacts, making them accessible throughout your pipeline lifecycle.
143+
144+
- 📖 [ZenML Materializers Documentation](https://docs.zenml.io/concepts/artifacts/materializers)
145+
- 📄 [View YOLO model materializer code](./materializers/ultralytics_materializer.py)
146+
147+
### 🎨 Customization
148+
149+
**Use a different dataset**: To use your own dataset (in YOLO format), modify the dataset loading logic in [`run.py`](./run.py) and/or the relevant pipeline step (e.g., `load_coco_dataset` in [`steps/data_loader.py`](./steps/data_loader.py)) to point to your images, labels, and `data.yaml`.
150+
151+
**Use a larger model**: For better accuracy, use `yolov8s.pt`, `yolov8m.pt`, `yolov8l.pt`, or `yolov8x.pt`:
152+
153+
```bash
154+
python run.py --train --model yolov8m.pt --epochs 10
155+
```
156+
157+
**Run training on cloud**: Use ZenML's remote orchestrators for scalable training:
158+
159+
```bash
160+
# Kubernetes orchestrator
161+
zenml orchestrator register k8s --flavor=kubernetes
162+
zenml stack update -o k8s
163+
164+
# Then run training remotely
165+
python run.py --train --samples 500 --epochs 20 --model yolov8m.pt
166+
```
167+
168+
**Deploy inference to cloud**: Use ZenML's cloud deployers:
169+
170+
```bash
171+
# AWS App Runner
172+
zenml deployer register aws --flavor=aws-app-runner --region=us-east-1
173+
zenml stack update -d aws
174+
zenml pipeline deploy pipelines.inference_pipeline.object_detection_inference_pipeline
175+
```
176+
177+
## 📚 Learn More
178+
179+
- [Pipeline Deployments Guide](https://docs.zenml.io/how-to/deployment/deployment)
180+
- [Deployment Settings](https://docs.zenml.io/how-to/deployment/deployment_settings)
181+
- [Pipeline Hooks](https://docs.zenml.io/how-to/steps-pipelines/advanced_features#pipeline-and-step-hooks)
182+
- [Ultralytics Documentation](https://docs.ultralytics.com/)
183+
- [FiftyOne Documentation](https://docs.voxel51.com/)
184+
- [Related Example: Deploying ML Models](../deploying_ml_model/README.md)

0 commit comments

Comments
 (0)