-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbaseline.py
64 lines (51 loc) · 2.16 KB
/
baseline.py
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
import tensorflow as tf
import numpy as np
import os
import cv2
import argparse
parser = argparse.ArgumentParser(description='manual to this script')
parser.add_argument('--gpu', type=str, default='0', help="choose a GPU")
parser.add_argument('--input', type=str, default=None, help="the path of the content image")
parser.add_argument('--output', type=str, default=None, help="the path to save the output image")
parser.add_argument('--model', type=str, default=None, help="the path of the model")
args = parser.parse_args()
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
def unpadding(image, padding):
width, height = image.shape[1], image.shape[0]
image = image[padding:height - padding, padding:width - padding]
return image
def padding(image, padding):
new_image = cv2.copyMakeBorder(image, padding, padding, padding, padding, cv2.BORDER_REFLECT)
return new_image
def style_transfer(image, sess):
with sess.graph.as_default():
with sess.as_default():
input_op = sess.graph.get_tensor_by_name("input:0")
output_op = sess.graph.get_tensor_by_name("output:0")
image_output = sess.run(output_op, feed_dict={
input_op: [cv2.cvtColor(image, cv2.COLOR_BGR2RGB)]
})[0]
image_output = np.clip(image_output, 0, 255).astype(np.uint8)
image_output = cv2.cvtColor(image_output, cv2.COLOR_RGB2BGR)
return image_output
def load_model(model_name):
graph = tf.Graph()
sess = tf.InteractiveSession(graph=graph)
with sess.graph.as_default():
with sess.as_default():
tf.global_variables_initializer().run()
with tf.gfile.FastGFile(model_name, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')
return sess
def baseline(image, sess):
image = padding(image, padding=40)
image = style_transfer(image, sess)
image = unpadding(image, padding=40)
return image
if __name__ == '__main__':
sess = load_model(args.model)
image = cv2.imread(args.input)
image = baseline(image, sess)
cv2.imwrite(args.output, image)