简体   繁体   中英

Can't train model for hand gesture (ASL) by using CNN

I've successfully distinguish dog and cat by using CNN, now I am trying to train model for (ASL) American Sign Language, I made some changes but not worked and now I've no idea what to change in code and which way and also I google for this but unfortunately didn't worked, this is my FYP- (Final Year Project) and I am stuck, please help me.

I changed loss = binary_crossentropy to loss = sparse_categorical_crossentropy and but still showing label error.

1 class Data_preprocessing:

'Data preprocessing before goes to ML'

# Train by data list initilization
training_data = []

def __init__(self, datadir, categories, img_size):
    Data_preprocessing.img_size = img_size
    Data_preprocessing.datadir = datadir
    Data_preprocessing.categories = categories




def Create_training_data(self):

    for category in Data_preprocessing.categories:
        # path to cats or dogs dir
        path = os.path.join(Data_preprocessing.datadir, category)
        class_num = Data_preprocessing.categories.index(category)
        # After having the directory for images
        # Started to read image by using OpenCv and directly convert it to GRAYSCALE
        for img in os.listdir(path):
            try:
                img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)
                new_array = cv2.resize(img_array, (Data_preprocessing.img_size, Data_preprocessing.img_size))
                Data_preprocessing.training_data.append([new_array, class_num])
            except Exception as e:
                pass

    self.Saving_processed_data()

def Saving_processed_data(self):

    random.shuffle(Data_preprocessing.training_data)
    x = []
    y = []
    for features, label in Data_preprocessing.training_data:
        x.append(features)
        y.append(label)

    x = np.array(x).reshape(-1, Data_preprocessing.img_size, Data_preprocessing.img_size, 1)
    # Saving data by using "pickle"
    pickle_out = open("x.pickle", "wb")
    pickle.dump(x, pickle_out)
    pickle_out.close()

    pickle_out = open("y.pickle", "wb")
    pickle.dump(y, pickle_out)
    pickle_out.close()

categories = ["Dog","Cat"]
categories = ["A","B","C","D","del","E","F","G","H","I","J","K","L","M","N","nothing","O","P","Q","R","S","space","T","U","V","W","X","Y","Z"]
data_preprocessing = Data_preprocessing("ASLDS\\ASLDS",categories, 50)
data_preprocessing.Create_training_data()

2 class Learning_model:

def __init__(self):
    pass


def TrainModel(self):    
    self.x = pickle.load(open("x.pickle", "rb"))
    self.y = pickle.load(open("y.pickle", "rb"))

    self.x = self.x/255.0

    self.model = Sequential()

    self.model.add(Conv2D(64, (3,3), input_shape = self.x.shape[1:]))
    self.model.add(Activation("relu"))
    self.model.add(MaxPooling2D(pool_size=(2,2)))

    self.model.add(Conv2D(64, (3,3)))
    self.model.add(Activation("relu"))
    self.model.add(MaxPooling2D(pool_size=(2,2)))

    self.model.add(Flatten())
    self.model.add(Dense(64))

    self.model.add(Dense(1))
    self.model.add(Activation('sigmoid'))

    self.model.compile(optimizer='adam',
          loss='sparse_categorical_crossentropy',
          metrics=['accuracy'])

    self.model.fit(self.x, self.y, batch_size = 32, epochs=10, validation_split = 0.1)

    self.model.save("64x3-CNN-ASL.model")


trained_model = Learning_model()
trained_model.TrainModel()

I am expecting that if I input an image of any alphabet so it is supposed to show me corresponding name of that alphabet.

You should change loss to categorical cross entropy. Even I built a similar CNN with Keras.

This CNN is built to recognize 3 different types of images, but you can change input_shape to detect any number of categories.

# Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense

# Initialising the CNN
classifier = Sequential()


classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))


classifier.add(MaxPooling2D(pool_size = (2, 2)))


classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))


classifier.add(Flatten())


classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 3, activation = 'softmax')) # output layer

# Compiling the CNN
classifier.compile(optimizer = 'adam', loss =  'categorical_crossentropy', metrics = ['accuracy'])

# Using the CNN on the images

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('dataset/training_set',
                                                 target_size = (64, 64),
                                                 batch_size = 32,
                                                 class_mode = 'categorical')

test_set = test_datagen.flow_from_directory('dataset/test_set',
                                            target_size = (64, 64),
                                            batch_size = 32,
                                            class_mode = 'categorical')

classifier.fit_generator(training_set,
                         steps_per_epoch = (8000/32),
                         epochs = 25,
                         validation_data = test_set,
                         validation_steps = (2000/32))


# Fetching Predictions
import numpy as np
from skimage.io import imread
from skimage.transform import resize 

class_labels = {v: k for k, v in training_set.class_indices.items()} 
img = imread('dataset/single_prediction/random.jpg') 
img = resize(img,(64,64))
img = np.expand_dims(img,axis=0) 
if(np.max(img)>1):    
    img = img/255.0 

prediction = classifier.predict_classes(img) 
print ("\n\n")
print (class_labels[prediction[0]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM