Keras适合在第一个时代结束时冻结

时间:2018-02-12 14:10:03

标签: tensorflow keras conv-neural-network

我目前正在尝试使用Keras微调VGG16网络。

我开始对猫狗数据集进行一些调整。

但是,根据目前的配置,训练似乎在第一个时期阻止了

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)

最后输出:

  

大纪元1/50 99/100 [============================&gt ;.] - ETA:0s - 损失:   0.5174 - acc:0.7581

我错过了什么吗?

4 个答案:

答案 0 :(得分:1)

随机播放

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

验证

另一个考虑因素是,验证将在时代结束时执行...如果未批量验证数据,特别是如果要填充数据,则可能对比您的培训批次大小将填充为验证数据的最大样本长度。这可能是内存不足的问题。

答案 1 :(得分:0)

我在合作实验室中遇到了这个问题,云中提供的内存有限(高达12 GB),这在解决问题时会产生很多问题。这就是为什么只使用300张图像进行训练和测试的原因。当图像以600x600的尺寸进行预处理并将批处理大小设置为128时,在第1阶段冻结了Keras模型。编译器未显示此错误。实际上该错误是运行时受限的内存CoLab无法处理,因为它只提供了12GB的有限使用内存。 通过将批量大小更改为4并将图像尺寸减小为300x300,可以解决上述问题,因为在600x600下它仍然无法使用。
最终,推荐的解决方案是减小图像尺寸和Batch_size,直到没有错误再运行一次,直到没有运行时错误

答案 2 :(得分:0)

如果您使用的是 function touch { <# .SYNOPSIS Emulates the Unix touch utility's core functionality. #> param( [Parameter(Mandatory, ValueFromRemainingArguments)] [string[]] $Path ) # For all paths of / resolving to existing files, update the last-write timestamp to now. if ($existingFiles = (Get-ChildItem -File $Path -ErrorAction SilentlyContinue -ErrorVariable errs)) { Write-Verbose "Updating last-write and last-access timestamps to now: $existingFiles" $now = Get-Date $existingFiles.ForEach('LastWriteTime', $now) $existingFiles.ForEach('LastAccessTime', $now) } # For all non-existing paths, create empty files. if ($nonExistingPaths = $errs.TargetObject) { Write-Verbose "Creatng empty file(s): $nonExistingPaths" $null = New-Item $nonExistingPaths } } ,请尝试将其更改为 from tensorflow.keras.preprocessing.image import ImageDataGenerator,反之亦然。为我工作。它说你永远不应该混合使用 keras 和 tensorflow。

答案 3 :(得分:-1)

我遇到了同样的问题。 这是因为模型在验证数据集上运行,这通常需要很多时间。尝试减少验证数据集,或等待一段时间它对我有用。看起来好像卡住了,但它正在验证数据集上运行。