简体   繁体   English

ValueError:未知指标 function:Keras 中的二进制精度错误,即使不使用任何自定义指标也是如此

[英]ValueError: Unknown metric function:binary_precision error in Keras even when not using any custom metric

I trained a cnn model with precision and recall metrics which are imported from keras_metrics .我用从keras_metrics导入的precisionrecall指标训练了一个 cnn model。 But when I tried to load the model, I am getting the error: ValueError: Unknown metric function:binary_precision .但是,当我尝试加载 model 时,出现错误: ValueError: Unknown metric function:binary_precision I have not used binary_precision function anywhere but still I gave `binary_precision' in custom_objects as per the error:我没有在任何地方使用binary_precision function 但我仍然根据错误在 custom_objects 中给出了“binary_precision”:

 model = load_model(modelfilepath,custom_objects={'metrics': [keras_metrics.precision(),binary_precision]})

With this I am getting the error, NameError: name 'binary_precision' is not defined .有了这个我得到了错误, NameError: name 'binary_precision' is not defined What should I do for this.我应该为此做些什么。

My code is as follows:我的代码如下:

#compiling the model
model.compile(optimizer=SGD(),loss='binary_crossentropy',metrics = ['accuracy',keras_metrics.precision(),keras_metrics.recall()])
#loading the model after training and saving it
model = load_model(modelfilepath,custom_objects={'metrics': [keras_metrics.precision(),binary_precision]})

I found a solution in https://github.com/netrack/keras-metrics/issues/27 : the author add name attribute in the module, so we can call metrics function by its name when loading model:我在https://github.com/netrack/keras-metrics/issues/27中找到了一个解决方案:作者在模块中添加了name属性,因此我们可以在加载 Z20F35E630DAF494DFA4 时通过其名称调用指标 function

import keras_metrics as km

# compile model
model.compile(loss='binary_crossentropy',optimizer='Adam',metrics=['acc', km.binary_precision(), km.binary_recall()])

# load model
model = load_model(os.path.join(FLAGS.init_checkpoint,model_name), custom_objects={'binary_precision':km.binary_precision(), 'binary_recall':km.binary_recall()})

Please try simply loading the model without custom objects.请尝试在没有自定义对象的情况下简单地加载 model。 In your case, it seems you are using KEras defined metrics only and no custom metrics.在您的情况下,您似乎只使用 KEras 定义的指标,而没有自定义指标。 That may be the reason you it is not able to find the function.这可能是您找不到 function 的原因。 If still not resolved, please share the complete code.如果还没有解决,请分享完整代码。

Update your Tensorflow and Keras package to new versions.将您的 Tensorflow 和 Keras package 更新到新版本。

1. pip install --upgrade tensorflow 
2. pip install --upgrade keras

Check this link for using Keras inbuilt - Metrics for precision, recall, AUC etc.检查此链接以使用内置 Keras - 精度、召回、AUC 等指标。

How to compute Receiving Operating Characteristic (ROC) and AUC in keras? 如何计算 keras 中的接收操作特性 (ROC) 和 AUC?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM