英文:
VGG16 Transfer Learning - Unkown Metric function: f1_score error
问题
以下是已翻译的内容:
"我有一个迁移学习模型,我在其中使用了VGG16,然后添加了一些密集层、丢失层和批次归一化层。我训练了模型并将其保存为 Study3_v1.h5,使用命令 model.save('Study3_v1.h5')
。然而,当我尝试使用命令 model = tf.keras.models.load_model('Study3_v1.h5')
加载它时,出现了以下错误:"
"ValueError: Unknown metric function: f1_score. Please ensure this object is passed to the
custom_objects argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
"
"我认为导致错误的原因可能是在编译和训练模型之前使用的回调或度量标准,它们的代码如下:"
def f1_score(y_true, y_pred): #取自旧的Keras源代码
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
recall = true_positives / (possible_positives + K.epsilon())
f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())
return f1_val
METRICS = [
tf.keras.metrics.BinaryAccuracy(name='accuracy'),
tf.keras.metrics.Precision(name='precision'),
tf.keras.metrics.Recall(name='recall'),
tf.keras.metrics.AUC(name='auc'),
f1_score,
]
lrd = ReduceLROnPlateau(monitor = 'val_loss',patience = 20,verbose = 1,factor = 0.50, min_lr = 1e-10)
mcp = ModelCheckpoint('model.h5')
es = EarlyStopping(verbose=1, patience=20)
"编译模型:"
model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=METRICS)
"训练模型:"
history=model.fit(train_dataset,validation_data=valid_dataset,epochs = 5,verbose = 1,callbacks=[lrd,mcp,es])
"导致错误的原因是什么?我需要向 load_model
函数添加哪些自定义对象?"
提前感谢您!
英文:
I have a transfer learning model where I used VGG16 and then added some dense, dropout and batch_normalization layers to. I trained the model and saved it as Study3_v1.h5 using the model.save('Study3_v1.h5')
command.
However, when I tried to load it using the model = tf.keras.models.load_model('Study3_v1.h5')
command it gave me the error below:
ValueError: Unknown metric function: f1_score. Please ensure this object is passed to the
custom_objects argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
What I think is causing the error are the callbacks or the metrics I used before compiling and training my model, their codes are below:
def f1_score(y_true, y_pred): #taken from old keras source code
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
recall = true_positives / (possible_positives + K.epsilon())
f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())
return f1_val
METRICS = [
tf.keras.metrics.BinaryAccuracy(name='accuracy'),
tf.keras.metrics.Precision(name='precision'),
tf.keras.metrics.Recall(name='recall'),
tf.keras.metrics.AUC(name='auc'),
f1_score,
]
lrd = ReduceLROnPlateau(monitor = 'val_loss',patience = 20,verbose = 1,factor = 0.50, min_lr = 1e-10)
mcp = ModelCheckpoint('model.h5')
es = EarlyStopping(verbose=1, patience=20)
Compiling the model:
model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=METRICS)
Training the model:
history=model.fit(train_dataset,validation_data=valid_dataset,epochs = 5,verbose = 1,callbacks=[lrd,mcp,es])
What is causing the error? And what are the custom objects that I need to add to the load_model
function
Thanks in advance!
答案1
得分: 1
因为您使用了自定义指标。所以现在您需要执行以下操作:
model = tf.keras.models.load_model('Study3_v1.h5', custom_objects={"f1_score": f1_score})
每当您有一个自定义对象,例如自定义模型、层、指标...,如果您将模型保存为 h5 格式,就需要执行这个操作。
英文:
Because you used a custom metric. So now you need to do this:
model = tf.keras.models.load_model('Study3_v1.h5',custom_objects={"f1_score":f1_score})
Whenever you have a custom object, like a custom model, layer, metrics... you need to do this if you save your model in h5.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论