将数据分成批次

时间:2020-04-03 21:58:05

标签: training-data mnist test-data imagedata

我想将训练数据,测试数据和验证数据分成批次。我正在研究Fashion MNIST数据集,并直接从keras.datasets访问它。 我找到了下面提到的代码:

trainbatches = ImageDataGenerator().flowfromdirectory(trainpath, targetsize=(224,224), classes= classname, batchsize=10 testbatches = ImageDataGenerator().flowfromdirectory(testpath, targetsize=(224,224), classes= classname, batchsize=10
valbatches = ImageDataGenerator().flowfromdirectory(valpath, targetsize=(224,224), classes= classname, batch_size=10

由于我尚未将数据下载到硬盘驱动器上并无法从keras.datasets访问数据,我该如何执行此操作? 我尝试使用ImageDataGenerator().flow,但不起作用? 有办法执行吗?

1 个答案:

答案 0 :(得分:1)

您基本上使用的格式不正确,将keras数据集的返回格式转换为带有图像的单独datalabel

此代码对我有用

# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__)

fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

print(train_images.shape)
print(test_images.shape)


from keras.preprocessing.image import ImageDataGenerator
from keras.utils import np_utils
y_train = np_utils.to_categorical(train_labels, 10)
y_test = np_utils.to_categorical(test_labels,10)

datagen = ImageDataGenerator(
    featurewise_center=True,
    featurewise_std_normalization=True,
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True)


train_images=train_images.reshape(60000,28,28,1)
test_images=test_images.reshape(10000,28,28,1)

datagen.fit(train_images)

准备好建模和编译

# fits the model on batches with real-time data augmentation:
model.fit_generator(datagen.flow(train_images,train_labels, batch_size=32),
                steps_per_epoch=len(x_train) / 32, epochs=epochs)