My project is hand sign recognition, I created the dataset and i wanted to train the model using keras and predict in real time .
I trained the model and i am getting better accuracy but all the predictions (completely ) are worng.
How do i get accurate predictions. Is there a defect in training or testing the model . How do i resolve this problem. Please help me.
What I have tried:
My cnn_model.py is as follows
import numpy as np
import keras
from keras.layers import Conv2D, MaxPool2D, Flatten, Dense, Dropout
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras import regularizers,callbacks
# Initialing the CNN
model = Sequential()
model.add(Conv2D(16, kernel_size = [3,3], padding = 'same', activation = 'relu', input_shape = (224,224,3)))
model.add(Conv2D(32, kernel_size = [3,3], padding = 'same', activation = 'relu'))
model.add(MaxPool2D(pool_size = [3,3]))
model.add(Conv2D(32, kernel_size = [3,3], padding = 'same', activation = 'relu'))
model.add(Conv2D(64, kernel_size = [3,3], padding = 'same', activation = 'relu'))
model.add(MaxPool2D(pool_size = [3,3]))
model.add(Conv2D(128, kernel_size = [3,3], padding = 'same', activation = 'relu'))
model.add(Conv2D(256, kernel_size = [3,3], padding = 'same', activation = 'relu'))
model.add(MaxPool2D(pool_size = [3,3]))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(512, activation = 'relu', kernel_regularizer = regularizers.l2(0.001)))
model.add(Dense(29, activation = 'softmax'))
model.compile(optimizer = 'adam', loss = keras.losses.categorical_crossentropy, metrics = ["accuracy"])
print("MODEL CREATED")
print(model.summary())
#Compiling The CNN
#Part 2 Fittting the CNN to the image
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
training_set = train_datagen.flow_from_directory(
'Dataset1/train',
target_size=(224,224),
batch_size=16,
class_mode='categorical')
test_set = test_datagen.flow_from_directory(
'Dataset1/test',
target_size=(224 ,224),
batch_size=16,
class_mode='categorical')
earlystopping = callbacks.EarlyStopping(monitor="val_loss" , mode = "min" , patience=5 , restore_best_weights=True , verbose=1)
model.fit(
training_set,
steps_per_epoch=4793/16,
epochs=30,
validation_data = test_set,
validation_steps = 1582/16,
callbacks= [earlystopping]
)
loss, accuracy = model.evaluate(test_set)
print('Final Accuracy of your model is :: %.2f%% '%(accuracy * 100))
print('Final Loss of your model is :: %.2f%% '%(loss * 100))
model.save("Trained_model.h5")
print("Saved model to disk")
"""
Final Accuracy of your model is :: 95.02%
Final Loss of your model is :: 30.57%
Saved model to disk
"""
In order to test the model i made randam images for each sign and tested the model against it.It predicted completely wrong.
The code is as follows
import string
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
import os
from PIL import ImageOps,Image
# image folder
folder_path = 'asl_alphabet_test'
# path to model
model_path = 'Trained_model.h5'
with open("Teachable_machine/keras/labels.txt", 'r') as f:
labels = [line.strip() for line in f.readlines()]
# load the trained model
model = load_model(model_path)
#model.evaluate()
# load all images into a list
a = list(string.ascii_uppercase)
a.extend(['Del','Nothing','Space'])
#print(a)
for i in a:
img = image.load_img("asl_alphabet_test/"+i+".jpg")
img=image.img_to_array(img)
img = np.expand_dims(img,axis=0)
img = img/255
result = model.predict(img)
#print(result)
top_k = result[0].argsort()[-len(result[0]):][::-1]
a = []
for j in top_k:
sign = labels[j]
score = result[0][j]
a.append((sign,score))
a = sorted(a, key = lambda x: x[1],reverse=True)
print(i,a[0][0] , a[0][1]*100)
"""
output
A Predicted label : J
B Predicted label : L
C Predicted label : C
D Predicted label : D
E Predicted label : F
F Predicted label : L
G Predicted label : H
H Predicted label : I
I Predicted label : Nothing
J Predicted label : K
K Predicted label : L
L Predicted label : M
M Predicted label : O
N Predicted label : O
O Predicted label : D
P Predicted label : R
Q Predicted label : S
R Predicted label : L
S Predicted label : Q
T Predicted label : T
U Predicted label : Z
V Predicted label : Y
W Predicted label : Z
X Predicted label : Del
Y Predicted label : Space
Z Predicted label : Nothing
Del Predicted label : E
Nothing Predicted label : P
Space Predicted label : V
"""
You can access the model and test images in the following link
https: