LoginSignup
161
131

More than 5 years have passed since last update.

TensorFlow LiteがGPUをサポートしたらしいのでiOSで試してみた

Last updated at Posted at 2019-01-19

TensorFlow LiteがAndroidやiOSデバイスのGPUをサポートしたようです1

バックエンドはAndroidではOpenGL ES 3.1、iOSではMetal。

This new backend leverages:

  • OpenGL ES 3.1 Compute Shaders on Android devices
  • Metal Compute Shaders on iOS devices

iPhone 7では最大6倍の高速化が見られたようです。

By leveraging the new GPU backend in the future, inference can be sped up from ~4x on Pixel 3 and Samsung S9 to ~6x on iPhone7.

というわけで以下のチュートリアルページに従って試してみました。

試すまでの最小手順

TFリポジトリには昔からiOSでリアルタイム物体認識を行うサンプルが同梱されているのですが、これをGPUで処理するように変更していきます。

チュートリアルページでは実行速度を最大化するように細かい設定も書いてますが(Releaseビルドしろとか)、動かすために必要なものだけ抜粋して書いていきます。

1. モデルをダウンロード

スクリプトを走らせてサンプルで利用するモデルをダウンロードします。

$ cd tensorflow/lite/examples/ios
$ sh ./download_models.sh

2. GPUサポート版TF Liteを使うようPodfileを変更

# pod 'TensorFlowLite', '1.12.0'
pod 'TensorFlowLiteGpuExperimental'

で、pod installします。

3. コードの修正

CameraExampleViewController.hの、TFLITE_USE_CONTRIB_LITEの値を1から0に、

// #define TFLITE_USE_CONTRIB_LITE 1
#define TFLITE_USE_CONTRIB_LITE 0

TFLITE_USE_GPU_DELEGATEの値を0から1に変更します。

// #define TFLITE_USE_GPU_DELEGATE 0
#define TFLITE_USE_GPU_DELEGATE 1

4. Build & Run!

tflite_camera_example.xcworkspaceをXcodeで開いて実機で実行します。

tflite.2019-01-19 20_02_02.gif

GPUで動いていることを確認する

よく見る物体認識デモなので、これだけだとGPUで動いている実感がないかもしれません。

XcodeのGPU Reportで見てみるとちゃんとGPUが働いていました。

Screen Shot 2019-01-19 at 19.28.31 copy.jpg

ちなみにこのときCPUの負荷は60%前後。(iPhone XS / iOS 12.1.2)

他の公式配布モデル

物体認識以外にも、魅力的なモデルが既に配布されています。以下のモデルはモバイルデバイスGPUで利用可能です。

DeepLab segmentation

セグメンテーション(領域分割)用のモデル。

image segmentation model that assigns semantic labels (e.g., dog, cat, car) to every pixel in the input image

image1.png

(画像はGoogle AI Blogの記事より)

PoseNet

ポーズ(姿勢)推定用のモデル。

vision model that estimates the poses of a person(s) in image or video

camera.gif

(画像はこちらから)

MobileNet SSD object detection

物体認識モデル。複数のオブジェクトをバウンディングボックスつきで検出してくれます。

image classification model that detects multiple objects with bounding boxes

ssd_at_big_table_compressed.2019-01-19 20_28_35.gif

(画像はこちらから)

まとめ

TensorFlow LiteがAndroid/iOSのGPUを利用するようになったとのことで、iOSで試してみました。姿勢推定、セグメンテーション、バウンディングボックス付き物体認識と魅力的なモデル群が既に利用可能なので、そちらも試してみたいと思います。

TensorFlow x iOS関連記事

iOS x 機械学習 関連記事


  1. とはいえまだDeveloper Preview 

161
131
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
161
131