-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
129 lines (101 loc) · 3.75 KB
/
main.py
File metadata and controls
129 lines (101 loc) · 3.75 KB
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
import os
import mvae
import numpy as np
import tensorflow as tf
from scipy import ndimage
from keras.datasets import cifar10
from mvae.custom_logger import logger
# ==============================================================================
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
tf.compat.v1.disable_eager_execution()
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
# ==============================================================================
EPOCHS = 100
STEP_SIZE = 20
LR_DECAY = 0.75
BATCH_SIZE = 32
SAMPLE_STD = 0.5
INITIAL_EPOCH = 0
R_LOSS_FACTOR = 1
KL_LOSS_FACTOR = 0.1
LEARNING_RATE = 0.01
EXPAND_DATASET = False
PRINT_EVERY_N_BATCHES = 2000
# run params
SECTION = "vae"
RUN_ID = "0001"
BASE_DIR = "./run"
DATA_NAME = "cifar10"
BASE_DIR_SECTION = "{0}/{1}/".format(BASE_DIR, SECTION)
RUN_FOLDER = BASE_DIR_SECTION + "_".join([RUN_ID, DATA_NAME])
logger.info("Creating training directories")
if not os.path.exists(BASE_DIR):
os.mkdir(BASE_DIR)
if not os.path.exists(BASE_DIR_SECTION):
os.mkdir(BASE_DIR_SECTION)
if not os.path.exists(RUN_FOLDER):
os.mkdir(RUN_FOLDER)
os.mkdir(os.path.join(RUN_FOLDER, "viz"))
os.mkdir(os.path.join(RUN_FOLDER, "images"))
os.mkdir(os.path.join(RUN_FOLDER, "weights"))
# delete existing images
images_directory = os.path.join(RUN_FOLDER, "images")
for filename in os.listdir(images_directory):
full_image_path = os.path.join(images_directory, filename)
os.remove(full_image_path)
# ==============================================================================
logger.info("Loading and expanding dataset")
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train.astype("float32")
x_test = x_test.astype("float32")
if EXPAND_DATASET:
x_train_extra = np.ndarray(x_train.shape,
dtype=np.float32)
no_training_samples = x_train.shape[0]
for i in range(no_training_samples):
if i % 1000 == 0:
logger.info("Expanding [{0}/{1}]".format(i, no_training_samples))
sample = x_train[i, :, :, :]
x_train_extra[i, :, :, :] = ndimage.median_filter(sample, size=3)
x_train = np.concatenate([x_train, x_train_extra, x_test], axis=0)
# ==============================================================================
logger.info("Creating model")
multiscale_vae = mvae.MultiscaleVAE(
input_dims=(32, 32, 3),
z_dims=[128, 64, 32],
min_value=0.0,
max_value=255.0,
sample_std=SAMPLE_STD,
encoder={
"filters": [32, 32, 32],
"kernel_size": [(3, 3), (3, 3), (3, 3)],
"strides": [(2, 2), (2, 2), (1, 1)]
})
# ==============================================================================
multiscale_vae.compile(
learning_rate=LEARNING_RATE,
r_loss_factor=R_LOSS_FACTOR,
kl_loss_factor=KL_LOSS_FACTOR
)
# serialize model to JSON
with open(os.path.join(BASE_DIR_SECTION, "model_trainable.json"), "w") as json_file:
json_file.write(multiscale_vae.model_trainable.to_json())
# serialize model to JSON
with open(os.path.join(BASE_DIR_SECTION, "model_encoder.json"), "w") as json_file:
json_file.write(multiscale_vae.encoder.to_json())
# serialize model to JSON
with open(os.path.join(BASE_DIR_SECTION, "model_decoder.json"), "w") as json_file:
json_file.write(multiscale_vae.decoder.to_json())
# ==============================================================================
logger.info("Training model")
multiscale_vae.model_trainable.summary()
multiscale_vae.train(
x_train,
epochs=EPOCHS,
lr_decay=LR_DECAY,
step_size=STEP_SIZE,
run_folder=RUN_FOLDER,
batch_size=BATCH_SIZE,
initial_epoch=INITIAL_EPOCH,
print_every_n_batches=PRINT_EVERY_N_BATCHES)
# ==============================================================================