From afb7ad650d12721504dcf9edfbb9fe3ec54caf91 Mon Sep 17 00:00:00 2001 From: Ivan-267 <61947090+Ivan-267@users.noreply.github.com> Date: Sun, 3 Dec 2023 11:01:32 +0100 Subject: [PATCH 1/3] fix dtype of image data --- godot_rl/core/godot_env.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/godot_rl/core/godot_env.py b/godot_rl/core/godot_env.py index b0901848..79902273 100644 --- a/godot_rl/core/godot_env.py +++ b/godot_rl/core/godot_env.py @@ -358,7 +358,7 @@ def _decode_2d_obs_from_string( shape, ): return ( - np.frombuffer(bytes.fromhex(hex_string), dtype=np.float16) + np.frombuffer(bytes.fromhex(hex_string), dtype=np.uint8) .reshape(shape) .astype(np.float32)[:, :, :] # TODO remove the alpha channel ) From 55e375be0d8a2f2c90f371a5347b285ec6f2abf8 Mon Sep 17 00:00:00 2001 From: Ivan-267 <61947090+Ivan-267@users.noreply.github.com> Date: Wed, 6 Dec 2023 13:08:19 +0100 Subject: [PATCH 2/3] Change image type to uint8 --- godot_rl/core/godot_env.py | 1 - 1 file changed, 1 deletion(-) diff --git a/godot_rl/core/godot_env.py b/godot_rl/core/godot_env.py index 79902273..77e44335 100644 --- a/godot_rl/core/godot_env.py +++ b/godot_rl/core/godot_env.py @@ -360,7 +360,6 @@ def _decode_2d_obs_from_string( return ( np.frombuffer(bytes.fromhex(hex_string), dtype=np.uint8) .reshape(shape) - .astype(np.float32)[:, :, :] # TODO remove the alpha channel ) def _send_as_json(self, dictionary): From a84160c0734de392c7018039979af1ebe83b2bfc Mon Sep 17 00:00:00 2001 From: Ivan-267 <61947090+Ivan-267@users.noreply.github.com> Date: Fri, 22 Dec 2023 14:36:31 +0100 Subject: [PATCH 3/3] Set uint8 box obs space for images --- godot_rl/core/godot_env.py | 72 ++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/godot_rl/core/godot_env.py b/godot_rl/core/godot_env.py index 77e44335..4808388a 100644 --- a/godot_rl/core/godot_env.py +++ b/godot_rl/core/godot_env.py @@ -5,30 +5,31 @@ import socket import subprocess import time +from collections import OrderedDict from sys import platform +from typing import Optional import numpy as np -from gymnasium import spaces -from typing import Optional from godot_rl.core.utils import ActionSpaceProcessor, convert_macos_path +from gymnasium import spaces class GodotEnv: - MAJOR_VERSION = "0" # Versioning for the environment + MAJOR_VERSION = "0" # Versioning for the environment MINOR_VERSION = "4" - DEFAULT_PORT = 11008 # Default port for communication with Godot Game - DEFAULT_TIMEOUT = 60 # Default socket timeout TODO + DEFAULT_PORT = 11008 # Default port for communication with Godot Game + DEFAULT_TIMEOUT = 60 # Default socket timeout TODO def __init__( - self, - env_path: str=None, - port: int=DEFAULT_PORT, - show_window: bool=False, - seed:int=0, - framerate:Optional[int]=None, - action_repeat:Optional[int]=None, - speedup:Optional[int]=None, - convert_action_space:bool=False, + self, + env_path: str = None, + port: int = DEFAULT_PORT, + show_window: bool = False, + seed: int = 0, + framerate: Optional[int] = None, + action_repeat: Optional[int] = None, + speedup: Optional[int] = None, + convert_action_space: bool = False, ): """ Initialize a new instance of GodotEnv @@ -96,18 +97,18 @@ def check_platform(self, filename: str): if platform == "linux" or platform == "linux2": # Linux assert ( - pathlib.Path(filename).suffix == ".x86_64" - ), f"Incorrect file suffix for filename {filename} suffix {pathlib.Path(filename).suffix }. Please provide a .x86_64 file" + pathlib.Path(filename).suffix == ".x86_64" + ), f"Incorrect file suffix for filename {filename} suffix {pathlib.Path(filename).suffix}. Please provide a .x86_64 file" elif platform == "darwin": # OSX assert ( - pathlib.Path(filename).suffix == ".app" - ), f"Incorrect file suffix for filename {filename} suffix {pathlib.Path(filename).suffix }. Please provide a .app file" + pathlib.Path(filename).suffix == ".app" + ), f"Incorrect file suffix for filename {filename} suffix {pathlib.Path(filename).suffix}. Please provide a .app file" elif platform == "win32": # Windows... assert ( - pathlib.Path(filename).suffix == ".exe" - ), f"Incorrect file suffix for filename {filename} suffix {pathlib.Path(filename).suffix }. Please provide a .exe file" + pathlib.Path(filename).suffix == ".exe" + ), f"Incorrect file suffix for filename {filename} suffix {pathlib.Path(filename).suffix}. Please provide a .exe file" else: assert 0, f"unknown filetype {pathlib.Path(filename).suffix}" @@ -157,7 +158,6 @@ def step(self, action, order_ij=False): self.step_send(action, order_ij=order_ij) return self.step_recv() - def step_send(self, action, order_ij=False): """ Send the action to the Godot environment. @@ -191,8 +191,6 @@ def step_recv(self): [{}] * len(response["done"]), ) - - def _process_obs(self, response_obs: dict): """ Process observation data. @@ -319,7 +317,7 @@ def _get_env_info(self): # actions can be "single" for a single action head # or "multi" for several outputeads - action_spaces = {} + action_spaces = OrderedDict() print("action space", json_dict["action_space"]) for k, v in json_dict["action_space"].items(): if v["action_type"] == "discrete": @@ -335,12 +333,20 @@ def _get_env_info(self): print("observation space", json_dict["observation_space"]) for k, v in json_dict["observation_space"].items(): if v["space"] == "box": - observation_spaces[k] = spaces.Box( - low=-1.0, - high=1.0, - shape=v["size"], - dtype=np.float32, - ) + if "2d" in k: + observation_spaces[k] = spaces.Box( + low=0, + high=255, + shape=v["size"], + dtype=np.uint8, + ) + else: + observation_spaces[k] = spaces.Box( + low=-1.0, + high=1.0, + shape=v["size"], + dtype=np.float32, + ) elif v["space"] == "discrete": observation_spaces[k] = spaces.Discrete(v["size"]) else: @@ -350,12 +356,10 @@ def _get_env_info(self): self.num_envs = json_dict["n_agents"] - - @staticmethod def _decode_2d_obs_from_string( - hex_string, - shape, + hex_string, + shape, ): return ( np.frombuffer(bytes.fromhex(hex_string), dtype=np.uint8)