【发布时间】:2020-06-06 09:31:28
【问题描述】:
我正在尝试在 Tensorflow 2.1 中转换 Tensor 的 shape 属性,但出现此错误:
AttributeError: 'Tensor' object has no attribute 'numpy'
我已经检查了tf.executing eagerly()的输出是True,
一点上下文:我从 TFRecords 加载 tf.data.Dataset,然后应用 map。映射函数正在尝试将数据集样本Tensor 之一的shape 属性转换为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)
这会引发错误:
dataset.map(transform, num_parallel_calls=1)
虽然这非常有效:
for img, labels in dataset.take(1):
print(img.shape.numpy())
编辑:尝试访问 img.numpy() 而不是 img.shape.numpy() 会导致变压器和上面的代码中出现相同的行为。
我检查了img_shape的类型,它是<class 'tensorflow.python.framework.ops.Tensor'>。
有人在新版本的 Tensorflow 中解决了这类问题吗?
【问题讨论】:
-
img的形状完全定义了吗?如果它的形状在任一维度中包含None,则可能会发生这种情况 -
我编辑了我的帖子以添加更多上下文。我正在使用
tf.io.decode_png解析img所以我猜这个形状是已知的,不是吗?还调用numpy()onimg而不是它的形状给了我相同的行为......奇怪的是,如果我在dataset.take()的元素中而不是在@ 中执行此操作,所有这些都不会导致错误987654344@...
标签: python numpy tensorflow tfrecord tensorflow2.x