-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add serializers/desirializers for models
- Loading branch information
blublinsky
committed
Jun 3, 2017
1 parent
7f6875e
commit 28078f7
Showing
6 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/scala/com/lightbend/modelServer/model/PMMLModelSerializerKryo.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.lightbend.modelServer.model | ||
|
||
/** | ||
* Created by boris on 6/2/17. | ||
*/ | ||
import com.esotericsoftware.kryo.io.{Input, Output} | ||
import com.esotericsoftware.kryo.{Kryo, Serializer} | ||
|
||
|
||
class PMMLModelSerializerKryo extends Serializer[PMMLModel]{ | ||
|
||
super.setAcceptsNull(false) | ||
super.setImmutable(true) | ||
|
||
/** Reads bytes and returns a new object of the specified concrete type. | ||
* <p> | ||
* Before Kryo can be used to read child objects, {@link Kryo#reference(Object)} must be called with the parent object to | ||
* ensure it can be referenced by the child objects. Any serializer that uses {@link Kryo} to read a child object may need to | ||
* be reentrant. | ||
* <p> | ||
* This method should not be called directly, instead this serializer can be passed to {@link Kryo} read methods that accept a | ||
* serialier. | ||
* | ||
* @return May be null if { @link #getAcceptsNull()} is true. */ | ||
|
||
override def read(kryo: Kryo, input: Input, `type`: Class[PMMLModel]): PMMLModel = { | ||
val bytes = Stream.continually(input.readByte()).takeWhile(_ != -1).toArray | ||
PMMLModel(bytes).get | ||
} | ||
|
||
/** Writes the bytes for the object to the output. | ||
* <p> | ||
* This method should not be called directly, instead this serializer can be passed to {@link Kryo} write methods that accept a | ||
* serialier. | ||
* | ||
* @param value May be null if { @link #getAcceptsNull()} is true. */ | ||
|
||
override def write(kryo: Kryo, output: Output, value: PMMLModel): Unit = { | ||
output.write(value.toBytes) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/scala/com/lightbend/modelServer/model/TensorFlowModelSerializerKryo.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.lightbend.modelServer.model | ||
|
||
/** | ||
* Created by boris on 6/2/17. | ||
*/ | ||
import com.esotericsoftware.kryo.{Kryo, Serializer} | ||
import com.esotericsoftware.kryo.io.{Input, Output} | ||
|
||
|
||
class TensorFlowModelSerializerKryo extends Serializer[TensorFlowModel]{ | ||
|
||
super.setAcceptsNull(false) | ||
super.setImmutable(true) | ||
|
||
/** Reads bytes and returns a new object of the specified concrete type. | ||
* <p> | ||
* Before Kryo can be used to read child objects, {@link Kryo#reference(Object)} must be called with the parent object to | ||
* ensure it can be referenced by the child objects. Any serializer that uses {@link Kryo} to read a child object may need to | ||
* be reentrant. | ||
* <p> | ||
* This method should not be called directly, instead this serializer can be passed to {@link Kryo} read methods that accept a | ||
* serialier. | ||
* | ||
* @return May be null if { @link #getAcceptsNull()} is true. */ | ||
|
||
override def read(kryo: Kryo, input: Input, `type`: Class[TensorFlowModel]): TensorFlowModel = { | ||
val bytes = Stream.continually(input.readByte()).takeWhile(_ != -1).toArray | ||
TensorFlowModel(bytes).get | ||
} | ||
|
||
/** Writes the bytes for the object to the output. | ||
* <p> | ||
* This method should not be called directly, instead this serializer can be passed to {@link Kryo} write methods that accept a | ||
* serialier. | ||
* | ||
* @param value May be null if { @link #getAcceptsNull()} is true. */ | ||
|
||
override def write(kryo: Kryo, output: Output, value: TensorFlowModel): Unit = { | ||
output.write(value.graph.toGraphDef) | ||
} | ||
} |