Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Reference kit update to AI Kit 2024.0.0 compatible components (#1) (#2)
Browse files Browse the repository at this point in the history
* Reference kit update to AI Kit 2024.0.0 compatible components (#1)

* Remove stock environment.

* Remove env/intel directory.

* Add intel_env.yml file

* Add .gitignore

* Delete stock-pipeline.png

* Move VOC.yaml to config directory.

* Remove training.patch

* Add new training.patch

* Add output dir to .gitignore

* Move deploy.yaml to config directory.

* Add src/yolov5 to .gitignore

* Remove stock part of code from run_inference.py.

* Remove inference.png

* Delete ThroughputOpenVinoQuantizationBenchmark.png

* Update intel_env.yml.

-Add ultralytics, gitpython

* Remove data.txt

* Update README.md

* Update run_inference.py

-Changed int8inc and save_image args to store_true

* Add log info to openvino_quantization.py

* Fix logger in openvino_quantization.py

-Logger wasn't showing information at INFO level.

* Add log notifications to openvino_quantization.py

-Add 'This will take time, please wait!' message before create pipeline and execute pipeline.

* Add sections to README.md.

- Add validation tags.
- Add Expected Output.
- Add Disclaimer.
- Modify style.

* Fix README.md typos and style.

* Update .gitignore

* Update README.md

- Correct typos.
- Correct styles.
- Add rm instruction.

* Update README.md

- Correct typos.
- Correct styles.

* Update README.md

- Remove text referencing a non-existing codeblock.

* Update README.md

- Give uniform style to Notes.

* Update README.md

- Change WORKSPACE dir name.
- Change . for $WORKSPACE in git command.

* Remove traffic-detection submodule

* Update intel_env.yml

- Remove ipython from environment.

* Fix typos in README.md

* Fix dataset dir structure in README.md

* Added curl install instruction to README.md
  • Loading branch information
aagalleg authored Jan 11, 2024
1 parent 4d985fd commit b388bad
Show file tree
Hide file tree
Showing 14 changed files with 689 additions and 498 deletions.
141 changes: 141 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

data/
output/
src/yolov5/
881 changes: 465 additions & 416 deletions README.md

Large diffs are not rendered by default.

Binary file not shown.
Binary file removed assets/inference.png
Binary file not shown.
Binary file removed assets/stock-pipeline.png
Binary file not shown.
2 changes: 1 addition & 1 deletion src/data/VOC.yaml → config/data/VOC.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# This yaml has been adapted from https://github.com/ultralytics/yolov5/blob/master/data/VOC.yaml

# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: ../../data/VOC
path: ../data/VOC
train: # train images (relative to 'path') 16551 images
- images/train2012
- images/train2007
Expand Down
File renamed without changes.
41 changes: 23 additions & 18 deletions src/training.patch → config/training.patch
Original file line number Diff line number Diff line change
@@ -1,46 +1,51 @@
diff --git a/train.py b/train.py
index 177e081..9b42fe9 100644
index 4c3bec3..a82fbfb 100644
--- a/train.py
+++ b/train.py
@@ -239,6 +239,16 @@ def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictio
@@ -24,6 +24,7 @@ import sys
import time
from copy import deepcopy
from datetime import datetime, timedelta
+import intel_extension_for_pytorch as ipex
from pathlib import Path

try:
@@ -249,6 +250,14 @@ def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictio
model.class_weights = labels_to_class_weights(dataset.labels, nc).to(device) * nc # attach class weights
model.names = names

+ # Ipex Optimizing Model
+ if opt.intel or opt.bf16:
+ import intel_extension_for_pytorch as ipex
+ model.model.to(memory_format=torch.channels_last)
+ if opt.bf16:
+ model, optimizer = ipex.optimize(model, optimizer=optimizer, dtype=torch.bfloat16)
+ else:
+ model, optimizer = ipex.optimize(model, optimizer=optimizer)
+ print("IPEX optimization enabled")
+ model.model.to(memory_format=torch.channels_last)
+ if opt.bf16:
+ model, optimizer = ipex.optimize(model, optimizer=optimizer, dtype=torch.bfloat16)
+ else:
+ model, optimizer = ipex.optimize(model, optimizer=optimizer)
+ print("IPEX optimization enabled")
+
# Start training
t0 = time.time()
nb = len(train_loader) # number of batches
@@ -281,7 +291,11 @@ def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictio
@@ -291,7 +300,10 @@ def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictio
for i, (imgs, targets, paths, _) in pbar: # batch -------------------------------------------------------------
callbacks.run('on_train_batch_start')
ni = i + nb * epoch # number integrated batches (since train start)
- imgs = imgs.to(device, non_blocking=True).float() / 255 # uint8 to float32, 0-255 to 0.0-1.0
+
+ if opt.bf16:
+ imgs = imgs.to(device, non_blocking=True).bfloat16() / 255 # uint8 to float16, 0-255 to 0.0-1.0
+ else:
+ imgs = imgs.to(device, non_blocking=True).float() / 255 # uint8 to float32, 0-255 to 0.0-1.0

# Warmup
if ni <= nw:
@@ -472,6 +486,11 @@ def parse_opt(known=False):
@@ -481,6 +493,8 @@ def parse_opt(known=False):
parser.add_argument('--upload_dataset', nargs='?', const=True, default=False, help='Upload data, "val" option')
parser.add_argument('--bbox_interval', type=int, default=-1, help='Set bounding-box image logging interval')
parser.add_argument('--artifact_alias', type=str, default='latest', help='Version of dataset artifact to use')

+ # Intel Ipex Optimization
+ parser.add_argument('--intel', '-i', type=int, default=0, help='To Enable Intel Optimization set to 1, default 0')
+ # Intel Ipex Optimization BF16
+ parser.add_argument('--bf16', action='store_true', help='Enable only on Intel® Fourth Gen Xeon, BF16')
+
return parser.parse_known_args()[0] if known else parser.parse_args()

return parser.parse_known_args()[0] if known else parser.parse_args()

--
2.34.1

1 change: 0 additions & 1 deletion data/data.txt

This file was deleted.

19 changes: 0 additions & 19 deletions env/intel/intel-pt.yml

This file was deleted.

22 changes: 22 additions & 0 deletions env/intel_env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: traffic_detection_intel
channels:
- intel
- conda-forge
dependencies:
- python=3.9
- opencv=4.8.0
- intel-aikit-pytorch=2024.0
- neural-compressor=2.3.1
- torchvision=0.15.2
- seaborn=0.13.0
- tqdm=4.66.1
- tensorboard=2.15.1
- pillow=10.0.1
- ultralytics=8.0.227
- gitpython=3.1.40
- pip=23.3.1
- pip:
- opencv-python-headless==4.8.1.78
- thop==0.1.1-2209072238
- openvino-dev[pytorch,onnx]==2023.2.0
- onnx==1.14.1
16 changes: 0 additions & 16 deletions env/stock/stock-pt.yml

This file was deleted.

33 changes: 29 additions & 4 deletions src/openvino/openvino_quantization.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os
from pathlib import Path
import argparse
import logging
import torch
import torch.nn.functional as nnf
from addict import Dict
Expand Down Expand Up @@ -185,6 +186,18 @@ def __len__(self):
data_path = FLAGS.data_yaml
batch_size = FLAGS.batchsize

logger = logging.getLogger('quantization')
formatter = logging.Formatter(
"[%(asctime)s][%(levelname)s] %(message)s", "%Y-%m-%d %H:%M:%S"
)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
stream_handler.setLevel(logging.INFO)
logger.addHandler(stream_handler)

logger.setLevel(logging.INFO)
logger.propagate = False

model_config = Dict(
{
"model_name": "TrafficOD_Onnx_Model",
Expand All @@ -207,12 +220,14 @@ def __len__(self):
]

# Step 1: Load the model
logger.info('Step 1/9: Load the model')

model = load_model(model_config=model_config)

original_model = copy.deepcopy(model)

# Step 2: Initialize the data loader
logger.info('Step 2/9: Initialize the data loader')

data = check_yaml(data_path)
data = check_dataset(data) # check
Expand All @@ -234,37 +249,47 @@ def __len__(self):

data_loader = Dataset(dataloader_l)

print("This will take time, Please wait")
# Step 3 (Optional. Required for AccuracyAwareQuantization): Initialize the metric
logger.info('Step 3/9: Initialize the metric')

metric = Accuracy(names)

# Step 4: Initialize the engine for metric calculation and statistics collection
logger.info('Step 4/9: Initialize the engine for metric calculation and statistics collection')
engine = IEEngine(config=engine_config, data_loader=data_loader, metric=metric)

# Step 5: Create a pipeline of compression algorithms
logger.info('This will take time, please wait!')
logger.info('Step 5/9: Create a pipeline of compression algorithms')
pipeline = create_pipeline(algo_config=algorithms, engine=engine)

original_metric_results = pipeline.evaluate(original_model)

# Step 6: Execute the pipeline
logger.info('This will take time, please wait!')
logger.info('Step 6/9: Execute the pipeline')
compressed_model = pipeline.run(model=model)

# Step 7 (Optional): Compress model weights quantized precision
# in order to reduce the size of final .bin file
logger.info('Step 7/9: Compress model weights quantized precision in" \
" order to reduce the size of final .bin file')
compress_model_weights(model=compressed_model)

# Step 8: Save the compressed model and get the path to the model
logger.info('Step 8/9: Save the compressed model and get the path to the model')
compressed_model_paths = save_model(
model=compressed_model, save_path=os.path.join(os.path.curdir, out_path)
)
compressed_model_xml = Path(compressed_model_paths[0]["model"])
print(f"The quantized model is stored in {compressed_model_xml}")
logger.info(f"The quantized model is stored in {compressed_model_xml}")

# Step 9 (Optional): Evaluate the original and compressed model. Print the results
logger.info('Step 9 (Optional): Evaluate the original and compressed model. Print the results')

quantized_metric_results = pipeline.evaluate(compressed_model)
if quantized_metric_results:
print(f"MeanAP of the quantized model: {next(iter(quantized_metric_results.values())):.5f}")
logger.info(f"MeanAP of the quantized model: {next(iter(quantized_metric_results.values())):.5f}")

if original_metric_results:
print(f"MeanAP of the original model: {next(iter(original_metric_results.values())):.5f}")
logger.info(f"MeanAP of the original model: {next(iter(original_metric_results.values())):.5f}")
Loading

0 comments on commit b388bad

Please sign in to comment.