Khadas Docs

Amazing Khadas, always amazes you!

User Tools

Site Tools


Sidebar

products:sbc:edge2:npu:demos:vgg16

VGG16 TensorFlow Keras Edge2 Demo - 4

Get Source Code

git clone https://github.com/Daipuwei/Mini-VGG-CIFAR10

Convert Model

Build virtual environment

The SDK only supports python3.6 or python3.8, here is an example of creating a virtual environment for python3.8.

Install python packages.

$ sudo apt update
$ sudo apt install python3-dev python3-numpy

Follow this docs to install conda.

Then create a virtual environment.

$ conda create -n npu-env python=3.8
$ conda activate npu-env     #activate
$ conda deactivate           #deactivate

Get convert tool

Download Tool from rockchip-linux/rknn-toolkit2.

$ git clone https://github.com/rockchip-linux/rknn-toolkit2.git
$ git checkout 9ad79343fae625f4910242e370035fcbc40cc31a

Install dependences and RKNN toolkit2 packages.

$ cd rknn-toolkit2
$ sudo apt-get install python3 python3-dev python3-pip
$ sudo apt-get install libxslt1-dev zlib1g-dev libglib2.0 libsm6 libgl1-mesa-glx libprotobuf-dev gcc cmake
$ pip3 install -r doc/requirements_cp38-*.txt
$ pip3 install packages/rknn_toolkit2-*-cp38-cp38-linux_x86_64.whl

Convert

Keras model can convert rknn model directly. But this demo is to convert tensorflow model(.pb) first, and convert tnesorflow model to rknn model. We use this tool to convert.

git clone https://github.com/amir-abdi/keras_to_tensorflow.git

Enter rknn-toolkit2/examples/tensorflow/ssd_mobilenet_v1 and modify test.py as follows.

test.py
# Create RKNN object
rknn = RKNN(verbose=True)
 
# Pre-process config
print('--> Config model')
rknn.config(mean_values=[0.0, 0.0, 0.0], std_values=[255.0, 255.0, 255.0], target_platform='rk3588')
print('done')
 
# Load model
print('--> Loading model')
ret = rknn.load_tensorflow(tf_pb='./vgg16.pb',
                            inputs=['image_input'],
                            outputs=['dense_2/Softmax'],
                            input_size_list=[[1, INPUT_SIZE, INPUT_SIZE, 3]])
if ret != 0:
    print('Load model failed!')
    exit(ret)
print('done')
 
# Build Model
print('--> Building model')
ret = rknn.build(do_quantization=True, dataset='./dataset.txt')
if ret != 0:
    print('Build model failed!')
    exit(ret)
print('done')
 
# Export rknn model
print('--> Export rknn model')
ret = rknn.export_rknn('./vgg16.rknn')
if ret != 0:
    print('Export rknn model failed!')
    exit(ret)
print('done')

Run test.py to generate rknn model.

$ python3 test.py

Run NPU

Get source code

Clone the source code from our khadas/edge2-npu.

$ git clone https://github.com/khadas/edge2-npu

Install dependencies

$ sudo apt update
$ sudo apt install cmake libopencv-dev

Compile and run

Picture input demo

Put vgg16.rknn in edge2-npu/C++/vgg16/data/model.

# Compile
$ bash build.sh
 
# Run
$ cd install/vgg16
$ ./vgg16 data/model/vgg16.rknn data/img/airplane.jpeg

If your VGG16 model classes is not the same as cifar10, please change data/vgg16_class.txt and the OBJ_CLASS_NUM in include/postprocess.h.

Last modified: 2023/09/20 03:12 by louis