ä»åãAIã®éååã«ã¤ãã¦å¦ãã§ããã¾ããè«æãèªããã¨ã¯ç¶ç¶ãã¤ã¤ãä»åã¯ãå®éã«éååã¢ãã«ãåããã¦ãæ¨è«ã®é«éåãå®æãããã¨æãã¾ãã
ååï¼TensorFlow Lite Pythonで量子化モデルをRaspberry Pi 4で動かす - daisukeの技術ブログï¼ã¯ãPython ã® tflite-runtime ãã¤ã³ã¹ãã¼ã«ãã¦å®è¡ãã¾ããããä»åã¯ãC++ã§ãã£ã¦ããã¾ããæé éãã«ããã°ãå¿
ãåããã¨ãåºæ¥ãããã«ãç°å¢æ§ç¯ããã«ããå®è¡ã®æé ãæ¸ãã¦ããã¾ãã
ããã§ã¯ããã£ã¦ããã¾ãï¼
ã¯ããã«
ãAIã¢ãã«ã®éååãã®è¨äºä¸è¦§ã§ããè¯ãã£ããåèã«ãã¦ãã ããã
AIã¢ãã«ã®éååã®è¨äºä¸è¦§
ã¨ã³ã¸ãã¢ã°ã«ã¼ãã®ã©ã³ãã³ã°ã«åå ä¸ã§ãã
æ°æ¥½ã«ãããã¨ãããããé¡ããããã¾ãð
ããã§ã¯ãã£ã¦ããã¾ãï¼
Raspberry Pi 4 㧠TensorFlow Lite ã®ç°å¢æ§ç¯
TensorFlow Lite ã®å
¬å¼ãµã¤ãã§ãCMakeã§ãã«ãããæé ã®ãã¼ã¸ã§ãã
www.tensorflow.org
å
¬å¼ãµã¤ãã§ã¯ãRaspberry Pi 4 ç¨ã®ãã«ãã¯ãã¯ãã¹ã³ã³ãã¤ã«ã®æé ããããã¾ããã§ããããããã§ã¯ã»ã«ãã³ã³ãã¤ã«ï¼Raspberry Pi 4 ã§ãã«ãï¼ã§ãã£ã¦ããã¾ãã
ã¾ãã¯ãGit 㨠CMake ãã¤ã³ã¹ãã¼ã«ãã¾ãã
$ sudo apt-get install git cmake
次ã«ãTensorFlow ã®ãªãã¸ããªãã¯ãã¼ã³ãã¾ããå
¬å¼ãµã¤ãã®æé ã§ã¯ master ãã¯ãã¼ã³ããããã«ãªã£ã¦ã¾ãããããã¯è¯ããªãã¨æãã®ã§ãææ°ã®ãªãªã¼ã¹ã® v2.16.1 ã®ã¿ã°ãã¯ãã¼ã³ãã¾ãã
ãªãããclone ã« -b ã¿ã°å ã§å¤±æããã®ã§ãã¯ãã¼ã³å¾ã«ã¹ã¤ãããã¾ããã
$ git clone -b v2.16.1 https://github.com/tensorflow/tensorflow.git
$ cd tensorflow/
$ git switch -c v2.16.1
$ git status
On branch v2.16.1
nothing to commit, working tree clean
ãã«ãæé ãã»ã¨ãã©æ¸ããã¦ãªãã®ã§ãæ®éã«ãã«ããã¦ã¿ã¾ããCMakeã§ã¨ã©ã¼åºãã®ã§ãbuild-essential ãå
¥ãã¦ããã¾ãã
$ mkdir build
$ cd build/
$ cmake ../tensorflow/lite
ã¨ã©ã¼
$ sudo apt install build-essential
$ cmake ../tensorflow/lite
$ make -j$(nproc)
ç´1æéã§ãã«ããå®äºãã¦ãbuildãã£ã¬ã¯ããªã«ãlibtensorflow-lite.aããä½ããã¾ããã
ãlabel_imageãã® C++çããã£ã¦ããã¾ããlabel_imageã¯ãã«ãããã¦ãªãã£ãã®ã§ãå
¬å¼æé éãã«ããã«ããã¾ãã
$ cmake --build . -j -t label_image
ããã«å®äºãã¾ãããbuild/examples/label_image/label_image ãä½ããã¾ããã
make ããå¾ã« label_image ããã«ããã¾ããããlabel_imageã®ãã«ãæã« libtensorflow-lite.a ãä½ãããã®ã§ãmake ã¯ä¸è¦ã ã£ãã¨æãã¾ãã
ããã§æºåå®äºã§ãã
Raspberry Pi 4 㧠TensorFlow Lite ã®å®è¡
ããã§ã¯å®è¡ãã¦ããã¾ããå¦ç¿æ¸ã¿ã¢ãã«ãå
¥åããç»åãã¡ã¤ã«ãã©ãã«ããã¹ãã¯ãåå使ã£ããã®ã使ãã¾ãã
daisuke20240310.hatenablog.com
Pythonçã¨ã¯ã³ãã³ãã©ã¤ã³å¼æ°ãç°ãªãã®ã§æ³¨æã§ãã--help ã§ã³ãã³ãã©ã¤ã³å¼æ°ãè¦ãã¾ãã
$ examples/label_image/label_image --help
INFO:
usage: label_image <flags>
Flags:
--num_threads=-1 int32 optional number of threads used for inference on CPU.
--max_delegated_partitions=0 int32 optional Max number of partitions to be delegated.
--min_nodes_per_partition=0 int32 optional The minimal number of TFLite graph nodes of a partition that has to be reached for it to be delegated.A negative value or 0 means to use the default choice of each delegate.
--first_delegate_node_index=0 int32 optional The index of the first node that could be delegated. Used only when TFLITE_DEBUG_DELEGATE is defined. Default is 0.
--last_delegate_node_index=2147483647 int32 optional The index of the last node that could be delegated. Used only when TFLITE_DEBUG_DELEGATE is defined. Default is INT_MAX.
--delegate_serialize_dir= string optional Directory to be used by delegates for serializing any model data. This allows the delegate to save data into this directory to reduce init time after the first run. Currently supported by NNAPI delegate with specific backends on Android. Note that delegate_serialize_token is also required to enable this feature.
--delegate_serialize_token= string optional Model-specific token acting as a namespace for delegate serialization. Unique tokens ensure that the delegate doesn't read inapplicable/invalid data. Note that delegate_serialize_dir is also required to enable this feature.
--use_xnnpack=false bool optional explicitly apply the XNNPACK delegate. Note the XNNPACK delegate could be implicitly applied by the TF Lite runtime regardless the value of this parameter. To disable this implicit application, set the value to false explicitly.
--xnnpack_force_fp16=false bool optional enforce float16 inference.
--accelerated, -a: [0|1] use Android NNAPI or not
--allow_fp16, -f: [0|1], allow running fp32 models with fp16 or not
--count, -c: loop interpreter->Invoke() for certain times
--gl_backend, -g: [0|1]: use GL GPU Delegate on Android
--hexagon_delegate, -j: [0|1]: use Hexagon Delegate on Android
--input_mean, -b: input mean
--input_std, -s: input standard deviation
--image, -i: image_name.bmp
--labels, -l: labels for the model
--tflite_model, -m: model_name.tflite
--profiling, -p: [0|1], profiling or not
--num_results, -r: number of results to show
--threads, -t: number of threads
--verbose, -v: [0|1] print more information
--warmup_runs, -w: number of warmup runs
--xnnpack_delegate, -x [0:1]: xnnpack delegate
--help, -h: Print this help message
ã¹ã¬ããæ°ãæå®ãããªãã·ã§ã³ãããã¾ãããããã§éãå®è¡ã§ãããã§ããã¾ãã¯ãéååãã¦ãªãã¢ãã«ã§ãããã©ã«ãã§å®è¡ãã¾ãã
$ examples/label_image/label_image -m ../../tflite/models/mobilenet_v1_1.0_224/mobilenet_v1_1.0_224.tflite -l ../../tflite/labels.txt -i ../../tf
lite/grace_hopper.bmp
INFO: Loaded model ../../tflite/models/mobilenet_v1_1.0_224/mobilenet_v1_1.0_224.tflite
INFO: resolved reporter
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
INFO: invoked
INFO: average time: 73.992 ms
INFO: 0.860174: 653 653:military uniform
INFO: 0.0481022: 907 907:Windsor tie
INFO: 0.00786703: 466 466:bulletproof vest
INFO: 0.00644932: 514 514:cornet, horn, trumpet, trump
INFO: 0.00608026: 543 543:drumstick
éããªãã¾ãããPython ã® tflite-runtime ã§å®è¡ããã¨ã144.901msãã§ããããC++ã§ã¯ã73.992msãã§ããç´2åéããªãã¾ãããã--thredsãã« 4 ãæå®ãã¦ã¿ãããã--num_threadsãã« 4 ãæå®ãã¦ã¿ã¾ããããå¤ããã¾ããã§ãããããã©ã«ãã§4ã¹ã¬ãããæå®ããã¦ããã®ããããã¾ããã
精度ã¯ãPython ã® tflite-runtime ã§ã¯ã92.0%ãã§ããããC++ã§ã¯ã86.0%ãã§ããã--input_meanãã¨ã--input_stdããæå®ãã¦ãªãããã¨æã£ã¦ãããããã127.5ï¼label_image.pyã®ããã©ã«ãã§æå®ãããå¤ï¼ããæå®ãã¦ã¿ã¾ããããçµæã¯å¤ããã¾ããã§ããã
ç¶ãã¦ãéååã¢ãã«ã§å®è¡ãã¦ã¿ã¾ãã
$ examples/label_image/label_image -m ../../tflite/models/mobilenet_v1_1.0_224_quant/mobilenet_v1_1.0_224_quant.tflite -l ../../tflite/labels.txt -i ../../tflite/grace_hopper.bmp -b 127.5 -s 127.5
INFO: Loaded model ../../tflite/models/mobilenet_v1_1.0_224_quant/mobilenet_v1_1.0_224_quant.tflite
INFO: resolved reporter
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
INFO: invoked
INFO: average time: 30.148 ms
INFO: 0.768627: 653 653:military uniform
INFO: 0.105882: 907 907:Windsor tie
INFO: 0.0196078: 458 458:bow tie, bow-tie, bowtie
INFO: 0.0117647: 466 466:bulletproof vest
INFO: 0.00784314: 835 835:suit, suit of clothes
ãã¡ããéããªãã¾ãããPython ã® tflite-runtime ã§å®è¡ããã¨ã99.783msãã§ããããC++ã§ã¯ã30.148msãã§ãã3å以ä¸éããªãã¾ããã
精度ã¯ãPython ã® tflite-runtime ã§ã¯ã87.5%ãã§ããããC++ã§ã¯ã76.9%ãã§ãããã¡ãããPython ã«æ¯ã¹ã¦æªåãã¾ããã
ãããã«
ä»åã¯ãTensorFlow Lite ã® C++çã§å®è¡ãã¦ã¿ã¾ãããã ãã¶éããªãã¾ãããã精度ãè½ã¡ã¦ãã¨ãããæ°ã«ãªãã¾ããã
ãã®ãããã®åæãããããã«ã次åï¼Raspberry Pi 4のTensorFlow Lite C++をVSCodeでリモートデバッグする - daisukeの技術ブログï¼ã¯ãããã°ç°å¢ãç«ã¡ä¸ãã¦ã¿ããã¨æãã¾ãã
ä»åã¯ä»¥ä¸ã§ãã
æå¾ã¾ã§ãèªã¿ããã ãããããã¨ããããã¾ããã