main.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # !/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # ==============================================================================
  4. #
  5. # Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. # ==============================================================================
  20. import os
  21. import warnings
  22. warnings.simplefilter("ignore")
  23. import tensorflow as tf
  24. import horovod.tensorflow as hvd
  25. from utils import hvd_utils
  26. from runtime import Runner
  27. from utils.cmdline_helper import parse_cmdline
  28. from utils.logging import init_dllogger
  29. if __name__ == "__main__":
  30. tf.logging.set_verbosity(tf.logging.ERROR)
  31. os.environ["TF_EXTRA_PTXAS_OPTIONS"] = "-sw200428197=true" # TODO: NINJA WAR
  32. FLAGS = parse_cmdline()
  33. init_dllogger(FLAGS.log_dir)
  34. RUNNING_CONFIG = tf.contrib.training.HParams(
  35. exec_mode=FLAGS.exec_mode,
  36. save_eval_results_to_json=FLAGS.save_eval_results_to_json,
  37. # ======= Directory HParams ======= #
  38. log_dir=os.path.join(FLAGS.results_dir, "logs"),
  39. model_dir=os.path.join(FLAGS.results_dir, "checkpoints"),
  40. summaries_dir=os.path.join(FLAGS.results_dir, "summaries"),
  41. sample_dir=os.path.join(FLAGS.results_dir, "samples"),
  42. data_dir=FLAGS.data_dir,
  43. dataset_name=FLAGS.dataset_name,
  44. dataset_hparams=dict(),
  45. # ========= Model HParams ========= #
  46. unet_variant=FLAGS.unet_variant,
  47. activation_fn=FLAGS.activation_fn,
  48. input_format='NHWC',
  49. compute_format=FLAGS.data_format,
  50. input_shape=(512, 512),
  51. mask_shape=(512, 512),
  52. n_channels=1,
  53. input_normalization_method="zero_one",
  54. # ======== Runtime HParams ======== #
  55. amp=FLAGS.amp,
  56. xla=FLAGS.xla,
  57. # ======= Training HParams ======== #
  58. iter_unit=FLAGS.iter_unit,
  59. num_iter=FLAGS.num_iter,
  60. warmup_steps=FLAGS.warmup_step,
  61. batch_size=FLAGS.batch_size,
  62. learning_rate=FLAGS.learning_rate,
  63. learning_rate_decay_factor=FLAGS.learning_rate_decay_factor,
  64. learning_rate_decay_steps=FLAGS.learning_rate_decay_steps,
  65. rmsprop_decay=FLAGS.rmsprop_decay,
  66. rmsprop_momentum=FLAGS.rmsprop_momentum,
  67. weight_decay=FLAGS.weight_decay,
  68. use_auto_loss_scaling=FLAGS.use_auto_loss_scaling,
  69. loss_fn_name=FLAGS.loss_fn_name,
  70. augment_data=FLAGS.augment_data,
  71. weight_init_method=FLAGS.weight_init_method,
  72. # ======== Debug Flags ======== #
  73. # 0: No debug
  74. # 1: Layer Creation Debug Info
  75. # 2: Layer + Var Creation Debug Info
  76. debug_verbosity=FLAGS.debug_verbosity,
  77. log_every_n_steps=FLAGS.display_every,
  78. seed=FLAGS.seed,
  79. )
  80. # ===================================
  81. if RUNNING_CONFIG.dataset_name == "DAGM2007":
  82. RUNNING_CONFIG.dataset_hparams["class_id"] = FLAGS.dataset_classID
  83. runner = Runner(
  84. input_format=RUNNING_CONFIG.input_format,
  85. compute_format=RUNNING_CONFIG.compute_format,
  86. n_channels=RUNNING_CONFIG.n_channels,
  87. model_variant=RUNNING_CONFIG.unet_variant,
  88. activation_fn=RUNNING_CONFIG.activation_fn,
  89. input_shape=RUNNING_CONFIG.input_shape,
  90. mask_shape=RUNNING_CONFIG.mask_shape,
  91. input_normalization_method=RUNNING_CONFIG.input_normalization_method,
  92. # Training HParams
  93. augment_data=RUNNING_CONFIG.augment_data,
  94. loss_fn_name=RUNNING_CONFIG.loss_fn_name,
  95. weight_init_method=RUNNING_CONFIG.weight_init_method,
  96. # Runtime HParams
  97. amp=RUNNING_CONFIG.amp,
  98. xla=RUNNING_CONFIG.xla,
  99. # Directory Params
  100. log_dir=RUNNING_CONFIG.log_dir,
  101. model_dir=RUNNING_CONFIG.model_dir,
  102. sample_dir=RUNNING_CONFIG.sample_dir,
  103. data_dir=RUNNING_CONFIG.data_dir,
  104. dataset_name=RUNNING_CONFIG.dataset_name,
  105. dataset_hparams=RUNNING_CONFIG.dataset_hparams,
  106. # Debug Params
  107. debug_verbosity=RUNNING_CONFIG.debug_verbosity,
  108. log_every_n_steps=RUNNING_CONFIG.log_every_n_steps,
  109. seed=RUNNING_CONFIG.seed
  110. )
  111. if RUNNING_CONFIG.exec_mode in ["train", "train_and_evaluate", "training_benchmark"]:
  112. runner.train(
  113. iter_unit=RUNNING_CONFIG.iter_unit,
  114. num_iter=RUNNING_CONFIG.num_iter,
  115. batch_size=RUNNING_CONFIG.batch_size,
  116. warmup_steps=RUNNING_CONFIG.warmup_steps,
  117. weight_decay=RUNNING_CONFIG.weight_decay,
  118. learning_rate=RUNNING_CONFIG.learning_rate,
  119. learning_rate_decay_factor=RUNNING_CONFIG.learning_rate_decay_factor,
  120. learning_rate_decay_steps=RUNNING_CONFIG.learning_rate_decay_steps,
  121. rmsprop_decay=RUNNING_CONFIG.rmsprop_decay,
  122. rmsprop_momentum=RUNNING_CONFIG.rmsprop_momentum,
  123. use_auto_loss_scaling=FLAGS.use_auto_loss_scaling,
  124. augment_data=RUNNING_CONFIG.augment_data,
  125. is_benchmark=RUNNING_CONFIG.exec_mode == 'training_benchmark'
  126. )
  127. if RUNNING_CONFIG.exec_mode in ["train_and_evaluate", 'evaluate', 'inference_benchmark'] and hvd.rank() == 0:
  128. runner.evaluate(
  129. iter_unit=RUNNING_CONFIG.iter_unit if RUNNING_CONFIG.exec_mode != "train_and_evaluate" else "epoch",
  130. num_iter=RUNNING_CONFIG.num_iter if RUNNING_CONFIG.exec_mode != "train_and_evaluate" else 1,
  131. warmup_steps=RUNNING_CONFIG.warmup_steps,
  132. batch_size=RUNNING_CONFIG.batch_size,
  133. is_benchmark=RUNNING_CONFIG.exec_mode == 'inference_benchmark',
  134. save_eval_results_to_json=RUNNING_CONFIG.save_eval_results_to_json
  135. )