DeepLearning linear classifier

The DeepLearning linear classifier App is a Gluon code sample. For a full list of Gluon code samples, refer to the Gluon website.

Deep Learning and Java

This sample explains how to get started with Deep Learning on Java client apps. Deep Learning is a class of machine learning. If you want to learn more about Deep Learning in general, check the deeplearning4j overview.

Java developers can leverage Deep Learning algorithms thanks to deeplearning4j which provides Java API’s to developers. The implementations that are part of deeplearning4j leverage native linear algebraic libraries, and therefore are very powerful. Deeplearning4j is an open source project created by Skymind.

While Deep Learning computations are typically done in cloud systems, there are a number of reasons why it makes sense to use Deep Learning on (mobile) client systems, for example

  • realtime, many or fast predictions are required. There is no time or bandwith to send data to a server and wait for a result. In this case, local evaluations are needed.

  • privacy. End users don’t want to upload all their data to cloud servers, including pictures of themselves or others that didn’t give permission to share the images.

Deep Learning on mobile

The deeplearning4j API’s contain native code that is available on iOS and Android systems, thanks to the JavaCPP project.

The Gluon Mobile tools allow Java developers to write mobile apps in Java, and deploy them to iOS and Android devices with 100% code reusability. As a consequence, all development can be done using a local development setup. Once everything works on desktop, the Gluon IDE plugins make sure it works on mobile as well. Keep in mind though that mobile devices have more memory and CPU constraints than typical enterprise systems.

In order to get started with Java development for mobile, it is recommended to get started using the Getting Started guide.

Run the sample

The code for the simple linear classifier in the first sample is available at GitHub.

You can clone the Gluon samples repository and open deeplearning-linearclassifier sample in your favourite IDE. Make sure to install the Gluon IDE Plugin as this will deal with the specific structure of the project.

Even without an IDE, you can easily run the sample:

cd deeplearning-linearclassifier
./gradlew run

This will compile the code and run it on your development system. It will start a UI that looks like this:

Starting the app

This is the default start screen of an application created with the Gluon Mobile tools. The nag screen can easily be clicked away and it does not hide any functionality. With a valid license file, the nag screen won’t be shown. Information on licenses can be obtained here. Note there are free, fully functional licenses available to open source projects and students.

Once you click the nag screen away, you will see the start screen of the application:

Started the app

Clicking on the "train network model" button will train a neural network on your local system. After a few seconds, the results of this training are shown on the screen:

Trained the network

Running on mobile

A huge advantage of using Java and Gluon Mobile for Deep Learning applications is that the code you run on desktop also runs on mobile devices. The Gluon Mobile tools make sure your code can be deployed on iOS and Android systems. In order to get started and to make sure you have the correct setup, it is highly recommended you first follow the getting started guide.

In case you don’t have a system with macOS, you won’t be able to run on iOS devices. If you don’t install the required Android tools, as explained in the getting started guide you won’t be able to deploy on Android devices. However, you can keep working on your code and run it on desktop. Once you have access to a macOS system, or a system with the Android tools, you will be able to deploy your app to mobile.

Android

In order to run the sample on Android, you need a Java 8 SDK. Make sure the JAVA_HOME environment variable points to a valid Java 8 SDK. If you want to run the application on an Android device, connect the device via USB to your system and run

./gradlew androidInstall

iOS

In order to run the sample on iOS, you need a Java 9 runtime. This is because the sample is using Gluon VM which is based on OpenJDK 9.

Note that if this is the first time you are running a Java application on iOS using Gluon Mobile, it may take a long time as the core Java classes need to be compiled. These classes are cached though, so the next time it will be much faster.

You need a MacOS system with Xcode in order to run the application on an iOS device or simulator.

Running the application on the iPhone Simulator can be done as follows:

./gradle launchIphoneSimulator

If you have a real iOS device connected to your system, you can deploy the app to that device by running

./gradle launchIOSDevice

Application

The application is a simple application that shows the basic principles of deep learning. A neural network is created and trained with some provided data, available in a CSV file. This training is done in different iterations, where the score at the end of each iteration can be calculated. The score is related to the error between the network predictions and the actual values. The lower the score, the better. By optimising the weights of the network, it is hoped that the score improves. This is what is shown in the graph.