- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试向我的图像添加随机缩放,这些图像是具有 128x160 分辨率 1 channel 的 tiff 文件,但是用于 keras tensorflow 的新版本随机缩放让我感到困惑,我不明白它期望的元组格式应该如何作为缩放范围争论。
从文档中。
tf.keras.preprocessing.image.random_zoom(
x, zoom_range, row_axis=1, col_axis=2, channel_axis=0, fill_mode='nearest',
cval=0.0, interpolation_order=1
)
我需要为我的图像添加一些随机缩放,我正在尝试这样:
zoom_range = ((0.4, 0.4))
img = tf.keras.preprocessing.image.random_zoom(
img, zoom_range, row_axis=1, col_axis=2, channel_axis=0, fill_mode='nearest',
cval=0.0, interpolation_order=1
)
输出是:
TypeError: float() argument must be a string or a number, not'NoneType'
<ipython-input-4-9ba0455797a4>:17 load *
img = tf.keras.preprocessing.image.random_zoom(img, zoom_range, row_axis=0, col_axis=1, channel_axis=2, fill_mode='nearest')
/opt/conda/lib/python3.7/site-packages/keras_preprocessing/image/affine_transformations.py:153 random_zoom *
x = apply_affine_transform(x, zx=zx, zy=zy, channel_axis=channel_axis,
/opt/conda/lib/python3.7/site-packages/keras_preprocessing/image/affine_transformations.py:321 apply_affine_transform *
transform_matrix = transform_matrix_offset_center(
/opt/conda/lib/python3.7/site-packages/keras_preprocessing/image/affine_transformations.py:246 transform_matrix_offset_center *
o_x = float(x) / 2 + 0.5
/opt/conda/lib/python3.7/site-packages/tensorflow/python/autograph/operators/py_builtins.py:195 float_ **
return _py_float(x)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/autograph/operators/py_builtins.py:206 _py_float
return float(x)
TypeError: float() argument must be a string or a number, not 'NoneTyp
e'
TypeError Traceback (most recent call last)
<ipython-input-4-9ba0455797a4> in <module>
27 train1, train2, test1 = d
28 train_ds = tf.data.Dataset.from_tensor_slices(train1 + train2).\
---> 29 shuffle(len(train1) + len(train2)).map(load).batch(4)
30 test_ds = tf.data.Dataset.from_tensor_slices(test1).\
31 shuffle(len(test1)).map(load).batch(4)
for i in range(len(groups)):
d = deque(groups)
d.rotate(i)
train1, train2, test1 = d
train_ds = tf.data.Dataset.from_tensor_slices(train1 + train2).\
shuffle(len(train1) + len(train2)).map(load).batch(4)
test_ds = tf.data.Dataset.from_tensor_slices(test1).\
shuffle(len(test1)).map(load).batch(4)
最佳答案
可能是你的 img
是错误的对象类型。对于 random_zoom(...)
您需要以张量或 3D 形式提供输入的函数 numpy
形状数组 (height, width, channels)
即对于大小为 300x200 的 RGB 图像,数组的形状应为 (200, 300, 3)。这种numpy数组可以通过例如PIL获得像下面的代码一样的库。
此外,如果您有 TF 代码,那么您正在处理张量,但是 random_zoom
需要知道所有维度,它们的整数大小。张量可能有 None
如果在图形构建时未知,则某些维度的大小,这可能会导致有关 NoneType
的错误在你的情况下。要克服这个问题,您需要包装 random_zoom
用法转化为numpy function interface ,这将强制函数输入为 numpy 数组而不是张量,并且 numpy 数组始终具有已知大小的所有维度。我也在下面的代码中实现了这种包装。
另外您可能需要更改 row_axis=1, col_axis=2, channel_axis=0
至 row_axis=0, col_axis=1, channel_axis=2
因为 channel (颜色)通常在最不重要的维度(最后)中。
tf.keras.preprocessing.image.random_zoom 的文档.
我接下来实现了简单的代码。
代码中的输入如下所示:
输出如下所示:
下一个代码也可以run here online .
# Needs: python -m pip install tensorflow numpy pillow requests
import tensorflow as tf, numpy as np, PIL.Image, requests, io
tf.compat.v1.enable_eager_execution()
zoom_range = (0.4, 0.5)
img = PIL.Image.open(io.BytesIO(requests.get('/image/Fc3Jb.png').content))
#img = PIL.Image.open('Ruler-Big-Icon-PNG.png')
img = np.array(img)
img = tf.convert_to_tensor(img) # This line is not needed if you already have a tensor.
# You need only this single line of code to fix your issue!
img = tf.numpy_function(lambda img: tf.keras.preprocessing.image.random_zoom(
img, zoom_range, row_axis=0, col_axis=1, channel_axis=2, fill_mode='nearest',
), [img], tf.float32)
img = np.array(img) # This line is not needed if you plan img to be a tensor futher
# Example output is /image/MWk9T.png
PIL.Image.fromarray(img).save('result.png')
关于python - 如何在 keras tensorflow 2.3 中使用随机缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64025732/
我是一名优秀的程序员,十分优秀!