- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试构建一个 tensorflow 模型 - 我在其中使用 tf.py_func 在普通 python 代码中创建部分代码。问题是当我将模型保存到 .pb 文件时,.pb 文件本身非常小并且不包含 py_func:0 张量。当我尝试从 .pb 文件加载和运行模型时,出现此错误:get ValueError: callback pyfunc_0 is not found。
当我不保存和加载为 .pb 文件时它工作
有人能帮忙吗?这对我来说非常重要,让我度过了几个不眠之夜。
model_version = "465555564"
tensorboard = TensorBoard(log_dir='./logs', histogram_freq = 0, write_graph = True, write_images = False)
sess = tf.Session()
K.set_session(sess)
K.set_learning_phase(0)
def my_func(x):
some_function
input = tf.placeholder(tf.float32)
y = tf.py_func(my_func, [input], tf.float32)
prediction_signature = tf.saved_model.signature_def_utils.predict_signature_def({"inputs": input}, {"prediction": y})
builder = saved_model_builder.SavedModelBuilder('./'+model_version)
legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')
builder.add_meta_graph_and_variables(
sess, [tag_constants.SERVING],
signature_def_map={
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:prediction_signature,
},
legacy_init_op=legacy_init_op)
builder.save()
最佳答案
有一种使用tf.py_func
保存 TF 模型的方法,但您必须不使用SavedModel
。
TF 有 2 个级别的模型保存:检查点和 SavedModels
。参见 this answer有关更多详细信息,但请在此处引用:
- A checkpoint contains the value of (some of the) variables in a TensorFlow model. It is created by a
Saver
. To use a checkpoint, you need to have a compatible TensorFlowGraph
, whoseVariable
s have the same names as theVariable
s in the checkpoint.SavedModel
is much more comprehensive: It contains a set ofGraph
s (MetaGraphs
, in fact, saving collections and such), as well as a checkpoint which is supposed to be compatible with theseGraph
s, and any asset files that are needed to run the model (e.g. Vocabulary files). For eachMetaGraph
it contains, it also stores a set of signatures. Signatures define (named) input and output tensors.
tf.py_func
操作 不能 用 SavedModel
保存(在 this page in the docs 上注明),这是您尝试做的这里。这是有充分理由的。 SavedModel
应该完全独立于原始代码,能够以可以反序列化它的任何其他语言加载。这允许模型通过 ML Engine 之类的东西加载。 ,这可能是用 C++ 或类似语言编写的。问题是它不能序列化任意 Python 代码,所以 py_func
是行不通的。
您可以通过使用检查点来解决这个问题,只要您愿意留在 Python 中即可。您将无法获得 SavedModel
提供的独立性。您可以在使用 tf.train.Saver
训练后保存检查点,然后在新的 Session
中重新构建整个图并使用该 加载它节省程序
。甚至还有一种在 ML Engine 中使用该代码的方法,它过去专门用于 SavedModel
。您可以使用 custom prediction routines回避对 SavedModel
的需求。
有关保存/恢复模型的更多信息,请参阅 the docs .
关于python - TensorFlow:将 py_func 保存到 .pb 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56233463/
我正在尝试使用 python 函数“IoU”作为 keras 损失函数,使用tensorflow.py_func,如下所示: def loss_fn(label, pred): return
这是在 tensorflow 中实现的 Faster R-CNN。 proposal_layer是implement通过 python 我很好奇梯度是否可以通过 tf.py_func 权重和偏差不断变
我目前正在使用 Tensorflow 开发四元数神经网络(我想使用 GPU)。 TensorFlow 不支持四元数,但是您可以将 than 表示为 4x4 实矩阵,因此可以在 TensorFlow 中
我有一个关于 tf.py_func 函数的简单问题。 我有一个形状为 (1,224,224,3) 的图像张量 my_img。为了测试 py_func,我将张量提供给 python 函数 return_
Python 版本 = 3.6.3Tensorflow 版本 = 1.3.0 我曾在 Keras 中工作,但现在正尝试直接在 TensorFlow 中工作。 我正在尝试实现 Keras 的 fit_g
根据文档,我可以使用 tf.py_func定义我自己的操作。它将接收 Numpy 数组并且必须返回 Numpy 数组。它没有明确写在文档中,但我从中得出结论,它将绑定(bind)到 CPU 设备? 如
我正在尝试在 TensorFlow 中创建一个简单的神经网络。唯一棘手的部分是我有一个使用 py_func 实现的自定义操作。 。当我传递 py_func 的输出时到 Dense层,TensorFlo
我正在尝试对 hdf5 文件中的数据进行排队。由于 Tensorflow 不支持 hdf5,因此我创建了一个 python 函数,该函数从 hdf5 文件中读取示例,并在到达文件末尾时引发 tf.er
我在使用 tensorflow 函数 py_func 时遇到一些效率问题。 上下文 在我的项目中,我有一批大小为 [? max_items m].第一个维度设置为 ? 因为它是一个动态形状(为自定义
我正在尝试使用 tf.py_func 和 tf.RegisterGradient 设置自定义渐变。具体来说,我正在尝试采用特征值的梯度 w.r.t 它的拉普拉斯算子。我得到了基本的工作,其中我的 py
我正在使用 tf.data.Dataset 编写输入管道.我想使用 python 代码来加载和转换我的样本,该代码返回一个张量字典。不幸的是,我不知道如何将其定义为传递给 tf.py_func 的输出
在使用 tf.py_func 实现在 for 循环中跨迭代共享变量的相同简单功能时,我观察到 Numpy 和纯 Tensorflow 的不同行为。 . 让我们从纯 Numpy 版本开始: def my
我尝试构建一个 tensorflow 模型 - 我在其中使用 tf.py_func 在普通 python 代码中创建部分代码。问题是当我将模型保存到 .pb 文件时,.pb 文件本身非常小并且不包含
我正在尝试使用自定义 py_func enqueue_op与 TensorFlow RandomShuffleQueue和QueueRunner 。我对TensorFlow很陌生并且很困惑。这是我现在
要重现我的问题,请先尝试这个(使用 py_func 进行映射): import tensorflow as tf import numpy as np def image_parser(image_n
我有一个保存的图形定义,它是用 tf.train.import_meta_graph 导入的。该图包含不可序列化的 py_func 操作。我可以在不从头开始构建图形的情况下定义 python 函数并将
我需要在 python 中编写一个自定义的 Op,它将根据模型和另一个将更新模型的 op 生成输出。在下面的示例代码中,我有一个非常简单的缩放器模型,w(但实际上它将是一个 nxm 矩阵)。我想出了如
我已经使用 numpy 实现了自定义池层,如下所示: def pooling_np(input): #input:[batch,h,v,channel] #output:[batch,h/2,v/2,
我在数据处理中使用 tf.data.Dataset,我想用 tf.py_func 应用一些 python 代码。 顺便说一句,我发现在 tf.py_func 中,我无法返回字典。有什么办法或解决方法吗
我想编写自己的调用 python 函数的 Keras 层。所以我遇到了 tf.py_func ,它实际上应该完成这项工作。 我尝试了以下操作:在 MyLayer 类的调用方法中,我将自定义 pytho
我是一名优秀的程序员,十分优秀!