我正试图使用tensorflow from_generator制作tensorflow数据集,我很确定我已经制作了一个运行良好的python生成器,但当我试图将其传递给from_generators时,我总是出错。这是我用来创建数据集的一段代码
def dataset_generator(X, Y):
for idx in range(X.shape[0]):
img = X[idx, :, :, :]
labels = Y[idx, :]
yield img, labels
import tensorflow as tf
ds_generator = dataset_generator(X_data, Y_data)
ds = tf.data.Dataset.from_generator(ds_generator, output_signature=(tf.TensorSpec(shape=[None, 720, 720, 3], dtype=tf.int32), tf.TensorSpec(shape=[None, 30], dtype=tf.float16)))
但当我运行它时,它总是会产生错误
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-63-af75191f4a28> in <module>
1 import tensorflow as tf
2 ds_generator = dataset_generator(X_data, Y_data)
----> 3 ds = tf.data.Dataset.from_generator(ds_generator, output_signature=(tf.TensorSpec(shape=[None, 720, 720, 3], dtype=tf.int32), tf.TensorSpec(shape=[None, 30], dtype=tf.float16)))
~/.local/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py in new_func(*args, **kwargs)
~/.local/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py in from_generator(generator, output_types, output_shapes, args, output_signature)
TypeError: `generator` must be callable.
嗨,您的gen函数的问题是,您必须通过args命令将其作为函数传递,而不是作为这样的函数传递
import tensorflow as tf
import numpy as np
# Gen Function
def dataset_generator(X, Y):
for idx in range(X.shape[0]):
img = X[idx, :, :, :]
labels = Y[idx, :]
yield img, labels
# Created random data for testing
X_data = np.random.randn(100, 720, 720, 3).astype(np.float32)
Y_data = tf.one_hot(np.random.randint(0, 30, (100, )), 30)
# Testing function
ds = tf.data.Dataset.from_generator(
dataset_generator,
args=(X_data, Y_data),
output_types=(tf.float32, tf.uint8)
)
# Get output
next(iter(ds.batch(10).take(1)))