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

Python 如何在keras tensorflow 2.3中使用随机缩放,python,python-3.x,tensorflow,keras,Python,Python 3.x,Tensorflow,Keras,我正在尝试将随机缩放添加到我的图像中,这些图像是分辨率为128x160的tiff文件1通道,但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, in

我正在尝试将随机缩放添加到我的图像中,这些图像是分辨率为128x160的tiff文件1通道,但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()参数必须是字符串或数字,而不是 “非类型”

我应该如何将随机缩放量作为参数传递给我的图像

公共卡格尔笔记本:

TypeError:在用户代码中:

    <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)
:17加载*
img=tf.keras.preprocessing.image.random\u zoom(img,缩放范围,行轴=0,列轴=1,通道轴=2,填充模式为最近)
/opt/conda/lib/python3.7/site packages/keras_preprocessing/image/affine_transformations.py:153随机缩放*
x=应用仿射变换(x,zx=zx,zy=zy,通道轴=通道轴,
/opt/conda/lib/python3.7/site packages/keras_preprocessing/image/affine_transformation.py:321 apply_affine_transform*
变换矩阵=变换矩阵偏移中心(
/opt/conda/lib/python3.7/site packages/keras_preprocessing/image/affine_transformations.py:246 transform_matrix_offset_center*
o_x=浮动(x)/2+0.5
/opt/conda/lib/python3.7/site packages/tensorflow/python/autograph/operators/py_builtins.py:195 float_**
返回浮点数(x)
/opt/conda/lib/python3.7/site packages/tensorflow/python/autograph/operators/py_builtins.py:206_py_float
返回浮动(x)
TypeError:float()参数必须是字符串或数字,而不是“NoneTyp”
e'
TypeError回溯(最近一次调用上次)
在里面
27第1列、第2列、试验1=d
28序列ds=tf.data.Dataset.来自张量切片(序列1+2)\
--->29洗牌(列队(第1列)+列队(第2列))。地图(装载)。批次(4)
30 test_ds=tf.data.Dataset.from_tensor_切片(test1)\
31洗牌(len(test1)).map(load).batch(4)
对于范围内的i(len(组)):
d=deque(组)
d、 轮换(一)
第1列、第2列、测试1=d
训练ds=tf.data.Dataset.from_张量切片(训练1+2)\
洗牌(列队(第1列)+列队(第2列)).map(装载).批次(4)
test_ds=tf.data.Dataset.from_tensor_切片(test1)\
shuffle(len(test1)).map(load).batch(4)

可能您的
img
对象类型错误。对于
随机缩放(…)
功能,您需要提供张量或3D
numpy
形状数组
(高度、宽度、通道)
即对于300x200大小的RGB图像,数组应为形状(200、300、3)。例如,可以通过如下代码中的库获取这种numpy数组

同样,如果你有TF代码,那么你在处理张量,但是
random\u zoom
需要知道所有维度,它们的整数大小。如果在图形构造时未知,张量在某些维度上可能有
None
大小,这可能会导致在你的例子中关于
NoneType
的错误。要克服这个问题您需要将
random\u zoom
用法包装为,这将强制函数输入为numpy数组,而不是张量,并且numpy数组总是具有已知大小的所有维度。我在下面的代码中也实现了这种包装

此外,您可能需要将
行_轴=1、列_轴=2、通道_轴=0
更改为
行_轴=0、列_轴=1、通道_轴=2
,因为通道(颜色)通常位于最低有效维度(最后一个)

文件

接下来,我实现了简单的代码

代码中的输入如下所示:

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
)

输出如下所示:

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
)

下一个代码也可以是


我通常不认为在Keras预处理函数所属范围之外使用它们是正确的方法。一个简单的方法是使用
tf.image.random\u crop
。假设您的图像大于
(200,200,3)
,您可以使用这一行:

img = tf.image.random_crop(img, (200, 200, 3))
让我们尝试一个示例。原始图像:

大小为
(200,200,3)
的随机裁剪图像:

奇怪,我注释掉了这一行,在上面的一行中,我有一行可以旋转90度,但当我添加这一行时,错误仍然存在,我上传了一个公共kaggle笔记本,你能看一下吗?@JohnJones我想我知道原因。大多数TF/Keras函数在位置0和batc处都有所谓的批处理轴h轴通常没有大小,这在例外情况下显示。因此您需要此修复
行\轴=1,列\轴=2,通道\轴=3
@JohnJones我刚刚阅读了随机\缩放的文档,不幸的是,您必须考虑到它只支持3D输入张量。这意味着您无法处理批。因此,如果您有以前操作的4D输出等一下,我这里的网速很慢(每秒几十KBs)而且需要很多时间。也许你有3D张量,但其中的宽度或高度维度是未知的,即没有。@JohnJones如果一些维度是未知的,并且与你的情况不同,你需要用kaggle将随机缩放线包装到其中,这将使所有维度都为人所知。互联网速度非常慢,等待加载。@JohnJones我把
numpy\u function
将我的答案包装成代码,还添加了第二段关于此的内容,您可以尝试这样做(包装)。你能提供完整的异常堆栈吗?不仅仅是最后一条消息行。也许这个案例可以被crop取代,但我认为一般来说,混合不同库的代码,添加一些小的适配器是可以的。keras是一个巨大的酷库,有许多函数没有回接到TFs后端函数中,比如这个随机缩放,以及probably most根本不会被移植。因此,知道如何从一个库到另一个库进行必要的包装是很好的。如果你看看我现在的answ