From ed05fc09f330d42740841ecd4656328e6a790e2b Mon Sep 17 00:00:00 2001 From: wassname Date: Sat, 20 Feb 2021 07:25:32 +0800 Subject: [PATCH] video and logging --- README.md | 2 + curl_sac.py | 37 +++++---- .../args.json | 44 ---------- ...ut.tfevents.1613620610.mjcdesktop.457460.0 | Bin 143 -> 0 bytes scripts/play.sh | 16 ++++ scripts/run.sh | 28 ++++++- train.py | 78 +++++++++++++----- video.py | 8 +- 8 files changed, 128 insertions(+), 85 deletions(-) delete mode 100644 runs/ApplePick-v0-02-18-im84-b32-s1-mixed/args.json delete mode 100644 runs/ApplePick-v0-02-18-im84-b32-s1-mixed/tb/events.out.tfevents.1613620610.mjcdesktop.457460.0 create mode 100755 scripts/play.sh diff --git a/README.md b/README.md index 4beb1e8..c5f975f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ Adapt to Apple gym +UPTO: env.render returns nothing... make it return last obs. This should have +video work TODO: - apple diff --git a/curl_sac.py b/curl_sac.py index cdedcab..3acb1b8 100644 --- a/curl_sac.py +++ b/curl_sac.py @@ -4,7 +4,7 @@ import torch.nn as nn import torch.nn.functional as F import copy import math - +from pathlib import Path import utils from encoder import make_encoder @@ -466,24 +466,33 @@ class CurlSacAgent(object): obs_anchor, obs_pos = cpc_kwargs["obs_anchor"], cpc_kwargs["obs_pos"] self.update_cpc(obs_anchor, obs_pos,cpc_kwargs, L, step) - def save(self, model_dir, step): - torch.save( - self.actor.state_dict(), '%s/actor_%s.pt' % (model_dir, step) - ) - torch.save( - self.critic.state_dict(), '%s/critic_%s.pt' % (model_dir, step) - ) + # def save(self, model_dir, step): + # torch.save( + # self.actor.state_dict(), '%s/actor_%s.pt' % (model_dir, step) + # ) + # torch.save( + # self.critic.state_dict(), '%s/critic_%s.pt' % (model_dir, step) + # ) def save_curl(self, model_dir, step): torch.save( self.CURL.state_dict(), '%s/curl_%s.pt' % (model_dir, step) ) - def load(self, model_dir, step): - self.actor.load_state_dict( - torch.load('%s/actor_%s.pt' % (model_dir, step)) - ) - self.critic.load_state_dict( - torch.load('%s/critic_%s.pt' % (model_dir, step)) + # def load(self, model_dir, step): + # self.actor.load_state_dict( + # torch.load('%s/actor_%s.pt' % (model_dir, step)) + # ) + # self.critic.load_state_dict( + # torch.load('%s/critic_%s.pt' % (model_dir, step)) + # ) + + def load_curl(self, model_dir, step=None): + if step is None: + steps = sorted([int(f.stem.split('_')[1]) for f in Path(model_dir).glob("curl_*.pt")]) + step = steps[-1] + print(f'auto loading steps step {step}') + self.CURL.load_state_dict( + torch.load('%s/curl_%s.pt' % (model_dir, step)) ) \ No newline at end of file diff --git a/runs/ApplePick-v0-02-18-im84-b32-s1-mixed/args.json b/runs/ApplePick-v0-02-18-im84-b32-s1-mixed/args.json deleted file mode 100644 index 20023f5..0000000 --- a/runs/ApplePick-v0-02-18-im84-b32-s1-mixed/args.json +++ /dev/null @@ -1,44 +0,0 @@ -{ - "action_repeat": 1, - "actor_beta": 0.9, - "actor_log_std_max": 2, - "actor_log_std_min": -10, - "actor_lr": 0.001, - "actor_update_freq": 2, - "agent": "curl_sac", - "alpha_beta": 0.5, - "alpha_lr": 0.0001, - "batch_size": 32, - "critic_beta": 0.9, - "critic_lr": 0.001, - "critic_target_update_freq": 2, - "critic_tau": 0.01, - "curl_latent_dim": 128, - "detach_encoder": false, - "discount": 0.99, - "domain_name": "ApplePick-v0", - "encoder_feature_dim": 50, - "encoder_lr": 0.001, - "encoder_tau": 0.05, - "encoder_type": "mixed", - "eval_freq": 1000, - "frame_stack": 3, - "hidden_dim": 1024, - "image_size": 84, - "init_steps": 1000, - "init_temperature": 0.1, - "log_interval": 100, - "num_eval_episodes": 10, - "num_filters": 32, - "num_layers": 4, - "num_train_steps": 1000000, - "pre_transform_image_size": 124, - "render": false, - "replay_buffer_capacity": 10000, - "save_buffer": false, - "save_model": false, - "save_tb": true, - "save_video": false, - "seed": 1, - "work_dir": "./runs/ApplePick-v0-02-18-im84-b32-s1-mixed" -} \ No newline at end of file diff --git a/runs/ApplePick-v0-02-18-im84-b32-s1-mixed/tb/events.out.tfevents.1613620610.mjcdesktop.457460.0 b/runs/ApplePick-v0-02-18-im84-b32-s1-mixed/tb/events.out.tfevents.1613620610.mjcdesktop.457460.0 deleted file mode 100644 index 223248349026f86efb8c324fd20bfffebe40b857..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 143 zcmb1OfPlsI-b$Q|#~KnUxor7Pik3Wj(%!EW^sN>swha=)w;eX>QME&A8vHx{4Dx49b~SS43`v_FtYmiqSW%l Qq7>1v=}%l%Ot args.eval_freq): + if (step % args.eval_freq == 0) and (step >= args.eval_freq): L.log("eval/episode", episode, step) evaluate(env, agent, video, args.num_eval_episodes, L, step, args) if args.save_model: @@ -281,12 +308,16 @@ def main(): if step % args.log_interval == 0: L.log("train/episode_reward", episode_reward, step) + for k in keys_to_monitor: + L.log("train/episode_info" + k, np.sum(episode_info[k]), step) + obs = env.reset() assert env.observation_space.contains( obs ), f"obs should be in space. ob={obs} space={env.observation_space}" done = False episode_reward = 0 + episode_info = defaultdict(int) episode_step = 0 episode += 1 if step % args.log_interval == 0: @@ -302,11 +333,12 @@ def main(): action ), f"obs should be in space. ob={action} space={env.action_space}" - # run training update - if step >= args.init_steps: - num_updates = 1 - for _ in range(num_updates): - agent.update(replay_buffer, L, step) + if step % 10 ==0: + # run training update + if step >= args.init_steps: + num_updates = 1 + for _ in range(num_updates): + agent.update(replay_buffer, L, step) next_obs, reward, done, info = env.step(action) @@ -314,6 +346,8 @@ def main(): done_bool = 0 if episode_step + 1 == env._max_episode_steps else float(done) episode_reward += reward replay_buffer.add(obs, action, reward, next_obs, done_bool) + for k in keys_to_monitor: + episode_info[k] += info[k] obs = next_obs episode_step += 1 diff --git a/video.py b/video.py index 0e319f8..82bda28 100644 --- a/video.py +++ b/video.py @@ -1,7 +1,7 @@ import imageio import os import numpy as np - +import logging class VideoRecorder(object): def __init__(self, dir_name, height=256, width=256, camera_id=0, fps=30): @@ -16,14 +16,14 @@ class VideoRecorder(object): self.frames = [] self.enabled = self.dir_name is not None and enabled - def record(self, env): + def record(self, env, yaw=0): if self.enabled: try: frame = env.render( mode='rgb_array', height=self.height, width=self.width, - camera_id=self.camera_id + yaw=yaw ) except: frame = env.render( @@ -36,3 +36,5 @@ class VideoRecorder(object): if self.enabled: path = os.path.join(self.dir_name, file_name) imageio.mimsave(path, self.frames, fps=self.fps) + + logging.info(f"saved {len(self.frames)} frames to video: '{path}'")