我想优化经过冻结训练的Tensorflow模型。但是,我发现该optimize_for_inference
库不再可用。
import tensorflow as tf
from tensorflow.python.tools import freeze_graph
from tensorflow.python.tools import optimize_for_inference_lib
input_graph_def = tf.GraphDef()
with tf.gfile.Open("./inference_graph/frozen_model.pb", "rb") as f:
data = f.read()
input_graph_def.ParseFromString(data)
output_graph_def = optimize_for_inference_lib.optimize_for_inference(
input_graph_def,
["image_tensor"], ## input
["'detection_boxes, detection_scores, detection_classes, num_detections"], ## outputs
tf.float32.as_datatype_enum)
f = tf.gfile.FastGFile("./optimized_model.pb", "wb")
f.write(output_graph_def.SerializeToString())
Run Code Online (Sandbox Code Playgroud)
我transform_graph
从https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/graph_transforms/README.md#strip_unused_nodes找到了优化我的冻结模型。我能够成功地为我的对象检测模型生成一个可以工作的优化模型。生成模型的优化版本的目的是提高模型的推理速度。我在bash(/ tensorflow根目录)中输入了以下代码:
bazel-bin/tensorflow/tools/graph_transforms/transform_graph \
--in_graph=/Users/cvsanbuenaventura/Documents/tensorflow_fastlog/models/research/object_detection/inference_graph/frozen_inference_graph.pb \
--out_graph=/Users/cvsanbuenaventura/Documents/tensorflow_fastlog/models/research/object_detection/inference_graph/optimized_inference_graph-transform_graph-manyoutputs-planA2-v2.pb \
--inputs='image_tensor' \
--outputs='detection_boxes, detection_scores, detection_classes, num_detections' \
--transforms='fold_batch_norms
fold_old_batch_norms
fold_constants(ignore_errors=true)'
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
fold_batch_norms, fold_old_batch_norms, fold_constants(ignore_errors=true)
strip_unused_nodes(type=float, shape="1,299,299,3")
)。这是做什么的?我应该在这里放什么形状?optimize_for_inference
库不存在了吗?