python - 预测单个观察结果始终显示相同的类别

标签 python keras deep-learning image-recognition

我正在尝试为孟加拉语字符制作 OCR。我有 168 个不同的类(class)。为了预测每个角色,我的模型是

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=(42,28,1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

它在我的测试集中表现良好。 我试图预测单个实例,但对于每个不同的实例,它显示相同的类输出。我读到的单张图像如下

from PIL import Image
#location for single image
location='Bangla-Compound-Character-Recognition/data/2/ka.jpg'

#image size = (42x28) grayscale
image=np.array(Image.open(location)).reshape(42,28,1)
image=np.expand_dims(image,axis=0)
single_image_cls=model.predict_classes(image)
print(single_image_cls)

预测测试集的单个实例显示了正确的结果,测试准确率为 90%

#predicting a single test instance
probablity=model.predict_classes(x_test[100:101])

最佳答案

在新图像上进行测试时,您必须应用与训练集相同的归一化,通常将图像像素除以 255:

single_image_cls=model.predict_classes(image / 255)

关于python - 预测单个观察结果始终显示相同的类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56710800/

相关文章:

python - 如何在 Tensorboard 中显示所有渐变的列表?

python - 创建正确的轴标签并理解它们

python - 来自 Tkinter Python 上顶级行为的顶级

python - 如何知道一个词的具体TF-IDF值?

python - 使用 keras 保存模型

python - ubuntu中gpu上的tensorflow安装

machine-learning - Keras 模型评估中的损失

python - 为什么 TensorFlow 的 `tf.data` 包会减慢我的代码速度?

python - 在 Python Pandas 中将 GZIP 压缩应用于 CSV

python - 在新维度中堆叠数组