Skip to content

Commit d821596

Browse files
authored
v0.5.0 Modular pip install for ml backends (lobe#24)
Use setuptools extras_require for install options -- [all], [tf], [tflite], and [onnx] * customize what backends you want installed * Split out backend package requirements into setup extras for modular installation * Get the right tflite_runtime wheel url based on the machine, update readme to reflect modular install instructions. * Update error message for installing modular backends. * Add release notes for 0.5
1 parent fdcc140 commit d821596

9 files changed

Lines changed: 96 additions & 34 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#pycharm
2+
.idea
3+
14
# Byte-compiled / optimized / DLL files
25
__pycache__/
36
*.py[cod]
@@ -109,6 +112,9 @@ venv/
109112
ENV/
110113
env.bak/
111114
venv.bak/
115+
.v36
116+
.v37
117+
.v38
112118

113119
# Spyder project settings
114120
.spyderproject

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Microsoft
3+
Copyright (c) 2021 Microsoft
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
# Lobe Python API
22
Code to run exported Lobe models in Python using the TensorFlow, TensorFlow Lite, or ONNX options.
33

4+
Works with Python 3.6, 3.7, or 3.8, untested for other versions.
5+
46
## Install
7+
### Backend options with pip
8+
You can install each of the backends on an individual basis, or all together through pip like so:
9+
```shell
10+
# For all of the supported backends (TensorFlow, TensorFlow Lite, ONNX)
11+
pip install lobe[all]
12+
13+
# For TensorFlow only
14+
pip install lobe[tf]
15+
16+
# For TensorFlow Lite only
17+
pip install lobe[tflite]
18+
19+
# For ONNX only
20+
pip install lobe[onnx]
21+
```
22+
23+
Installing lobe-python without any options (`pip install lobe`) will only install the base requirements, no backends will be installed.
24+
If you try to load a model with a backend that hasn't been installed, an error message will
25+
show you the instructions to install the correct backend.
26+
527
### Linux
628
Before running these commands, make sure that you have [git](https://git-scm.com/download/linux) installed.
729

@@ -16,7 +38,8 @@ sudo apt install -y libatlas-base-dev libopenjp2-7 libtiff5 libjpeg62-dev
1638

1739
# Install lobe-python
1840
pip3 install setuptools
19-
pip3 install git+https://github.com/lobe/lobe-python --no-cache-dir
41+
# Swap out the 'all' option here for your desired backend from 'backend options with pip' above.
42+
pip3 install lobe[all]
2043
```
2144

2245
For Raspberry Pi OS (Raspian) run:
@@ -27,7 +50,7 @@ sudo ./lobe-rpi-install.sh
2750
```
2851

2952
### Mac/Windows
30-
Use a virtual environment with Python 3.7 or 3.8
53+
We recommend using a virtual environment:
3154
```shell script
3255
python3 -m venv .venv
3356

@@ -39,10 +62,10 @@ source .venv/bin/activate
3962
```
4063
Install the library
4164
```shell script
42-
# make sure pip is up to date
65+
# Make sure pip is up to date
4366
python -m pip install --upgrade pip
44-
# install
45-
pip install git+https://github.com/lobe/lobe-python
67+
# Swap out the 'all' option here for your desired backend from 'backend options with pip' above.
68+
pip install lobe[all]
4669
```
4770

4871
## Usage

RELEASE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# Release 0.5.0
2+
___
3+
## Breaking Changes
4+
* Install Lobe with your desired backend options through pip -- `pip install lobe[all]` for everything,
5+
`pip install lobe[tf]` for tensorflow, `pip install lobe[tflite]` for tensorflow lite, `pip install lobe[onnx]` for onnx.
6+
7+
18
# Release 0.4.0
29
___
310
## Bug Fixes and Other Improvements

scripts/lobe-rpi-install.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ sudo apt install -y \
1010
libtiff5 \
1111
libjpeg62-turbo
1212
sudo apt-get install -y git
13-
# Install lobe-python
13+
# Install lobe-python with TensorFlow Lite backend (ONNX backend could also work for Raspberry Pi)
1414
sudo pip3 install setuptools
15-
sudo pip3 install git+https://github.com/lobe/lobe-python --no-cache-dir
15+
sudo pip3 install lobe[tflite]

setup.py

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,87 @@
11
from setuptools import setup, find_packages
22
import sys
3+
import pathlib
34
import platform
45

6+
parent = pathlib.Path(__file__).parent
7+
# get the readme for use in our long description
8+
readme = (parent / "README.md").read_text()
59

610
python_version = platform.python_version().rsplit('.', maxsplit=1)[0]
711

812
mac_v, _, _ = platform.mac_ver()
913
if mac_v != '':
10-
mac_version = '.'.join(mac_v.split('.')[:2])
14+
mac_v_split = mac_v.split('.')
15+
mac_major_version = mac_v_split[0]
16+
mac_minor_version = mac_v_split[1]
17+
mac_version = '.'.join([mac_major_version, mac_minor_version])
1118
else:
19+
mac_major_version = None
1220
mac_version = None
1321

1422
requirements = [
15-
"pillow>=8.1.1",
16-
"requests",
17-
"numpy~=1.19.3",
18-
"tensorflow~=2.4;platform_machine!='armv7l'",
19-
"onnxruntime~=1.7.0;platform_machine!='armv7l'"
23+
"numpy~=1.19.5",
24+
"pillow~=8.3.1",
25+
"requests"
2026
]
27+
tf_req = "tensorflow~=2.5.0;platform_machine!='armv7l'"
28+
onnx_req = "onnxruntime~=1.8.1;platform_machine!='armv7l'"
29+
tflite_req = None
2130

2231
# get the right TF Lite runtime packages based on OS and python version: https://www.tensorflow.org/lite/guide/python#install_just_the_tensorflow_lite_interpreter
2332
tflite_python = None
33+
tflite_platform = None
2434
tflite_machine = None
2535

2636
# get the right python string for the version
27-
if python_version == '3.5':
28-
tflite_python = 'cp35-cp35m'
29-
elif python_version == '3.6':
37+
if python_version == '3.6':
3038
tflite_python = 'cp36-cp36m'
3139
elif python_version == '3.7':
3240
tflite_python = 'cp37-cp37m'
3341
elif python_version == '3.8':
3442
tflite_python = 'cp38-cp38'
43+
elif python_version == '3.9':
44+
tflite_python = 'cp39-cp39'
3545

36-
# get the right machine string
37-
if sys.platform == 'win32':
38-
tflite_machine = 'win_amd64'
39-
elif sys.platform == 'darwin' and mac_version == '10.15':
40-
tflite_machine = 'macosx_10_15_x86_64'
41-
elif sys.platform == 'linux':
42-
if platform.machine() == 'x86_64':
43-
tflite_machine = 'linux_x86_64'
44-
elif platform.machine() == 'armv7l':
45-
tflite_machine = 'linux_armv7l'
46+
# get the right platform and machine strings for the tflite_runtime wheel URL
47+
sys_platform = sys.platform.lower()
48+
machine = platform.machine().lower()
49+
if sys_platform == 'linux':
50+
tflite_platform = sys_platform
51+
tflite_machine = machine
52+
elif sys_platform == 'win32':
53+
tflite_platform = 'win'
54+
tflite_machine = machine
55+
elif sys_platform == 'darwin' and machine == 'x86_64':
56+
if mac_version == '10.15':
57+
tflite_platform = 'macosx_10_15'
58+
elif mac_major_version == '11':
59+
tflite_platform = 'macosx_11_0'
60+
tflite_machine = machine
4661

4762
# add it to the requirements, or print the location to find the version to install
48-
if tflite_python and tflite_machine:
49-
requirements.append(f"tflite_runtime @ https://github.com/google-coral/pycoral/releases/download/release-frogfish/tflite_runtime-2.5.0-{tflite_python}-{tflite_machine}.whl")
63+
if tflite_python and tflite_platform and tflite_machine:
64+
tflite_req = f"tflite_runtime @ https://github.com/google-coral/pycoral/releases/download/v2.0.0/tflite_runtime-2.5.0.post1-{tflite_python}-{tflite_platform}_{tflite_machine}.whl"
5065
else:
5166
print(
52-
f"Couldn't find tflite_runtime for your platform {sys.platform}, machine {platform.machine()}, and python version {python_version}, please see the install guide for the right version: https://www.tensorflow.org/lite/guide/python#install_just_the_tensorflow_lite_interpreter"
67+
f"Couldn't find tflite_runtime for your platform {sys.platform}, machine {platform.machine()}, python version {python_version}, and mac version {mac_version}. If you are trying to use TensorFlow Lite, please see the install guide for the right version: https://www.tensorflow.org/lite/guide/python#install_just_the_tensorflow_lite_interpreter"
5368
)
5469

5570
setup(
5671
name="lobe",
57-
version="0.4.0",
72+
version="0.5.0",
73+
description="Lobe Python SDK",
74+
long_description=readme,
75+
long_description_content_type="text/markdown",
76+
url="https://github.com/lobe/lobe-python",
77+
license="MIT",
5878
packages=find_packages("src"),
5979
package_dir={"": "src"},
6080
install_requires=requirements,
81+
extras_require={
82+
'all': [tf_req, onnx_req, tflite_req],
83+
'tf': [tf_req],
84+
'onnx': [onnx_req],
85+
'tflite': [tflite_req],
86+
}
6187
)

src/lobe/backends/backend_onnx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
except ImportError:
1010
# Needs better error text
1111
raise ImportError(
12-
"ERROR: This is an ONNX model and requires onnx runtime to be installed on this device. Please go to https://www.onnxruntime.ai/ for install instructions."
12+
"ERROR: This is an ONNX model and requires onnx runtime to be installed on this device. Please install lobe-python with lobe[onnx] or lobe[all] options. If that doesn't work, please go to https://www.onnxruntime.ai/ for install instructions."
1313
)
1414

1515

src/lobe/backends/backend_tf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
try:
77
import tensorflow as tf
88
except ImportError:
9-
raise ImportError("ERROR: This is a TensorFlow model and requires tensorflow to be installed on this device. Please run\n\tpip install tensorflow==2.4\n")
9+
raise ImportError("ERROR: This is a TensorFlow model and requires tensorflow to be installed on this device. Please install lobe-python with lobe[tf] or lobe[all] options. If that doesn't work, please go to https://www.tensorflow.org/install for instructions.")
1010

1111

1212
class TFModel(object):

src/lobe/backends/backend_tflite.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
except ImportError:
1010
# Needs better error text
1111
raise ImportError(
12-
"ERROR: This is a TensorFlow Lite model and requires TensorFlow Lite interpreter to be installed on this device. Please go to https://www.tensorflow.org/lite/guide/python and download the appropriate version for you device."
12+
"ERROR: This is a TensorFlow Lite model and requires TensorFlow Lite interpreter to be installed on this device. Please install lobe-python with lobe[tflite] or lobe[all] options. If that doesn't work, please go to https://www.tensorflow.org/lite/guide/python and download the appropriate version for you device."
1313
)
1414

1515

0 commit comments

Comments
 (0)