数据API错误:无法将符号张量truediv:0转换为numpy数组

问题描述

我有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)
     #converting  channel 1st to channel last move axis 1 to 3
     x_norm_3ch = moveaxis(x_norm_3ch,2) 
     print('Rotn MIP ch last dim: ',x_norm_3ch.shape)  # (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,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,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