A lightweight, high-performance .NET 8+ SDK for TensorFlow Lite inference with XNNPACK hardware acceleration support.
- 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
| 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 |
The precompiled TensorFlow Lite C libraries are sourced from tphakala/tflite_c, which provides optimized builds with XNNPACK support for multiple platforms.
Add the TFLiteNet package to your project:
dotnet add package TFLiteNetusing 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}");
}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()}");using var engine = TfLiteInferenceEngine
.FromModel("model.tflite")
.WithoutXnnpack()
.WithThreads(4)
.Build();
var results = engine.Infer("image.jpg");using TFLiteNet.Diagnostics;
using var engine = TfLiteInferenceEngine
.FromModel("model.tflite")
.WithXnnpack()
.WithLogger(InferenceLogger.Console) // Logs to console
.Build();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();For non-image inputs or custom preprocessing:
float[] preprocessedData = YourCustomPreprocessing();
var results = engine.Infer<float>(preprocessedData);The main entry point for inference operations.
| 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 |
| Method | Description |
|---|---|
Infer(string imagePath) |
Runs inference on an image file |
Infer<T>(ReadOnlySpan<T> data) |
Runs inference on raw data |
| Property | Description |
|---|---|
InputInfo |
Information about the input tensor |
OutputCount |
Number of output tensors |
XnnpackEnabled |
Whether XNNPACK acceleration is active |
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 |
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) |
| 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 |
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
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 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
- Use XNNPACK - Provides significant speedup on supported operations
- Match thread count to CPU cores - Use
Environment.ProcessorCount - Reuse the engine - Create once, run inference multiple times
- Use FP16 mode - Enable
ForceFp16for faster inference on supported hardware - Warmup - Run one inference before timing to allow JIT compilation
// 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");
}- .NET 8.0 or later
- SkiaSharp 2.88.8 (included as dependency)
MIT License
- TensorFlow Lite - The underlying inference engine
- tphakala/tflite_c - Precompiled TensorFlow Lite C libraries with XNNPACK support
- SkiaSharp - Cross-platform 2D graphics library for image preprocessing