Tensorflow modeling sample that can be converted to PMML

964 views
Skip to first unread message

Ron Gonzalez

unread,
Nov 28, 2017, 3:06:51 PM11/28/17
to Java PMML API
Hi,
I can't seem to find any links that points me to any code that creates a tensorflow model that can be converted into PMML using the JPMML converter.
Does anyone have that link and can share with me?

Thanks,
Ron

Villu Ruusmann

unread,
Nov 29, 2017, 12:54:15 AM11/29/17
to Java PMML API
Hi Ron,

> I can't seem to find any links that points me to any
> code that creates a tensorflow model that can be
> converted into PMML using the JPMML converter.
>

All my converter projects (starting from JPMML-SkLearn, and ending
with JPMML-TensorFlow) follow a convention, where integration testing
resources reside under the /src/test/resources directory. Better yet,
there's usually a R or Python script available that takes care of
generating individual resource files.

In case of the JPMML-TensorFlow project, see here:
https://github.com/jpmml/jpmml-tensorflow/tree/master/src/test/resources

If you delete the /src/test/resources/savedmodel directory, and then
run the /src/test/resources/main.py script, then this deleted
directory should be automatically re-created and re-populated with
latest SavedModel subdirectories. I'm using this approach to detect
SavedModel data format changes between TensorFlow version upgrades.

For an individual test case (binary classification), see here:
https://github.com/jpmml/jpmml-tensorflow/blob/master/src/test/resources/main.py#L70-L82


VR

Ron Gonzalez

unread,
Dec 5, 2017, 1:52:12 AM12/5/17
to Java PMML API
I'm trying to compare TF Serving<->JPMML performance by taking the generated savedmodel/DNNClassifierIris model and running it in Tensorflow Serving. I see that in order to generate the savedmodel directory, it needs to do the following:

def _serving_input_fn(cont_feature_columns, cat_feature_columns):
cont_feature_placeholders = {column : tf.placeholder(dtype = tf.float64, shape = [None, 1], name = column) for column in cont_feature_columns}
cat_feature_placeholders = {column : tf.placeholder(dtype = tf.string, shape = [None, 1], name = column) for column in cat_feature_columns}
feature_placeholders = dict(list(cont_feature_placeholders.items()) + list(cat_feature_placeholders.items()))
features = {column : tensor for column, tensor in feature_placeholders.items()}
label = None
return InputFnOps(features, label, feature_placeholders)

where the shape of the tensor is [None, 1].

When I try to use the Tensorflow Serving Java API, I can't seem to get it to work with this savedmodel/DNNClassifierIris.

This is kind of out of scope of JPMML but I need to be able to show the difference in scores (if any) between Tensorflow Serving and JPMML along with any performance differences.

Any help sorting out how to build the TensorProto input for this model would be great...

Thanks,
Ron

Villu Ruusmann

unread,
Dec 5, 2017, 9:19:56 PM12/5/17
to Java PMML API
Hi Ron,

>
> This is kind of out of scope of JPMML but I need to be able
> to show the difference in scores (if any) between Tensorflow
> Serving and JPMML along with any performance differences.
>
> Any help sorting out how to build the TensorProto input for
> this model would be great...
>

Attached is a sample Java project that executes the DNNClassifierIris
model using the org.tensorflow:tensorflow Java library. This is not
quite the "official" TensorFlow Serving, but if you're into making an
apples-to-apples comparison between (J)TensorFlow and (J)PMML, then
this is how I would do it (executing in local computer/CPU mode, one
data record at a time).

Installation and usage:
1) Unzip the jpmml-tensorflow-serving.zip file, and step into this directory.
2) Build using Apache Maven: $ mvn clean install
3) Execute the application class org.jpmml.tensorflow.Main; it takes a
single command-line argument, which is the path to the
DNNClassifierIris model: $ java -jar
target/converter-executable-1.0-SNAPSHOT.jar
/path/to/DNNClassificationIris/

If everything goes fine, then you should see the vector of
probabilities printed to System.out.

You should upgrade the org.tensroflow:tensorflow dependency from 1.1.0
to whatever is installed in your computer, and generalize application
code lines 25 -- 46 to iterate over all data records in the Iris.csv
file (at the moment it's scoring only the first data record).

As for feeding input to TensorFlow models, and fetching their output,
then you must lookup operator names from the corresponding ProtoBuf
file. In the current case, input names are the same as CSV column
names, but the output name varies according to model type - for
multi-class DNN models it defaults to
"dnn/multi_class_head/predictions/probabilities".


VR
jpmml-tensorflow-serving.zip
Reply all
Reply to author
Forward
0 new messages