这些天来,我一直在努力寻找有关在TPU支持下部署TF模型的错误。
我可以在不运行TPU支持的情况下获得模型,但是一旦启用量化功能,我就会迷路。
我处于以下情况:
最后一点,我使用了TFLiteConverter的Python API。生成功能性tflite模型的脚本是
import tensorflow as tf
graph_def_file = 'frozen_model.pb'
inputs = ['dense_input']
outputs = ['dense/BiasAdd']
converter = tf.lite.TFLiteConverter.from_frozen_graph(graph_def_file, inputs, outputs)
converter.inference_type = tf.lite.constants.FLOAT
input_arrays = converter.get_input_arrays()
converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
tflite_model = converter.convert()
open('model.tflite', 'wb').write(tflite_model)
Run Code Online (Sandbox Code Playgroud)
这告诉我,到目前为止我的方法似乎还可以。现在,如果我想使用Coral TPU棒,就必须对我的模型进行量化(在训练过程中考虑了这一点)。我要做的就是修改转换器脚本。我认为我必须将其更改为
import tensorflow as tf
graph_def_file = 'frozen_model.pb'
inputs = ['dense_input']
outputs = ['dense/BiasAdd']
converter = tf.lite.TFLiteConverter.from_frozen_graph(graph_def_file, inputs, outputs)
converter.inference_type = tf.lite.constants.QUANTIZED_UINT8 ## Indicates TPU compatibility
input_arrays = converter.get_input_arrays()
converter.quantized_input_stats = {input_arrays[0]: (0., 1.)} ## mean, std_dev
converter.default_ranges_stats …Run Code Online (Sandbox Code Playgroud) 目前我偶然发现了变量自动编码器,并尝试使用keras使它们在MNIST上运行.我在github上找到了一个教程.
我的问题涉及以下几行代码:
# Build model
vae = Model(x, x_decoded_mean)
# Calculate custom loss
xent_loss = original_dim * metrics.binary_crossentropy(x, x_decoded_mean)
kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
vae_loss = K.mean(xent_loss + kl_loss)
# Compile
vae.add_loss(vae_loss)
vae.compile(optimizer='rmsprop')
Run Code Online (Sandbox Code Playgroud)
为什么使用add_loss而不是将其指定为编译选项?vae.compile(optimizer='rmsprop', loss=vae_loss)似乎没有工作的东西 ,并抛出以下错误:
ValueError: The model cannot be compiled because it has no loss to optimize.
Run Code Online (Sandbox Code Playgroud)
这个函数和自定义丢失函数有什么区别,我可以添加它作为Model.fit()的参数?
提前致谢!
PS:我知道在github上存在几个与此有关的问题,但大多数问题都是开放的,没有注释.如果已经解决,请分享链接!
编辑: 我删除了向模型添加损失的行,并使用了编译函数的loss参数.它现在看起来像这样:
# Build model
vae = Model(x, x_decoded_mean)
# Calculate custom loss
xent_loss = original_dim …Run Code Online (Sandbox Code Playgroud) 我正在尝试让 TensorFlow Lite 示例在配备 ARM Cortex-A72 处理器的机器上运行。不幸的是,由于缺少有关如何使用 C++ API 的示例,我无法部署测试模型。我将尝试解释到目前为止我所取得的成就。
创建 tflite 模型
我创建了一个简单的线性回归模型并对其进行了转换,它应该近似于函数f(x) = 2x - 1。我从一些教程中得到了这个代码片段,但我再也找不到了。
import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.contrib import lite
model = keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')
xs = np.array([ -1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([ -3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)
model.fit(xs, ys, epochs=500)
print(model.predict([10.0]))
keras_file = 'linear.h5'
keras.models.save_model(model, keras_file)
converter = lite.TocoConverter.from_keras_model_file(keras_file)
tflite_model = converter.convert()
open('linear.tflite', 'wb').write(tflite_model)
Run Code Online (Sandbox Code Playgroud)
这将创建一个名为 …
我有一个 TensorFlow 模型,我想将其转换为 tflite 模型,该模型将部署在 ARM64 平台上。
恰好是我的模型的两个操作(RandomStandardNormal、Softplus)似乎需要自定义实现。由于执行时间不是那么重要,我决定采用使用扩展运行时间的混合模型。我通过以下方式转换它:
graph_def_file = './model/frozen_model.pb'
inputs = ['eval_inputs']
outputs = ['model/y']
converter = tf.lite.TFLiteConverter.from_frozen_graph(graph_def_file, inputs, outputs)
converter.target_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_file_name = 'vae_' + str(tf.__version__) + '.tflite'
tflite_model = converter.convert()
open(tflite_file_name, 'wb').write(tflite_model)
Run Code Online (Sandbox Code Playgroud)
这奏效了,我最终得到了一个看似有效的 tflite 模型文件。每当我尝试使用解释器加载此模型时,都会收到错误消息(我使用 Python 还是 C++ API 都没有关系):
ERROR: Regular TensorFlow ops are not supported by this interpreter. Make sure you invoke the Flex delegate before inference.
ERROR: Node number 4 (FlexSoftplus) failed to prepare.
Run Code Online (Sandbox Code Playgroud)
我很难在 tf 网站上找到有关如何为这两个 API 调用 Flex 委托的文档。我偶然发现了一个头文件(“tensorflow/lite/delegates/flex/delegate_data.h”),它似乎与这个问题有关,但将它包含在我的 …
我目前正在一个稍大的 TensorFlow 项目中工作,并尝试像往常一样可视化网络的某些变量,即执行此工作流程
tf.summary.scalar('loss', loss)summary_op = tf.summary.merge_all()writer = tf.summary.FileWriter('PATH')并添加图表s = sess.run(summary_op)writer.add_summary(s, epoch)通常这对我有用。但这一次,我只显示了图表,当我检查事件文件时,我发现它是空的。巧合的是,我发现有人建议writer.flush()在添加我的摘要作为第 6 步后使用。这解决了我的问题。
因此,合乎逻辑的后续问题是:我必须何时以及如何使用FileWriter.flush()才能使 tensorflow 正常工作?
我目前负责让tensorflow-gpu 1.8在我的机器上工作。到目前为止,我一直在使用tf-gpu 1.2,但是由于某些必需的功能,我必须升级我的安装。
在这样做之前,我想检查是否有最佳实践。我当前的设置如下所示:
正如在tf主页上所写,我将不得不使用CUDA v9.0以及cuDNN v7.1。由于所有这些说明都是针对全新安装而不是更新,因此,我不确定是否最好先卸载旧版本。
如果您已经遇到相同的问题,请分享您的经验。谢谢!
为了能够运行TensorFlow支持本机TensorFlow操作的精简模型,libtensorflow-lite必须重新编译静态库。C++可以在此处找到执行此操作的说明。
它指出
使用 bazel 管道构建 TensorFlow Lite 库时,可以包含和启用额外的 TensorFlow ops 库,如下所示:
如有必要,通过添加 --config=monolithic 构建标志来启用单体构建。
将 TensorFlow ops 委托库依赖添加到构建依赖中:tensorflow/lite/delegates/flex:delegate。
请注意,只要委托链接到客户端库,在运行时创建解释器时将自动安装必要的 TfLiteDelegate。没有必要像其他委托类型通常需要的那样显式安装委托实例。
问题是构建静态库的标准方法是通过 shell 脚本/make(请参阅此处的文档;这是用于arm64,但也有脚本可用于x86_64)。我没有明显的方法可以tensorflow-lite通过bazel那里的构建命令进行构建和修改。
有没有人在尝试构建arm64/x86_64架构模型时成功构建了这个并且可以分享这个?我是新手,bazel找不到详细的演练。
编辑
在@jdehesa 提出的故障排除步骤之后,我能够构建libtensorflowlite.so,但遇到了另一个问题。我的应用程序构建成功,但在执行应用程序时,.so找不到该文件:
./myapp: error while loading shared libraries: libtensorflowlite.so: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)
由于其他.so文件位于可以找到的同一目录中,因此路径是正确的。此外,如果使用静态库,该应用程序也能正常工作。
为了重现该问题,我使用了tensorflow/tensorflow:devel-gpu-py3docker 构建映像(此处提供了 …
我现在正在玩标志,并在使用tf.app.run(). 下面的代码片段应该简单地打印通过命令行给出的字符串。
import tensorflow as tf
# command line flags
tf.app.flags.DEFINE_string('mystring', 'Hello World!',
'''String to print to console.''')
FLAGS = tf.app.flags.FLAGS
def main():
print(FLAGS.mystring)
if __name__ == '__main__':
tf.app.run()
Run Code Online (Sandbox Code Playgroud)
在执行过程中,抛出此错误:
回溯(最近一次调用最后一次):
File "", line 1, in runfile('/path/flags.py', wdir='/path')
文件“/home/abc/anaconda3/envs/tensorflow/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py”,第710行,运行文件execfile(文件名,命名空间)
文件“/home/abc/anaconda3/envs/tensorflow/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py”,第101行,在execfile exec(compile(f.read(), filename) , 'exec'), 命名空间)
文件“/path/flags.py”,第 19 行,在 tf.app.run() 中
文件“/home/abc/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/platform/app.py”,第126行,运行_sys.exit(main(argv))
类型错误:main() 采用 0 个位置参数,但给出了 1 个
...这很奇怪,因为我没有给 main() 一个参数。但是,如果我添加下划线def main(_):,它可以正常工作而不会出现任何错误。
我找不到描述使用下划线的文档。有人知道这里发生了什么吗?谢谢!
在Python 3中,通过定义__iter__和__next__方法,使类成为可迭代和迭代器是标准过程.但我有问题要绕过这个问题.以此示例创建一个仅生成偶数的迭代器:
class EvenNumbers:
def __init__(self, max_):
self.max_ = max_
def __iter__(self):
self.n = 0
return self
def __next__(self):
if self.n <= self.max:
result = 2 * self.n
self.n += 1
return result
raise StopIteration
instance = EvenNumbers(4)
for entry in instance:
print(entry)
Run Code Online (Sandbox Code Playgroud)
据我所知(如果我错了,请纠正我),当我创建循环时,通过调用itr = iter(instance)内部调用__iter__方法的内容来创建迭代器.这应该返回一个迭代器对象(实例是由于定义__next__,因此我可以返回self).要从中获取元素,next(itr)直到引发异常时才会调用它.
在这里我想问的是现在:是否以及如何能__iter__和__next__分离,从而使后者的功能的内容被定义别的地方?什么时候这可能有用?我知道我必须改变__iter__它以便它返回一个迭代器.
顺便说一句,这个想法来自这个网站(LINK),它没有说明如何实现这个.
tensorflow ×7
python ×5
python-3.x ×3
c++ ×2
autoencoder ×1
bazel ×1
cuda ×1
cudnn ×1
flags ×1
inference ×1
iterable ×1
iterator ×1
keras ×1
tensorboard ×1
upgrade ×1