Giter VIP home page Giter VIP logo

jpmml-android's Introduction

Java API for producing and scoring models in Predictive Model Markup Language (PMML).

IMPORTANT

This is a legacy codebase.

Starting from March 2014, this project has been superseded by [JPMML-Model] (https://github.com/jpmml/jpmml-model) and [JPMML-Evaluator] (https://github.com/jpmml/jpmml-evaluator) projects.

Features

Class model

  • Full support for PMML 3.0, 3.1, 3.2, 4.0 and 4.1 schemas:
    • Class hierarchy.
    • Schema version annotations.
  • Fluent API:
    • Value constructors.
  • SAX Locator information
  • [Visitor pattern] (http://en.wikipedia.org/wiki/Visitor_pattern):
    • Validation agents.
    • Optimization and transformation agents.

Evaluation engine

Installation

JPMML library JAR files (together with accompanying Java source and Javadocs JAR files) are released via [Maven Central Repository] (http://repo1.maven.org/maven2/org/jpmml/). Please join the [JPMML mailing list] (https://groups.google.com/forum/#!forum/jpmml) for release announcements.

The current version is 1.0.22 (17 February, 2014).

Class model

<!-- Class model classes -->
<dependency>
	<groupId>org.jpmml</groupId>
	<artifactId>pmml-model</artifactId>
	<version>${jpmml.version}</version>
</dependency>
<!-- Class model annotations -->
<dependency>
	<groupId>org.jpmml</groupId>
	<artifactId>pmml-schema</artifactId>
	<version>${jpmml.version}</version>
</dependency>

Evaluation engine

<dependency>
	<groupId>org.jpmml</groupId>
	<artifactId>pmml-evaluator</artifactId>
	<version>${jpmml.version}</version>
</dependency>

Usage

Class model

The class model consists of two types of classes. There is a small number of manually crafted classes that are used for structuring the class hierarchy. They are permanently stored in the Java sources directory /pmml-model/src/main/java. Additionally, there is a much greater number of automatically generated classes that represent actual PMML elements. They can be found in the generated Java sources directory /pmml-model/target/generated-sources/xjc after a successful build operation.

All class model classes descend from class org.dmg.pmml.PMMLObject. Additional class hierarchy levels, if any, represent common behaviour and/or features. For example, all model classes descend from class org.dmg.pmml.Model.

There is not much documentation accompanying class model classes. The application developer should consult with the [PMML specification] (http://www.dmg.org/v4-1/GeneralStructure.html) about individual PMML elements and attributes.

Example applications

Evaluation engine

A model evaluator class can be instantiated directly when the contents of the PMML document is known:

PMML pmml = ...;

ModelEvaluator<TreeModel> modelEvaluator = new TreeModelEvaluator(pmml);

Otherwise, a PMML manager class should be instantiated first, which will inspect the contents of the PMML document and instantiate the right model evaluator class later:

PMML pmml = ...;

PMMLManager pmmlManager = new PMMLManager(pmml);
 
ModelEvaluator<?> modelEvaluator = (ModelEvaluator<?>)pmmlManager.getModelManager(null, ModelEvaluatorFactory.getInstance());

Model evaluator classes follow functional programming principles. Model evaluator instances are cheap enough to be created and discarded as needed (ie. not worth the pooling effort).

It is advisable for application code to work against the org.jpmml.evaluator.Evaluator interface:

Evaluator evaluator = (Evaluator)modelEvaluator;

An evaluator instance can be queried for the definition of active (ie. independent), predicted (ie. primary dependent) and output (ie. secondary dependent) fields:

List<FieldName> activeFields = evaluator.getActiveFields();
List<FieldName> predictedFields = evaluator.getPredictedFields();
List<FieldName> outputFields = evaluator.getOutputFields();

The PMML scoring operation must be invoked with valid arguments. Otherwise, the behaviour of the model evaluator class is unspecified.

The preparation of field values:

Map<FieldName, FieldValue> arguments = new LinkedHashMap<FieldName, FieldValue>();

List<FieldName> activeFields = evaluator.getActiveFields();
for(FieldName activeField : activeFields){
	// The raw (ie. user-supplied) value could be any Java primitive value
	Object rawValue = ...;

	// The raw value is passed through: 1) outlier treatment, 2) missing value treatment, 3) invalid value treatment and 4) type conversion
	FieldValue activeValue = evaluator.prepare(activeField, rawValue);

	arguments.put(activeField, activeValue);
}

The scoring:

Map<FieldName, ?> results = evaluator.evaluate(arguments);

Typically, a model has exactly one predicted field, which is called the target field:

FieldName targetName = evaluator.getTargetField();
Object targetValue = results.get(targetName);

The target value is either a Java primitive value (as a wrapper object) or an instance of org.jpmml.evaluator.Computable:

if(targetValue instanceof Computable){
	Computable computable = (Computable)targetValue;

	Object primitiveValue = computable.getResult();
}

The target value may implement interfaces that descend from interface org.jpmml.evaluator.ResultFeature:

// Test for "entityId" result feature
if(targetValue instanceof HasEntityId){
	HasEntityId hasEntityId = (HasEntityId)targetValue;
	HasEntityRegistry<?> hasEntityRegistry = (HasEntityRegistry<?>)evaluator;
	BiMap<String, ? extends Entity> entities = hasEntityRegistry.getEntityRegistry();
	Entity winner = entities.get(hasEntityId.getEntityId());

	// Test for "probability" result feature
	if(targetValue instanceof HasProbability){
		HasProbability hasProbability = (HasProbability)targetValue;
		Double winnerProbability = hasProbability.getProbability(winner.getId());
	}
}
Example applications

Additional information

Please contact [[email protected]] (mailto:[email protected])

jpmml-android's People

Contributors

vruusmann 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

Watchers

 avatar  avatar  avatar  avatar  avatar

jpmml-android's Issues

java.io.InvalidClassException: org.dmg.pmml.PMML; Incompatible class (SUID)

Since Android (DVM) doesn't support JAXB processing, I've serialized my PMML object on a native Java environment (JVM). My goal was to be able only to create the Evaluator and do the evaluation in my Android app by just sending the .pmml.ser file from another environment.

When the PMML object it's about to be deserialized on the Android side, this exception ocurred.

My question is:

Is there another solution for this issue while I just want to evaluate PMML on an Android app, instead of using a JVM environment?

I've being avoiding JAXB libraries for serializing/deserializing PMML object, but seems imposible.

Also, I'm using Gradle for compiling, so Maven is not the solution for built .pmml.ser files.

Thanks in advanced.

Request support for PMML 4.3

While most of the remaining jpmml modules produce 4.3 files by default, this one only supports 4.2, making it unusable nowadays. Though older commits for those modules may be checked out, they lack essential features such as support for Pipeline objects. Please kindly consider adding support for PMML 4.3 version files.

java.lang.RuntimeException: java.io.FileNotFoundException: model.pmml.ser (Android)

I'm testing the jpmml-android example app and realize that model.pmml file was not serialized. I'd like to know how can I serialize the .pmml file inside Android or even use another method. Is there any way to do that? Is there any step-by-step?

I'm using gradle to compile the librarys inside pom.xml file from the example app:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
    compile group: 'org.jpmml', name: 'pmml-model', version: '1.3.6'
    compile group: 'org.jpmml', name: 'jpmml-evaluator', version: '1.3.6'
    compile group: 'org.codehaus.plexus', name: 'plexus-io', version: '3.0.0'

}

allprojects {
    repositories {
        jcenter()
        maven { url 'https://mvnrepository.com/artifact/org.jpmml/pmml-model' }
        maven { url 'https://mvnrepository.com/artifact/org.jpmml/jpmml-evaluator' }
        maven { url 'https://mvnrepository.com/artifact/org.codehaus.plexus/plexus-io' }
    }
}

Error:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: jpmml.org.androidm, PID: 17753
                  java.lang.RuntimeException: java.io.FileNotFoundException: model.pmml.ser
                      at jpmml.org.androidm.MainActivity$1.onClick(MainActivity.java:37)
                      at android.view.View.performClick(View.java:5246)
                      at android.widget.TextView.performClick(TextView.java:10565)
                      at android.view.View$PerformClick.run(View.java:21200)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:145)
                      at android.app.ActivityThread.main(ActivityThread.java:6946)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
                   Caused by: java.io.FileNotFoundException: model.pmml.ser
                      at android.content.res.AssetManager.openAsset(Native Method)
                      at android.content.res.AssetManager.open(AssetManager.java:323)
                      at android.content.res.AssetManager.open(AssetManager.java:297)
                      at jpmml.org.androidm.MainActivity.createEvaluator(MainActivity.java:75)
                      at jpmml.org.androidm.MainActivity.access$000(MainActivity.java:18)
                      at jpmml.org.androidm.MainActivity$1.onClick(MainActivity.java:35)
                      at android.view.View.performClick(View.java:5246) 
                      at android.widget.TextView.performClick(TextView.java:10565) 
                      at android.view.View$PerformClick.run(View.java:21200) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:145) 
                      at android.app.ActivityThread.main(ActivityThread.java:6946) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
Application terminated.

Issue of the consistency between SER producer and consumer

Hi,

Thank you for this very helpful repository. I'd like to report an issue of the consistency between the SER producer and SER consumer.

As quoted from the README file:

Extra caution is required to ensure that the SER producer (ie. the JPMML-Model Maven plugin) and the SER consumer (ie. the JPMML-Android library) are using exactly the same version of the JPMML-Model library at all times.

The issue I've found is, the serialversionUID is still different even I'm using the same version of both SER producer and consumer.

For example, when I'm using org.jpmml:pmml-maven-plugin:1.4.8 for both .pmml.ser gerneration and the Android app, I got the following error:
local class incompatible: stream classdesc serialVersionUID = 6735485, local class serialVersionUID = 6735487
I'm not sure about the exact cause of this error.

To have the same serialVersionUID, I came up with a workaround by using org.jpmml:pmml-maven-plugin:1.4.8 for generating the SER file, while using org.jpmml:pmml-maven-plugin:1.4.6 in the Android dependency.

Please let me know if you have any insight on this issue.

Thanks!

Workflow on using jpmml-android

Hi there. I'm very new to PMML and JPMML, and my goal is to use a PMML model that I've generated through Python in my Android application to do live predictions by feeding it input variables. From answers I've received to other questions, JPMML-Android seems like the most promising way to go about doing this. However, I'm not at all sure of how to implement this.

  1. Do I need to do anything with JPMML-Model or JPMML-Evaluator, or can I directly use JPMML-Android?
  2. When integrating JPMML-Android with my project, is all I need the Library JAR file that gets generated from the Maven build?
  3. How do I send my own data into the model to get an output prediction?

I apologize for these probably simple questions; I'm just super new, and on a time-crunch to implement some ML models in my Android app. Thanks in advance!

Converted PMML model to Java Ser failed JAXB missing

Hi,

Thanks for all your work on the jpmml ecosystem. I was able to learn a lot.

I have created a PMML model from a PMML pipeline with jpmml-sklearn. I was able to load it and test in my Python environment without any issue. I am now trying to ship it in a Android application.

I have followed the example in the pom.xml file in the jpmml-android-exemple directory (I have generated android jar library), but every time I try to convert the model I end up with this error :

[ERROR] Failed to execute goal org.jpmml:pmml-maven-plugin:1.4.13:ser (default) on project pmml-android-example: Execution default of goal org.jpmml:pmml-maven-plugin:1.4.13:ser failed: A required class was missing while executing org.jpmml:pmml-maven-plugin:1.4.13:ser: javax/xml/bind/JAXBContext
[ERROR] -----------------------------------------------------
[ERROR] realm =    plugin>org.jpmml:pmml-maven-plugin:1.4.13
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
[ERROR] urls[0] = file:/home/alex/.m2/repository/org/jpmml/pmml-maven-plugin/1.4.13/pmml-maven-plugin-1.4.13.jar
[ERROR] urls[1] = file:/home/alex/.m2/repository/org/jpmml/pmml-model/1.4.13/pmml-model-1.4.13.jar
[ERROR] urls[2] = file:/home/alex/.m2/repository/org/jpmml/pmml-agent/1.4.13/pmml-agent-1.4.13.jar
[ERROR] urls[3] = file:/home/alex/.m2/repository/org/codehaus/plexus/plexus-io/2.7/plexus-io-2.7.jar
[ERROR] urls[4] = file:/home/alex/.m2/repository/org/codehaus/plexus/plexus-utils/3.0.22/plexus-utils-3.0.22.jar
[ERROR] urls[5] = file:/home/alex/.m2/repository/commons-io/commons-io/2.2/commons-io-2.2.jar
[ERROR] Number of foreign imports: 1
[ERROR] import: Entry[import  from realm ClassRealm[maven.api, parent: null]]
[ERROR] 
[ERROR] -----------------------------------------------------
[ERROR] : javax.xml.bind.JAXBContext
[ERROR] -> [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] http://cwiki.apache.org/confluence/display/MAVEN/PluginContainerException

Since it is a jabx issue and I am running Java 11, I went and try to run the jabx_demo project.. But the build was successful, hence I think the issue is not from my JVM.

I have upgraded the pmml-model library to the last available version, added the minimal glassfish metro dependency and customize the pom file to only do the conversion in a specific directory. The file is available here.

Please, if you have any idea from where this error is from, I would be happy to hunt the bug down.

Regards, Alex.

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.