yctung / AndroidLibSVM

The well-known LibSVM on Android
BSD 3-Clause "New" or "Revised" License
111 stars 43 forks source link

AndroidLibSvm

An open-source Android AAR library of the famous LibSVM

Update

Getting Started

Prerequisites

AndroidStudio (I am using 2.3.3 + Gradle 2.2.3, but it should be ok for other versions)

Install

Install is easy, you just import our AAR library into your Android project by the following steps:

Right-click your module -> new -> module -> Import .JAR/.AAR Package -> select our Release/androidlibsvm-release.aar

After this, you should add the app dependency by:

Right-click your app module -> open module setting -> clieck your app -> dependencies -> + -> module dependency -> androidlibsvm

Once you finish these two steps, you should be able to import our LibSVM class in your JAVA code by:

import umich.cse.yctung.androidlibsvm.LibSVM;

If you get any AAR import issue, please read this Android official AAR guide.

Usage

You can initialize our LibSVM class either by

LibSVM svm = new LibSVM();

or

LibSVM svm = LibSVM().getInstance();

Our implementation uses files as an input/output interface (just like how the original LibSVM works on the shell). So if you are familiar with the original LibSVM, it should be trivial to use our implementation. In the following example, you can assume you have LibSVM's heart_scale and heart_scale_predict datasets in your Android storage. Please check our testing app for further reference.

Train/Scale/Predict

You can train/scale/predict just by the following three member functions declared in the LibSVM class:

public class LibSVM {
    public void train(String options); // equivalent to "bash svm-train options"
    public void predict(String options); // equivalent to "bash svm-predict options"
    public void scale(String options, String fileOutPath); // equivalent to "bash svm-scale options > fileOutPath"
}

For example, if you are trying to train/scale/predict the heart_scale and heart_scale_predict datasets, you can do:

String systemPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
String appFolderPath = systemPath + "libsvm/"; // your datasets folder

// NOTE the space between option parameters, which is important to
// keep the options consistent to the original LibSVM format
svm.scale(appFolderPath + "heart_scale", appFolderPath + "heart_scale_scaled");
svm.train("-t 2 "/* svm kernel */ + appFolderPath + "heart_scale_scaled " + appFolderPath + "model");
svm.predict(appFolderPath + "hear_scale_predict " + appFolderPath + "model " + appFolderPath + "result");

By reading and analyze the output file (e.g., appFolderPath + "result" in this case), most applications can easily enjoy the powerful LibSVM functionality with this project.

Building from the source code

You need Android NDK to build AndroidLibSVM. I am using the NDK-r15b and the customized(deprecated) c++ compile setting as shown in the build.gradle. Your Android Studio might complain something about "(null)/ndk-build". This is because the compiler doesn't get the path of your local NDK path

Author

Copyright

Example Demo App (with a beautiful GUI)

Thank Isaac Salem for building such a useful demo app. Following shows some GUI of this demo app. Users can easily train/scale/predict their model with LibSVM through this GUI. Example Demo App

Credit

Isaac Salem

Troubleshooting