1.ubuntu18.04 minist

1、查看python版本

python3 --version

ps:发现是3.6.8可以用

2、将默认的python调整为python3

一、默认Python2调整为Python3

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 200

二、默认Python3调整为Python2

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 200
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 100

2、安装pip

sudo apt install python3-pip

3、安装tensorflow

pip3 install tensorflow

pip3 uninstall gast
pip3 install gast==0.2.2

4、在home下创建文件夹k210

5、下载

git clone https://github.com/sipeed/Maix-TF-workspace.git

6、下载的mnist的训练也放进去,快点

7、训练得到mnist.pb

python3 mnist.py

8、下载转换工具

git clone https://github.com/sipeed/Maix_Toolbox.git

9、在其中创建个文件夹workspace,并把mnist.pb放入workspace目录下

python3 gen_pb_graph.py workspace/mnist.pb

10、如果找不到tensorboard

pip3 show tensorflow

11、找到对应路径后,在./barsh最后添加

export PATH=/home/chenub/.local/bin:$PATH

source ~/.bashrc

12、

我们就可以打开浏览器,输入它提示的网址进行查看计算图:

image|306x499

可以看到输入节点名为“inputs”,输出节点名为“output”

14、先安装toco(虚拟机中略过这一步)

pip3 install toco
运行已经写好的.sh脚本后,在终端运行时提示找不到这个文件,并且提示:bad variable name

原因是:脚本是在window下用notpad++写的,window和linux的编码不同

解决办法:vim pb2tflite.sh 进入该文件的编辑状态

输入内容  :set ff=unix #这是进行编码切换

               :wq #保存退出

 

14、转换模型

出现问题的解决方法:右击xxx.sh文件,打开属性———>权限——>勾选允许作为程序执行文件,将下面的所有文件权限打开

首先从pb转换为tflite:

./pb2tflite.sh workspace/mnist.pb

This script help you generate cmd to convert *.pb to *.tflite

Please put your pb into workspace dir

1. pb file name: (don't include workspace)

mnist.pb

2. input_arrays name:

inputs

3. output_arrays name:

output

4. input width:

28

5. input height:

28

6. input channel:

1

15、格式转换

运行已经写好的.sh脚本后,在终端运行时提示找不到这个文件,并且提示:bad variable name

原因是:脚本是在window下用notpad++写的,window和linux的编码不同

解决办法:vim tflite2kmodel.sh 进入该文件的编辑状态

输入内容  :set ff=unix #这是进行编码切换

               :wq #保存退出




解决办法:vim get_nncase.sh 进入该文件的编辑状态

输入内容  :set ff=unix #这是进行编码切换

               :wq #保存退出

17、赋予get_nncase.sh,读写权限

(1)
./get_nncase.sh

(2)

虚拟机中只要安装

pip3 install scipy
下面不用
-------------------------------
pip3 install mkl
pip3 install numpy
pip3 install pillow
pip3 install scipy

18. 准备量化图片(添加测试图片)

TFlite模型在转换为K210模型的时候,会对原来的网络结构进行量化quantization, 量化会将原来网络中的浮点运算转化为8比特整数运算,起到压缩模型尺寸与加速的目的。线性激活函数, relu激活函数, relu6激活函数等均可以转换为等价的量化运算。

在Maix_Toolbox的根目录下新建一images文件夹,用于存放量化图片数据, 在量化的过程中需要用到。

测试图片必须直接放置在images文件夹中。 图像的名称可以任意,图像尺寸跟训练集的图像尺寸保持一致,可以从训练集的图像样本中选取一些有代表性的,放在images文件夹中。

但是我们下载的mnist数据集是二进制的,并不是图片格式,所以使用以下脚本转换成图片:

(1)回到mnist目录下

(2)

python3 chenreadimage.py


 

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) #MNIST数据输入

from PIL import Image 
import scipy.misc
import os
save_dir = './images/'
if os.path.exists(save_dir) is False:
    os.makedirs(save_dir)
for i in range(20):
    image = mnist.train.images[i,:]
    image = image.reshape(28,28)
    file = save_dir+'mnist_train_%d.jpg' % i
    #scipy.misc.toimage(image,cmin=0.0,cmax=1.0).save(file)
    Image.fromarray((image*255).astype('uint8'), mode='L').convert('RGB').save(file)

19、重要

(1)把mninst下面刚才images下生成的图拷贝到toolbox下,再运行

./tflite2kmodel.sh workspace/mnist.tflite

这时在workspace下生成了mnist.kmodel。可以看到模型大小为200KB大小左右

20、创建文件夹standalone

git clone https://github.com/sipeed/LicheeDan_K210_examples.git

21、拷贝生成的mnist.kmodel到src/mnist下面,并重命名为m.kmodel,在src/mnist下面

(1)虚拟机中,只要将mnist.kmodel拷贝到mnist/convertokmodel目录下

./pack_model.sh

得到 model.kfpkg

22、kflash先烧录.bin再烧录kfpgk

 

23\

import sensor,lcd,image
import KPU as kpu
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_windowing((224, 224))    #set to 224x224 input
sensor.set_hmirror(0)               #flip camera
task = kpu.load(0x00600000)         #load model from 
sensor.run(1)
while True:
    img = sensor.snapshot()
    lcd.display(img,oft=(0,0))      #display large picture
    img1=img.to_grayscale(1)        #convert to gray
    img2=img1.resize(28,28)         #resize to mnist input 28x28
    a=img2.invert()                 #invert picture as mnist need
    a=img2.strech_char(1)           #preprocessing pictures, eliminate dark corner
    lcd.display(img2,oft=(224,32))  #display small 28x28 picture
    a=img2.pix_to_ai();             #generate data for ai
    fmap=kpu.forward(task,img2)     #run neural network model 
    plist=fmap[:]                   #get result (10 digit's probability)
    pmax=max(plist)                 #get max probability
    max_index=plist.index(pmax)     #get the digit
    lcd.draw_string(224,0,"%d: %.3f"%(max_index,pmax),lcd.WHITE,lcd.BLACK)  #show result

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值