raghakot / keras-vis

Neural network visualization toolkit for keras
https://raghakot.github.io/keras-vis
MIT License
2.97k stars 664 forks source link

backprop_modifier error #142

Open jinchenglee opened 5 years ago

jinchenglee commented 5 years ago

To cumbersome to share my code here, but the problem is that customized metrics cannot be understood by the model rebuilt process incurred by modify_model_backprop().

~/.virtualenvs/cv/lib/python3.5/site-packages/keras_vis-0.4.1-py3.5.egg/vis/backprop_modifiers.py in guided(model)
     15         (https://arxiv.org/pdf/1412.6806.pdf)
     16     """
---> 17     return backend.modify_model_backprop(model, 'guided')
     18 
     19 

~/.virtualenvs/cv/lib/python3.5/site-packages/keras_vis-0.4.1-py3.5.egg/vis/backend/tensorflow_backend.py in modify_model_backprop(model, backprop_modifier)
     95         with tf.get_default_graph().gradient_override_map({'Relu': backprop_modifier}):
     96             #  This should rebuild graph with modifications.
---> 97             modified_model = load_model(model_path)
     98 
     99             # Cache to improve subsequent call performance.

~/.virtualenvs/cv/lib/python3.5/site-packages/keras/engine/saving.py in load_model(filepath, custom_objects, compile)
    287                           metrics=metrics,
    288                           loss_weights=loss_weights,
--> 289                           sample_weight_mode=sample_weight_mode)
    290 
    291             # Set optimizer weights.

~/.virtualenvs/cv/lib/python3.5/site-packages/keras/engine/training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, **kwargs)
    438                 output_metrics = nested_metrics[i]
    439                 output_weighted_metrics = nested_weighted_metrics[i]
--> 440                 handle_metrics(output_metrics)
    441                 handle_metrics(output_weighted_metrics, weights=weights)
    442 

~/.virtualenvs/cv/lib/python3.5/site-packages/keras/engine/training.py in handle_metrics(metrics, weights)
    395                     metric_name = metric_name_prefix + suffix
    396                 else:
--> 397                     metric_fn = metrics_module.get(metric)
    398                     weighted_metric_fn = weighted_masked_objective(metric_fn)
    399                     # Get metric name as string

~/.virtualenvs/cv/lib/python3.5/site-packages/keras/metrics.py in get(identifier)
     73         return deserialize(config)
     74     elif isinstance(identifier, six.string_types):
---> 75         return deserialize(str(identifier))
     76     elif callable(identifier):
     77         return identifier

~/.virtualenvs/cv/lib/python3.5/site-packages/keras/metrics.py in deserialize(config, custom_objects)
     65                                     module_objects=globals(),
     66                                     custom_objects=custom_objects,
---> 67                                     printable_module_name='metric function')
     68 
     69 

~/.virtualenvs/cv/lib/python3.5/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    163             if fn is None:
    164                 raise ValueError('Unknown ' + printable_module_name +
--> 165                                  ':' + function_name)
    166         return fn
    167     else:

ValueError: Unknown metric function:fbeta
jinchenglee commented 5 years ago

Keras provides customized metric argument while loading model. For example, https://github.com/keras-team/keras/issues/3911

IMHO, keras-vis could extract customized metrics from models being loaded, then call API appropriately.

keisen commented 5 years ago

Hi, @jinchenglee . Thank you for your reporting.

Could you try below way? I think this solution is so smart if it's able to solve your problem. https://github.com/raghakot/keras-vis/issues/80#issuecomment-423529695