Skip to content

Commit 40aa834

Browse files
committed
Use test pip image for OpenVINO
1 parent ffc25a9 commit 40aa834

File tree

5 files changed

+60
-29
lines changed

5 files changed

+60
-29
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,18 @@ $ source venv3/bin/activate
7878
```
7979

8080
To optimize inference on CPU with Intel OpenVINO:
81-
* Download and install OpenVINO from https://software.seek.intel.com/openvino-toolkit
81+
82+
1. Convert model to OpenVINO IR (optional if you already have `.xml` and `.bin` files)
83+
```bash
84+
(venv3) $ git clone -b releases/2021/1 --depth 1 https://github.com/openvinotoolkit/openvino
85+
(venv3) $ export PYTHONPATH=openvino/model-optimizer/:$PYTHONPATH
86+
(venv3) $ pip install -r openvino/model-optimizer/requirements_onnx.txt
87+
(venv3) $ python bonito/openvino/convert.py dna_r9.4.1
88+
```
89+
90+
2. Run evaluation
8291
```bash
83-
(venv3) $ pip install -r /opt/intel/openvino/deployment_tools/model_optimizer/requirements_onnx.txt
84-
(venv3) $ source /opt/intel/openvino/bin/setupvars.sh
92+
(venv3) $ export LD_LIBRARY_PATH=$(pwd)/venv3/lib:$LD_LIBRARY_PATH
8593
(venv3) $ bonito evaluate dna_r9.4.1 --use_openvino --device=cpu
8694
```
8795

bonito/openvino/convert.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os
2+
import torch
3+
import argparse
4+
from bonito.util import load_model
5+
6+
parser = argparse.ArgumentParser(description='Use this script to prepare OpenVINO IR for trained model')
7+
parser.add_argument("model_directory")
8+
parser.add_argument("--half", action="store_true", default=False)
9+
parser.add_argument("--weights", default="0", type=str)
10+
args = parser.parse_args()
11+
12+
__dir__ = os.path.dirname(os.path.dirname(__file__))
13+
__models__ = os.path.join(__dir__, "models")
14+
dirname = args.model_directory
15+
if not os.path.isdir(dirname) and os.path.isdir(os.path.join(__models__, dirname)):
16+
dirname = os.path.join(__models__, dirname)
17+
18+
model = load_model(dirname, 'cpu', weights=int(args.weights), half=args.half)
19+
20+
21+
# Convert to ONNX
22+
onnx_path = os.path.join(dirname, model.config['model']) + '.onnx'
23+
inp = torch.randn(1, 1, 1000) # Just dummy input shape. We will reshape model later
24+
model.eval()
25+
with torch.no_grad():
26+
torch.onnx.export(model, inp, onnx_path,
27+
input_names=['input'],
28+
output_names=['output'],
29+
operator_export_type=torch.onnx.OperatorExportTypes.ONNX_ATEN_FALLBACK)
30+
31+
32+
# Convert to IR
33+
import mo_onnx
34+
import subprocess
35+
model_name = model.config['model'] + ('_fp16' if args.half else '')
36+
subprocess.call([mo_onnx.__file__,
37+
'--input_model', onnx_path,
38+
'--extension', os.path.join(os.path.dirname(__file__), 'mo_extension'),
39+
'--keep_shape_ops',
40+
'--model_name', model_name,
41+
'--data_type', 'FP16' if args.half else 'FP32',
42+
'--input_shape=[1,1,1,1000]',
43+
'--output_dir', dirname])

bonito/openvino/model.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,8 @@ def __init__(self, model, half, dirname):
1313
self.model = model
1414
self.alphabet = model.alphabet
1515

16-
onnx_path = os.path.join(dirname, model.config['model']) + '.onnx'
1716
model_name = model.config['model'] + ('_fp16' if half else '')
1817
xml_path, bin_path = [os.path.join(dirname, model_name) + ext for ext in ['.xml', '.bin']]
19-
if not os.path.exists(xml_path) or not os.path.exists(bin_path):
20-
21-
# Convert to ONNX
22-
if not os.path.exists(onnx_path):
23-
inp = torch.randn(1, 1, 1000) # Just dummy input shape. We will reshape model later
24-
model.eval()
25-
with torch.no_grad():
26-
torch.onnx.export(model, inp, onnx_path,
27-
input_names=['input'],
28-
output_names=['output'],
29-
operator_export_type=torch.onnx.OperatorExportTypes.ONNX_ATEN_FALLBACK)
30-
31-
# Convert to IR
32-
import mo_onnx
33-
import subprocess
34-
subprocess.call([mo_onnx.__file__,
35-
'--input_model', onnx_path,
36-
'--extension', os.path.join(os.path.dirname(__file__), 'mo_extension'),
37-
'--keep_shape_ops',
38-
'--model_name', model_name,
39-
'--data_type', 'FP16' if half else 'FP32',
40-
'--input_shape=[1,1,1,1000]',
41-
'--output_dir', dirname])
42-
4318
self.ie = IECore()
4419
self.net = self.ie.read_network(xml_path, bin_path)
4520
self.exec_net = None

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ ont-fast5-api==3.0.1
99
fast-ctc-decode==0.2.5
1010
bonito-cuda-runtime==0.0.2a2
1111
#pyclaragenomics-cuda-10-0==0.4.2
12+
--extra-index-url https://test.pypi.org/simple/
13+
openvino

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
raise RuntimeError('Unable to find version string in "{}/__init__.py".'.format(__pkg_name__))
1414

1515
with open('requirements.txt') as f:
16-
requirements = f.read().splitlines()
16+
requirements = []
17+
for req in f.read().splitlines():
18+
if not req.startswith('--extra-index-url'):
19+
requirements.append(req)
1720

1821
with open('README.md', encoding='utf-8') as f:
1922
long_description = f.read()

0 commit comments

Comments
 (0)