Python Tensorflow值错误:预期展平输入具有形状:加载和处理图像的正确方法?
我尝试使用教程。好吧,我明白了 然后我用paint7.jpg,200x200px制作了这个图像 现在我想让模型试着猜出数字是多少。我尝试处理图像:Python Tensorflow值错误:预期展平输入具有形状:加载和处理图像的正确方法?,python,tensorflow,machine-learning,Python,Tensorflow,Machine Learning,我尝试使用教程。好吧,我明白了 然后我用paint7.jpg,200x200px制作了这个图像 现在我想让模型试着猜出数字是多少。我尝试处理图像: 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
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)
和输出:
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数据库只有灰度图片,因此它们只有一个颜色通道。所以是的,你必须把3个通道转换成单色,我想是RGB到灰度。你可以用
tf.image.rgb_to_grayscale(images)
您可以查看文档以了解更多信息:mnist数据库只有灰度图片,因此它们只有一个颜色通道。所以是的,你必须把3个通道转换成单色,我想是RGB到灰度。你可以用
tf.image.rgb_to_grayscale(images)
您可以查看文档以了解更多信息:您的模型设计为在一次运行中处理多个图像,因此它希望接收多个与训练时形状相同的图像。所以你应该用28,28,1来传递张量。你想预测一个图像,所以它应该是1,28,28,1。但是你的图像是200,200,3。由于您有一个灰度图像,您可以只拍摄第一个通道:
img_final = tf.expand_dims(img_final[:, :, :1], axis=0)
跑步预测
您可以使用tf.image.rgb_到_灰度,而不是使用第一个通道,如下所示。您的模型设计为在一次运行中处理多个图像,因此它希望接收与训练时形状相同的多个图像。所以你应该用28,28,1来传递张量。你想预测一个图像,所以它应该是1,28,28,1。但是你的图像是200,200,3。由于您有一个灰度图像,您可以只拍摄第一个通道:
img_final = tf.expand_dims(img_final[:, :, :1], axis=0)
跑步预测
您可以使用tf.image.rgb_来设置灰度,而不是使用第一个通道,如下所示。好的,我有灰度图像28,28,1。如何制作模型以理解图像?请参阅Updatery以从中删除tf.expand_dims函数。我认为模型试图得到一个28,28,1的形状,而tf.expand_dims将轴扩展为=0,因此它变成了1,28,28,1。如果模型仍然需要另一个图像形状1,28,28,那么你可以使用tf.reformateSensor[1,28,28]来获得它。如果我对tf.expand\u dims进行注释,我会得到值错误:检查输入时出错:期望展平\u输入具有形状28,28,但得到了具有形状28的数组,1编辑上面的注释:我认为你的答案现在已经存在了,我有灰度图像28,28,1。如何制作模型以理解图像?请参阅Updatery以从中删除tf.expand_dims函数。我认为模型试图得到一个28,28,1的形状,而tf.expand_dims将轴扩展为=0,因此它变成了1,28,28,1。如果模型仍然需要另一个图像形状1,28,28,那么你可以使用tf.reformateSensor[1,28,28]来获得它。如果我对tf.expand\u dims进行注释,我会得到值错误:检查输入时出错:期望展平\u输入具有形状28,28,但获得了具有形状28的数组,1编辑了上面的注释:我想你的答案现在在那里我有形状1,28的图像,28,rgb_后1到_灰度并展开_dims。但是模型不理解它,请参见更新。您可以使用tf.squezeimg_final,axis=3删除最后一个维度并获得1、28、28或更改tf.keras.layers.flattinput_shape=28、28到28、28、1的输入形状。顺便说一句,您不再需要img_final[:,:,:1],因为您已经使用了tf.image.rgb_来显示灰度。它已经是28、28、1了,我在rgb_到_灰度和展开_dims后有形状为1、28、28、1的图像。但是模型不理解它,请参见更新。您可以使用tf.squezeimg_final,axis=3删除最后一个维度并获得1、28、28或更改tf.keras.layers.flattinput_shape=28、28到28、28、1的输入形状。顺便说一句,您不再需要img_final[:,:,:1],因为您已经使用了tf.image.rgb_来显示灰度。已经是28,28,1了