【Edge TPU】backprop_last_layer.pyを用いて転移学習を行ってみる
はじめに
こんにちは、がんがんです。Edge TPUのアップデートとしてexampleにbackprop_last_layer.py
とimprinting_learning.py
が追加されていました(9月のアップデートかな?)。
プログラムのコメントを見てみるとこの2つはどちらも転移学習用のプログラムのようです。
そこで今回はbackprop_last_layer.py
を用いて転移学習を行っていこうと思います。
前回行った転移学習の実験は以下に掲載しております。
gangannikki.hatenadiary.jp
目的
backprop_last_layer.py
で転移学習を行う
環境(学習および実行環境)
- ラズパイ:Raspberry Pi 3 Model B+
- 環境は以下のとおりです
$ cat /etc/os-release PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)" NAME="Raspbian GNU/Linux" VERSION_ID="9" VERSION="9 (stretch)" VERSION_CODENAME=stretch ID=raspbian ID_LIKE=debian HOME_URL="http://www.raspbian.org/" SUPPORT_URL="http://www.raspbian.org/RaspbianForums" BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
再学習していく
基本的にはプログラムのコメントに書かれているとおりに試していきます。
まずはディレクトリを作成し、再学習用の花の画像をDLします。
$ mkdir -p ~/tmp/retrain/ $ curl http://download.tensorflow.org/example_images/flower_photos.tgz | tar xz -C ~/tmp/retrain
以下のコマンドで再学習を実行します。
$ python3 edgetpu/examples/backprop_last_layer.py \ --data_dir ~/tmp/retrain/flower_photos \ --embedding_extractor_path edgetpu/test_data/mobilenet_v1_1.0_224_quant_embedding_extractor_edgetpu.tflite \ --output_dir ~/tmp/retrain/output/ Reading dir: /home/pi/tmp/retrain/flower_photos/sunflowers, which has 699 images Reading dir: /home/pi/tmp/retrain/flower_photos/dandelion, which has 898 images Reading dir: /home/pi/tmp/retrain/flower_photos/daisy, which has 633 images Reading dir: /home/pi/tmp/retrain/flower_photos/tulips, which has 799 images Reading dir: /home/pi/tmp/retrain/flower_photos/roses, which has 641 images Extract embeddings for data_train Extract embeddings for data_val Data preprocessing takes 68.33 seconds Loss 2.59, train acc 29.19%, val acc 31.06% Loss 0.42, train acc 85.01%, val acc 81.47% Loss 0.29, train acc 89.34%, val acc 85.83% Loss 0.18, train acc 91.04%, val acc 88.01% Loss 0.22, train acc 92.37%, val acc 88.56% Training takes 5.38 seconds Model /home/pi/tmp/retrain/output/retrained_model_edgetpu.tflite saved. Label map /home/pi/tmp/retrain/output/label_map.txt saved. Saving retrained model and label map takes 0.07 seconds Saved tflite model test accuracy: 88.56% Checking test accuracy takes 7.31 seconds
テストしてみる
前回のテスト時にも使用したひまわりの画像を使ってテストしてみます。画像はこんな感じです。
$ python3 edgetpu/examples/classify_image.py \ --model tmp/retrain/output/retrained_model_edgetpu.tflite \ --label tmp/retrain/output/label_map.txt \ --image edgetpu_transfer/sunflower.jpg --------------------------- sunflowers Score : 0.980469
かなりいい結果が出てびっくりしてます。別の画像でも試してみます。
$ python3 edgetpu/examples/classify_image.py \ --model tmp/retrain/output/retrained_model_edgetpu.tflite \ --label tmp/retrain/output/label_map.txt \ --image edgetpu_transfer/Dandelion.jpg --------------------------- dandelion Score : 0.84375 --------------------------- sunflowers Score : 0.140625
おわりに
今回はexamples/backprop_last_layer.py
を用いて転移学習を行いました。個人的にはこっちのほうがすごく簡単な印象でした。別の画像でも転移学習を実行してみようと思います。