gpt4 book ai didi

python - Keras - 如何将图像数组传递给 ImageDataGenerator.flow

转载 作者:行者123 更新时间:2023-12-01 07:02:27 25 4
gpt4 key购买 nike

我正在学习 keras 中的图像分类。我已经下载了 donut 和华夫饼的示例数据集,但它们的大小不同。为了标准化它们的大小,我从它们的目录中加载图像,调整它们的大小并将它们存储在 numpy 数组中:

test_data_dir = 'v_data/train/donuts_and_waffles/'
validation_data_dir = 'v_data/test/donuts_and_waffles/'

loaded_test_donuts = list()
for filename in listdir(test_data_dir + 'donuts/'):
image1 = Image.open(test_data_dir + 'donuts/' + filename)
img_resized = image1.resize((224,224))
img_data = asarray(img_resized)
loaded_test_donuts.append(img_data)

loaded_test_waffles = list()
for filename in listdir(test_data_dir + 'waffles/'):
image1 = Image.open(test_data_dir + 'waffles/' + filename)
img_resized = image1.resize((224,224))
img_data = asarray(img_resized)
loaded_test_waffles.append(img_data)

loaded_validation_donuts = list()
for filename in listdir(validation_data_dir + 'donuts/'):
image1 = Image.open(validation_data_dir + 'donuts/' + filename)
img_resized = image1.resize((224,224))
img_data = asarray(img_resized)
loaded_validation_donuts.append(img_data)

loaded_validation_waffles = list()
for filename in listdir(validation_data_dir + 'waffles/'):
image1 = Image.open(validation_data_dir + 'waffles/' + filename)
img_resized = image1.resize((224,224))
img_data = asarray(img_resized)
loaded_validation_waffles.append(img_data)

test_data = list()
validation_data = list()

test_data.append(np.array(loaded_test_donuts))
test_data.append(np.array(loaded_test_waffles))
validation_data.append(np.array(loaded_validation_donuts))
validation_data.append(np.array(loaded_validation_waffles))

test_data = np.array(test_data)
validation_data = np.array(validation_data)

然后我想为我的数据创建一个 ImageDataGenerator:

train_datagen = ImageDataGenerator( 
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow(
#how can I pass here test_data to make it work (along with which parameters)
)

validation_generator = test_datagen.flow(
#how can I pass here validation_data to make it work (along with which parameters)
)

如何实现这一目标?我试过这样:

train_generator = train_datagen.flow( 
test_data, #does not work
batch_size=batch_size)

validation_generator = test_datagen.flow(
validation_data, #does not work
batch_size=batch_size)

但随后我收到此错误:

Traceback (most recent call last):
...

ValueError: ('Input data in `NumpyArrayIterator` should have rank 4. You passed an array with shape', (2, 770, 224, 224, 3))

最佳答案

如果没有错误消息,很难说什么不起作用,但我认为问题在于您将列表传递给 ImageDataGenerators。您可以通过将列表转换为 numpy 数组来轻松解决此问题:

test_data = list()
validation_data = list()

test_data.append(np.array(loaded_test_donuts))
test_data.append(np.array(loaded_test_waffles))
validation_data.append(np.array(loaded_validation_donuts))
validation_data.append(np.array(loaded_validation_waffles))

test_data = np.array(test_data)
validation_data = np.array(validation_data)

编辑:更好的方法,堆叠而不是附加到列表并转换

test_data = np.vstack((np.array(loaded_test_donuts),np.array(loaded_test_waffles)))

validation_data = np.vstack((np.array(loaded_validation_donuts),np.array(loaded_validation_waffles)))

关于python - Keras - 如何将图像数组传递给 ImageDataGenerator.flow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58579059/

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