pythontensorflowkerasdeep-learningq-learning

ValueError: Input 0 of layer sequential_5 is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 953]


import pyautogui as pg
from PIL import ImageGrab, ImageOps
import numpy as np
import tensorflow as tf
from tensorflow import keras
from time import sleep
from tensorflow.keras.layers import *
import cv2
import random

box = (0,259,953,428)

def bigjump():
    pg.keyDown('space')
    sleep(0.1)
    pg.keyUp('space')
    
def smalljump():
    pg.press('space')
    
def duck():
    pg.keyDown('down')
    sleep(0.4)
    pg.keyUp('down')
    
def noting():
    sleep(0.1)

screen = ImageGrab.grab(bbox = box)
screen = ImageOps.grayscale(screen)

screen_n = np.array(screen)/255

model = keras.models.Sequential()

model.add(Conv2D(32,(8,8),strides=(2,2)))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Activation('relu'))
model.add(Conv2D(64,(4,4),strides=(2,2),padding= 'same'))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Activation('relu'))
model.add(Conv2D(64,(3,3),strides=(1,1),padding= 'same'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dense(512,activation='relu'))
model.add(Dense(4))

model.compile(optimizer = 'adam',loss = 'mean_square_error',metrics =['accurate']) 

model.build((169,953))
model.summary()

done = 0
count = 0

discount = 0.99

max_episodes = 1000

state_list = list()
q_list = list()

replay_mem = list()

replay_size = 32
replay_limit = 2048

for episode in range(max_episodes):
    while not done:
        
        count += 1
        e = 1 / ((episode / 10) + 1)
    
        screen = ImageGrab.grab(bbox = box)
        screen_n = np.array(ImageOps.grayscale(screen))/255
        
        q = model.predict(screen_n)
    
        if np.random.rand(1)<e:
            action = random.randint(1,4)
        else:
            action = np.argmax(q)

        if action == 1:
            bigjump()
        elif action == 2:
            smalljump()
        elif action == 3:
            duck()
        elif action == 4:
            noting()
        
        screen_next = ImageGrab.grab(bbox = box)
        screen_next_n = np.array(ImageOps.grayscale(screen_next))/255
        
        if screen_next_n == screen_n:
            done = 1
            reward = -100
        else:
            reward = 1
            
        replay_mem.append([screen_n,screen_next_n,action,done,reward])
        
        if len(replay_mem)<replay_size:
            replay_size = len(replay_mem)
        
        for sample in random.sample(replay_mem,replay_size):
            screen_n,screen_next_n,action_dont,reward = sample
            if done:
                q[0,action] = -100
            else:
                q_next = model.predict(screen_next_n)
                q[0,action] = reward + discount*argmax(q_next)
            
            state_list.append(screen_n)
            q_list.append(q)
        hist = model.fit(state_list,q_list,epochs = 5,verbose =0)

I am making Q-learning Algorithm to play Chrome dino

I capture screen and convert to binary image and convert to numpy array

And i use model.predict to find q-value but error apear

is there any method for solving this?

ValueError: Input 0 of layer sequential_5 is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 953]


Solution

  • I think its something to do with the grayscaling, can you please share the dimensions of the input layer of the model.

    Probably, the model was trained on 3 channel data, that is why it asks for 4 dims (batch_size, ch1, ch2, ch3), while your grayscale image is single channel and hence 2 dims (batch_size, ch1)