- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 Tensorflow 流水线中,我定义了一个 load()
函数,用于验证给定路径下是否存在特定图像文件。它看起来有点像这样:
import tensorflow as tf
def load(image_file):
if tf.io.gfile.exists(image_file):
input_image = tf.io.read_file(image_file)
# do things with input_image
return input_image
这本身就没有问题。当我稍后在设置数据集时包装这个函数时出现错误:
train_dataset = tf.data.Dataset.list_files(IMAGE_PATH)
train_dataset = train_dataset.map(load,
num_parallel_calls=tf.data.experimental.AUTOTUNE)
#...
TypeError: in converted code:
<ipython-input-22-bdfc518ba578>:13 load *
if tf.io.gfile.exists(image_file):
/home/bdavid/.conda/envs/DL_env/lib/python3.7/site-packages/tensorflow_core/python/lib/io/file_io.py:280 file_exists_v2
pywrap_tensorflow.FileExists(compat.as_bytes(path))
/home/bdavid/.conda/envs/DL_env/lib/python3.7/site-packages/tensorflow_core/python/util/compat.py:87 as_bytes
(bytes_or_text,))
TypeError: Expected binary or unicode string, got <tf.Tensor 'args_0:0' shape=() dtype=string>
问题似乎是在 EagerMode 中对 image_file
的评估,因为 tf.io.gfile.exists
需要一个字符串作为输入,而不是字符串类型的 Tensor。
我已经尝试使用 image_file.numpy()
返回字符串值,导致 AttributeError: 'Tensor' object has no attribute 'numpy'
。
我还尝试按照 this closely related question 中的建议将我的函数包装在 tf.py_function()
中,这会在执行期间导致完全相同的 TypeError
。使用 os.path.exists
而不是 tf.io.gfile.exists
当然也会出现同样的错误。
如有任何关于解决此问题的解决方法或正确方法的建议,我们将不胜感激!
最佳答案
我已经使用 map_fn 和 matching_files 创建了一个解决方法,我执行时没有任何错误。
我认为您可以尝试在您的代码中实现这种方法。
def load(image_file):
if tf.io.gfile.exists(image_file.numpy()):
input_image = tf.io.read_file(image_file)
return input_image
IMAGE_PATH = '/content/images'
# train_dataset = tf.data.Dataset.list_files(IMAGE_PATH)
tf_matching = tf.io.matching_files('/content/images/*.png')
# train_dataset = train_dataset.map(load, num_parallel_calls=tf.data.experimental.AUTOTUNE)
train_dataset = tf.map_fn(load, tf_matching)
我还包含了注释掉的代码供您比较。
您可以阅读有关我在这些链接中使用的这些函数的更多信息。
此 link 中的 TensorFlow 映射函数 引用.
此 link 中的 TensorFlow 匹配文件 引用.
关于python - 使用 Tensorflow 的 tf.io.gfile.exists 检查文件路径是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60738823/
在 Tensorflow(从 v1.2.1 开始)中,似乎有(至少)两个并行 API 来构建计算图。 tf.nn 中有函数,如 conv2d、avg_pool、relu、dropout,tf.laye
我正在处理眼睛轨迹数据和卷积神经网络。我被要求使用 tf.reduce_max(lastconv, axis=2)代替 MaxPooling 层和 tf.reduce_sum(lastconv,axi
TensorFlow 提供了 3 种不同的数据存储格式 tf.train.Feature .它们是: tf.train.BytesList tf.train.FloatList tf.train.In
我正在尝试为上下文强盗问题 (https://medium.com/emergent-future/simple-reinforcement-learning-with-tensorflow-part
我在使用 Tensorflow 时遇到问题: 以下代码为卷积 block 生成正确的图: def conv_layer(self, inputs, filter_size = 3, num_filte
我正在将我的训练循环迁移到 Tensorflow 2.0 API .在急切执行模式下,tf.GradientTape替换 tf.gradients .问题是,它们是否具有相同的功能?具体来说: 在函数
tensorflow 中 tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)) 的目的是什么? 更多上下文:
我一直在努力学习 TensorFlow,我注意到不同的函数用于相同的目标。例如,为了平方变量,我看到了 tf.square()、tf.math.square() 和 tf.keras.backend.
我正在尝试使用自动编码器开发图像着色器。有 13000 张训练图像。如果我使用 tf.data,每个 epoch 大约需要 45 分钟,如果我使用 tf.utils.keras.Sequence 大约
我尝试按照 tensorflow 教程实现 MNIST CNN 神经网络,并找到这些实现 softmax 交叉熵的方法给出了不同的结果: (1) 不好的结果 softmax = tf.nn.softm
其实,我正在coursera上做deeplearning.ai的作业“Art Generation with Neural Style Transfer”。在函数 compute_layer_styl
训练神经网络学习“异或” 我正在尝试使用“批量归一化”,我创建了一个批量归一化层函数“batch_norm1”。 import tensorflow as tf import nump
我正在尝试协调来自 TF“图形和 session ”指南以及 TF“Keras”指南和 TF Estimators 指南的信息。现在在前者中它说 tf.Session 使计算图能够访问物理硬件以执行图
我正在关注此处的多层感知器示例:https://github.com/aymericdamien/TensorFlow-Examples我对函数 tf.nn.softmax_cross_entropy
回到 TensorFlow = 2.0 中消失了。因此,像这样的解决方案...... with tf.variable_scope("foo"): with tf.variable_scope
我按照官方网站中的步骤安装了tensorflow。但是,在该网站中,作为安装的最后一步,他们给出了一行代码来“验证安装”。但他们没有告诉这段代码会给出什么输出。 该行是: python -c "imp
代码: x = tf.constant([1.,2.,3.], shape = (3,2,4)) y = tf.constant([1.,2.,3.], shape = (3,21,4)) tf.ma
我正在尝试从 Github 训练一个 3D 分割网络.我的模型是用 Keras (Python) 实现的,这是一个典型的 U-Net 模型。模型,总结如下, Model: "functional_3"
我正在使用 TensorFlow 2。我正在尝试优化一个函数,该函数使用经过训练的 tensorflow 模型(毒药)的损失。 @tf.function def totalloss(x): x
试图了解 keras 优化器中的 SGD 优化代码 (source code)。在 get_updates 模块中,我们有: # momentum shapes = [K.int_shape(p) f
我是一名优秀的程序员,十分优秀!