简体   繁体   中英

"ValueError: Unknown layer: Functional"

I have trained my model on Google Teachable Machine and have downloaded the trained model and I am using that model to classify the images. I am experiencing this error and don't know what is it trying to say or how to solve it. My environment package details are: Tensorflow: 2.1.0 Keras: 2.3.1 Pillow: 7.0.0 h5py: 2.10.0 Below is the code I am trying to run.


import tensorflow.keras
from PIL import Image, ImageOps
import numpy as np

# Disable scientific notation for clarity
np.set_printoptions(suppress=True)

# Load the model
model = tensorflow.keras.models.load_model('keras_model.h5')

# Create the array of the right shape to feed into the keras model
# The 'length' or number of images you can put into the array is
# determined by the first position in the shape tuple, in this case 1.
data = np.ndarray(shape=(1, 32, 32, 3), dtype=np.float32)

# Replace this with the path to your image
image = Image.open(r'C:\Users\DELL\Desktop\Dataset\TEST\2_Final\Alaa\image_14022021_065404.jpg')

#resize the image to a 224x224 with the same strategy as in TM2:
#resizing the image to be at least 224x224 and then cropping from the center
size = (32, 32)
image = ImageOps.fit(image, size, Image.ANTIALIAS)

#turn the image into a numpy array
image_array = np.asarray(image)

# display the resized image
image.show()

# Normalize the image
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1

# Load the image into the array
data[0] = normalized_image_array

# run the inference
prediction = model.predict(data)
print(prediction)

This is the full traceback

ValueError                                Traceback (most recent call last)
<ipython-input-3-ac2b19895981> in <module>
      9 
     10 # Load the model
---> 11 model = tensorflow.keras.models.load_model('keras_model.h5')
     12 
     13 # Create the array of the right shape to feed into the keras model

~\Anaconda3\envs\teach11\lib\site-packages\tensorflow_core\python\keras\saving\save.py in load_model(filepath, custom_objects, compile)
    144   if (h5py is not None and (
    145       isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))):
--> 146     return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
    147 
    148   if isinstance(filepath, six.string_types):

~\Anaconda3\envs\teach11\lib\site-packages\tensorflow_core\python\keras\saving\hdf5_format.py in load_model_from_hdf5(filepath, custom_objects, compile)
    166     model_config = json.loads(model_config.decode('utf-8'))
    167     model = model_config_lib.model_from_config(model_config,
--> 168                                                custom_objects=custom_objects)
    169 
    170     # set weights

~\Anaconda3\envs\teach11\lib\site-packages\tensorflow_core\python\keras\saving\model_config.py in model_from_config(config, custom_objects)
     53                     '`Sequential.from_config(config)`?')
     54   from tensorflow.python.keras.layers import deserialize  # pylint: disable=g-import-not-at-top
---> 55   return deserialize(config, custom_objects=custom_objects)
     56 
     57 

~\Anaconda3\envs\teach11\lib\site-packages\tensorflow_core\python\keras\layers\serialization.py in deserialize(config, custom_objects)
    104       module_objects=globs,
    105       custom_objects=custom_objects,
--> 106       printable_module_name='layer')

~\Anaconda3\envs\teach11\lib\site-packages\tensorflow_core\python\keras\utils\generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    301             custom_objects=dict(
    302                 list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 303                 list(custom_objects.items())))
    304       with CustomObjectScope(custom_objects):
    305         return cls.from_config(cls_config)

~\Anaconda3\envs\teach11\lib\site-packages\tensorflow_core\python\keras\engine\sequential.py in from_config(cls, config, custom_objects)
    375     for layer_config in layer_configs:
    376       layer = layer_module.deserialize(layer_config,
--> 377                                        custom_objects=custom_objects)
    378       model.add(layer)
    379     if not model.inputs and build_input_shape:

~\Anaconda3\envs\teach11\lib\site-packages\tensorflow_core\python\keras\layers\serialization.py in deserialize(config, custom_objects)
    104       module_objects=globs,
    105       custom_objects=custom_objects,
--> 106       printable_module_name='layer')

~\Anaconda3\envs\teach11\lib\site-packages\tensorflow_core\python\keras\utils\generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    301             custom_objects=dict(
    302                 list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 303                 list(custom_objects.items())))
    304       with CustomObjectScope(custom_objects):
    305         return cls.from_config(cls_config)

~\Anaconda3\envs\teach11\lib\site-packages\tensorflow_core\python\keras\engine\sequential.py in from_config(cls, config, custom_objects)
    375     for layer_config in layer_configs:
    376       layer = layer_module.deserialize(layer_config,
--> 377                                        custom_objects=custom_objects)
    378       model.add(layer)
    379     if not model.inputs and build_input_shape:

~\Anaconda3\envs\teach11\lib\site-packages\tensorflow_core\python\keras\layers\serialization.py in deserialize(config, custom_objects)
    104       module_objects=globs,
    105       custom_objects=custom_objects,
--> 106       printable_module_name='layer')

~\Anaconda3\envs\teach11\lib\site-packages\tensorflow_core\python\keras\utils\generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    290     config = identifier
    291     (cls, cls_config) = class_and_config_for_serialized_keras_object(
--> 292         config, module_objects, custom_objects, printable_module_name)
    293 
    294     if hasattr(cls, 'from_config'):

~\Anaconda3\envs\teach11\lib\site-packages\tensorflow_core\python\keras\utils\generic_utils.py in class_and_config_for_serialized_keras_object(config, module_objects, custom_objects, printable_module_name)
    248     cls = module_objects.get(class_name)
    249     if cls is None:
--> 250       raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
    251 
    252   cls_config = config['config']

ValueError: Unknown layer: Functional

The model file you are loading seems not to contain the model, but only the weights. Can you make sure the h5 file also contains the full model definition?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM