wahaha commited on
Commit
50b8f2b
1 Parent(s): 5d68cfc
Files changed (1) hide show
  1. test.py +81 -0
test.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ from tools.utils import *
3
+ import os
4
+ from tqdm import tqdm
5
+ from glob import glob
6
+ import time
7
+ import numpy as np
8
+ from net import generator
9
+ os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
10
+
11
+ def parse_args():
12
+ desc = "AnimeGANv2"
13
+ parser = argparse.ArgumentParser(description=desc)
14
+
15
+ parser.add_argument('--checkpoint_dir', type=str, default='checkpoint/'+'generator_Shinkai_weight',
16
+ help='Directory name to save the checkpoints')
17
+ parser.add_argument('--test_dir', type=str, default='dataset/test/t',
18
+ help='Directory name of test photos')
19
+ parser.add_argument('--save_dir', type=str, default='Shinkai/t',
20
+ help='what style you want to get')
21
+ parser.add_argument('--if_adjust_brightness', type=bool, default=True,
22
+ help='adjust brightness by the real photo')
23
+
24
+ """checking arguments"""
25
+
26
+ return parser.parse_args()
27
+
28
+ def stats_graph(graph):
29
+ flops = tf.profiler.profile(graph, options=tf.profiler.ProfileOptionBuilder.float_operation())
30
+ # params = tf.profiler.profile(graph, options=tf.profiler.ProfileOptionBuilder.trainable_variables_parameter())
31
+ print('FLOPs: {}'.format(flops.total_float_ops))
32
+
33
+ def test(checkpoint_dir, style_name, test_dir, if_adjust_brightness, img_size=[256,256]):
34
+ # tf.reset_default_graph()
35
+ result_dir = 'results/'+style_name
36
+ check_folder(result_dir)
37
+ test_files = [test_dir]
38
+
39
+ test_real = tf.placeholder(tf.float32, [1, None, None, 3], name='test')
40
+
41
+ with tf.variable_scope("generator", reuse=False):
42
+ test_generated = generator.G_net(test_real).fake
43
+ saver = tf.train.Saver()
44
+
45
+ out_paths = []
46
+
47
+ gpu_options = tf.GPUOptions(allow_growth=True)
48
+ with tf.Session(config=tf.ConfigProto(allow_soft_placement=True, gpu_options=gpu_options)) as sess:
49
+ # tf.global_variables_initializer().run()
50
+ # load model
51
+ ckpt = tf.train.get_checkpoint_state(checkpoint_dir) # checkpoint file information
52
+ if ckpt and ckpt.model_checkpoint_path:
53
+ ckpt_name = os.path.basename(ckpt.model_checkpoint_path) # first line
54
+ saver.restore(sess, os.path.join(checkpoint_dir, ckpt_name))
55
+ print(" [*] Success to read {}".format(os.path.join(checkpoint_dir, ckpt_name)))
56
+ else:
57
+ print(" [*] Failed to find a checkpoint")
58
+ return
59
+ # stats_graph(tf.get_default_graph())
60
+
61
+ begin = time.time()
62
+ for sample_file in tqdm(test_files) :
63
+ # print('Processing image: ' + sample_file)
64
+ sample_image = np.asarray(load_test_data(sample_file, img_size))
65
+ image_path = os.path.join(result_dir,'{0}'.format(os.path.basename(sample_file)))
66
+ fake_img = sess.run(test_generated, feed_dict = {test_real : sample_image})
67
+ if if_adjust_brightness:
68
+ save_images(fake_img, image_path, sample_file)
69
+ else:
70
+ save_images(fake_img, image_path, None)
71
+
72
+ out_paths.push(image_path)
73
+ end = time.time()
74
+ print(f'test-time: {end-begin} s')
75
+
76
+ return out_paths
77
+
78
+ if __name__ == '__main__':
79
+ arg = parse_args()
80
+ print(arg.checkpoint_dir)
81
+ test(arg.checkpoint_dir, arg.save_dir, arg.test_dir, arg.if_adjust_brightness)