<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>预热 &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/%E9%A2%84%E7%83%AD/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Mon, 27 Apr 2020 18:00:16 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创] 加快TensorFlow在树莓派上的执行速度——模型预热</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%8a%a0%e5%bf%abtensorflow%e5%9c%a8%e6%a0%91%e8%8e%93%e6%b4%be%e4%b8%8a%e7%9a%84%e6%89%a7%e8%a1%8c%e9%80%9f%e5%ba%a6-%e6%a8%a1%e5%9e%8b%e9%a2%84%e7%83%ad/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%8a%a0%e5%bf%abtensorflow%e5%9c%a8%e6%a0%91%e8%8e%93%e6%b4%be%e4%b8%8a%e7%9a%84%e6%89%a7%e8%a1%8c%e9%80%9f%e5%ba%a6-%e6%a8%a1%e5%9e%8b%e9%a2%84%e7%83%ad/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Sat, 10 Dec 2016 04:42:08 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Raspberry Pi/树莓派]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Raspberry Pi]]></category>
		<category><![CDATA[TensorFlow]]></category>
		<category><![CDATA[warm-up]]></category>
		<category><![CDATA[树莓派]]></category>
		<category><![CDATA[模型]]></category>
		<category><![CDATA[深度学习]]></category>
		<category><![CDATA[预热]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=8984</guid>

					<description><![CDATA[<p>
本文软硬件环境：<br />
树莓派：3代 Model B V1.2，内存1GB<br />
OS：Arch Linux ARM</p>
<p>在<a href="http://www.codelast.com/?p=8941" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">上一篇文章</span></a>中，我写了在树莓派上用TensorFlow做的一个深度学习（图像识别）实验，但正如文中所说，50秒执行一次预测的实用性为0。因此，有必要采取一些措施来加快TensorFlow的执行速度，其中一个可行的方法就是&#8220;预热&#8221;（warm-up），把TensorFlow移植到树莓派上的作者Sam Abrahams已经比较详细地在GitHub上列出了<a href="https://github.com/samjabrahams/tensorflow-on-raspberry-pi/tree/master/benchmarks/inceptionv3" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">性能测试的结果</span></a>。依照作者的描述，我也测试了一下，看看那悲催的50秒时间能减少到多少秒。<br />
<span id="more-8984"></span><br />
<span style="background-color:#00ff00;">『1』</span>什么是预热（warm-up）<br />
首先，本文还是对TensorFlow的Python图像分类程序 <span style="color:#ff0000;">classify_image.py</span> 来描述的。<br />
预热就是指在真正执行一次预测之前，先执行若干次 <span style="color:#0000ff;">Session.run()&#160;</span>方法，从而达到加快一次预测的执行速度的目的。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color:#00ff00;">『2』</span>代码修改<br />
代码改起来其实很简单。为了能衡量程序运行时间，需要使用Python的time模块，因此在一开始需要import：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="python language-python hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&#160;time
</code></pre>
</section>
<p>然后对 <span style="color:#0000ff;">run_inference_on_image</span> 方法做一些修改，如下：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="python language-python hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">def</span>&#160;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">run_inference_on_image</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(image)</span>:</span>
&#160;&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;&#34;&#34;Runs&#160;inference&#160;on&#160;an&#160;image.

&#160;&#160;Args:
&#160;&#160;&#160;&#160;image:&#160;Image&#160;file&#160;name.</span></code></pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%8a%a0%e5%bf%abtensorflow%e5%9c%a8%e6%a0%91%e8%8e%93%e6%b4%be%e4%b8%8a%e7%9a%84%e6%89%a7%e8%a1%8c%e9%80%9f%e5%ba%a6-%e6%a8%a1%e5%9e%8b%e9%a2%84%e7%83%ad/" class="read-more">Read More </a></section>]]></description>
										<content:encoded><![CDATA[<p>
本文软硬件环境：<br />
树莓派：3代 Model B V1.2，内存1GB<br />
OS：Arch Linux ARM</p>
<p>在<a href="http://www.codelast.com/?p=8941" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">上一篇文章</span></a>中，我写了在树莓派上用TensorFlow做的一个深度学习（图像识别）实验，但正如文中所说，50秒执行一次预测的实用性为0。因此，有必要采取一些措施来加快TensorFlow的执行速度，其中一个可行的方法就是&ldquo;预热&rdquo;（warm-up），把TensorFlow移植到树莓派上的作者Sam Abrahams已经比较详细地在GitHub上列出了<a href="https://github.com/samjabrahams/tensorflow-on-raspberry-pi/tree/master/benchmarks/inceptionv3" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">性能测试的结果</span></a>。依照作者的描述，我也测试了一下，看看那悲催的50秒时间能减少到多少秒。<br />
<span id="more-8984"></span><br />
<span style="background-color:#00ff00;">『1』</span>什么是预热（warm-up）<br />
首先，本文还是对TensorFlow的Python图像分类程序 <span style="color:#ff0000;">classify_image.py</span> 来描述的。<br />
预热就是指在真正执行一次预测之前，先执行若干次 <span style="color:#0000ff;">Session.run()&nbsp;</span>方法，从而达到加快一次预测的执行速度的目的。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color:#00ff00;">『2』</span>代码修改<br />
代码改起来其实很简单。为了能衡量程序运行时间，需要使用Python的time模块，因此在一开始需要import：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="python language-python hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;time
</code></pre>
</section>
<p>然后对 <span style="color:#0000ff;">run_inference_on_image</span> 方法做一些修改，如下：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="python language-python hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">def</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">run_inference_on_image</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(image)</span>:</span>
&nbsp;&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;&quot;&quot;Runs&nbsp;inference&nbsp;on&nbsp;an&nbsp;image.

&nbsp;&nbsp;Args:
&nbsp;&nbsp;&nbsp;&nbsp;image:&nbsp;Image&nbsp;file&nbsp;name.

&nbsp;&nbsp;Returns:
&nbsp;&nbsp;&nbsp;&nbsp;Nothing
&nbsp;&nbsp;&quot;&quot;&quot;</span>
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">if</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">not</span>&nbsp;tf.gfile.Exists(image):
&nbsp;&nbsp;&nbsp;&nbsp;tf.logging.fatal(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;File&nbsp;does&nbsp;not&nbsp;exist&nbsp;%s&#39;</span>,&nbsp;image)
&nbsp;&nbsp;image_data&nbsp;=&nbsp;tf.gfile.FastGFile(image,&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;rb&#39;</span>).read()

&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;the&nbsp;image&nbsp;used&nbsp;to&nbsp;warm-up&nbsp;TensorFlow&nbsp;model</span>
&nbsp;&nbsp;warm_up_image_data&nbsp;=&nbsp;tf.gfile.FastGFile(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;/root/tensorflow-related/test-images/ubike.jpg&#39;</span>,&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;rb&#39;</span>).read()

&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;Creates&nbsp;graph&nbsp;from&nbsp;saved&nbsp;GraphDef.</span>
&nbsp;&nbsp;create_graph()

&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">with</span>&nbsp;tf.Session()&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">as</span>&nbsp;sess:
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;Some&nbsp;useful&nbsp;tensors:</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;&#39;softmax:0&#39;:&nbsp;A&nbsp;tensor&nbsp;containing&nbsp;the&nbsp;normalized&nbsp;prediction&nbsp;across</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;&nbsp;&nbsp;1000&nbsp;labels.</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;&#39;pool_3:0&#39;:&nbsp;A&nbsp;tensor&nbsp;containing&nbsp;the&nbsp;next-to-last&nbsp;layer&nbsp;containing&nbsp;2048</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;&nbsp;&nbsp;float&nbsp;description&nbsp;of&nbsp;the&nbsp;image.</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;&#39;DecodeJpeg/contents:0&#39;:&nbsp;A&nbsp;tensor&nbsp;containing&nbsp;a&nbsp;string&nbsp;providing&nbsp;JPEG</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;&nbsp;&nbsp;encoding&nbsp;of&nbsp;the&nbsp;image.</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;Runs&nbsp;the&nbsp;softmax&nbsp;tensor&nbsp;by&nbsp;feeding&nbsp;the&nbsp;image_data&nbsp;as&nbsp;input&nbsp;to&nbsp;the&nbsp;graph.</span>
&nbsp;&nbsp;&nbsp;&nbsp;softmax_tensor&nbsp;=&nbsp;sess.graph.get_tensor_by_name(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;softmax:0&#39;</span>)

&nbsp;&nbsp;&nbsp;&nbsp;print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Warm-up&nbsp;start&quot;</span>)
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">for</span>&nbsp;i&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">in</span>&nbsp;range(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">10</span>):
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Warm-up&nbsp;for&nbsp;time&nbsp;{}&quot;</span>.format(i))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;predictions&nbsp;=&nbsp;sess.run(softmax_tensor,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;DecodeJpeg/contents:0&#39;</span>:&nbsp;warm_up_image_data})

&nbsp;&nbsp;&nbsp;&nbsp;print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Warm-up&nbsp;finished&quot;</span>)

&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;record&nbsp;the&nbsp;start&nbsp;time&nbsp;of&nbsp;the&nbsp;actual&nbsp;prediction</span>
&nbsp;&nbsp;&nbsp;&nbsp;start_time&nbsp;=&nbsp;time.time()

&nbsp;&nbsp;&nbsp;&nbsp;predictions&nbsp;=&nbsp;sess.run(softmax_tensor,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;DecodeJpeg/contents:0&#39;</span>:&nbsp;image_data})
&nbsp;&nbsp;&nbsp;&nbsp;predictions&nbsp;=&nbsp;np.squeeze(predictions)

&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;Creates&nbsp;node&nbsp;ID&nbsp;--&gt;&nbsp;English&nbsp;string&nbsp;lookup.</span>
&nbsp;&nbsp;&nbsp;&nbsp;node_lookup&nbsp;=&nbsp;NodeLookup()

&nbsp;&nbsp;&nbsp;&nbsp;top_k&nbsp;=&nbsp;predictions.argsort()[-FLAGS.num_top_predictions:][::<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">-1</span>]
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">for</span>&nbsp;node_id&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">in</span>&nbsp;top_k:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;human_string&nbsp;=&nbsp;node_lookup.id_to_string(node_id)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;score&nbsp;=&nbsp;predictions[node_id]
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;%s&nbsp;(score&nbsp;=&nbsp;%.5f)&#39;</span>&nbsp;%&nbsp;(human_string,&nbsp;score))

&nbsp;&nbsp;&nbsp;&nbsp;print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Prediction&nbsp;used&nbsp;time:{}&nbsp;S&quot;</span>.format(time.time()&nbsp;-&nbsp;start_time))
</code></pre>
</section>
<p>其中，我们自己添加的代码有如下几部分：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="python language-python hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;the&nbsp;image&nbsp;used&nbsp;to&nbsp;warm-up&nbsp;TensorFlow&nbsp;model</span>
warm_up_image_data&nbsp;=&nbsp;tf.gfile.FastGFile(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;/root/tensorflow-related/test-images/ubike.jpg&#39;</span>,&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;rb&#39;</span>).read()
</code></pre>
</section>
<p>这里使用了另外一张图片来预热模型（和真正预测时使用的不是同一张图片），为了简单写死了路径。</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="python language-python hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Warm-up&nbsp;start&quot;</span>)
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">for</span>&nbsp;i&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">in</span>&nbsp;range(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">10</span>):
&nbsp;&nbsp;print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Warm-up&nbsp;for&nbsp;time&nbsp;{}&quot;</span>.format(i))
&nbsp;&nbsp;predictions&nbsp;=&nbsp;sess.run(softmax_tensor,
&nbsp;&nbsp;&nbsp;{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;DecodeJpeg/contents:0&#39;</span>:&nbsp;warm_up_image_data})

print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Warm-up&nbsp;finished&quot;</span>)
</code></pre>
</section>
<p>这里循环10次来预热模型。</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="python language-python hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;record&nbsp;the&nbsp;start&nbsp;time&nbsp;of&nbsp;the&nbsp;actual&nbsp;prediction</span>
start_time&nbsp;=&nbsp;time.time()
<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;(中间省略)</span>
print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Prediction&nbsp;used&nbsp;time:{}&nbsp;S&quot;</span>.format(time.time()&nbsp;-&nbsp;start_time))
</code></pre>
</section>
<p>这里打印出了真正预测一张图片的执行时间（秒数），这个时间就是我们真正需要关心的，看它能减少到多少秒。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color:#00ff00;">『3』</span>测试结果<br />
执行和上一篇文章一样的命令，输出如下：</p>
<blockquote>
<div>
		/usr/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py:1750: VisibleDeprecationWarning: converting an array with ndim &gt; 0 to an index will result in an error in the future</div>
<div>
		&nbsp; result_shape.insert(dim, 1)</div>
<div>
		<span style="color:#0000ff;">Warm-up start</span></div>
<div>
		<span style="color:#0000ff;">Warm-up for time 0</span></div>
<div>
		W tensorflow/core/framework/op_def_util.cc:332] Op BatchNormWithGlobalNormalization is deprecated. It will cease to work in GraphDef version 9. Use tf.nn.batch_normalization().</div>
<div>
		<span style="color:#0000ff;">Warm-up for time 1</span></div>
<div>
		<span style="color:#0000ff;">Warm-up for time 2</span></div>
<div>
		<span style="color:#0000ff;">Warm-up for time 3</span></div>
<div>
		<span style="color:#0000ff;">Warm-up for time 4</span></div>
<div>
		<span style="color:#0000ff;">Warm-up for time 5</span></div>
<div>
		<span style="color:#0000ff;">Warm-up for time 6</span></div>
<div>
		<span style="color:#0000ff;">Warm-up for time 7</span></div>
<div>
		<span style="color:#0000ff;">Warm-up for time 8</span></div>
<div>
		<span style="color:#0000ff;">Warm-up for time 9</span></div>
<div>
		<span style="color:#0000ff;">Warm-up finished</span></div>
<div>
		mountain bike, all-terrain bike, off-roader (score = 0.56671)</div>
<div>
		tricycle, trike, velocipede (score = 0.12035)</div>
<div>
		bicycle-built-for-two, tandem bicycle, tandem (score = 0.08768)</div>
<div>
		lawn mower, mower (score = 0.00651)</div>
<div>
		alp (score = 0.00387)</div>
<div>
		Prediction used time:<span style="color:#ff0000;">4.141446590423584 Seconds</span></div>
</blockquote>
<div>
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
	可见：在10次预热之后，一次预测消耗的时间是 4.14 秒，虽然4秒多还是没有达到我们心目中的理想速度，但这已经比之前的50秒强太多了。<br />
	此外，从测试结果我们可以体会到的是：预热（Session.run()）的头几次特别慢，后面就快起来了，所以，预热次数太少是不行的。</p>
<p>	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
	转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
	感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
		<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%8a%a0%e5%bf%abtensorflow%e5%9c%a8%e6%a0%91%e8%8e%93%e6%b4%be%e4%b8%8a%e7%9a%84%e6%89%a7%e8%a1%8c%e9%80%9f%e5%ba%a6-%e6%a8%a1%e5%9e%8b%e9%a2%84%e7%83%ad/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
