#python #python-3.x #keras #vision
#python #python-3.x #keras #миссия
Вопрос:
Контекст:
1. python==3.6.6
2. Keras==2.2.4
3. tensorflow==2.1.0
4. pillow==7.0.0
Когда я загружаю модель, обученную на обучаемой машине Google, она показывает мне следующий код ошибки.
Код программы загрузки модели:
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
Ответ №1:
Самая важная строка находится внизу, где упоминается ragged
.
Это происходит со мной, когда я использую новую модель Keras в старой версии Keras. Как вы сгенерировали модель? Держу пари, что вы использовали более новую версию TensorFlow для ее создания.
Лучшее и самое простое, что можно сделать, это перестроить модель Keras .h5, используя ту же версию TF, которую вы используете. Однако вы также можете экспортировать модель как .json, изменить входной слой и затем перезагрузить в более старой версии. Однако имейте в виду, что произошла пара других изменений, с которыми вы столкнетесь после этого.