1

我有一个如下创建的数据集,其中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.functiontf.data.experimental.enable_debug_mode(). 总是给出同样的错误。

我不太明白哪个部分导致了“迭代”,尽管我猜问题是切片。有没有其他方法可以做到这一点?

4

1 回答 1

1

该函数tf.strings.join需要一个张量列表,如文档所述:

精氨酸

输入: 相同大小和 tf.string dtype的 tf.Tensor 对象列表。

tf.slice返回一个张量,然后连接函数将尝试对其进行迭代,从而导致错误。

您可以使用简单的列表理解来提供函数:

def preprocessData(images_path):
    folder=tf.strings.split(images_path,'/')
    foldername=tf.strings.join([folder[i] for i in range(6)],"/")
    return foldername
于 2021-04-08T07:38:38.633 回答