【发布时间】:2019-02-20 23:55:10
【问题描述】:
我想优化我的冷冻训练 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())
我从https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/graph_transforms/README.md#strip_unused_nodes 中找到了transform_graph 来优化我的冻结模型。我能够成功地为我的对象检测模型生成一个有效的优化模型。生成模型的优化版本的目的是提高模型的推理速度。我在 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)'
所以我的问题是:
- 转换有什么作用?
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库是否不再存在?
【问题讨论】:
标签: python macos tensorflow