Go中的Tensorflow服务
我正试着在围棋中运行keras模型。首先,我用python训练模型:Go中的Tensorflow服务,go,tensorflow,Go,Tensorflow,我正试着在围棋中运行keras模型。首先,我用python训练模型: import keras as krs from keras import backend as K import tensorflow as tf sess = tf.Session() K.set_session(sess) K._LEARNING_PHASE = tf.constant(0) K.set_learning_phase(0) m1 = krs.models.Sequential() m1.Add(krs
import keras as krs
from keras import backend as K
import tensorflow as tf
sess = tf.Session()
K.set_session(sess)
K._LEARNING_PHASE = tf.constant(0)
K.set_learning_phase(0)
m1 = krs.models.Sequential()
m1.Add(krs.layers.Dense(..., name="inputNode"))
...
m1.Add(krs.layers.Dense(..., activation="softmax", name="outputNode"))
m1.compile(...)
m1.fit(...)
然后我了解到,建议将模型冻结—将占位符转换为常量
saver = tf.train.Saver()
tf.train.write_graph(sess.graph_def, '.', 'my_model.pbtxt')
saver.save(sess, save_path="my_model.ckpt")
from tensorflow.python.tools import freeze_graph
from tensorflow.python.tools import optimize_for_inference_lib
freeze_graph.freeze_graph(input_graph = 'my_model.pbtxt', input_saver = "",
input_binary = False, input_checkpoint = "my_model.ckpt", output_node_names = "outputNode/Softmax",
restore_op_name = "save/restore_all", filename_tensor_name = "save/Const:0",
output_graph = "frozen_my_model.pb", clear_devices = True, initializer_nodes = "")
在Golang尝试使用冻结模型时:
model, err := tf.LoadSavedModel("frozen_my_model.pb", []string{"serve"}, nil)
它返回一个错误,即找不到标记{serve}的标记服务savedModelLoad;状态:失败。
因此,我的问题是:
optimize\u for\u inference
,在上述设置中如何实现 # Create a builder to export the model
builder = tf.saved_model.builder.SavedModelBuilder("export")
# Tag the model in order to be capable of restoring it specifying the tag set
builder.add_meta_graph_and_variables(sess, ["tag"])
builder.save()
之后,您可以将其加载到Go中
然而,一个更方便的解决方案是使用
正如您在自述文件中看到的,这两个方面都有代码:python中的训练和Go中的推理。
我在这里为您报道:
Python:在MNIST上训练LeNet(示例)
围棋:推理
import sys
import tensorflow as tf
from dytb.inputs.predefined.MNIST import MNIST
from dytb.models.predefined.LeNetDropout import LeNetDropout
from dytb.train import train
def main():
"""main executes the operations described in the module docstring"""
lenet = LeNetDropout()
mnist = MNIST()
info = train(
model=lenet,
dataset=mnist,
hyperparameters={"epochs": 2},)
checkpoint_path = info["paths"]["best"]
with tf.Session() as sess:
# Define a new model, import the weights from best model trained
# Change the input structure to use a placeholder
images = tf.placeholder(tf.float32, shape=(None, 28, 28, 1), name="input_")
# define in the default graph the model that uses placeholder as input
_ = lenet.get(images, mnist.num_classes)
# The best checkpoint path contains just one checkpoint, thus the last is the best
saver = tf.train.Saver()
saver.restore(sess, tf.train.latest_checkpoint(checkpoint_path))
# Create a builder to export the model
builder = tf.saved_model.builder.SavedModelBuilder("export")
# Tag the model in order to be capable of restoring it specifying the tag set
builder.add_meta_graph_and_variables(sess, ["tag"])
builder.save()
return 0
if __name__ == '__main__':
sys.exit(main())
package main
import (
"fmt"
tg "github.com/galeone/tfgo"
tf "github.com/tensorflow/tensorflow/tensorflow/go"
)
func main() {
model := tg.LoadModel("test_models/export", []string{"tag"}, nil)
fakeInput, _ := tf.NewTensor([1][28][28][1]float32{})
results := model.Exec([]tf.Output{
model.Op("LeNetDropout/softmax_linear/Identity", 0),
}, map[tf.Output]*tf.Tensor{
model.Op("input_", 0): fakeInput,
})
predictions := results[0].Value().([][]float32)
fmt.Println(predictions)
}