The smartphone in your pocket contains a dedicated Neural Processing Unit (NPU) capable of billions of AI operations per second. In 2023, on-device machine learning is accessible to every mobile developer through TensorFlow Lite (Android) and Core ML (iOS). No server costs, no latency, no data leaves the device.
Why On-Device ML?
- Privacy — user data never leaves the device
- Latency — inference in under 10ms, no network round-trip
- Offline — works without internet connectivity
- Cost — no cloud inference costs at scale
TensorFlow Lite on Android
Add the dependency, load a .tflite model from the assets folder, and run inference:
// build.gradle
implementation 'org.tensorflow:tensorflow-lite:2.12.0'
implementation 'org.tensorflow:tensorflow-lite-support:0.4.3'
// Kotlin
val model = ImageClassifier.createFromFile(context, "mobilenet_v2.tflite")
val image = TensorImage.fromBitmap(bitmap)
val results = model.classify(image)
results.forEach { classification ->
Log.d("AI", "${classification.categories[0].label}: ${classification.categories[0].score}")
}
Core ML on iOS
Drag a .mlmodel file into Xcode. It auto-generates a Swift class with a type-safe prediction API:
import CoreML
import Vision
let model = try VNCoreMLModel(for: MobileNetV2().model)
let request = VNCoreMLRequest(model: model) { request, _ in
guard let results = request.results as? [VNClassificationObservation],
let top = results.first else { return }
print("(top.identifier): (top.confidence)")
}
let handler = VNImageRequestHandler(ciImage: ciImage)
try handler.perform([request])
Model Formats and Conversion
Train your model in TensorFlow, PyTorch, or scikit-learn, then convert:
- Android:
TFLiteConverterfrom TensorFlow →.tflite - iOS:
coremltools(Python) from TensorFlow/PyTorch →.mlmodel
Pre-Trained Models (No Training Required)
- Object detection: MobileNet SSD, EfficientDet
- Image classification: MobileNetV2, EfficientNet
- Text classification: MobileBERT
- Face detection: MediaPipe Face Mesh
For most product use cases, fine-tuning a pre-trained model on your domain data (transfer learning) outperforms training from scratch and requires a fraction of the data.
On-device AI is no longer the exclusive province of AI specialists. With TensorFlow Lite and Core ML, any mobile developer can add genuinely intelligent features to their app today.