raghakot / keras-vis

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

How to use custom objects for the model in the apply_modifications method #80

Closed mahesh-infrrd closed 5 years ago

mahesh-infrrd commented 6 years ago

I'm using Keras 2.0.6 and have trained a model based on the Inception-V3 architecture with custom objects for metrics. As it is mentioned in the Keras documentation, the custom objects need to be used in load_model method. But in Keras-Vis the apply_modifications method only accepts the model, not the custom objects after swapping the activation layers.

model = utils.apply_modifications(model)

ValueError: Unknown metric function:precision

wdeback commented 6 years ago

Had the same issue with MobileNet.

My solution is to save and reload the model yourself, which is what apply_modifications does. In the load_model step, you can use your custom_objects.

# modify model, here swapping softmax for linear activation
model.layers[-1].activation = activations.linear
# save model to a temp file
model.save('temp.h5')
# reload model with `custom_objects`
model = load_model('temp.h5', custom_objects={'relu6': relu6, 'DepthwiseConv2D': DepthwiseConv2D})
# maybe remove the temp file
saksham-s commented 6 years ago

@wdeback the visualize_saliency is giving error due to the custom layer. Any solutions?

maor121 commented 5 years ago

@saksham15082 I ran into the same problem, visualize_saliency of mobilenet, with guided backprop_modifier.

Solution:

from keras.utils import CustomObjectScope

with CustomObjectScope({'relu6': relu6}): 
    grads =  visualize_saliency(model, layer_idx, filter_indices=class_idx, seed_input=inp, backprop_modifier='guided') 
keisen commented 5 years ago

@maor121 's solution is so smart.

I believe this issue is resolved. If there is still the problem, feel free to reopen this issue.