Software/Arduino Core/Neural Networks

PixelKit Detectors

Running object detectors trained in PixelKit, the exported sketch folder, the PixelKitYOLOX post-processor, the letterbox input and the bundled circles and squares sample.

intermediate3 min read

PixelKit is Ohm Lab's tool for training object detectors. It trains YOLOX-Nano and YOLOX-Tiny models and exports each as an Arduino sketch folder for the Neuro N6: the sketch, the quantised model and a labels header with the class names, the input size and the thresholds. The PostProcess library decodes these models with one factory, PostProcess_PixelKitYOLOX().

Example

The PixelKitDetector example, under File > Examples > NeuroN6Examples > 03.NeuralNetwork, has the form of an export. Its sample model finds printed circles and squares and streams labelled boxes to Neuro Studio:

cpp
#include <OV5640_Arduino.h>
#include <PostProcess.h>

#include "shapes_labels.h"   // class names, input size and thresholds

#pragma neuron6 model="pixelkit_shapes_int8.onnx" name=shapes

OV5640 camera;                         // OV5640 camera(CARRIER_TFT); on the TFT carrier
NEURON6_DECLARE_MODEL(shapes);

IPostProcessor *detector = nullptr;

// The score a box needs, shown as a slider in Neuro Studio.
static float confidence = SHAPES_CONF_THRESHOLD;

void setup() {
  VisionConfig cfg;
  cfg.camera    = CAMERA_VGA;
  cfg.fps       = FPS_45;
  cfg.nn.width  = SHAPES_INPUT_W;
  cfg.nn.height = SHAPES_INPUT_H;
  cfg.nn.aspect = ASPECT_LETTERBOX;    // the whole picture on grey, as PixelKit trains
  cfg.transport = TRANSPORT_USB;

  n6_tune_float("confidence", &confidence, 0.01f, 0.99f);
  detector = PostProcess_PixelKitYOLOX(confidence, SHAPES_NMS_THRESHOLD,
                                       SHAPES_CLASSES, SHAPES_NUM_CLASSES);
  Vision.begin(&NN_Instance_shapes, detector, cfg);
}

void loop() {
  // Calling the factory again changes the settings of its one instance.
  static float applied = SHAPES_CONF_THRESHOLD;
  if (confidence != applied) {
    applied = confidence;
    PostProcess_PixelKitYOLOX(confidence, SHAPES_NMS_THRESHOLD,
                              SHAPES_CLASSES, SHAPES_NUM_CLASSES);
  }

  Vision.run();

  const od_pp_outBuffer_t *boxes = nullptr;
  const int n = PostProcess_ZooResults(detector, &boxes);
  for (int i = 0; i < n; i++) {
    // SHAPES_CLASSES[boxes[i].class_index] found, with score boxes[i].conf
  }
}

Labels header

The export's header names every constant the sketch uses. The sample's shapes_labels.h holds:

ConstantValue in the sampleMeaning
SHAPES_NUM_CLASSES2Number of classes
SHAPES_CLASSES[]"circle", "square"Class names, in the model's order
SHAPES_INPUT_W, SHAPES_INPUT_H256, 256Model input size, for cfg.nn
SHAPES_CONF_THRESHOLD0.65Confidence threshold calibrated by PixelKit
SHAPES_NMS_THRESHOLD0.45Overlap above which the weaker of two boxes of the same class is dropped

The prefix follows the export's name.

Post-processor

PostProcess_PixelKitYOLOX(conf, iou, labels, n) publishes boxes and labels to metadata slot 0, draws them on the panel when one is in use, and keeps the results for PostProcess_ZooResults(), which returns the count and points at od_pp_outBuffer_t boxes normalised to the picture the network saw, valid until the next frame. PostProcess_PixelKitYOLOX_Ex(conf, iou, labels, n, slot) publishes to another slot, for use with a second model.

The factory returns one shared instance. Calling it again with a new confidence changes that instance, which is how the example's Neuro Studio slider works: n6_tune_float() registers the variable, Neuro Studio's Tuning panel writes it, and loop() passes the new value to the factory.

The model must have a square input that is a multiple of 32 and one output with a row per cell of the stride 8, 16 and 32 grids, int8 or float. Class names whose count differs from the model's class count are refused, because they belong to another export. PostProcess_ZooStatus(pp) returns 0 while the model's outputs decode and a negative code otherwise.

Letterbox input

PixelKit trains on letterboxed images, so its sketches set cfg.nn.aspect = ASPECT_LETTERBOX. The model receives the whole frame with its aspect ratio kept, scaled to fit the input and centred on grey (114) bars: a 640x480 frame into the 256x256 input becomes a 256x192 picture between two 32-row bars. Results map back to camera coordinates and boxes are clipped to the frame. See Aspect Modes and Coordinates.

Examples

ExampleLocationOutput
PixelKitDetectorNeuroN6Examples > 03.NeuralNetworkNeuro Studio over USB, VGA at 45 fps, with the confidence slider
PixelKitDetectorDisplayNeuroN6Examples > 03.NeuralNetworkThe TFT carrier's panel, OV5640 camera(CARRIER_TFT), WVGA at 60 fps with TRANSPORT_LCD
GlobalShutterPixelKitVD66GYNeuro Studio over USB from the VD66GY at VGA, with the confidence slider

Test sheet

pixelkit_shapes_test_sheet.pdf, in the PixelKitDetector example's folder, is a test sheet for the sample model. It is printed at 100% on A4, and a page is held so that it fills the camera's height. Pages 1 to 3 hold 4, 6 and 8 shapes; page 4 holds none.

Licence

pixelkit_shapes_int8.onnx derives from Megvii's YOLOX-Nano and is under Apache-2.0 (models/pixelkit_shapes_int8.onnx.license). The examples and the test sheet are in the public domain.