Click here to Skip to main content
15,921,577 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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 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(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"])

#Compiling The CNN
#Part 2 Fittting the CNN to the image
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(

test_datagen = ImageDataGenerator(rescale=1./255)

training_set = train_datagen.flow_from_directory(

test_set = test_datagen.flow_from_directory(
        target_size=(224 ,224),

earlystopping = callbacks.EarlyStopping(monitor="val_loss" , mode = "min" , patience=5 , restore_best_weights=True , verbose=1)
        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))"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)
# load all images into a list
a = list(string.ascii_uppercase)
for i in a:   
    img = image.load_img("asl_alphabet_test/"+i+".jpg")
    img = np.expand_dims(img,axis=0)
    img = img/255
    result = model.predict(img)
    top_k = result[0].argsort()[-len(result[0]):][::-1]
    a = []
    for j in top_k:
            sign = labels[j]
            score = result[0][j]
    a = sorted(a, key = lambda x: x[1],reverse=True)
    print(i,a[0][0] , a[0][1]*100) 

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<pre>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900