-
Notifications
You must be signed in to change notification settings - Fork 92
/
DQN_sekiro_training_gpu.py
240 lines (216 loc) · 7.99 KB
/
DQN_sekiro_training_gpu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 27 21:10:06 2021
@author: pang
"""
import numpy as np
from grabscreen import grab_screen
import cv2
import time
import directkeys
from getkeys import key_check
import random
from DQN_tensorflow_gpu import DQN
import os
import pandas as pd
from restart import restart
import random
import tensorflow.compat.v1 as tf
def pause_game(paused):
keys = key_check()
if 'T' in keys:
if paused:
paused = False
print('start game')
time.sleep(1)
else:
paused = True
print('pause game')
time.sleep(1)
if paused:
print('paused')
while True:
keys = key_check()
# pauses game and can get annoying.
if 'T' in keys:
if paused:
paused = False
print('start game')
time.sleep(1)
break
else:
paused = True
time.sleep(1)
return paused
def self_blood_count(self_gray):
self_blood = 0
for self_bd_num in self_gray[469]:
# self blood gray pixel 80~98
# 血量灰度值80~98
if self_bd_num > 90 and self_bd_num < 98:
self_blood += 1
return self_blood
def boss_blood_count(boss_gray):
boss_blood = 0
for boss_bd_num in boss_gray[0]:
# boss blood gray pixel 65~75
# 血量灰度值65~75
if boss_bd_num > 65 and boss_bd_num < 75:
boss_blood += 1
return boss_blood
def take_action(action):
if action == 0: # n_choose
pass
elif action == 1: # j
directkeys.attack()
elif action == 2: # k
directkeys.jump()
elif action == 3: # m
directkeys.defense()
elif action == 4: # r
directkeys.dodge()
def action_judge(boss_blood, next_boss_blood, self_blood, next_self_blood, stop, emergence_break):
# get action reward
# emergence_break is used to break down training
# 用于防止出现意外紧急停止训练防止错误训练数据扰乱神经网络
if next_self_blood < 3: # self dead
if emergence_break < 2:
reward = -10
done = 1
stop = 0
emergence_break += 1
return reward, done, stop, emergence_break
else:
reward = -10
done = 1
stop = 0
emergence_break = 100
return reward, done, stop, emergence_break
elif next_boss_blood - boss_blood > 15: #boss dead
if emergence_break < 2:
reward = 20
done = 0
stop = 0
emergence_break += 1
return reward, done, stop, emergence_break
else:
reward = 20
done = 0
stop = 0
emergence_break = 100
return reward, done, stop, emergence_break
else:
self_blood_reward = 0
boss_blood_reward = 0
# print(next_self_blood - self_blood)
# print(next_boss_blood - boss_blood)
if next_self_blood - self_blood < -7:
if stop == 0:
self_blood_reward = -6
stop = 1
# 防止连续取帧时一直计算掉血
else:
stop = 0
if next_boss_blood - boss_blood <= -3:
boss_blood_reward = 4
# print("self_blood_reward: ",self_blood_reward)
# print("boss_blood_reward: ",boss_blood_reward)
reward = self_blood_reward + boss_blood_reward
done = 0
emergence_break = 0
return reward, done, stop, emergence_break
DQN_model_path = "model_gpu"
DQN_log_path = "logs_gpu/"
WIDTH = 96
HEIGHT = 88
window_size = (320,100,704,452)#384,352 192,176 96,88 48,44 24,22
# station window_size
blood_window = (60,91,280,562)
# used to get boss and self blood
action_size = 5
# action[n_choose,j,k,m,r]
# j-attack, k-jump, m-defense, r-dodge, n_choose-do nothing
EPISODES = 3000
big_BATCH_SIZE = 16
UPDATE_STEP = 50
# times that evaluate the network
num_step = 0
# used to save log graph
target_step = 0
# used to update target Q network
paused = True
# used to stop training
if __name__ == '__main__':
agent = DQN(WIDTH, HEIGHT, action_size, DQN_model_path, DQN_log_path)
# DQN init
paused = pause_game(paused)
# paused at the begin
emergence_break = 0
# emergence_break is used to break down training
# 用于防止出现意外紧急停止训练防止错误训练数据扰乱神经网络
for episode in range(EPISODES):
screen_gray = cv2.cvtColor(grab_screen(window_size),cv2.COLOR_BGR2GRAY)
# collect station gray graph
blood_window_gray = cv2.cvtColor(grab_screen(blood_window),cv2.COLOR_BGR2GRAY)
# collect blood gray graph for count self and boss blood
station = cv2.resize(screen_gray,(WIDTH,HEIGHT))
# change graph to WIDTH * HEIGHT for station input
boss_blood = boss_blood_count(blood_window_gray)
self_blood = self_blood_count(blood_window_gray)
# count init blood
target_step = 0
# used to update target Q network
done = 0
total_reward = 0
stop = 0
# 用于防止连续帧重复计算reward
last_time = time.time()
while True:
station = np.array(station).reshape(-1,HEIGHT,WIDTH,1)[0]
# reshape station for tf input placeholder
print('loop took {} seconds'.format(time.time()-last_time))
last_time = time.time()
target_step += 1
# get the action by state
action = agent.Choose_Action(station)
take_action(action)
# take station then the station change
screen_gray = cv2.cvtColor(grab_screen(window_size),cv2.COLOR_BGR2GRAY)
# collect station gray graph
blood_window_gray = cv2.cvtColor(grab_screen(blood_window),cv2.COLOR_BGR2GRAY)
# collect blood gray graph for count self and boss blood
next_station = cv2.resize(screen_gray,(WIDTH,HEIGHT))
next_station = np.array(next_station).reshape(-1,HEIGHT,WIDTH,1)[0]
next_boss_blood = boss_blood_count(blood_window_gray)
next_self_blood = self_blood_count(blood_window_gray)
reward, done, stop, emergence_break = action_judge(boss_blood, next_boss_blood,
self_blood, next_self_blood,
stop, emergence_break)
# get action reward
if emergence_break == 100:
# emergence break , save model and paused
# 遇到紧急情况,保存数据,并且暂停
print("emergence_break")
agent.save_model()
paused = True
agent.Store_Data(station, action, reward, next_station, done)
if len(agent.replay_buffer) > big_BATCH_SIZE:
num_step += 1
# save loss graph
# print('train')
agent.Train_Network(big_BATCH_SIZE, num_step)
if target_step % UPDATE_STEP == 0:
agent.Update_Target_Network()
# update target Q network
station = next_station
self_blood = next_self_blood
boss_blood = next_boss_blood
total_reward += reward
paused = pause_game(paused)
if done == 1:
break
if episode % 10 == 0:
agent.save_model()
# save model
print('episode: ', episode, 'Evaluation Average Reward:', total_reward/target_step)
restart()