- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试使用 TensorFlow 的 C++ API 在 iOS 上运行我的模型。型号是SavedModel
保存为 .pb
文件。但是,请调用 Session::Run()
导致错误:
"Invalid argument: Session was not created with a graph before Run()!"
with tf.Session() as sess:
tf.saved_model.loader.load(sess, ['serve'], '/path/to/model/export')
result = sess.run(['OutputTensorA:0', 'OutputTensorB:0'], feed_dict={
'InputTensorA:0': np.array([5000.00] * 1000).reshape(1, 1000),
'InputTensorB:0': np.array([300.00] * 1000).reshape(1, 1000)
})
print(result[0])
print(result[1])
tensorflow::Input::Initializer input_a(5000.00, tensorflow::TensorShape({1, 1000}));
tensorflow::Input::Initializer input_b(300.00, tensorflow::TensorShape({1, 1000}));
tensorflow::Session* session_pointer = nullptr;
tensorflow::SessionOptions options;
tensorflow::Status session_status = tensorflow::NewSession(options, &session_pointer);
std::cout << session_status.ToString() << std::endl; // prints OK
std::unique_ptr<tensorflow::Session> session(session_pointer);
tensorflow::GraphDef model_graph;
NSString* model_path = FilePathForResourceName(@"saved_model", @"pb");
PortableReadFileToProto([model_path UTF8String], &model_graph);
tensorflow::Status session_init = session->Create(model_graph);
std::cout << session_init.ToString() << std::endl; // prints OK
std::vector<tensorflow::Tensor> outputs;
tensorflow::Status session_run = session->Run({{"InputTensorA:0", input_a.tensor}, {"InputTensorB:0", input_b.tensor}}, {"OutputTensorA:0", "OutputTensorB:0"}, {}, &outputs);
std::cout << session_run.ToString() << std::endl; // Invalid argument: Session was not created with a graph before Run()!
FilePathForResourceName
和
PortableReadFileToProto
取自 TensorFlow iOS 示例
here .
最佳答案
这里的主要问题是您将图形导出到 SavedModel
在 Python 中,然后将其作为 GraphDef
读入在 C++ 中。虽然两者都有 .pb
扩展和相似,它们不等价。
正在发生的事情是您正在阅读 SavedModel
与 PortableReadFileToProto()
它失败了,留下一个空指针( model_graph
)到 GraphDef
目的。所以在执行 PortableReadFileToProto()
之后, model_graph
仍然是空的,但有效,GraphDef
,这就是为什么错误说 Session 不是在 Run() 之前用图形创建的。 session->Create()
成功是因为您成功地创建了一个带有空图的 session 。
检查是否PortableReadFileToProto()
的方法失败是检查其返回值。它返回一个 bool 值,如果读取图形失败,它将为 0。如果您想在此处获得描述性错误,请使用 ReadBinaryProto()
.判断读取图形是否失败的另一种方法是检查 model_graph.node_size()
的值。 .如果这是 0,那么您有一个空图并且读取它失败。
虽然您可以使用 TensorFlow 的 C API 在 SavedModel
上执行推理通过使用 TF_LoadSessionFromSavedModel()
和 TF_SessionRun()
,推荐的方法是使用 freeze_graph.py
将图形导出到卡住模型。或写信给 GraphDef
使用 tf.train.write_graph()
.我将演示使用 tf.train.write_graph()
导出的模型的成功推理:
在 Python 中:
# Build graph, call it g
g = tf.Graph()
with g.as_default():
input_tensor_a = tf.placeholder(dtype=tf.int32, name="InputTensorA")
input_tensor_b = tf.placeholder(dtype=tf.int32, name="InputTensorB")
output_tensor_a = tf.stack([input_tensor_a], name="OutputTensorA")
output_tensor_b = tf.stack([input_tensor_b], name="OutputTensorB")
# Save graph g
with tf.Session(graph=g) as sess:
sess.run(tf.global_variables_initializer())
tf.train.write_graph(
graph_or_graph_def=sess.graph_def,
logdir='/path/to/export',
name='saved_model.pb',
as_text=False
)
using namespace tensorflow;
using namespace std;
NSMutableArray* predictions = [NSMutableArray array];
Input::Initializer input_tensor_a(1, TensorShape({1}));
Input::Initializer input_tensor_b(2, TensorShape({1}));
SessionOptions options;
Session* session_pointer = nullptr;
Status session_status = NewSession(options, &session_pointer);
unique_ptr<Session> session(session_pointer);
GraphDef model_graph;
string model_path = string([FilePathForResourceName(@"saved_model", @"pb") UTF8String]);
Status load_graph = ReadBinaryProto(Env::Default(), model_path, &model_graph);
Status session_init = session->Create(model_graph);
cout << "Session creation Status: " << session_init.ToString() << endl;
cout << "Number of nodes in model_graph: " << model_graph.node_size() << endl;
cout << "Load graph Status: " << load_graph.ToString() << endl;
vector<pair<string, Tensor>> feed_dict = {
{"InputTensorA:0", input_tensor_a.tensor},
{"InputTensorB:0", input_tensor_b.tensor}
};
vector<Tensor> outputs;
Status session_run = session->Run(feed_dict, {"OutputTensorA:0", "OutputTensorB:0"}, {}, &outputs);
[predictions addObject:outputs[0].scalar<int>()];
[predictions addObject:outputs[1].scalar<int>()];
Status session_close = session->Close();
1.3
通过在您的机器上克隆 repo 并运行
tensorflow/contrib/makefile/build_all_ios.sh
从根
tensorflow-1.3.0
目录。如果您使用
TensorFlow-experimental
,推理不太可能适用于自定义的非固定模型。 Pod 喜欢这些例子。一旦你使用
build_all_ios.sh
构建了一个静态库,您需要在您的
.xcconfig
中链接它按照说明操作
here .
OpKernel ('op: "[operation]" device_type: "CPU"') for unknown op: [operation]
No OpKernel was registered to support Op '[operation]' with these attrs. Registered devices: [CPU], Registered kernels: [...]
.cc
文件来自
tensorflow/core/ops
或
tensorflow/core/kernels
对应的操作(或密切相关的操作)不在
tf_op_files.txt
中文件在
tensorflow/contrib/makefile
.您必须找到
.cc
包含
REGISTER_OP("YourOperation")
并将其添加到
tf_op_files.txt
.您必须通过运行
tensorflow/contrib/makefile/build_all_ios.sh
来重建再次。
.cc
相应操作的文件在您的
tf_op_files.txt
中文件,但是您为操作提供了它 (a) 不支持或 (b) 被剥离以减小构建大小的数据类型。
tf.float64
在你的模型的实现中,这被导出为
TF_DOUBLE
在您的
.pb
文件,大多数操作不支持此功能。使用
tf.float32
代替
tf.float64
然后使用
tf.train.write_graph()
重新保存您的模型.
__ANDROID_TYPES_SLIM__
在位于
tensorflow/contrib/makefile
的 makefile 中或替换为
__ANDROID_TYPES_FULL__
然后重建。
关于c++ - iOS : "Invalid argument: Session was not created with a graph before Run()!" 上的 TensorFlow C++ 推理错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46201109/
我想将模型及其各自训练的权重从 tensorflow.js 转换为标准 tensorflow,但无法弄清楚如何做到这一点,tensorflow.js 的文档对此没有任何说明 我有一个 manifest
我有一个运行良好的 TF 模型,它是用 Python 和 TFlearn 构建的。有没有办法在另一个系统上运行这个模型而不安装 Tensorflow?它已经经过预训练,所以我只需要通过它运行数据。 我
当执行 tensorflow_model_server 二进制文件时,它需要一个模型名称命令行参数,model_name。 如何在训练期间指定模型名称,以便在运行 tensorflow_model_s
我一直在 R 中使用标准包进行生存分析。我知道如何在 TensorFlow 中处理分类问题,例如逻辑回归,但我很难将其映射到生存分析问题。在某种程度上,您有两个输出向量而不是一个输出向量(time_t
Torch7 has a library for generating Gaussian Kernels在一个固定的支持。 Tensorflow 中有什么可比的吗?我看到 these distribu
在Keras中我们可以简单的添加回调,如下所示: self.model.fit(X_train,y_train,callbacks=[Custom_callback]) 回调在doc中定义,但我找不到
我正在寻找一种在 tensorflow 中有条件打印节点的方法,使用下面的示例代码行,其中每 10 个循环计数,它应该在控制台中打印一些东西。但这对我不起作用。谁能建议? 谢谢,哈米德雷萨, epsi
我想使用 tensorflow object detection API 创建我自己的 .tfrecord 文件,并将它们用于训练。该记录将是原始数据集的子集,因此模型将仅检测特定类别。我不明白也无法
我在 TensorFlow 中训练了一个聊天机器人,想保存模型以便使用 TensorFlow.js 将其部署到 Web。我有以下内容 checkpoint = "./chatbot_weights.c
我最近开始学习 Tensorflow,特别是我想使用卷积神经网络进行图像分类。我一直在看官方仓库中的android demo,特别是这个例子:https://github.com/tensorflow
我目前正在研究单图像超分辨率,并且我设法卡住了现有的检查点文件并将其转换为 tensorflow lite。但是,使用 .tflite 文件执行推理时,对一张图像进行上采样所需的时间至少是使用 .ck
我注意到 tensorflow 的 api 中已经有批量标准化函数。我不明白的一件事是如何更改训练和测试之间的程序? 批量归一化在测试和训练期间的作用不同。具体来说,在训练期间使用固定的均值和方差。
我创建了一个模型,该模型将 Mobilenet V2 应用于 Google colab 中的卷积基础层。然后我使用这个命令转换它: path_to_h5 = working_dir + '/Tenso
代码取自:- http://adventuresinmachinelearning.com/python-tensorflow-tutorial/ import tensorflow as tf fr
好了,所以我准备在Tensorflow中运行 tf.nn.softmax_cross_entropy_with_logits() 函数。 据我了解,“logit”应该是概率的张量,每个对应于某个像素的
tensorflow 服务构建依赖于大型 tensorflow ;但我已经成功构建了 tensorflow。所以我想用它。我做这些事情:我更改了 tensorflow 服务 WORKSPACE(org
Tensoflow 嵌入层 ( https://www.tensorflow.org/api_docs/python/tf/keras/layers/Embedding ) 易于使用, 并且有大量的文
我正在尝试使用非常大的数据集(比我的内存大得多)训练 Tensorflow 模型。 为了充分利用所有可用的训练数据,我正在考虑将它们分成几个小的“分片”,并一次在一个分片上进行训练。 经过一番研究,我
根据 Sutton 的书 - Reinforcement Learning: An Introduction,网络权重的更新方程为: 其中 et 是资格轨迹。 这类似于带有额外 et 的梯度下降更新。
如何根据条件选择执行图表的一部分? 我的网络有一部分只有在 feed_dict 中提供占位符值时才会执行.如果未提供该值,则采用备用路径。我该如何使用 tensorflow 来实现它? 以下是我的代码
我是一名优秀的程序员,十分优秀!