Keras - .flow_from_directory(目录)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Keras - .flow_from_directory(目录)相关的知识,希望对你有一定的参考价值。
我正在尝试使用.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准确度有些不确定。有人可以指出问题所在
答案
根据Keras文件。
flow_from_directory(directory)
,描述:获取目录的路径,并生成批量的扩充/规范化数据。在无限循环中无限期地产生批次。
使用shuffle = False
,它会无限期地使用相同的批次。导致这些准确度值。我改变了shuffle = True
,现在工作正常。
以上是关于Keras - .flow_from_directory(目录)的主要内容,如果未能解决你的问题,请参考以下文章
keras与tensorflow.python.keras - 使用哪一个?
keras 与 tensorflow.python.keras - 使用哪一个?