固话和检测一张图片的过程(python版本)
import numpy as np
import tensorflow as tf
from cv2 import cv2
from tensorflow.python.framework import graph_util
def freeze_graph():
output_node_names = "MobilenetV1/Predictions/Reshape_1"
meta_path = "E:\\Classify_Data\\Models\\20190830_poker_3232_gray_cls_14_mobivenet\\model.ckpt-3188828"
saver = tf.train.import_meta_graph(meta_path+".meta",clear_devices=True)
with tf.Session() as sess:
saver.restore(sess,meta_path)
output_graph_def = graph_util.convert_variables_to_constants(
sess=sess,
input_graph_def=sess.graph_def,
output_node_names = output_node_names.split(",")
)
pb_name ="xxx/xxx/frozen_graph.pb"
with tf.gfile.GFile(pb_name,"wb") as f:
f.write(output_graph_def.SerializeToString())
for op in sess.graph.get_operations():
print(op.name,op.values())
def interence_one_image(num_pb_path):
with tf.gfile.GFile(num_pb_path,'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def,
input_map=None,
return_elements=None,
name="",
op_dict=None,
producer_op_list=None
)
image_batch = graph.get_tensor_by_name("input:0")
softmax = graph.get_tensor_by_name("MobilenetV1/Predictions/Reshape_1:0")
src = cv2.imread("C:\\Users\\admin_user\\Desktop\\123.jpg",0)
dst=cv2.resize(src,(32,32),0,0)
dst = dst/255-0.5
dst = dst.reshape([1,32,32,1])
with tf.Session(graph=graph) as sess:
results = sess.run(softmax,feed_dict={image_batch:dst})
results = np.squeeze(results)
top_k=results.argsort()[-1:]
print("result:",top_k[0],results[top_k[0]])
if __name__ == '__main__':
num_pb_path = "E:\\Android_Data\\PokerDealer\\AndroidEyesDealer\\app\\src\\main\\assets\\jni\\20191112_quantized_graph_num.pb"
switch_o = "t"
if(switch_o=='t'):
interence_one_image(num_pb_path)
else:
freeze_graph()