Closed asittiwari07 closed 3 years ago
Hi @asittiwari07 Can not reproduce the error.
there's a discontinuity in your model.
Got exactly the same issue. @Hassanfarooq92 not sure what you mean by discontinuity, but the model I'm using has multiple binary outputs.
Tried to test with vanilla InceptionV3 and got a bit better message after restarting the notebook (see below). I suspect something changed after upgrading to TF 2.3.x since I had this working with older versions of TF 2.x.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in
2
3 explainer = GradCAM()
----> 4 grid = explainer.explain(image_exp, model, class_index=1)
~/.virtualenvs/tf2/lib/python3.7/site-packages/tf_explain/core/grad_cam.py in explain(self, validation_data, model, class_index, layer_name, colormap, image_weight)
51
52 outputs, guided_grads = GradCAM.get_gradients_and_filters(
---> 53 model, images, layer_name, class_index
54 )
55
~/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
778 else:
779 compiler = "nonXla"
--> 780 result = self._call(*args, **kwds)
781
782 new_tracing_count = self._get_tracing_count()
~/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
838 # Lifting succeeded, so variables are initialized and we can run the
839 # stateless function.
--> 840 return self._stateless_fn(*args, **kwds)
841 else:
842 canon_args, canon_kwds = \
~/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/eager/function.py in __call__(self, *args, **kwargs)
2826 """Calls a graph function specialized to the inputs."""
2827 with self._lock:
-> 2828 graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
2829 return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access
2830
~/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
3211
3212 self._function_cache.missed.add(call_context_key)
-> 3213 graph_function = self._create_graph_function(args, kwargs)
3214 self._function_cache.primary[cache_key] = graph_function
3215 return graph_function, args, kwargs
~/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
3073 arg_names=arg_names,
3074 override_flat_arg_shapes=override_flat_arg_shapes,
-> 3075 capture_by_value=self._capture_by_value),
3076 self._function_attributes,
3077 function_spec=self.function_spec,
~/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
984 _, original_func = tf_decorator.unwrap(python_func)
985
--> 986 func_outputs = python_func(*func_args, **func_kwargs)
987
988 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
~/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds)
598 # __wrapped__ allows AutoGraph to swap in a converted function. We give
599 # the function a weak reference to itself to avoid a reference cycle.
--> 600 return weak_wrapped_fn().__wrapped__(*args, **kwds)
601 weak_wrapped_fn = weakref.ref(wrapped_fn)
602
~/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
971 except Exception as e: # pylint:disable=broad-except
972 if hasattr(e, "ag_error_metadata"):
--> 973 raise e.ag_error_metadata.to_exception(e)
974 else:
975 raise
ValueError: in user code:
/Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tf_explain/core/grad_cam.py:106 get_gradients_and_filters *
grad_model = tf.keras.models.Model(
/Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:242 __new__ **
return functional.Functional(*args, **kwargs)
/Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py:457 _method_wrapper
result = method(self, *args, **kwargs)
/Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/keras/engine/functional.py:114 __init__
super(Functional, self).__init__(name=name, trainable=trainable)
/Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py:457 _method_wrapper
result = method(self, *args, **kwargs)
/Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:308 __init__
self._init_batch_counters()
/Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py:457 _method_wrapper
result = method(self, *args, **kwargs)
/Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:317 _init_batch_counters
self._train_counter = variables.Variable(0, dtype='int64', aggregation=agg)
/Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/ops/variables.py:262 __call__
return cls._variable_v2_call(*args, **kwargs)
/Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/ops/variables.py:256 _variable_v2_call
shape=shape)
/Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/ops/variables.py:67 getter
return captured_getter(captured_previous, **kwargs)
/Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py:702 invalid_creator_scope
"tf.function-decorated function tried to create "
ValueError: tf.function-decorated function tried to create variables on non-first call.
@rstml same as #149
Tried to test with vanilla InceptionV3 and got a bit better message after restarting the notebook (see below). I suspect something changed after upgrading to TF 2.3.x since I had this working with older versions of TF 2.x.
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) in 2 3 explainer = GradCAM() ----> 4 grid = explainer.explain(image_exp, model, class_index=1) ~/.virtualenvs/tf2/lib/python3.7/site-packages/tf_explain/core/grad_cam.py in explain(self, validation_data, model, class_index, layer_name, colormap, image_weight) 51 52 outputs, guided_grads = GradCAM.get_gradients_and_filters( ---> 53 model, images, layer_name, class_index 54 ) 55 ~/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds) 778 else: 779 compiler = "nonXla" --> 780 result = self._call(*args, **kwds) 781 782 new_tracing_count = self._get_tracing_count() ~/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds) 838 # Lifting succeeded, so variables are initialized and we can run the 839 # stateless function. --> 840 return self._stateless_fn(*args, **kwds) 841 else: 842 canon_args, canon_kwds = \ ~/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/eager/function.py in __call__(self, *args, **kwargs) 2826 """Calls a graph function specialized to the inputs.""" 2827 with self._lock: -> 2828 graph_function, args, kwargs = self._maybe_define_function(args, kwargs) 2829 return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access 2830 ~/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs) 3211 3212 self._function_cache.missed.add(call_context_key) -> 3213 graph_function = self._create_graph_function(args, kwargs) 3214 self._function_cache.primary[cache_key] = graph_function 3215 return graph_function, args, kwargs ~/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes) 3073 arg_names=arg_names, 3074 override_flat_arg_shapes=override_flat_arg_shapes, -> 3075 capture_by_value=self._capture_by_value), 3076 self._function_attributes, 3077 function_spec=self.function_spec, ~/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes) 984 _, original_func = tf_decorator.unwrap(python_func) 985 --> 986 func_outputs = python_func(*func_args, **func_kwargs) 987 988 # invariant: `func_outputs` contains only Tensors, CompositeTensors, ~/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds) 598 # __wrapped__ allows AutoGraph to swap in a converted function. We give 599 # the function a weak reference to itself to avoid a reference cycle. --> 600 return weak_wrapped_fn().__wrapped__(*args, **kwds) 601 weak_wrapped_fn = weakref.ref(wrapped_fn) 602 ~/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs) 971 except Exception as e: # pylint:disable=broad-except 972 if hasattr(e, "ag_error_metadata"): --> 973 raise e.ag_error_metadata.to_exception(e) 974 else: 975 raise ValueError: in user code: /Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tf_explain/core/grad_cam.py:106 get_gradients_and_filters * grad_model = tf.keras.models.Model( /Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:242 __new__ ** return functional.Functional(*args, **kwargs) /Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py:457 _method_wrapper result = method(self, *args, **kwargs) /Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/keras/engine/functional.py:114 __init__ super(Functional, self).__init__(name=name, trainable=trainable) /Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py:457 _method_wrapper result = method(self, *args, **kwargs) /Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:308 __init__ self._init_batch_counters() /Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py:457 _method_wrapper result = method(self, *args, **kwargs) /Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:317 _init_batch_counters self._train_counter = variables.Variable(0, dtype='int64', aggregation=agg) /Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/ops/variables.py:262 __call__ return cls._variable_v2_call(*args, **kwargs) /Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/ops/variables.py:256 _variable_v2_call shape=shape) /Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/ops/variables.py:67 getter return captured_getter(captured_previous, **kwargs) /Users/rustam/.virtualenvs/tf2/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py:702 invalid_creator_scope "tf.function-decorated function tried to create " ValueError: tf.function-decorated function tried to create variables on non-first call.
Thanks @vscv , removing tf.function
fixed it.
Fix is about to be merged, thanks for raising this!
https://github.com/sicara/tf-explain/blob/8dff129ff7b1012dba2761a61e3c3e68e9ecbec2/examples/core/grad_cam.py#L19
Any Idea what could be the issue?