忍者ブログ

設備のマニアどっとこむ

IoT開発、設備・DIYのブログ!

Tensorflowで画像分類した話

なかなか使いこなせないでいるTensorflowですが、画像分類をやってみました。

VGG16という大量にある学習済みモデルを用いて画像を判別。可能性の高いベスト5をAIに選んでもらいました。

コードはこちら↓
from keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions
from keras.preprocessing import image
import numpy as np
import sys
"""
ImageNetで学習済みのVGG16モデルを使って入力画像のクラスを予測する
"""
# 学習済みのVGG16をロード
# 構造とともに学習済みの重みも読み込まれる
model = VGG16(weights='imagenet')
# model.summary()
# 引数で指定した画像ファイルを読み込む
# サイズはVGG16のデフォルトである224x224にリサイズされる
img = image.load_img('C:\\Users\\masashi\\Desktop\\photo.jpg', target_size=(224, 224))
# 読み込んだPIL形式の画像をarrayに変換
x = image.img_to_array(img)
# 3次元テンソル(rows, cols, channels) を
# 4次元テンソル (samples, rows, cols, channels) に変換
# 入力画像は1枚なのでsamples=1でよい
x = np.expand_dims(x, axis=0)
# Top-5のクラスを予測する
# VGG16の1000クラスはdecode_predictions()で文字列に変換される
preds = model.predict(preprocess_input(x))
results = decode_predictions(preds, top=5)[0]
for result in results:
    print(result)



例えばこんな犬の画像だと…


('n02087394', 'Rhodesian_ridgeback', 0.58250064)
('n02090379', 'redbone', 0.1364721)
('n02099601', 'golden_retriever', 0.058095142)
('n02088466', 'bloodhound', 0.055783514)
('n02106662', 'German_shepherd', 0.03908478)




このように犬の種類が出ます。

参考にしたサイトです↓
http://aidiary.hatenablog.com/entry/20170104/1483535144

ラズパイでできればと思ったんですが、全然だめでした…






拍手[0回]

PR

コメント

翻訳(Translate)

プロフィール

HN:
佐々木 雅史
性別:
男性
自己紹介:
2021年 ラズパイ、M5stackを用いたIoT開発を専門で受注するアルティメンテを設立。代表を務める。
・2020年ラズパイコンテスト優良賞受賞
・設備系資格多数(電験3種、消防設備士甲4、2級ボイラー技士、危険物乙4、電工2種、技術士補(電気・電子)、エネ電、フォークリフトなど)

AD

カレンダー

03 2024/04 05
S M T W T F S
1 2 3 4 5
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30