def render(env_name): from many_world import IS_PATCHED assert IS_PATCHED, "need the patched gym" import gym, numpy as np from tqdm import tqdm from ml_logger import logger logger.prefix = env_name env = gym.make(env_name) env.reset() frames = [] y = 0 args = [ dict(x=x, y=y, filename=f"images/{x:0.3f},{y:0.3f}.png") for x in np.linspace(-0.25, 0.25, 128) ] logger.log_data(args, 'index.pkl') for p in tqdm(args): x, y, filename = p['x'], p['y'], p['filename'] env.set_state(np.array([x, y, 0, 0]), np.array([0, 0, 0, 0])) # env.do_simulation([0, 0], 1) # PointMass does not need this. image = env.render('grey', width=20, height=20) frames.append(image) logger.log_image(image, filename) print('saving video') logger.log_video(frames, f"{env_name}.mp4") print('done')
def test_pyplot(setup): import scipy.misc import matplotlib matplotlib.use('TKAgg') import matplotlib.pyplot as plt import numpy as np face = scipy.misc.face() logger.log_image(face, "face.png") fig = plt.figure(figsize=(4, 2)) xs = np.linspace(0, 5, 1000) plt.plot(xs, np.cos(xs)) logger.savefig("face_02.png", fig=fig) plt.close() fig = plt.figure(figsize=(4, 2)) xs = np.linspace(0, 5, 1000) plt.plot(xs, np.cos(xs)) logger.savefig('sine.pdf')
def test_image(setup): import scipy.misc import numpy as np image_bw = np.zeros((64, 64, 1), dtype=np.uint8) image_bw_2 = scipy.misc.face(gray=True)[::4, ::4] image_rgb = np.zeros((64, 64, 3), dtype=np.uint8) image_rgba = scipy.misc.face()[::4, ::4, :] logger.log_image(image_bw, "black_white.png") logger.log_image(image_bw_2, "bw_face.png") logger.log_image(image_rgb, 'rgb.png') logger.log_image(image_rgba, f'rgba_face_{100}.png') logger.log_image(image_bw, f"bw_{100}.png") logger.log_image(image_rgba, f"rbga_{100}.png")
def test_image(setup): import scipy.misc import numpy as np image_bw = np.zeros((64, 64, 1), dtype=np.uint8) image_bw_2 = scipy.misc.face(gray=True)[::4, ::4] image_rgb = np.zeros((64, 64, 3), dtype=np.uint8) image_rgba = scipy.misc.face()[::4, ::4, :] logger.log_image(image_bw, "black_white.png") logger.log_image(image_bw_2, "bw_face.png") logger.log_image(image_rgb, 'rgb.png') logger.log_image(image_rgba, f'rgba_face_{100}.png') logger.log_image(image_bw, f"bw_{100}.png") logger.log_image(image_rgba, f"rbga_{100}.png") logger.log_image(image_bw[:, :, 0].astype(np.float32), "black_white_individual.png", normalize='individual') logger.log_image(np.ones([64, 64]), "black_white_grid.png", normalize='grid')
logger.remove(prefix) logger.configure(prefix=prefix) logger.log_params(Args=dict(lr=10**(-2 - i), weight_decay=0.001, gradient_clip=0.9, env_id="GoalMassDiscreteIdLess-v0", seed=int(i * 100))) for ep in range(500 + 1): logger.log_metrics(epoch=ep, sine=fn(ep), slow_sine=fn_1(ep)) logger.flush() if ep % 10 == 0: logger.log_image(face('gray'), f"figures/gray_{ep:04d}.png") logger.log_image(face('rgb'), f"figures/rgb_{ep:04d}.png") logger.log_image(face('gray'), "figures/face_gray.png") logger.log_image(face('rgb'), "figures/face_rgb.png") with logger.PrefixContext(f"runs/{username}/{project}"): logger.log_line("# Root Files\n", file="RAEDME.md") # import numpy as np # import matplotlib.pyplot as plt # # xs = np.arange(500) # ys = (1 + 0.001 * xs) * np.sin(xs * 0.1 / np.pi) # ys += np.random.rand(*ys.shape) * 1
# obs['goal_img'] = self.render('grey', width=self.width, height=self.height).transpose(0, 1)[None, ...] # self.set_state(curr_qpos, self.sim.data.qvel) # return obs from gym.envs import register if __name__ == "__main__": import gym env = gym.make('GoalMassDiscrete-v0') env.reset() frame = env.render('rgb', width=200, height=200) from os.path import basename from ml_logger import logger logger.log_image(frame, f"../figures/{basename(__file__)}:{env.spec.id}.png") # to show thy human. from PIL import Image im = Image.fromarray(frame) im.show() else: # note: kwargs are not passed in to the constructor when entry_point is a function. register(id="ManyWorld-v0", entry_point=ManyWorldEnv, kwargs=dict(n_objs=1, ), max_episode_steps=50)
import os import scipy.misc import matplotlib matplotlib.use('TKAgg') import matplotlib.pyplot as plt import numpy as np from ml_logger import logger logger.configure(os.path.realpath('.')) face = scipy.misc.face() logger.log_image(0, test_image=face) fig = plt.figure(figsize=(4, 2)) xs = np.linspace(0, 5, 1000) plt.plot(xs, np.cos(xs)) logger.log_pyplot(0, fig) plt.close()