Beam Bots Logo

bb_inference

CI License: Apache 2.0 Hex version badge Hexdocs badge REUSE status Ask DeepWiki

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:

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.

Documentation