我有一个如下创建的数据集,其中image_train_path
是图像文件路径的列表,例如。[b'/content/drive/My Drive/data/folder1/im1.png', b'/content/drive/My Drive/data/folder2/im6.png',...]
. 我需要提取文件夹路径,例如,'/content/drive/My Drive/data/folder1'
然后进行一些其他操作。我尝试使用以下preprocessData
函数来执行此操作。
dataset = tf.data.Dataset.from_tensor_slices(image_train_path)
dataset = dataset.map(preprocessData, num_parallel_calls=16)
哪里preprocessData
是:
def preprocessData(images_path):
folder=tf.strings.split(images_path,'/')
foldername=tf.strings.join(tf.slice(folder,(0,),(6,)),'/')
....
但是,切片线会导致以下错误:
OperatorNotAllowedInGraphError: in user code:
<ipython-input-21-2a9827982c16>:4 preprocessData *
foldername=tf.strings.join(tf.slice(folder,(0,),(6,)),'/')
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:210 wrapper **
result = dispatch(wrapper, args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:122 dispatch
result = dispatcher.handle(args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/ragged/ragged_dispatch.py:130 handle
for elt in x:
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py:524 __iter__
self._disallow_iteration()
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py:520 _disallow_iteration
self._disallow_in_graph_mode("iterating over `tf.Tensor`")
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py:500 _disallow_in_graph_mode
" this function with @tf.function.".format(task))
OperatorNotAllowedInGraphError: iterating over `tf.Tensor` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.
我在 Tf2.4 和 tf nightly 中都试过这个。我尝试@tf.function
用tf.data.experimental.enable_debug_mode()
. 总是给出同样的错误。
我不太明白哪个部分导致了“迭代”,尽管我猜问题是切片。有没有其他方法可以做到这一点?