forked from axelbr/dvbf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
140 lines (120 loc) · 4.6 KB
/
train.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import time
import cv2
import gym as gym
import imageio
import numpy as np
import torch
import torch.nn as nn
import torch.distributions as td
from torch.optim.lr_scheduler import ExponentialLR
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
from torch.utils.data import TensorDataset, DataLoader, Dataset
from gym.wrappers import GrayScaleObservation, FlattenObservation, TransformObservation, ResizeObservation
from gym.wrappers.pixel_observation import PixelObservationWrapper
from model import DVBF
from wrapper import PixelDictWrapper, PendulumEnv
dim_z = 3
dim_x = (16, 16)
dim_u = 1
dim_a = 16
dim_w = 3
batch_size = 32
num_iterations = int(5000)
learning_rate = 0.01
def make_env():
env = PendulumEnv()
env.reset()
env = PixelDictWrapper(PixelObservationWrapper(env))
env = GrayScaleObservation(env)
env = ResizeObservation(env, shape=(16, 16))
print(env.action_space)
print(env.observation_space)
return env
def collect_data(num_sequences: int, sequence_length: int):
data = dict(obs=[], actions=[])
env = make_env()
episodes = 0
while episodes < num_sequences:
obs = env.reset()
done, t = False, 0
observations, actions = [], []
while not done and episodes < num_sequences:
action = env.action_space.sample()
observations.append(obs)
actions.append(action)
obs, reward, done, info = env.step(action)
t += 1
if t == sequence_length:
t = 0
data['obs'].append(observations)
data['actions'].append(actions)
observations, actions = [], []
episodes += 1
np.savez(f'dataset/raw.npz', obs=np.array(data['obs']), actions=np.asarray(data['actions']))
def load_data(file: str, device='cpu') -> Dataset:
data = torch.from_numpy(np.load(file)['data']).to(device)
x, u = data[..., :-1], data[..., -1:]
x = x.to(torch.float32) / 255. - 0.5
return TensorDataset(x, u)
def train():
device = 'cuda' if torch.cuda.is_available() else 'cpu'
writer = SummaryWriter()
datasets = dict((k, load_data(file=f'dataset/{k}.npz', device=device)) for k in ['training', 'test', 'validation'])
train_loader = DataLoader(datasets['training'], batch_size=batch_size, shuffle=True)
validation_loader = DataLoader(datasets['validation'], batch_size=batch_size, shuffle=False)
dvbf = DVBF(dim_x=16*16, dim_u=1, dim_z=4, dim_w=4).to(device)
#dvbf = torch.load('dvbf.th').to(device)
optimizer = torch.optim.Adam(dvbf.parameters(), lr=learning_rate)
scheduler = ExponentialLR(optimizer, gamma=0.9)
for i in range(num_iterations):
total_loss = 0
dvbf.train()
for batch in train_loader:
x, u = batch[0], batch[1]
optimizer.zero_grad()
loss = dvbf.loss(x, u)
loss.backward()
optimizer.step()
total_loss += loss.item()
scheduler.step()
writer.add_scalar('loss', scalar_value=total_loss, global_step=i)
writer.add_scalar('learning rate', scalar_value=scheduler.get_lr()[0], global_step=i)
dvbf.train(False)
total_val_loss = 0
for batch in validation_loader:
x, u = batch[0], batch[1]
val_loss = dvbf.loss(x, u)
total_val_loss += val_loss.item()
writer.add_scalar('val_loss', scalar_value=total_val_loss, global_step=i)
print(f'[Epoch {i}] train_loss: {total_loss}, val_loss: {total_val_loss}')
if i % 100 == 0:
torch.save(dvbf, 'checkpoints/dvbf.th')
generate(filename=f'dvbf-epoch-{i}')
torch.save(dvbf, 'dvbf.th')
def generate(filename):
dvbf = torch.load('dvbf.th').to('cpu')
dataset = load_data('dataset/validation.npz')
x = dataset[0][0].unsqueeze(dim=0)
u = dataset[0][1].unsqueeze(dim=0)
T = u.shape[1]
z, _ = dvbf.filter(x=x[:1, :5], u=u)
reconstructed = dvbf.reconstruct(z).view(1, T, -1)
def format(x):
img = torch.clip((x + 0.5) * 255., 0, 255).to(torch.uint8)
return img.view(-1, 16, 16).numpy()
frames = []
for i in range(T):
gt = format(x[:, i])
pred = format(reconstructed[:, i])
img = np.concatenate([gt, pred], axis=1).squeeze()
#cv2.imshow(mat=img, winname='generated')
#cv2.waitKey(50)
frames.append(img)
with imageio.get_writer(f"checkpoints/{filename}.mp4", mode="I") as writer:
for idx, frame in enumerate(frames):
writer.append_data(frame)
if __name__ == '__main__':
#collect_data(5, 15)
train()
generate()