DL4J on Termux Linux Emulator on Android

Hello,

I’ve recently heard that DL4J is usable on an Android device which I am interested in doing. Just to be clear with what I am trying to do: on Android there is a Linux emulator known as Termux which is basically a sandboxed virtual machine type of setup that gives an Android device a Linux command line environment. I have a version of Java 9 installed on this sandboxed “distro” that I code in using the vim feature and I’d like to do some projects where I write source code, train networks and run the networks on new data - all on my mobile device. I have standard Maven installed and running on this setup. Since I understand it is possible to train and run networks with the latest version of DL4J on Android, I assume native code of the math engine that powers DL4J has been prepared to run on arm processors found in Android tablets.

Would you please tell me if it sounds possible to write DL4J code, train and run neural networks on an Android device as described above using only maven in a command line environment without touching Eclipse or other IDEs? I am willing to step through any recommended installation process to definitively answer this question but I figured this might require a more customized approach which is why I’ve decided to ask here. Thanks for any help anyone can provide.

LM

I vaguely remember @agibsonccc using Termux himself while working some of the DL4J Android bugs out. So it should work in principle.

1 Like

Awesome! How can I view his question/post and possibly message him for installation details? Thanks.

LM

@LobsterMan Javacpp - Deeplearning4j

1 Like

Thanks for the link. Now I saw that there was mention of requiring access to a Raspberry Pi. I unfortunatly do not have one. How crucial is it that I have this? Do I need to have a rooted device in order to install DL4J on my Android and use it? I also saw mention of something called LineageOS in the link’s material and I am not familiar with this (it sounds like some sort of OS to replace Android). I was hoping the installation process would just involve certain dependencies in Maven’s pom file and maybe some less commonly used settings for Javacpp but my glance at the material seems like this is a rather involved process (regretfully I am not at an advanced level of Linux tinkering so my apologies for any novice concerns above). Thanks and I’d welcome any of your thoughts on the above.

LM

@LobsterMan the raspberry pi is just what I used to run android. LineageOS is a customized version of android that people use in rooted phones. Since most android devices are also ARM (alongside the pi) it’s just an easy way to develop/prototype android libraries.

Just using the JDK I mentioned from termux in the docs should be enough.

1 Like

Ok, that’s a relief that I don’t need the Pi or the alternative OS. Would you then please direct me to where I should begin with installing DL4J on the Android with Termux? The jdk deb files you provide in the link look like the jdk I have installed from my previous work.

LM

@LobsterMan there’s no “installing” dl4j. It’s a software library. If you mean “installation of a development environment” then that involves the JDK linked from the documentation and setting up the maven build tool to run in termux based on maven using the JDK you setup within termux.

1 Like

What I meant was (just to be sure I’ve understood things correctly):

  1. What do I write in my Maven pom file as a dependency to summon DL4J in my project (since I’ll be working purely in Termux I won’t be using/installing an IDE)?
  2. Is the only Maven line necessary to build a project using DL4J in Android Termux simply what was mentioned in the link (i.e. mvn -DargLine=“org.bytedeco.javacpp.pathsfirst=true -Djavacpp.platform=android-arm” -Dorg.bytedeco.javacpp.pathsfirst=true -Djavcpp.platform=android-arm clean test)?
  3. Is the last thing I’d need to do be simply to alter my bashrc file to contain the 4 export commands given in the link?
  4. Once items 1-3 above are done (and assuming no build errors), I should be ready to begin programming/working?

Thanks again.

LM

@LobsterMan thanks for clarifying.

  1. It depends on what you want to use. If you are just using android just setup the standard dependencies. We have prebuilt binaries for android already.
  2. Yes use those arguments when running. That is necessary due to how android handles its dependencies.
  3. #2 is something we need when running an app on android + termux due to how the native dependencies are loaded on android. It’s not really something you can put in a bash rc.
  4. Beyond that, yes that should be fine if you want to just run a hello world from the command line. Do that first before proceeding with anything else.
1 Like

Wonderful. My follow-up then:

  1. Would you please provide me with a link of where the standard dependencies are found on the DL4J website that I can use in Termux on Android (there’s a lot of material on the website and I want to be sure I use the right things)?
  2. Do the “export” commands from the link you gave me (i.e. export JAVA_HOME=/data/data/com.termux/files/usr/lib/jvm/openjdk-9) go in the bash rc file? They looked to me like modifications to bash rc I’d seen in the past which is why I ask.

Thanks.

LM

@LobsterMan again it depends on what you’re doing. You’re treating dl4j as a monolithic library that does 1 thing when it’s really several sub libraries ranging from a numpy like math library, a python execution library, and a high level deep learning library.
If you’re asking about the last one just follow this: deeplearning4j-examples/mvn-project-template at master · eclipse/deeplearning4j-examples · GitHub

This will use the cpu backend (which is what you want on android). As long as you set the android classifier (android-arm64 or android-arm in this case) when running maven you should be setup with these dependencies.

1 Like

I’ll give this a shot. Thank you again and sorry if my questions were not on point. I’ll reach out again if I run into build errors.

LM

@LobsterMan no this is great. It’ll help the documentation. It’s fairly obvious we should setup a guide for termux here.

1 Like

Ok, so I tried to build from the provided pom file but I got the following error:


T E S T S

Error: Could not find or load main class org.bytedeco.javacpp.pathsfirst=true
Caused by: java.lang.ClassNotFoundException: org.bytedeco.javacpp.pathsfirst=true

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 47.516 s
[INFO] Finished at: 2022-05-11T02:51:14Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project dl4jTesting: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test failed: The forked VM terminated without saying properly goodbye. VM crash or System.exit called ? → [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] PluginExecutionException - Apache Maven - Apache Software Foundation

My pom file can be found in the following github: startingDL4J/pom.xml at main · LobsterMan123/startingDL4J · GitHub

The maven command I used once my pom file was finished was: mvn -DargLine=“org.bytedeco.javacpp.pathsfirst=true -Djavacpp.platform=android-arm” -Dorg.bytedeco.javacpp.pathsfirst=true -Djavcpp.platform=android-arm clean test

Why is it not building and compiling?

Thanks.

LM

@LobsterMan that just tells me you’re not setting up the command line properly. Because of the way you’re passing the arguments it thinks the system property is a class. I would step back and understand how java parses arguments independent of anything to do with termux. Here’s a fairly good overview: Java System Properties - HowToDoInJava

From the looks of it you have a missing -D in there, try this:

mvn -DargLine=“-Dorg.bytedeco.javacpp.pathsfirst=true -Djavacpp.platform=android-arm" -Dorg.bytedeco.javacpp.pathsfirst=true -Djavcpp.platform=android-arm clean test
1 Like

It built/worked! I do want to say though that in the link (Javacpp - Deeplearning4j), the maven command there lacks the “-D” . It was from this link that I verbatim used the maven command.

Am I then ready to try and build a neural network and train? If so, is there a github link you can recommend with code that sets up a network (and that has a data file)?

EDIT: I realized that by replacing the “clean test” with “package” I obtained my uber jar, so I hope I’m ready to try testing a neural network at this point.

LM

@LobsterMan ah thanks sorry about that. Yeah you should be ready then as long as you have the uber jar setup.

1 Like

Wonderful. Is there a sample program I can try running, perhaps something on GitHub to start off with?

Also, I noticed that packaging my uber file results in a uber jar that’s over 1 Gigabyte in size. Are there any dependencies that can be excluded that aren’t used in doing just deep learning analysis to make this smaller? If not it’s ok, but I was taken back by the resulting jar file size (and it was just for a “Hello World” program).

Thanks.

LM

@LobsterMan we cover that here:

Specify -Djavacpp.platform=android-arm or android-arm64 to reduce the dependencies down to just the dependencies you use. I linked to our maven entry level project already. That contains a simple neural network for you to run. Find that again here:

1 Like