Tensorflow分析时间轴结果不一致

时间:2018-07-24 12:56:54

标签: tensorflow profiling

我想使用时间轴功能来分析我的Tensorflow模型。 问题是我得到的总运行时结果不一致。

上班时间

除了Tesnorflow时间轴之外,我还使用time.time()来测量运行时间。

第一次sess.run大约需要1.6秒
我的第二次sess.run大约需要0.03秒

这很有意义,因为Tensorflow首先会初始化一些与优化相关的东西。但是我在时间轴跟踪图中看不到。

时间轴

enter image description here

每次运行大约需要0.3s,而不是1.6s和0.03s。

代码

我之前已经加载了图表。

import os
import tempfile
import json
import tensorflow as tf
from tensorflow.python.client import timeline
import time

class TimeLiner:
    _timeline_dict = None

    def update_timeline(self, chrome_trace):
        # convert crome trace to python dict
        chrome_trace_dict = json.loads(chrome_trace)
        # for first run store full trace
        if self._timeline_dict is None:
            self._timeline_dict = chrome_trace_dict
        # for other - update only time consumption, not definitions
        else:
            for event in chrome_trace_dict['traceEvents']:
                # events time consumption started with 'ts' prefix
                if 'ts' in event:
                    self._timeline_dict['traceEvents'].append(event)

    def save(self, f_name):
        with open(f_name, 'w') as f:
            json.dump(self._timeline_dict, f)


with tf.Session(graph=graph) as sess:
    # add additional options to trace the session execution
    options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
    run_metadata = tf.RunMetadata()

    input = graph.get_tensor_by_name("inference/input_1_1:0")
    output = graph.get_tensor_by_name("inference/output_node0:0")

    many_runs_timeline = TimeLiner()
    for i in range(2):
        start_time = time.time()
        print(len(sess.run(output,feed_dict={input: np.ones((1,256,640,3))}, options=options, run_metadata=run_metadata)))
        stop_time = time.time()
        print(stop_time - start_time)  

        # Create the Timeline object, and write it to a json file
        fetched_timeline = timeline.Timeline(run_metadata.step_stats)
        chrome_trace = fetched_timeline.generate_chrome_trace_format(show_memory=True,
                                            show_dataflow=True)
        many_runs_timeline.update_timeline(chrome_trace)

    many_runs_timeline.save('timeline_merged2.json')

0 个答案:

没有答案