All Articles

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:

  1. Build torch-android for armv7 and armv8 to get the native .so files
  2. Add the built files to your Android Studio project, setup Gradle, NDK and cmake build stuff so you can use Torch
  3. Write some native C that loads Torch, manage the Torch lifecycle, push and pull data to/from Torch for your neuron net to analyze
  4. 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:

Flow chart
Flow chart for sample project

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.

  1. The data to be run through Torch is created in Java as a float[]
  2. The float[] is bridged from JNIBridge.java to JNIBridge.c
  3. JNIBridge.c calls TorchStateMachine.cpp with the float[]
  4. TorchStateMachine.cpp converts the float[] to a Torch FloatTensor and passes it to our Torch .lua script
  5. The .lua script invokes our Torch neuron net with the input FloatTensor
  6. TorchStateMachine.cpp retreives the result from Torch and returns it to the caller
  7. The neuron nets result is passed back to our JNIBridge.java as a float[]

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 :)

Published 11 Apr 2017