gpt4 book ai didi

python - 无法使用 CNN 训练手势 (ASL) 模型

转载 作者:行者123 更新时间:2023-11-30 08:48:08 24 4
gpt4 key购买 nike

我已经使用 CNN 成功区分了狗和猫,现在我正在尝试训练(ASL)美国手语模型,我做了一些更改但没有成功,现在我不知道要更改代码和我用哪种方式谷歌搜索了这个,但不幸的是没有成功,这是我的 FYP-(最后一年项目),我陷入困境,请帮助我。

我将 loss = binary_crossentropy 更改为 loss =稀疏_categorical_crossentropy,但仍然显示标签错误。

1 类数据预处理:

'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 类 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()

我希望如果我输入任何字母表的图像,那么它应该显示该字母表的相应名称。

最佳答案

您应该将损失更改为分类交叉熵。甚至我也用 Keras 构建了一个类似的 CNN。

此 CNN 旨在识别 3 种不同类型的图像,但您可以更改 input_shape 来检测任意数量的类别。

# 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]])

关于python - 无法使用 CNN 训练手势 (ASL) 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57116133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com