Skip to content

Commit

Permalink
lobe-python 0.3 refactor (#15)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mbeissinger authored Dec 29, 2020
1 parent aa6991f commit 143ddfe
Show file tree
Hide file tree
Showing 23 changed files with 690 additions and 365 deletions.
51 changes: 32 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# lobe-python
Code to run exported Lobe models in Python.
# Lobe Python API
Code to run exported Lobe models in Python using the TensorFlow, TensorFlow Lite, or ONNX options.

## Install
```
### Linux
```shell script
# Install Python3
sudo apt update
sudo apt install -y python3-dev python3-pip
Expand All @@ -13,15 +14,36 @@ sudo apt install -y \
libatlas-base-dev \
libopenjp2-7 \
libtiff5 \
libjpeg62-turbo
libjpeg62-dev

# Install lobe-python
pip3 install setuptools git
pip3 install git+https://github.com/lobe/lobe-python
```

## Usage
_Note for Raspbian OS (Raspberry Pi)_: Please install `libjpeg62-turbo` instead of `libjpeg62-dev`

### Mac/Windows
Use a virtual environment with Python 3.7
```shell script
python3 -m venv .venv

# Mac:
source .venv/bin/activate

# Windows:
.venv\Scripts\activate
```
Install the library
```shell script
# make sure pip is up to date
python -m pip install --upgrade pip
# install
pip install git+https://github.com/lobe/lobe-python
```

## Usage
```python
from lobe import ImageModel

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

# OPTION 2: Predict from an image url
result = model.predict_from_url('http://path/to/file.jpg')
result = model.predict_from_url('http://url/to/file.jpg')

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

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

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

## Resources

If you're running this on a Pi and having issues, and seeing this error:

```bash
Could not install packages due to an EnvironmentError: 404 Client Error: Not Found for url: https://pypi.org/simple/tflite-runtime/
```

running this may help:

```bash
pip3 install https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl
```
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).
12 changes: 12 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Release 0.3.0
___
## Breaking Changes
* Previous use of Signature should be ImageClassificationSignature. `from lobe.signature import Signature` ->
`from lobe.signature import ImageClassificationSignature`

## Bug Fixes and Other Improvements
* Update to TensorFlow 2.4 from 1.15.4
* Add ONNX runtime backend
* Use requests instead of urllib
* Make backends thread-safe
* Added constants file for signature keys to enable backwards-compatibility
6 changes: 3 additions & 3 deletions examples/basic_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
result = model.predict_from_file('path/to/file.jpg')

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

# Predict from Pillow image
from PIL import Image
Expand All @@ -18,5 +18,5 @@
print("Top prediction:", result.prediction)

# Print all classes
for label, prop in result.labels:
print(f"{label}: {prop*100:.6f}%")
for label, confidence in result.labels:
print(f"{label}: {confidence*100:.6f}%")
61 changes: 49 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,55 @@
from setuptools import setup, find_packages
import sys
import platform


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

requirements = [
"pillow",
"requests",
"numpy==1.19.3",
"tensorflow==2.4;platform_machine!='armv7l'",
"onnxruntime==1.6.0;platform_machine!='armv7l'"
]

# 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
tflite_python = None
tflite_machine = None

# get the right python string for the version
if python_version == '3.5':
tflite_python = 'cp35-cp35m'
elif python_version == '3.6':
tflite_python = 'cp36-cp36m'
elif python_version == '3.7':
tflite_python = 'cp37-cp37m'
elif python_version == '3.8':
tflite_python = 'cp38-cp38'

# get the right machine string
if sys.platform == 'win32':
tflite_machine = 'win_amd64'
elif sys.platform == 'darwin':
tflite_machine = 'macosx_10_15_x86_64'
elif sys.platform == 'linux':
if platform.machine() == 'x86_64':
tflite_machine = 'linux_x86_64'
elif platform.machine() == 'armv7l':
tflite_machine = 'linux_armv7l'

# add it to the requirements, or print the location to find the version to install
if tflite_python and tflite_machine:
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")
else:
print(
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"
)

setup(
name="lobe",
version="0.2.1",
version="0.3.0",
packages=find_packages("src"),
package_dir={"": "src"},
install_requires=[
"numpy",
"pillow",
"requests",
"tensorflow>=1.15.2,<2;platform_machine!='armv7l'",
"tflite_runtime ; platform_machine=='armv7l'"
],
dependency_links=[
"https://www.piwheels.org/simple/tensorflow",
"https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl"
]
install_requires=requirements,
)
47 changes: 0 additions & 47 deletions src/lobe/ImageModel.py

This file was deleted.

89 changes: 0 additions & 89 deletions src/lobe/Signature.py

This file was deleted.

2 changes: 2 additions & 0 deletions src/lobe/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .signature import Signature
from .model.image_model import ImageModel
24 changes: 0 additions & 24 deletions src/lobe/_model.py

This file was deleted.

Loading

0 comments on commit 143ddfe

Please sign in to comment.