keract: Using an input other than the one provided by the pre-trained model fails.
I have a fine-tuned resnet50 model which i I’m trying to visualize the activations, although I’m facing a few issues when calling get_activations function. Code and output below:
Building model fn:
def load_and_configure_model(model_name, optimizer, loss, metrics, path):
model = 0
num_classes = 250
if model_name == 'resnet50':
model = ResNet50(include_top=True, weights='imagenet')
transfer_layer = model.get_layer('avg_pool')
conv_model = Model(inputs=model.input,outputs=transfer_layer.output)
conv_model.trainable = True
for layer in conv_model.layers:
layer.trainable = False
for layer in conv_model.layers:
trainable = ('conv5_block3' in layer.name)
layer.trainable = trainable
transferred_resnet50 = Sequential()
transferred_resnet50.add(conv_model)
transferred_resnet50.add(Dense(num_classes, activation='softmax'))
transferred_resnet50.compile(optimizer=optimizer, loss=loss, metrics=metrics)
model = transferred_resnet50
if not path == None :
model.load_weights(path)
return model
Loading model & img
weight_file = DRIVE_DIR+'ImageCLEF2013PlantTask/Models/ResNet50/crop_and_translate/resnet_50.h5'
loss = tf.keras.losses.sparse_categorical_crossentropy
metrics = ['accuracy']
optimizer = Adam(lr=2e-6)
resnet_50_crop_and_translate = load_and_configure_model('resnet50', optimizer, loss, metrics, weight_file)
resnet_50_crop_and_translate.compile(optimizer=optimizer,loss=loss)
img = cv2.imread(DRIVE_DIR + DATA_DIR+'/visualization/test_image.jpg',cv2.IMREAD_UNCHANGED)
res = cv2.resize(img, dsize=(224, 224))
activations = keract.get_activations(resnet_50_crop_and_translate, np.expand_dims(res,axis=0), nodes_to_evaluate=None, output_format='full', nested=False, auto_compile=False)
First Error, although warnings concerning eager execution and other related exceptions seemed also to be thrown:
Run it without eager mode. Paste those commands at the beginning of your script:
> import tensorflow as tf
> tf.compat.v1.disable_eager_execution()
Run it without eager mode. Paste those commands at the beginning of your script:
> import tensorflow as tf
> tf.compat.v1.disable_eager_execution()
ValueError: Tensor Tensor("avg_pool_1/Mean:0", shape=(None, 2048), dtype=float32) is not an element of this graph.
Then when I have also tried:
import keras.backend as K
K.clear_session()
activations = keract.get_activations(resnet_50_crop_and_translate, np.expand_dims(res,axis=0), nodes_to_evaluate=None, output_format='full', nested=False, auto_compile=False)
Which raised:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-17-cb7cc7958838> in <module>()
----> 1 activations = keract.get_activations(resnet_50_crop_and_translate, np.expand_dims(res,axis=0),layer_names=['conv5_block3_out'], nodes_to_evaluate=None, output_format='full', nested=False, auto_compile=False)
/usr/local/lib/python3.6/dist-packages/keract/keract.py in get_activations(model, x, layer_names, nodes_to_evaluate, output_format, nested, auto_compile)
319 network_layers = ', '.join([layer.name for layer in model.layers])
320 raise KeyError('Could not find a layer with name: [{}]. '
--> 321 'Network layers are [{}]'.format(', '.join(layer_names), network_layers))
322 else:
323 raise ValueError('Nodes list is empty. Or maybe the model is empty.')
KeyError: 'Could not find a layer with name: [conv5_block3_out]. Network layers are [model_1, dense_1]'
Full trace from error no. 1 (without specifying layer names):
Run it without eager mode. Paste those commands at the beginning of your script:
> import tensorflow as tf
> tf.compat.v1.disable_eager_execution()
Run it without eager mode. Paste those commands at the beginning of your script:
> import tensorflow as tf
> tf.compat.v1.disable_eager_execution()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/keract/keract.py in _evaluate(model, nodes_to_evaluate, x, y, auto_compile)
102 try:
--> 103 return eval_fn(model._feed_inputs + model._feed_targets + model._feed_sample_weights)
104 except Exception:
18 frames
/usr/local/lib/python3.6/dist-packages/keract/keract.py in eval_fn(k_inputs)
99 print('> tf.compat.v1.disable_eager_execution()')
--> 100 raise e
101
/usr/local/lib/python3.6/dist-packages/keract/keract.py in eval_fn(k_inputs)
90 try:
---> 91 return K.function(k_inputs, nodes_to_evaluate)(model._standardize_user_data(x, y))
92 except AttributeError: # one way to avoid forcing non eager mode.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py in function(inputs, outputs, updates, name, **kwargs)
4086 return GraphExecutionFunction(
-> 4087 inputs, outputs, updates=updates, name=name, **kwargs)
4088
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py in __init__(self, inputs, outputs, updates, name, **session_kwargs)
3809 # Index 0 = total loss or model output for `predict`.
-> 3810 with ops.control_dependencies([self.outputs[0]]):
3811 updates_ops = []
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in control_dependencies(control_inputs)
5358 else:
-> 5359 return get_default_graph().control_dependencies(control_inputs)
5360
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in control_dependencies(self, control_inputs)
4814 c = c.op
-> 4815 c = self.as_graph_element(c)
4816 if isinstance(c, Tensor):
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in as_graph_element(self, obj, allow_tensor, allow_operation)
3725 with self._lock:
-> 3726 return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
3727
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in _as_graph_element_locked(self, obj, allow_tensor, allow_operation)
3804 if obj.graph is not self:
-> 3805 raise ValueError("Tensor %s is not an element of this graph." % obj)
3806 return obj
ValueError: Tensor Tensor("avg_pool_1/Mean:0", shape=(None, 2048), dtype=float32) is not an element of this graph.
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-18-1fc67112db22> in <module>()
----> 1 activations = keract.get_activations(resnet_50_crop_and_translate, np.expand_dims(res,axis=0), nodes_to_evaluate=None, output_format='full', nested=False, auto_compile=False)
/usr/local/lib/python3.6/dist-packages/keract/keract.py in get_activations(model, x, layer_names, nodes_to_evaluate, output_format, nested, auto_compile)
347
348 if len(layer_outputs) > 0:
--> 349 activations = _evaluate(model, layer_outputs.values(), x, y=None, auto_compile=auto_compile)
350 else:
351 activations = {}
/usr/local/lib/python3.6/dist-packages/keract/keract.py in _evaluate(model, nodes_to_evaluate, x, y, auto_compile)
114 acts.append(n.numpy())
115 return acts
--> 116 return eval_fn(model._feed_inputs)
117
118
/usr/local/lib/python3.6/dist-packages/keract/keract.py in eval_fn(k_inputs)
98 print('> import tensorflow as tf')
99 print('> tf.compat.v1.disable_eager_execution()')
--> 100 raise e
101
102 try:
/usr/local/lib/python3.6/dist-packages/keract/keract.py in eval_fn(k_inputs)
89 def eval_fn(k_inputs):
90 try:
---> 91 return K.function(k_inputs, nodes_to_evaluate)(model._standardize_user_data(x, y))
92 except AttributeError: # one way to avoid forcing non eager mode.
93 if y is None: # tf 2.3.0 upgrade compatibility.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py in function(inputs, outputs, updates, name, **kwargs)
4085 raise ValueError(msg)
4086 return GraphExecutionFunction(
-> 4087 inputs, outputs, updates=updates, name=name, **kwargs)
4088
4089
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py in __init__(self, inputs, outputs, updates, name, **session_kwargs)
3808 # dependencies in call.
3809 # Index 0 = total loss or model output for `predict`.
-> 3810 with ops.control_dependencies([self.outputs[0]]):
3811 updates_ops = []
3812 for update in updates:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in control_dependencies(control_inputs)
5357 return NullContextmanager()
5358 else:
-> 5359 return get_default_graph().control_dependencies(control_inputs)
5360
5361
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in control_dependencies(self, control_inputs)
4813 (hasattr(c, "_handle") and hasattr(c, "op"))):
4814 c = c.op
-> 4815 c = self.as_graph_element(c)
4816 if isinstance(c, Tensor):
4817 c = c.op
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in as_graph_element(self, obj, allow_tensor, allow_operation)
3724
3725 with self._lock:
-> 3726 return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
3727
3728 def _as_graph_element_locked(self, obj, allow_tensor, allow_operation):
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in _as_graph_element_locked(self, obj, allow_tensor, allow_operation)
3803 # Actually obj is just the object it's referring to.
3804 if obj.graph is not self:
-> 3805 raise ValueError("Tensor %s is not an element of this graph." % obj)
3806 return obj
3807 elif isinstance(obj, Operation) and allow_operation:
ValueError: Tensor Tensor("avg_pool_1/Mean:0", shape=(None, 2048), dtype=float32) is not an element of this graph.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 20 (10 by maintainers)
But that solution should save you for now. The trick is to use the default Input tensor provided by the pre-trained model and push extra information (push num_slices into the first dimension), calling the pre-trained model and un-squeeze the first dimension to recover batch_size and num_slices.
From this reference: https://keras.io/api/applications/
You can do it easily like that:
By the way those are the shapes of each layer output for ResNet50:
Nevermind what i said earlier, i realized that i was returning the model before loading the weights.
When in fact after trying to get the activations from the fine-tuned model leads to the same error:
Python Invalid argument: You must feed a value for placeholder tensor 'input_1' with dtype float and shape [?,224,224,3]Great! Keras can sometimes be a pain to use.
@FalsoMoralista great! Ideally your case should be properly handled but it’s quite a pain to handle all the cases.
You can also follow some of the examples in: https://github.com/philipperemy/keract/blob/20846aaed0da68d5c328864b56d9a07fd696a76c/tests/get_activations_test.py#L16