Skip to content

Commit b6f74bf

Browse files
authored
OpenVino Training Kit for Scikit-Learn and Pytorch (#989)
* OpenVino Training Kit for Scikit-Learn and Pytorch * Update Readme * update third party programs and readme * Update System Requirements and Organization * Update versions compability
1 parent 1723881 commit b6f74bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+5425
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This list gives an overview of all modules available inside the contrib reposito
1515
* [**Token Merging**](./modules/token_merging/): adaptation of [Token Merging method](https://arxiv.org/abs/2210.09461) for OpenVINO.
1616
* [**OpenVINO Code**](./modules/openvino_code): VSCode extension for AI code completion with OpenVINO.
1717
* [**Ollama-OpenVINO**](./modules/ollama_openvino): OpenVINO GenAI empowered Ollama which accelerate LLM on Intel platforms(including CPU, iGPU/dGPU, NPU).
18+
* [**ov_training_kit**](./modules/ov_training_kit): Training Kit Python library -- provides scikit-learn, PyTorch and Tensorflow wrappers for training, optimization, and deployment with OpenVINO on AI PCs.
1819

1920
## How to build OpenVINO with extra modules
2021
You can build OpenVINO, so it will include the modules from this repository. Contrib modules are under constant development and it is recommended to use them alongside the master branch or latest releases of OpenVINO.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
src/ov_training_kit.egg-info/
2+
dist/
3+
build/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# OpenVino training kit
2+
3+
Wrappers for scikit-learn and PyTorch models with OpenVINO optimization.
4+
5+
## About
6+
7+
This module provides easy-to-use wrappers for training, evaluating, and exporting classical (scikit-learn) and deep learning (PyTorch) models optimized for OpenVINO, targeting local AI PCs and edge deployment.
8+
9+
10+
## System Requirements
11+
12+
- **Operating System:** Linux (Ubuntu 18.04+), Windows 10/11, Windows Server 2019+
13+
- **CPU:** x86-64 (Intel or AMD)
14+
- **Python:** 3.8, 3.9, 3.10, 3.11
15+
- **RAM:** 8GB+ recommended
16+
- **GPU:** Optional (not required)
17+
- **Note:** Intel Extension for PyTorch (IPEX) is only supported on Linux/Windows with x86-64 CPUs. On MacOS, some features may not be available.
18+
19+
## Installation
20+
21+
```bash
22+
pip install ov-training-kit
23+
```
24+
25+
## Usage
26+
27+
For detailed usage instructions and examples, please refer to the README files inside the `src/sklearn` and `src/pytorch` folders.
28+
29+
---
30+
31+
For questions, suggestions, or contributions, feel free to open an issue or pull
32+
33+
## 🎓 Credits & License
34+
35+
Developed as part of a GSoC
36+
37+
### Authors
38+
39+
- Leonardo Heim
40+
- Shivam Basia
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from setuptools import setup, find_packages
2+
3+
with open("README.md", "r", encoding="utf-8") as f:
4+
long_description = f.read()
5+
6+
setup(
7+
name="ov_training_kit",
8+
version="0.1.9",
9+
description="Wrappers for scikit-learn and PyTorch models with OpenVINO optimization",
10+
long_description=long_description,
11+
long_description_content_type="text/markdown",
12+
url="https://github.com/openvinotoolkit/openvino_contrib",
13+
packages=find_packages(where="src"),
14+
package_dir={"": "src"},
15+
include_package_data=True,
16+
install_requires=[
17+
"scikit-learn==1.2.2",
18+
"scikit-learn-intelex==2023.1.1",
19+
"torch>=1.12.0",
20+
"openvino>=2023.0",
21+
"nncf>=2.7.0",
22+
"joblib>=1.2.0",
23+
"numpy>=1.21.0,<2.0.0",
24+
"psutil>=5.9.0",
25+
],
26+
extras_require={
27+
"ipex": [
28+
"intel_extension_for_pytorch>=2.1.0"
29+
],
30+
"dev": [
31+
"pytest>=7.0.0",
32+
"pytest-cov>=4.0.0",
33+
"flake8>=6.0.0",
34+
"black>=23.0.0",
35+
"isort>=5.10.0",
36+
],
37+
"docs": [
38+
"sphinx>=5.0.0",
39+
"sphinx_rtd_theme>=1.0.0",
40+
],
41+
},
42+
python_requires=">=3.8, <3.12",
43+
license="Apache-2.0",
44+
classifiers=[
45+
"Programming Language :: Python :: 3",
46+
"License :: OSI Approved :: Apache Software License",
47+
"Operating System :: OS Independent",
48+
"Intended Audience :: Developers",
49+
"Intended Audience :: Science/Research",
50+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
51+
],
52+
keywords="openvino scikit-learn pytorch machine-learning edge-ai",
53+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (C) 2025 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""Root package for training_kit: exposes sklearn and pytorch wrappers."""
5+
6+
from .sklearn import *
7+
from .pytorch import *
8+
9+
__all__ = ["sklearn", "pytorch"]
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# OpenVINO Kit - PyTorch Integration
2+
3+
Wrappers for PyTorch models with OpenVINO for inference, quantization, and deployment.
4+
5+
## Features
6+
7+
- PyTorch model integration
8+
- Quantization-Aware Training (QAT) and mixed-precision (AMP) support
9+
- OpenVINO IR export and compilation
10+
- Built-in metrics for classification, regression, segmentation, and detection
11+
12+
## Installation
13+
14+
```bash
15+
pip install torch torchvision openvino nncf
16+
```
17+
18+
## Basic Usage
19+
20+
```python
21+
from torchvision.models import resnet18
22+
from ov_training_kit.pytorch import BaseWrapper
23+
24+
model = resnet18(pretrained=True)
25+
wrapper = BaseWrapper(model)
26+
27+
# Train
28+
from torch import nn, optim
29+
criterion = nn.CrossEntropyLoss()
30+
optimizer = optim.Adam(model.parameters())
31+
wrapper.train(train_loader, criterion, optimizer, num_epochs=5, device="cuda")
32+
33+
# Compile for OpenVINO IR (default)
34+
wrapper.compile()
35+
36+
# Evaluate (default metric: accuracy for classification)
37+
def accuracy_metric(preds, targets):
38+
return (preds.argmax(dim=1) == targets).float().mean().item()
39+
score = wrapper.evaluate(test_loader, accuracy_metric, device="cuda")
40+
print("Accuracy:", score)
41+
```
42+
43+
## Metrics Examples
44+
45+
**Classification**
46+
```python
47+
from ov_training_kit.pytorch import ClassificationWrapper
48+
classifier = ClassificationWrapper(model)
49+
acc = classifier.evaluate_accuracy(test_loader, device="cuda")
50+
```
51+
52+
**Regression**
53+
```python
54+
from ov_training_kit.pytorch import RegressionWrapper
55+
regressor = RegressionWrapper(model)
56+
mse = regressor.evaluate_mse(test_loader, device="cuda")
57+
```
58+
59+
**Segmentation**
60+
```python
61+
from ov_training_kit.pytorch import SegmentationWrapper
62+
segmenter = SegmentationWrapper(model)
63+
iou = segmenter.evaluate_iou(test_loader, num_classes=21, device="cuda")
64+
```
65+
66+
**Detection**
67+
```python
68+
from ov_training_kit.pytorch import DetectionWrapper
69+
detector = DetectionWrapper(model)
70+
map_score = detector.evaluate_map(test_loader, metric_fn, device="cuda")
71+
```
72+
73+
## Export to ONNX
74+
75+
```python
76+
import torch
77+
from ov_training_kit.pytorch import export_model
78+
export_model(wrapper.model, input_sample=torch.randn(1, 3, 224, 224), export_path="model.onnx")
79+
```
80+
81+
## Requirements
82+
83+
- PyTorch >= 1.12
84+
- OpenVINO >= 2023.0
85+
- NNCF >= 2.7
86+
- Intel® Extension for PyTorch (IPEX) >= 2.1
87+
- Numpy
88+
89+
## 🎓 Credits & License
90+
91+
Developed as part of a GSoC
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (C) 2025 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""Pytorch models with OpenVINO optimizations"""
5+
6+
from .base_wrapper import BaseWrapper
7+
from .classification_wrapper import ClassificationWrapper
8+
from .regression_wrapper import RegressionWrapper
9+
from .segmentation_wrapper import SegmentationWrapper
10+
from .detection_wrapper import DetectionWrapper
11+
from .compiler import compile_model
12+
13+
14+
__all__ = [
15+
"BaseWrapper",
16+
"ClassificationWrapper",
17+
"RegressionWrapper",
18+
"SegmentationWrapper",
19+
"DetectionWrapper",
20+
"compile_model",
21+
]

0 commit comments

Comments
 (0)