python编码项目中的谷歌可教机器模型加载问题



上下文:

1. python==3.6.6 
2. Keras==2.2.4
3. tensorflow==2.1.0
4. pillow==7.0.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, 224, 224, 3), dtype=np.float32)
# Replace this with the path to your image
image = Image.open('38.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 = (224, 224)
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)

错误消息:

"/home/muhammad_abdullah/anaconda3/envs/google teachable machine/bin/python" "/home/muhammad_abdullah/PycharmProjects/google teachable machine/main.py"
/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint16 = np.dtype([("qint16", np.int16, 1)])
/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint32 = np.dtype([("qint32", np.int32, 1)])
/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
np_resource = np.dtype([("resource", np.ubyte, 1)])
/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint16 = np.dtype([("qint16", np.int16, 1)])
/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint32 = np.dtype([("qint32", np.int32, 1)])
/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
np_resource = np.dtype([("resource", np.ubyte, 1)])
Traceback (most recent call last):
File "/home/muhammad_abdullah/PycharmProjects/google teachable machine/main.py", line 9, in <module>
model = tensorflow.keras.models.load_model('keras_model.h5')
File "/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/keras/saving/save.py", line 146, in load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
File "/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 212, in load_model_from_hdf5
custom_objects=custom_objects)
File "/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/keras/saving/model_config.py", line 55, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/keras/layers/serialization.py", line 89, in deserialize
printable_module_name='layer')
File "/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 192, in deserialize_keras_object
list(custom_objects.items())))
File "/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py", line 352, in from_config
custom_objects=custom_objects)
File "/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/keras/layers/serialization.py", line 89, in deserialize
printable_module_name='layer')
File "/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 192, in deserialize_keras_object
list(custom_objects.items())))
File "/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py", line 352, in from_config
custom_objects=custom_objects)
File "/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/keras/layers/serialization.py", line 89, in deserialize
printable_module_name='layer')
File "/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 192, in deserialize_keras_object
list(custom_objects.items())))
File "/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1121, in from_config
process_layer(layer_data)
File "/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1105, in process_layer
layer = deserialize_layer(layer_data, custom_objects=custom_objects)
File "/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/keras/layers/serialization.py", line 89, in deserialize
printable_module_name='layer')
File "/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 194, in deserialize_keras_object
return cls.from_config(cls_config)
File "/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 446, in from_config
return cls(**config)
File "/home/muhammad_abdullah/anaconda3/envs/google teachable machine/lib/python3.6/site-packages/tensorflow/python/keras/engine/input_layer.py", line 80, in __init__
raise ValueError('Unrecognized keyword arguments:', kwargs.keys())
ValueError: ('Unrecognized keyword arguments:', dict_keys(['ragged']))
Process finished with exit code 1

最重要的一行是指向底部的ragged

当我在旧版本的Keras中使用新的Keras模型时,就会发生这种情况。你是如何生成模型的?我敢打赌,你使用了一个新版本的TensorFlow来构建它。

最好和最简单的方法是使用您正在使用的相同版本的TF重建.h5 Keras模型。但是,您也可以将模型导出为.json,修改输入层,然后在旧版本中重新加载。然而,请注意,在那之后,你还会遇到其他一些变化。

最新更新