FailedPreconditionError:尝试使用未初始化的值Adam / lr

时间:2019-03-25 19:35:13

标签: tensorflow keras training-data vgg-net finetunning

你好,我是机器学习的新手。我正在训练VGG16微调模型。在第一个时期之后,程序停止并给出了以下错误:

Screenshot of the error on jupyter notebook

以下是我用于模型的代码

# 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模型。我正在他使用mobileNet的blog后面。

我正在遵循这个VGG16 tutorial来编写模型代码。

如果有人可以帮助我纠正此错误或解释发生这种错误的方式和原因,我将非常感谢您的帮助。

非常感谢您。

依赖项:

  • tensorflow 1.12.0
  • tensorflow-gpu 1.12.0
  • python 3.6.0
  • keras 2.2.4

1 个答案:

答案 0 :(得分:1)

使用ReduceLROnPlateau回调时,我遇到了相同的错误。除非绝对必要,否则您可以省略其用法。