【发布时间】:2019-08-16 03:09:44
【问题描述】:
我正在训练一个 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 为模型编写代码。
如果有人可以帮助我解决此错误或解释它是如何发生的以及为什么会发生,我将非常感谢您的帮助。
依赖关系:
- 张量流 1.12.0
- tensorflow-gpu 1.12.0
- python 3.6.0
- keras 2.2.4
【问题讨论】:
-
您是否尝试加载预训练的权重?
标签: tensorflow keras training-data vgg-net