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

标签 python python-3.x tensorflow keras

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

相关文章:

swift - 如何在 swift 中为 tensorflow 编写 RNN/LSTM 自定义层?

python - mac 运行 deepfakes - faceswap ' faceswap.py' 出现错误

tensorflow - 如何在 tensorflow 中实现过滤器?

python - 使用python3在Windows中创建文件的快捷方式(.lnk)

python - 从 Pandas 数据框中的字符串中删除单个字母

Python 3.0 urllib.parse 错误 "Type str doesn' t 支持缓冲区 API"

python - Django:我怎样才能获得一个模型来存储鞋子尺寸的列表(例如Python或无论如何)

python - 在 Python 中编辑 Mozilla 配置文件

python - 属性错误 : module 'numbers' has no attribute 'Integral'

python - Pandas_合并两个大数据集