- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个旧的训练有素的 tf1.x 模型(让它成为 Model1 ),用占位符、tf.contrib 等构建。我可以通过从 tf.Session(在 tf1.x 中)中的 .ckpt 检查点恢复图形来使用这个模型。
我解决了使用 的最简单方法型号1 是导出它:
# tf1.x code
tf.saved_model.simple_save(sess, saved_Model1_path,
inputs={'input':'Placeholder:0'}, outputs={'output':'.../Sigmoid:0'})
# tf2.0 code
Model1 = tf.saved_model.load(saved_Model1_path)
out = Model1.signatures['serving_default'](tf.convert_to_tensor(data))['output'].numpy()
out = Model1.signatures['serving_default'].prune('Placeholder:0', '.../Sigmoid:0')(data)
out = Model1.prune('Placeholder:0', '.../Sigmoid:0')(data)
import os
import tensorflow as tf
assert tf.__version__[0] == '2'
class M1(tf.Module):
def __init__(self):
super(M1, self).__init__()
self.v = tf.Variable(2.)
@tf.function(input_signature=[tf.TensorSpec([], tf.float32)])
def M1_func(self, x):
return x * self.v
# build some saved_model.pb
m1 = M1()
path_1 = './save1'
path_to_save = os.path.realpath(path_1)
tf.saved_model.save(m1, path_to_save)
# load built saved_model.pb and check it works
m1 = tf.saved_model.load(path_1)
assert 6 == m1.M1_func(3.).numpy()
# build other saved_model.pb using first saved_model.pb as a part of computing graph
class M2(tf.Module):
def __init__(self):
super(M2, self).__init__()
self.run = m1
self.v = tf.Variable(3.)
@tf.function(input_signature=[tf.TensorSpec([], tf.float32)])
def M2_func(self, x):
return self.run.M1_func(x) * self.v
m2 = M2()
path_2 = './save2'
path_to_save = os.path.realpath(path_2)
tf.saved_model.save(m2, path_to_save)
m2 = tf.saved_model.load(path_2)
assert 18 == m2.M2_func(3.).numpy()
# save first saved_model.pb with tf1.x
import tensorflow as tf
assert tf.__version__[0] == '1'
inp = tf.placeholder(shape=[],dtype=tf.float32)
a = tf.Variable(1.5)
out = a*inp
sess = tf.Session()
sess.run(tf.global_variables_initializer())
assert 7.5 == out.eval({inp:5.}, sess)
path_3 = './save3'
path_to_save = os.path.realpath(path_3)
tf.saved_model.simple_save(sess, path_to_save, inputs={'input': inp}, outputs={'output': out})
import os
import tensorflow as tf
assert tf.__version__[0] == '2'
path_3 = './save3'
path_to_save = os.path.realpath(path_3)
m1 = tf.saved_model.load(path_to_save)
class M2(tf.Module):
def __init__(self):
super(M2, self).__init__()
self.run = m1.signatures['serving_default'].prune('Placeholder:0', 'mul:0')
self.v = tf.Variable(3.)
@tf.function(input_signature=[tf.TensorSpec([], tf.float32)])
def M2_func(self, x):
return self.run(x) * self.v
m2 = M2()
assert 22.5 == m2.M2_func(5.) # ofc eager execution works
# now save M2 to saved_model.pb and check it works (it does not)
path_4 = './save4'
path_to_save = os.path.realpath(path_4)
tf.saved_model.save(m2, path_to_save)
m2 = tf.saved_model.load(path_4)
m2.M2_func(5.) # error:
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value StatefulPartitionedCall/StatefulPartitionedCall/Variable
[[{{node StatefulPartitionedCall/StatefulPartitionedCall/Variable/read}}]] [Op:__inference_restored_function_body_207]
Function call stack:
restored_function_body
最佳答案
问题解决了。看看这个导出函数以及如何使用它。此函数实现接受单个输入张量名称和输出张量名称列表。
import tensorflow as tf
def export_tf1(session, in_tnsr_fullname, out_tnsrS_fullname, export_dir='./export'):
assert isinstance(in_tnsr_fullname, str)
assert all([isinstance(out_tnsr_fullname, str) for out_tnsr_fullname in out_tnsrS_fullname])
in_tnsr_name = in_tnsr_fullname.split(':')[0]
out_tnsrS_name = [out_tnsr_fullname.split(':')[0] for out_tnsr_fullname in out_tnsrS_fullname]
graph_def = tf.graph_util.convert_variables_to_constants(session, session.graph.as_graph_def(), out_tnsrS_name)
tf.reset_default_graph()
outs = tf.import_graph_def(graph_def, name="", return_elements=out_tnsrS_fullname)
g = outs[0].graph
builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
with tf.Session(graph=g) as sess:
input_signatures = {in_tnsr_name: g.get_tensor_by_name(in_tnsr_fullname)}
output_signatures = {}
for out_tnsr_name, out_tnsr_fullname in zip(out_tnsrS_name, out_tnsrS_fullname):
output_signatures[out_tnsr_name] = g.get_tensor_by_name(out_tnsr_fullname)
signature = tf.saved_model.signature_def_utils.predict_signature_def(input_signatures, output_signatures)
builder.add_meta_graph_and_variables(
sess,
[tf.saved_model.tag_constants.SERVING],
{tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature},
clear_devices=True
)
builder.save()
如何使用导出函数从 tf1_ckpt 检查点接收 .pb:
import tensorflow as tf
assert tf.__version__[0] == '1'
g = tf.get_default_graph()
sess = tf.Session(graph=g)
ckpt_tf1_path = 'some_directory/name.ckpt' # just an example
tf.train.Saver().restore(sess, ckpt_tf1_path)
input_tensor_name = 'x_tnsr:0' # just an example
out_tensor_name = 'y_tnsr:0' # just an example
export_tf1(sess, input_tensor_name, [out_tensor_name], export_dir)
如何使用 tf2.0 在 .pb 中重用来自 tf1_ckpt 的 .pb:
import tensorflow as tf
assert tf.__version__[0] == '2'
class Export(tf.Module):
def __init__(self):
super(Export, self).__init__()
tf1_saved_model_directory = 'directory/saved_model' # just an example
self.tf1_model = tf.saved_model.load(tf1_saved_model_directory)
input_tensor_name = 'x_tnsr:0' # just an example
out_tensor_name = 'y_tnsr:0' # just an example
self.tf1_model = self.tf1_model.prune(input_tensor_name, out_tensor_name)
@tf.function
def __call__(self, x):
out = self.tf1_model(x)
return out
export_dir = './saved_model'
tf.saved_model.save(Export(), export_dir)
关于python - 将 tf1.x saved_model.pb 重新保存到新的 tf2.0 saved_model.pb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59504276/
我有一个经过训练的模型 (Faster R-CNN),我使用 export_inference_graph.py 将其导出以用于推理。我试图了解创建的 frozen_inference_graph.p
我正在开发一个使用Google Protocol Buffer (protobuf)和CMake的库。该项目具有以下目录树。 MyProject/ MyProject/include/myprojec
我有一个旧的训练有素的 tf1.x 模型(让它成为 Model1 ),用占位符、tf.contrib 等构建。我可以通过从 tf.Session(在 tf1.x 中)中的 .ckpt 检查点恢复图形来
我想预先执行查询并将结果存储在@query中: BEGIN DECLARE @query VARCHAR(50); SET @table = 'top20-img-link'; SET @query
有了这个玩具数据:: df = pd.DataFrame(pd.np.random.randint(2, 9, size=(8, 3))) df.index = pd.date_range(start
我使用的是 Twisted 16.1.1 和 python 3.4。在 twisted 的 16.1.1 版文档中,there is a tutorial上面写着“from twisted.sprea
最近尝试将模型(tf1.x)转换为saved_model,关注官方migrate document .但是在我的用例中,我手中的大部分模型或 tensorflow 模型动物园通常是 pb 文件,根据
有谁知道tensorflow_inception_graph.pb的原始源代码。 真不想知道示例项目中的操作 tensorflow/example/android - 读我。 Tensorflow A
完整的错误说: cv2.error:OpenCV(4.1.0)C:\ projects \ opencv-python \ opencv \ modules \ dnn \ src \ caffe \
我已将模型保存在图表(.pb 文件)中。但现在这个模型不准确,我想开发它。我有其他数据的图片需要学习,但我不知道是否可能或如何做到这一点?结果必须是新数据pb图的修改。 最佳答案 这是个好问题。实际上
假设我有模型(tf.keras.Model): class ContextExtractor(tf.keras.Model): def __init__(self): supe
我正在尝试加载取自 https://github.com/tensorflow/models/tree/master/official/resnet 的已训练模型,但是当我尝试加载 .pb 时,我在
我计划将 .aab 上传到 Play 商店进行发布,在发布之前,我正在尝试反编译以查看在逆向工程过程中哪些数据可能会暴露给用户。 在 .aab_FILES/base/中,我看到了 assets.pb
libphonenumber 中没有phonenumber.pb.h 和phonemetadata.pb.h (CPP) 库 那么有什么办法可以找出来吗? 谢谢! 最佳答案 这些是由 Google p
我需要在 CSV 甲酸盐文件中搜索超过 PB 的数据。使用 LUCENE 建立索引后,索引文件的大小是原始文件的两倍。是否可以减少索引文件的大小??? HADOOP中如何分发LUCENE索引文
我公司的产品每年将在我们的客户站点产生数 PB 的数据。我想填满一个多 PB 的 NAS 来模拟一个已经运行了很长时间(3 个月、6 个月、一年等)的系统。我们希望在我们的软件在负载下的存储系统上运行
我在 Windows 10 机器上创建了一个 tensorflow 模型并使用以下方法保存它: model.save('myfolder') 文件夹内myfolder我得到: - saved_mode
我使用 Pytorch 创建了一个对象检测模型然后从 .pth 转换而来至 .onnx然后 .pb , 但现在我需要将其转换为 .tflite对于安卓应用程序!怎么做?这是我的第一次。 input_a
在这种情况下,大文件会发生什么? 1)Spark从NameNode获取数据位置。 Spark是否会在同一时间停止,因为根据NameNode的信息,数据大小太长? 2)Spark根据数据节点块大小对数据
我在对象检测 API 中使用 ssd_mobilenets 来训练我自己的模型,并获取 .ckpt 文件。它在我的电脑上运行良好,但现在我想在我的手机上使用该模型。所以,我需要将它转换为 .pb 文件
我是一名优秀的程序员,十分优秀!