python - 打印 Tensor 时为 "args_0:0"

标签 python tensorflow tensorflow-datasets image-rotation data-augmentation

我有一个被映射到生成器中的增强函数;然而,出于某种原因,tfa.image.rotate函数导致错误。

def customGenerator(input_file_paths, dims, data_type):
    for i, file_path in enumerate(input_file_paths):
        if data_type.decode("utf-8") in ["png" or "tif"]:
            img = plt.imread((file_path.decode("utf-8")))
        elif data_type.decode("utf-8") == "npy":
            img = np.load(file_path.decode("utf-8"))
        x = resize(img[:,:,:3], dims)           
        yield x, x

def augment(image,label) :
    print('image', image)
    print('shape', image.shape)
    print('type', type(image))

    #angle = random.uniform(0, tf.constant(np.pi))
    image = tfa.image.rotate(image, tf.constant(np.pi)


train_dataset = tf.data.Dataset.from_generator(generator=customGenerator, 
                                                 output_types=(np.float32, np.float32), 
                                                 output_shapes=(dims, dims), 
                                                 args=[X_train_paths, dims, "png"])

train_dataset = train_dataset.map(augment, num_parallel_calls=AUTOTUNE)

我查看了 tfa.image.rotate 的实现其他人用过,他们的工作正常。我尝试打印 image增强函数中的变量。结果是:

print('image', image) # these lines is in the augment function, result below
print('type', type(image))
image Tensor("args_0:0", shape=(256, 256, 3), dtype=float32)
type <class 'tensorflow.python.framework.ops.Tensor'>

相比之下,当我转到其他用户的实现并打印他们的图像时,该图像未映射到数据集中。他们的print(image)print(type(image))打印出来:

image tf.Tensor(
[[[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  ...
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]

 ...

 [[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  ...
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]], shape=(256, 256, 3), dtype=float32)

type <class 'tensorflow.python.framework.ops.EagerTensor'>

当我打印 image 时,我预计它会被打印出来在 augment功能。所以我不确定发生了什么。所以有几个问题。 另外,tf.executing_eagerly()结果 True

到底是什么"args_0:0"什么意思?

图像应该在augment函数类型为 <class 'tensorflow.python.framework.ops.EagerTensor'>而不是普通张量?

有什么办法可以让"args_0:0"转换成我希望在其中打印数字数组的格式?因为我相信这会修复旋转功能

最后,如果没有,是否有更好的方法通过随机旋转来增强图像?

感谢您的宝贵时间和帮助。

最佳答案

args_0:0 是张量。参见 here

我对您的代码进行了一些更改以使其正常工作。

代码:

import tensorflow_addons as tfa
import os
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np


def customGenerator(input_file_paths, dims, data_type):
    for i, file_path in enumerate(input_file_paths):
        image = tf.io.read_file(file_path)
        image = tf.image.decode_png(image, channels = 3)
        image = tf.image.convert_image_dtype(image, tf.float32)
        image = tf.image.resize(image, [dims[0],dims[1]])
        yield image, image

def augment(image,label) :
    img = tfa.image.rotate(image, tf.constant(np.pi/8))   
    return (img, label)

X_train_paths = [os.path.join('data','img',name) for name in os.listdir('data/img')]
dims = (256,256,3)

train_dataset = tf.data.Dataset.from_generator(generator=customGenerator, 
                                                 output_types=(tf.float32, tf.float32), 
                                                 output_shapes=(dims, dims), 
                                                 args=[X_train_paths, dims, "png"])

train_dataset = train_dataset.map(augment)

遍历数据集:

for images in train_dataset:
    rotatedimg, normalimg= images[0],images[1]
    break

输出:

plt.imshow(rotatedimg)

enter image description here

plt.imshow(normalimg)

enter image description here

要记住的事情:

  1. map 功能没有急切执行。
  2. 始终在生成器函数和映射函数中使用 TensorFlow 函数,因为 TensorFlow 将这些函数作为图形的一部分执行,以加快其执行速度。参见 here
  3. 如果您使用其他函数,TensorFlow 可能无法将这些操作转换为图形,这将导致错误。

关于python - 打印 Tensor 时为 "args_0:0",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63642715/

相关文章:

python - 分布式 Tensorflow 模型并不比独立模型快

tensorflow - 如何将张量映射到 tensorflow 中的索引

Tensorflow Dataset API 如何排序 list_files?

python - Tensorflow:如何找到 tf.data.Dataset API 对象的大小

dataframe - 将 "columns"添加到 tf.data.dataset 的最佳方法

python - 通过 for 循环创建数据帧字典

python - Tensorflow 2 -Keras SAVE/LOAD 模型错误(DenseFeatures 和 DistributionLambda 层)

使用 Pdb 进行 Python 调试

python - 生成列表,允许重复并符合条件

python - 使用tensorflow对句子相似度建模