如何解决如何解决 tensorflow 中的 random_rotation 错误?
我有一些图像,每个图像都有 [128,128,60] 大小。我想使用 tf.keras.preprocessing.image.random_rotation
函数对它们进行一些扩充。
def augment_rotate_tf(x):
x = tf.keras.preprocessing.image.random_rotation(x,50,row_axis=0,col_axis=1,channel_axis=2)
return x
当我将 numpy 数组传递给此函数时,它可以正常工作,但是当我在 tensorflow 图中使用它时,会出现此错误:
AttributeError: in user code:
<ipython-input-99-790177542fdb>:15 augmentation *
img = tf.cond(cond,lambda: augment_rotate_tf_(img),lambda: img)
<ipython-input-124-28f8f87fa48b>:4 augment_rotate_tf_ *
x = tf.keras.preprocessing.image.random_rotation(x,C:\Users\user\AppData\Roaming\Python\python38\site-packages\keras_preprocessing\image\affine_transformations.py:56 random_rotation *
x = apply_affine_transform(x,theta=theta,channel_axis=channel_axis,C:\Users\user\AppData\Roaming\Python\python38\site-packages\keras_preprocessing\image\affine_transformations.py:323 apply_affine_transform *
x = np.rollaxis(x,channel_axis,0)
<__array_function__ internals>:5 rollaxis **
C:\Users\user\anaconda3\envs\tf2.3\lib\site-packages\numpy\core\numeric.py:1259 rollaxis
n = a.ndim
AttributeError: 'Tensor' object has no attribute 'ndim'
如何解决这个问题?
解决方法
我在这里knuckles发现了一个优雅的 A possible workaround is to incorporate the layers into the input pipeline. hack:
random_rotator=tf.keras.layer.experimental.preprocessing.RandomRotation(factor=0.055,fill_mode='constant',fill_value=0.)
<....>
ds= ds.batch(batch_size).prefetch(1)
ds = ds.map(lambda d,l: (random_rotator.call(d,training=True),l),num_parallel_calls=AUTO)
这是我对 hack 的使用。如果您之前应用批处理,它会起作用,因为输入需要 rank=4,其中第一个维度是批量大小 > 0
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。