I am trying to create a sign language interpreter which detects hand gestures using the webcam and displays the corresponding letter or number on the screen.
I was following a tutorial and trying to train the model using Keras, but I am getting the following error:
ValueError: (500,44) and (500,40) are incompatible
The code:
import numpy as np
import pickle
import cv2, os
from glob import glob
from tensorflow.keras import optimizers
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint
from tensorflow.keras import backend as K
K.image_data_format()
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
def get_image_size():
img = cv2.imread('gestures/1/100.jpg', 0)
return img.shape
def get_num_of_classes():
return len(glob('gestures/*'))
image_x, image_y = get_image_size()
def cnn_model():
num_of_classes = get_num_of_classes()
model = Sequential()
model.add(Conv2D(16, (2,2), input_shape=(image_x, image_y, 1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))
model.add(Conv2D(32, (3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(3, 3), padding='same'))
model.add(Conv2D(64, (5,5), activation='relu'))
model.add(MaxPooling2D(pool_size=(5, 5), strides=(5, 5), padding='same'))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_of_classes, activation='softmax'))
sgd = optimizers.SGD(lr=1e-2)
class_mode='categorical'
model.compile(loss='categorical_crossentropy',
optimizer=sgd, metrics=['accuracy'])
filepath="cnn_model_keras2.h5"
checkpoint1 = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint1]
from keras.utils import plot_model
plot_model(model, to_file='model.png', show_shapes=True)
return model, callbacks_list
def train():
with open("train_images", "rb") as f:
train_images = np.array(pickle.load(f))
with open("train_labels", "rb") as f:
train_labels = np.array(pickle.load(f), dtype=np.int32)
with open("val_images", "rb") as f:
val_images = np.array(pickle.load(f))
with open("val_labels", "rb") as f:
val_labels = np.array(pickle.load(f), dtype=np.int32)
train_images = np.reshape(train_images, (train_images.shape[0], image_x, image_y, 1))
val_images = np.reshape(val_images, (val_images.shape[0], image_x, image_y, 1))
train_labels = np_utils.to_categorical(train_labels)
val_labels = np_utils.to_categorical(val_labels)
print(val_labels.shape)
model, callbacks_list = cnn_model()
model.summary()
model.fit(train_images, train_labels, validation_data=(val_images, val_labels), epochs=20, batch_size=500, callbacks=callbacks_list)
scores = model.evaluate(val_images, val_labels, verbose=0)
print("CNN Error: %.2f%%" % (100-scores[1]*100))
#model.save('cnn_model_keras2.h5')
train()
K.clear_session();
The issue is likely with how you're splitting out your training and validation data set. This error is thrown because there is a mismatch between the number of training classes (44) and the number of validation classes (40).
You'll likely want to either make sure all classes are represented in the validation set or utilize the num_classes
parameter in to_categorical to ensure they are all represented in the one-hot encoding.
train_labels = np_utils.to_categorical(train_labels, num_classes=44)
val_labels = np_utils.to_categorical(val_labels, num_classes=44)
https://www.tensorflow.org/api_docs/python/tf/keras/utils/to_categorical
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.