我有一个在MNIST数据集上训练的模型,但是当我输入一个手工制作的图像样本时,它会引发ValueError:层序的输入0与层不兼容:期望输入形状的轴-1的值为784,但接收到的输入形状为(None, 1)。
我已经检查了模型的输入,它与MNIST的形状相同。x_train[0].shape (784,) 以及我的图像 arr.shape (784,) 请帮助我!
…
from tensorflow.keras.datasets import fashion_mnist from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Dropout from tensorflow.keras import utils from tensorflow.keras.preprocessing import image import numpy as np import tensorflow as tf import matplotlib.pyplot as plt %matplotlib inline print(x_train[3].shape) x_train = x_train.reshape(60000, 784) x_train = x_train / 255 model = Sequential() model.add(Dense(800, input_dim=784, activation="relu")) model.add(Dense(10, activation="softmax")) model.compile(loss="categorical_crossentropy", optimizer="SGD", metrics=["accuracy"]) history = model.fit(x_train, y_train, batch_size=200, epochs=100, verbose=1) predictions = model.predict(x_train) n = 0 plt.imshow(x_train[n].reshape(28, 28), cmap=plt.cm.binary) plt.show() x_train[0].shape #Out[28]: (784,) import matplotlib.image as mpimg import numpy as np from PIL import Image img = Image.open('yboot.jpg').convert('L') arr = np.asarray(img, dtype=np.float64) arr = arr.reshape(784) arr.shape arr = arr/255 print(arr.shape) # (784,) RealPred = model.predict(arr)
ValueError: 层序的输入0与层不兼容:期望输入形状的轴-1的值为784,但接收到的输入形状为(None, 1)
回答:
你需要在这里增加一个额外的维度,arr.reshape(1, 784)
。这是完整的工作代码
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()# train set / data x_train = x_train.reshape(-1, 28*28)x_train = x_train.astype('float32') / 255# train set / target y_train = tf.keras.utils.to_categorical(y_train , num_classes=10)
模型
model = Sequential()model.add(Dense(800, input_dim=784, activation="relu"))model.add(Dense(10, activation="softmax"))model.compile(loss="categorical_crossentropy", optimizer="SGD", metrics=["accuracy"])history = model.fit(x_train, y_train, batch_size=200, epochs=20, verbose=1)
评估
predictions = model.predict(x_train)n = 0plt.imshow(x_train[n].reshape(28, 28), cmap=plt.cm.binary)plt.title(np.argmax(predictions[n], axis=0))plt.show()
推理
import numpy as npimport cv2def input_prepare(img): img = np.asarray(img) # 转换为数组 img = cv2.resize(img, (28, 28 )) # 调整到目标形状 img = cv2.bitwise_not(img) # [可选] 我的输入背景是白色,我将其转为黑色 - {bitwise_not} 将1转换为0,将0转换为1 img = img / 255 # 归一化 img = img.reshape(1, 784) # 重塑 return img img = cv2.imread('/content/5.png')orig = img.copy() # 保存以便稍后绘图 img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 灰度化 img = input_prepare(img)print(img.shape)pred = model.predict(img)plt.imshow(cv2.cvtColor(orig, cv2.COLOR_BGR2RGB))plt.title(np.argmax(pred, axis=1))plt.show()