2020-06-05 09:57:25 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import os
|
|
|
|
import sys
|
2021-12-16 08:12:24 +08:00
|
|
|
from collections import defaultdict
|
|
|
|
from typing import Any
|
2020-10-10 07:25:21 +08:00
|
|
|
|
2024-03-20 03:39:09 +08:00
|
|
|
from openpilot.common.git import get_commit
|
2023-08-21 11:49:55 +08:00
|
|
|
from openpilot.system.hardware import PC
|
2024-01-17 02:58:51 +08:00
|
|
|
from openpilot.tools.lib.openpilotci import BASE_URL, get_url
|
2023-10-05 13:59:03 +08:00
|
|
|
from openpilot.selfdrive.test.process_replay.compare_logs import compare_logs, format_diff
|
2023-08-21 11:49:55 +08:00
|
|
|
from openpilot.selfdrive.test.process_replay.process_replay import get_process_config, replay_process
|
|
|
|
from openpilot.tools.lib.framereader import FrameReader
|
|
|
|
from openpilot.tools.lib.logreader import LogReader
|
|
|
|
from openpilot.tools.lib.helpers import save_log
|
2020-06-05 09:57:25 +08:00
|
|
|
|
2022-12-08 11:36:30 +08:00
|
|
|
TEST_ROUTE = "2f4452b03ccb98f0|2022-12-03--13-45-30"
|
|
|
|
SEGMENT = 6
|
2022-10-21 11:58:58 +08:00
|
|
|
MAX_FRAMES = 100 if PC else 600
|
2020-06-05 09:57:25 +08:00
|
|
|
|
2023-11-03 09:10:11 +08:00
|
|
|
NO_MODEL = "NO_MODEL" in os.environ
|
2023-06-23 08:36:25 +08:00
|
|
|
SEND_EXTRA_INPUTS = bool(int(os.getenv("SEND_EXTRA_INPUTS", "0")))
|
2021-11-25 07:59:19 +08:00
|
|
|
|
2022-12-08 11:36:30 +08:00
|
|
|
|
2022-06-04 01:43:09 +08:00
|
|
|
def get_log_fn(ref_commit, test_route):
|
|
|
|
return f"{test_route}_model_tici_{ref_commit}.bz2"
|
2021-03-05 18:03:23 +08:00
|
|
|
|
2021-12-16 08:12:24 +08:00
|
|
|
|
2023-06-23 08:36:25 +08:00
|
|
|
def trim_logs_to_max_frames(logs, max_frames, frs_types, include_all_types):
|
|
|
|
all_msgs = []
|
|
|
|
cam_state_counts = defaultdict(int)
|
|
|
|
# keep adding messages until cam states are equal to MAX_FRAMES
|
|
|
|
for msg in sorted(logs, key=lambda m: m.logMonoTime):
|
|
|
|
all_msgs.append(msg)
|
|
|
|
if msg.which() in frs_types:
|
|
|
|
cam_state_counts[msg.which()] += 1
|
|
|
|
|
|
|
|
if all(cam_state_counts[state] == max_frames for state in frs_types):
|
|
|
|
break
|
|
|
|
|
|
|
|
if len(include_all_types) != 0:
|
|
|
|
other_msgs = [m for m in logs if m.which() in include_all_types]
|
|
|
|
all_msgs.extend(other_msgs)
|
|
|
|
|
|
|
|
return all_msgs
|
2020-12-09 11:01:59 +08:00
|
|
|
|
2021-12-16 08:12:24 +08:00
|
|
|
|
|
|
|
def model_replay(lr, frs):
|
2023-06-23 08:36:25 +08:00
|
|
|
# modeld is using frame pairs
|
2024-01-22 04:09:48 +08:00
|
|
|
modeld_logs = trim_logs_to_max_frames(lr, MAX_FRAMES, {"roadCameraState", "wideRoadCameraState"}, {"roadEncodeIdx", "wideRoadEncodeIdx", "carParams"})
|
|
|
|
dmodeld_logs = trim_logs_to_max_frames(lr, MAX_FRAMES, {"driverCameraState"}, {"driverEncodeIdx", "carParams"})
|
2024-03-11 07:39:40 +08:00
|
|
|
|
2023-06-23 08:36:25 +08:00
|
|
|
if not SEND_EXTRA_INPUTS:
|
2024-03-11 07:39:40 +08:00
|
|
|
modeld_logs = [msg for msg in modeld_logs if msg.which() != 'liveCalibration']
|
|
|
|
dmodeld_logs = [msg for msg in dmodeld_logs if msg.which() != 'liveCalibration']
|
|
|
|
|
|
|
|
# initial setup
|
|
|
|
for s in ('liveCalibration', 'deviceState'):
|
|
|
|
msg = next(msg for msg in lr if msg.which() == s).as_builder()
|
|
|
|
msg.logMonoTime = lr[0].logMonoTime
|
|
|
|
modeld_logs.insert(1, msg.as_reader())
|
|
|
|
dmodeld_logs.insert(1, msg.as_reader())
|
2023-06-23 08:36:25 +08:00
|
|
|
|
|
|
|
modeld = get_process_config("modeld")
|
|
|
|
dmonitoringmodeld = get_process_config("dmonitoringmodeld")
|
2020-06-05 09:57:25 +08:00
|
|
|
|
2023-09-23 08:10:36 +08:00
|
|
|
modeld_msgs = replay_process(modeld, modeld_logs, frs)
|
|
|
|
dmonitoringmodeld_msgs = replay_process(dmonitoringmodeld, dmodeld_logs, frs)
|
|
|
|
return modeld_msgs + dmonitoringmodeld_msgs
|
2020-06-05 09:57:25 +08:00
|
|
|
|
2021-12-16 08:12:24 +08:00
|
|
|
|
2020-06-05 09:57:25 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
update = "--update" in sys.argv
|
2020-12-09 11:01:59 +08:00
|
|
|
replay_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
ref_commit_fn = os.path.join(replay_dir, "model_replay_ref_commit")
|
|
|
|
|
2021-12-16 08:12:24 +08:00
|
|
|
# load logs
|
|
|
|
lr = list(LogReader(get_url(TEST_ROUTE, SEGMENT)))
|
|
|
|
frs = {
|
2022-10-21 11:58:58 +08:00
|
|
|
'roadCameraState': FrameReader(get_url(TEST_ROUTE, SEGMENT, log_type="fcamera"), readahead=True),
|
|
|
|
'driverCameraState': FrameReader(get_url(TEST_ROUTE, SEGMENT, log_type="dcamera"), readahead=True),
|
|
|
|
'wideRoadCameraState': FrameReader(get_url(TEST_ROUTE, SEGMENT, log_type="ecamera"), readahead=True)
|
2021-12-16 08:12:24 +08:00
|
|
|
}
|
2020-06-05 09:57:25 +08:00
|
|
|
|
2023-05-31 11:33:01 +08:00
|
|
|
# Update tile refs
|
|
|
|
if update:
|
|
|
|
import urllib
|
|
|
|
import requests
|
|
|
|
import threading
|
|
|
|
import http.server
|
2024-01-17 02:58:51 +08:00
|
|
|
from openpilot.tools.lib.openpilotci import upload_bytes
|
2023-05-31 11:33:01 +08:00
|
|
|
os.environ['MAPS_HOST'] = 'http://localhost:5000'
|
|
|
|
|
|
|
|
class HTTPRequestHandler(http.server.BaseHTTPRequestHandler):
|
|
|
|
def do_GET(self):
|
|
|
|
assert len(self.path) > 10 # Sanity check on path length
|
|
|
|
r = requests.get(f'https://api.mapbox.com{self.path}', timeout=30)
|
|
|
|
upload_bytes(r.content, urllib.parse.urlparse(self.path).path.lstrip('/'))
|
|
|
|
self.send_response(r.status_code)
|
|
|
|
self.send_header('Content-type','text/html')
|
|
|
|
self.end_headers()
|
|
|
|
self.wfile.write(r.content)
|
|
|
|
|
|
|
|
server = http.server.HTTPServer(("127.0.0.1", 5000), HTTPRequestHandler)
|
|
|
|
thread = threading.Thread(None, server.serve_forever, daemon=True)
|
|
|
|
thread.start()
|
|
|
|
else:
|
|
|
|
os.environ['MAPS_HOST'] = BASE_URL.rstrip('/')
|
|
|
|
|
2023-11-03 09:10:11 +08:00
|
|
|
log_msgs = []
|
2022-12-08 11:36:30 +08:00
|
|
|
# run replays
|
2023-11-03 09:10:11 +08:00
|
|
|
if not NO_MODEL:
|
|
|
|
log_msgs += model_replay(lr, frs)
|
2020-06-05 09:57:25 +08:00
|
|
|
|
2021-12-16 08:12:24 +08:00
|
|
|
# get diff
|
2020-11-20 08:47:14 +08:00
|
|
|
failed = False
|
|
|
|
if not update:
|
2021-12-16 08:12:24 +08:00
|
|
|
with open(ref_commit_fn) as f:
|
|
|
|
ref_commit = f.read().strip()
|
2022-06-04 01:43:09 +08:00
|
|
|
log_fn = get_log_fn(ref_commit, TEST_ROUTE)
|
2022-02-20 08:06:31 +08:00
|
|
|
try:
|
2023-06-23 08:36:25 +08:00
|
|
|
all_logs = list(LogReader(BASE_URL + log_fn))
|
|
|
|
cmp_log = []
|
|
|
|
|
2024-04-10 01:40:38 +08:00
|
|
|
# logs are ordered based on type: modelV2, driverStateV2
|
2023-11-03 09:10:11 +08:00
|
|
|
if not NO_MODEL:
|
|
|
|
model_start_index = next(i for i, m in enumerate(all_logs) if m.which() in ("modelV2", "cameraOdometry"))
|
|
|
|
cmp_log += all_logs[model_start_index:model_start_index + MAX_FRAMES*2]
|
|
|
|
dmon_start_index = next(i for i, m in enumerate(all_logs) if m.which() == "driverStateV2")
|
|
|
|
cmp_log += all_logs[dmon_start_index:dmon_start_index + MAX_FRAMES]
|
2022-02-20 08:06:31 +08:00
|
|
|
|
|
|
|
ignore = [
|
|
|
|
'logMonoTime',
|
|
|
|
'modelV2.frameDropPerc',
|
|
|
|
'modelV2.modelExecutionTime',
|
2022-06-21 07:24:51 +08:00
|
|
|
'driverStateV2.modelExecutionTime',
|
2024-04-10 01:40:38 +08:00
|
|
|
'driverStateV2.dspExecutionTime'
|
2022-02-20 08:06:31 +08:00
|
|
|
]
|
2022-12-08 11:36:30 +08:00
|
|
|
if PC:
|
|
|
|
ignore += [
|
|
|
|
'modelV2.laneLines.0.t',
|
|
|
|
'modelV2.laneLines.1.t',
|
|
|
|
'modelV2.laneLines.2.t',
|
|
|
|
'modelV2.laneLines.3.t',
|
|
|
|
'modelV2.roadEdges.0.t',
|
|
|
|
'modelV2.roadEdges.1.t',
|
|
|
|
]
|
2022-08-31 02:20:55 +08:00
|
|
|
# TODO this tolerance is absurdly large
|
2022-10-06 12:43:38 +08:00
|
|
|
tolerance = 2.0 if PC else None
|
2022-02-20 08:06:31 +08:00
|
|
|
results: Any = {TEST_ROUTE: {}}
|
2022-08-16 15:32:42 +08:00
|
|
|
log_paths: Any = {TEST_ROUTE: {"models": {'ref': BASE_URL + log_fn, 'new': log_fn}}}
|
2022-02-20 08:06:31 +08:00
|
|
|
results[TEST_ROUTE]["models"] = compare_logs(cmp_log, log_msgs, tolerance=tolerance, ignore_fields=ignore)
|
2023-10-17 08:04:14 +08:00
|
|
|
diff_short, diff_long, failed = format_diff(results, log_paths, ref_commit)
|
2022-02-20 08:06:31 +08:00
|
|
|
|
2024-03-11 07:39:40 +08:00
|
|
|
if "CI" in os.environ:
|
|
|
|
print(diff_long)
|
2022-02-20 08:06:31 +08:00
|
|
|
print('-------------\n'*5)
|
2023-10-17 08:04:14 +08:00
|
|
|
print(diff_short)
|
2022-02-20 08:06:31 +08:00
|
|
|
with open("model_diff.txt", "w") as f:
|
2023-10-17 08:04:14 +08:00
|
|
|
f.write(diff_long)
|
2022-02-20 08:06:31 +08:00
|
|
|
except Exception as e:
|
|
|
|
print(str(e))
|
|
|
|
failed = True
|
2020-06-05 09:57:25 +08:00
|
|
|
|
2021-12-16 08:12:24 +08:00
|
|
|
# upload new refs
|
2022-06-04 01:43:09 +08:00
|
|
|
if (update or failed) and not PC:
|
2024-01-17 02:58:51 +08:00
|
|
|
from openpilot.tools.lib.openpilotci import upload_file
|
2020-11-20 08:47:14 +08:00
|
|
|
|
|
|
|
print("Uploading new refs")
|
|
|
|
|
2021-11-30 02:38:55 +08:00
|
|
|
new_commit = get_commit()
|
2022-06-04 01:43:09 +08:00
|
|
|
log_fn = get_log_fn(new_commit, TEST_ROUTE)
|
2020-11-20 08:47:14 +08:00
|
|
|
save_log(log_fn, log_msgs)
|
2020-12-09 11:01:59 +08:00
|
|
|
try:
|
|
|
|
upload_file(log_fn, os.path.basename(log_fn))
|
|
|
|
except Exception as e:
|
|
|
|
print("failed to upload", e)
|
|
|
|
|
|
|
|
with open(ref_commit_fn, 'w') as f:
|
|
|
|
f.write(str(new_commit))
|
2020-11-20 08:47:14 +08:00
|
|
|
|
|
|
|
print("\n\nNew ref commit: ", new_commit)
|
|
|
|
|
|
|
|
sys.exit(int(failed))
|