Skip to content

Commit 143ddfe

Browse files
authored
lobe-python 0.3 refactor (#15)
* refactor! more flexible and backwards-compatible with lobe signature files. tf_backend uses the tf 1.15 session and saved model loader rather than the contrib inference library. * update to tensorflow 2.4 * add onnx backend! * add note about libjpeg62-turbo for raspbian * onnxruntime not on arm * remove tflite install resource (taken care of in setup.py), and add link to trash classifier example
1 parent aa6991f commit 143ddfe

23 files changed

+690
-365
lines changed

README.md

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# lobe-python
2-
Code to run exported Lobe models in Python.
1+
# Lobe Python API
2+
Code to run exported Lobe models in Python using the TensorFlow, TensorFlow Lite, or ONNX options.
33

44
## Install
5-
```
5+
### Linux
6+
```shell script
67
# Install Python3
78
sudo apt update
89
sudo apt install -y python3-dev python3-pip
@@ -13,15 +14,36 @@ sudo apt install -y \
1314
libatlas-base-dev \
1415
libopenjp2-7 \
1516
libtiff5 \
16-
libjpeg62-turbo
17+
libjpeg62-dev
1718

1819
# Install lobe-python
1920
pip3 install setuptools git
2021
pip3 install git+https://github.com/lobe/lobe-python
2122
```
2223

23-
## Usage
24+
_Note for Raspbian OS (Raspberry Pi)_: Please install `libjpeg62-turbo` instead of `libjpeg62-dev`
25+
26+
### Mac/Windows
27+
Use a virtual environment with Python 3.7
28+
```shell script
29+
python3 -m venv .venv
30+
31+
# Mac:
32+
source .venv/bin/activate
33+
34+
# Windows:
35+
.venv\Scripts\activate
36+
```
37+
Install the library
38+
```shell script
39+
# make sure pip is up to date
40+
python -m pip install --upgrade pip
41+
# install
42+
pip install git+https://github.com/lobe/lobe-python
2443
```
44+
45+
## Usage
46+
```python
2547
from lobe import ImageModel
2648

2749
model = ImageModel.load('path/to/exported/model')
@@ -30,7 +52,7 @@ model = ImageModel.load('path/to/exported/model')
3052
result = model.predict_from_file('path/to/file.jpg')
3153

3254
# OPTION 2: Predict from an image url
33-
result = model.predict_from_url('http://path/to/file.jpg')
55+
result = model.predict_from_url('http://url/to/file.jpg')
3456

3557
# OPTION 3: Predict from Pillow image
3658
from PIL import Image
@@ -41,21 +63,12 @@ result = model.predict(img)
4163
print(result.prediction)
4264

4365
# Print all classes
44-
for label, prop in result.labels:
45-
print(f"{label}: {prop*100}%")
66+
for label, confidence in result.labels:
67+
print(f"{label}: {confidence*100}%")
4668

4769
```
70+
Note: model predict functions should be thread-safe. If you find bugs please file an issue.
4871

4972
## Resources
5073

51-
If you're running this on a Pi and having issues, and seeing this error:
52-
53-
```bash
54-
Could not install packages due to an EnvironmentError: 404 Client Error: Not Found for url: https://pypi.org/simple/tflite-runtime/
55-
```
56-
57-
running this may help:
58-
59-
```bash
60-
pip3 install https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl
61-
```
74+
See the [Raspberry Pi Trash Classifier](https://github.com/microsoft/TrashClassifier) example, and its [Adafruit Tutorial](https://learn.adafruit.com/lobe-trash-classifier-machine-learning).

RELEASE.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Release 0.3.0
2+
___
3+
## Breaking Changes
4+
* Previous use of Signature should be ImageClassificationSignature. `from lobe.signature import Signature` ->
5+
`from lobe.signature import ImageClassificationSignature`
6+
7+
## Bug Fixes and Other Improvements
8+
* Update to TensorFlow 2.4 from 1.15.4
9+
* Add ONNX runtime backend
10+
* Use requests instead of urllib
11+
* Make backends thread-safe
12+
* Added constants file for signature keys to enable backwards-compatibility

examples/basic_usage.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
result = model.predict_from_file('path/to/file.jpg')
88

99
# Predict from an image url
10-
result = model.predict_from_url('http://path/to/file.jpg')
10+
result = model.predict_from_url('http://url/to/file.jpg')
1111

1212
# Predict from Pillow image
1313
from PIL import Image
@@ -18,5 +18,5 @@
1818
print("Top prediction:", result.prediction)
1919

2020
# Print all classes
21-
for label, prop in result.labels:
22-
print(f"{label}: {prop*100:.6f}%")
21+
for label, confidence in result.labels:
22+
print(f"{label}: {confidence*100:.6f}%")

setup.py

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,55 @@
11
from setuptools import setup, find_packages
2+
import sys
3+
import platform
4+
5+
6+
python_version = platform.python_version().rsplit('.', maxsplit=1)[0]
7+
8+
requirements = [
9+
"pillow",
10+
"requests",
11+
"numpy==1.19.3",
12+
"tensorflow==2.4;platform_machine!='armv7l'",
13+
"onnxruntime==1.6.0;platform_machine!='armv7l'"
14+
]
15+
16+
# 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
17+
tflite_python = None
18+
tflite_machine = None
19+
20+
# get the right python string for the version
21+
if python_version == '3.5':
22+
tflite_python = 'cp35-cp35m'
23+
elif python_version == '3.6':
24+
tflite_python = 'cp36-cp36m'
25+
elif python_version == '3.7':
26+
tflite_python = 'cp37-cp37m'
27+
elif python_version == '3.8':
28+
tflite_python = 'cp38-cp38'
29+
30+
# get the right machine string
31+
if sys.platform == 'win32':
32+
tflite_machine = 'win_amd64'
33+
elif sys.platform == 'darwin':
34+
tflite_machine = 'macosx_10_15_x86_64'
35+
elif sys.platform == 'linux':
36+
if platform.machine() == 'x86_64':
37+
tflite_machine = 'linux_x86_64'
38+
elif platform.machine() == 'armv7l':
39+
tflite_machine = 'linux_armv7l'
40+
41+
# add it to the requirements, or print the location to find the version to install
42+
if tflite_python and tflite_machine:
43+
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")
44+
else:
45+
print(
46+
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"
47+
)
48+
249
setup(
350
name="lobe",
4-
version="0.2.1",
51+
version="0.3.0",
552
packages=find_packages("src"),
653
package_dir={"": "src"},
7-
install_requires=[
8-
"numpy",
9-
"pillow",
10-
"requests",
11-
"tensorflow>=1.15.2,<2;platform_machine!='armv7l'",
12-
"tflite_runtime ; platform_machine=='armv7l'"
13-
],
14-
dependency_links=[
15-
"https://www.piwheels.org/simple/tensorflow",
16-
"https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl"
17-
]
54+
install_requires=requirements,
1855
)

src/lobe/ImageModel.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/lobe/Signature.py

Lines changed: 0 additions & 89 deletions
This file was deleted.

src/lobe/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .signature import Signature
2+
from .model.image_model import ImageModel

src/lobe/_model.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)