1# Image Classification with the Arm NN Tensorflow Lite Delegate 2 3This application demonstrates the use of the Arm NN Tensorflow Lite Delegate. 4In this application we integrate the Arm NN Tensorflow Lite Delegate into the 5TensorFlow Lite Python package. 6 7## Before You Begin 8 9This repository assumes you have built, or have downloaded the 10`libarmnnDelegate.so` and `libarmnn.so` from the GitHub releases page. You will 11also need to have built the TensorFlow Lite library from source if you plan on building 12these ArmNN library files yourself. 13 14If you have not already installed these, please follow our guides in the ArmNN 15repository. The guide to build the delegate can be found 16[here](../../delegate/BuildGuideNative.md) and the guide to integrate the 17delegate into Python can be found 18[here](../../delegate/DelegateQuickStartGuide.md). 19 20This guide will assume you have retrieved the binaries 21from the ArmNN Github page, so there is no need to build Tensorflow from source. 22 23## Getting Started 24 25Before running the application, we will first need to: 26 27- Install the required Python packages 28- Download this example 29- Download a model and corresponding label mapping 30- Download an example image 31 321. Install required packages and Git Large File Storage (to download models 33from the Arm ML-Zoo). 34 35 ```bash 36 sudo apt-get install -y python3 python3-pip wget git git-lfs unzip 37 git lfs install 38 ``` 39 402. Clone the Arm NN repository and change directory to this example. 41 42 ```bash 43 git clone https://github.com/arm-software/armnn.git 44 cd armnn/samples/ImageClassification 45 ``` 46 473. Download your model and label mappings. 48 49 For this example we use the `MobileNetV2` model. This model can be found in 50 the Arm ML-Zoo as well as scripts to download the labels for the model. 51 52 ```bash 53 export BASEDIR=$(pwd) 54 #clone the model zoo 55 git clone https://github.com/arm-software/ml-zoo.git 56 #go to the mobilenetv2 uint8 folder 57 cd ml-zoo/models/image_classification/mobilenet_v2_1.0_224/tflite_uint8 58 #generate the labelmapping 59 ./get_class_labels.sh 60 #cd back to this project folder 61 cd BASEDIR 62 #copy your model and label mapping 63 cp ml-zoo/models/image_classification/mobilenet_v2_1.0_224/tflite_uint8/mobilenet_v2_1.0_224_quantized_1_default_1.tflite . 64 cp ml-zoo/models/image_classification/mobilenet_v2_1.0_224/tflite_uint8/labelmappings.txt . 65 ``` 66 674. Download a test image. 68 69 ```bash 70 wget -O cat.png "https://github.com/dmlc/mxnet.js/blob/main/data/cat.png?raw=true" 71 ``` 72 735. Download the required Python packages. 74 75 ```bash 76 pip3 install -r requirements.txt 77 ``` 78 796. Copy over your `libarmnnDelegate.so` and `libarmnn.so` library files 80you built/downloaded before trying this application to the application 81folder. For example: 82 83 ```bash 84 cp /path/to/armnn/binaries/libarmnnDelegate.so . 85 cp /path/to/armnn/binaries/libarmnn.so . 86 ``` 87 88## Folder Structure 89 90You should now have the following folder structure: 91 92``` 93. 94├── README.md 95├── run_classifier.py # script for the demo 96├── libarmnnDelegate.so 97├── libarmnn.so 98├── cat.png # downloaded example image 99├── mobilenet_v2_1.0_224_quantized_1_default_1.tflite # tflite model from ml-zoo 100└── labelmappings.txt # model label mappings for output processing 101``` 102 103## Run the model 104 105```bash 106python3 run_classifier.py \ 107--input_image cat.png \ 108--model_file mobilenet_v2_1.0_224_quantized_1_default_1.tflite \ 109--label_file labelmappings.txt \ 110--delegate_path /path/to/armnn/binaries/libarmnnDelegate.so \ 111--preferred_backends GpuAcc CpuAcc CpuRef 112``` 113 114The output prediction will be printed. In this example we get: 115 116```bash 117'tabby, tabby cat' 118``` 119 120## Running an inference with the Arm NN TensorFlow Lite Delegate 121 122Compared to your usual TensorFlow Lite projects, using the Arm NN TensorFlow 123Lite Delegate requires one extra step when loading in your model: 124 125```python 126import tflite_runtime.interpreter as tflite 127 128armnn_delegate = tflite.load_delegate("/path/to/armnn/binaries/libarmnnDelegate.so", 129 options={ 130 "backends": "GpuAcc,CpuAcc,CpuRef", 131 "logging-severity": "info" 132 } 133) 134interpreter = tflite.Interpreter( 135 model_path="mobilenet_v2_1.0_224_quantized_1_default_1.tflite", 136 experimental_delegates=[armnn_delegate] 137) 138``` 139