首页 > 解决方案 > 将数据拆分为批次

问题描述

我想将我的训练数据、测试数据和验证数据分成批次。我正在研究 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但它不起作用?有没有办法做到这一点?

标签: training-datamnisttest-dataimagedata

解决方案


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

这段代码对我有用

# 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)

推荐阅读