我有大约6200张培训图像,我想使用keras.preprocessing.image.ImageDataGenerator类的keras.preprocessing.image.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(X_train , y_train)
validation_generator = test_datagen.flow(X_val , y_val)
history = model.fit_generator(
train_generator,
samples_per_epoch=1920,
nb_epoch=10,
verbose=1,
validation_data=validation_generator,
nb_val_samples=800)其中X_train / y_train包含大约6000个训练图像和标签以及X_val / y_val,验证数据和模型是一个增强的VGG16模型。
文件上说
flow(X,y):接受numpy数据和标签数组,并生成批量的增广/规范化数据。在无限循环中无限期地产生批。
对于10个历元的训练装置,每个历元有1920个样本,batch_size为32,我得到以下训练跟踪:
1920/1920 [==============================] - 3525s - loss: 3.9101 - val_loss: 0.0269
Epoch 2/10
1920/1920 [==============================] - 3609s - loss: 1.0245 - val_loss: 0.0229
Epoch 3/10
1920/1920 [==============================] - 3201s - loss: 0.7620 - val_loss: 0.0161
Epoch 4/10
1916/1920 [============================>.] - ETA: 4s - loss: 0.5978 C:\Miniconda3\envs\carnd-term1\lib\site-packages\keras\engine\training.py:1537: UserWarning: Epoch comprised more than `samples_per_epoch` samples, which might affect learning results. Set `samples_per_epoch` correctly to avoid this warning.
warnings.warn('Epoch comprised more than 为什么生成器不像文档所说的那样生成无限批?
发布于 2017-01-22 14:37:45
因此,在KerasImageGenerator类实现中基本上存在一个小错误。什么是好的-除了这个恼人的警告,没有任何错误发生。因此,要使:
flow和flow_from_directory实际上都在无限循环中生成样本。您可以通过测试以下代码(警告-它将冻结您的Python)来轻松地检查该代码:
对于x,y在train_generator中:x=无fit_generator方法中很少见。它基本上检查在一个时代处理的样本数量是否小于或等于samples_per_epoch。在您的情况下--如果samples_per_epoch的实现是正确的--如果batch_size是可除的--则不应引发此警告.但是..。batch_size = 3:- it will first shuffle the order of this 10 examples,
- then it would take 3 first of shuffled examples, then next three and so on,
- after the 3rd batch - when there is only 1 example left - it will return a batch.. with only one sample.不要问我为什么-这是生成器的实现方式。好消息是它几乎不会影响训练过程。
总之,您可以忽略此警告,也可以使传递给生成器的样本数被batch_size整除。我知道它很麻烦,我希望它能在下一个版本中被修复。
https://stackoverflow.com/questions/41789961
复制相似问题