python - 无法使用 Keras fit_generator 重现结果

标签 python tensorflow keras classification vgg-net

我刚刚注意到每次运行 Keras 模型时都会得到不同的结果。我尝试了 this issue 中的解决方案在 GitHub 上,基本上:

  • 在导入任何其他内容之前设置种子
  • fit_generator() 上设置 shuffle=False

即使我这样做了,我似乎仍然无法重现相同的结果。

我已经在我刚刚链接的问题上发布了同样的问题,但由于可见性,我决定也在这里发布,希望任何人都可以帮助我找出问题所在。

import numpy as np
import tensorflow as tf
import random as rn
import os
os.environ['PYTHONHASHSEED'] = '0'
np.random.seed(42)
rn.seed(12345)
session_conf = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)
from keras import backend as K
tf.set_random_seed(1234)
sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
K.set_session(sess)

from keras.layers import Input, Dropout, Flatten, Conv2D, MaxPooling2D, Dense, Activation, Lambda,GlobalAveragePooling2D
from keras.optimizers import RMSprop , SGD, Adam,Nadam
from keras.callbacks import ModelCheckpoint, Callback, EarlyStopping, History
from keras.preprocessing.image import ImageDataGenerator
from keras.applications import VGG16, VGG19, ResNet50, Xception
from keras.models import Model

batch_size = 32
num_channels = 3
img_size = 512
img_full_size = (img_size, img_size, num_channels)
num_classes = 2
seed = 1 # for image transformations
train_path = 'keras_folders/train/'
validation_path = 'keras_folders/val/'
test_path = 'keras_folders/test/'

train_datagen = ImageDataGenerator(
    rescale=1./255,
    horizontal_flip=True)

validation_datagen = ImageDataGenerator(
    rescale=1./255)

test_datagen = ImageDataGenerator(
    rescale=1./255)

train_generator = train_datagen.flow_from_directory(
    train_path,
    target_size=(img_size, img_size),
    batch_size=batch_size,
    class_mode='categorical', 
    seed=seed)

validation_generator = validation_datagen.flow_from_directory(
    validation_path,
    target_size=(img_size, img_size),
    batch_size=batch_size,
    shuffle=False,
    class_mode='categorical',
    seed=seed)

from collections import Counter
counter = Counter(train_generator.classes)
max_val = float(max(counter.values()))
class_weights = {class_id : max_val/num_images for class_id, num_images in counter.items()}  

conv_base = VGG16(weights='imagenet', include_top=False, input_shape=img_full_size)
conv_base.trainable=True
for layer in conv_base.layers[:4]:
    layer.trainable = False
x = Flatten()(conv_base.output)
x = Dense(256, activation='relu')(x)
x = Dropout(0.218)(x)
predictions = Dense(num_classes, activation='softmax')(x)
model = Model(inputs = conv_base.input , outputs=predictions)

adam = Adam(lr=0.0001)
model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])

train_samples = train_generator.samples
validation_samples = validation_generator.samples
model.fit_generator(
    train_generator,
    class_weight=class_weights,
    steps_per_epoch= train_samples // batch_size,
    epochs=1,
    validation_data= validation_generator,
    validation_steps= validation_samples // batch_size,
    shuffle=False)

最佳答案

我认为你必须做相反的事情。 fit 函数默认打开 shuffle,而 fit_generator 函数从你的生成器中获取 shuffle。您的 train_generator 设置了 seed 参数,但没有设置 shuffle 参数。您的 ImageDataGenerator 是否有可能默认将 shuffle 设置为 False?

此讨论建议您在训练迭代器中打开随机播放:https://github.com/keras-team/keras/issues/2389 .我遇到了同样的问题,这解决了它。

仅当您想要为给定的代码段精确地重现结果时才需要设置种子。我怀疑设置种子会在 fit 和 fit_generator 之间产生完全相同的结果。

关于python - 无法使用 Keras fit_generator 重现结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51972909/

相关文章:

python - sklearn.externals 模块说明

python - SQL 仅选择最近 5 分钟的记录

python - 有没有办法在 Tornado 中发送 "lock"请求,直到完成某些操作?

tensorflow - 在tensorflow estimator类中,一步训练是什么意思?

python - 在 Keras 中规范化神经网络的验证集

python - Keras模型只能预测一个类

python - 在 Pandas Dataframe 中查找重复行,然后在 Dataframe 中添加一列,说明该行是否重复

android - 为 Tensorflow 编译 Android 示例

python - for循环内的神经网络

machine-learning - 使用keras训练多类nn时,loss无法进一步往下走,可能是什么原因