model.predict(img) - error
Что упускаю ? не получается передать 1 подготовленное изображение в model.predict(img)
Модель на вход ожидает:
(batchSize,imageWeidth,imageHeidth,imgChannels)
(1,150,80,1)
Ошибка:
ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 150, 80, 1), found shape=(None, 80, 1)
Код:
# Создать тензор из изображения
# 1. Считать изображение
img = tf.io.read_file('/content/drive/MyDrive/testImg.jpg')
# 2. Декодировать и преобразовать в оттенки серого
img = tf.io.decode_jpeg(img, channels=1)
# 3. Преобразовать в float32 в диапазоне [0, 1]
img = tf.image.convert_image_dtype(img, tf.float32)
# 4. Измененить до нужного размера
img = tf.image.resize(img, [80, 150])
print(img.shape)
# 5. Переместить
img = tf.transpose(img, perm=[1, 0, 2])
print(img.shape)
pred = prediction_model.predict(img)
pred_texts = decode_batch_predictions(pred)
Вывод в консоли:
(80, 150, 1)
(150, 80, 1)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-26-d7d5c6459088> in <module>
17 print(img.shape)
18
---> 19 pred = prediction_model.predict(img)
20 #pred_texts = decode_batch_predictions(pred)
1 frames
/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
/usr/local/lib/python3.8/dist-packages/keras/engine/training.py in tf__predict_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
ValueError: in user code:
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1845, in predict_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1834, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1823, in run_step **
outputs = model.predict_step(data)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1791, in predict_step
return self(x, training=False)
File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.8/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
ValueError: Input 0 of layer "model_1" is incompatible with the layer: expected shape=(None, 150, 80, 1), found shape=(None, 80, 1)
prediction_model.summary()
Model: "model_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
image (InputLayer) [(None, 150, 80, 1)] 0
Conv1 (Conv2D) (None, 150, 80, 32) 320
pool1 (MaxPooling2D) (None, 75, 40, 32) 0
Conv2 (Conv2D) (None, 75, 40, 64) 18496
pool2 (MaxPooling2D) (None, 37, 20, 64) 0
reshape (Reshape) (None, 37, 1280) 0
dense1 (Dense) (None, 37, 64) 81984
dropout (Dropout) (None, 37, 64) 0
bidirectional (Bidirectiona (None, 37, 256) 197632
l)
bidirectional_1 (Bidirectio (None, 37, 128) 164352
nal)
dense2 (Dense) (None, 37, 12) 1548
=================================================================
Total params: 464,332
Trainable params: 464,332
Non-trainable params: 0
_________________________________________________________________
Источник: Stack Overflow на русском