我想创建一个能够独立玩赛车游戏的CNN。 我用30.000个样本训练了模型,我的特征(x_test)是帧,我的标签(y_test)是w和d键(单热编码) 但我的模型预测这不是预测 三个概率:
[ 1. 0. 0.]
[ 1. 0. 0.]
[ 1. 0. 0.]
[ 1. 0. 0.]
[ 1. 0. 0.]
[ 1. 0. 0.]
我的模型结构中存在问题,还是需要更大的数据集?
我的代码是:
训练
classifier = create_model()
train_data=np.load('data1/final_data.npy')
for i in range(52):
file_name = 'data2/training_data-{}.npy'.format(i)
train_data_2=np.load(file_name)
train_data=np.concatenate((train_data,train_data_2))
shuffle(train_data)
x=train_data[:,0]
y=train_data[:,1]
train_data=[]
x=x.tolist()
x=np.array(x)
y=y.tolist()
y=np.array(y)
classifier.fit(x,y, epochs=5, batch_size=32)
classifier.save('/output/model.model')
模型
def create_model():
classifier = Sequential()
classifier.add(Conv2D(96,(3,3),input_shape=(120,160,3),activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Conv2D(256,(3,3),activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Conv2D(384,(3,3),activation='relu'))
classifier.add(Conv2D(384,(3,3),activation='relu'))
classifier.add(Conv2D(256,(3,3),activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Flatten())
classifier.add(Dense(units = 2048, activation = 'relu'))
classifier.add(Dropout(0.5))
classifier.add(Dense(units = 2048, activation = 'relu'))
classifier.add(Dropout(0.5))
classifier.add(Dense(units = 3, activation = 'softmax'))
classifier.compile(optimizer = 'rmsprop', loss = 'categorical_crossentropy',
metrics = ['accuracy'])
return classifier
测试
def straight():
ReleaseKey(A)
ReleaseKey(D)
PressKey(W)
def left():
ReleaseKey(D)
PressKey(W)
PressKey(A)
def right():
ReleaseKey(A)
PressKey(W)
PressKey(D)
def main():
model=load_model('model.model')
for i in list(range(5))[::-1]:
print(i+1)
time.sleep(1)
paused = False
while(True):
if not paused:
screen = grab_screen(region=(0,64,640,480))
screen = cv2.resize(screen, (160,120))
screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)
screen = np.expand_dims(screen, axis = 0)
prediction = model.predict(screen)[0]
print(prediction)
maxval=max(prediction)
if prediction[1] == maxval:
# straight()
print('straigh',maxval)
elif prediction[2] == maxval:
# right()
print('right',maxval)
elif prediction[0] == maxval:
# left()
print('left',maxval)
#
# keys = key_check()
#
# if 'P' in keys:
# if paused:
# paused = False
# time.sleep(1)
# else:
# paused = True
# ReleaseKey(A)
# ReleaseKey(W)
# ReleaseKey(D)
# time.sleep(1)
main()
答案 0 :(得分:0)
可以通过创建更深层的神经网络来解决。