-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtrain_vae.py
144 lines (136 loc) · 4.16 KB
/
train_vae.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
"""
## Script for training the Cond-DFC-VAE
## Example:
## >> python3 train_vae.py --name heusler --samples 5000 --epochs 100
--------------------------------------------------
## Author: Callum J. Court.
## Email: [email protected]
## Version: 1.0.0
--------------------------------------------------
## License: MIT
## Copyright: Copyright Callum Court & Batuhan Yildirim 2020, ICSG3D
-------------------------------------------------
"""
import argparse
import os
import random
import re
import warnings
import numpy as np
import tensorflow as tf
from utils import data_split
from vae.data import VAEDataGenerator
from vae.lattice_vae import LatticeDFCVAE
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" # surpress tf warnings
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--name", metavar="name", type=str, help="Name of data folder")
parser.add_argument(
"--samples",
metavar="samples",
type=int,
help="Total number of training and validation samples",
default=40000,
)
parser.add_argument(
"--epochs",
metavar="epochs",
type=int,
help="NUmber of epochs to train",
default=50,
)
parser.add_argument(
"--batch_size",
metavar="batch_size",
type=int,
help="Batch size for training",
default=20,
)
parser.add_argument(
"--ncond",
metavar="ncond",
type=int,
help="Number of condition bins",
default=10,
)
parser.add_argument(
"--nrot", metavar="nrot", type=int, help="Number of augmentations", default=10
)
parser.add_argument(
"--cond",
metavar="cond",
type=str,
help="Wether or not to condition the vae latent space",
default='formation_energy_per_atom',
)
parser.add_argument(
"--split",
metavar="split",
type=float,
help="Train-test split fraction",
default=0.8,
)
parser.add_argument(
"--d",
metavar="d",
type=int,
help="Dimension of density matrices (number of voxels)",
default=32,
)
namespace = parser.parse_args()
mode = namespace.name
path = os.path.join("data", mode, "matrices")
csv_path = os.path.join("data", mode, mode + ".csv")
d = namespace.d
input_shape = (d, d, d, 4)
n = namespace.samples
batch_size = namespace.batch_size
epochs = namespace.epochs
condition = namespace.cond
weights_dir = os.path.join("saved_models", "vae", mode)
os.makedirs(weights_dir, exist_ok=True)
os.makedirs(os.path.join("output", "vae", mode), exist_ok=True)
weights = os.path.join(weights_dir, "vae_weights_" + mode + ".best.hdf5")
perceptual_model = os.path.join(
"saved_models", "unet", mode, "unet_weights_" + mode + ".best.h5"
)
# Train-test split
training_ids, validation_ids = data_split(
path, n, frac=namespace.split, n_rot=namespace.nrot
)
# Make sure ids are unit of batch size
if len(training_ids) % batch_size != 0:
training_ids = training_ids[:-1 * int(len(training_ids) % batch_size)]
if len(validation_ids) % batch_size != 0:
validation_ids = validation_ids[:-1*int(len(validation_ids) % batch_size)]
print(len(training_ids), len(validation_ids))
# Create the VAE data generators
training_generator = VAEDataGenerator(
training_ids,
data_path=path,
property_csv=csv_path,
batch_size=batch_size,
n_channels=input_shape[-1],
shuffle=True,
n_bins=namespace.ncond,
target=condition
)
validation_generator = VAEDataGenerator(
validation_ids,
data_path=path,
property_csv=csv_path,
batch_size=batch_size,
n_channels=input_shape[-1],
shuffle=True,
n_bins=namespace.ncond,
target=condition
)
# # Train
lattice_vae = LatticeDFCVAE(
perceptual_model=perceptual_model,
cond_shape=namespace.ncond,
output_dir=os.path.join("output", "vae", mode),
)
lattice_vae.train(
training_generator, validation_generator, epochs=epochs, weights=weights
)