Skip to content

Instantly share code, notes, and snippets.

@m0rph03nix
Last active December 5, 2019 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m0rph03nix/9607a85bbe5e25c37ad176668ce74dc5 to your computer and use it in GitHub Desktop.
Save m0rph03nix/9607a85bbe5e25c37ad176668ce74dc5 to your computer and use it in GitHub Desktop.
CMSIS-NN experiment with CIFAR-10 / CAFFE / MBED

Work side by side with : https://developer.arm.com/solutions/machine-learning-on-arm/developer-material/how-to-guides/image-recognition-on-arm-cortex-m-with-cmsis-nn/before-you-begin

Install Target development tools

# home...
cd

# Get the compilation toolchain archive
wget https://developer.arm.com/-/media/Arm%20Developer%20Community/Images/Tutorial%20Guide%20Diagrams%20and%20Screenshots/Machine%20Learning/Image%20recognition%20on%20Arm%20Cortex-M%20with%20CMSIS-NN/gcc-arm-none-eabi-7-2017-q4-major-linux.tar.bz2?revision=09f5c905-fb63-4870-85fb-b5b911c0304c&la=en&hash=97C8EB7737FA9221B4A166CD9B040E6FEB624BDA

# Extract archive
tar -jf gcc-arm-none-eabi-7-2017-q4-major-linux.tar.bz2

# Update environnement variables 
export PATH=$PATH:"~/gcc-arm-none-eabi-7-2017-q4-major/bin"
echo 'export PATH=$PATH:~/gcc-arm-none-eabi-7-2017-q4-major/bin' >> ~/.bashrc

Once Arm Mbed CLI is installed in the arm tutorial :

mbed config -G GCC_ARM_PATH "~/gcc-arm-none-eabi-7-2017-q4-major/bin"

Install Caffee and prepare CIFAR dataset

Install

Follow below instructions or check offical source :

# home...
cd

# clone caffee repository
git clone https://github.com/BVLC/caffe.git

# Update environnement variables 
export CAFFE_ROOT="~/caffe" 
echo 'export CAFFE_ROOT=~/caffe' >> ~/.bashrc
export PYTHONPATH="${PYTHONPATH}:~/caffe/python"
echo 'export PYTHONPATH="${PYTHONPATH}:~/caffe/python"' >> ~/.bashrc

# Setup compilation and compile
mkdir build
cd build
cmake .. # or cmake .. -DCPU_ONLY=ON if no GPU. You can also use cmake-gui .. and generate once to see options
make all
make install
make runtest

Prepare CIFAR10 dataset

source : https://caffe.berkeleyvision.org/gathered/examples/cifar10.html

cd $CAFFE_ROOT
./data/cifar10/get_cifar10.sh
./examples/cifar10/create_cifar10.sh

Train CIFAR10 dataset

If you work in CPU_Only, change GPU by CPU in every prototxt files in the folder CAFFE_ROOT/examples/cifar10

Then launch the training (roughly an hour in CPU mode) :

cd $CAFFE_ROOT
./examples/cifar10/train_quick.sh

Quantize the model

Just follow the tutorial...
Don't forget to change paths to your "caffe/examples/cifar10" files in ~/CMSISNN_Webinar/ML-examples/cmsisnn-cifar10/models/cifar10_m7_train_test.prototxt

Warning A step is missing in the tutorial : Converting the caffemodel file in h5 file. For this you can use caffemodel_convertor.py

Convert the model

Just follow the tutorial...

CMSIS-NN

Just follow the tutorial...

Deploy transformed model on an Arm Cortex-M processor

cd ~/CMSISNN_Webinar/cmsisnn_demo/

mbed compile -m DISCO_F746NG -t GCC_ARM --source . --source ../ML-examples/cmsisnn-cifar10/code/m7 --source ../ML-examples/cmsisnn-cifar10/camera_demo/camera_with_nn/ --source ../CMSIS_5/CMSIS/NN/Include --source ../CMSIS_5/CMSIS/NN/Source --source ../CMSIS_5/CMSIS/DSP/Include --source ../CMSIS_5/CMSIS/Core/Include -j8

Drag & drop the bin file generated into the usb mass storage available on plugin the board

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment