Sree
Sree

Reputation: 983

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. But when I tried to load the model, I am getting the error: 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:

 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. 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]})

Upvotes: 2

Views: 3453

Answers (3)

hp_elite
hp_elite

Reputation: 188

Update your Tensorflow and Keras package to new versions.

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

Check this link for using Keras inbuilt - Metrics for precision, recall, AUC etc.

How to compute Receiving Operating Characteristic (ROC) and AUC in keras?

Upvotes: 0

jeicy
jeicy

Reputation: 21

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:

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()})

Upvotes: 2

AG_Gurgaon
AG_Gurgaon

Reputation: 19

Please try simply loading the model without custom objects. In your case, it seems you are using KEras defined metrics only and no custom metrics. That may be the reason you it is not able to find the function. If still not resolved, please share the complete code.

Upvotes: 0

Related Questions