Declares machine-learning models on a robot and runs them.

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
end

A model is one artefact, the BB.Inference.Runtime that executes it, and the BB.Inference.ModelCodec that converts to and from the artefact's own conventions. The codec also says what task the model performs.

Models are controllers

Each model is lowered into a robot-level controller of the same name, running BB.Inference.ModelController. It shares the namespace every controller, sensor, actuator, link and joint shares, and its restarts count against the topology supervisor's budget like any other controller's. It defaults to simulation: :omit.

Referring to a model

A component names a model through an ordinary option typed with model_ref/1:

use BB.Estimator,
  options_schema: [
    model: [type: BB.Inference.model_ref(:object_detection), required: true]
  ]

Compiling the robot fails if that option names a model the robot doesn't declare, or one whose codec performs a different task.

Artefacts

Artefacts are fetched at build time by the :bb_inference compiler, which places them under the application's priv. See Mix.Tasks.Compile.BbInference.

inference

Models available to this robot's components.

Nested DSLs

inference.model

model name

A model artefact, the runtime that executes it and the codec that speaks its conventions.

Lowered into a robot-level controller of the same name.

Examples

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

Arguments

NameTypeDefaultDocs
nameatomA unique name for the model.

Options

NameTypeDefaultDocs
runtimemodule | {module, keyword}The runtime that loads and executes the artefact, with any options it takes.
sourceString.tWhere to fetch the artefact from: an http or https URL, or a path relative to the project root.
codecmodule | {module, keyword}The codec that converts inputs to and outputs from this artefact, with options overriding its conventions. Validated against the codec's options_schema/0 at compile time.
archive_memberString.tWhen source is an archive (.zip, .tar, .tar.gz or .tgz), the path of the artefact inside it.
sha256anyThe artefact's SHA-256, hex encoded. For an archive, the hash of the member, not of the archive. Required when source is a URL.
labelslist(String.t)[]Class labels, by index.
simulation:omit | :start:omitWhether the model runs when the robot is started in simulation. A component naming an omitted model cannot start in simulation.

Introspection

Target: BB.Inference.Dsl.Model