Keras模型转TensorFlow格式及使用

2021-11-23 16:47:51 浏览数 (1)

由于方便快捷,所以先使用Keras来搭建网络并进行训练,得到比较好的模型后,这时候就该考虑做成服务使用的问题了,TensorFlow的serving就很合适,所以需要把Keras保存的模型转为TensorFlow格式来使用。

Keras模型转TensorFlow

其实由于TensorFlow本身以及把Keras作为其高层简化API,且也是建议由浅入深地来研究应用,TensorFlow本身就对Keras的模型格式转化有支持,所以核心的代码很少。这里给出一份代码:https://github.com/amir-abdi/keras_to_tensorflow,作者提供了一份很好的工具,能够满足绝大多数人的需求了。原理很简单:原理很简单,首先用 Keras 读取 .h5 模型文件,然后用 tensorflow 的 convert_variables_to_constants 函数将所有变量转换成常量,最后再 write_graph 就是一个包含了网络以及参数值的 .pb 文件了。

这里由于我是python2的环境,与作者的代码有一点点冲突,以我的一些需要,所以做了一点点修改:

代码语言:javascript复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright (c) 2018, by the Authors: Amir H. Abdi
This script is freely available under the MIT Public License.
Please see the License file in the root for details.
The following code snippet will convert the keras model files
to the freezed .pb tensorflow weight file. The resultant TensorFlow model
holds both the model architecture and its associated weights.
"""
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "6"
import tensorflow as tf
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io
from pathlib import Path
from absl import app
from absl import flags
from absl import logging
import keras
from keras import backend as K
from keras.models import model_from_json

K.set_learning_phase(0)
FLAGS = flags.FLAGS

flags.DEFINE_string('input_model', None, 'Path to the input model.')
flags.DEFINE_string('input_model_json', None, 'Path to the input model '
                                              'architecture in json format.')
flags.DEFINE_string('output_model', None, 'Path where the converted model will '
                                          'be stored.')
flags.DEFINE_boolean('save_graph_def', False,
                     'Whether to save the graphdef.pbtxt file which contains '
                     'the graph definition in ASCII format.')
flags.DEFINE_string('output_nodes_prefix', None,
                    'If set, the output nodes will be renamed to '
                    '`output_nodes_prefix` i, where `i` will numerate the '
                    'number of of output nodes of the network.')
flags.DEFINE_boolean('quantize', False,
                     'If set, the resultant TensorFlow graph weights will be '
                     'converted from float into eight-bit equivalents. See '
                     'documentation here: '
                     'https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tools/graph_transforms')
flags.DEFINE_boolean('channels_first', False,
                     'Whether channels are the first dimension of a tensor. '
                     'The default is TensorFlow behaviour where channels are '
                     'the last dimension.')
flags.DEFINE_boolean('output_meta_ckpt', False,
                     'If set to True, exports the model as .meta, .index, and '
                     '.data files, with a checkpoint file. These can be later '
                     'loaded in TensorFlow to continue training.')

flags.mark_flag_as_required('input_model')
flags.mark_flag_as_required('output_model')


def load_model(input_model_path, input_json_path):
    if not Path(input_model_path).exists():
        raise FileNotFoundError(
            'Model file `{}` does not exist.'.format(input_model_path))
    try:
        model = keras.models.load_model(input_model_path)
        return model
    except FileNotFoundError as err:
        logging.error('Input mode file (%s) does not exist.', FLAGS.input_model)
        raise err
    except ValueError as wrong_file_err:
        if input_json_path:
            if not Path(input_json_path).exists():
                raise FileNotFoundError(
                    'Model description json file `{}` does not exist.'.format(
                        input_json_path))
            try:
                model = model_from_json(open(str(input_json_path)).read())
                model.load_weights(input_model_path)
                return model
            except Exception as err:
                logging.error("Couldn't load model from json.")
                raise err
        else:
            logging.error(
                'Input file specified only holds the weights, and not '
                'the model definition. Save the model using '
                'model.save(filename.h5) which will contain the network '
                'architecture as well as its weights. If the model is '
                'saved using model.save_weights(filename), the flag '
                'input_model_json should also be set to the '
                'architecture which is exported separately in a '
                'json format. Check the keras documentation for more details '
                '(https://keras.io/getting-started/faq/)')
            raise wrong_file_err


def main(args):
    logging.info("begin====================================================")
    # If output_model path is relative and in cwd, make it absolute from root
    output_model = FLAGS.output_model
    if str(Path(output_model).parent) == '.':
        output_model = str((Path.cwd() / output_model))

    output_fld = Path(output_model).parent
    output_model_name = Path(output_model).name
    output_model_stem = Path(output_model).stem
    output_model_pbtxt_name = output_model_stem   '.pbtxt'

    # Create output directory if it does not exist
    # print (Path(output_model).parent)
    if not os.path.exists(str(Path(output_model).parent)):
        Path(output_model).parent.mkdir(parents=True)

    if FLAGS.channels_first:
        K.set_image_data_format('channels_first')
    else:
        K.set_image_data_format('channels_last')

    model = load_model(FLAGS.input_model, FLAGS.input_model_json)

    input_node_names = [node.op.name for node in model.inputs]
    logging.info('Input nodes names are: %s', str(input_node_names))

    # TODO(amirabdi): Support networks with multiple inputs
    orig_output_node_names = [node.op.name for node in model.outputs]
    if FLAGS.output_nodes_prefix: # 给模型节点编号
        num_output = len(orig_output_node_names)
        pred = [None] * num_output
        converted_output_node_names = [None] * num_output

        # Create dummy tf nodes to rename output
        for i in range(num_output):
            converted_output_node_names[i] = '{}{}'.format(
                FLAGS.output_nodes_prefix, i)
            pred[i] = tf.identity(model.outputs[i],
                                  name=converted_output_node_names[i])
    else:
        converted_output_node_names = orig_output_node_names
    logging.info('Converted output node names are: %s',
                 str(converted_output_node_names))
    

    sess = K.get_session()
    if FLAGS.output_meta_ckpt:  # 让转化的模型可以继续被训练
        saver = tf.train.Saver()
        saver.save(sess, str(output_fld / output_model_stem))

    if FLAGS.save_graph_def: # 以ascii形式存储模型
        tf.train.write_graph(sess.graph.as_graph_def(), str(output_fld),
                             output_model_pbtxt_name, as_text=True)
        logging.info('Saved the graph definition in ascii format at %s',
                     str(Path(output_fld) / output_model_pbtxt_name))

    if FLAGS.quantize: # 将权重从float转为八位比特
        from tensorflow.tools.graph_transforms import TransformGraph
        transforms = ["quantize_weights", "quantize_nodes"]
        transformed_graph_def = TransformGraph(sess.graph.as_graph_def(), [],
                                               converted_output_node_names,
                                               transforms)
        constant_graph = graph_util.convert_variables_to_constants(
            sess,
            transformed_graph_def,
            converted_output_node_names)
    else: # float形式存储权重
        constant_graph = graph_util.convert_variables_to_constants(
            sess,
            sess.graph.as_graph_def(),
            converted_output_node_names)

    graph_io.write_graph(constant_graph, str(output_fld), output_model_name,
                         as_text=False)
    logging.info('Saved the freezed graph at %s',
                 str(Path(output_fld) / output_model_name))


if __name__ == "__main__":
    app.run(main)

如果你的Keras模型是一个包含了网络结构和权重的h5文件,那么使用下面的命令就可以了:

代码语言:javascript复制
python keras_to_tensorflow.py 
    --input_model="path/to/keras/model.h5" 
    --output_model="path/to/save/model.pb"

两个参数,一个输入路径,一个输出路径。输出路径即使你没创建好,代码也会帮你创建。建议使用绝对地址。此外作者还做了很多选项,比如如果你的keras模型文件分为网络结构和权重两个文件也可以支持,或者你想给转化后的网络节点编号,或者想在TensorFlow下继续训练等等,这份代码都是支持的,只是使用上需要输入不同的参数来设置。

在我改进的代码中,一个是适配python 2,另一个就是会输出输入层与输出层的名字,而这个是在你使用模型的时候需要的,运行我的代码后如果成功则输出如下:

代码语言:javascript复制
begin====================================================
I1229 14:29:44.819010 140709034264384 keras_to_tf.py:119] Input nodes names are: [u'input_1']
I1229 14:29:44.819385 140709034264384 keras_to_tf.py:137] Converted output node names are: [u'dense_2/Sigmoid']
INFO:tensorflow:Froze 322 variables.
I1229 14:29:47.091161 140709034264384 tf_logging.py:82] Froze 322 variables.
Converted 322 variables to const ops.
I1229 14:29:48.504235 140709034264384 keras_to_tf.py:170] Saved the freezed graph at /path/to/save/model.pb

这里首先把输入的层和输出的层名字给出来了,也就是“input_1”和“dense_2/Sigmoid”,这两个下面会用到。另外还告诉你冻结了多少个变量,以及你输出的模型路径,pb文件就是TensorFlow下的模型文件。

使用TensorFlow模型

转换后我们当然要使用一下看是否转换成功,其实也就是TensorFlow的常见代码,如果只用过Keras的,可以参考一下:

代码语言:javascript复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
from tensorflow.python.platform import gfile
import cv2
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "6"

# img = cv2.imread(os.path.expanduser('/test_imgs/img_1.png'))
# img = cv2.resize(img, dsize=(1000, 1000), interpolation=cv2.INTER_LINEAR)
# img = img.astype(float)
# img /= 255
# img = np.array([img])

# 初始化TensorFlow的session
with tf.Session() as sess:
    # 读取得到的pb文件加载模型
    with gfile.FastGFile("/path/to/save/model.pb",'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        # 把图加到session中
        tf.import_graph_def(graph_def, name='')

    # 获取当前计算图
    graph = tf.get_default_graph()

    # 从图中获输出那一层
    pred = graph.get_tensor_by_name("dense_2/Sigmoid:0")
    
    # 运行并预测输入的img
    res = sess.run(pred, feed_dict={"input_1:0": img})

    # 执行得到结果
    pred_index = res[0][0]
    print('Predict:', pred_index)

在代码中可以看到,我们用到了上面得到的输入层和输出层的名称,但是在后面加了一个“:0”,也就是索引,因为名称只是指定了一个层,大部分层的输出都是一个tensor,但依然有输出多个tensor的层,所以需要制定是第几个输出,对于一个输出的情况,那就是索引0了。输入同理。

如果你输出res,会得到这样的结果:

代码语言:javascript复制
('Predict:', array([[0.9998584]], dtype=float32))

这也就是为什么我们要取res[0][0]了,这个输出其实取决于具体的需求,因为这里我是对一张图做二分类预测,所以会得到这样一个结果

运行的结果如果和使用Keras模型时一样,那就说明转换成功了!

参考文章: http://www.liuxiao.org/2018/10/keras-trained-model-export-for-tensorflow-serving/

0 人点赞