x1816 发表于 2021-8-1 01:00

R329 周易AIPU环境搭建和仿真实战

本帖最后由 x1816 于 2021-8-1 01:16 编辑

<p>申请了测评开发板,还在报名阶段,并没有拿到板子。好在入门教程已经发布了,先搭建一个环境,试试仿真的流程能不能走通。</p>

<p>主要流程参考:R329教程一|周易 AIPU 部署及仿真教程 <a href="https://bbs.eeworld.com.cn/thread-1173906-1-1.html">http://bbs.e</a><a href="https://bbs.eeworld.com.cn/thread-1173906-1-1.html">e</a><a href="https://bbs.eeworld.com.cn/thread-1173906-1-1.html">world.com.cn/thread-1173906-1-1.html</a></p>

<p>&nbsp;</p>

<p>系统环境:Ubuntu 20.04 LTS</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p><span style="font-size:24px;"><span style="color:#336699;">安装docker</span></span></p>

<p>矽速科技提供docker环境用于开发测试,因此需要先在Ubuntu里安装docker。</p>

<p>这部分内容可以参考网上的docker教程。例如<a href="https://developer.aliyun.com/article/763005">https://developer.aliyun.com/article/763005</a></p>

<p>&nbsp;</p>

<p><span style="color:#336699;"><span style="font-size:16px;">导入docker软件源</span></span></p>

<pre>
<code class="language-bash">sudo apt update
sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
sudo add-apt-repository "deb https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"</code></pre>

<p>&nbsp;</p>

<p><span style="color:#336699;"><span style="font-size:16px;">安装docker</span></span></p>

<pre>
<code class="language-bash">sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io</code></pre>

<p>&nbsp;</p>

<p><span style="color:#336699;"><span style="font-size:24px;">下载开发环境</span></span></p>

<pre>
<code class="language-bash">sudo docker pull zepan/zhouyi</code></pre>

<p>需要下载的镜像比较大(接近3GB),如果由于众所周知的原因,网络连接性不稳,也可以用官方给出的百度云链接下载镜像文件。</p>

<p>百度云下载镜像文件(压缩包约2.9GB,解压后约5.3GB)</p>

<pre>
<code class="language-bash"># 链接:https://pan.baidu.com/s/1yaKBPDxR_oakdTnqgyn5fg
# 提取码:f8dr
gunzip zhouyi_docker.tar.gz
sudo docker load --input zhouyi_docker.tar</code></pre>

<p>&nbsp;</p>

<p><span style="color:#336699;"><span style="font-size:24px;">启动并测试开发环境</span></span></p>

<p>官方给出的启动镜像命令:</p>

<pre>
<code class="language-bash">sudo docker run -i -t zepan/zhouyi /bin/bash</code></pre>

<p>先别急着运行。为了方便数据交换,可以开一个共享目录,这里主机里的/home/vm/shared目录共享到容器里的/share,启动命令修改为:</p>

<pre>
<code class="language-bash">sudo docker run -v /home/vm/shared:/share -i -t zepan/zhouyi /bin/bash</code></pre>

<p></p>

<pre>
<code class="language-bash">./run_sim.sh</code></pre>

<p></p>

<p>模拟通过,最后一行会提示0 errors,0 warnings。</p>

<pre>
<code class="language-bash">python3 quant_predict.py</code></pre>

<p></p>

<p>&nbsp;</p>

<p>量化后前5个label发生了一些变化。</p>

<p>&nbsp;</p>

<p>原图:</p>

<p></p>

<p>&nbsp;</p>

<p>result.jpeg</p>

<p></p>

<p>&nbsp;</p>

<p><span style="color:#336699;"><span style="font-size:24px;">生成模型文件</span></span></p>

<p>&nbsp;</p>

<p><span style="font-size:16px;"><span style="color:#336699;">在主机上安装tensorflow</span></span></p>

<p>为了方便开发,在主机上安装python和tensorflow 1.15</p>

<p>Python通过anaconda安装,可参考<a href="https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/">https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/</a>。</p>

<p>这里使用最新版本安装包:Anaconda3-2021.05-Linux-x86_64.sh</p>

<p>&nbsp;</p>

<p>安装完anaconda后,先创建一个python3.6的虚拟环境(tensorflow 1.15不支持高版本的python)</p>

<pre>
<code class="language-bash">conda create -n tf python=3.6</code></pre>

<p>进入tf虚拟环境:</p>

<pre>
<code class="language-bash">conda activate tf</code></pre>

<p>安装tensorflow 1.15</p>

<pre>
<code class="language-bash">pip install tensorflow==1.15.3</code></pre>

<p>确认版本都装对了:</p>

<p></p>

<p>&nbsp;</p>

<p><span style="color:#336699;"><span style="font-size:16px;">下载并转换预训练模型</span></span></p>

<p>模型来源:<a href="https://github.com/tensorflow/models/tree/master/research/slim">https://github.com/tensorflow/models/tree/master/research/slim#pre-trained-models</a></p>

<p>将master分支下载到本地,进入research/slim子目录,安装slim模块</p>

<pre>
<code class="language-bash">cd ~/models-master/research/slim
python setup.py install</code></pre>

<p>根据教程,导出图:</p>

<pre>
<code class="language-bash">python3 export_inference_graph.py \
--alsologtostderr \
--model_name=resnet_v1_50 \
--image_size=224 \
--labels_offset=1 \
--output_file=/home/vm/model_dump/resnet_v1_50_inf.pb</code></pre>

<p></p>

<p>&nbsp;</p>

<p><span style="font-size:16px;"><span style="color:#336699;">使用预训练权重冻结</span></span></p>

<p>其中ckpt文件在前面提到的github页面上有下载地址,解压后放到model_dump目录下。</p>

<pre>
<code class="language-bash">python3 /home/vm/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow_core/python/tools/freeze_graph.py \
--input_graph=/home/vm/model_dump/resnet_v1_50_inf.pb \
--input_checkpoint=/home/vm/model_dump/resnet_v1_50.ckpt \
--input_binary=true --output_graph=/home/vm/model_dump/resnet_v1_50_frozen.pb \
--output_node_names=resnet_v1_50/predictions/Reshape_1</code></pre>

<p>将冻结的pb文件复制到共享目录下。</p>

<pre>
<code class="language-bash">cp ~/model_dump/resnet_v1_50_frozen.pb ~/shared/</code></pre>

<p>&nbsp;</p>

<p><span style="color:#336699;"><span style="font-size:24px;">仿真运行模型</span></span></p>

<p>量化数据集用/demos/pb/dataset里自带的数据。</p>

<p>&nbsp;</p>

<pre>
<code class="language-bash">cd /demos/pb/config</code></pre>

<p>复制一份配置文件,修改为前面冻结的模型:</p>

<pre>
<code class="language-bash">cp resnet_50_run.cfg resnet_50_run2.cfg</code></pre>

<p>修改参数</p>

<pre>
<code class="language-bash">vim resnet_50_run2.cfg</code></pre>

<p></p>

<p>运行:</p>

<pre>
<code class="language-bash">aipubuild config/resnet_50_run2.cfg</code></pre>

<p></p>

<p></p>

<p>查看predict结果</p>

<p>编写一个view_result.py程序,和官方demo的结果对比:</p>

<pre>
<code class="language-python">mport numpy as np
outputfile = './output_resnet_50.bin'
npyoutput = np.fromfile(outputfile, dtype=np.int8)
outputclass = npyoutput.argmax()
print("1 Predict Class is %d"%outputclass)
outputfile = './output_resnet_50_2.bin'
npyoutput = np.fromfile(outputfile, dtype=np.int8)
outputclass = npyoutput.argmax()
print("2 Predict Class is %d"%outputclass)
</code></pre>

<p></p>

<p>可见predict结果一致,上述流程是正确的。</p>

<p></p>

<p>&nbsp;</p>

<p>附录</p>

<p>查看pb文件网络结构的方法</p>

<pre>
<code class="language-python">import tensorflow as tf
#model = 'resnet_v1_50_frozen.pb'
model = '/home/vm/shared/frozen_orig.pb'
graph = tf.get_default_graph()
graph_def = graph.as_graph_def()
graph_def.ParseFromString(tf.gfile.FastGFile(model, 'rb').read())
tf.import_graph_def(graph_def, name='graph')
summaryWriter = tf.summary.FileWriter('log/', graph)
#run tensorboard --logdir=log/ --port=6006
</code></pre>

<p></p>

tagetage 发表于 2021-8-1 09:47

<p>名字起的听玄的,学习学习。</p>

Jacktang 发表于 2021-8-1 10:57

<p>使用预训练权重冻结,这个方法不错</p>
页: [1]
查看完整版本: R329 周易AIPU环境搭建和仿真实战