Java调用Keras、Tensorflow模型
实现python离线训练模型,Java在线预测部署。
目前深度学习主流使用python训练自己的模型,有非常多的框架提供了能快速搭建神经网络的功能,其中Keras提供了high-level的语法,底层可以使用tensorflow或者theano。
但是有很多公司后台应用是用Java开发的,如果用python提供HTTP接口,对业务延迟要求比较高的话,仍然会有一定得延迟,所以能不能使用Java调用模型,python可以离线的训练模型?(tensorflow也提供了成熟的部署方案TensorFlow Serving)
手头上有一个用Keras训练的模型,网上关于Java调用Keras模型的资料不是很多,而且大部分是重复的,并且也没有讲的很详细。大致有两种方案,一种是基于Java的深度学习库导入Keras模型实现,另外一种是用tensorflow提供的Java接口调用。
Deeplearning4J
Eclipse Deeplearning4j is the first commercial-grade, open-source, distributed deep-learning library written for Java and Scala. Integrated with Hadoop and Spark, DL4J brings AIAI to business environments for use on distributed GPUs and CPUs.
Deeplearning4j目前支持导入Keras训练的模型,并且提供了类似python中numpy的一些功能,更方便地处理结构化的数据。遗憾的是,Deeplearning4j现在只覆盖了Keras <2.0版本的大部分Layer,如果你是用Keras 2.0以上的版本,在导入模型的时候可能会报错。
了解更多:
Keras Model Import: Supported Features
Importing Models From Keras to Deeplearning4j
Tensorflow
文档,Java的文档很少,不过调用模型的过程也很简单。采用这种方式调用模型需要先将Keras导出的模型转成tensorflow的protobuf协议的模型。
1、Keras的h5模型转为pb模型
在Keras中使用model.save(model.h5)
保存当前模型为HDF5格式的文件中。
Keras的后端框架使用的是tensorflow,所以先把模型导出为pb模型。在Java中只需要调用模型进行预测,所以将当前的graph中的Variable全部变成Constant,并且使用训练后的weight。以下是freeze graph的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True): """ :param session: 需要转换的tensorflow的session :param keep_var_names:需要保留的variable,默认全部转换constant :param output_names:output的名字 :param clear_devices:是否移除设备指令以获得更好的可移植性 :return: """ from tensorflow.python.framework.graph_util import convert_variables_to_constants graph = session.graph with graph.as_default(): freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or [])) output_names = output_names or [] # 如果指定了output名字,则复制一个新的Tensor,并且以指定的名字命名 if len(output_names) > 0: for i in range(output_names): # 当前graph中复制一个新的Tensor,指定名字 tf.identity(model.model.outputs[i], name=output_names[i]) output_names += [v.op.name for v in tf.global_variables()] input_graph_def = graph.as_graph_def() if clear_devices: for node in input_graph_def.node: node.device = "" frozen_graph = convert_variables_to_constants(session, input_graph_def, output_names, freeze_var_names) return frozen_graph |
该方法可以将tensor为Variable的graph全部转为constant并且使用训练后的weight。注意output_name比较重要,后面Java调用模型的时候会用到。
在Keras中,模型是这么定义的:
1 2 3 4 5 6 7 8 9 10 11 12 |
def create_model(self): input_tensor = Input(shape=(self.maxlen,), name="input") x = Embedding(len(self.text2id) + 1, 200)(input_tensor) x = Bidirectional(LSTM(128))(x) x = Dense(256, activation="relu")(x) x = Dropout(self.dropout)(x) x = Dense(len(self.id2class), activation='softmax', name="output_softmax")(x) model = Model(inputs=input_tensor, outputs=x) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) |
下面的代码可以查看定义好的Keras模型的输入、输出的name,这对之后Java调用有帮助。
1 2 3 |
print(model.input.op.name) print(model.output.op.name) |
训练好Keras模型后,转换为pb模型:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
from keras import backend as K import tensorflow as tf model.load_model("model.h5") print(model.input.op.name) print(model.output.op.name) # 自定义output_names frozen_graph = freeze_session(K.get_session(), output_names=["output"]) tf.train.write_graph(frozen_graph, "./", "model.pb", as_text=False) ### 输出: # input # output_softmax/Softmax # 如果不自定义output_name,则生成的pb模型的output_name为output_softmax/Softmax,如果自定义则以自定义名为output_name |
运行之后会生成model.pb的模型,这将是之后调用的模型。
2、Java调用
新建一个maven项目,pom里面导入tensorflow包:
1 2 3 4 5 6 |
<dependency> <groupId>org.tensorflow</groupId> <artifactId>tensorflow</artifactId> <version>1.6.0</version> </dependency> |
核心代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public void predict() throws Exception { try (Graph graph = new Graph()) { graph.importGraphDef(Files.readAllBytes(Paths.get( "path/to/model.pb" ))); try (Session sess = new Session(graph)) { // 自己构造一个输入 float[][] input = {{56, 632, 675, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; try (Tensor x = Tensor.create(input); // input是输入的name,output是输出的name Tensor y = sess.runner().feed("input", x).fetch("output").run().get(0)) { float[][] result = new float[1][y.shape[1]]; y.copyTo(result); System.out.println(Arrays.toString(y.shape())); System.out.println(Arrays.toString(result[0])); } } } } |
Graph和Tensor对象都是需要通过close()
方法显式地释放占用的资源,代码中使用了try-with-resources
的方法实现的。
至此,已经可以实现Keras离线训练,Java在线预测的功能。
链接:https://www.ioiogoo.cn/2018/04/03/java调用keras、tensorflow模型/
本站所有文章除特殊说明外均为原创,转载请注明出处!