training-data - 将数据拆分为批次
问题描述
我想将我的训练数据、测试数据和验证数据分成批次。我正在研究 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
但它不起作用?有没有办法做到这一点?
解决方案
您使用的格式基本上不正确,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)
推荐阅读
- versioning - 我可以使用什么来自动增加版本号?
- bash - 可变时间执行相同的 bash 代码
- r - R在R中的循环期间临时写入csv文件
- selenium - 我在我的硒(java)范围报告中得到一个空饼图。它看起来全黑
- oracle - Oracle:立即执行和 ORA-01086 中的保存点
- android - 在方向更改时管理 ListView 内容
- excel - 对匹配功能感到困惑
- python - 使用 Jupyter Notebook 到 Blender/Adobe After-Effects
- mongodb - Mongodb地图减少限制
- java - Camel 的流模式 vs Java 8 Stream