Не работает метод predict() Keras models
Обучил свёрточную нейросеть и записал результаты в файл .hdf5 При попытке распознать изображение, на методе predict() выдает ошибку Traceback (most recent call last).
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import matplotlib.pyplot as plt
import numpy as np
classes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
model = load_model('model-dense-17-0.00010-1.0000.hdf5')
# ['model_weights', 'optimizer_weights']
# print(model.summary())
img_path = '0.06170.jpg'
img = image.load_img(img_path, target_size=(56, 56), color_mode="grayscale")
# Преобразуем картинку в массив
x = image.img_to_array(img)
# Меняем форму массива в плоский вектор
x = x.reshape(1, 3136)
# Инвертируем изображение
x = 255 - x
# Нормализуем изображение
x /= 255
prediction = model.predict(x)
print(prediction)
# prediction = np.argmax(prediction)
# print("Номер класса:", prediction)
# print("Название класса:", classes[prediction])
model-dense-17-0.00010-1.0000.hdf5
Полный код ошибки
Traceback (most recent call last):
File "D:/3_load_hdf5.py", line 27, in <module>
prediction = model.predict(x)
File "D:\python\keras\engine\training.py", line 88, in _method_wrapper
return method(self, *args, **kwargs)
File "D:\python\keras\engine\training.py", line 1268, in predict
tmp_batch_outputs = predict_function(iterator)
File "D:\python\eager\def_function.py", line 580, in __call__
result = self._call(*args, **kwds)
File "D:\python\eager\def_function.py", line 627, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "D:\python\eager\def_function.py", line 506, in _initialize
*args, **kwds))
File "D:\python\eager\function.py", line 2446, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "D:\python\eager\function.py", line 2777, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "D:\python\eager\function.py", line 2667, in _create_graph_function
capture_by_value=self._capture_by_value),
File "D:\python\framework\func_graph.py", line 981, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "D:\python\eager\def_function.py", line 441, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "D:\python\framework\func_graph.py", line 968, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
D:\python\keras\engine\training.py:1147 predict_function *
outputs = self.distribute_strategy.run(
D:\python\distribute\distribute_lib.py:951 run **
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
D:\python\distribute\distribute_lib.py:2290 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
D:\python\distribute\distribute_lib.py:2649 _call_for_each_replica
return fn(*args, **kwargs)
D:\python\keras\engine\training.py:1122 predict_step **
return self(x, training=False)
D:\python\keras\engine\base_layer.py:886 __call__
self.name)
D:\python\keras\engine\input_spec.py:180 assert_input_compatibility
str(x.shape.as_list()))
ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 3136]