"args_0:0" 打印 Tensor 时
"args_0:0" when printing Tensor
我有一个正在映射到生成器中的增强函数;但是,由于某种原因,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'>
当我在 augment
函数中打印 image
时,我预计它会被打印出来。所以我不确定发生了什么。所以有几个问题。
此外,tf.executing_eagerly()
导致 True
"args_0:0"
到底是什么意思?
augment
函数中的图像应该是 <class 'tensorflow.python.framework.ops.EagerTensor'>
类型而不是普通张量吗?
有没有什么方法可以让 "args_0:0"
变成我希望打印数字数组的格式?因为我相信这会修复旋转功能
最后,如果没有,有没有更好的方法通过随机旋转来增强图像?
感谢您的宝贵时间和帮助。
args_0:0
是张量。参见
我对您的代码进行了一些更改以使其正常工作。
代码:
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)
plt.imshow(normalimg)
要记住的事情:
- map函数没有急于执行
- 始终在生成器函数和映射函数中使用 TensorFlow 函数,因为 TensorFlow 将这些函数作为图形的一部分执行,以加快其执行速度。参见 here
- 如果您使用其他函数,TensorFlow 可能无法将这些操作转换为图形,这将导致错误。
我有一个正在映射到生成器中的增强函数;但是,由于某种原因,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'>
当我在 augment
函数中打印 image
时,我预计它会被打印出来。所以我不确定发生了什么。所以有几个问题。
此外,tf.executing_eagerly()
导致 True
"args_0:0"
到底是什么意思?
augment
函数中的图像应该是 <class 'tensorflow.python.framework.ops.EagerTensor'>
类型而不是普通张量吗?
有没有什么方法可以让 "args_0:0"
变成我希望打印数字数组的格式?因为我相信这会修复旋转功能
最后,如果没有,有没有更好的方法通过随机旋转来增强图像?
感谢您的宝贵时间和帮助。
args_0:0
是张量。参见
我对您的代码进行了一些更改以使其正常工作。
代码:
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)
plt.imshow(normalimg)
要记住的事情:
- map函数没有急于执行
- 始终在生成器函数和映射函数中使用 TensorFlow 函数,因为 TensorFlow 将这些函数作为图形的一部分执行,以加快其执行速度。参见 here
- 如果您使用其他函数,TensorFlow 可能无法将这些操作转换为图形,这将导致错误。