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

 

發佈了116 篇原創文章 · 獲贊 33 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章