“在执行图形时不允许在`tf.Tensor`上进行迭代。”同时使用numpy_function返回多个变量

时间:2020-10-29 15:58:24

标签: tensorflow2.0 tensorflow-datasets

我不确定这是不是故意的,是错误的还是我做错了。我正在尝试使用一个称为专辑库,以增强图像及其对应的关键点。但是,在遵循指南here并尝试添加关键点增强时,出现以下错误:

OperatorNotAllowedInGraphError: iterating over `tf.Tensor` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.

使用numpy_function行。是否有一种既可以返回图像又可以返回关键点的解决方法?我显然很想保留tf.data带来的所有功能,而不必在管道外进行扩充。

以下代码在我的机器上重现此错误:

import tensorflow as tf


def aug_fn(image, keypoints):
    # Augment image and keypoints here
    return image, keypoints


def process_data(data):
    image = data[0]
    keypoints = data[1]
    image, keypoints = tf.numpy_function(func=aug_fn, inp=[image, keypoints], Tout=tf.float32)
    return image, keypoints


if __name__ == '__main__':
    dummy_data = [tf.zeros((50, 50, 3))[(10, 20)]]
    dataset = tf.data.Dataset.from_tensor_slices(dummy_data)
    dataset = dataset.map(process_data)
    for s in dataset:
        print(s)

这是整个错误消息:

/home/ab/anaconda3/bin/python3.8 /snap/pycharm-community/214/plugins/python-ce/helpers/pydev/pydevd.py --multiproc --qt-support=auto --client 127.0.0.1 --port 45661 --file /home/ab/Documents/GitLab/landing-page-dl-model/test.py
pydev debugger: process 85444 is connecting

Connected to pydev debugger (build 202.7660.27)
2020-10-29 16:49:33.515315: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1
2020-10-29 16:49:35.788599: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcuda.so.1
2020-10-29 16:49:35.821091: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:982] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-10-29 16:49:35.821839: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties: 
pciBusID: 0000:01:00.0 name: GeForce GTX 1070 computeCapability: 6.1
coreClock: 1.7715GHz coreCount: 15 deviceMemorySize: 7.93GiB deviceMemoryBandwidth: 238.66GiB/s
2020-10-29 16:49:35.821870: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1
2020-10-29 16:49:35.823450: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcublas.so.10
2020-10-29 16:49:35.824841: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcufft.so.10
2020-10-29 16:49:35.825091: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcurand.so.10
2020-10-29 16:49:35.826777: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcusolver.so.10
2020-10-29 16:49:35.827672: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcusparse.so.10
2020-10-29 16:49:35.830437: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudnn.so.7
2020-10-29 16:49:35.830552: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:982] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-10-29 16:49:35.831208: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:982] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-10-29 16:49:35.831719: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1858] Adding visible gpu devices: 0
2020-10-29 16:49:35.832054: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-10-29 16:49:35.837064: I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 3392040000 Hz
2020-10-29 16:49:35.837401: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x558a0dab57b0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-10-29 16:49:35.837414: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-10-29 16:49:35.924237: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:982] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-10-29 16:49:35.924685: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x558a0db49630 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2020-10-29 16:49:35.924702: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): GeForce GTX 1070, Compute Capability 6.1
2020-10-29 16:49:35.924899: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:982] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-10-29 16:49:35.925308: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties: 
pciBusID: 0000:01:00.0 name: GeForce GTX 1070 computeCapability: 6.1
coreClock: 1.7715GHz coreCount: 15 deviceMemorySize: 7.93GiB deviceMemoryBandwidth: 238.66GiB/s
2020-10-29 16:49:35.925339: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1
2020-10-29 16:49:35.925368: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcublas.so.10
2020-10-29 16:49:35.925387: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcufft.so.10
2020-10-29 16:49:35.925404: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcurand.so.10
2020-10-29 16:49:35.925422: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcusolver.so.10
2020-10-29 16:49:35.925439: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcusparse.so.10
2020-10-29 16:49:35.925455: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudnn.so.7
2020-10-29 16:49:35.925516: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:982] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-10-29 16:49:35.925921: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:982] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-10-29 16:49:35.927635: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1858] Adding visible gpu devices: 0
2020-10-29 16:49:35.927680: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1
2020-10-29 16:49:36.255274: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1257] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-10-29 16:49:36.255309: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1263]      0 
2020-10-29 16:49:36.255321: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1276] 0:   N 
2020-10-29 16:49:36.255529: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:982] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-10-29 16:49:36.255965: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:982] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-10-29 16:49:36.256337: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1402] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 7101 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1070, pci bus id: 0000:01:00.0, compute capability: 6.1)
Traceback (most recent call last):
  File "/home/ab/anaconda3/lib/python3.8/contextlib.py", line 131, in __exit__
    self.gen.throw(type, value, traceback)
  File "/home/ab/.local/lib/python3.8/site-packages/tensorflow/python/training/tracking/tracking.py", line 178, in resource_tracker_scope
    yield
  File "/home/ab/.local/lib/python3.8/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 3371, in __init__
    self._function = wrapper_fn.get_concrete_function()
  File "/home/ab/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2938, in get_concrete_function
    graph_function = self._get_concrete_function_garbage_collected(
  File "/home/ab/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2906, in _get_concrete_function_garbage_collected
    graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
  File "/home/ab/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 3213, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
  File "/home/ab/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 3065, in _create_graph_function
    func_graph_module.func_graph_from_py_func(
  File "/home/ab/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 986, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "/home/ab/.local/lib/python3.8/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 3364, in wrapper_fn
    ret = _wrapper_helper(*args)
  File "/home/ab/.local/lib/python3.8/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 3299, in _wrapper_helper
    ret = autograph.tf_convert(func, ag_ctx)(*nested_args)
  File "/home/ab/.local/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py", line 258, in wrapper
    raise e.ag_error_metadata.to_exception(e)
tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: in user code:

    /home/ab/Documents/GitLab/landing-page-dl-model/test.py:12 process_data  *
        image, keypoints = tf.numpy_function(func=aug_fn, inp=[image, keypoints], Tout=tf.float32)
    /home/ab/.local/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:503 __iter__
        self._disallow_iteration()
    /home/ab/.local/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:499 _disallow_iteration
        self._disallow_in_graph_mode("iterating over `tf.Tensor`")
    /home/ab/.local/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:477 _disallow_in_graph_mode
        raise errors.OperatorNotAllowedInGraphError(

    OperatorNotAllowedInGraphError: iterating over `tf.Tensor` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.

python-BaseException

Process finished with exit code 130 (interrupted by signal 2: SIGINT)

0 个答案:

没有答案