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

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)

我正在制作 Q 学习算法来玩 Chrome 恐龙

我捕获屏幕并转换为二进制图像并转换为 numpy 数组

我使用 model.predict 查找 q 值但出现错误

有什么方法可以解决吗?

ValueError:层 sequential_5 的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=2。已收到完整形状:[None, 953]

我觉得跟灰度有关,能不能分享一下模型输入层的尺寸。

可能,该模型是在 3 通道数据上训练的,这就是为什么它要求 4 暗淡(batch_size、ch1、ch2、ch3),而您的灰度图像是单通道,因此是 2 暗淡( batch_size, ch1)