0
4

More than 3 years have passed since last update.

AndroidにLinuxを入れてtensorflow liteを実行する

Last updated at Posted at 2020-09-05

Android スマホに userland で linux を入れて tensorflow lite を実行します。

下準備

UserLAnd - Google Play のアプリ
https://play.google.com/store/apps/details?id=tech.ula&hl=ja
※userland で linux を入れるとポート番号 2022 で ssh ログインができるようになります。

IP Tools: WiFi Analyzer - Google Play のアプリ
https://play.google.com/store/apps/details?id=com.ddm.iptools&hl=ja
※IP アドレスの確認ができます。192.168 から始まるものを探します。

Android端末へsshログイン

ssh username@192.168.xxx.xxx -p 2022 ;

アップデートとユーティリティのインストール

sudo apt update ;
sudo apt upgrade -y ;
sudo apt install -y curl git unzip wget ;

python3 関係のインストール

sudo apt install -y python3-dev python3-pip python3-setuptools ;
sudo apt install -y jupyter-qtconsole jupyter-notebook python3-jupyter-client python3-jupyter-console python3-spyder spyder3 ;
sudo python3 -m pip install --upgrade pip ;

python3 のバージョン確認

python3 --version

tensorflow lite のインストール

以下のページからバージョンとCPUに合致するものをダウンロードします
https://www.tensorflow.org/lite/guide/python

今回は python3.7 の arm64bit 版をダウンロード

wget https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_aarch64.whl ;
pip3 install tflite_runtime-2.1.0.post1-cp37-cp37m-linux_aarch64.whl ;
pip3 install numpy ;

tflite のバージョン確認

python3 -c 'import tflite_runtime as tf; print(tf.__version__)'  # for Python 3

サンプルソースの実行(tfliteに少し書き換えが必要です)
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/examples/python/label_image.py

プログラムのダウンロード

wget https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/examples/python/label_image.py
 ;

以下 tflite 用に書き換え

import tensorflow as tf
↓
import tflite_runtime.interpreter as tflite
interpreter = tf.lite.Interpreter(model_path=args.model_file)
↓
interpreter = tflite.Interpreter(model_path=args.model_file)
model_path=args.model_file, num_threads=args.num_threads)
↓
model_path=args.model_file)

サンプル画像やモデルをダウンロード
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/examples/python/README.md

curl https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/lite/examples/label_image/testdata/grace_hopper.bmp > /tmp/grace_hopper.bmp
curl https://storage.googleapis.com/download.tensorflow.org/models/mobilenet_v1_2018_02_22/mobilenet_v1_1.0_224.tgz | tar xzv -C /tmp
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
mv /tmp/mobilenet_v1_1.0_224/labels.txt /tmp/

実行

python3 label_image.py \
  --model_file /tmp/mobilenet_v1_1.0_224.tflite \
  --label_file /tmp/labels.txt \
  --image /tmp/grace_hopper.bmp

補足

tensorflow の git からソースビルドも通ったのですが、python にくくりつける方法が良くわからなかったので知っているかたいましたらお知らせお願いしますm(_ _)m

参考
Pythonクイックスタート  |  TensorFlow Lite
https://www.tensorflow.org/lite/guide/python

0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4