-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00_benchmark_agent.py
201 lines (176 loc) · 6.53 KB
/
00_benchmark_agent.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
if __name__ == '__main__':
import warnings
warnings.filterwarnings("ignore")
import argparse
import time
import sys
sys.path.append('./carla_RL_IAs/PythonAPI/carla/dist/carla-0.9.6-py3.5-linux-x86_64.egg')
import torchvision.models as models
from pathlib import Path
from benchmark import make_suite, get_suites, ALL_SUITES
from benchmark.run_rnd_benchmark import run_benchmark
import torch
import random
import os
import carla
import numpy as np
from datetime import datetime
result_folder = datetime.now().strftime("%m_%d_%Y_%H_%M_%S")
os.mkdir('./results'+result_folder+'/')
def run(args, model_path, port, suite, seed, resume, max_run, replay):
log_dir = model_path
total_time = 0.0
for suite_name in get_suites(suite):
tick = time.time()
benchmark_dir = log_dir / "benchmark" / ("%s_seed%d" % (suite_name, seed))
benchmark_dir.mkdir(parents=True, exist_ok=True)
with make_suite(suite_name, port=port, crop_sky=args.crop_sky) as env:
from bird_view.models import agent_IAs_RL
agent_class = agent_IAs_RL.AgentIAsRL
agent_maker = lambda: agent_class(args)
if args.replay:
print('recording')
env._client.start_recorder(args.path_videos)
run_benchmark(agent_maker, env, benchmark_dir, seed, resume, result_folder, max_run=max_run, replay=replay)
if args.replay:
env._client.stop_recorder()
elapsed = time.time() - tick
total_time += elapsed
print("%s: %.3f hours." % (suite_name, elapsed / 3600))
break
print("Total time: %.3f hours." % (total_time / 3600))
def plotting(args, model_path, port, suite, seed, resume, max_run, replay,):
def set_sync_mode(client, sync):
world = client.get_world()
settings = world.get_settings()
settings.synchronous_mode = sync
settings.fixed_delta_seconds = 0.1
world.apply_settings(settings)
total_time = 0.0
for suite_name in get_suites(suite):
tick = time.time()
client = carla.Client("localhost", port)
client.set_timeout(30.0)
set_sync_mode(client, False)
client.set_replayer_time_factor(1)
client.replay_file(args.path_videos, 0, 0, 1920)
elapsed = time.time() - tick
total_time += elapsed
print("%s: %.3f hours." % (suite_name, elapsed / 3600))
break
print("Total time: %.3f hours." % (total_time / 3600))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--path-folder-model",
required=True,
type=str,
help="Folder containing all models, ie the supervised Resnet18 and the RL models",
)
parser.add_argument("--port", type=int, default=2000)
parser.add_argument("--suite", choices=ALL_SUITES, default="town1")
parser.add_argument("--seed", type=int, default=2021)
parser.add_argument("--resume", action="store_true")
parser.add_argument("--replay", action="store_true")
parser.add_argument("--max-run", type=int, default=100)
parser.add_argument("--guide", action="store_true")
parser.add_argument("--plotting", action="store_true")
parser.add_argument("--path_videos", type=str, default="./")
parser.add_argument(
"--nb_action_steering",
type=int,
default=27,
help="How much different steering values in the action (should be odd)",
)
parser.add_argument(
"--max_steering", type=float, default=0.6, help="Max steering value possible in action"
)
parser.add_argument(
"--nb_action_throttle",
type=int,
default=3,
help="How much different throttle values in the action",
)
parser.add_argument(
"--max_throttle", type=float, default=1, help="Max throttle value possible in action"
)
parser.add_argument("--front-camera-width", type=int, default=288)
parser.add_argument("--front-camera-height", type=int, default=288)
parser.add_argument("--front-camera-fov", type=int, default=100)
parser.add_argument(
"--crop-sky",
action="store_true",
default=False,
help="if using CARLA challenge model, let sky, we cropped "
"it for the models trained only on Town01/train weather",
)
parser.add_argument("--render", action="store_true", help="Display screen (testing only)")
parser.add_argument("--disable-cuda", action="store_true", help="disable cuda")
parser.add_argument("--disable-cudnn", action="store_true", help="disable cuDNN")
parser.add_argument("--kappa", default=1.0, type=float, help="kappa for Huber Loss in IQN")
parser.add_argument(
"--num-tau-samples", default=8, type=int, help="N in equation 3 in IQN paper"
)
parser.add_argument(
"--num-tau-prime-samples", default=8, type=int, help="N' in equation 3 in IQN paper"
)
parser.add_argument(
"--num-quantile-samples", default=32, type=int, help="K in equation 3 in IQN paper"
)
parser.add_argument(
"--quantile-embedding-dim", default=64, type=int, help="n in equation 4 in IQN paper"
)
args = parser.parse_args()
args.steps_image = [
-10,
-2,
-1,
0,
]
# np.random.seed(2021)
# random.seed(2021)
if torch.cuda.is_available() and not args.disable_cuda:
args.device = torch.device("cuda")
torch.cuda.manual_seed(2021)
torch.backends.cudnn.enabled = not args.disable_cudnn
else:
args.device = torch.device("cpu")
args.path_folder_model = os.path.join(
os.path.dirname(os.path.realpath(__file__)), args.path_folder_model
)
if args.replay:
run(
args,
Path(args.path_folder_model),
args.port,
args.suite,
args.seed,
args.resume,
max_run=args.max_run,
replay=args.replay,
)
elif args.plotting:
plotting(
args,
Path(args.path_folder_model),
args.port,
args.suite,
args.seed,
args.resume,
max_run=args.max_run,
replay=args.replay,
)
else:
f = open('./results'+result_folder+'/cure_timing'+str(args.seed)+'.txt', 'w', buffering=1)
sys.stdout = f
run(
args,
Path(args.path_folder_model),
args.port,
args.suite,
args.seed,
args.resume,
max_run=args.max_run,
replay=args.replay,
)
f.close()