-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
246 lines (212 loc) · 8.27 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#%%
import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'
os.chdir(os.path.dirname(os.path.abspath(__file__)))
#%%
import numpy as np
import pandas as pd
import tqdm
from PIL import Image
import matplotlib.pyplot as plt
plt.switch_backend('agg')
import torch
from torch import nn
import torch.nn.functional as F
from torch.utils.data import TensorDataset, DataLoader
from torch.utils.data import Dataset
from modules.simulation import (
set_random_seed,
is_dag,
)
from modules.datasets import (
LabeledDataset,
UnLabeledDataset,
)
from modules.train import (
train_VAE,
train_InfoMax,
train_CDGVAE,
)
#%%
import sys
import subprocess
try:
import wandb
except:
subprocess.check_call([sys.executable, "-m", "pip", "install", "wandb"])
with open("./wandb_api.txt", "r") as f:
key = f.readlines()
subprocess.run(["wandb", "login"], input=key[0], encoding='utf-8')
import wandb
run = wandb.init(
project="CausalDisentangled",
entity="anseunghwan",
tags=["VAEBased"],
)
#%%
import argparse
import ast
def arg_as_list(s):
v = ast.literal_eval(s)
if type(v) is not list:
raise argparse.ArgumentTypeError("Argument \"%s\" is not a list" % (s))
return v
def get_args(debug):
parser = argparse.ArgumentParser('parameters')
parser.add_argument('--seed', type=int, default=1,
help='seed for repeatable results')
parser.add_argument('--model', type=str, default='CDGVAE',
help='VAE based model options: VAE, InfoMax, CDGVAE')
# causal structure
parser.add_argument("--node", default=4, type=int,
help="the number of nodes")
parser.add_argument("--scm", default='linear', type=str,
help="SCM structure options: linear or nonlinear")
parser.add_argument("--flow_num", default=1, type=int,
help="the number of invertible NN flow")
parser.add_argument("--inverse_loop", default=100, type=int,
help="the number of inverse loop")
parser.add_argument("--factor", default=[1, 1, 2], type=arg_as_list,
help="Numbers of latents allocated to each factor in image")
# data options
parser.add_argument('--labeled_ratio', default=1, type=float, # fully-supervised
help='ratio of labeled dataset for semi-supervised learning')
parser.add_argument("--label_normalization", default=True, type=bool,
help="If True, normalize additional information label data")
parser.add_argument("--adjacency_scaling", default=True, type=bool,
help="If True, scaling adjacency matrix with in-degree")
parser.add_argument('--image_size', default=64, type=int,
help='width and heigh of image')
# optimization options
parser.add_argument('--epochs', default=100, type=int,
help='maximum iteration')
parser.add_argument('--batch_size', default=128, type=int,
help='batch size')
parser.add_argument('--lr', default=0.001, type=float,
help='learning rate')
parser.add_argument('--lr_D', default=0.0001, type=float, # InfoMax
help='learning rate for discriminator in InfoMax')
# loss coefficients
parser.add_argument('--beta', default=0.1, type=float,
help='observation noise')
parser.add_argument('--lambda', default=5, type=float,
help='weight of label alignment loss')
parser.add_argument('--gamma', default=1, type=float, # InfoMax
help='weight of f-divergence (lower bound of information)')
if debug:
return parser.parse_args(args=[])
else:
return parser.parse_args()
#%%
def main():
config = vars(get_args(debug=False)) # default configuration
config["cuda"] = torch.cuda.is_available()
device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
wandb.config.update(config)
set_random_seed(config["seed"])
torch.manual_seed(config["seed"])
if config["cuda"]:
torch.cuda.manual_seed(config["seed"])
"""dataset"""
dataset = LabeledDataset(config)
dataloader = DataLoader(dataset, batch_size=config["batch_size"], shuffle=True)
"""
Causal Adjacency Matrix
light -> length
light -> position
angle -> length
angle -> position
"""
B = torch.zeros(config["node"], config["node"])
B[dataset.name.index('light'), dataset.name.index('length')] = 1
B[dataset.name.index('light'), dataset.name.index('position')] = 1
B[dataset.name.index('angle'), dataset.name.index('length')] = 1
B[dataset.name.index('angle'), dataset.name.index('position')] = 1
"""adjacency matrix scaling"""
if config["adjacency_scaling"]:
indegree = B.sum(axis=0)
mask = (indegree != 0)
B[:, mask] = B[:, mask] / indegree[mask]
"""model"""
if config["model"] == 'VAE':
from modules.model import VAE
model = VAE(B, config, device)
elif config["model"] == 'InfoMax':
from modules.model import VAE, Discriminator
model = VAE(B, config, device)
discriminator = Discriminator(config, device)
discriminator = discriminator.to(device)
optimizer_D = torch.optim.Adam(
discriminator.parameters(),
lr=config["lr_D"]
)
elif config["model"] == 'CDGVAE':
"""Decoder masking"""
mask = []
# light
m = torch.zeros(config["image_size"], config["image_size"], 3)
m[:20, ...] = 1
mask.append(m)
# angle
m = torch.zeros(config["image_size"], config["image_size"], 3)
m[20:51, ...] = 1
mask.append(m)
# shadow
m = torch.zeros(config["image_size"], config["image_size"], 3)
m[51:, ...] = 1
mask.append(m)
from modules.model import CDGVAE
model = CDGVAE(B, mask, config, device)
else:
raise ValueError('Not supported model!')
model = model.to(device)
optimizer = torch.optim.Adam(
model.parameters(),
lr=config["lr"]
)
model.train()
for epoch in range(config["epochs"]):
if config["model"] == 'VAE':
logs, xhat = train_VAE(dataloader, model, config, optimizer, device)
elif config["model"] == 'InfoMax':
logs, xhat = train_InfoMax(dataloader, model, discriminator, config, optimizer, optimizer_D, device)
elif config["model"] == 'CDGVAE':
logs, xhat = train_CDGVAE(dataloader, model, config, optimizer, device)
else:
raise ValueError('Not supported model!')
print_input = "[epoch {:03d}]".format(epoch + 1)
print_input += ''.join([', {}: {:.4f}'.format(x, np.mean(y)) for x, y in logs.items()])
print(print_input)
"""update log"""
wandb.log({x : np.mean(y) for x, y in logs.items()})
if epoch % 10 == 0:
plt.figure(figsize=(4, 4))
for i in range(9):
plt.subplot(3, 3, i+1)
plt.imshow((xhat[i].cpu().detach().numpy() + 1) / 2)
plt.axis('off')
plt.savefig('./assets/tmp_image_{}.png'.format(epoch))
plt.close()
"""reconstruction result"""
fig = plt.figure(figsize=(4, 4))
for i in range(9):
plt.subplot(3, 3, i+1)
plt.imshow((xhat[i].cpu().detach().numpy() + 1) / 2)
plt.axis('off')
plt.savefig('./assets/recon.png')
plt.close()
wandb.log({'reconstruction': wandb.Image(fig)})
"""model save"""
torch.save(model.state_dict(), './assets/model_{}_{}.pth'.format(config["model"], config["scm"]))
artifact = wandb.Artifact('model_{}_{}'.format(config["model"], config["scm"]),
type='model',
metadata=config) # description=""
artifact.add_file('./assets/model_{}_{}.pth'.format(config["model"], config["scm"]))
artifact.add_file('./main.py')
artifact.add_file('./modules/model.py')
wandb.log_artifact(artifact)
wandb.run.finish()
#%%
if __name__ == '__main__':
main()
#%%