-
Notifications
You must be signed in to change notification settings - Fork 3
/
Test.py
100 lines (78 loc) · 2.81 KB
/
Test.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
#!/usr/bin/python3
'''
Test.py
Authors: Rafael Zamora
Last Updated: 3/3/17
Notes:
-Clipping and Prioritized replay
'''
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
from RLAgent import RLAgent
from DoomScenario import DoomScenario
from Models import DQNModel, HDQNModel, StatePredictionModel, all_skills_HDQN, all_skills_shooting_HDQN
"""
This script is used to run test on trained DQN models, trained Hierarchical-DQN models,
and allow human play to test out scenarios.
"""
# Testing Parameters
scenario = 'all_skills_shooting.cfg'
model_weights = "double_dqlearn_HDQNModel_all_skills_shooting.h5"
depth_radius = 1.0
depth_contrast = 0.6
test_param = {
'frame_skips' : 4,
'nb_frames' : 3
}
nb_runs = 10
testing = 'HDQN'
def test_model(runs=1):
'''
Method used to test DQN models on VizDoom scenario. Testing run are replayed
in higher resolution (800X600).
Param:
runs - int : number of test runs done on model.
'''
# Initiates VizDoom Scenario
doom = DoomScenario(scenario)
# Load Model and Weights
model = DQNModel(resolution=doom.get_processed_state(depth_radius, depth_contrast).shape[-2:], nb_frames=test_param['nb_frames'], actions=doom.actions, depth_radius=depth_radius, depth_contrast=depth_contrast)
model.load_weights(model_weights)
agent = RLAgent(model, **test_param)
print("\nTesting DQN-Model:", model_weights)
# Run Scenario and play replay
for i in range(runs):
doom = DoomScenario(scenario)
doom.run(agent, save_replay='test.lmp', verbose=True)
doom.replay('test.lmp', doom_like=False)
def test_heirarchical_model(runs=1):
'''
Method used to test Hierarchical-DQN models on VizDoom scenario. Testing run are replayed
in higher resolution (800X600).
Param:
runs - int : number of test runs done on model.
'''
# Initiates VizDoom Scenario
doom = DoomScenario(scenario)
resolution = doom.get_processed_state(depth_radius, depth_contrast).shape[-2:]
# Load Hierarchical-DQN and Sub-models
model = all_skills_shooting_HDQN(resolution, 6, depth_radius, depth_contrast, test_param)
model.load_weights(model_weights)
agent = RLAgent(model, **test_param)
# Run Scenario and play replay using Hierarchical-DQN
print("\nTesting Hierarchical-DQN:", model_weights)
for i in range(runs):
doom = DoomScenario(scenario)
doom.run(agent, save_replay='test.lmp', verbose=True)
doom.replay('test.lmp', doom_like=False)
def play():
'''
Method used to test Vizdoom Scenarios with human players.
'''
#Initiates VizDoom Scenario and play
doom = DoomScenario(scenario)
doom.apprentice_run()
if __name__ == '__main__':
if testing == 'DQN': test_model(nb_runs)
if testing == 'HDQN': test_heirarchical_model(nb_runs)
if testing == 'human': play()