如何解决Tensorflow:CNN具有99%的测试准确度,总是在预测时出错
- 这是我使用CNN训练的模型*
import numpy as np # linear algebra
import pandas as pd # data processing,CSV file I/O (e.g. pd.read_csv)
import os
import matplotlib.pyplot as plt
import seaborn as sns
import keras
from keras.models import Sequential`enter code here`
from keras.layers import Dense,Conv2D,MaxPool2D,Flatten,Dropout,BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report,confusion_matrix
from keras.callbacks import ReduceLROnPlateau
import matplotlib.pyplot as plt
import seaborn as sns
import keras
from keras.models import Sequential
from keras.layers import Dense,confusion_matrix
from keras.callbacks import ReduceLROnPlateau
from sklearn.preprocessing import LabelBinarizer
train_df = pd.read_csv("D:/RealFinalProject/asl/sign_mnist_train/sign_mnist_train.csv")
test_df = pd.read_csv("D:/RealFinalProject/asl/sign_mnist_test/sign_mnist_test.csv")
test = pd.read_csv("D:/RealFinalProject/asl/sign_mnist_test/sign_mnist_test.csv")
y = test['label']
y_train = train_df['label']
y_test = test_df['label']
del train_df['label']
del test_df['label']
label_binarizer = LabelBinarizer()
y_train = label_binarizer.fit_transform(y_train)
y_test = label_binarizer.fit_transform(y_test)
x_train = train_df.values
x_test = test_df.values
x_train = x_train / 255
x_test = x_test / 255
x_train = x_train.reshape(-1,28,1)
x_test = x_test.reshape(-1,1)
"""f,ax = plt.subplots(2,5)
f.set_size_inches(10,10)
k = 0
for i in range(2):
for j in range(5):
ax[i,j].imshow(x_train[k].reshape(28,28),cmap = "gray")
k += 1
plt.tight_layout() """
datagen = ImageDataGenerator(
featurewise_center=False,# set input mean to 0 over the dataset
samplewise_center=False,# set each sample mean to 0
featurewise_std_normalization=False,# divide inputs by std of the dataset
samplewise_std_normalization=False,# divide each input by its std
zca_whitening=False,# apply ZCA whitening
rotation_range=10,# randomly rotate images in the range (degrees,0 to 180)
zoom_range = 0.1,# Randomly zoom image
width_shift_range=0.1,# randomly shift images horizontally (fraction of total width)
height_shift_range=0.1,# randomly shift images vertically (fraction of total height)
horizontal_flip=False,# randomly flip images
vertical_flip=False) # randomly flip images
datagen.fit(x_train)
learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',patience = 2,verbose=1,factor=0.5,min_lr=0.00001)
model = Sequential()
model.add(Conv2D(75,(3,3),strides = 1,padding = 'same',activation = 'relu',input_shape = (28,1)))
model.add(BatchNormalization())
model.add(MaxPool2D((2,2),strides = 2,padding = 'same'))
model.add(Conv2D(50,activation = 'relu'))
model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(MaxPool2D((2,padding = 'same'))
model.add(Conv2D(25,activation = 'relu'))
model.add(BatchNormalization())
model.add(MaxPool2D((2,padding = 'same'))
model.add(Flatten())
model.add(Dense(units = 512,activation = 'relu'))
model.add(Dropout(0.3))
model.add(Dense(units = 24,activation = 'softmax'))
model.compile(optimizer = 'adam',loss = 'categorical_crossentropy',metrics = ['accuracy'])
#model.summary()
history = model.fit(datagen.flow(x_train,y_train,batch_size = 128),epochs = 5,validation_data =
(x_test,y_test),callbacks = [learning_rate_reduction])
print("Accuracy of the model is - ",model.evaluate(x_test,y_test)[1]*100,"%")
model.save('trained_data.h5')
'''**This is my prediction using model**This is my prediction using model'''
import cv2
import numpy as np
from keras.models import load_model
from keras.preprocessing import image
import imutils
model=load_model('trained_data.h5')
print('model loaded!')
out_label=['A','B','C','D','E','F','G','H','I','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y']
pre=[]
s=''
cchar=[0,0]
c1=''
aWeight = 0.5
capture = cv2.VideoCapture(0)
top,right,bottom,left = 170,150,425,450
num_frames = 0
flag=0
flag1=0
while capture.isOpened():
ret,frame = capture.read()
frame = imutils.resize(frame,width=700)
frame = cv2.flip(frame,1)
clone = frame.copy()
(height,width) = frame.shape[:2]
roi = frame[top:bottom,right:left]
gray = cv2.cvtColor(roi,cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray,(7,7),0)
img=gray
IMG_SIZE=28
img = cv2.resize(img,(IMG_SIZE,IMG_SIZE))
test_data =img
orig = img
data = img.reshape(-1,IMG_SIZE,1)
model_out = model.predict([data])
# print(model_out)
model_out = model.predict([data])
# print(model_out)
pnb=np.argmax(model_out)
print(str(np.argmax(model_out))+" "+str(out_label[pnb]))
pre.append(out_label[pnb])
cv2.putText(clone,'%s ' % (str(out_label[pnb])),(450,150),cv2.FONT_HERSHEY_PLAIN,5,(0,255,0))
cv2.rectangle(frame,(100,100),(300,300),0),0)
crop_image = frame[100:300,100:300]
blur = cv2.GaussianBlur(crop_image,0)
hsv = cv2.cvtColor(blur,cv2.COLOR_BGR2HSV)
mask2 = cv2.inRange(hsv,np.array([2,0]),np.array([20,255]))
kernel = np.ones((5,5))
dilation = cv2.dilate(mask2,kernel,iterations=1)
erosion = cv2.erode(dilation,iterations=1)
filtered = cv2.GaussianBlur(erosion,0)
ret,thresh = cv2.threshold(filtered,127,0)
cv2.imshow("Gesture",frame)
#all_image = np.hstack((drawing,crop_image))
#cv2.imshow("contours",all_image)
cv2.imshow("Thresholded",thresh)
if cv2.waitKey(1) == ord('q'):
break
capture.release()
cv2.destroyAllWindows()
我确实在tensorflow之上使用keras构建了一个convnet并对其进行了遍历训练,但是一直以来,它的准确性都达到了99%,但是当我预测图像时,它总是会预测错误
我的模型总是预测错误我的代码中有什么错误,请纠正我 I used keras dataset cvs file
解决方法
问题是您对火车上的输入图像(x_train / 255
)进行了归一化,但是当您预测直接使用原始图像时。
这里
data = img.reshape(-1,IMG_SIZE,1)
model_out = model.predict([data])
应该是
data = img.reshape(-1,1)
data = data / 255
model_out = model.predict([data])
这应该使结果相对接近训练时的水平。
但是您的网络看起来很小,因此如果准确性仍然不令人满意,您应该添加更多的CNN层。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。