gpt4 book ai didi

python - 如何在 keras tensorflow 2.3 中使用随机缩放

转载 作者:行者123 更新时间:2023-12-04 12:36:40 25 4
gpt4 key购买 nike

我正在尝试向我的图像添加随机缩放,这些图像是具有 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'


我应该如何将任何随机缩放量作为参数传递给我的图像?
公共(public) kaggle 笔记本在这里:
https://www.kaggle.com/puelon/notebook75c416766a
类型错误:在用户代码中:
    <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=0row_axis=0, col_axis=1, channel_axis=2因为 channel (颜色)通常在最不重要的维度(最后)中。
tf.keras.preprocessing.image.random_zoom 的文档.
我接下来实现了简单的代码。
代码中的输入如下所示:
input
输出如下所示:
input
下一个代码也可以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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com