-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
222 lines (184 loc) · 7.62 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
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
from env import Environment
from model import GNN
from agent import Agent
import numpy as np
import torch
from torch_geometric.data import Batch
import os
from utils.traversal import GraphTraversal
def purely_random(path_to_map, num_episodes=50, num_steps=25):
"""
This method tests an environment in a purely random fashion.
Returns a list, whose len is num_episodes, with percentages
of visited nodes.
Parameters
----------
path_to_map : string
absolute path to the environment.
num_episodes : int
Maximum number of episodes of the test (default 50)
num_steps : int
Number of steps for every episode (default 25)
Returns
-------
pctg_vis_nodes : List
List of percentages of visited node in every one of the
`num_episodes` episodes.
"""
env = Environment(path_to_map=path_to_map)
val_nodes = len(env.valid_nodes)
vis_nodes = []
for _ in range(num_episodes):
state = env.reset()
for _ in range(num_steps):
action = np.random.choice(range(4))
next_state, reward, done, _ = env.step(action)
state = next_state.clone()
vis_nodes.append(env.get_history())
pctg_vis_nodes = [100*len(set(v))/val_nodes for v in vis_nodes]
return pctg_vis_nodes
@torch.no_grad()
def single_graph_test(path_to_map, path_to_model,
num_episodes=50, num_steps=25):
"""
This method tests an environment following the action
proposed by the network only.
Parameters
----------
path_to_map : string
absolute path to the environment.
num_episodes : int
Maximum number of episodes of the test (default 50)
num_steps : int
Number of steps for every episode (default 25)
Returns
-------
pctg_vis_nodes : List
List of percentages of visited node in every one of the
`num_episodes` episodes.
"""
test_env = Environment(path_to_map=path_to_map)
model = GNN(num_features=7, num_actions=4)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.load_state_dict(torch.load(path_to_model, map_location=device))
model.eval()
val_nodes = len(test_env.valid_nodes)
vis_nodes = []
for ep in range(num_episodes):
state = test_env.reset()
for step in range(num_steps):
state = Batch.from_data_list([state])
action = torch.argmax(model(state)).item()
next_state, reward, done, _ = test_env.step(action)
state = next_state.clone()
vis_nodes.append(test_env.get_history())
pctg_vis_nodes = [100*len(set(v))/val_nodes for v in vis_nodes]
#gt = GraphTraversal(test_env.G, test_env.pos, test_env.valid_nodes,
# test_env.obstacles, vis_nodes[best_run])
#gt.animate()
return pctg_vis_nodes
def test_random_directory(test_dir='/content/test_set',
num_episodes=50, num_steps=25):
"""
This method tests a whole `test_dir` directory going purely random.
Calculates mean and std for every file inside the `test_dir` and
calculates a final mean and std for the whole dir.
Parameters
----------
path_to_model : string
Path to the trained model (usually a .pt).
test_dir : string
Path to the directory containing the testing environments.
"""
verb = True
means, variances = [], []
test_files = sorted([filename for filename in os.listdir(test_dir) if
filename.endswith('.npy')])
print('\n| Map | Mean | Std | Best Run |\n|:------------:|:----:|:----:|:--------:|')
for fnam in test_files:
pctg_vis_nodes = purely_random(test_dir+'/'+fnam,
num_episodes=num_episodes,
num_steps=num_steps)
mean, var = np.mean(pctg_vis_nodes), np.var(pctg_vis_nodes)
# This is the environment on which I've trained my models.
# Change this if you did not do that.
if '5x5_0' not in fnam:
means.append(mean)
variances.append(var)
if verb:
nam = fnam.split('/')[-1].split('.')[0]
best_run = pctg_vis_nodes.index(max(pctg_vis_nodes))
print('| `{}` | {} | {} | {} |'.
format(nam,
round(mean,1),
round(np.sqrt(var),1),
round(pctg_vis_nodes[best_run],1)))
mean = np.mean(means)
var = np.sum(variances) / (len(variances)**2)
print('\n\tFINAL : MEAN = {} STD = {}'.
format(round(mean, 2), round(np.sqrt(var),2)))
def test_model_directory(path_to_model, test_dir='/content/test_set',
num_episodes=50, num_steps=25):
"""
This method tests a whole `test_dir` directory with the use of the
trained model. Calculates mean and std for every file inside the
`test_dir` and calculates a final mean and std for the whole dir.
Parameters
----------
path_to_model : string
Path to the trained model (usually a .pt).
test_dir : string
Path to the directory containing the testing environments.
"""
verb = True
means, variances = [], []
test_files = sorted([filename for filename in os.listdir(test_dir) if
filename.endswith('.npy')])
print('\n| Map | Mean | Std | Best Run |\n|:------------:|:----:|:----:|:--------:|')
for fnam in test_files:
pctg_vis_nodes = single_graph_test(test_dir+'/'+fnam, path_to_model,
num_episodes=num_episodes,
num_steps=num_steps)
mean, var = np.mean(pctg_vis_nodes), np.var(pctg_vis_nodes)
# This is the environment on which I've trained my models.
# Change this if you did not do that.
if '5x5_0' not in fnam:
means.append(mean)
variances.append(var)
if verb:
nam = fnam.split('/')[-1].split('.')[0]
best_run = pctg_vis_nodes.index(max(pctg_vis_nodes))
print('| `{}` | {} | {} | {} |'.
format(nam,
round(mean,1),
round(np.sqrt(var),1),
round(pctg_vis_nodes[best_run],1)))
mean = np.mean(means)
var = np.sum(variances) / (len(variances)**2)
print('\n\tFINAL : MEAN = {} STD = {}'.
format(round(mean, 2), round(np.sqrt(var),2)))
if __name__ == '__main__':
"""
This main test a `test_dir` directory, containing several environments,
in two manners: the first one going purely random, the second one
using only the trained model. If several models are present inside the
`models/` directory this test will go through everyone of them.
"""
num_episodes = 30
num_steps = 25
test_dir = 'mazes/5x5'
path_to_models = 'models/'
print('Testing {} with {} episodes of {} steps'.format(test_dir,
num_episodes,
num_steps))
print('\nRANDOM')
test_random_directory(test_dir=test_dir,
num_episodes=num_episodes, num_steps=num_steps)
print(40*'-')
models = sorted([path_to_models+filename for filename in
os.listdir(path_to_models) if filename.endswith('.pt')])
for model_fname in models:
print('\nMODEL {}'.format(model_fname))
test_model_directory(model_fname, test_dir=test_dir,
num_episodes=num_episodes, num_steps=num_steps)
print(40*'-')