-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilis.py
144 lines (109 loc) · 4.14 KB
/
utilis.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
from typing import Literal
import torch
from torch.utils.data import DataLoader
import BiGAN
import BiGAN.detect_GAN
import BiGAN.discriminator
import BiGAN.encoder
import BiGAN.generator
import BiGAN.results
import BiGAN.train_GAN
import dataset
import flow.layers
import flow.maf
import flow.train_flow
from vae.train_vae import train_and_save
from vae.vae import VariationalAutoencoder
PATH_TEST_TYPICAL = "./dataset/test_typical"
PATH_TEST_NOVEL = "./dataset/test_novel/all"
RANDOM_SEED = 42
FREQ_PRINT = 20
PATH_TRAIN = "./dataset/train_typical"
PATH_VALIDATION = "./dataset/validation_typical"
ModelType = Literal["GAN", "VAE", "FLOW"]
def get_transform(model_name: ModelType):
if model_name == "GAN":
return dataset.ToTensorWithScaling()
elif model_name == "VAE":
return dataset.ToTensorWithScaling(-1.0, 1.0)
elif model_name == "FLOW":
return dataset.Dequantize()
else:
raise ValueError("Unknown model")
def get_loaders(transform, batch_size: int):
train_dataset = dataset.ImageDataLoader(PATH_TRAIN, transform=transform)
valdiaiton_dataset = dataset.ImageDataLoader(PATH_VALIDATION, transform=transform)
test_typical_dataset = dataset.ImageDataLoader(
PATH_TEST_TYPICAL, transform=transform
)
test_novel_dataset = dataset.ImageDataLoader(PATH_TEST_NOVEL, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
val_loader = DataLoader(valdiaiton_dataset, batch_size=batch_size)
test_typical_loader = DataLoader(test_typical_dataset, batch_size=1)
test_novel_loader = DataLoader(test_novel_dataset, batch_size=1)
return train_loader, val_loader, test_typical_loader, test_novel_loader
def train_model(
model_name: ModelType, epoch_number: int, lr: float, device: str, save_path: str
):
print(model_name, lr, epoch_number, device)
transform = get_transform(model_name)
train_loader, val_loader, test_typical_loader, test_novel_loader = get_loaders(
transform,
64,
)
if model_name == "GAN":
model = BiGAN.train_GAN.TrainerBiGAN(
epoch_number, lr, train_loader, val_loader, device
)
encoder, generator, discriminator = model.train()
torch.save(
{
"encoder_state_dict": encoder.state_dict(),
"generator_state_dict": generator.state_dict(),
"discriminator_state_dict": discriminator.state_dict(),
},
save_path,
)
elif model_name == "VAE":
n_data_features = 64 * 64 * 6
n_hidden_features = 1024
n_latent_features = 256
model = VariationalAutoencoder(
n_data_features=n_data_features,
n_encoder_hidden_features=n_hidden_features,
n_decoder_hidden_features=n_hidden_features,
n_latent_features=n_latent_features,
)
print(f"Starting training: \n{n_latent_features=}, {n_hidden_features=}, {lr=}")
train_and_save(
model=model,
epochs=epoch_number,
train_loader=train_loader,
val_loader=val_loader,
lr=lr,
model_name="vae",
save_path=save_path,
)
elif model_name == "FLOW":
model = flow.maf.MAF(6 * 64 * 64, [64, 64, 64, 64, 64], 5, use_reverse=True)
trainer = flow.train_flow.TrainerMAF(
model, epoch_number, lr, train_loader, device
)
trainer.train()
save_flow_model(model, save_path)
else:
raise ValueError("Unkown Model")
def save_flow_model(model: flow.maf.MAF, path: str):
model_state = {
"model_state_dict": model.state_dict(),
"batch_norm_running_states": {},
}
for index, layer in enumerate(model.layers):
if isinstance(layer, flow.layers.BatchNormLayerWithRunning):
model_state["batch_norm_running_states"][
f"batch_norm_{index}_running_mean"
] = layer.running_mean
model_state["batch_norm_running_states"][
f"batch_norm_{index}_running_var"
] = layer.running_var
torch.save(model_state, path)