Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用;ValueError:未知层:功能性“;_Python_Tensorflow_Machine Learning_Keras_Deep Learning - Fatal编程技术网

Python &引用;ValueError:未知层:功能性“;

Python &引用;ValueError:未知层:功能性“;,python,tensorflow,machine-learning,keras,deep-learning,Python,Tensorflow,Machine Learning,Keras,Deep Learning,我已经在谷歌可教机器上训练了我的模型,并下载了训练过的模型,我正在使用该模型对图像进行分类。我正在经历这个错误,不知道它想说什么或如何解决它。我的环境包详细信息如下: Tensorflow:2.1.0 Keras:2.3.1 枕头:7.0.0 h5py:2.10.0 下面是我试图运行的代码。 import tensorflow.keras from PIL import Image, ImageOps import numpy as np # Disable scientific notat

我已经在谷歌可教机器上训练了我的模型,并下载了训练过的模型,我正在使用该模型对图像进行分类。我正在经历这个错误,不知道它想说什么或如何解决它。我的环境包详细信息如下: Tensorflow:2.1.0 Keras:2.3.1 枕头:7.0.0 h5py:2.10.0 下面是我试图运行的代码。


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)
这是完整的回溯

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
ValueError回溯(最近一次调用)
在里面
9
10#加载模型
--->11模型=tensorflow.keras.models.load_模型('keras_模型.h5'))
12
13#创建正确形状的数组以输入keras模型
加载\模型中的~\Anaconda3\envs\teach11\lib\site packages\tensorflow\u core\python\keras\saving\save.py(文件路径、自定义\对象、编译)
144如果(h5py)不是无和(
145 iInstance(文件路径,h5py.File)或h5py.is_hdf5(文件路径)):
-->146返回hdf5格式。从hdf5加载模型(文件路径、自定义对象、编译)
147
148如果isinstance(文件路径,六种字符串类型):
~\Anaconda3\envs\teach11\lib\site packages\tensorflow\u core\python\keras\saving\hdf5\u format.py从\u hdf5加载\u模型(文件路径、自定义\u对象、编译)
166 model_config=json.load(model_config.decode('utf-8'))
167 model=model_config_lib.model_from_config(model_config,
-->168个自定义\u对象=自定义\u对象)
169
170#设置重量
~\Anaconda3\envs\teach11\lib\site packages\tensorflow\u core\python\keras\saving\model\u config.py在模型中从配置(配置,自定义对象)
53'`Sequential.from_config(config)`?')
54从tensorflow.python.keras.layers导入反序列化#pylint:disable=g-import-not-at-top
--->55返回反序列化(配置,自定义对象=自定义对象)
56
57
反序列化中的~\Anaconda3\envs\teach11\lib\site packages\tensorflow\u core\python\keras\layers\serialization.py(配置,自定义对象)
104模块_对象=全局,
105自定义对象=自定义对象,
-->106可打印的(模块名称=图层)
反序列化keras对象中的~\Anaconda3\envs\teach11\lib\site packages\tensorflow\u core\python\keras\utils\generic\u utils.py(标识符、模块对象、自定义对象、可打印模块名称)
301自定义对象=dict(
302列表(\u全局\u自定义\u对象.items())+
-->303列表(自定义对象.items())
304带有CustomObjectScope(自定义对象):
305从配置(配置)返回cls
~\Anaconda3\envs\teach11\lib\site packages\tensorflow\u core\python\keras\engine\sequential.py from\u config(cls、config、自定义\u对象)
375对于层配置中的层配置:
376层=层模块。反序列化(层配置,
-->377自定义对象=自定义对象)
378模型。添加(图层)
379如果不是模型输入和构建输入形状:
反序列化中的~\Anaconda3\envs\teach11\lib\site packages\tensorflow\u core\python\keras\layers\serialization.py(配置,自定义对象)
104模块_对象=全局,
105自定义对象=自定义对象,
-->106可打印的(模块名称=图层)
反序列化keras对象中的~\Anaconda3\envs\teach11\lib\site packages\tensorflow\u core\python\keras\utils\generic\u utils.py(标识符、模块对象、自定义对象、可打印模块名称)
301自定义对象=dict(
302列表(\u全局\u自定义\u对象.items())+
-->303列表(自定义对象.items())
304带有CustomObjectScope(自定义对象):
305从配置(配置)返回cls
~\Anaconda3\envs\teach11\lib\site packages\tensorflow\u core\python\keras\engine\sequential.py from\u config(cls、config、自定义\u对象)
375对于层配置中的层配置:
376层=层模块。反序列化(层配置,
-->377自定义对象=自定义对象)
378模型。添加(图层)
379如果不是模型输入和构建输入形状:
反序列化中的~\Anaconda3\envs\teach11\lib\site packages\tensorflow\u core\python\keras\layers\serialization.py(配置,自定义对象)
104模块_对象=全局,
105自定义对象=自定义对象,
-->106可打印的(模块名称=图层)
反序列化keras对象中的~\Anaconda3\envs\teach11\lib\site packages\tensorflow\u core\python\keras\utils\generic\u utils.py(标识符、模块对象、自定义对象、可打印模块名称)
290配置=标识符
291(cls,cls_config)=类_和_配置_,用于序列化的_keras_对象(
-->292配置、模块对象、自定义对象、可打印模块名称)
293
294如果hasattr(cls,“来自配置”):
~\Anaconda3\envs\teach11\lib\site packages\tensorflow\u core\python\keras\utils\generic\u utils.py在类和配置中,用于序列化的\u keras\u对象(配置、模块\u对象、自定义\u对象、可打印的\u模块\u名称)
248 cls=模块对象.get(类名称)
249如果cls为无:
-->250 raise VALUERROR('未知'+可打印模块名称+':'+类名称)
251
252 cls_config=config['config']
ValueError:未知层:功能性

您正在加载的模型文件似乎不包含模型,而只包含权重。您能否确保h5文件也包含完整的模型定义?

否,如果它只有权重,OP将有不同的错误。您可能需要最新的tensorflow版本(因为您使用tf.keras)来加载此模型。