python - 使用单图像 tensorflow keras 进行预测

标签 python tensorflow machine-learning keras neural-network

我正在尝试构建和训练一个模型来预测美国手语(使用手语 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。我在这里做错了什么..请为我指出正确的方向。

最佳答案

图像“a.jpg”是同一数据集的一部分吗?

如果答案是否定的 -> 您应该记住,神经网络只能预测与训练图像具有相似特征的图像。如果神经网络使用多种类型的图像进行训练,它可以预测多种图像,但如果神经网络使用非常静态的数据集(白色背景、相同大小、以手为中心等)进行训练,则它会失败,如果输入图像非常不同。

如果答案是肯定的 -> 如果您获得了 80% 的准确率,则图像可能会被错误分类。如果您使用一组测试数据来验证您的神经网络,则必须通过将它们作为一组使用或逐个传递它们来获得相同的结果。

关于python - 使用单图像 tensorflow keras 进行预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59927921/

相关文章:

python - Pandas DataFrame 列与自定义函数的成对相关

python - 导入 tensorflow 抛出导入错误: DLL load failed

image-processing - 全局池有什么作用?

python - 对数缩放(应用函数?)稀疏矩阵

python - Python 3 中的输入变量

python - ImportError : No module named pip.日志--使用pip安装

tensorflow - 训练 CNN 后,其输出是否应该是确定性的?

tensorflow - 可视化增强的火车图像 [tensorflow object detection api]

machine-learning - 如何对大相似度矩阵进行层次聚类

python - "Josephus-p‌r‌o‌b‌l‌e‌m"在 python 中使用列表