【发布时间】:2019-01-25 00:00:54
【问题描述】:
我正在尝试使用 Tensorflow 上的示例创建 NN,并将我自己的手写数字输入它以预测正确的标签,但数组的形状不允许我这样做。
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
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(512, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)
path = 'C:/Users/pewdu/Desktop/third.jpg'
img = cv2.imread(path)
new_img = cv2.resize(img, (28, 28))
new_img = new_img / 255.0
print(new_img.shape) # it equals to (28,28,3)
prediction = model.predict(new_img)
所以错误是:
ValueError: Error when checking input: expected flatten_7_input to have shape (28, 28) but got array with shape (28, 3)
【问题讨论】:
标签: python tensorflow jupyter-notebook jupyter