ä»åãAIã®éååã«ã¤ãã¦å¦ãã§ããã¾ããè«æãèªããã¨ã¯ç¶ç¶ãã¤ã¤ãä»åã¯ãå®éã«éååã¢ãã«ãåããã¦ãæ¨è«ã®é«éåãå®æãããã¨æãã¾ãã
ã¾ãã¯ãVirtualBox ã® Ubuntu 22.04 ã§åããã¦ããã¾ãããTensorFlow Lite ã¯ãARM CPU ã«æé©åããã¦ããã¨æãã®ã§ãæ§è½ãåºãªãããããã¾ããããã®å¾ãRaspberry Pi 4ï¼Raspberry Pi OSï¼ã§ãåããã¦ããã¾ãã
ããã§ã¯ããã£ã¦ããã¾ãï¼
ã¯ããã«
ãAIã¢ãã«ã®éååãã®è¨äºä¸è¦§ã§ããè¯ãã£ããåèã«ãã¦ãã ããã
AIã¢ãã«ã®éååã®è¨äºä¸è¦§
ã¨ã³ã¸ãã¢ã°ã«ã¼ãã®ã©ã³ãã³ã°ã«åå ä¸ã§ãã
æ°æ¥½ã«ãããã¨ãããããé¡ããããã¾ãð
ããã§ã¯ãã£ã¦ããã¾ãï¼
TensorFlow Lite ã®ç°å¢æ§ç¯
TensorFlow Lite ã®å
¬å¼ãµã¤ãã§ãã
www.tensorflow.org
æçµçã«ã¯ãRaspberry Pi ã§åããã¾ãããã¾ãã¯ãæ軽㫠Ubuntu ã®ç°å¢ã§åããã¦ããã¾ãã
ã¾ããRaspberry Pi ãå«ã Linuxãã©ãããã©ã¼ã ã§åä½ãããå ´åãPython 㨠C++ ã®é¸æè¢ãããããã§ããã¾ãã¯ãç°¡å㪠Python ã§åããã¦ããã¾ãã
å
¬å¼ãµã¤ãã®æé ã§ãã
www.tensorflow.org
ããããæ¸ãã¦ã¾ãããã¨ããããåãããã人ã¯ãtflite-runtime
ãå
¥ãã¦è©¦ãã¦ããã¨ãããã¨ã§ãããã¡ããã¨åããã«ã¯ãTensorFlow ã®ããã±ã¼ã¸ãå¿
è¦ã¨æ¸ããã¦ãã¾ãã
ã¾ããPythonã¹ã¯ãªãããlabel_image.pyããå®è¡ãã¦ããã¨æ¸ããã¦ãã¾ãã®ã§ããã¦ã³ãã¼ããã¦ããã¾ãï¼åããã«ããã£ãã®ã§ããªã³ã¯è²¼ã£ã¦ããã¾ããï¼ã
ãã¨ãæ¸ããã¦ãªãã§ãããç»åãã¡ã¤ã«ãå¿
è¦ãªã®ã§ããgrace_hopper.bmpãããã¦ã³ãã¼ããã¦ããã¾ãï¼ãã¡ãããªã³ã¯è²¼ã£ã¦ããã¾ããï¼ã
ãã¨ãã©ãã«ãã¡ã¤ã«ï¼æ£è§£ã©ãã«ãæ¸ããããã¡ã¤ã«ï¼ãå¿
è¦ã§ããã¡ããæ¸ããã¦ã¾ããããä¸è¨ã®ããã«ãã¦ãã¦ã³ãã¼ããã¦ããã¾ãã/tmp/mobilenet_v1_1.0_224/labels.txt
ã«åºåãããã®ã§ãã«ã¬ã³ããã£ã¬ã¯ããªã«ã³ãã¼ãã¦ããã¾ãï¼ç´æ¥ãã¦ã³ãã¼ãã§ããã¨ãããè¦ã¤ãããã¾ããã§ããï¼ã
$ curl https://storage.googleapis.com/download.tensorflow.org/models/mobilenet_v1_1.0_224_frozen.tgz | tar xzv -C /tmp mobilenet_v1_1.0_224/labels.txt
ã§ã¯ãæ©é tflite-runtime
ãã¤ã³ã¹ãã¼ã«ãã¾ãï¼tfliteãã£ã¬ã¯ããªãä½æãã¦ãããã«ä»®æ³ç°å¢ãä½ã£ã¦ä½æ¥ãã¾ãï¼ã
ããã±ã¼ã¸ã足ããªãã¨è¨ãããã®ã§ãæ示éãã«å
¥ãã¦ããã¾ããã
$ mkdir tflite
$ cd tflite
$ python3 -m venv venv
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt install python3.10-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
Failing command: /home/daisuke/svn_/tflite/venv/bin/python3
$ rm -rf ./*
$ sudo apt install python3.10-venv
$ source venv/bin/activate
(venv) $ pip install tflite-runtime
Successfully installed numpy-1.26.4 tflite-runtime-2.14.0
ããã§å®è¡ç°å¢ã®æºåã¯å®äºã§ãï¼
TensorFlow Lite ã¢ãã«ã®å
¥æ
å
¬å¼ãµã¤ãã«å¦ç¿æ¸ã¿ã®tfliteã¢ãã«ãããã¤ãå
¬éããã¦ãã¾ãã
www.tensorflow.org
ä»åã¯ãç»ååé¡ã®ã¢ãã«ã使ãã¾ããå
·ä½çã«ã¯ãMobilenet V1ã®éååãã¦ãªãã¢ãã«ã¨éååãã¦ãã¢ãã«ã使ãã¾ãã
- Mobilenet_V1_1.0_224_quantï¼éååã¢ãã«ï¼mobilenet_v1_1.0_224_quant.tgzï¼
- Mobilenet_V1_1.0_224ï¼éååãã¦ãªãã¢ãã«ï¼mobilenet_v1_1.0_224.tgzï¼
ãã¡ã¤ã«åã«ã¤ãã¦èª¬æãã¦ããã¾ãããmobilenetãã¯ã¢ãã«ã®ååã§ããv1ãã¯ãMobileNetã®æåã®ãã¼ã¸ã§ã³ã¨ããæå³ã§ããæè¿ãMobileNetV4ãçºè¡¨ããã¦ã¾ããããã1.0ãã¯ããããã¯ã¼ã¯ã®ã·ã¥ãªã³ã¯ï¼åæ¸ï¼ã®ãã©ã¡ã¼ã¿Î±ã®å¤ã§ã0.25ã0.5ã0.75ã1.0ã®4ã¤ããé¸ã¹ã¦ãæ°åãå°ããã»ã©ãã·ã¥ãªã³ã¯ãããã¢ãã«ã¨ãªãã¾ããã224ãã¯å
¥åç»åãµã¤ãºï¼224x224ï¼ã§ãã
MobileNetV1ã®è«æã«å½æã®ããããã®ç²¾åº¦ãæ¸ããã¦ã¾ããã®ã§ãè²¼ã£ã¦ããã¾ãã
ã§ã¯ãå
ã»ã©ã®ãlabel_image.pyããç½®ãã¦ããã¦ã³ãã¼ããããã¡ã¤ã«ã解åãã¾ãã
(venv) $ mkdir -p models/mobilenet_v1_1.0_224
(venv) $ mv mobilenet_v1_1.0_224.tgz models/mobilenet_v1_1.0_224/
(venv) $ mkdir -p models/mobilenet_v1_1.0_224_quant
(venv) $ mv mobilenet_v1_1.0_224_quant.tgz models/mobilenet_v1_1.0_224_quant/
(venv) $ cd models/mobilenet_v1_1.0_224
(venv) $ tar zxvf mobilenet_v1_1.0_224.tgz
(venv) $ cd ../mobilenet_v1_1.0_224_quant/
(venv) $ tar zxvf mobilenet_v1_1.0_224_quant.tgz
(venv) $ cd ../../
(venv) $ tree -I venv/
.
|-- grace_hopper.bmp
|-- label_image.py
|-- labels.txt
`-- models
|-- mobilenet_v1_1.0_224
| |-- mobilenet_v1_1.0_224.ckpt.data-00000-of-00001
| |-- mobilenet_v1_1.0_224.ckpt.index
| |-- mobilenet_v1_1.0_224.ckpt.meta
| |-- mobilenet_v1_1.0_224.tflite
| |-- mobilenet_v1_1.0_224.tgz
| |-- mobilenet_v1_1.0_224_eval.pbtxt
| |-- mobilenet_v1_1.0_224_frozen.pb
| `-- mobilenet_v1_1.0_224_info.txt
`-- mobilenet_v1_1.0_224_quant
|-- mobilenet_v1_1.0_224_quant.ckpt.data-00000-of-00001
|-- mobilenet_v1_1.0_224_quant.ckpt.index
|-- mobilenet_v1_1.0_224_quant.ckpt.meta
|-- mobilenet_v1_1.0_224_quant.tflite
|-- mobilenet_v1_1.0_224_quant.tgz
|-- mobilenet_v1_1.0_224_quant_eval.pbtxt
|-- mobilenet_v1_1.0_224_quant_frozen.pb
`-- mobilenet_v1_1.0_224_quant_info.txt
3 directories, 19 files
TensorFlow Lite ã§ã¢ãã«ãåãã
ã¾ãã¯ãVirtualBox ã® Ubuntu 22.04 ã§åããã¦ããã¾ãããã®å¾ãRaspberry Pi 4ï¼Raspberry Pi OSï¼ã§ãåããã¾ãã
VirtualBox ã® Ubuntu 22.04 ã§å®è¡ãã
æ©éå®è¡ãããã¨ããã§ãããtflite-runtime
ã®å ´åãlabel_image.py ã2ãæå¤æ´ããªããã°ãªããªãããã§ãã
1ãæç®ã§ããTensorFlowãã¤ã³ã¹ãã¼ã«ããå ´åãæ³å®ãã¦ããããã§ãã
import tflite_runtime.interpreter as tflite
2ãæç®ã§ãããã¡ãããTensorFlowãæ³å®ããã½ã¼ã¹ã³ã¼ãã«ãªã£ã¦ãã¾ããã
interpreter = tflite.Interpreter(
å®è¡ããåã«ãå
¥åããç»åãã¡ã¤ã«ãè²¼ã£ã¦ããã¾ããã°ã¬ã¼ã¹ã»ãããã¼ããã¨ãããCOBOLè¨èªãéçºããå¦è
ããã§ãã女æ§ã ã£ããã§ãããããã¯åãã¦ç¥ãã¾ããã
ãã£ã¨å®è¡ã§ãã¾ããPILãç¡ãã¨è¨ãããã®ã§ãpillowãã¤ã³ã¹ãã¼ã«ãã¦ããã¾ãã
(venv) $ python label_image.py --model_file models/mobilenet_v1_1.0_224/mobilenet_v1_1.0_224.tflite --label_file ./labels.txt --image grace_hopper.bmp
Traceback (most recent call last):
File "/home/daisuke/svn_/tflite/label_image.py", line 21, in <module>
from PIL import Image
ModuleNotFoundError: No module named 'PIL'
(venv) $ pip install pillow
Successfully installed pillow-10.3.0
(venv) $ python label_image.py --model_file models/mobilenet_v1_1.0_224/mobilenet_v1_1.0_224.tflite --label_file ./labels.txt --image grace_hopper.bmp
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
0.919721: 653:military uniform
0.017762: 907:Windsor tie
0.007507: 668:mortarboard
0.005419: 466:bulletproof vest
0.003828: 458:bow tie, bow-tie, bowtie
time: 152.961ms
å®è¡ã§ãã¾ããã152ms ã ã£ãããã§ããè»æ㨠92.0% ã§å¤å®ãã¦ããçµæãªã®ã§ãæ£ããåãã¦ããã§ããç¶ãã¦ãéååã¢ãã«ãå®è¡ãã¾ãã
(venv) $ python label_image.py --model_file models/mobilenet_v1_1.0_224_quant/mobilenet_v1_1.0_224_quant.tflite --label_file ./labels.txt --image grace_hopper.bmp
0.874510: 653:military uniform
0.031373: 907:Windsor tie
0.015686: 668:mortarboard
0.011765: 466:bulletproof vest
0.007843: 458:bow tie, bow-tie, bowtie
time: 873.819ms
éååã¢ãã«ã®æ¹ããã ãã¶é
ãã§ãããè»æã¨ããå¤å®ã¯ã87.5%ã§ããã
Raspberry Pi 4 ã® Raspberry Pi OS ã§å®è¡ãã
Raspberry Pi 4 ã§ããã£ã¦ã¿ã¾ãã
$ uname -a
Linux raspberrypi 6.6.20+rpt-rpi-v8
ã¾ããéååãã¦ããªãã¢ãã«ã§ãã
$ python label_image.py --model_file models/mobilenet_v1_1.0_224/mobilenet_v1_1.0_224.tflite --label_file ./labels.txt --image grace_hopper.bmp
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
0.919721: 653:military uniform
0.017762: 907:Windsor tie
0.007507: 668:mortarboard
0.005419: 466:bulletproof vest
0.003828: 458:bow tie, bow-tie, bowtie
time: 144.901ms
VirtualBox ã® Ubuntu22.04 ã¨åããããªæãã§ãããåããã92.0% ã§ããç¶ãã¦ãéååã¢ãã«ã§ãã
$ python label_image.py --model_file models/mobilenet_v1_1.0_224_quant/mobilenet_v1_1.0_224_quant.tflite --label_file ./labels.txt --image grace_hopper.bmp
0.874510: 653:military uniform
0.031373: 907:Windsor tie
0.015686: 668:mortarboard
0.011765: 466:bulletproof vest
0.007843: 458:bow tie, bow-tie, bowtie
time: 99.783ms
ã ãã¶æ©ããªãã¾ãããIntel CPU ã§ã¯ãå¹æãç¡ãã£ããã¨ãããæªåãã¾ããããARM CPUã§å®è¡ããã¨æ©ããªãã¾ããã精度ã¯ãVirtualBox ã® Ubuntu22.04 ã¨åãã87.5%ã§ããã
ãããã«
ä»åã¯ãTensorFlow Lite ã®å®è¡ããã£ã¦ã¿ã¾ããã
tflite-runtime ã使ã£ã¦ã¿ã¾ããããããããããã«ãã¹ã¬ããã§åãã¦ãªãã®ã§ã¯ãªããã¨æãã¾ãããã¨ãPython ãã C++ ã®æ¹ãéããããããªãã§ãã
ãã®ãããã次åï¼TensorFlow Lite C++で量子化モデルをRaspberry Pi 4で動かす - daisukeの技術ブログï¼ã¯ãã£ã¦ããããã¨æãã¾ãã
ä»åã¯ä»¥ä¸ã§ãã
æå¾ã¾ã§ãèªã¿ããã ãããããã¨ããããã¾ããã