Code Monkey home page Code Monkey logo

androidlibsvm's Introduction

AndroidLibSvm

An open-source Android AAR library of the famous LibSVM

Update

  • 2017/09/26: Build to AAR, so users don't need to worry about JNI/NDK
  • 2017/09/27: Thank Isaac Salem for sharing his app using AndroidLibSVM Example Demo App

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

  • Solution: add NDK path to your local.properties file like this: ndk.dir=/Users/MyPath/Android/ndk

Author

  • This Android library is currently maintained by Yu-Chih Tung
  • The core LibSVM is developed by Chih-Jen Lin and his team

Copyright

  • Feel free to use this wrapper at whatever manner you want :)
  • But remember to include the LibSVM's copyright file in your project

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

  • If LibSVM can't read/write the expected output, check if your app has the permission of READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE
  • If LibSVM crashes, ensure your input options follow the original LibSVM format
  • We don't support input as an array (or other data types) now. Please save your dataset to train/predict as files and ensure they are accessible in the Android.

androidlibsvm's People

Contributors

aravindgear4 avatar yctung avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

androidlibsvm's Issues

Storing the SVM model

I am creating a mobile application with SVM.
Since SVM learning takes time, is there way to store the learnt model once and then re-use it in the later when the application is opened again.

Does this library outputs the model to a file ? If so my issue will be almost resolved

Import scikit-learn model file

Hi, I'm interesting with this library and i want to use svm in smartphones.
Can i import svm model file like (.pkl) or (.pmml)?
If not, then how to restore model file via this library?

Android minsdk version support

Hello,
we integrate your android libsvm wrapper in a project where we support android sdk from 19 to upper versions.
Building with your wrapper, we figured out the minsdk version declared is 22.
We tested a modified version of the AndroidManifest.xml provided in your aar release to downgrade minsdk to version 19, without meeting any problem.
Can you tell us if there were any reason to not support android sdk from 19 to 26 versions ?

If there is no particular issue, could you update the
AndroidLibSVM/AndroidLibSVM/androidlibsvm/build.gradle file to allow support from the 19 sdk version in minsdk ?

Best regards and thanks for the good work done around libSVM

How to use it?

I'm sorry but I don't really get it, can you tell me how to use it like what I need to do first with the dataset and then etc to get the output... Thank you

NDK: unknown text in model file: [1]

Hi, thanks for this library and the documentation.

I've been trying to load a pre-trained svm-model into my Android application and predict the liveliness of an photo. But I'm having trouble at loading the model.

This is the error I'm getting :

D/LibSVM: LibSVM init
D/LibSVM-NDK: NDK: jniSvmPredict cmd = /storage/emulated/0/libsvm/predict.png /storage/emulated/0/libsvm/MODEL_FILE /storage/emulated/0/libsvm/output 
D/LibSVM-NDK: NDK: unknown text in model file: [1]
    NDK: ERROR: fscanf failed to read model
    NDK: can't open model file /storage/emulated/0/libsvm/MODEL_FILE
Application terminated.

The part where I do the initialization and prediction.
dataPredictPath contains the image I want to test,
modelPath is the MODEL_FILE location
outputPath : ? I'm not sure what this location is, but I have it nonetheless.

String dataPredictPath = appFolderPath+"predict.png ";
String modelPath = appFolderPath+"MODEL_FILE ";
String outputPath = appFolderPath+"output ";
 svm = new LibSVM();
 svm.predict(dataPredictPath + modelPath + outputPath);

My MODEL_FILE goes like this : 1 1:0.094391245 2:0.14827052 3:0.088652398 4:0.28704535 5:0.05657944 6:0.25682053 7:0.28452203 8:0.20615581 9:0.042102296 10:0.25249034 11:0.2981357 12:0.11878572 13:0.047860448 14:0.29053951 15:0.19971732 16:0.11536864 17:0.07729373 18:0.32926438 19:0.20244 20:0.071077895 21:0.20135299 22:0.27581759 23:0.13864515 24:0.065852396 25:0.18637253 26:0.22433392

I had this in the start of the model but I deleted it, seeing its not the same with libsvm format.

svm_type c_svc
kernel_type linear
nr_class 2
total_sv 449
rho 1.33522
label 1 -1
probA -6.83453
probB -0.235144
nr_sv 212 237
SV

Could you please help me out ? Thanks

Add example case in readme file

Hello, thanks for this library

But I don't know how to use this
I'm very excited with this project and create predict and train .cpp use this library

Because use ndk, I must compile/build ndk in jni folder and not working, can you use this example for the case in readme file

Thanks

Error

When i imported the project in android while gradle buildling i am getting the following error:
The following packages are not available:
Package id platforms;android-24
Please, suggest me how to run this project.

encounter an error when invoke LibSVM.getInstance()

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hzf.recognition, PID: 18386
java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "__aeabi_memcpy" referenced by "/data/app/com.example.recognition-1/lib/arm/libjnilibsvm.so"...
at java.lang.Runtime.loadLibrary(Runtime.java:372)
at java.lang.System.loadLibrary(System.java:1076)
at umich.cse.yctung.androidlibsvm.LibSVM.(LibSVM.java:14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.