AttributeError: 'Tensor' object has no attribute 'numpy' in Tensorflow 2.1

Nic*_*ker 6 python numpy tensorflow tensorflow2.x

I am trying to convert the shape property of a Tensor in Tensorflow 2.1 and I get this error:

AttributeError: 'Tensor' object has no attribute 'numpy'
Run Code Online (Sandbox Code Playgroud)

I already checked that the output of tf.executing eagerly() is True,

A bit of context: I load a tf.data.Dataset from a TFRecords, then I apply a map. The maping function is trying to convert the shape property of one of the dataset sample Tensor to numpy:

def _parse_and_decode(serialized_example):
    """ parse and decode each image """
    features = tf.io.parse_single_example(
        serialized_example,
        features={
            'encoded_image': tf.io.FixedLenFeature([], tf.string),
            'kp_flat': tf.io.VarLenFeature(tf.int64),
            'kp_shape': tf.io.FixedLenFeature([3], tf.int64),
        }
    )
    image = tf.io.decode_png(features['encoded_image'], dtype=tf.uint8)
    image = tf.cast(image, tf.float32)

    kp_shape = features['kp_shape']

    kp_flat = tf.sparse.to_dense(features['kp_flat'])
    kp = tf.reshape(kp_flat, kp_shape)

    return image, kp


def read_tfrecords(records_dir, batch_size=1):
    # Read dataset from tfrecords
    tfrecords_files = glob.glob(os.path.join(records_dir, '*'))
    dataset = tf.data.TFRecordDataset(tfrecords_files)
    dataset = dataset.map(_parse_and_decode, num_parallel_calls=batch_size)
    return dataset


def transform(img, labels):
    img_shape = img.shape  # type: <class 'tensorflow.python.framework.ops.Tensor'>`
    img_shape = img_shape.numpy()  # <-- Throws the error
    # ...    

dataset = read_tfrecords(records_dir)
Run Code Online (Sandbox Code Playgroud)

This throws the error:

dataset.map(transform, num_parallel_calls=1)
Run Code Online (Sandbox Code Playgroud)

While this perfecly works:

for img, labels in dataset.take(1):
    print(img.shape.numpy())
Run Code Online (Sandbox Code Playgroud)

Edit: trying to access the img.numpy() instead of img.shape.numpy() results in the same behavior in the tranformer and the codde just above.

I checked the type of img_shape and it is <class 'tensorflow.python.framework.ops.Tensor'>.

Has anyone solved this sort of issue in new versions of Tensorflow?

Tim*_*lin 15

您代码中的问题是您不能使用.numpy()映射到 的内部函数tf.data.Datasets,因为 . numpy()是 Python 代码不是纯 TensorFlow 代码。

当您使用类似 的函数时my_dataset.map(my_function),您只能使用tf.*函数内部的my_function函数。

这不是 TensorFlow 2.x 版本的错误,而是出于性能目的在幕后如何生成静态图。

如果要在映射到数据集的函数中使用自定义 Python 代码,则必须使用 tf.py_function(), docs: https://www.tensorflow.org/api_docs/python/tf/py_function。在数据集上映射时,真的没有其他方法可以混合 Python 代码和 TensorFlow 代码。

您也可以咨询此问题以获取更多信息;这是我几个月前问的确切问题:对于自定义 Python 代码,是否有替代 tf.py_function() 的方法?