tensorflow保存图和权重

TensorFlow模型冻结与部署
本文介绍如何在TensorFlow中冻结模型变量以实现单文件模型导出,包括模型的保存和加载过程,以及如何在导出的模型中使用自定义输入。
部署运行你感兴趣的模型镜像

有时候,我们需要将TensorFlow的模型导出为单个文件(同时包含模型架构定义与权重),方便在其他地方使用(如在c++中部署网络)。利用tf.train.write_graph()默认情况下只导出了网络的定义(没有权重),而利用tf.train.Saver().save()导出的文件graph_def与权重是分离的,因此需要采用别的方法。

我们知道,graph_def文件中没有包含网络中的Variable值(通常情况存储了权重),但是却包含了constant值,所以如果我们能把Variable转换为constant,即可达到使用一个文件同时存储网络架构与权重的目标。

我们可以采用以下方式冻结权重并保存网络:

import tensorflow as tf
from tensorflow.python.framework.graph_util import convert_variables_to_constants

# 构造网络
a = tf.Variable([[3],[4]], dtype=tf.float32, name='a')
b = tf.Variable(4, dtype=tf.float32, name='b')
# 一定要给输出tensor取一个名字!!
output = tf.add(a, b, name='out')

# 转换Variable为constant,并将网络写入到文件
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    # 这里需要填入输出tensor的名字
    graph = convert_variables_to_constants(sess, sess.graph_def, ["out"])
    tf.train.write_graph(graph, '.', 'graph.pb', as_text=False)

当恢复网络时,可以使用如下方式:

import tensorflow as tf
with tf.Session() as sess:
    with open('./graph.pb', 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read()) 
        output = tf.import_graph_def(graph_def, return_elements=['out:0']) 
        print(sess.run(output))

输出结果为:

[array([[ 7.],
       [ 8.]], dtype=float32)]

可以看到之前的权重确实保存了下来!!

问题来了,我们的网络需要能有一个输入自定义数据的接口啊!不然这玩意有什么用。。别急,当然有办法。

import tensorflow as tf
from tensorflow.python.framework.graph_util import convert_variables_to_constants

a = tf.Variable([[3],[4]], dtype=tf.float32, name='a')
b = tf.Variable(4, dtype=tf.float32, name='b')
input_tensor = tf.placeholder(tf.float32, name='input')
output = tf.add((a+b), input_tensor, name='out')

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    graph = convert_variables_to_constants(sess, sess.graph_def, ["out"])
    tf.train.write_graph(graph, '.', 'graph.pb', as_text=False)

用上述代码重新保存网络至graph.pb,这次我们有了一个输入placeholder,下面来看看怎么恢复网络并输入自定义数据。

import tensorflow as tf

with tf.Session() as sess:
    with open('./graph.pb', 'rb') as f: 
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read()) 
        output = tf.import_graph_def(graph_def, input_map={'input:0':4.}, return_elements=['out:0'], name='a') 
        print(sess.run(output))

输出结果为:

[array([[ 11.],
       [ 12.]], dtype=float32)]

可以看到结果没有问题,当然在input_map那里可以替换为新的自定义的placeholder,如下所示:

import tensorflow as tf

new_input = tf.placeholder(tf.float32, shape=())

with tf.Session() as sess:
    with open('./graph.pb', 'rb') as f: 
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read()) 
        output = tf.import_graph_def(graph_def, input_map={'input:0':new_input}, return_elements=['out:0'], name='a') 
        print(sess.run(output, feed_dict={new_input:4}))

看看输出,同样没有问题。

[array([[ 11.],
       [ 12.]], dtype=float32)]

另外需要说明的一点是,在利用tf.train.write_graph写网络架构的时候,如果令as_text=True了,则在导入网络的时候,需要做一点小修改。

import tensorflow as tf
from google.protobuf import text_format

with tf.Session() as sess:
    # 不使用'rb'模式
    with open('./graph.pb', 'r') as f:
        graph_def = tf.GraphDef()
        # 不使用graph_def.ParseFromString(f.read())
        text_format.Merge(f.read(), graph_def)
        output = tf.import_graph_def(graph_def, return_elements=['out:0']) 
        print(sess.run(output))

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

### 如何在 TensorFlow 中加载使用预训练模型的权重 为了在 TensorFlow 中加载并使用预训练模型的权重,可以按照以下方法操作: 对于已经下载好的预训练模型文件,可以通过指定路径来加载模型及其权重。例如,如果预先下载了一个 VGG16 模型的权重文件 `vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5` 并存储于本地磁盘上的特定目录,则可通过下面的方式创建带有自定义输入形状且不带顶部全连接层(即分类器部分)的 VGG16 实例,并应用已有的权重[^2]。 ```python import tensorflow as tf path_weights = r"D:\XX\vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5" VGG16_MODEL = tf.keras.applications.VGG16( input_shape=(224, 224, 3), include_top=False, weights=path_weights ) ``` 上述代码片段展示了如何通过设置参数 `weights` 来指向本地存在的 HDF5 文件从而完成对预训练权重的应用过程。这里需要注意的是,当指定了具体的 `.h5` 或其他格式的权重文件时,该文件应当与所使用的架构相匹配,比如这里的例子适用于 VGG16 架构下的无顶层结构版本。 另外,在实际项目开发过程中,除了直接利用官方提供的预训练模型外,还可以基于这些模型进行微调 (Fine-tuning),这通常涉及到冻结某些卷积层而仅更新新增加的部分或是解冻少量高层特征提取单元来进行更广泛的再训练以适应新的数据集需求[^3]。 #### 加载预训练模型的一般流程概述 - 准备工作:确保拥有合适的预训练权重文件; - 创建模型实例:根据所需的任务选择相应的基底网络,并决定是否保留原始的分类头 (`include_top`) 及其对应的输入尺寸 (`input_shape`); - 应用权重:通过传递给 `weights` 参数一个有效的文件路径字符串来指示框架读取外部保存下来的参数值; - 后续处理:依据具体应用场景考虑进一步调整模型配置,如修改激活函数、增加正则化项等措施提升性能表现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值