简体   繁体   English

Keras -.flow_from_directory(目录)

[英]Keras - .flow_from_directory(directory)

I am trying to run an example of Resnet with cifar10 dataset using .flow_from_directory(directory) .我正在尝试使用.flow_from_directory(directory)运行带有 cifar10 数据集的 Resnet 示例。 The below code is below:下面的代码如下:

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)

However, I am obtaining the following accuracy value.但是,我获得了以下准确度值。

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

I am using Resnet from this link .我正在使用此 链接中的 Resnet。 I tried numerous examples to sort the problem including the one on the official documentation.我尝试了许多示例来解决问题,包括官方文档中的示例。 However, I am unable to resolve the problem.但是,我无法解决问题。 Training accuracy is changing however val accuracy is somewhat constatn.训练准确度在变化,但是验证准确度有些恒定。 Can some one point the problem有人可以指出问题吗

According the Keras documentation.根据 Keras 文档。

flow_from_directory(directory) , Description:Takes the path to a directory, and generates batches of augmented/normalized data. flow_from_directory(directory) ,描述:获取目录的路径,并生成成批的扩充/规范化数据。 Yields batches indefinitely, in an infinite loop.在无限循环中无限期地产生批次。

With shuffle = False , it takes the same batch indefinitely.使用shuffle = False ,它会无限期地采用同一批次。 leading to these accuracy values.导致这些精度值。 I changed shuffle = True and it works fine now.我更改了shuffle = True ,现在它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM