Skip to main content

Lobe Python API

Code to run exported Lobe models in Python using the TensorFlow, TensorFlow Lite, or ONNX options.

Works with Python 3.7, 3.8, 3.9, and 3.10 untested for other versions. [Note: 3.10 only works with the TensorFlow backend]

Install

Backend options with pip

You can install each of the backends on an individual basis, or all together through pip like so:

# For all of the supported backends (TensorFlow, TensorFlow Lite, ONNX)
pip install lobe[all]

# For TensorFlow only
pip install lobe[tf]

# For TensorFlow Lite only (note for Raspberry Pi see our setup script in scripts/lobe-rpi-install.sh)
pip install lobe[tflite]

# For ONNX only
pip install lobe[onnx]

Installing lobe-python without any options (pip install lobe) will only install the base requirements, no backends will be installed. If you try to load a model with a backend that hasn't been installed, an error message will show you the instructions to install the correct backend.

Linux

Before running these commands, make sure that you have git installed.

# Install Python3
sudo apt update
sudo apt install -y python3-dev python3-pip

# Install Pillow dependencies
sudo apt update
sudo apt install -y libatlas-base-dev libopenjp2-7 libtiff5 libjpeg62-dev

# Install lobe-python
pip3 install setuptools
# Swap out the 'all' option here for your desired backend from 'backend options with pip' above.
pip3 install lobe[all]

For Raspberry Pi OS (Raspian) run:

cd ~
wget https://raw.githubusercontent.com/lobe/lobe-python/master/scripts/lobe-rpi-install.sh
chmod 755 lobe-rpi-install.sh
sudo ./lobe-rpi-install.sh

Mac/Windows

We recommend using a virtual environment:

python3 -m venv .venv

# Mac:
source .venv/bin/activate

# Windows:
.venv\Scripts\activate

Install the library

# Make sure pip is up to date
python -m pip install --upgrade pip
# Swap out the 'all' option here for your desired backend from 'backend options with pip' above.
pip install lobe[all]

Usage

from lobe import ImageModel

model = ImageModel.load('path/to/exported/model/folder')

# OPTION 1: Predict from an image file
result = model.predict_from_file('path/to/file.jpg')

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

# OPTION 3: Predict from Pillow image
from PIL import Image
img = Image.open('path/to/file.jpg')
result = model.predict(img)

# Print top prediction
print(result.prediction)

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

# Visualize the heatmap of the prediction on the image 
# this shows where the model was looking to make its prediction.
heatmap = model.visualize(img)
heatmap.show()

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

Resources

See the Raspberry Pi Trash Classifier example, and its Adafruit Tutorial.

Metadata

Release files for lobe 0.6.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lobe 0.6.2
File Size Uploaded
lobe-0.6.2.tar.gz 17.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lobe 0.6.2
File Interpreter ABI Platform
lobe-0.6.2-py3-none-any.whl Python 3 none any Details

Total release size: 40.5 kB

Release files / lobe-0.6.2.tar.gz

Download URL lobe-0.6.2.tar.gz
Size 17.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2bbc3eeff97acdb573e128f44a9d0282b10fd9db7433837a3745922f5f41bcdf
BLAKE2b-256 checksum
How to use checksums
fc4a056eab0e6b09a5b1ddefdd0f5018ddf3c7e842830b30aa9fe8b72d5e6d6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Release files / lobe-0.6.2-py3-none-any.whl

Download URL lobe-0.6.2-py3-none-any.whl
Size 22.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8232fbd8fb376a90423ac3701722a40c25607df5f3f45c0af45acdfcd225c2f4
BLAKE2b-256 checksum
How to use checksums
24e42f05f3e2aee8c489d8685dc07e0601a3c11904c11e4ea2c68168a3b54b7b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Release history Release notifications | RSS feed

This release

0.6.2 This release

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page