tensorflow将训练好的模型freeze,即将权重固化到图里面,并使用该模型进行预测

ML主要分为训练和预测两个阶段,此教程就是将训练好的模型freeze并保存下来.freeze的含义就是将该模型的图结构和该模型的权重固化到一起了.也即加载freeze的模型之后,立刻能够使用了。

下面使用一个简单的demo来详细解释该过程,

一、首先运行脚本tiny_model.py

#-*- coding:utf-8 -*-
import tensorflow as tf
import numpy as np


with tf.variable_scope('Placeholder'):
    inputs_placeholder = tf.placeholder(tf.float32, name='inputs_placeholder', shape=[None, 10])
    labels_placeholder = tf.placeholder(tf.float32, name='labels_placeholder', shape=[None, 1])

with tf.variable_scope('NN'):
    W1 = tf.get_variable('W1', shape=[10, 1], initializer=tf.random_normal_initializer(stddev=1e-1))
    b1 = tf.get_variable('b1', shape=[1], initializer=tf.constant_initializer(0.1))
    W2 = tf.get_variable('W2', shape=[10, 1], initializer=tf.random_normal_initializer(stddev=1e-1))
    b2 = tf.get_variable('b2', shape=[1], initializer=tf.constant_initializer(0.1))

    a = tf.nn.relu(tf.matmul(inputs_placeholder, W1) + b1)
    a2 = tf.nn.relu(tf.matmul(inputs_placeholder, W2) + b2)

    y = tf.div(tf.add(a, a2), 2)

with tf.variable_scope('Loss'):
    loss = tf.reduce_sum(tf.square(y - labels_placeholder) / 2)

with tf.variable_scope('Accuracy'):
    predictions = tf.greater(y, 0.5, name="predictions")
    correct_predictions = tf.equal(predictions, tf.cast(labels_placeholder, tf.bool), name="correct_predictions")
    accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32))


adam = tf.train.AdamOptimizer(learning_rate=1e-3)
train_op = adam.minimize(loss)

# generate_data
inputs = np.random.choice(10, size=[10000, 10])
labels = (np.sum(inputs, axis=1) > 45).reshape(-1, 1).astype(np.float32)
print('inputs.shape:', inputs.shape)
print('labels.shape:', labels.shape)


test_inputs = np.random.choice(10, size=[100, 10])
test_labels = (np.sum(test_inputs, axis=1) > 45).reshape(-1, 1).astype(np.float32)
print('test_inputs.shape:', test_inputs.shape)
print('test_labels.shape:', test_labels.shape)

batch_size = 32
epochs = 10

batches = []
print("%d items in batch of %d gives us %d full batches and %d batches of %d items" % (
    len(inputs),
    batch_size,
    len(inputs) // batch_size,
    batch_size - len(inputs) // batch_size,
    len(inputs) - (len(inputs) // batch_size) * 32)
)
for i in range(len(inputs) // batch_size):
    batch = [ inputs[batch_size*i:batch_size*i+batch_size], labels[batch_size*i:batch_size*i+batch_size] ]
    batches.append(list(batch))
if (i + 1) * batch_size < len(inputs):
    batch = [ inputs[batch_size*(i + 1):],labels[batch_size*(i + 1):] ]
    batches.append(list(batch))
print("Number of batches: %d" % len(batches))
print("Size of full batch: %d" % len(batches[0]))
print("Size if final batch: %d" % len(batches[-1]))

global_count = 0

with tf.Session() as sess:
#sv = tf.train.Supervisor()
#with sv.managed_session() as sess:
    sess.run(tf.initialize_all_variables())
    for i in range(epochs):
        for batch in batches:
            # print(batch[0].shape, batch[1].shape)
            train_loss , _= sess.run([loss, train_op], feed_dict={
                inputs_placeholder: batch[0],
                labels_placeholder: batch[1]
            })
            # print('train_loss: %d' % train_loss)

            if global_count % 100 == 0:
                acc = sess.run(accuracy, feed_dict={
                    inputs_placeholder: test_inputs,
                    labels_placeholder: test_labels
                })
                print('accuracy: %f' % acc)
            global_count += 1

    acc = sess.run(accuracy, feed_dict={
        inputs_placeholder: test_inputs,
        labels_placeholder: test_labels
    })
    print("final accuracy: %f" % acc)
    #在session当中就要将模型进行保存
    saver = tf.train.Saver()
    last_chkp = saver.save(sess, 'results/graph.chkp')
    #sv.saver.save(sess, 'results/graph.chkp')

for op in tf.get_default_graph().get_operations():
    print(op.name)
说明:saver.save必须在session里面,因为在session里面,整个图才是激活的,才能够将参数存进来,使用save之后能够得到如下的文件:


说明:
.data:存放的是权重参数
.meta:存放的是图和metadata,metadata是其他配置的数据
如果想将我们的模型固化,让别人能够使用,我们仅仅需要的是图和参数,metadata是不需要的

二、综合上述几个文件,生成可以使用的模型的步骤如下

1、恢复我们保存的图
2、开启一个Session,然后载入该图要求的权重
3、删除对预测无关的metadata
4、将处理好的模型序列化之后保存
运行freeze.py

#-*- coding:utf-8 -*-
import os, argparse
import tensorflow as tf
from tensorflow.python.framework import graph_util

dir = os.path.dirname(os.path.realpath(__file__))

def freeze_graph(model_folder):
    # We retrieve our checkpoint fullpath
    checkpoint = tf.train.get_checkpoint_state(model_folder)
    input_checkpoint = checkpoint.model_checkpoint_path
    
    # We precise the file fullname of our freezed graph
    absolute_model_folder = "/".join(input_checkpoint.split('/')[:-1])
    output_graph = absolute_model_folder + "/frozen_model.pb"

    # Before exporting our graph, we need to precise what is our output node
    # this variables is plural, because you can have multiple output nodes
    #freeze之前必须明确哪个是输出结点,也就是我们要得到推论结果的结点
    #输出结点可以看我们模型的定义
    #只有定义了输出结点,freeze才会把得到输出结点所必要的结点都保存下来,或者哪些结点可以丢弃
    #所以,output_node_names必须根据不同的网络进行修改
    output_node_names = "Accuracy/predictions"

    # We clear the devices, to allow TensorFlow to control on the loading where it wants operations to be calculated
    clear_devices = True
    
    # We import the meta graph and retrive a Saver
    saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices)

    # We retrieve the protobuf graph definition
    graph = tf.get_default_graph()
    input_graph_def = graph.as_graph_def()

    #We start a session and restore the graph weights
    #这边已经将训练好的参数加载进来,也即最后保存的模型是有图,并且图里面已经有参数了,所以才叫做是frozen
    #相当于将参数已经固化在了图当中 
    with tf.Session() as sess:
        saver.restore(sess, input_checkpoint)

        # We use a built-in TF helper to export variables to constant
        output_graph_def = graph_util.convert_variables_to_constants(
            sess, 
            input_graph_def, 
            output_node_names.split(",") # We split on comma for convenience
        ) 

        # Finally we serialize and dump the output graph to the filesystem
        with tf.gfile.GFile(output_graph, "wb") as f:
            f.write(output_graph_def.SerializeToString())
        print("%d ops in the final graph." % len(output_graph_def.node))


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--model_folder", type=str, help="Model folder to export")
    args = parser.parse_args()

    freeze_graph(args.model_folder)

说明: 对于freeze操作,我们需要定义输出结点的名字.因为网络其实是比较复杂的,定义了输出结点的名字,那么freeze的时候就只把输出该结点所需要的子图都固化下来,其他无关的就舍弃掉.因为我们freeze模型的目的是接下来做预测.所以,一般情况下,output_node_names就是我们预测的目标.

三、加载freeze后的模型,注意该模型已经是包含图和相应的参数了.所以,我们不需要再加载参数进来.也即该模型加载进来已经是可以使用了.

#-*- coding:utf-8 -*-
import argparse 
import tensorflow as tf

def load_graph(frozen_graph_filename):
    # We parse the graph_def file
    with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

    # We load the graph_def in the default graph
    with tf.Graph().as_default() as graph:
        tf.import_graph_def(
            graph_def, 
            input_map=None, 
            return_elements=None, 
            name="prefix", 
            op_dict=None, 
            producer_op_list=None
        )
    return graph

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--frozen_model_filename", default="results/frozen_model.pb", type=str, help="Frozen model file to import")
    args = parser.parse_args()
    #加载已经将参数固化后的图
    graph = load_graph(args.frozen_model_filename)

    # We can list operations
    #op.values() gives you a list of tensors it produces
    #op.name gives you the name
    #输入,输出结点也是operation,所以,我们可以得到operation的名字
    for op in graph.get_operations():
        print(op.name,op.values())
        # prefix/Placeholder/inputs_placeholder
        # ...
        # prefix/Accuracy/predictions
    #操作有:prefix/Placeholder/inputs_placeholder
    #操作有:prefix/Accuracy/predictions
    #为了预测,我们需要找到我们需要feed的tensor,那么就需要该tensor的名字
    #注意prefix/Placeholder/inputs_placeholder仅仅是操作的名字,prefix/Placeholder/inputs_placeholder:0才是tensor的名字
    x = graph.get_tensor_by_name('prefix/Placeholder/inputs_placeholder:0')
    y = graph.get_tensor_by_name('prefix/Accuracy/predictions:0')
        
    with tf.Session(graph=graph) as sess:
        y_out = sess.run(y, feed_dict={
            x: [[3, 5, 7, 4, 5, 1, 1, 1, 1, 1]] # < 45
        })
        print(y_out) # [[ 0.]] Yay!
    print ("finish")
说明:

1、在预测的过程中,当把freeze后的模型加载进来后,我们只需要定义好输入的tensor和目标tensor即可

2、在这里要注意一下tensor_name和ops_name,

注意prefix/Placeholder/inputs_placeholder仅仅是操作的名字,prefix/Placeholder/inputs_placeholder:0才是tensor的名字

x = graph.get_tensor_by_name('prefix/Placeholder/inputs_placeholder:0')一定要使用tensor的名字

3、要获取图中ops的名字和对应的tensor的名字,可用如下的代码:

    # We can list operations
    #op.values() gives you a list of tensors it produces
    #op.name gives you the name
    #输入,输出结点也是operation,所以,我们可以得到operation的名字
    for op in graph.get_operations():
        print(op.name,op.values())

=============================================================================================================================

上面是使用了Saver()来保存模型,也可以使用sv = tf.train.Supervisor()来保存模型

#-*- coding:utf-8 -*-
import tensorflow as tf
import numpy as np


with tf.variable_scope('Placeholder'):
    inputs_placeholder = tf.placeholder(tf.float32, name='inputs_placeholder', shape=[None, 10])
    labels_placeholder = tf.placeholder(tf.float32, name='labels_placeholder', shape=[None, 1])

with tf.variable_scope('NN'):
    W1 = tf.get_variable('W1', shape=[10, 1], initializer=tf.random_normal_initializer(stddev=1e-1))
    b1 = tf.get_variable('b1', shape=[1], initializer=tf.constant_initializer(0.1))
    W2 = tf.get_variable('W2', shape=[10, 1], initializer=tf.random_normal_initializer(stddev=1e-1))
    b2 = tf.get_variable('b2', shape=[1], initializer=tf.constant_initializer(0.1))

    a = tf.nn.relu(tf.matmul(inputs_placeholder, W1) + b1)
    a2 = tf.nn.relu(tf.matmul(inputs_placeholder, W2) + b2)

    y = tf.div(tf.add(a, a2), 2)

with tf.variable_scope('Loss'):
    loss = tf.reduce_sum(tf.square(y - labels_placeholder) / 2)

with tf.variable_scope('Accuracy'):
    predictions = tf.greater(y, 0.5, name="predictions")
    correct_predictions = tf.equal(predictions, tf.cast(labels_placeholder, tf.bool), name="correct_predictions")
    accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32))


adam = tf.train.AdamOptimizer(learning_rate=1e-3)
train_op = adam.minimize(loss)

# generate_data
inputs = np.random.choice(10, size=[10000, 10])
labels = (np.sum(inputs, axis=1) > 45).reshape(-1, 1).astype(np.float32)
print('inputs.shape:', inputs.shape)
print('labels.shape:', labels.shape)


test_inputs = np.random.choice(10, size=[100, 10])
test_labels = (np.sum(test_inputs, axis=1) > 45).reshape(-1, 1).astype(np.float32)
print('test_inputs.shape:', test_inputs.shape)
print('test_labels.shape:', test_labels.shape)

batch_size = 32
epochs = 10

batches = []
print("%d items in batch of %d gives us %d full batches and %d batches of %d items" % (
    len(inputs),
    batch_size,
    len(inputs) // batch_size,
    batch_size - len(inputs) // batch_size,
    len(inputs) - (len(inputs) // batch_size) * 32)
)
for i in range(len(inputs) // batch_size):
    batch = [ inputs[batch_size*i:batch_size*i+batch_size], labels[batch_size*i:batch_size*i+batch_size] ]
    batches.append(list(batch))
if (i + 1) * batch_size < len(inputs):
    batch = [ inputs[batch_size*(i + 1):],labels[batch_size*(i + 1):] ]
    batches.append(list(batch))
print("Number of batches: %d" % len(batches))
print("Size of full batch: %d" % len(batches[0]))
print("Size if final batch: %d" % len(batches[-1]))

global_count = 0

#with tf.Session() as sess:
sv = tf.train.Supervisor()
with sv.managed_session() as sess:
    #sess.run(tf.initialize_all_variables())
    for i in range(epochs):
        for batch in batches:
            # print(batch[0].shape, batch[1].shape)
            train_loss , _= sess.run([loss, train_op], feed_dict={
                inputs_placeholder: batch[0],
                labels_placeholder: batch[1]
            })
            # print('train_loss: %d' % train_loss)

            if global_count % 100 == 0:
                acc = sess.run(accuracy, feed_dict={
                    inputs_placeholder: test_inputs,
                    labels_placeholder: test_labels
                })
                print('accuracy: %f' % acc)
            global_count += 1

    acc = sess.run(accuracy, feed_dict={
        inputs_placeholder: test_inputs,
        labels_placeholder: test_labels
    })
    print("final accuracy: %f" % acc)
    #在session当中就要将模型进行保存
    #saver = tf.train.Saver()
    #last_chkp = saver.save(sess, 'results/graph.chkp')
    sv.saver.save(sess, 'results/graph.chkp')

for op in tf.get_default_graph().get_operations():
    print(op.name)

注意:使用了sv = tf.train.Supervisor(),就不需要再初始化了,将sess.run(tf.initialize_all_variables())注释掉,否则会报错.



  • 32
    点赞
  • 125
    收藏
    觉得还不错? 一键收藏
  • 34
    评论
训练模型成pb文件: 首先,你需要先定义并训练好一个Tensorflow模型。在模型训练完成后,你可以使用Tensorflow的`freeze_graph.py`脚本将模型转换为pb文件。 在终端中输入以下命令: ``` python tensorflow/python/tools/freeze_graph.py \ --input_graph=<input_graph.pb> \ --input_checkpoint=<input_checkpoint> \ --output_graph=<output_graph.pb> \ --output_node_names=<output_node_names> ``` 其中: - `<input_graph.pb>`:模型的GraphDef文件。 - `<input_checkpoint>`:模型的checkpoint文件。 - `<output_graph.pb>`:转换后的pb文件的输出路径。 - `<output_node_names>`:输出节点的名称,可以在定义模型时指定。 例如: ``` python tensorflow/python/tools/freeze_graph.py \ --input_graph=./models/graph.pb \ --input_checkpoint=./models/model.ckpt \ --output_graph=./models/frozen_graph.pb \ --output_node_names=output_node ``` 这将把`graph.pb`和`model.ckpt`转换为`frozen_graph.pb`文件。其中`output_node`是模型定义时指定的输出节点名称。 加载已经训练好的模型文件: 要加载已经训练好的模型文件,你需要使用Tensorflow的`tf.Session()`来创建一个会话,并使用`tf.train.import_meta_graph()`方法将模型的MetaGraph文件导入到当前的计算中。然后,你可以使用`tf.get_default_graph()`方法获取默认的计算,并使用`get_tensor_by_name()`方法获取模型中的张量。 以下是一个加载已经训练好的模型文件的示例代码: ``` import tensorflow as tf # 创建一个会话 sess = tf.Session() # 加载MetaGraph文件 saver = tf.train.import_meta_graph('./models/model.ckpt.meta') # 恢复变量 saver.restore(sess, './models/model.ckpt') # 获取默认计算 graph = tf.get_default_graph() # 获取模型中的张量 input_tensor = graph.get_tensor_by_name('input:0') output_tensor = graph.get_tensor_by_name('output:0') ``` 在这个例子中,我们使用`saver.restore()`方法恢复了模型的变量,然后获取模型中的`input`和`output`张量。这里`input`和`output`是在定义模型时所命名的张量名称。
评论 34
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值