I try to test a neural network for a game but always when i run the script i get this error:
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 24), found shape=(None, 8410)
The model takes data from a game in order to predict the correct key to press.I realy don't understand what i m doing wrong
Here is the code for the neural network program:
import pickleimport tensorflow as tfimport matplotlib.pyplot as pltimport numpy as npimport kerasimport keras.metricsimport keras.backend as Kimport keras.layersdefault_sequence = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]def f1(y_true, y_pred): def recall_function(y_true, y_pred): true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) possible_positives = K.sum(K.round(K.clip(y_true, 0, 1))) recall_value = true_positives / (possible_positives + K.epsilon()) return recall_value def precision_function(y_true, y_pred): true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1))) precision_value = true_positives / (predicted_positives + K.epsilon()) return precision_value precision = precision_function(y_true, y_pred) recall = recall_function(y_true, y_pred) return 2 * ((precision * recall) / (precision + recall + K.epsilon()))inputPath = "Dataset/stateData0.pickle"outputPath = "Dataset/outputData0.pickle"f = open(inputPath, "rb")g = open(outputPath, "rb")X = []y = []try: while True: example = pickle.load(f) a = example[0] a += example[2] X.append(a) y.append(pickle.load(g))except: passf.close()g.close()y = list(filter(None, y))while len(X) != len(y): y.append(default_sequence)print(len(X))print(len(y))for i in range(len(y)): print(y[i]) print("\n")X = np.asarray(X, dtype=np.float32)y = np.asarray(y, dtype=np.float32)training_size = 3000x_train = X[:training_size]y_train = y[:training_size]x_test = X[training_size:]y_test = y[training_size:]a = 0for i in range(len(y_train)): if y_train[i][13] == 1: a += 1print("node imbalance:", a, "/", len(y_train))print("X SHAPE:", X.shape)print("Y SHAPE:", y.shape)print("Train SHAPES")print(x_train.shape)print(y_train.shape)print("Test SHAPES")print(x_test.shape)print(y_test.shape)config = tf.compat.v1.ConfigProto()config.gpu_options.allow_growth = Truesession = tf.compat.v1.Session(config=config)model = keras.models.Sequential()model.add(keras.layers.Dense(128, activation=tf.nn.relu))model.add(keras.layers.Dense(128, activation=tf.nn.relu))model.add(keras.layers.Dense(128, activation=tf.nn.relu))model.add(keras.layers.Dense(14, activation=tf.nn.sigmoid))model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=[f1])class_weights = { 0: 1, 1: 1, 2: 1, 3: 1, 4: 3, 5: 3, 6: 3, 7: 2, 8: 2, 9: 2, 10: 2, 11: 1, 12: 1, 13: 1 }model.fit(x_train, y_train, epochs=10, batch_size=16, class_weight=class_weights, validation_data=[x_test,y_test])val_loss, val_prec = model.evaluate(x_test, y_test)print("Validation loss: ", val_loss, "Validation_accuracy: ", val_prec)model.save("Models/nk_ff_fighter.model")print('Model Saved')print(model.summary())
OUTPUT:
node imbalance: 52 / 3000X SHAPE: (3349, 24)Y SHAPE: (3349, 14)Train SHAPES(3000, 24)(3000, 14)Test SHAPES(349, 24)(349, 14)Model SavedModel: "sequential"_________________________________________________________________Layer (type) Output Shape Param # ================================================================= dense (Dense) (None, 128) 3200 dense_1 (Dense) (None, 128) 16512 dense_2 (Dense) (None, 128) 16512 dense_3 (Dense) (None, 14) 1806 =================================================================Total params: 38,030Trainable params: 38,030Non-trainable params: 0_________________________________________________________________None
Here is the code for the test program:
import numpy as npfrom PIL import ImageGrabimport cv2import timefrom Scripts.OutputControlScript import switch_guard, UPfrom Scripts.StateDetector import checkCurrentGuardInput, generate_feature_listfrom Scripts.processInputToGame import inputToGameimport keyboardimport tensorflow as tfimport kerasimport keras.backend as KmaxHealth = 6running = Falsegpu_options = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=0.333)sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpu_options))def toggle_running(key): global running if running: print("Neural Knight OFF.") running = False else: print("Neural Knight ON...")def read_data(state): if state[3] == 1: print("The oponent guard is right") if state[4] == 1: print("The oponent guard is top") if state[5] == 1: print("The oponent guard is left")def f1(y_true, y_pred): def recall_function(y_true, y_pred): true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) possible_positives = K.sum(K.round(K.clip(y_true, 0, 1))) recall_value = true_positives / (possible_positives + K.epsilon()) return recall_value def precision_function(y_true, y_pred): true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1))) precision_value = true_positives / (predicted_positives + K.epsilon()) return precision_value precision = precision_function(y_true, y_pred) recall = recall_function(y_true, y_pred) return 2 * ((precision * recall) / (precision + recall + K.epsilon()))def main(): global running keyboard.on_release_key("v", toggle_running) for i in list(range(1))[::-1]: print(i + 1) time.sleep(1) print("Loading model...") switch_guard(UP) currentGuardDirection = UP model = keras.models.load_model('path_to_model', custom_objects={"f1": f1}) print("Neural Warrior is now Active") prediction = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]) prevFrames = 600 prevPrediction = [] for i in range(prevFrames): prevPrediction.append(prediction) print(prediction) while True: # screen = np.array(ImageGrab.grab(bbox=(0, 130, 1024, 700))) screen = cv2.imread('test.jpg') currentGuardDirection = checkCurrentGuardInput(prediction, currentGuardDirection) state = generate_feature_list(screen, currentGuardDirection) extractedState = state[0] prevPrediction = prevPrediction[1:] prevPrediction.append(prediction) predHistory = [y for x in prevPrediction for y in x] extractedState = np.expand_dims(extractedState, axis=0) extractedState = np.concatenate((extractedState[0], predHistory), axis=0) extractedState = np.expand_dims(extractedState, axis=0) prediction = model.predict(extractedState)[0] b = np.zeros_like(prediction) biggest = 0 biggest_Index = -1 for i in range(4, 7): if prediction[i] > 0.2: if prediction[i] > biggest: biggest = prediction[i] biggest_Index = i if biggest_Index > -1: b[biggest_Index] = 1 for i in range(0, 4): if prediction[i] > 0.2: b[i] = 1 for i in range(7, len(b)): if prediction[i] > 0.2: b[i] = 1 inputToGame(b) print(b) if keyboard.is_pressed("."): cv2.destroyAllWindows() breakmain()
OUTPUT
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 24), found shape=(None, 8410)