我正在尝试使用单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)]]
答案 0 :(得分:0)
你有没有想到这个?
我遇到了完全相同类型的问题,我的问题是生成器与我输入output_shapes之间的维度不匹配。
另外看你的代码我相信你必须提供有效的数据,例如numpy数组,而不是TensorFlow常量。
答案 1 :(得分:0)
简而言之,原因是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.
当生成的元素是张量时,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)