Keras - .flow_from_directory(目录)
Posted
技术标签:
【中文标题】Keras - .flow_from_directory(目录)【英文标题】:Keras - .flow_from_directory(directory) 【发布时间】:2018-08-07 14:39:56 【问题描述】:我正在尝试使用.flow_from_directory(directory)
运行带有 cifar10 数据集的 Resnet 示例。以下代码如下:
from __future__ import print_function
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import np_utils
from keras.callbacks import ReduceLROnPlateau, CSVLogger, EarlyStopping
import numpy as np
import resnet
import os
import cv2
import csv
#import keras
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
# input image dimensions
img_rows, img_cols = 32, 32
# The CIFAR10 images are RGB.
img_channels = 3
nb_classes = 10
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0,
zoom_range=0,
horizontal_flip=False,
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.1) # randomly shift images vertically (fraction of total height))
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
'/home/datasets/cifar10/train',
target_size=(32, 32),
batch_size=32,
shuffle=False)
validation_generator = test_datagen.flow_from_directory(
'/home/datasets/cifar10/test',
target_size=(32, 32),
batch_size=32,
shuffle=False)
model = resnet.ResnetBuilder.build_resnet_18((img_channels, img_rows, img_cols), nb_classes)
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit_generator(
train_generator,
steps_per_epoch=500,
epochs=50,
validation_data=validation_generator,
validation_steps=250)
但是,我获得了以下准确度值。
500/500 [==============================] - 22s - loss: 0.8139 - acc: 0.9254 - val_loss: 12.7198 - val_acc: 0.1250
Epoch 2/50
500/500 [==============================] - 19s - loss: 1.0645 - acc: 0.8856 - val_loss: 8.4179 - val_acc: 0.0560
Epoch 3/50
500/500 [==============================] - 19s - loss: 2.1014 - acc: 0.7492 - val_loss: 10.7770 - val_acc: 0.0956
Epoch 4/50
500/500 [==============================] - 19s - loss: 1.6806 - acc: 0.7772 - val_loss: 6.1023 - val_acc: 0.0741
Epoch 5/50
500/500 [==============================] - 19s - loss: 1.1798 - acc: 0.8669 - val_loss: 6.9016 - val_acc: 0.1253
Epoch 6/50
500/500 [==============================] - 19s - loss: 1.5448 - acc: 0.8369 - val_loss: 3.6371 - val_acc: 0.0370
Epoch 7/50
500/500 [==============================] - 19s - loss: 1.3763 - acc: 0.8599 - val_loss: 4.8012 - val_acc: 0.1204
Epoch 8/50
500/500 [==============================] - 19s - loss: 1.0186 - acc: 0.8891 - val_loss: 6.8395 - val_acc: 0.0912
Epoch 9/50
500/500 [==============================] - 19s - loss: 0.9477 - acc: 0.9081 - val_loss: 10.4287 - val_acc: 0.1253
Epoch 10/50
500/500 [==============================] - 19s - loss: 1.0689 - acc: 0.8686 - val_loss: 7.9931 - val_acc: 0.1253
我正在使用来自 link 的 Resnet。我尝试了许多示例来解决问题,包括官方文档中的示例。但是,我无法解决问题。训练精度正在发生变化,但 val 精度有些恒定。 有人能指出问题吗
【问题讨论】:
【参考方案1】:根据 Keras 文档。
flow_from_directory(directory)
,描述:获取目录的路径,并生成批量增强/规范化的数据。在无限循环中无限产生批次。
使用shuffle = False
,它会无限期地占用同一批次。导致这些精度值。我更改了shuffle = True
,现在可以正常使用了。
【讨论】:
以上是关于Keras - .flow_from_directory(目录)的主要内容,如果未能解决你的问题,请参考以下文章
keras与tensorflow.python.keras - 使用哪一个?
keras 与 tensorflow.python.keras - 使用哪一个?