Building torch-android and staying sane
Torch7 is a machine learning framework with a lot of pros; easy to learn, fast, cross platform and it has a helpful community.
The official port for Android torch-android
shares one of those pros; it’s fast when you finally got it built and integrated. The process of actually building the native packages (.so:s) for armv7/v8, deciphering the weird ANT demos and rewriting every bit of code in the examples to work in 2017 is quite tedious. Hence this article series.
To sum it up I spent near 200 hours to get torch-android
built, integrated and working for my client. My aim for this article series is to get that initial boilerplate time down to one hour for anyone lucky enough to read this before starting with torch-android
. From 200 hours to one, imagine that!
Roadmap
There are 4 distinct steps to get Torch working, each covered in a distinct article:
- Build torch-android for armv7 and armv8 to get the native .so files
- Add the built files to your Android Studio project, setup Gradle, NDK and cmake build stuff so you can use Torch
- Write some native C that loads Torch, manage the Torch lifecycle, push and pull data to/from Torch for your neuron net to analyze
- Write a JNI bridge between your native C and Java to push and receive data for your neuron net to analyze
Sample project goal
The flow chart outlines the basic components and data flow in the sample project for this guide:
The basic idea is that we’ve got some data produced in Java, like a photo, that we want to analyze with a Torch neuron net.
- The data to be run through Torch is created in Java as a
float[]
- The
float[]
is bridged fromJNIBridge.java
toJNIBridge.c
JNIBridge.c
callsTorchStateMachine.cpp
with thefloat[]
TorchStateMachine.cpp
converts thefloat[]
to a TorchFloatTensor
and passes it to our Torch .lua script- The .lua script invokes our Torch neuron net with the input
FloatTensor
TorchStateMachine.cpp
retreives the result from Torch and returns it to the caller- The neuron nets result is passed back to our
JNIBridge.java
as afloat[]
Thats probably one of the most common ways to model the data flow when analyzing a dataset in Torch. I will focus on code snippets in the comming articles and let the Github repo speak for itself, you can use it as a base for your implementation to get a quick start.
Let’s get started! Read the next article to build the native Torch .so binaries. The next article will show up here in a day, you’re early :)