-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
209 lines (167 loc) · 6.33 KB
/
main.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
##########################
# Autor: Junyeob Baek
# email: [email protected]
##########################
import secrets
import easydict
import matplotlib.pyplot as plt
import torch
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from tqdm import tqdm
from src.models import LSTMAE, LSTMVAE
from utils.MovingMNIST import MovingMNIST
writer = SummaryWriter()
## visualization
def imshow(past_data, title="MovingMNIST"):
num_img = len(past_data)
fig = fig = plt.figure(figsize=(4 * num_img, 4))
for idx in range(1, num_img + 1):
ax = fig.add_subplot(1, num_img + 1, idx)
ax.imshow(past_data[idx - 1])
plt.suptitle(title, fontsize=30)
plt.savefig(f"{title}")
plt.close()
def train(args, model, train_loader, test_loader):
# optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=args.learning_rate)
## interation setup
epochs = tqdm(range(args.max_iter // len(train_loader) + 1))
## training
count = 0
for epoch in epochs:
model.train()
optimizer.zero_grad()
train_iterator = tqdm(
enumerate(train_loader), total=len(train_loader), desc="training"
)
for i, batch_data in train_iterator:
if count > args.max_iter:
return model
count += 1
future_data, past_data = batch_data
## reshape
batch_size = past_data.size(0)
example_size = past_data.size(1)
image_size = past_data.size(2), past_data.size(3)
past_data = (
past_data.view(batch_size, example_size, -1).float().to(args.device)
)
# future_data = future_data.view(batch_size, example_size, -1).float().to(args.device)
mloss, recon_x, info = model(past_data)
# Backward and optimize
optimizer.zero_grad()
mloss.mean().backward()
optimizer.step()
train_iterator.set_postfix({"train_loss": float(mloss.mean())})
writer.add_scalar("train_loss", float(mloss.mean()), epoch)
model.eval()
eval_loss = 0
test_iterator = tqdm(
enumerate(test_loader), total=len(test_loader), desc="testing"
)
with torch.no_grad():
for i, batch_data in test_iterator:
future_data, past_data = batch_data
## reshape
batch_size = past_data.size(0)
example_size = past_data.size(1)
past_data = (
past_data.view(batch_size, example_size, -1).float().to(args.device)
)
# future_data = future_data.view(batch_size, example_size, -1).float().to(args.device)
mloss, recon_x, info = model(past_data)
eval_loss += mloss.mean().item()
test_iterator.set_postfix({"eval_loss": float(mloss.mean())})
if i == 0:
nhw_orig = past_data[0].view(example_size, image_size[0], -1)
nhw_recon = recon_x[0].view(example_size, image_size[0], -1)
imshow(nhw_orig.cpu(), f"orig{epoch}")
imshow(nhw_recon.cpu(), f"recon{epoch}")
# writer.add_images(f"original{i}", nchw_orig, epoch)
# writer.add_images(f"reconstructed{i}", nchw_recon, epoch)
eval_loss = eval_loss / len(test_loader)
writer.add_scalar("eval_loss", float(eval_loss), epoch)
print("Evaluation Score : [{}]".format(eval_loss))
return model
if __name__ == "__main__":
# training dataset
train_set = MovingMNIST(
root=".data/mnist",
train=True,
download=True,
transform=transforms.ToTensor(),
target_transform=transforms.ToTensor(),
)
# test dataset
test_set = MovingMNIST(
root=".data/mnist",
train=False,
download=True,
transform=transforms.ToTensor(),
target_transform=transforms.ToTensor(),
)
args = easydict.EasyDict(
{
"batch_size": 512,
"device": torch.device("cuda")
if torch.cuda.is_available()
else torch.device("cpu"),
"input_size": 4096,
"hidden_size": 2048,
"latent_size": 1024,
"learning_rate": 0.001,
"max_iter": 1000,
}
)
batch_size = args.batch_size
input_size = args.input_size
hidden_size = args.hidden_size
latent_size = args.latent_size
# define LSTM-based VAE model
model = LSTMVAE(input_size, hidden_size, latent_size, device=args.device)
model.to(args.device)
# reduce dataset size(customization for quick experiments)
# tr_split_len, te_split_len = 9000, 1000
# part_tr = torch.utils.data.random_split(
# train_set, [tr_split_len, len(train_set) - tr_split_len]
# )[0]
# part_te = torch.utils.data.random_split(
# test_set, [te_split_len, len(test_set) - te_split_len]
# )[0]
# convert to format of data loader
train_loader = torch.utils.data.DataLoader(
dataset=train_set, batch_size=args.batch_size, shuffle=True
)
test_loader = torch.utils.data.DataLoader(
dataset=test_set, batch_size=args.batch_size, shuffle=False
)
# training
train(args, model, train_loader, test_loader)
# save model
id_ = secrets.token_hex(nbytes=4)
torch.save(model.state_dict(), f"lstmvae{id_}.model")
# load model
model_to_load = LSTMVAE(input_size, hidden_size, latent_size, device=args.device)
model_to_load.to(args.device)
model_to_load.load_state_dict(torch.load(f"lstmvae{id_}.model"))
model_to_load.eval()
# show results
## past_data, future_data -> shape: (10,10)
future_data, past_data = train_set[0]
## reshape
example_size = past_data.size(0)
image_size = past_data.size(1), past_data.size(2)
past_data = past_data.view(example_size, -1).float().to(args.device)
_, recon_data, info = model_to_load(past_data.unsqueeze(0))
nhw_orig = past_data.view(example_size, image_size[0], -1).cpu()
nhw_recon = (
recon_data.squeeze(0)
.view(example_size, image_size[0], -1)
.detach()
.cpu()
.numpy()
)
imshow(nhw_orig, title=f"final_input{id_}")
imshow(nhw_recon, title=f"final_output{id_}")
plt.show()