tinymind 0.3.10

Neural networks for embedded Rust: a no_std, allocation-free port of the tinymind (C++) library, bit-exact with it
Documentation
//! Port of tinymind (C++) `examples/tflite_streaming_int8`: a streaming int8
//! CNN that detects a bearing fault in a machine's vibration, trained in Keras
//! and converted to an int8 TensorFlow Lite model by TFLiteConverter, run from
//! two Rust modules that `tools/import_tflite` generated from the `.tflite` file
//! that example commits:
//!
//! - `forward` over a whole window of 32 frames (0.64 s), and
//! - `step`, which takes one new 20 ms frame and keeps, in a caller-owned
//!   state, the frames each layer still needs, so it computes one frame per
//!   layer instead of rerunning the whole window.
//!
//! The program feeds a 16-second test recording frame by frame to `step`, runs
//! `forward` over the window ending at each frame as well, and requires both to
//! give, for every window, the two int8 outputs TensorFlow Lite for
//! Microcontrollers computed for it. It writes the same files the C++ example
//! writes, byte for byte:
//!
//! - stdout: how many outputs differ from TFLite Micro's (none), false alarms
//!   and detections, the first alarm, and the memory each form needs
//! - `tflite_streaming_int8.csv`: per frame, the time, the fault's severity,
//!   the streamed fault probability (per 256) and the frame's 16 bands
//! - `tflite_streaming_int8_summary.csv`: the figures above as CSV
//!
//! It exits with status 1 if any output differs from TFLite Micro's, as the
//! C++ example does.
//!
//! Run: `cargo run --release --example tflite_streaming_int8 -- [output_dir]`
//! (default `output`). The generated modules use an arena, which needs Rust
//! 1.88.

use std::fs;
use std::io::{self, BufWriter, Write};
use std::path::PathBuf;

#[path = "common/tflite_streaming_driver.rs"]
mod tflite_streaming_driver;

fn main() -> io::Result<()> {
    let out_dir = PathBuf::from(std::env::args().nth(1).unwrap_or_else(|| "output".into()));
    fs::create_dir_all(&out_dir)?;

    let stdout = io::stdout();
    let mut stdout = BufWriter::new(stdout.lock());
    match tflite_streaming_driver::run(&out_dir, &mut stdout) {
        Ok(differing) => {
            stdout.flush()?;
            // Both forms must compute exactly what TFLite Micro computes.
            if differing != 0 {
                std::process::exit(1);
            }
            Ok(())
        }
        Err(e) => {
            eprintln!("tflite_streaming_int8: {e}");
            std::process::exit(1);
        }
    }
}