Python图像重塑(1,28,28,1)在图像处理中不起作用

这个问题已经困扰我很长时间了,我一直找不到解决方案。

问题: 我需要对训练的人工智能进行“重塑”练习,但我无法做到。
通常,当你从外部获取相同的图片时,它是有效的。

def load_image(filename):    img = load_img(filename, grayscale=True, target_size=(28, 28))    img = img_to_array(img)    img = img.reshape(1, 28, 28, 1)    img = img.astype('float32')    img = img / 255.0    return img def run_example():    img = load_image('images/5.png')    model = load_model('final_model.h5')    digit = model.predict_classes(img)    print(digit[0]) run_example()

这段代码是有效的,但当我想从一张有很多数字的图片中获取数字时,我必须从图片内部获取这些数字。enter image description here我能够在这里获取一个数字,其中一个是这样的:

digits_with_zeros[0].shape# output: (171, 171)

当我尝试对这个序列应用“重塑”时,我得到了这样的错误。

img = digits_with_zeros[0].reshape(28,28)img = img_to_array(img)img = img.reshape(1, 28, 28, 1)img = img.astype('float32')img = img / 255.0model = load_model('final_model.h5')digit = model.predict_classes(img)

输出:

----> 1 img = digits_with_zeros[0].reshape(-1,28,28,1)      2 img = img_to_array(img)      3 img = img.reshape(1, 28, 28, 1)      4 img = img.astype('float32')      5 img = img / 255.0ValueError: cannot reshape array of size 29241 into shape (28,28,1)

回答:

img = digits_with_zeros[0]img = cv2.resize(img, dsize=(28, 28))img = np.array(img).reshape(-1, 28,28,1)model = load_model('final_model.h5')digit = model.predict_classes(img)

Related Posts

如何对SVC进行超参数调优?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

如何在初始训练后向模型添加训练数据?

我想在我的scikit-learn模型已经训练完成后再…

使用Google Cloud Function并行运行带有不同用户参数的相同训练作业

我正在寻找一种方法来并行运行带有不同用户参数的相同训练…

加载Keras模型,TypeError: ‘module’ object is not callable

我已经在StackOverflow上搜索并阅读了文档,…

在计算KNN填补方法中特定列中NaN值的”距离平均值”时

当我从头开始实现KNN填补方法来处理缺失数据时,我遇到…

使用巨大的S3 CSV文件或直接从预处理的关系型或NoSQL数据库获取数据的机器学习训练/测试工作

已关闭。此问题需要更多细节或更清晰的说明。目前不接受回…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注