-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay_image_sequences.py
More file actions
71 lines (57 loc) · 2.57 KB
/
Copy pathdisplay_image_sequences.py
File metadata and controls
71 lines (57 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import numpy as np
from environments import EnvFactory
from environments.wrappers.DefaultWrappers import DefaultWrappers
from singletons.Logger import Logger
import torch
import hydra
from omegaconf import OmegaConf, open_dict
import agents.math_fc.functions as math_fc
from hydra.utils import instantiate
from agents.save.Checkpoint import Checkpoint
from environments.dSpritesEnv import dSpritesEnv
@hydra.main(config_path="config", config_name="training")
def display_images(config):
# Create the logger and keep track of the configuration.
Logger.get().info("Configuration:\n{}".format(OmegaConf.to_yaml(config)))
# Create the environment and apply standard wrappers.
Logger.get().info("Load the environment...\n")
initial_state = np.array([0, 2, 5, 15, 0, 31], dtype=np.float64)
env = dSpritesEnv(config, initial_state)
with open_dict(config):
config.env.n_actions = env.action_space.n
env = DefaultWrappers.apply(env, config["images"]["shape"])
# Load the agent from the checkpoint.
Logger.get().info("Load the agent...\n")
archive = Checkpoint(config["agent"]["tensorboard_dir"], config["checkpoint"]["file"])
agent = archive.load_model() if archive.exists() else instantiate(config["agent"])
# Collect the initial image from the environment and infer the associated state.
obs = env.reset()
obs = torch.unsqueeze(obs, dim=0)
mean, log_var = agent.encoder(obs)
next_state = math_fc.reparameterize(mean, log_var)
# Collect images from the environment.
Logger.get().info("Gather images from the environment...\n")
images = []
actions = [torch.tensor([3])] * 10
for action in actions:
# Generate an images using the VAE.
image = agent.decoder(next_state)
image = torch.nn.Sigmoid()(image)
images.append(obs)
images.append(image)
# Take an action in the environment.
obs, _, _, _ = env.step(action)
obs = torch.unsqueeze(obs, dim=0)
# Make the agent imagine what would append if it was taking the action.
mean, log_var = agent.transition(next_state, action)
next_state = math_fc.reparameterize(mean, log_var)
# Display the images in tensorboard.
Logger.get().info("Display images...\n")
images = torch.cat(images, dim=0)
agent.writer.add_images("An example of (true and generated) trajectories", images)
Logger.get().info("End.\n")
if __name__ == '__main__':
# Make hydra able to load tuples.
OmegaConf.register_new_resolver("tuple", lambda *args: tuple(args))
# Train the DGN.
display_images()