-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
46 lines (40 loc) · 1.42 KB
/
predict.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
import tensorflow as tf
import argparse
from image_utils import *
#加载固化模型
def load_pb(frozen_graph_filename):
with tf.Graph().as_default():
output_graph_def = tf.GraphDef()
with open(frozen_graph_filename, "rb") as f:
output_graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(output_graph_def, name="")
# We load the graph_def in the default graph
with tf.Graph().as_default() as graph:
tf.import_graph_def(
output_graph_def,
input_map=None,
return_elements=None,
name="prefix",
op_dict=None,
producer_op_list=None
)
return graph
def predict(img):
graph = load_pb('ckpt/nsfw_freeze.pb')
# textcnn 预测
x = graph.get_tensor_by_name('prefix/input:0')
#logits =graph.get_tensor_by_name('prefix/logits:0')
predictions =graph.get_tensor_by_name('prefix/predictions:0')
with tf.Session(graph=graph) as sess:
fn_load_image = create_tensorflow_image_loader(sess)
image = fn_load_image(img)
pred= sess.run([predictions], feed_dict={x:image})
print(pred)
if __name__=="__main__":
parser=argparse.ArgumentParser()
parser.add_argument('-i','--img',help='图片地址')
args=parser.parse_args()
if args.img=='':
print('请先输入图片')
else:
predict(args.img)