main.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from __future__ import print_function
  2. import os
  3. import argparse
  4. from glob import glob
  5. from PIL import Image
  6. import tensorflow as tf
  7. from model import lowlight_enhance
  8. from utils import *
  9. parser = argparse.ArgumentParser(description='')
  10. parser.add_argument('--use_gpu', dest='use_gpu', type=int, default=1, help='gpu flag, 1 for GPU and 0 for CPU')
  11. parser.add_argument('--gpu_idx', dest='gpu_idx', default="0", help='GPU idx')
  12. parser.add_argument('--gpu_mem', dest='gpu_mem', type=float, default=0.5, help="0 to 1, gpu memory usage")
  13. parser.add_argument('--phase', dest='phase', default='train', help='train or test')
  14. parser.add_argument('--epoch', dest='epoch', type=int, default=100, help='number of total epoches')
  15. parser.add_argument('--batch_size', dest='batch_size', type=int, default=16, help='number of samples in one batch')
  16. parser.add_argument('--patch_size', dest='patch_size', type=int, default=48, help='patch size')
  17. parser.add_argument('--start_lr', dest='start_lr', type=float, default=0.001, help='initial learning rate for adam')
  18. parser.add_argument('--eval_every_epoch', dest='eval_every_epoch', default=20, help='evaluating and saving checkpoints every # epoch')
  19. parser.add_argument('--checkpoint_dir', dest='ckpt_dir', default='./checkpoint', help='directory for checkpoints')
  20. parser.add_argument('--sample_dir', dest='sample_dir', default='./sample', help='directory for evaluating outputs')
  21. parser.add_argument('--save_dir', dest='save_dir', default='./test_results', help='directory for testing outputs')
  22. parser.add_argument('--test_dir', dest='test_dir', default='./data/test/low', help='directory for testing inputs')
  23. parser.add_argument('--decom', dest='decom', default=0, help='decom flag, 0 for enhanced results only and 1 for decomposition results')
  24. args = parser.parse_args()
  25. def lowlight_train(lowlight_enhance):
  26. if not os.path.exists(args.ckpt_dir):
  27. os.makedirs(args.ckpt_dir)
  28. if not os.path.exists(args.sample_dir):
  29. os.makedirs(args.sample_dir)
  30. lr = args.start_lr * np.ones([args.epoch])
  31. lr[20:] = lr[0] / 10.0
  32. train_low_data = []
  33. train_high_data = []
  34. train_low_data_names = glob('./data/our485/low/*.png') + glob('./data/syn/low/*.png')
  35. train_low_data_names.sort()
  36. train_high_data_names = glob('./data/our485/high/*.png') + glob('./data/syn/high/*.png')
  37. train_high_data_names.sort()
  38. assert len(train_low_data_names) == len(train_high_data_names)
  39. print('[*] Number of training data: %d' % len(train_low_data_names))
  40. for idx in range(len(train_low_data_names)):
  41. low_im = load_images(train_low_data_names[idx])
  42. train_low_data.append(low_im)
  43. high_im = load_images(train_high_data_names[idx])
  44. train_high_data.append(high_im)
  45. eval_low_data = []
  46. eval_high_data = []
  47. eval_low_data_name = glob('./data/eval/low/*.*')
  48. for idx in range(len(eval_low_data_name)):
  49. eval_low_im = load_images(eval_low_data_name[idx])
  50. eval_low_data.append(eval_low_im)
  51. lowlight_enhance.train(train_low_data, train_high_data, eval_low_data, batch_size=args.batch_size, patch_size=args.patch_size, epoch=args.epoch, lr=lr, sample_dir=args.sample_dir, ckpt_dir=os.path.join(args.ckpt_dir, 'Decom'), eval_every_epoch=args.eval_every_epoch, train_phase="Decom")
  52. lowlight_enhance.train(train_low_data, train_high_data, eval_low_data, batch_size=args.batch_size, patch_size=args.patch_size, epoch=args.epoch, lr=lr, sample_dir=args.sample_dir, ckpt_dir=os.path.join(args.ckpt_dir, 'Relight'), eval_every_epoch=args.eval_every_epoch, train_phase="Relight")
  53. def lowlight_test(lowlight_enhance):
  54. if args.test_dir == None:
  55. print("[!] please provide --test_dir")
  56. exit(0)
  57. if not os.path.exists(args.save_dir):
  58. os.makedirs(args.save_dir)
  59. test_low_data_name = glob(os.path.join(args.test_dir) + '/*.*')
  60. test_low_data = []
  61. test_high_data = []
  62. for idx in range(len(test_low_data_name)):
  63. test_low_im = load_images(test_low_data_name[idx])
  64. test_low_data.append(test_low_im)
  65. lowlight_enhance.test(test_low_data, test_high_data, test_low_data_name, save_dir=args.save_dir, decom_flag=args.decom)
  66. def main(_):
  67. if args.use_gpu:
  68. print("[*] GPU\n")
  69. os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_idx
  70. gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_mem)
  71. with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
  72. model = lowlight_enhance(sess)
  73. if args.phase == 'train':
  74. lowlight_train(model)
  75. elif args.phase == 'test':
  76. lowlight_test(model)
  77. else:
  78. print('[!] Unknown phase')
  79. exit(0)
  80. else:
  81. print("[*] CPU\n")
  82. with tf.Session() as sess:
  83. model = lowlight_enhance(sess)
  84. if args.phase == 'train':
  85. lowlight_train(model)
  86. elif args.phase == 'test':
  87. lowlight_test(model)
  88. else:
  89. print('[!] Unknown phase')
  90. exit(0)
  91. if __name__ == '__main__':
  92. tf.app.run()