|
# |
|
# Copyright 2017 Jongwook Choi. |
|
# |
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
# you may not use this file except in compliance with the License. |
|
# You may obtain a copy of the License at |
|
# |
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
# |
|
# Unless required by applicable law or agreed to in writing, software |
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
# See the License for the specific language governing permissions and |
|
# limitations under the License. |
|
|
|
""" Example main.py for managing configuration. """ |
|
|
|
from __future__ import absolute_import |
|
from __future__ import division |
|
from __future__ import print_function |
|
|
|
import argparse |
|
import tensorflow as tf |
|
|
|
|
|
class Config(argparse.Namespace): |
|
""" Configuration of models and training, etc. """ |
|
|
|
@staticmethod |
|
def get_parser(parser=None): |
|
if parser is None: |
|
parser = argparse.ArgumentParser(description=__doc__) |
|
parser.add_argument('--train-dir', default=None) |
|
parser.add_argument('--max-steps', default=10, type=int) |
|
parser.add_argument('--batch_size', default=64, type=int) |
|
parser.add_argument('--dataset', default='mnist', choices=['mnist', 'cifar']) |
|
parser.add_argument('--image-size', default='[64, 64]', type=str) |
|
parser.add_argument('--learning-rate', default=1e-4, type=float) |
|
return parser |
|
|
|
def __init__(self, **kwargs): |
|
super(Config, self).__init__(**kwargs) |
|
|
|
self.image_size = eval(self.image_size) |
|
assert isinstance(self.image_size, (tuple, list)) |
|
|
|
# assign an random id |
|
import uuid |
|
self.train_id = uuid.uuid1().hex[:6] |
|
|
|
if self.train_dir is None: |
|
self.train_dir = "./logs/example-%s" % self.train_id |
|
print("Train Dir: %s" % self.train_dir) |
|
tf.gfile.MakeDirs(self.train_dir) |
|
|
|
def as_text_matrix(self): |
|
return [[k, str(w)] for k, w in sorted(vars(self).items())] |
|
|
|
|
|
class Trainer(object): |
|
""" Main training loop of TensorFlow application. """ |
|
|
|
def __init__(self, config): |
|
self.config = config |
|
self.summary_writer = tf.summary.FileWriter(config.train_dir) |
|
self.session = tf.InteractiveSession() # FIXME: or a regular session |
|
|
|
def run(self): |
|
""" Run main training loops. """ |
|
self.summary_op = tf.summary.merge_all() |
|
|
|
# Add configuration matrix as a text-summary into TensorBoard. |
|
config_summary = tf.summary.text('TrainConfig', tf.convert_to_tensor(self.config.as_text_matrix()), collections=[]) |
|
self.summary_writer.add_summary(config_summary.eval(session=self.session)) |
|
# Note: the following should be called after all text summaries are configured |
|
self.summary_writer.add_graph(tf.get_default_graph()) |
|
|
|
# FIXME: The rest of code follows. |
|
self.session.run(tf.global_variables_initializer()) |
|
print("[*] Training starts!") |
|
|
|
|
|
def main(): |
|
parser = Config.get_parser() |
|
args = parser.parse_args() |
|
config = Config(**vars(args)) |
|
|
|
print("============== Configuration ==============") |
|
print(config) |
|
|
|
print("============== Training Starts ==============") |
|
Trainer(config).run() |
|
|
|
|
|
if __name__ == '__main__': |
|
main() |