如何分析在tf-serving上运行的tensorflow模型?

时间:2019-01-25 07:28:13

标签: tensorflow profiling tensorflow-serving

据我所知,当我在python脚本上运行tensorflow模型时,我可以使用以下代码片段来描述模型中每个块的时间轴。

from tensorflow.python.client import timeline

options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()
batch_positive_score = sess.run([positive_score], feed_dict, options=options, run_metadata=run_metadata)
fetched_timeline = timeline.Timeline(run_metadata.step_stats)
chrome_trace = fetched_timeline.generate_chrome_trace_format()
with open('./result/timeline.json', 'w') as f:
    f.write(chrome_trace)

但是如何分析在张量流服务中加载的模型?

1 个答案:

答案 0 :(得分:0)

我认为您甚至可以在服务期间使用tf.profiler,因为它最终是一个Tensorflow图,并且在培训期间所做的更改(根据我的理解,包括分析)也将反映在服务中。

请找到以下Tensorflow代码:

# User can control the tracing steps and
# dumping steps. User can also run online profiling during training.
#
# Create options to profile time/memory as well as parameters.
builder = tf.profiler.ProfileOptionBuilder
opts = builder(builder.time_and_memory()).order_by('micros').build()
opts2 = tf.profiler.ProfileOptionBuilder.trainable_variables_parameter()

# Collect traces of steps 10~20, dump the whole profile (with traces of
# step 10~20) at step 20. The dumped profile can be used for further profiling
# with command line interface or Web UI.
with tf.contrib.tfprof.ProfileContext('/tmp/train_dir',
                                      trace_steps=range(10, 20),
                                      dump_steps=[20]) as pctx:
  # Run online profiling with 'op' view and 'opts' options at step 15, 18, 20.
  pctx.add_auto_profiling('op', opts, [15, 18, 20])
  # Run online profiling with 'scope' view and 'opts2' options at step 20.
  pctx.add_auto_profiling('scope', opts2, [20])
  # High level API, such as slim, Estimator, etc.
  train_loop()

之后,我们可以在命令提示符下运行以下提到的命令:

bazel-bin/tensorflow/core/profiler/profiler \
    --profile_path=/tmp/train_dir/profile_xx
tfprof> op -select micros,bytes,occurrence -order_by micros

# Profiler ui available at: https://github.com/tensorflow/profiler-ui
python ui.py --profile_context_path=/tmp/train_dir/profile_xx

可视化时间和记忆的代码:

# The following example generates a timeline.
tfprof> graph -step -1 -max_depth 100000 -output timeline:outfile=<filename>

generating trace file.

******************************************************
Timeline file is written to <filename>.
Open a Chrome browser, enter URL chrome://tracing and load the timeline file.
******************************************************

将TensorFlow图的运行时间分配给您的Python代码:

tfprof> code -max_depth 1000 -show_name_regexes .*model_analyzer.*py.* -select micros -account_type_regexes .* -order_by micros

显示模型变量和参数数量:

tfprof> scope -account_type_regexes VariableV2 -max_depth 4 -select params

显示最昂贵的操作类型:

tfprof> op -select micros,bytes,occurrence -order_by micros

自动配置文件:

tfprof> advise

有关此的更多详细信息,您可以参考以下链接:

了解此页中提到的所有类=>

https://www.tensorflow.org/api_docs/python/tf/profiler

下面的链接中详细给出了代码:

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/profiler/README.md