Running Inception_V3 using On-device AI

 Introduction

 The Inception v3 Imagenet classification model is trained to classify images with 1000 labels.

Inception-V3 is a convolutional neural network architecture designed for image classification and recognition tasks.


Tensorflow Setup :

To set up TensorFlow, perform the following procedure:

1.  Download android-ndk-, and set the ANDROID_NDK_ROOT environment variable. 

  •  $export ANDROID_NDK_ROOT=<path to Android Ndk root>

2. Go to the SNPE SDK root directory, and run the following script to set up the SNPE SDK environment (the TensorFlow home directory must be provided):

  •  $export TENSORFLOW_DIR=/usr2//.local/bin/ $source bin/envsetup.sh -t $TENSORFLOW_DIR 

 where $TENSORFLOW_DIR is the path to the TensorFlow installation.

The script sets up the following environment variables:

  •  SNPE_ROOT: root directory of the SNPE SDK installation
  •  TENSORFLOW_HOME: root directory of the TensorFlow installation provided 

 The script also updates:

  •  PATH, LD_LIBRARY_PATH, and PYTHONPATH.

Running Inception_V3 Model 

Convert the TensorFlow Inception_v3 model to DLC 

1. Download the Inception_v3 model and assets from https://storage.googleapis.com/download.tensorflow.org/models/inception_v3_2016_ 08_28_frozen.pb.tar.gz into the tmp folder.

  •  $cd $SNPE_ROOT $mkdir tmp $wget -N ./tmp “https://storage.googleapis.com/download.tensorflow.org/models/inception _v3_2016_08_28_frozen.pb.tar.gz”

2. Prepare input data for the Inception_v3 model. 

  •  The SNPE requires that the input image be in a format that is different from Caffe or TensorFlow networks. 
  • In the SNPE, the image must be presented as a tensor of shape (height × width × channel), where channel is the fastest-changing dimension.

3. Create the folder structure like this  in the examples/Models/inception_V3 folder










4. Extract the downloaded inception_v3xxx.tar file.
  •  $cd $SNPE_ROOT/models/inception_v3/ $mkdir -p tensorflow_dlc_data/cropped $tar -xzf ./tmp/inception_v3_2016_08_28_frozen.pb.tar.gz -C models/inception_v3/tensorflow

 == Copy List of trained data set to data folder for classification==
  •  $cp tensorflow/imagenet_slim_labels.txt data/
5. Copy the test image data set from $SNPE_ROOT/models/alexnet/data/. 
  • $cp $SNPE_ROOT/models/alexnet/data/*.jpg  data/ 

A sample test image data set is provided by the SNPE SDK in the 
$SNPE_ROOT/models/alexnet/data/ directory. 

 Input data preparation is not related to AlexNet. 

6. The inception_v3 model accepts 299 × 299 raw images as input 
The create_inception_v3_raws.py script CAB file is used to convert the .jpeg files 
into 299 × 299 raw files. 

By default, this script converts to 299 × 299. Use -S <size> to generate the other 
resolutions. 

Create SNPE inception_v3 raw data (convert JPEG to raw): 

  • $python $SNPE_ROOT 
  • /models/inception_v3/scripts/create_inceptionv3_raws.py -i data -d 
  • data/cropped/ -s 299 
The size of raw images must be equal to the model input layer dimensions. Here, the 
size of raw images 299 × 299 ×  3 (three RGB channels) is equal to the TensorFlow 
model input layer dimensions.
 
7. Create the raw file list test files. 

Create image list data files: 

  • $python $SNPE_ROOT /models/inception_v3/scripts/create_file_list.py -i 
  • data/cropped  -o data/cropped/raw_list.txt -e *.raw 
  • $python $SNPE_ROOT/ /models/inception_v3/scripts/ create_file_list.py -i 
  • data/cropped  -o data/target_raw_list.txt  -e *.raw -r 
 Convert the Inception_v3 model

After preparing the assets  convert the .pb format to the DLC format using the snpe-tensorflow-to-dlc tool.
 Convert the .pb file to DLC format:
  •  $snpe-tensorflow-to-dlc --graph tensorflow/inception_v3_2016_08_28_frozen.pb --input_dim input "1,299,299,3" --out_node InceptionV3/Predictions/Reshape_1 --dlc inception_v3.dlc

Run the Inception_v3 model
   
   Run on the host :

Go to the base location for the model, and run snpe-net-run. 
  •  $cd $SNPE_ROOT/models/inception_v3 $snpe-net-run --container dlc/inception_v3_quantized.dlc --input_list data/cropped/raw_list.txt 
A Python script to interpret the classification results is provided and can be used as follows:
  •  $python $SNPE_ROOT/models/inception_v3/scripts/show_inceptionv3_classifications.py -i data/cropped/raw_list.txt \ -o output/ \ -l data/imagenet_slim_labels.txt
The output should show classification results for all images, like the following:



Run on an Android target :

Prerequisite: Push SNPE libraries and the prebuilt snpe-net-run executable to /data/local/tmp/snpeexample on the Android target.

  •  $export SNPE_TARGET_ARCH= aarch64-android-clang5.0 
  •  $export SNPE_TARGET_STL=libgnustl_shared.so $adb shell "mkdir -p /data/local/tmp/snpeexample/$SNPE_TARGET_ARCH/bin" 
  •  $adb shell "mkdir -p /data/local/tmp/snpeexample/
  •  $SNPE_TARGET_ARCH/lib"
  •  $adb shell "mkdir -p /data/local/tmp/snpeexample/dsp/lib" 
  •  $adb push
  •  $SNPE_ROOT/lib/
  •  $SNPE_TARGET_ARCH/* /data/local/tmp/snpeexample/
  •  $SNPE_TARGET_ARCH/lib $adb push $SNPE_ROOT/lib/dsp/* \ \ /data/local/tmp/snpeexample/dsp/lib $adb push 
  •  $SNPE_ROOT/bin/$SNPE_TARGET_ARCH/snpe-net-run \ /data/local/tmp/snpeexample/$SNPE_TARGET_ARCH/bin  
To execute the Inception_v3 classification model on an Android target, perform the following procedure:
     1. Prepare assets in the Android device.
  •  $cd $SNPE_ROOT/models/inception_v3 
  •  $mkdir data/rawfiles && cp data/cropped/*.raw data/rawfiles/ 
  •  $adb shell "mkdir -p /data/local/tmp/inception_v3" 
  •  $adb push data/rawfiles /data/local/tmp/inception_v3/cropped
  •  $adb push data/target_raw_list.txt /data/local/tmp/inception_v3
  •  $adb push dlc/inception_v3_quantized.dlc /data/local/tmp/inception_v3 $rm -rf data/rawfiles

      2. Execute the Inception_v3 model on the Android device using CPU runtime.

 $adb shell 
 #export SNPE_TARGET_ARCH=aarch64-android-clang5.0 
 #export
 LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/data/local/tmp/snpeexample/
$SNPE_TARGE T_ARCH/lib 
 #export PATH=$PATH:/data/local/tmp/snpeexample/$SNPE_TARGET_ARCH/bin
 #snpe-net-run -h
 #cd /data/local/tmp/inception_v3
 #snpe-net-run --container inception_v3_quantized.dlc --input_list target_raw_list.txt
 #exit The executable creates the results folder: /data/local/tmp/inception_v3/output.

    3. To pull the output, run the following:
  •  $adb pull /data/local/tmp/inception_v3/output output_android

 Run the following Python script to check the classification results: 

  •  $python scripts/show_inceptionv3_classifications.py \ -i data/target_raw_list.txt \ -o output_android \ -l data/imagenet_slim_labels.txt 

 The output should show classification results for all images, like the following:



Running Benchmarks for Inception_V3

Pre-requisite :
  • your_model.dlc 
  •   A text file listing all the input data. For an example, see: $SNPE_ROOT/models/alexnet/data/image_list.txt. 
  •   All the input data listed in the text file. For an example, see $SNPE_ROOT/models/alexnet/data/cropped.
$cd $SNPE_ROOT/benchmark  
$cp alexnet_sample.json inception_v3.json 
$vi inception_v3.json 
// modify the inception_v3.json parameters according to inception_v3 
{
  "Name":"inceptionV3", 
  "HostRootPath": "inception_v3",
  "HostResultsDir":"inception_v3/results",
  "DevicePath":"/data/local/tmp/snpeexample", 
  "Devices":["2d07d8f9"], 
  "HostName": "localhost", 
  "Runs":5,

"Model": { 
      "Name": "Inception_v3", 
      "Dlc": 
  "$SNPE_ROOT/models/inception_v3/dlc/inception_v3_quantized.dlc",
                 "InputList":
 "$SNPE_ROOT/models/inception_v3/data/target_raw_list.txt",
                     "Data": [
       "$SNPE_ROOT/models/inception_v3/data/cropped"
              ]
              }, 
     "Runtimes":["GPU", "CPU", "DSP"], 
     "Measurements": ["timing"] 
 }

 Run the benchmark for Inception_v3 

run the following command to run on the only device connected to your computer
  • $cd $SNPE_ROOT/benchmarks  
  • $python snpe_bench.py -c inception_v3.json  
View the results

A latest_results link that points to the most recent run is created.

 # In inception_v3.json, "HostResultDir" is set to "inception_v3/results"
  •  cd $SNPE_ROOT/benchmarks/inception_v3/results 
 # Notice the time stamped directories and the "latest_results" link. 
  •  cd $SNPE_ROOT/benchmarks/inception_v3/results/latest_results 
 # Notice the .csv file, open this file in a csv viewer (Excel, LibreOffice Calc) 


Comments

Popular posts from this blog

Examples of running Machine Learning Model on Device using Qualcomm AI HUB

Medical Report Analyzer - Progess