Environment setup
This lesson is a map of the whole path. You will train in Python on a Mac. Later you will use Android Studio with an emulator or a device. You do not need a CUDA machine.
mkdir -p clothing-android-litert && cd clothing-android-litert
uv init --python 3.11
uv venv
uv run python --versionFor Android, install Android Studio, the JDK it sets up, Android SDK Platform 35 or newer, and an emulator image. You can also turn on USB debugging on a device. Lesson 04 uses the Android Studio wizard to create the Gradle project.
The shape of the project
We use the same small ClothingCNN model and the same ten Fashion-MNIST labels as the Apple Core AI / Xcode course. The last step is different. Instead of SwiftUI and Core AI, you use Kotlin, an Android APK, and Google’s LiteRT runtime.
LiteRT, TFLite, and the artifact
LiteRT is Google’s current name for its on-device runtime and tools. It grew out of TensorFlow Lite (TFLite). The model file is still a .tflite FlatBuffer, and that is the file this course ships. The current Android API is CompiledModel. The older Interpreter API still works for older apps, but this course does not use it for new code.
One contract, three boundaries
- Training:
ToTensor()turns each image into grayscale float32 pixels in[0, 1]. PyTorch then sees NCHW[1, 1, 28, 28](batch, channel, height, width) and returns ten logits (raw scores, not yet probabilities). - Conversion: a CPU example tensor locks the exported shape and type. The path is PyTorch to LiteRT Torch to
clothing.tflite. You do not need an ONNX step in the middle. - Android: resize or draw the image to 28×28 grayscale. Write the same NCHW float order into LiteRT’s input buffer. Read the ten logits. Use softmax only when you show a confidence. Then map argmax (the highest score) to the same class list.
litert-torch converter currently says it supports Linux. Train and save clothing.pt on the Mac. Then run lesson 03’s conversion steps on Linux, for example in a VM, a CI runner, or a Docker container. The clothing.tflite file you get is portable and runs in the Android APK.What you will finish
At the end, a Kotlin screen takes a bundled or selected image, prepares it the same way Python did, and shows one of the ten labels plus a confidence. You will also measure warm inference time (how fast a later run is after the first one) and find common mistakes in tensor layout, how the model is packaged, and accelerator (delegate) setup.
Key concepts
- Four stages: Mac PyTorch training, Linux
litert-torchconversion, APK asset, Android inference. - LiteRT is the current name for Google's on-device runtime. The file is still
.tflite. CompiledModelis the current Android API. This course does not useInterpreterfor new code.- Convert on Linux. Train and save
clothing.pton the Mac.
Takeaways
- There is no ONNX step. The path is PyTorch to LiteRT Torch to
clothing.tflite. - Input is NCHW
[1, 1, 28, 28]. Output is ten logits, not probabilities. - You finish with a Kotlin screen that shows a label and confidence.