1、使用tensorflow-yolo-v3中的convert_weights_pb.py转tensorflow模型
转换命令:
python convert_weights_pb.py --class_names /home/***/text.names --weights_file /home/***/yolov3.weights --data_format NHWC --output_graph /home/***/tensorflow-yolo-v3_convert/output_yolo.pb
2、下载最新的TensorFlow库
git clone https://github.com/tensorflow/tensorflow.git
3、编译压缩模块
cd tensorflow
bazel build tensorflow/tools/graph_transforms:transform_graph
压缩命令
bazel-bin/tensorflow/tools/graph_transforms/transform_graph --in_graph=/home/***/tensorflow-yolo-v3/output_yolo.pb --out_graph=/home/***/tensorflow-yolo-v3/yolov3_optimized_frozen_inference_graph.pb --inputs="inputs" --outputs="output_boxes" --transforms='fold_old_batch_norms quantize_weights strip_unused_nodes sort_by_execution_order merge_duplicate_nodes'
其中in_graph是使用tensorflow-yolo-v3中的convert_weights_pb.py转换之后的模型路径,out_graph是输出路径
注意:使用tensorflow-yolo-v3转换以后的pb模型,最终输出的xmin、ymin、xmax、ymax,可能由于网络结构的原因,可能最终的输出的框会有偏移和缩放,由于压缩以后的模型yolov3_optimized_frozen_inference_graph.pb是output_yolo.pb压缩来的,所以压缩模型的输出框也会有偏移,如果结果有偏移,可以使用乘以相应的系数来处理
对偏移做的处理时,分别乘以2.4605749、2.4651933、2.4610288、2.4612722
xmin = pred[:, 0] * 2.4605749 # *2.47596
xmax = pred[:, 2] * 2.4651933 # *2.43272
ymin = pred[:, 1] * 2.4610288 # *2.45355
ymax = pred[:, 3] * 2.4612722 # *2.45412
scores = pred[:, 4]
压缩以后的yolo模型推理速度能提升6倍左右,推理精度和准确率下降1%左右
这个是导入压缩以后pb模型,以及相应推理的代码
def load_detection_model(model):
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(model, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
image_tensor = tf.get_default_graph().get_tensor_by_name('inputs:0')
boxes_tensor = tf.get_default_graph().get_tensor_by_name('output_boxes:0')
sessd=tf.Session(config=config,graph=detection_graph)
print('Loaded detection model from file "%s"' % model)
return sessd,image_tensor,boxes_tensor
def detection(image,sess,image_tensor,boxes_tensor):
output_dict = sess.run(boxes_tensor,feed_dict={image_tensor: image})
return output_dict
inputimage = cv2.resize(img, (416, 416))
input = np.expand_dims(inputimage, 0)
outputdic = detection(input, sessd, image_tensor, boxes_tensor)
pred = np.squeeze(outputdic)
xmin = predict[:, 0] * 2.4605749 # *2.47596
xmax = predict[:, 2] * 2.4651933 # *2.43272
ymin = predict[:, 1] * 2.4610288 # *2.45355
ymax = predict[:, 3] * 2.4612722 # *2.45412
scores = predict[:, 4]
indexs = np.where(predict[:, 5] > predict[:, 6])[0]
scores = scores[indexs]
xmin = xmin[indexs]
xmax = xmax[indexs]
ymin = ymin[indexs]
ymax = ymax[indexs]