keisen / tf-keras-vis

Neural network visualization toolkit for tf.keras
https://keisen.github.io/tf-keras-vis-docs/
MIT License
311 stars 45 forks source link

GradCam for custom models #11

Closed koriavinash1 closed 4 years ago

koriavinash1 commented 4 years ago

I'm trying to use gradcam for my custom model with binary class output

I'm getting the following error, please let me know how to fix this:

---> 25     cam = gradcam(lambda x: x, ds[weights])
     26     cam = normalize(cam)
     27     print(cam.shape)

~/miniconda/envs/bioexp/lib/python3.6/site-packages/tf_keras_vis/gradcam.py in __call__(self, loss, seed_input, penultimate_layer, seek_penultimate_layer, activation_modifier, normalize_gradient)
     57             loss_values = [loss(y) for y, loss in zip(outputs[:-1], losses)]
     58             penultimate_outputs = outputs[-1]
---> 59         grads = tape.gradient(loss_values, penultimate_outputs)
     60         if normalize_gradient:
     61             grads = K.l2_normalize(grads)

~/miniconda/envs/bioexp/lib/python3.6/site-packages/tensorflow_core/python/eager/backprop.py in gradient(self, target, sources, output_gradients, unconnected_gradients)
   1013         output_gradients=output_gradients,
   1014         sources_raw=flat_sources_raw,
-> 1015         unconnected_gradients=unconnected_gradients)
   1016 
   1017     if not self._persistent:

~/miniconda/envs/bioexp/lib/python3.6/site-packages/tensorflow_core/python/eager/imperative_grad.py in imperative_grad(tape, target, sources, output_gradients, sources_raw, unconnected_gradients)
     74       output_gradients,
     75       sources_raw,
---> 76       compat.as_str(unconnected_gradients.value))

~/miniconda/envs/bioexp/lib/python3.6/site-packages/tensorflow_core/python/eager/backprop.py in _gradient_function(op_name, attr_tuple, num_inputs, inputs, outputs, out_grads, skip_input_indices)
    136     return [None] * num_inputs
    137 
--> 138   return grad_fn(mock_op, *out_grads)
    139 
    140 

~/miniconda/envs/bioexp/lib/python3.6/site-packages/tensorflow_core/python/ops/control_flow_grad.py in _SwitchGrad(op, *grad)
     42   graph = ops.get_default_graph()
     43   # pylint: disable=protected-access
---> 44   op_ctxt = op._get_control_flow_context()
     45   grad_ctxt = graph._get_control_flow_context()
     46   # pylint: enable=protected-access

~/miniconda/envs/bioexp/lib/python3.6/site-packages/tensorflow_core/python/eager/backprop.py in _get_control_flow_context(self)
    107   def _get_control_flow_context(self):
    108     raise NotImplementedError(
--> 109         "tf.GradientTape.gradients() does not support graph control flow "
    110         "operations like tf.cond or tf.while at this time. Use tf.gradients() "
    111         "instead. If you need this feature, please file a feature request at "

NotImplementedError: tf.GradientTape.gradients() does not support graph control flow operations like tf.cond or tf.while at this time. Use tf.gradients() instead. If you need this feature, please file a feature request at https://github.com/tensorflow/tensorflow/issues/new

Thank you Avinash

keisen commented 4 years ago

Thank you for your reporting. Could you please let me know the versions of Tensorflow and tf-keras-vis that you use.

koriavinash1 commented 4 years ago

Hi @keisen,

sorry to bother, I could fix this by updating tf version to 2.1 and the latest version of tf-keras-vis

keisen commented 4 years ago

I'm glad it's okay. Thank you letting me know that.