TensorFlow Lite on Unity
TensorFlow Lite is a lightweight inference engine released as a sub package of TensorFlow. It runs on mobile and supports GPU acceleration with Metal on iOS/macOS, OpenGL on Android. A simple Unity plugin (the only single add ops network model) has been added in the experiments directory.
Unity already has developed official ML libraries called ML-Agent and Barracuda as an inference engine. It supports the ONNX format, so you can convert the model from TensorFlow or PyTorch... But I thought it would be great if I could use the pre-trained TFLite model without converting to the ONNX format. That’s why I started to test some models on TFLite examples to Unity, making some pull-requests to improve the Unity plugin which includes GPU support on Unity. Here is my repository.
How to use
Using the TFLite is simple.
- Prepare the input array
- Run `interpreter.Invoke();`
- Postprocess the output array
Here is a simple MNIST script.
GPU delegate option will dramatically improve the speed if your model has image inputs/outputs. At the moment, it does not support passing GPU-texture as models’input directly, despite Unity Barracuda supports it.
Usually, the model using Image needs pre/post-processing. Unity makes it easier and faster than native implementations.
ShowCases
SSD (single shot detector) Object Detection.
DeepLab Semantic Segmentation.
Also, the Text Classification worked on the CPU-mode.
A practical sample that uses Style Transfer as a camera filter.
Future development plans
- Support direct passing Unity texture to TensorFlow GPU delegate to reduce the exchanging textures between the GPU and CPU.
- Support custom Ops insertion to use MediaPipe examples.