Python 如何查看ImageDataGenerator()中的示例
我目前正在学习使用cifar10图像的教程。我已经编写了一些完整的工作代码,其中包含行Python 如何查看ImageDataGenerator()中的示例,python,tensorflow2.0,Python,Tensorflow2.0,我目前正在学习使用cifar10图像的教程。我已经编写了一些完整的工作代码,其中包含行model.fit(x_train,y_train)其中x_train作为尺寸为50000x32x32x3和数据类型为“uint8”的numpy数组。即,它包含50000个32x32像素的彩色图像。我可以通过调用imshow()来显示这些图像的一个示例-所有这些图像看起来都很好,工作正常 但现在在本教程的下一部分中,它建议如果我们使用ImageDataGenerator()创建多个扭曲(旋转、缩放、倾斜等)版本
model.fit(x_train,y_train)
其中x_train作为尺寸为50000x32x32x3和数据类型为“uint8”的numpy数组。即,它包含50000个32x32像素的彩色图像。我可以通过调用imshow()来显示这些图像的一个示例-所有这些图像看起来都很好,工作正常
但现在在本教程的下一部分中,它建议如果我们使用ImageDataGenerator()创建多个扭曲(旋转、缩放、倾斜等)版本的训练图像,模型将更通用。我想通过显示过程中产生的一些扭曲图像来更好地理解ImageDataGenerator()。请看下面的示例:
# here's a more "manual" example
for e in range(epochs):
print('Epoch', e)
batches = 0
for x_batch, y_batch in datagen.flow(x_train, y_train, batch_size=32):
model.fit(x_batch, y_batch)
batches += 1
if batches >= len(x_train) / 32:
# we need to break the loop by hand because
# the generator loops indefinitely
break
我的当前代码(无扭曲)使用线model.fit(x\u-train,y\u-train)
来训练模型,因此查看示例model.fit(x\u-batch,y\u-batch)
中的线,我假设x\u-batch必须是当前x\u-train图像的32个不同扭曲版本的集合。我试着编写一些代码,这样我就可以实际显示32幅图像,如下所示:
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
def print_array_info(v):
print("{} is of type {} with shape {} and dtype {}".format(v,
eval("type({})".format(v)),
eval("{}.shape".format(v)),
eval("{}.dtype".format(v))
))
def show_samples(array_of_images):
n = array_of_images.shape[0]
total_rows = 1+int((n-1)/5)
total_columns = 5
fig = plt.figure()
gridspec_array = fig.add_gridspec(total_rows, total_columns)
for i, img in enumerate(array_of_images):
row = int(i/5)
col = i % 5
ax = fig.add_subplot(gridspec_array[row, col])
ax.imshow(img)
plt.show()
cifar_data = tf.keras.datasets.cifar10
(x_train, y_train), (x_test, y_test) = cifar_data.load_data()
data_generator = tf.keras.preprocessing.image.ImageDataGenerator(rotation_range=20)
print_array_info("x_train")
batches = 0
batch_size=32
for x_batch, y_batch in data_generator.flow(x_train, y_train, batch_size=batch_size):
print_array_info("x_batch")
batches += 1
if batches >= len(x_train) / 32:
break
show_samples(x_batch[:batch_size])
我以为第一次通过循环时,我会看到x_序列中第0张图像的32个不同扭曲版本。但当我运行这个程序时,它会生成几乎空白的图像——我说几乎是因为其中一个或多个可能包含一些看起来像垃圾的像素。我预计x_批次的大小为32x32x3,即32幅32x32像素和3种颜色的彩色图像的集合,这看起来确实是真的,但数据类型是浮动的32,这让我感到困惑-我认为整经过程不会改变数据类型
我的代码中是否有错误,或者我是否误解了文档?
x\u batch
是一个浮点型数组,因为对数据集应用了随机旋转
当传递给的图像数据(x_batch
)为float
类型时,rgb值必须在0
-1
的范围内。将x_批次
乘以1/225
,得到单位分数表示
对于数据生成器中的x_批,y_批(x_列,y_列,批大小=批大小):
打印阵列信息(“x\U批次”)
批次+=1
如果批次>=长度(x\U系列)/批次大小:
打破
显示样本(x批次*1/255)
x_batch
是一个浮点型数组,因为随机旋转应用于数据集
当传递给的图像数据(x_batch
)为float
类型时,rgb值必须在0
-1
的范围内。将x_批次
乘以1/225
,得到单位分数表示
对于数据生成器中的x_批,y_批(x_列,y_列,批大小=批大小):
打印阵列信息(“x\U批次”)
批次+=1
如果批次>=长度(x\U系列)/批次大小:
打破
显示样本(x批次*1/255)