使用Tensorflow随机旋转和裁剪顺序抛出错误
Using Tensorflow random rotation and cropping sequentially throws errors
我正在尝试在 Keras 页面上编辑此代码:https://keras.io/examples/vision/deeplabv3_plus/。我想做的一件事是为数据集添加一个扩充函数。这是我写的:
def image_augmentation(img):
img = tf.image.random_flip_left_right(img)
img = tf.image.random_flip_up_down(img)
img = tf.image.random_brightness(img, 0.2)
img = tf.image.random_crop(value = img, size=(2, 2))
img = tf.keras.preprocessing.image.random_rotation(img, 90, row_axis=0, col_axis=1,channel_axis=2)
但是,最后两行引发了我遇到问题的错误。
这一行 img = tf.image.random_crop(value = img, size=(2, 2))
抛出这个错误:
ValueError: Dimensions must be equal, but are 3 and 2 for '{{node random_crop/GreaterEqual}} = GreaterEqual[T=DT_INT32](random_crop/Shape, random_crop/size)' with input shapes: [3], [2].
这一行 img = tf.keras.preprocessing.image.random_rotation(img, 90, row_axis=0, col_axis=1,channel_axis=2)
抛出这个错误:
AttributeError: in user code:
File "<ipython-input-3-8db8a894e0d0>", line 41, in data_loader *
mask = image_process(mask_list, mask=True)
File "<ipython-input-3-8db8a894e0d0>", line 28, in image_process *
img = image_augmentation(img)
File "<ipython-input-7-037af66af3d3>", line 18, in image_augmentation *
img = tf.keras.preprocessing.image.random_rotation(img, 90, row_axis=0, col_axis=1,channel_axis=2)
File "/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/affine_transformations.py", line 56, in random_rotation *
x = apply_affine_transform(x, theta=theta, channel_axis=channel_axis,
File "/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/affine_transformations.py", line 323, in apply_affine_transform *
x = np.rollaxis(x, channel_axis, 0)
File "<__array_function__ internals>", line 6, in rollaxis **
File "/usr/local/lib/python3.7/dist-packages/numpy/core/numeric.py", line 1290, in rollaxis
n = a.ndim
AttributeError: 'Tensor' object has no attribute 'ndim'
对于行中的第一个错误,
img = tf.image.random_crop(value = img, size=(2, 2))
在方法的documentation中tf.image.random_crop
明确提到,
If a dimension should not be cropped, pass the full size of that dimension. For example, RGB images can be cropped with size = [crop_height, crop_width, 3].
所以,为了修复错误,
img = tf.image.random_crop(value = img, size=(2, 2, 3))
对于行中的第二个错误,
img = tf.keras.preprocessing.image.random_rotation(img, 90, row_axis=0, col_axis=1,channel_axis=2)
此方法要求 img
是 NumPy 数组(张量)而不是 Tensor
对象。此外,方法 returns 以 NumPy 数组的形式旋转图像。
如果函数 image_augmentation
的期望输出是 ndarray
那么您只需要进行以下更改,
img = tf.keras.preprocessing.image.random_rotation(img.numpy(), 90, row_axis=0, col_axis=1,channel_axis=2)
否则,如果所需的输出类型是 Tensor
,我们可以使用来自 TensorFlow Addons 包的 tfa.image.rotate
方法,
import math
import random
upper = 90 * (math.pi/180.0) # degrees -> radian
lower = 0 * (math.pi/180.0)
def rand_degree():
return random.uniform( lower , upper )
def image_augmentation(img):
...
img = tfa.image.rotate( img , rand_degree() )
# img is a Tensor
return img
我正在尝试在 Keras 页面上编辑此代码:https://keras.io/examples/vision/deeplabv3_plus/。我想做的一件事是为数据集添加一个扩充函数。这是我写的:
def image_augmentation(img):
img = tf.image.random_flip_left_right(img)
img = tf.image.random_flip_up_down(img)
img = tf.image.random_brightness(img, 0.2)
img = tf.image.random_crop(value = img, size=(2, 2))
img = tf.keras.preprocessing.image.random_rotation(img, 90, row_axis=0, col_axis=1,channel_axis=2)
但是,最后两行引发了我遇到问题的错误。
这一行 img = tf.image.random_crop(value = img, size=(2, 2))
抛出这个错误:
ValueError: Dimensions must be equal, but are 3 and 2 for '{{node random_crop/GreaterEqual}} = GreaterEqual[T=DT_INT32](random_crop/Shape, random_crop/size)' with input shapes: [3], [2].
这一行 img = tf.keras.preprocessing.image.random_rotation(img, 90, row_axis=0, col_axis=1,channel_axis=2)
抛出这个错误:
AttributeError: in user code:
File "<ipython-input-3-8db8a894e0d0>", line 41, in data_loader *
mask = image_process(mask_list, mask=True)
File "<ipython-input-3-8db8a894e0d0>", line 28, in image_process *
img = image_augmentation(img)
File "<ipython-input-7-037af66af3d3>", line 18, in image_augmentation *
img = tf.keras.preprocessing.image.random_rotation(img, 90, row_axis=0, col_axis=1,channel_axis=2)
File "/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/affine_transformations.py", line 56, in random_rotation *
x = apply_affine_transform(x, theta=theta, channel_axis=channel_axis,
File "/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/affine_transformations.py", line 323, in apply_affine_transform *
x = np.rollaxis(x, channel_axis, 0)
File "<__array_function__ internals>", line 6, in rollaxis **
File "/usr/local/lib/python3.7/dist-packages/numpy/core/numeric.py", line 1290, in rollaxis
n = a.ndim
AttributeError: 'Tensor' object has no attribute 'ndim'
对于行中的第一个错误,
img = tf.image.random_crop(value = img, size=(2, 2))
在方法的documentation中tf.image.random_crop
明确提到,
If a dimension should not be cropped, pass the full size of that dimension. For example, RGB images can be cropped with size = [crop_height, crop_width, 3].
所以,为了修复错误,
img = tf.image.random_crop(value = img, size=(2, 2, 3))
对于行中的第二个错误,
img = tf.keras.preprocessing.image.random_rotation(img, 90, row_axis=0, col_axis=1,channel_axis=2)
此方法要求 img
是 NumPy 数组(张量)而不是 Tensor
对象。此外,方法 returns 以 NumPy 数组的形式旋转图像。
如果函数 image_augmentation
的期望输出是 ndarray
那么您只需要进行以下更改,
img = tf.keras.preprocessing.image.random_rotation(img.numpy(), 90, row_axis=0, col_axis=1,channel_axis=2)
否则,如果所需的输出类型是 Tensor
,我们可以使用来自 TensorFlow Addons 包的 tfa.image.rotate
方法,
import math
import random
upper = 90 * (math.pi/180.0) # degrees -> radian
lower = 0 * (math.pi/180.0)
def rand_degree():
return random.uniform( lower , upper )
def image_augmentation(img):
...
img = tfa.image.rotate( img , rand_degree() )
# img is a Tensor
return img