Keras 模型提供 87% 的准确率,但实时结果不准确



我正在使用 mnist 手语数据集进行模型训练。 该模型训练良好,准确性足够好,但在实时预测中失败。此外,如果有人还可以指导如何使用此模型进行基于相机的实时手势,以便执行某些任务。

第二点是,将csv数据集用于图像而不是实际图像进行模型训练是一个好主意。它是否会影响模型的准确性。

"""AlmostFinal.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1bjW72xyST8i2WAzRYj7qXoRPRgID5XZJ
"""
pip install kaggle
from google.colab import files
files.upload()
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
!chmod 600  ~/.kaggle/kaggle.json
!kaggle datasets download -d datamunge/sign-language-mnist
from zipfile import ZipFile
file_name = 'sign-language-mnist.zip'
with ZipFile(file_name,'r') as zip:
zip.extractall()
print('done')
import numpy as np
import pandas as pd
train = pd.read_csv("/content/sign_mnist_train.csv").values
test  = pd.read_csv("/content/sign_mnist_test.csv").values

trainX = train[:, 1:].reshape(train.shape[0],28,28,1).astype( 'float32' )
X_train = trainX / 255.0
y_train = train[:,0]

# Reshape and normalize test data
testX = test[:,1:].reshape(test.shape[0],28,28,1).astype( 'float32' )
X_test = testX / 255.0
y_test = test[:,0]
from sklearn import preprocessing
lb = preprocessing.LabelBinarizer()
y_train = lb.fit_transform(y_train)
y_test = lb.fit_transform(y_test)
print(X_test.shape[1:])
print(y_test.shape[1:])
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
model = keras.Sequential(
[
layers.Convolution2D(50,(3,3),input_shape=(28,28,1),activation='relu',padding='same'),
layers.MaxPooling2D((2,2),padding='same'),
layers.Convolution2D(250,(3,3),activation='relu',padding='same'),
layers.Flatten(),
layers.Dropout(0.2),
layers.Dense(250, activation="relu"),
layers.Dense(24,activation='softmax')
]
)

#model.add(Convolution2D(30, 3, 3, border_mode= 'valid' , input_shape=(28, 28, 1),activation= 'relu', dim_ordering='tf' ))
##model.add(MaxPooling2D(pool_size=(2, 2)))
#model.add(Dense(24, activation= 'softmax' ))
model.compile(loss= 'categorical_crossentropy' , optimizer= 'adam' , metrics=[ 'accuracy' ])
#model.compile(loss= 'categorical_crossentropy' , optimizer= 'adam' , metrics=[ 'accuracy' ])
model.summary()
model.fit(X_train, y_train,
epochs=20,batch_size = 20,verbose=1)
score = model.evaluate(X_test, y_test, batch_size=128)
print(score)
print("test loss, test acc:", score)
predictions = model.predict()
import numpy as np
import scipy.misc
from google.colab import files
from keras.preprocessing import image
import cv2

uploaded = files.upload()
for fn in uploaded.keys():
path = fn
#img = image.load_img(path, target_size=(28, 28),color_mode="grayscale")
#x = tf.image.resize_images(fn, (28, 28),)

image = cv2.imread(fn)
new_image = cv2.imread(fn,0)
new_image = cv2.resize(image,(28,28))
x = np.expand_dims(new_image,axis = 3)

#x = np.expand_dims(x, axis=3)

images = np.vstack([x])
classes = model.predict(images, batch_size=10)
print(classes)
print(classes) 

准确率为87%,但实时结果并不好。

您的用例与您的模型训练的数据不匹配。尝试将一些您自己的图像添加到数据集中或创建您自己的数据集。此外,尝试添加一些泛化技术,如数据增强、权重衰减等。

最新更新