带有自己手写图像的Tensorflow示例

2024-07-03 07:33:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我在这里用zalando mnist尝试了tensorflow示例: https://www.tensorflow.org/tutorials/keras/basic_classification

之后我用手写的mnist数据库修改了衣服的图片,这也很有效

现在我想用mnist手写数据库来训练人工智能,从我手写的“1”中拍照,让KI猜数字

我在KI的训练后附加了几行代码。 我尝试的是:

ownPicArr = imageio.imread(filename) #it is a 28x28 PNG file
ownPicArr = ownPicArr / 255.0
pred = model.predict(ownPicArr)

我有以下错误:

ValueError: Error when checking input: expected flatten_input to have 3 dimensions, but got array with shape (28, 28)

如何解决这个问题?那你


Tags: httpsorg数据库示例inputbasictensorflowwww
1条回答
网友
1楼 · 发布于 2024-07-03 07:33:22

即使图片的颜色是反转的,这也是使用OpenCV执行预测的方法

import os, cv2
image=cv2.imread(imagePath)
image_from_array = Image.fromarray(image, 'RGB')
size_image = image_from_array.resize((28,28))
p = np.expand_dims(size_image, 0)
img = tf.cast(p, tf.float32)
pred = model.predict(img)

首先,我们使用OpenCV读取图像,OpenCV将图像存储为一个数组。然后我们转换数组,并指定颜色通道。调整图像大小后,我们创建了一批单个图像,然后将数据类型更改为float32或与模型匹配的数据类型后,我们最终进行预测

相关问题 更多 >