Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added config saving on new training run #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions sim/algo/ppo/on_policy_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
# Copyright (c) 2024 Beijing RobotEra TECHNOLOGY CO.,LTD. All rights reserved.
# type: ignore

import json
import os
import statistics
import time
Expand All @@ -46,6 +47,51 @@
from sim.algo.vec_env import VecEnv


def make_serializable(obj):
"""Recursively convert an object and its components to JSON-serializable types."""
if isinstance(obj, dict):
return {key: make_serializable(value) for key, value in obj.items()}
elif isinstance(obj, (list, tuple)):
return [make_serializable(item) for item in obj]
try:
# Try standard JSON serialization first
json.dumps(obj)
return obj
except (TypeError, OverflowError):
try:
# for numpy types
return float(obj)
except:
try:
return int(obj)
except:
# If all else fails, convert to string representation
return f"<{type(obj).__name__}:{str(obj)}>"


def write_config_file(config: dict, log_dir: str) -> None:
"""Writes the configuration to a JSON file in the log directory."""
if not os.path.exists(log_dir):
os.makedirs(log_dir)

metadata = {
"creation_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"experiment_name": config["runner"]["experiment_name"],
"run_name": config["runner"]["run_name"],
}

serializable_config = make_serializable(config)

full_config = {
"metadata": metadata,
"configuration": serializable_config
}

json_path = os.path.join(log_dir, "experiment_config.json")
with open(json_path, 'w') as f:
json.dump(full_config, f, indent=2, sort_keys=True)


class OnPolicyRunner:
def __init__(self, env: VecEnv, train_cfg: dict, log_dir: Optional[str] = None, device: str = "cpu"):
self.cfg = train_cfg["runner"]
Expand Down Expand Up @@ -102,6 +148,8 @@ def learn(self, num_learning_iterations: int, init_at_random_ep_len: bool = Fals
config=self.all_cfg,
)
self.writer = SummaryWriter(log_dir=self.log_dir, flush_secs=10)
if self.current_learning_iteration == 0:
write_config_file(self.all_cfg, self.log_dir)
if init_at_random_ep_len:
self.env.episode_length_buf = torch.randint_like(
self.env.episode_length_buf, high=int(self.env.max_episode_length)
Expand Down