bb_inference
Declare machine-learning models on a Beam Bots robot, fetch their artefacts at build time, and run them through a pluggable runtime.
defmodule MyRobot do
use BB, extensions: [BB.Inference]
inference do
model :detector do
runtime BB.Inference.OpenCV
source "https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_nano.onnx"
sha256 "c789161ed43c8269fcd4e67c67eeeb4e80c622da2eb296a20bc6007bd18a0b7d"
codec BB.Estimator.Yolo.YoloX
labels MyRobot.Coco.labels()
end
end
topology do
link :head do
sensor :camera, {BB.Camera.V4L, device: "/dev/video0"} do
estimator :objects, {BB.Estimator.ObjectDetection, model: :detector}
end
end
end
end
This package has no camera or vision dependencies. It provides:
- The
inferenceDSL section. Amodelis one artefact, the runtime that executes it, and the codec that speaks its conventions. BB.Inference.Runtime, the behaviour a runtime package implements:load,describe,run,can?, and optionallyunload.bb_inference_opencvruns ONNX through OpenCV;bb_inference_tfliteruns TensorFlow Lite.BB.Inference.ModelCodec, the behaviour for converting a task's input into a model's tensors and its output back. The codec states its task;bb_estimator_object_detectiondefines:object_detection.- A controller per model. Each
modelbecomes a robot-level controller that loads the artefact after the robot has started and serves it to components. - A Mix compiler that fetches every artefact into
privwhen the project builds, through a download cache outside the project, so models are part of every release. - A compile-time check that each component naming a model names one that exists and performs the task it needs.
Installation
mix igniter.install bb_inference
This adds the extension to your robot, the :bb_inference compiler to your
project and priv/bb_inference/ to .gitignore. By hand:
# mix.exs
def project do
[
compilers: Mix.compilers() ++ [:bb_inference],
# ...
]
end
def deps do
[
{:bb_inference, "~> 0.1.0"}
]
end
Artefacts
source is an http/https URL or a path relative to the project. A URL
must declare a sha256. An archive (.zip, .tar, .tar.gz, .tgz)
names the file inside it with archive_member, and sha256 is the hash of
that file:
model :detector do
runtime BB.Inference.TFLite
source "https://storage.googleapis.com/download.tensorflow.org/models/tflite/coco_ssd_mobilenet_v1_1.0_quant_2018_06_29.zip"
archive_member "detect.tflite"
sha256 "e4b118e5e4531945de2e659742c7c590f7536f8d0ed26d135abcfe83b4779d13"
codec {BB.Estimator.ObjectDetection.FinalDetections, mean: 127.5, std: 127.5}
end
Downloads are cached in BB_INFERENCE_CACHE_DIR, or the user cache
directory (~/.cache/bb_inference on Linux). A download that fails is a
warning, and is tried again on every build; an artefact whose hash doesn't
match is an error, and the bad download is deleted from the cache.
When a model fails to load
The model's controller reports the error through BB.Safety.report_error/3,
logs one line saying why, and stops. Its supervisor restarts it straight
away, and once the restart budget is spent the robot latches in :error.
BB.Safety.force_disarm/1 acknowledges the fault and tries again. There is
no back-off between attempts.
Simulation
A model's controller is omitted in simulation unless the model is declared
with simulation :start. A component that needs an omitted model gets a
BB.Error.State.Inference.ModelNotRunning saying so.