i using tensorflow java api load created tensorflow model jvm. using example: tensorflow/examples/labelimage.java
here simple scala code:
import java.nio.file.{files, path, paths} import org.tensorflow.{graph, session, tensor} def readallbytesorexit(path: path): array[byte] = files.readallbytes(path) val graphdef = readallbytesorexit(paths.get("path_to_a_single_file_describing_tf_model.pb")) val g = new graph() g.importgraphdef(graphdef) val session = new session(g) val result: tensor = session.runner().feed("input", image).fetch("output").run().get(0))
how save model both session , graph stored in same file. described in "path_to_a_single_file_describing_tf_model.pb" above.
described here mentions:
the serialized representation of graph, referred graphdef, can generated tographdef() , equivalents in other language apis.
what equivalents in other language apis? dont find obvious
note: looked @ mnist_saved_model.py under tensorflow_serving saving through procedure gives me .pb
file , variables
folder. when trying load .pb
file get: java.lang.illegalargumentexception: invalid graphdef
currently java api of tensorflow, found how save graph graphdef (i.e. without variables , meta-data). can done writing array[byte] file:
files.write(paths.get(modeldir, modelname), mygraph.tographdef)
here mygraph
java object graph class.
i suggest save model python api, using savedmodel api defined here. save model in folder both serialized graph in .pb file , variables in folder. note tag_constants use you'll need in scala/java code load model variables. graph , session variables loaded savedmodelbundle java class java api. returns wrapper both graph , session containing variables values:
val model = savedmodelbundle.load(modeldir, modeltag)
if tried this, maybe can share code see why returned invalid graphdef.
another option freeze graph, i.e. turned variable nodes constant nodes self-contained in .pb file. mores infos here freezing part
Comments
Post a Comment