-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
64 lines (45 loc) · 1.78 KB
/
test.py
File metadata and controls
64 lines (45 loc) · 1.78 KB
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
# -*- coding: utf-8 -*-
# author: K
import tensorflow as tf
import numpy as np
from os.path import join
from utils.read_config import read_config
from utils.read_data import read_data
from utils.meta_initializer import meta_initializer
from cycle_gan import CycleGAN
flags = tf.app.flags
flags.DEFINE_string('config_path', '', 'The path of the config file')
flags.DEFINE_string('class_config', '', 'The path of the class config')
flags.DEFINE_string('data_dir', '' ,'The image data directory')
flags.DEFINE_string('f', '', 'The condition from class')
flags.DEFINE_string('t', '', 'The condition to class')
flags.DEFINE_string('model_path', '', 'The path of the model')
flags.DEFINE_string('output_dir', '', 'The output directory')
FLAGS = flags.FLAGS
def inference():
configs = read_config(FLAGS.config_path)
class_ids = read_config(FLAGS.class_config)
img_data = read_data(FLAGS.data_dir, int(configs['width']), int(configs['height']))
cyc_GAN = CycleGAN(configs)
cyc_GAN.build_model()
# You must define saver after you build a tensorflow graph
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess, FLAGS.model_path)
meta_data = meta_initializer(int(class_ids[FLAGS.t]), [int(configs['width']), int(configs['height'])])
idx = 0
for img in img_data:
img_vec = np.expand_dims(img, axis = 0)
if int(class_ids[FLAGS.f]) < int(class_ids[FLAGS.t]):
mode = "AtoB"
else:
mode = "BtoA"
output_img = sess.run(cyc_GAN.predict(mode), feed_dict = {cyc_GAN.sample_vector: img_vec, cyc_GAN.sample_meta_data: meta_data})
output_img = sess.run(tf.image.encode_jpeg(tf.squeeze(output_img)))
with open(join(FLAGS.output_dir, str(idx) + '.jpg'), 'wb') as f:
f.write(output_img)
idx += 1
def main(_):
inference()
if __name__ == '__main__':
tf.app.run()