Data API Error : Cannot convert a symbolic Tensor (truediv:0) to a numpy array
Data API Error : Cannot convert a symbolic Tensor (truediv:0) to a numpy array
我有 45000 张大小为 224*224 的图像,存储为一个 numpy 数组。这个名为 source_arr
的数组的形状为 45000,224,224,它适合内存。
我想将这个数组分成训练、测试和验证数组,并使用 tf.data API.
对它们进行预处理(规范化并将灰度转换为 3 通道 RGB)
我写了一个预处理函数,如:
def pre_process(x):
#Zero centering the scaled dataset
x_norm = (x-mean_Rot_MIP)/Var_Rot_MIP
#Stack 3 channels
x_norm_3ch= np.stack((x_norm, x_norm, x_norm),axis=0)
print('Rotn MIP 3ch dim:', x_norm_3ch.shape) # (3, 224, 224)
#converting channel 1st to channel last move axis 1 to 3
x_norm_3ch = moveaxis(x_norm_3ch, 0,2)
print('Rotn MIP ch last dim: ',x_norm_3ch.shape) # (224, 224, 3)
return x_norm_3ch
X_train_cases_idx.idx
包含 source_arr 中作为训练数据一部分的图像的索引。
我已经从数据集对象中的 source_arr
中读取了相应的训练图像,例如:
X_train = tf.data.Dataset.from_tensor_slices([source_arr[i] for i in X_train_cases_idx.idx])
然后我在训练图像上应用 pre_process
函数,例如
X_train = X_train.map(pre_process)
但我收到以下错误
Traceback (most recent call last):
File "<ipython-input-37-69aa131a6944>", line 1, in <module>
X_train = X_train.map(pre_process)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 1695, in map
return MapDataset(self, map_func, preserve_cardinality=True)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 4045, in __init__
use_legacy_function=use_legacy_function)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 3371, in __init__
self._function = wrapper_fn.get_concrete_function()
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 2939, in get_concrete_function
*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\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 "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 3213, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 3075, in _create_graph_function
capture_by_value=self._capture_by_value),
File "C:\ProgramData\Anaconda3\lib\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 "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 3364, in wrapper_fn
ret = _wrapper_helper(*args)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 3299, in _wrapper_helper
ret = autograph.tf_convert(func, ag_ctx)(*nested_args)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\autograph\impl\api.py", line 258, in wrapper
raise e.ag_error_metadata.to_exception(e)
NotImplementedError: in user code:
<ipython-input-2-746b4230fbd1>:58 pre_process *
x_norm_3ch= np.stack((x_norm, x_norm, x_norm),axis=1)
<__array_function__ internals>:6 stack **
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\shape_base.py:419 stack
arrays = [asanyarray(arr) for arr in arrays]
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\shape_base.py:419 <listcomp>
arrays = [asanyarray(arr) for arr in arrays]
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\_asarray.py:138 asanyarray
return array(a, dtype, copy=False, order=order, subok=True)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py:848 __array__
" a NumPy call, which is not supported".format(self.name))
NotImplementedError: Cannot convert a symbolic Tensor (truediv:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported
我做错了什么,我该如何解决?
我在 windows 10
上使用带有 python 3.7 的 Tensorflow 2.0
正如错误消息指出的那样,您正在尝试使用 NumPy 函数来处理 TensorFlow 张量。相反,您应该使用 TensorFlow 操作。这等同于您尝试执行的操作:
def pre_process(x):
x_norm = (x - mean_Rot_MIP) / Var_Rot_MIP
# Stacking along the last dimension to avoid having to move channel axis
x_norm_3ch = tf.stack((x_norm, x_norm, x_norm), axis=-1)
return x_norm_3ch
我有 45000 张大小为 224*224 的图像,存储为一个 numpy 数组。这个名为 source_arr
的数组的形状为 45000,224,224,它适合内存。
我想将这个数组分成训练、测试和验证数组,并使用 tf.data API.
对它们进行预处理(规范化并将灰度转换为 3 通道 RGB)我写了一个预处理函数,如:
def pre_process(x):
#Zero centering the scaled dataset
x_norm = (x-mean_Rot_MIP)/Var_Rot_MIP
#Stack 3 channels
x_norm_3ch= np.stack((x_norm, x_norm, x_norm),axis=0)
print('Rotn MIP 3ch dim:', x_norm_3ch.shape) # (3, 224, 224)
#converting channel 1st to channel last move axis 1 to 3
x_norm_3ch = moveaxis(x_norm_3ch, 0,2)
print('Rotn MIP ch last dim: ',x_norm_3ch.shape) # (224, 224, 3)
return x_norm_3ch
X_train_cases_idx.idx
包含 source_arr 中作为训练数据一部分的图像的索引。
我已经从数据集对象中的 source_arr
中读取了相应的训练图像,例如:
X_train = tf.data.Dataset.from_tensor_slices([source_arr[i] for i in X_train_cases_idx.idx])
然后我在训练图像上应用 pre_process
函数,例如
X_train = X_train.map(pre_process)
但我收到以下错误
Traceback (most recent call last):
File "<ipython-input-37-69aa131a6944>", line 1, in <module>
X_train = X_train.map(pre_process)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 1695, in map
return MapDataset(self, map_func, preserve_cardinality=True)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 4045, in __init__
use_legacy_function=use_legacy_function)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 3371, in __init__
self._function = wrapper_fn.get_concrete_function()
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 2939, in get_concrete_function
*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\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 "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 3213, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 3075, in _create_graph_function
capture_by_value=self._capture_by_value),
File "C:\ProgramData\Anaconda3\lib\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 "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 3364, in wrapper_fn
ret = _wrapper_helper(*args)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 3299, in _wrapper_helper
ret = autograph.tf_convert(func, ag_ctx)(*nested_args)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\autograph\impl\api.py", line 258, in wrapper
raise e.ag_error_metadata.to_exception(e)
NotImplementedError: in user code:
<ipython-input-2-746b4230fbd1>:58 pre_process *
x_norm_3ch= np.stack((x_norm, x_norm, x_norm),axis=1)
<__array_function__ internals>:6 stack **
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\shape_base.py:419 stack
arrays = [asanyarray(arr) for arr in arrays]
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\shape_base.py:419 <listcomp>
arrays = [asanyarray(arr) for arr in arrays]
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\_asarray.py:138 asanyarray
return array(a, dtype, copy=False, order=order, subok=True)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py:848 __array__
" a NumPy call, which is not supported".format(self.name))
NotImplementedError: Cannot convert a symbolic Tensor (truediv:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported
我做错了什么,我该如何解决? 我在 windows 10
上使用带有 python 3.7 的 Tensorflow 2.0正如错误消息指出的那样,您正在尝试使用 NumPy 函数来处理 TensorFlow 张量。相反,您应该使用 TensorFlow 操作。这等同于您尝试执行的操作:
def pre_process(x):
x_norm = (x - mean_Rot_MIP) / Var_Rot_MIP
# Stacking along the last dimension to avoid having to move channel axis
x_norm_3ch = tf.stack((x_norm, x_norm, x_norm), axis=-1)
return x_norm_3ch