- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试生成我自己的图像数据集以在单个 GPU 上使用 Tensorflow Dataset API 测量推理性能
resolutions = [
(2048, 1080)
]
def generate_image(size, channels):
image_value = random.random()
image_shape = [1, size[1], size[0], channels]
return tf.constant(
value=image_value,
shape=image_shape,
dtype=tf.float32)
def generate_single_input(size):
source = generate_image(size, 3)
target = generate_image(size, 3)
return source, target
def input_generator_fn():
for res in resolutions:
for i in range(10):
yield generate_single_input(res)
def benchmark():
...
ds = tf.data.Dataset.from_generator(
generator=input_generator_fn,
output_types=(tf.float32, tf.float32),
output_shapes=(tf.TensorShape([1, 1080, 2048, 3]),
tf.TensorShape([1, 1080, 2048, 3])))
iterator = ds.make_one_shot_iterator()
next_record = iterator.get_next()
inputs = next_record[0]
outputs = next_record[1]
predictions = {
'input_images': inputs
'output_images': outputs
}
session = tf.Session()
with session:
tf.global_variables_initializer()
for res in resolutions:
for i in range(10):
session.run(predictions)
.....
但我在运行后观察到以下异常:
2018-04-06 13:38:44.050448: W tensorflow/core/framework/op_kernel.cc:1198] Invalid argument: ValueError: setting an array element with a sequence.
2018-04-06 13:38:44.050581: W tensorflow/core/framework/op_kernel.cc:1198] Invalid argument: ValueError: setting an array element with a sequence.
[[Node: PyFunc = PyFunc[Tin=[DT_INT64], Tout=[DT_FLOAT, DT_FLOAT], token="pyfunc_1"](arg0)]]
Traceback (most recent call last):
File "tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1350, in _do_call
return fn(*args)
File "tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1329, in _run_fn
status, run_metadata)
File "tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 473, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: ValueError: setting an array element with a sequence.
[[Node: PyFunc = PyFunc[Tin=[DT_INT64], Tout=[DT_FLOAT, DT_FLOAT], token="pyfunc_1"](arg0)]]
[[Node: IteratorGetNext = IteratorGetNext[output_shapes=[[1,1080,2048,3], [1,1080,2048,3]], output_types=[DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](OneShotIterator)]]
最佳答案
简而言之,原因是 from_generator 可以展平 NumPy 数组,但不能展平 Tensor。
这是一个较短的代码,可以重现错误:
import tensorflow as tf
import numpy as np
print(tf.__version__)
def g():
img = tf.random_uniform([3])
# img = np.random.rand(3)
# img = tf.convert_to_tensor(img)
yield img
dataset = tf.data.Dataset.from_generator(g, tf.float64, tf.TensorShape([3]))
iterator = dataset.make_one_shot_iterator()
next_iterator = iterator.get_next()
sess = tf.Session()
sess.run(next_iterator)
1.14 版本中的错误信息很有帮助。 (确切的代码行会因版本不同而有所不同,但我检查过我使用的1.12和1.13原因是一样的。)
InvalidArgumentError: TypeError: `generator` yielded an element that could not be converted to the expected type. The expected type was float64, but the yielded element was Tensor("random_uniform:0", shape=(3,), dtype=float32).
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 530, in generator_py_func
ret, dtype=dtype.as_numpy_dtype))
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/script_ops.py", line 169, in _convert
result = np.asarray(value, dtype=dtype, order="C")
File "/usr/local/lib/python3.6/dist-packages/numpy/core/numeric.py", line 538, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.
当生成的元素是 Tensor 时,from_generator 会将其展平为 output_types
。转换功能不起作用。
要解决此问题,只需在生成器生成张量时不要使用 from_generator
即可。您可以使用 from_tensors
或 from_tensor_slices
。
img = tf.random_uniform([3])
dataset = tf.data.Dataset.from_tensors(img).repeat()
iterator = dataset.make_initializable_iterator()
next_iterator = iterator.get_next()
sess = tf.Session()
sess.run(iterator.initializer)
sess.run(next_iterator)
关于python - 使用 tensorflow.data.Dataset.from_generator 时出现 InvalidArgumentError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49691315/
我正在尝试使用 TensorFlow Dataset API 读取 HDF5 文件,使用 from_generator 方法。一切正常,除非批量大小不能平均分配给事件数。我不太明白如何使用 API 进
我正在尝试使用生成器训练估算器,但我想为该估算器提供每次迭代的样本包。我显示代码: def _generator(): for i in range(100): feats = np.ran
我已经为多输入nn编写了一个生成器,但是在使用tf.data.Dataset.from_generator()函数时出现错误,所有数据都在numpy中,其中:输入1的形状为(16,100,223,3)
我有一个不平凡的输入管道, from_generator 非常适合... dataset = tf.data.Dataset.from_generator(complex_img_label_gene
这是重复我在尝试使用生成器动态生成/提供训练数据时遇到的问题的玩具代码。 def makeRand(): yield np.random.rand(1) dataset = tf.data.D
我想使用通过 from_generator 构建的 TensorFlow 数据集来访问格式化文件。大多数一切正常,除了我不知道如何在生成器用完数据时停止数据集迭代器(当您超出范围时,生成器只会永远返回
我有一个包含各种尺寸图像的文件夹。我需要将它们全部调整为 (160,120) 并创建一个 tf.data.Dataset。为此,我使用了 tf.data.Dataset.from_generator。
我想创建一个 tf.data.Dataset.from_generator(...) 数据集。我需要传入一个 Python generator。 我想将先前数据集的属性传递给生成器,如下所示: dat
我正在根据需要尝试使用 tensorflow 的 nightly 1.4 Dataset.from_generator将一些可变长度的数据集拼在一起。这个简单的代码(来自 here 的想法): imp
我想使用 from_generator() 函数创建一些 tf.data.Dataset。我想向生成器函数 (raw_data_gen) 发送一个参数。这个想法是生成器函数将根据发送的参数产生不同的数
我正在尝试生成我自己的图像数据集以在单个 GPU 上使用 Tensorflow Dataset API 测量推理性能 resolutions = [ (2048, 1080) ] def ge
在生产环境中,我有来自 N 个生产者的数据,这些数据必须通过网络。我在 parallelising tf.data.Dataset.from_generator 上找到了这条评论这真的描述了我想要的东
我有一堆 JSON 数组文件(准确地说是 AVRO),每个文件都会产生多个样本来训练 Keras 模型。使用来自 @GPhilo 的想法来自@jsimsa ,我能够想出这个来并行化我的输入管道。无法弄
我需要遍历大量图像文件并将数据提供给 tensorflow。我创建了一个 Dataset由生成器函数返回,该函数将文件路径名生成为字符串,然后使用 map 将字符串路径转换为图像数据。 .但它失败了,
tf.data 有一个 from_generator初始值设定项,它似乎不可扩展。来自官方指南 Caution: While this is a convienient approach it has
我正在尝试在 keras 中实现一个具有多个输入的模型: 图片 (200x200) 一些数字 (1x50) 三个一维信号(1x50000、2x100000) 为了提供该模型,我想编写一个与 tf.da
当我从生成器创建 tf 数据集并尝试运行 tf2.0 代码时,它会向我发出贬值消息警告。 代码: import tensorflow as tf from tensorflow.keras.layer
该脚本试图使用一个函数(在本例中为 np.sin())作为生成器,旨在最终通过管道传输到模型中进行训练(因此 tf.Session( ))。不幸的是,我不断收到错误消息: “ValueError:ge
我正在为我的训练数据使用 tensorflow 数据集 api,为 tf.data.Dataset.from_generator api 使用 input_fn 和生成器 def generator(
我正在处理一个时间序列问题,其中每个时间序列都相当长(10^3-10^4 个时间步长,并且每个时间序列的长度不同)。 对于每个序列,我可以定义一个 Python 生成器,它一次生成一个时间步长的值。我
我是一名优秀的程序员,十分优秀!