【发布时间】: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 哦,我不知道。我加了。
-
训练数据是什么?它从哪里来的?那些是位图吗?
-
而且,顺便说一下,
while、if和elifPython 中的语句不使用额外的括号。它们不是函数。这是 C 和 C++ 遗留下来的一个坏习惯。 -
CrossValidated 可能是提出这些问题的更好地方。
标签: python tensorflow machine-learning