LoginSignup
3
5

More than 5 years have passed since last update.

今更ながらKerasでCIFAR-10の画像認識をやってみた - 画像認識編 -

Posted at

はじめに

  • 前回、学習したモデルを元に画像認識やってみました。
  • 精度は良好そうです。

予測プログラム

#!/usr/local/bin/python3
#!-*- coding: utf-8 -*-

import os
import numpy as np
from keras.models import model_from_json
from keras.preprocessing.image import load_img, img_to_array

if __name__ == '__main__':

    # モデルを読み込む
    model_json = open('model.json').read()
    model = model_from_json(model_json)
    model.load_weights('model.h5')

    # 画像を読み込む
    image = load_img('{画像ファイルパス}', target_size=(32, 32))

    # 配列に変換
    x = img_to_array(image)

    # 次元数を増やす
    x = np.expand_dims(x, axis=0)

    # 入力データは[0,1]の範囲に正規化
    x = x.astype('float32')
    x /= 255.0

    # 予測
    preds = model.predict(x)

    # 最大値のインデックスを取得
    answer = np.argmax(preds)

    cifar_map = {
        0: "airplane",
        1: "automobile",
        2: "bird",
        3: "cat",
        4: "deer",
        5: "dog",
        6: "frog",
        7: "hourse",
        8: "ship",
        9: "truck"
    }

    print(cifar_map[answer])

予測結果

  • 以下5枚でやりました。

フェラーリ

1.jpg

automobile

ミニチュアダックスフンド

2.jpg

frog

3.jpg

hourse

大型客船

4.jpg

ship

ディープインパクト

5.jpg

hourse

感想

  • 4足歩行動物は特徴量似ているからわかりづらい?
  • 画像加工して顕著性だしたりするともう少し精度あがるかも。
3
5
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
3
5