gpt4 book ai didi

python - tensorflow 值错误: expected flatten_input to have shape: the correct way to load and process image?

转载 作者:行者123 更新时间:2023-11-30 09:27:52 32 4
gpt4 key购买 nike

我尝试使用教程 TensorFlow 2 quickstart for beginners 。嗯,我知道它有效。

然后我在 Paint 中制作了图像(7.jpg,200x200 像素)。

enter image description here

现在我希望模型尝试猜测数字是多少。我尝试处理图像:

import tensorflow as tf
import numpy as np


mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

# the tutorial example contains epochs=5, but 1 is to run faster and get smaller output
model.fit(x_train, y_train, epochs=1)

img_path = "7.jpg"
img_raw = tf.io.read_file(img_path)
img_tensor = tf.image.decode_image(img_raw)
img_final = tf.image.resize(img_tensor, [28, 28])
img_final = img_final / 255.0

print("img_final.shape =", img_final.shape)
predict = model.predict(img_final)

并获取输出:

Train on 60000 samples                                                                                                                                
60000/60000 [==============================] - 3s 52us/sample - loss: 0.3013 - accuracy: 0.9120
img_final.shape = (28, 28, 3)
Traceback (most recent call last):
File "main.py", line 33, in <module>
predict = model.predict(img_final)
File "D:\user\python\tensor\venv\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 909, in predict
use_multiprocessing=use_multiprocessing)
File "D:\user\python\tensor\venv\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 462, in predict
steps=steps, callbacks=callbacks, **kwargs)
File "D:\user\python\tensor\venv\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 396, in _model_iteration
distribution_strategy=strategy)
File "D:\user\python\tensor\venv\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 594, in _process_inputs
steps=steps)
File "D:\user\python\tensor\venv\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2472, in _standardize_user_data
exception_prefix='input')
File "D:\user\python\tensor\venv\lib\site-packages\tensorflow_core\python\keras\engine\training_utils.py", line 574, in standardize_input_data
str(data_shape))
ValueError: Error when checking input: expected flatten_input to have shape (28, 28) but got array with shape (28, 3)

我添加了 print("img_final.shape =", img_final.shape) 来查看输入图像的形状。我看到 img_final.shape = (28, 28, 3)

我知道数字3是 channel 数。来自文档 tf.io.decode_image :

Note: decode_gif returns a 4-D array [num_frames, height, width, 3], as opposed to decode_bmp, decode_jpeg and decode_png, which return 3-D arrays [height, width, num_channels].

因此,我将图像作为 3-D 数组,但模型等待形状 = (28, 28) 的数组作为输入。

如何将其转换为 (28, 28)?我是否将其设为单色或进行其他处理?

更新:根据RonaldTaras我已经添加了灰度转换和一些打印。现在我有:

print("img_final.shape =", img_final.shape)

img_final = tf.image.rgb_to_grayscale(img_final)
print("grayscale img_final.shape =", img_final.shape)

img_final = tf.expand_dims(img_final[:, :, :1], axis=0)
print("expand_dims img_final.shape =", img_final.shape)

predict = model.predict(img_final)

并输出:

img_final.shape = (28, 28, 3)                                                                                                                         
grayscale img_final.shape = (28, 28, 1)
expand_dims img_final.shape = (1, 28, 28, 1)
Traceback (most recent call last):
File "main.py", line 42, in <module>
...
ValueError: Error when checking input: expected flatten_input to have 3 dimensions, but got array with shape (1, 28, 28, 1)

最佳答案

mnist数据库只有灰度图片,因此它们只有1个颜色 channel 。所以是的,你必须通过将 3 个 channel (我想是 RGB)转换为灰度来使其成为单色。您可以使用

tf.image.rgb_to_grayscale(images)

您可以查看文档以获取更多信息:tf.image.rgb_to_grayscale doc

关于python - tensorflow 值错误: expected flatten_input to have shape: the correct way to load and process image?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58557296/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com