本ブログはアフィリエイト広告を利用しています。

Teachable Machineで作成したモデルで画像を人物、風景、Legoで振り分けてみる

tm-new-pj プログラミング

撮りためてある写真を3つのラベルで振り分けてみました。
Microsoft Lobeで作成したモデルで推論するとconfidenceが0 or 1にしかならない で作成したモデルを使用していきます。

実行環境

  • Windows 10 Pro
  • Python 3.8

Teachable Machineでモデル作成

https://teachablemachine.withgoogle.com/ にアクセスし、使ってみるボタンをクリック
今回は画像振り分けを行うので画像プロジェクトを選択

tm-new-pj

お好きなモデルを選択します。今回は標準の画像モデルを使用していきます。

tm-select-model

ラベル名を変更し、モデル作成用の画像をアップロードします。

tm

画像のアップロード完了後にトレーニング欄の モデルをトレーニングする を実行します。
多少時間掛かりますがそのまま待機する必要があります。

次にモデルが正しく動作しているかプレビューで確認しましょう。
上手く推論出来ていなければ画像の再選定とトレーニング欄の詳細をクリックし、エポック、もしくは学習率を上下させてみて最適と思われる値を探していきます。バッチサイズは改善に恐らく必要ないと説明されてるのでそのままでいいと思います。

モデルのエクスポート

モデルの調整が完了したらプレビュー欄でモデルをエクスポートするをクリックします。
お好きな形式でモデルをエクスポートしてください。
今回はデフォルトで選択されているTensorflowのKerasを使用します。
ここでも多少時間がかかりますが待ちましょう。

tm-model-export

振り分けする

エクスポート画面にサンプルのPythonソースがあるのでこちらを利用していきます。
画像が格納されているフォルダを指定し、配下全ての画像をラベル名のフォルダに振り分けていくよう一部修正しました。
以下がソースです。

from tensorflow.keras.models import load_model
from PIL import Image, ImageOps
import numpy as np
import os
import shutil

path = 'F:\\export\\teachablemachine\\image'
destLegoPath = 'F:\\export\\teachablemachine\\lego'
destHumanPath = 'F:\\export\\teachablemachine\\human'
destLandscapePath = 'F:\\export\\teachablemachine\\landscape'
destOtherPath = 'F:\\export\\teachablemachine\\other'

# Load the model
model = load_model('keras_model.h5')

# Create the array of the right shape to feed into the keras model
# The 'length' or number of images you can put into the array is
# determined by the first position in the shape tuple, in this case 1.
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
# Replace this with the path to your image
for pathname, dirnames, filenames in os.walk(path):
    for filename in filenames:
        image = Image.open(pathname + '\\' + filename)
        #resize the image to a 224x224 with the same strategy as in TM2:
        #resizing the image to be at least 224x224 and then cropping from the center
        size = (224, 224)
        image = ImageOps.fit(image, size, Image.ANTIALIAS)

        #turn the image into a numpy array
        image_array = np.asarray(image)
        # Normalize the image
        normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
        # Load the image into the array
        data[0] = normalized_image_array

        # run the inference
        prediction = model.predict(data)

        # print(prediction)
        if prediction[0][0] > 0.7:
            shutil.move(pathname + '\\' + filename, destHumanPath + '\\' + filename)
        elif prediction[0][1] > 0.7:
            shutil.move(pathname + '\\' + filename, destLegoPath + '\\' + filename)
        elif prediction[0][2] > 0.7:
            shutil.move(pathname + '\\' + filename, destLandscapePath + '\\' + filename)
        else:
            shutil.move(pathname + '\\' + filename, destOtherPath + '\\' + filename)

振り分け結果

対象はとりあえず150ファイルほどで実行しましたが、そこそこ良い感じに振り分けてくれました。
ペットのハムスターがLegoに振り分けられてたり人(顔のアップ)がどれにも振り分けされずにotherフォルダに入ったりイマイチなところもあるにはりますが、それはモデル作成時の画像の選定、パラメータ(エポック、学習率)の調整が不慣れであることが原因と個人的には思っています。
もっと触ってみて精度を高くしていきたい・・・。

以上です。

コメント

タイトルとURLをコピーしました