【问题标题】:Letter recognition is super inaccurate字母识别超级不准确
【发布时间】:2022-01-14 00:40:21
【问题描述】:

我正在尝试制作一个对字母进行分类的神经网络。我对其进行了训练,它的准确率达到了 96%,但是当我让它对事物进行分类时,它要么正确猜测,要么猜测 R。当我试图让它打印出确定性时,它总是很低(我见过的最高是 22%),每当它猜测 R(除非它实际上是 R)时,它都会说 17% 的准确度。

这里是完整的代码:

import cv2
import numpy as np
import os
import matplotlib.pyplot as plt
import tensorflow as tf
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle

word_dict = {0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I',9:'J',10:'K',11:'L',12:'M',13:'N',14:'O',15:'P',16:'Q',17:'R',18:'S',19:'T',20:'U',21:'V',22:'W',23:'X', 24:'Y',25:'Z'}
a = input("Would you like to train a new model ('train'), evaluate the current one ('evaluate'), run the current one ('run'), or exit the program ('exit)? ")
while(a!="exit"):
    
    if(a.lower()=="train"):

        data = pd.read_csv(r"training_data.csv").astype('float32')

        X = data.drop('0',axis=1)
        Y = data['0']

        x_train, x_test, y_train, y_test = train_test_split(X,Y, test_size = 0.2)

        x_train = np.reshape(x_train.values, (x_train.shape[0], 28, 28))
        x_test = np.reshape(x_test.values, (x_test.shape[0], 28, 28))

        shuff = shuffle(x_train[:100])

        x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], 1)

        x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], 1)

        model = tf.keras.models.Sequential()

        model.add(tf.keras.layers.Flatten(input_shape=(28,28)))

        model.add(tf.keras.layers.Dense(units=256, activation=tf.nn.relu))
        model.add(tf.keras.layers.Dense(units=256, activation=tf.nn.relu))
        model.add(tf.keras.layers.Dense(units=256, activation=tf.nn.relu))
        model.add(tf.keras.layers.Dense(units=256, activation=tf.nn.relu))

        model.add(tf.keras.layers.Dense(units=26, activation=tf.nn.softmax))

        model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])


        model.fit(x_train, y_train, epochs=4)

        model.save('handwritting.model')

        accuracy, loss = model.evaluate(x_test, y_test)
        print("the accuracy is: ", accuracy)
        print("The loss is: ", loss)
    elif(a.lower()=="evaluate"):
        data = pd.read_csv(r"training_data.csv").astype('float32')
        X = data.drop('0',axis=1)
        Y = data['0']
        x_train, x_test, y_train, y_test = train_test_split(X,Y, test_size = 0.2)
        x_test = np.reshape(x_test.values, (x_test.shape[0], 28, 28))
        shuff = shuffle(x_train[:100])
        x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], 1)
        model = tf.keras.models.load_model('handwritting.model')
        accuracy, loss = model.evaluate(x_test, y_test)
        print("the accuracy is: ", accuracy)
        print("The loss is: ", loss)
    elif(a.lower()=="run"):
        model = tf.keras.models.load_model('handwritting.model')
        a2 = input("What is the filename of the png image?")
        
        img = cv2.imread(f"{a2}")[:,:,0]
        img = np.invert(np.array([img]))
        prediction = model.predict(img)
        print(f"This letter is {word_dict[np.argmax(prediction)]}")
        print(f"The program is {np.argmax(prediction).round()}% certain")
    a = input("Would you like to train a new model ('train'), evaluate the current one ('evaluate'), run the current one ('run'), or exit the program ('exit)? ")

这是一个谷歌文档的链接,其中包含我使用的代码、训练数据和测试图像,以防你想用与我完全相同的设置进行尝试:https://drive.google.com/drive/folders/1p4CAHc-YrcQ8XPO7k539Z4giKLk-xIsV?usp=sharing

【问题讨论】:

  • 所有信息和代码都必须作为文本出现在问题中,而不是作为外部链接。
  • @Dr.Snoopy 哦,我不知道。我加了。
  • 训练数据是什么?它从哪里来的?那些是位图吗?
  • 而且,顺便说一下,whileifelif Python 中的语句不使用额外的括号。它们不是函数。这是 C 和 C++ 遗留下来的一个坏习惯。
  • CrossValidated 可能是提出这些问题的更好地方。

标签: python tensorflow machine-learning


【解决方案1】:

您可以尝试在很短的时间内提高您当前正在训练的 epoch。我怀疑您的模型可能仅在 4 个 Epoch 之后就没有收敛……尝试将其设置得更高。例如

model.fit(x_train, y_train, epochs=50)

观察你的损失是否正在改善,或者它是否在整个训练期间保持不变。

在训练期间防止过度拟合的另一件事是在模型中包含 dropout 层。例如

    model.add(tf.keras.layers.Flatten(input_shape=(28,28)))

    model.add(tf.keras.layers.Dense(units=256, activation=tf.nn.relu))
    model.add(Dropout(0.2))
    model.add(tf.keras.layers.Dense(units=256, activation=tf.nn.relu))
    model.add(Dropout(0.2))
    model.add(tf.keras.layers.Dense(units=256, activation=tf.nn.relu))
    model.add(Dropout(0.2))
    model.add(tf.keras.layers.Dense(units=256, activation=tf.nn.relu))

    model.add(tf.keras.layers.Dense(units=26, activation=tf.nn.softmax))

如果这有帮助,请告诉我,我稍后回家时会测试它。

【讨论】:

  • epoch 6 是我看到显着改进的最后一个 epoch(之后一些 epoch 甚至比前一个更差)。它最终浮动在 93.25% 左右的准确率和 0.33 损失。我只有 11 个 epoch,我会在一夜之间运行它以查看它的结果,然后添加新评论或编辑此评论
  • 还要确保当您在最后一个 elif 中进行单一预测时,您准备图像就像准备火车和测试日期一样;)
  • 另外,降低 .compile 中的学习因素可能会有所帮助
  • 这不起作用。在尝试了您和@rikyeah 的建议后,我尝试正确的 9 个得到了 1 个
  • 所以我设置了你的项目,我很确定当你加载单个图像与你的火车数据相比时,你的图像数据准备是不一样的。你是怎么得到csv的。你有这个的代码吗?
猜你喜欢
  • 1970-01-01
  • 2020-03-10
  • 2012-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多