As a summary, I am trying to load in a Keras model file and I need to access the model outputs. This is raising an exception and I’ve managed to narrow the code down to the following:
import os
import keras.models
import numpy as np
top_dir = '/home/jcgm/PycharmProjects/CIFAKE/data'
model_file = top_dir + os.sep + "keras_model.keras"
model = keras.models.load_model(model_file)
model_outputs = model.output
This raises the following exception:
Traceback (most recent call last):
File "/home/jcgm/PycharmProjects/CIFAKE/test.py", line 11, in <module>
model_outputs = model.output
File "/home/jcgm/PycharmProjects/CIFAKE/venv/lib/python3.10/site-packages/keras/src/ops/operation.py", line 240, in output
return self._get_node_attribute_at_index(0, "output_tensors", "output")
File "/home/jcgm/PycharmProjects/CIFAKE/venv/lib/python3.10/site-packages/keras/src/ops/operation.py", line 259, in _get_node_attribute_at_index
raise ValueError(
ValueError: The layer sequential has never been called and thus has no defined output.
Background to this, is that I’m implementing the grad_cam for a CNN I have trained. I have followed the code Grad Cam and this works perfectly. This uses the xception CNN.
If I modify the code to write this CNN to disk and then read it back in the code fails when I try and create the back propagated gradient model:
grad_model = keras.models.Model(
model.inputs, [model.get_layer(last_conv_layer_name).output, model.output]
)
I have narrowed this down to the model.output
line.
James Matthews is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.