Skip to content

mapo80/TFLiteNet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TFLiteNet

A lightweight, high-performance .NET 8+ SDK for TensorFlow Lite inference with XNNPACK hardware acceleration support.

Features

  • Simple, fluent API - Easy-to-use builder pattern for configuring inference
  • XNNPACK acceleration - Optional hardware-accelerated inference with configurable threading
  • Cross-platform - Supports macOS ARM64, Linux x64/ARM64, and Windows x64
  • Image preprocessing - Built-in image preprocessing with SkiaSharp (resize, normalization, color format)
  • Multiple data types - Supports Float32, Int8, and UInt8 models
  • Diagnostic logging - Optional logging for debugging and performance analysis
  • Code generation - Generate strongly-typed C# wrapper classes from TFLite models

Supported Platforms

Platform Architecture Native Library
macOS ARM64 (Apple Silicon) libtensorflowlite_c.dylib
Linux x64 (AMD64) libtensorflowlite_c.so
Linux ARM64 libtensorflowlite_c.so
Windows x64 (AMD64) tensorflowlite_c.dll

Native Libraries

The precompiled TensorFlow Lite C libraries are sourced from tphakala/tflite_c, which provides optimized builds with XNNPACK support for multiple platforms.

Installation

Add the TFLiteNet package to your project:

dotnet add package TFLiteNet

Quick Start

Basic Inference

using TFLiteNet;

// One-liner inference
var results = TfLiteInferenceEngine
    .FromModel("model.tflite")
    .RunInference("image.jpg");

// Get predictions
var topPredictions = results.TopK(5);
foreach (var (index, score) in topPredictions)
{
    Console.WriteLine($"Class {index}: {score:P2}");
}

With XNNPACK Acceleration

using TFLiteNet;
using TFLiteNet.Core;

using var engine = TfLiteInferenceEngine
    .FromModel("model.tflite")
    .WithXnnpack(new XnnpackOptions
    {
        NumThreads = Environment.ProcessorCount,
        ForceFp16 = true  // Enable FP16 for faster inference
    })
    .WithThreads(Environment.ProcessorCount)
    .Build();

var results = engine.Infer("image.jpg");
Console.WriteLine($"Predicted class: {results.PredictedClass()}");

Without XNNPACK (Default CPU Backend)

using var engine = TfLiteInferenceEngine
    .FromModel("model.tflite")
    .WithoutXnnpack()
    .WithThreads(4)
    .Build();

var results = engine.Infer("image.jpg");

With Diagnostic Logging

using TFLiteNet.Diagnostics;

using var engine = TfLiteInferenceEngine
    .FromModel("model.tflite")
    .WithXnnpack()
    .WithLogger(InferenceLogger.Console)  // Logs to console
    .Build();

Custom Image Preprocessing

using TFLiteNet.ImageProcessing;

using var engine = TfLiteInferenceEngine
    .FromModel("model.tflite")
    .WithColorFormat(ColorFormat.BGR)  // Use BGR instead of RGB
    .WithNormalization(Normalization.ImageNet)  // ImageNet mean/std normalization
    .Build();

Raw Data Inference

For non-image inputs or custom preprocessing:

float[] preprocessedData = YourCustomPreprocessing();
var results = engine.Infer<float>(preprocessedData);

API Reference

TfLiteInferenceEngine

The main entry point for inference operations.

Builder Methods

Method Description
FromModel(string path) Creates a builder from a .tflite model file
WithThreads(int count) Sets the number of inference threads
WithXnnpack(XnnpackOptions?) Enables XNNPACK hardware acceleration
WithoutXnnpack() Disables XNNPACK (uses default CPU backend)
WithColorFormat(ColorFormat) Sets RGB or BGR color ordering
WithNormalization(INormalization) Sets normalization strategy for float32 models
WithLogger(InferenceLogger) Enables diagnostic logging
Build() Creates the configured engine
RunInference(string imagePath) Builds and immediately runs inference

Engine Methods

Method Description
Infer(string imagePath) Runs inference on an image file
Infer<T>(ReadOnlySpan<T> data) Runs inference on raw data

Properties

Property Description
InputInfo Information about the input tensor
OutputCount Number of output tensors
XnnpackEnabled Whether XNNPACK acceleration is active

XnnpackOptions

Configuration for XNNPACK delegate.

Property Default Description
NumThreads 0 Number of threads (0 = system default, -1 = use interpreter threads)
ForceFp16 false Force FP16 inference for faster computation

InferenceResult

Contains output tensors from inference.

Method/Property Description
Primary Gets the first output tensor
this[int index] Gets output tensor by index
All Gets all output tensors
AsFloat() Gets primary output as float array
TopK(int k) Gets top K predictions (index, score)
PredictedClass() Gets the predicted class index (argmax)

Normalization Strategies

Strategy Description
Normalization.None No normalization (raw 0-255 values)
Normalization.ZeroToOne Scales to [0, 1]
Normalization.NegativeOneToOne Scales to [-1, 1]
Normalization.ImageNet ImageNet mean/std normalization
Normalization.Custom(mean, std) Custom per-channel normalization

Code Generation

TFLiteNet includes a code generator that creates strongly-typed C# wrapper classes from TFLite models. This provides compile-time safety and IntelliSense support for your models.

See the full guide: Code Generation Guide

Quick Example

using TFLiteNet.CodeGen;

// Generate C# source code from a model
var code = TfLiteModelGenerator.GenerateClass(
    modelPath: "models/yolov5.tflite",
    className: "Yolov5Detector",
    @namespace: "MyApp.Models");

File.WriteAllText("Yolov5Detector.generated.cs", code);

Using the Generated Class

using MyApp.Models;

// Create instance with XNNPACK acceleration
using var detector = new Yolov5Detector("models/yolov5.tflite", useXnnpack: true, numThreads: 4);

// Check model metadata (available at compile time!)
Console.WriteLine($"Model: {Yolov5Detector.ModelFileName}");
Console.WriteLine($"Input size: {Yolov5DetectorInput.ImageWidth}x{Yolov5DetectorInput.ImageHeight}");
Console.WriteLine($"Output shape: [{string.Join(", ", Yolov5DetectorOutput.IdentityShape)}]");

// Run inference
var output = detector.Infer("image.jpg");
Console.WriteLine($"Output values: {output.Identity.Length}");

For detailed documentation including advanced usage, examples with YOLOv5 and document detection, and build-time generation, see the Code Generation Guide

Performance Tips

  1. Use XNNPACK - Provides significant speedup on supported operations
  2. Match thread count to CPU cores - Use Environment.ProcessorCount
  3. Reuse the engine - Create once, run inference multiple times
  4. Use FP16 mode - Enable ForceFp16 for faster inference on supported hardware
  5. Warmup - Run one inference before timing to allow JIT compilation

Example: Benchmark Comparison

// Without XNNPACK
using (var engine = TfLiteInferenceEngine
    .FromModel(modelPath)
    .WithoutXnnpack()
    .Build())
{
    engine.Infer(imagePath); // Warmup

    var sw = Stopwatch.StartNew();
    for (int i = 0; i < 100; i++)
        engine.Infer(imagePath);
    sw.Stop();

    Console.WriteLine($"Without XNNPACK: {sw.ElapsedMilliseconds / 100.0:F1}ms avg");
}

// With XNNPACK
using (var engine = TfLiteInferenceEngine
    .FromModel(modelPath)
    .WithXnnpack(new XnnpackOptions { NumThreads = Environment.ProcessorCount })
    .Build())
{
    engine.Infer(imagePath); // Warmup

    var sw = Stopwatch.StartNew();
    for (int i = 0; i < 100; i++)
        engine.Infer(imagePath);
    sw.Stop();

    Console.WriteLine($"With XNNPACK: {sw.ElapsedMilliseconds / 100.0:F1}ms avg");
}

Requirements

  • .NET 8.0 or later
  • SkiaSharp 2.88.8 (included as dependency)

License

MIT License

Acknowledgments

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages