我正在训练一个 VGG16 微调模型。在第一个 epoch 之后,程序停止并给出了这个错误:
下面是我用于模型的代码:
# create a copy of a mobilenet model
import keras
vgg_model=keras.applications.vgg16.VGG16()
type(vgg_model)
vgg_model.summary()
from keras.models import Sequential
model = Sequential()
for layer in vgg_model.layers[:-1]:
model.add(layer)
model.summary()
# CREATE THE MODEL ARCHITECTURE
from keras.layers import Dense, Activation, Dropout
model.add(Dropout(0.25))
model.add(Dense(7,activation='softmax'))
model.summary()
#Train the Model
# Define Top2 and Top3 Accuracy
from keras.metrics import categorical_accuracy, top_k_categorical_accuracy
def top_3_accuracy(y_true, y_pred):
return top_k_categorical_accuracy(y_true, y_pred, k=3)
def top_2_accuracy(y_true, y_pred):
return top_k_categorical_accuracy(y_true, y_pred, k=2)
from keras.optimizers import Adam
model.compile(Adam(lr=0.01), loss='categorical_crossentropy',
metrics=[categorical_accuracy, top_2_accuracy, top_3_accuracy])
# Get the labels that are associated with each index
print(valid_batches.class_indices)
# Add weights to try to make the model more sensitive to melanoma
class_weights={
0: 1.0, # akiec
1: 1.0, # bcc
2: 1.0, # bkl
3: 1.0, # df
4: 3.0, # mel # Try to make the model more sensitive to Melanoma.
5: 1.0, # nv
6: 1.0, # vasc
}
filepath = "skin.h5"
checkpoint = ModelCheckpoint(filepath, monitor='val_top_3_accuracy', verbose=1,
save_best_only=True, mode='max')
reduce_lr = ReduceLROnPlateau(monitor='val_top_3_accuracy', factor=0.5, patience=2,
verbose=1, mode='max', min_lr=0.00001)
callbacks_list = [checkpoint, reduce_lr]
history = model.fit_generator(train_batches, steps_per_epoch=train_steps,
class_weight=class_weights,
validation_data=valid_batches,
validation_steps=val_steps,
epochs=40, verbose=1,
callbacks=callbacks_list)
我正在尝试学习如何在图像数据集上微调、训练和使用 VGG16 模型。我在关注这个blog他使用 mobileNet 的地方。
我在关注这个 VGG16 tutorial为模型编写代码。
如果有人可以帮助我修复此错误或解释它是如何以及为什么发生的,我将非常感谢您的帮助。
依赖关系:
- tensorflow 1.12.0
- tensorflow-GPU 1.12.0
- python 3.6.0
- keras 2.2.4
最佳答案
我在使用 ReduceLROnPlateau
回调时遇到了同样的错误。除非绝对必要,否则您可以省略它的用法。
关于tensorflow - 失败前提条件错误 : Attempting to use uninitialized value Adam/lr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55345211/