修正layer_name = 'block1_conv2'
原始程式碼:
layer_name = 'block1_conv2'
layer = layer_dict[layer_name]
activations = get_activations(vgg16, layer, input_img_data)
出現錯誤:
ValueError Traceback (most recent call last)
Cell In[35], line 3
1 layer_name = 'block1_conv2'
2 layer = layer_dict[layer_name]
----> 3 activations = get_activations(vgg16, layer, input_img_data)
Cell In[34], line 2, in get_activations(model, layer, input_img_data)
1 def get_activations(model, layer, input_img_data):
----> 2 activations_f = K.function([model.layers[0].input, K.learning_phase()], [layer.output,])
3 activations = activations_f((input_img_data, False))
4 return activations
File C:\ProgramData\Anaconda3\lib\site-packages\keras\src\backend.py:4656, in function(inputs, outputs, updates, name, **kwargs)
4650 raise ValueError(
4651 "`updates` argument is not supported during "
4652 "eager execution. You passed: %s" % (updates,)
4653 )
4654 from keras.src import models
-> 4656 model = models.Model(inputs=inputs, outputs=outputs)
4658 wrap_outputs = isinstance(outputs, list) and len(outputs) == 1
4660 def func(model_inputs):
File C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\trackable\base.py:204, in no_automatic_dependency_tracking.<locals>._method_wrapper(self, *args, **kwargs)
202 self._self_setattr_tracking = False # pylint: disable=protected-access
203 try:
--> 204 result = method(self, *args, **kwargs)
205 finally:
206 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
File C:\ProgramData\Anaconda3\lib\site-packages\keras\src\engine\functional.py:159, in Functional.__init__(self, inputs, outputs, name, trainable, **kwargs)
153 # Check if the inputs contain any intermediate `KerasTensor` (not
154 # created by tf.keras.Input()). In this case we need to clone the `Node`
155 # and `KerasTensor` objects to mimic rebuilding a new model from new
156 # inputs. This feature is only enabled in TF2 not in v1 graph mode.
157 if tf.compat.v1.executing_eagerly_outside_functions():
158 if not all(
--> 159 [
160 functional_utils.is_input_keras_tensor(t)
161 for t in tf.nest.flatten(inputs)
162 ]
163 ):
164 inputs, outputs = functional_utils.clone_graph_nodes(
165 inputs, outputs
166 )
167 self._init_graph_network(inputs, outputs)
File C:\ProgramData\Anaconda3\lib\site-packages\keras\src\engine\functional.py:160, in <listcomp>(.0)
153 # Check if the inputs contain any intermediate `KerasTensor` (not
154 # created by tf.keras.Input()). In this case we need to clone the `Node`
155 # and `KerasTensor` objects to mimic rebuilding a new model from new
156 # inputs. This feature is only enabled in TF2 not in v1 graph mode.
157 if tf.compat.v1.executing_eagerly_outside_functions():
158 if not all(
159 [
--> 160 functional_utils.is_input_keras_tensor(t)
161 for t in tf.nest.flatten(inputs)
162 ]
163 ):
164 inputs, outputs = functional_utils.clone_graph_nodes(
165 inputs, outputs
166 )
167 self._init_graph_network(inputs, outputs)
File C:\ProgramData\Anaconda3\lib\site-packages\keras\src\engine\functional_utils.py:48, in is_input_keras_tensor(tensor)
32 """Check if tensor is directly generated from `tf.keras.Input`.
33
34 This check is useful when constructing the functional model, since we will
(...)
45 ValueError: if the tensor is not a KerasTensor instance.
46 """
47 if not node_module.is_keras_tensor(tensor):
---> 48 raise ValueError(_KERAS_TENSOR_TYPE_CHECK_ERROR_MSG.format(tensor))
49 return tensor.node.is_input
ValueError: Found unexpected instance while processing input tensors for keras functional model. Expecting KerasTensor which is from tf.keras.Input() or output from keras layer call(). Got: 0
改成這樣就好了
import tensorflow as tf
def get_activations(model, layer, input_img_data):
activations_f = tf.keras.backend.function(model.input, layer.output)
activations = activations_f(input_img_data)
return activations
评论
发表评论