Skip to content

Instantly share code, notes, and snippets.

@adekunleba
Created September 11, 2018 11:32
Show Gist options
  • Select an option

  • Save adekunleba/a47070aed2ba172a4ecb4f9a3434a268 to your computer and use it in GitHub Desktop.

Select an option

Save adekunleba/a47070aed2ba172a4ecb4f9a3434a268 to your computer and use it in GitHub Desktop.
Object to load a PMML file and serve a pmml format saved model
package com.mlserver.reactivefacenet.modelserver
import java.io.InputStream
import com.mlserver.reactivefacenet.utility.ResourceUtils
import org.dmg.pmml.{FieldName, PMML}
import org.jpmml.evaluator.{Computable, FieldValue, ModelEvaluatorFactory}
import org.jpmml.model.PMMLUtil
import org.jpmml.evaluator.visitors._
import sun.misc.BASE64Decoder
import scala.collection._
import scala.collection.JavaConverters._
/***
* Load PMML File Using jppml and use it for Prediction ofor Classifiying Images
*/
object PMMLUtility extends App{
def readPMML(file: InputStream) :PMML = {
// val pmlIs = new FileInputStream(file)
PMMLUtil.unmarshal(file)
}
private val optimizers = Array(new ExpressionOptimizer, new RegressionModelOptimizer,
new GeneralRegressionModelOptimizer, new FieldOptimizer)
def optimize(pmml: PMML) = this.synchronized {
optimizers.foreach {
opt => opt.applyTo(pmml)
}
}
//ResourceUtil is a custom function that helps get Input Stream from resource model
val pmml = readPMML(ResourceUtils.getInputStream("model/DecisionTreeIris.pmml"))
optimize(pmml)
val names: Map[String, Int] = (1 until 512).map(e => ("_" + e.toString ,e)).toMap
val evaluator = ModelEvaluatorFactory.newInstance().newModelEvaluator(pmml)
evaluator.verify()
val inputField = evaluator.getInputFields
//print(inputField)
val outputField = evaluator.getOutputFields.get(2)
val tname = outputField.getName
//println(tname)
//Implement getting an array of embedding to something the PMML can interpret.
//Try and get embedding from DB
//Predict on the Embedding
val imageEmbedding:Option[Map[String, String]] =
client.hmget[String, String]("imagemichelle", "enrollmentEmbedding")
//Decode ImageEmbedding inside a SomeMap
val embedding = imageEmbedding match {
case Some(i) => i("enrollmentEmbedding")
case _ => "None"
}
val finalInnerValue = new BASE64Decoder()
.decodeBuffer(embedding)
.map(_.toDouble)
.map(_ / 1000).map(_.toFloat)
val arguments = mutable.Map[FieldName, FieldValue]()
inputField.forEach( field => {
arguments.put(field.getName, field.prepare(getValueByName(finalInnerValue, field.getName.getValue)))
})
def getValueByName(inputs: Array[Float], name:String):Double = {
names.get(name) match {
case Some(index) =>
val v = inputs(index)
v.asInstanceOf[Double]
case _ => 0
}
}
val results = evaluator.evaluate(mapAsJavaMap(arguments))
val prediction = mapAsScalaMap(results).getOrElse(tname, "No Result")
println(prediction)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment