forked from captain-pool/GSOC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
156 lines (140 loc) · 5.43 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
"""
Compressing GANs using Knowledge Distillation.
Teacher GAN: ESRGAN (https://github.com/captain-pool/E2_ESRGAN)
Citation:
@article{DBLP:journals/corr/abs-1902-00159,
author = {Angeline Aguinaldo and
Ping{-}Yeh Chiang and
Alexander Gain and
Ameya Patil and
Kolten Pearson and
Soheil Feizi},
title = {Compressing GANs using Knowledge Distillation},
journal = {CoRR},
volume = {abs/1902.00159},
year = {2019},
url = {http://arxiv.org/abs/1902.00159},
archivePrefix = {arXiv},
eprint = {1902.00159},
timestamp = {Tue, 21 May 2019 18:03:39 +0200},
biburl = {https://dblp.org/rec/bib/journals/corr/abs-1902-00159},
bibsource = {dblp computer science bibliography, https://dblp.org}
}
"""
import os
from absl import logging
import argparse
from libs import lazy_loader
from libs import model
from libs import settings
import tensorflow as tf
def train_and_export(**kwargs):
""" Train and Export Compressed ESRGAN
Args:
config: path to config file.
logdir: path to logging directory
modeldir: Path to store the checkpoints and exported model.
datadir: Path to custom data directory.
manual: Boolean to indicate if `datadir` contains Raw Files(True) / TFRecords (False)
"""
lazy = lazy_loader.LazyLoader()
student_settings = settings.Settings(
kwargs["config"], use_student_settings=True)
# Lazy importing dependencies from teacher
lazy.import_("teacher_imports", parent="libs", return_=False)
lazy.import_("teacher", parent="libs.models", return_=False)
lazy.import_("train", parent="libs", return_=False)
lazy.import_("utils", parent="libs", return_=False)
globals().update(lazy.import_dict)
tf.random.set_seed(10)
teacher_settings = settings.Settings(
student_settings["teacher_config"], use_student_settings=False)
stats = settings.Stats(os.path.join(student_settings.path, "stats.yaml"))
strategy = utils.SingleDeviceStrategy()
if kwargs["tpu"]:
cluster_resolver = tf.distribute.cluster_resolver.TPUClusterResolver(
kwargs["tpu"])
tf.config.experimental_connect_to_host(cluster_resolver.get_master())
tf.tpu.experimental.initialize_tpu_system(cluster_resolver)
strategy = tf.distribute.experimental.TPUStrategy(cluster_resolver)
device_name = utils.assign_to_worker(kwargs["tpu"])
with tf.device(device_name), strategy.scope():
summary_writer = tf.summary.create_file_writer(
os.path.join(kwargs["logdir"], "student"))
teacher_summary_writer = tf.summary.create_file_writer(
os.path.join(kwargs["logdir"], "teacher"))
teacher_generator = teacher.generator(out_channel=3, first_call=False)
teacher_discriminator = teacher.discriminator(
batch_size=teacher_settings["batch_size"])
student_generator = (
model.Registry
.models[student_settings["student_network"]]())
hr_size = tf.cast(tf.convert_to_tensor([1] + student_settings['hr_size']), tf.float32)
lr_size = tf.cast(hr_size * tf.convert_to_tensor([1, 1/4, 1/4, 1]), tf.int32)
logging.debug("Initializing Convolutions")
student_generator.unsigned_call(tf.random.normal(lr_size))
trainer = train.Trainer(
teacher_generator,
teacher_discriminator,
summary_writer,
summary_writer_2=teacher_summary_writer,
model_dir=kwargs["modeldir"],
data_dir=kwargs["datadir"],
strategy=strategy)
phase_name = None
if kwargs["type"].lower().startswith("comparative"):
trainer.train_comparative(
student_generator,
export_only=stats.get("comparative") or kwargs["export_only"])
if not kwargs["export_only"]:
stats["comparative"] = True
elif kwargs["type"].lower().startswith("adversarial"):
trainer.train_adversarial(
student_generator,
export_only=stats.get("adversarial") or kwargs["export_only"])
if not kwargs["export_only"]:
stats["adversarial"] = True
# Tracing Graph to put input signature
_ = student_generator.predict(
tf.random.normal([1, 180, 320, 3]))
tf.saved_model.save(
student_generator,
os.path.join(
kwargs["modeldir"],
"compressed_esrgan"))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--tpu", default=None, help="Name of the TPU to use")
parser.add_argument("--logdir", default=None, help="Path to log directory")
parser.add_argument(
"--export_only",
default=False,
action="store_true",
help="Do not train, only export the model")
parser.add_argument(
"--config",
default="config/config.yaml",
help="path to config file")
parser.add_argument(
"--datadir",
default=None,
help="Path to custom data directory containing sharded TFRecords")
parser.add_argument(
"--modeldir",
default=None,
help="directory to store checkpoints and SavedModel")
parser.add_argument(
"--type",
default=None,
help="Train Student 'adversarial'-ly / 'comparative'-ly")
parser.add_argument(
"--verbose",
"-v",
default=0,
action="count",
help="Increases Verbosity. Repeat to increase more")
FLAGS, unparsed = parser.parse_known_args()
log_levels = [logging.FATAL, logging.WARNING, logging.INFO, logging.DEBUG]
log_level = log_levels[min(FLAGS.verbose, len(log_levels) - 1)]
logging.set_verbosity(log_level)
train_and_export(**vars(FLAGS))