Keras fit在第一个纪元结束时冻结 [英] Keras fit freezes at the end of the first epoch

查看:197
本文介绍了Keras fit在第一个纪元结束时冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用Keras对VGG16网络进行微调.

I am currently experimenting with fine tuning the VGG16 network using Keras.

我开始对猫和狗的数据集进行一些调整.

I started tweaking a little bit with the cats and dogs dataset.

但是,在当前配置下,训练似乎在第一个时期就被阻止了

However, with the current configuration the training seems to block on the first epoch

from keras import applications
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
from keras.models import Sequential, Model
from keras.layers import Dropout, Flatten, Dense


img_width, img_height = 224, 224

train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
nb_train_samples = 2000
nb_validation_samples = 800
epochs = 50
batch_size = 20


model = applications.VGG16(weights='imagenet', include_top=False , input_shape=(224,224,3))
print('Model loaded.')


top_model = Sequential()
top_model.add(Flatten(input_shape=model.output_shape[1:]))
top_model.add(Dense(256, activation='relu',name='newlayer'))
top_model.add(Dropout(0.5))
top_model.add(Dense(2, activation='softmax'))


model = Model(inputs= model.input, outputs= top_model(model.output))


for layer in model.layers[:19]:
    layer.trainable = False


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

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

test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_height, img_width),
    batch_size=batch_size,
    shuffle=True,
    class_mode='categorical')

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_height, img_width),
    batch_size=batch_size,
    class_mode='categorical')


model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples// batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples)

最后输出:

Epoch 1/50 99/100 [===========================>.]-ETA:0秒-损失: 0.5174-acc:0.7581

Epoch 1/50 99/100 [============================>.] - ETA: 0s - loss: 0.5174 - acc: 0.7581

我想念什么吗?

推荐答案

随机播放

就我而言,我正在用shuffle='batch'调用fit(...).从参数中删除此参数可解决问题. (我认为这是一个TensorFlow错误,但我没有对此进行深入研究.)

Shuffle

In my case, I was calling fit(...) with shuffle='batch'. Removing this parameter from the arguments resolved the problem. (I assume it's a TensorFlow bug but I didn't dig into it.)

另一个注意事项是,验证将在时代结束时执行...如果验证数据没有被批处理,尤其是当您填充数据时,您可能会对比大于2的数据进行验证.您的培训批次大小将填充为验证数据的最大样本长度.这可能是内存不足的问题.

Another consideration is that validation is being performed at the end of the epoch... If your validation data isn't being batched, and particularly if you are padding your data, then you could be performing validation on data much larger than your training batch size padded to the maximum sample length of your validation data. This could be a problem of out-of-memory proportions.

这篇关于Keras fit在第一个纪元结束时冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆