在Keras fit_generator中将shuffle设置为True时,精度会降低

死了

我正在使用的数据非常不平衡。

我正在使用VGG16训练图像分类器。我冻结了VGG16中的所有层,接受最后两个完全连接的层。

BATCH_SIZE = 128

EPOCHS = 80

当我设置shuffle = False时,每个类的精度和查全率都很高(介于.80-.90之间),但是当我将shuffle = True时,每个类的精度和查全率都降至0.10-0.20。我不确定发生了什么。可以帮忙吗?

下面是代码:

img_size = 224
trainGen = trainAug.flow_from_directory(
    trainPath,
    class_mode="categorical",
    target_size=(img_size, img_size),
    color_mode="rgb",
    shuffle=False,
    batch_size=BATCH_SIZE)
valGen = valAug.flow_from_directory(
    valPath,
    class_mode="categorical",
    target_size=(img_size, img_size),
    color_mode="rgb",
    shuffle=False,
    batch_size=BATCH_SIZE)

testGen = valAug.flow_from_directory(
    testPath,
    class_mode="categorical",
    target_size=(img_size, img_size),
    color_mode="rgb",
    shuffle=False,
    batch_size=BATCH_SIZE)

baseModel = VGG16(weights="imagenet", include_top=False,input_tensor=Input(shape=(img_size, img_size, 3)))
headModel = baseModel.output
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(512, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(PFR_NUM_CLASS, activation="softmax")(headModel)
# place the head FC model on top of the base model (this will become
# the actual model we will train)
model = Model(inputs=baseModel.input, outputs=headModel)
# loop over all layers in the base model and freeze them so they will
# *not* be updated during the first training process
for layer in baseModel.layers:
    layer.trainable = False

班级权重计算如下:

from sklearn.utils import class_weight
import numpy as np

class_weights = class_weight.compute_class_weight(
               'balanced',
                np.unique(trainGen.classes), 
                trainGen.classes)

这些是班级权重:

array([0.18511007, 2.06740331, 1.00321716, 3.53018868, 2.48637874,
       2.27477204, 1.57557895, 6.68214286, 1.04233983, 4.02365591])

培训代码为:

# compile our model (this needs to be done after our setting our layers to being non-trainable
print("[INFO] compiling model...")
opt = SGD(lr=1e-5, momentum=0.8)
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
# train the head of the network for a few epochs (all other layers
# are frozen) -- this will allow the new FC layers to start to become
#initialized with actual "learned" values versus pure random
print("[INFO] training head...")
H = model.fit_generator(
    trainGen,
    steps_per_epoch=totalTrain // BATCH_SIZE,
    validation_data=valGen,
    validation_steps=totalVal // BATCH_SIZE,
    epochs=EPOCHS,
    class_weight=class_weights,
    verbose=1,
    callbacks=callbacks_list)
# reset the testing generator and evaluate the network after
# fine-tuning just the network head
蒂姆布斯·卡林(Timbus Calin)

就您而言,设置的问题shuffle=True在于,如果您对验证集进行混洗,结果将很混乱。碰巧预测是正确的,但与错误的索引进行比较可能会导致误导性结果,就像您的情况一样。

始终shuffle=True在训练集以及shuffle=False验证集和测试集上。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在keras中,fit_generator中的“ shuffle”有什么作用?

在Keras使用fit_generator

Keras fit_generator问题

使用 fit_generator 时,Keras 模型的批量大小为 1

Keras:使用fit_generator时出现notImplementedError / RuntimeError

使用fit_generator形状不匹配时出错(Keras)

在Keras中多次调用“ fit_generator()”

Keras fit_generator运行非常缓慢

fit_generator()在keras中的优势

Keras Fit_generator回调

Keras的fit_generator额外训练价值

在Keras模型中使用fit_generator

在Keras中正确使用fit_generator

Keras fit_generator() 用于长信号

当训练数据对于fit()太大时,如何知道何时在keras中使用fit_generator()?

Keras-SegNet使用ImageDataGenerator和fit或fit_generator

如何在fit_generator keras中的varibale输入长度中设置steps_per_epoch

当我尝试使用fit_generator训练Keras时,为什么Keras在第一个时期停止了?

使用Keras fit_generator时如何规范化数据

当处理不适合RAM的数据时,Keras fit_generator是最好的选择吗?

使用fit_generator时Keras进度栏会生成随机批号

使用fit_generator拟合keras模型时如何修复“MemoryError”?

Keras模型fit_generator奇数错误

Keras fit_generator()-时间序列的批处理如何工作?

Keras fit_generator具有多个输入层

如何在Keras中使用fit_generator()加权类?

Keras fit_generator上的错误,运行了几步

使用.fit_generator()在keras中训练GAN

Keras fit_generator和steps_per_epoch