diff --git a/.gitignore b/.gitignore index 765ba3bfc..1af8ad5c1 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,7 @@ /documentation/**/**.gif /documentation/source/img.png /documentation/source/demo.png + +# ignore documentation build output +**/filtered_dataset +**/semantics.png diff --git a/documentation/source/debug_mode.ipynb b/documentation/source/debug_mode.ipynb index b812c4ba1..d5e6f8d05 100644 --- a/documentation/source/debug_mode.ipynb +++ b/documentation/source/debug_mode.ipynb @@ -140,17 +140,6 @@ "In addition to the errors raised from MetaDrive, sometimes the game engine, Panda3D, will throw errors and warnings about the rendering service. To enable the logging of Panda3D, set `env_config[\"debug_panda3d\"]=True`. Besides, you can turn on Panda3D's profiler via `env_config[\"pstats\"]=True` and launch the `pstats` in the terminal. It can be used to analyze your program in terms of the time consumed for different functions like rendering, physics and so on, which is very useful if you are developing some graphics related features." ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "7b3e2ecb", - "metadata": {}, - "outputs": [], - "source": [ - "# launch pstats (bash)\n", - "!pstats" - ] - }, { "cell_type": "code", "execution_count": null, diff --git a/documentation/source/obs.ipynb b/documentation/source/obs.ipynb index 26ba4f276..d4792c4cd 100644 --- a/documentation/source/obs.ipynb +++ b/documentation/source/obs.ipynb @@ -4,7 +4,6 @@ "cell_type": "markdown", "id": "72c167e8", "metadata": { - "editable": true, "slideshow": { "slide_type": "" }, @@ -415,7 +414,6 @@ "execution_count": 4, "id": "ff7a70aa", "metadata": { - "editable": true, "slideshow": { "slide_type": "" }, @@ -544,7 +542,6 @@ "execution_count": 45, "id": "3562290f", "metadata": { - "editable": true, "slideshow": { "slide_type": "" }, @@ -592,7 +589,6 @@ "execution_count": 12, "id": "995d5314-92a7-4e68-8bb8-05f1bd8ab718", "metadata": { - "editable": true, "slideshow": { "slide_type": "" }, @@ -619,7 +615,6 @@ "execution_count": 13, "id": "7f20c293-77f9-451c-a552-882def3d6257", "metadata": { - "editable": true, "slideshow": { "slide_type": "" }, @@ -712,7 +707,6 @@ "execution_count": 10, "id": "9ea9966f-f123-40e8-a432-1ac19f396431", "metadata": { - "editable": true, "slideshow": { "slide_type": "" }, @@ -806,13 +800,16 @@ "from metadrive.envs.metadrive_env import MetaDriveEnv\n", "from metadrive.obs.state_obs import LidarStateObservation\n", "from metadrive.component.sensors.rgb_camera import RGBCamera\n", + "import os\n", + "test_doc = os.getenv('TEST_DOC')\n", + "sensor_size = (84, 60) if test_doc else (200, 100)\n", "\n", "env = MetaDriveEnv(config=dict(\n", " use_render=False,\n", " agent_observation=LidarStateObservation,\n", " image_observation=True,\n", " norm_pixel=False,\n", - " sensors=dict(rgb_camera=(RGBCamera, 512, 256)),\n", + " sensors=dict(rgb_camera=(RGBCamera, *sensor_size)),\n", "))\n", "\n", "obs, info = env.reset()\n", @@ -822,9 +819,10 @@ "image = env.engine.get_sensor(\"rgb_camera\").perceive(to_float=False)\n", "image = image[..., [2, 1, 0]]\n", "\n", - "import matplotlib.pyplot as plt\n", - "plt.imshow(image)\n", - "plt.show()" + "if not test_doc:\n", + " import matplotlib.pyplot as plt\n", + " plt.imshow(image)\n", + " plt.show()" ] } ], @@ -844,7 +842,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.13" + "version": "3.7.13" }, "mystnb": { "execution_mode": "force" diff --git a/metadrive/component/vehicle/base_vehicle.py b/metadrive/component/vehicle/base_vehicle.py index cb4494d61..f6890b89a 100644 --- a/metadrive/component/vehicle/base_vehicle.py +++ b/metadrive/component/vehicle/base_vehicle.py @@ -232,13 +232,37 @@ def before_step(self, action=None): return step_info def after_step(self): + step_info = {} if self.navigation and self.config["navigation_module"]: self.navigation.update_localization(self) + lanes_heading = self.navigation.navi_arrow_dir + lane_0_heading = lanes_heading[0] + lane_1_heading = lanes_heading[1] + navigation_straight = False + navigation_turn_left = False + navigation_turn_right = False + if abs(wrap_to_pi(lane_0_heading - lane_1_heading)) < 10 / 180 * math.pi: + navigation_straight = True + else: + dir_0 = np.array([math.cos(lane_0_heading), math.sin(lane_0_heading), 0]) + dir_1 = np.array([math.cos(lane_1_heading), math.sin(lane_1_heading), 0]) + cross_product = np.cross(dir_1, dir_0) + navigation_turn_left = True if cross_product[-1] < 0 else False + navigation_turn_right = not navigation_turn_left + step_info.update( + { + "navigation_command": "forward" if navigation_straight else + ("left" if navigation_turn_left else "right"), + "navigation_forward": navigation_straight, + "navigation_left": navigation_turn_left, + "navigation_right": navigation_turn_right + } + ) self._state_check() self.update_dist_to_left_right() step_energy, episode_energy = self._update_energy_consumption() self.out_of_route = self._out_of_route() - step_info = self._update_overtake_stat() + step_info.update(self._update_overtake_stat()) my_policy = self.engine.get_policy(self.name) step_info.update( { @@ -250,30 +274,6 @@ def after_step(self): "policy": my_policy.name if my_policy is not None else my_policy } ) - - lanes_heading = self.navigation.navi_arrow_dir - lane_0_heading = lanes_heading[0] - lane_1_heading = lanes_heading[1] - navigation_straight = False - navigation_turn_left = False - navigation_turn_right = False - if abs(wrap_to_pi(lane_0_heading - lane_1_heading)) < 10 / 180 * math.pi: - navigation_straight = True - else: - dir_0 = np.array([math.cos(lane_0_heading), math.sin(lane_0_heading), 0]) - dir_1 = np.array([math.cos(lane_1_heading), math.sin(lane_1_heading), 0]) - cross_product = np.cross(dir_1, dir_0) - navigation_turn_left = True if cross_product[-1] < 0 else False - navigation_turn_right = not navigation_turn_left - step_info.update( - { - "navigation_command": "forward" if navigation_straight else - ("left" if navigation_turn_left else "right"), - "navigation_forward": navigation_straight, - "navigation_left": navigation_turn_left, - "navigation_right": navigation_turn_right - } - ) return step_info def _out_of_route(self): diff --git a/metadrive/engine/core/engine_core.py b/metadrive/engine/core/engine_core.py index 4ba39f498..cdd529939 100644 --- a/metadrive/engine/core/engine_core.py +++ b/metadrive/engine/core/engine_core.py @@ -30,6 +30,8 @@ from metadrive.engine.logger import get_logger from metadrive.utils.utils import is_mac, setup_logger import logging +import subprocess +from metadrive.utils.utils import is_port_occupied logger = get_logger() @@ -121,9 +123,21 @@ def __init__(self, global_config): self.pid = os.getpid() EngineCore.global_config = global_config self.mode = global_config["_render_mode"] + self.pstats_process = None if self.global_config["pstats"]: # pstats debug provided by panda3d loadPrcFileData("", "want-pstats 1") + if not is_port_occupied(5185): + self.pstats_process = subprocess.Popen(['pstats']) + logger.info( + "pstats is launched successfully, tutorial is at: " + "https://docs.panda3d.org/1.10/python/optimization/using-pstats" + ) + else: + logger.warning( + "pstats is already launched! tutorial is at: " + "https://docs.panda3d.org/1.10/python/optimization/using-pstats" + ) # Setup onscreen render if self.global_config["use_render"]: diff --git a/metadrive/tests/test_component/test_lane_line_detector.py b/metadrive/tests/test_component/test_lane_line_detector.py index c6b8f1ad1..09d0211de 100644 --- a/metadrive/tests/test_component/test_lane_line_detector.py +++ b/metadrive/tests/test_component/test_lane_line_detector.py @@ -444,112 +444,112 @@ def test_pg_map(render=False): nuscenes_gt_1 = [ - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 0.5260612368583679, + 0.09075836837291718, + 0.09217655658721924, + 0.09512815624475479, + 0.09983516484498978, + 0.10680326074361801, + 0.11676304042339325, + 0.13101108372211456, + 0.15133066475391388, + 0.1828141212463379, + 0.23759107291698456, + 0.3464219272136688, + 0.5152920484542847, + 1.0, + 1.0, + 0.7216349840164185, + 0.4785699248313904, + 0.3620630204677582, + 0.2945554852485657, + 0.2518192529678345, + 0.22320537269115448, + 0.20315533876419067, + 0.18915221095085144, + 0.1796053946018219, + 0.17358174920082092, + 0.1705060601234436, + 0.17026464641094208, + 0.17269740998744965, + 0.17804943025112152, + 0.1867835521697998, + 0.1997312605381012, + 0.21826286613941193, + 0.24478337168693542, + 0.28362220525741577, + 0.3439479172229767, + 0.44482824206352234, + 1.0, + 0.2166164666414261, + 0.3294956684112549, + 0.5083940625190735, + 0.6360813975334167, + 0.2537195086479187, + 0.1899668127298355, + 0.1564241647720337, + 0.13358616828918457, + 0.11820273101329803, + 0.10741100460290909, + 0.10005706548690796, + 0.09534399211406708, + 0.09228239208459854, + 0.0907929316163063, + 0.5260613560676575, 0.4140831232070923, 0.5, 0.5, 0.5, - 7.98985729488777e-06, + 5.327035069058184e-06, 0.09075836837291718, - 0.09217660874128342, - 0.09512805193662643, - 0.09983530640602112, - 0.10680267214775085, - 0.1167638823390007, - 0.1310119777917862, - 0.15133075416088104, - 0.18281413614749908, - 0.23800653219223022, - 0.34642189741134644, - 0.515292227268219, - 1.0, - 1.0, - 0.13423746824264526, - 0.08499415963888168, - 0.06419597566127777, - 0.17124240100383759, - 0.2518191933631897, - 0.1298043578863144, - 0.11819353699684143, - 0.18915213644504547, - 0.031741853803396225, - 0.0306671354919672, - 0.030122023075819016, - 0.030061837285757065, - 0.03048192895948887, - 0.03141561895608902, - 0.03294506296515465, - 0.03521399199962616, - 0.03834167867898941, - 0.2447834461927414, - 0.2836225926876068, - 0.3439478874206543, - 0.2683734595775604, - 1.0, - 0.21661706268787384, - 0.3294958174228668, - 0.508394181728363, - 0.6357858180999756, - 0.25371864438056946, - 0.18996702134609222, - 0.15642258524894714, - 0.13358451426029205, - 0.11820243299007416, - 0.1074109822511673, + 0.09217655658721924, + 0.09512815624475479, + 0.09983516484498978, + 0.10680326074361801, + 0.11676304042339325, + 0.13101108372211456, + 0.15133066475391388, + 0.1828141212463379, + 0.23759107291698456, + 0.3464219272136688, + 0.5152920484542847, + 1.0, + 1.0, + 0.13423743844032288, + 0.08499369770288467, + 0.0641968622803688, + 0.17124241590499878, + 0.2518192529678345, + 0.12980437278747559, + 0.11819363385438919, + 0.18915221095085144, + 0.03174185752868652, + 0.03066714107990265, + 0.03012210875749588, + 0.030061468482017517, + 0.030481763184070587, + 0.03141583874821663, + 0.032944608479738235, + 0.03521450608968735, + 0.03834288939833641, + 0.24478337168693542, + 0.28362220525741577, + 0.3439479172229767, + 0.26837339997291565, + 1.0, + 0.2166164666414261, + 0.3294956684112549, + 0.5083940625190735, + 0.6360813975334167, + 0.2537195086479187, + 0.1899668127298355, + 0.1564241647720337, + 0.13358616828918457, + 0.11820273101329803, + 0.10741100460290909, 0.10005706548690796, - 0.09534407407045364, - 0.09228227287530899, - 0.09079291671514511, + 0.09534399211406708, + 0.09228239208459854, + 0.0907929316163063, 0.5623959898948669, 0.5623959898948669, 0.5956567525863647, @@ -569,7 +569,7 @@ def test_pg_map(render=False): 0.8258141875267029, 0.8258141875267029, 0.5182746052742004, - 0.5082992911338806, + 0.5082993507385254, 0.0, 0.0, 1.0, @@ -585,111 +585,111 @@ def test_pg_map(render=False): ] nuscenes_gt_2 = [ 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 1.0, - 0.39886173605918884, - 0.3929330110549927, - 0.5, - 0.5, - 0.5, - 0.00012117374717490748, - 1.0, - 0.7808146476745605, - 0.4016971290111542, - 0.4970487654209137, - 0.21977421641349792, + 0.7808130979537964, + 0.4014144241809845, + 0.497048556804657, + 0.21977420151233673, + 0.1833307445049286, + 0.2741309106349945, + 0.24397589266300201, + 0.22296150028705597, + 0.20816059410572052, + 0.19839195907115936, + 0.19227449595928192, + 0.18942095339298248, + 0.1900409460067749, + 0.19369441270828247, + 0.20063060522079468, + 0.1172584742307663, + 0.12590330839157104, + 0.13827820122241974, + 0.15608078241348267, + 0.18244224786758423, + 0.2237718254327774, + 0.2953447997570038, + 0.4444361925125122, + 0.9753275513648987, + 0.11675211787223816, + 0.04168549180030823, + 0.038301981985569, + 0.035907551646232605, + 0.034303370863199234, + 0.033339571207761765, + 0.03293364867568016, + 0.03305203467607498, + 0.035654980689287186, + 0.0451875701546669, + 0.06304079294204712, + 1.0, + 0.9279412627220154, + 0.47582244873046875, + 0.3268144130706787, + 0.25280630588531494, + 0.20891083776950836, + 0.18038643896579742, + 0.16094817221164703, + 0.1473899632692337, + 0.1379445493221283, + 0.1312660276889801, + 0.1269902139902115, + 0.12537911534309387, + 0.1267208307981491, + 0.3988616168498993, + 0.39293304085731506, + 0.5, + 0.5, + 0.5, + 0.00011851061572087929, + 1.0, + 0.7808130979537964, + 0.4014144241809845, + 0.497048556804657, + 0.21977420151233673, 0.1833307445049286, - 0.2741360664367676, - 0.24397584795951843, - 0.22295309603214264, - 0.20816056430339813, - 0.1983921080827713, - 0.19227470457553864, + 0.2741309106349945, + 0.24397589266300201, + 0.22296150028705597, + 0.20816059410572052, + 0.19839195907115936, + 0.19227449595928192, 0.18942095339298248, - 0.19004100561141968, - 0.19369380176067352, - 0.20063059031963348, - 0.11725883930921555, - 0.1259033977985382, - 0.13827748596668243, - 0.15607935190200806, - 0.18244585394859314, - 0.22377735376358032, - 0.2953443229198456, - 0.44443684816360474, - 0.9753272533416748, - 0.11670814454555511, - 0.041685495525598526, - 0.038300298154354095, - 0.03590720146894455, - 0.034303367137908936, - 0.03333956375718117, - 0.03293337672948837, - 0.03305184096097946, - 0.03565486893057823, - 0.045187562704086304, - 0.06304076313972473, - 1.0, - 0.927940845489502, - 0.47582200169563293, - 0.32681402564048767, - 0.252807080745697, - 0.20891286432743073, - 0.1803870052099228, - 0.1609482318162918, - 0.14739003777503967, - 0.13794484734535217, - 0.1312611699104309, - 0.12699010968208313, - 0.12537787854671478, - 0.1267206221818924, + 0.1900409460067749, + 0.19369441270828247, + 0.20063060522079468, + 0.1172584742307663, + 0.12590330839157104, + 0.13827820122241974, + 0.15608078241348267, + 0.18244224786758423, + 0.2237718254327774, + 0.2953447997570038, + 0.4444361925125122, + 0.9753275513648987, + 0.11675211787223816, + 0.04168549180030823, + 0.038301981985569, + 0.035907551646232605, + 0.034303370863199234, + 0.033339571207761765, + 0.03293364867568016, + 0.03305203467607498, + 0.035654980689287186, + 0.0451875701546669, + 0.06304079294204712, + 1.0, + 0.9279412627220154, + 0.47582244873046875, + 0.3268144130706787, + 0.25280630588531494, + 0.20891083776950836, + 0.18038643896579742, + 0.16094817221164703, + 0.1473899632692337, + 0.1379445493221283, + 0.1312660276889801, + 0.1269902139902115, + 0.12537911534309387, + 0.1267208307981491, 0.4396708011627197, 0.4396708011627197, 0.4618304371833801, @@ -709,7 +709,7 @@ def test_pg_map(render=False): 0.6053164601325989, 0.6053164601325989, 0.0, - 0.4675830006599426, + 0.46758297085762024, 0.0, 0.0, 1.0, diff --git a/metadrive/tests/test_functionality/test_memory_leak_engine.py b/metadrive/tests/test_functionality/test_memory_leak_engine.py index 7293a440c..0bd743608 100644 --- a/metadrive/tests/test_functionality/test_memory_leak_engine.py +++ b/metadrive/tests/test_functionality/test_memory_leak_engine.py @@ -86,7 +86,7 @@ def test_engine_memory_leak(): ct = time.time() last_lm = cm = process_memory() last_mem = 0.0 - for t in range(500): + for t in range(300): lt = time.time() engine.seed(0) @@ -102,6 +102,7 @@ def test_engine_memory_leak(): # ) last_lm = lm if t > 100: + time.sleep(0.1) assert abs((lm - cm) - last_mem) < 10 # Memory should not have change > 1KB last_mem = lm - cm finally: @@ -113,7 +114,7 @@ def test_config_memory_leak(): ct = time.time() last_lm = cm = process_memory() last_mem = 0.0 - for t in range(1000): + for t in range(800): lt = time.time() default_config = MetaDriveEnv.default_config() @@ -129,6 +130,7 @@ def test_config_memory_leak(): # ) last_lm = lm if t > 500: + time.sleep(0.1) assert abs((lm - cm) - last_mem) < 10 # Memory should not have change > 1KB last_mem = lm - cm diff --git a/metadrive/utils/utils.py b/metadrive/utils/utils.py index f53d15a0c..2b58aba0a 100644 --- a/metadrive/utils/utils.py +++ b/metadrive/utils/utils.py @@ -4,13 +4,26 @@ import os import sys import time - +import socket import numpy as np from panda3d.bullet import BulletBodyNode from metadrive.constants import MetaDriveType +def is_port_occupied(port, host='127.0.0.1'): + """ + Check if a given port is occupied on the specified host. + + :param port: Port number to check. + :param host: Host address to check the port on. Default is '127.0.0.1'. + :return: True if the port is occupied, False otherwise. + """ + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: + result = sock.connect_ex((host, port)) + return result == 0 + + def import_pygame(): os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide" import pygame