我正在尝试建立和训练一个预测美国手语的模型(使用手语MNIST数据集)。到目前为止,我已经成功构建了模型,并使用构建模型来预测列车数据集。火车图像中的准确性也高于70%。现在,我想使用经过训练的模型预测使用单个图像。问题是预测结果(类名)是错误的。我关注了this内核。我想预测任何给定图像的标志。
这是代码
train = pd.read_csv('../asl_data_train/sign-language-mnist/sign-mnist-train.csv')
test = pd.read_csv('../asl_data_train/sign-language-mnist/sign-mnist-test.csv')
train.head()
train.shape
labels = train['label'].values
unique_val = np.array(labels)
np.unique(unique_val)
plt.figure(figsize = (18,8))
sns.countplot(x =labels)
train.drop('label', axis = 1, inplace = True)
images = train.values
images = np.array([np.reshape(i, (28, 28)) for i in images])
images = np.array([i.flatten() for i in images])
label_binrizer = LabelBinarizer()
labels = label_binrizer.fit_transform(labels)
plt.imshow(images[0].reshape(28,28))
x_train, x_test, y_train, y_test = train_test_split(images, labels, test_size = 0.3, random_state = 101)
batch_size = 128
num_classes = 24
epochs = 50
x_train = x_train / 255
x_test = x_test / 255
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
plt.imshow(x_train[0].reshape(28,28))
构建模型代码
model = Sequential()
model.add(Conv2D(64, kernel_size=(3,3), activation = 'relu', input_shape=(28,28,1) ))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Conv2D(64, kernel_size = (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Conv2D(64, kernel_size = (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Flatten())
model.add(Dense(128, activation = 'relu'))
model.add(Dropout(0.20))
model.add(Dense(num_classes, activation = 'softmax'))
model.compile(loss = keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adam(),
metrics=['accuracy'])
history = model.fit(x_train, y_train, validation_data = (x_test, y_test), epochs=epochs, batch_size=batch_size)
model.save("testmodel.h5")
对测试图像的预测
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title("Accuracy")
plt.xlabel('epoch')
plt.ylabel('accuracy')
plt.legend(['train','test'])
plt.show()
test_labels = test['label']
test.drop('label', axis = 1, inplace = True)
test_images = test.values
test_images = np.array([np.reshape(i, (28, 28)) for i in test_images])
test_images = np.array([i.flatten() for i in test_images])
test_labels = label_binrizer.fit_transform(test_labels)
test_images = test_images.reshape(test_images.shape[0], 28, 28, 1)
test_images.shape
y_pred = model.predict(test_images)
accuracy_score(test_labels, y_pred.round())
在这里我得到的准确度得分约为0.8 ...
这就是我尝试使用单个图像来预测符号的方式
model = load_model("testmodel.h5")
test_image = image.load_img('a.jpg',color_mode="grayscale",target_size=(28,28,1))
print(test_image.format)
print(test_image.mode)
print(test_image.size)
test_image = image.img_to_array(test_image)
test_image = test_image / 255
test_image = test_image.reshape((-1,) + test_image.shape)
print(test_image.dtype)
print(test_image.shape)
y_pred = model.predict_classes(test_image)
print(y_pred)
classname = y_pred[0]
print("Class: ",classname)
在这里我得到了类名,但是例如对于字母“ A”(a.jpg)却发生了变化,我得到了第6类。我在这里做错了什么..请指出正确的方向。
答案 0 :(得分:1)
图像“ a.jpg”是否属于同一数据集?
如果答案为否->您应该记住,NN只能预测具有与训练图像相似特征的图像。如果使用大量类型的图像训练NN,则可以预测广泛的图像,但是如果使用非常静态的数据集(白色背景,相同大小,手心等)训练NN,则如果失败输入图像非常不同。
如果答案为是->如果您获得80%的准确性,则图像可能会分类错误。如果使用一组测试数据来验证您的NN,则必须通过将它们作为一个组或逐个传递来获得相同的结果。