サイドバー

目次

機械学習とは

プログラミング無しで機械学習

機械学習プログラミング入門

初めてのKeras2.0

初めてのTensorflow(YouTube)

初めてのChainer2.0

初めてのビットコイン

ビットコイン用語集

初めてのSolidityプログラミング

初めての医療統計

初めてのエクセルで医療統計

初めてのEZRで医療統計

初めてのRStudioでレポート作成

スマホアプリ作成ソフトIonic3

スマホアプリ作成ソフトIonic4

Ionicのためのjavascript tips

その他

NiftyCloudMobileBackend

税金など

Dokuwiki

kerasで自前データで機械学習

以前のリビジョンの文書です


(7)Kerasで自前データで機械学習

http://twosquirrel.mints.ne.jp/?p=19448

以下のサイトのコードをコピペです。(このような分かりやすい解説に大感謝です。)

Kerasによる、ものすごくシンプルな画像分類(りんごとオレンジ) hiroeorz@github 2017年02月15日に更新
http://qiita.com/hiroeorz@github/items/ecb39ed4042ebdc0a957

Google検索でりんごとオレンジの画像をそれぞれ25枚ずつゲット(ダウンロード)してくれば、私にもできました!

(環境) Windows 8.1 Anaconda 4.4.0 Python 3.6.1 Tensorflow 1.2.1 Keras 2.0.6

手順

1.C:/python/ フォルダ下に、dataフォルダ以下を作成
2.Anaconda Promptを起動後、jupyter notebookの起動
3.fruit.pyのコードをコピペして、Shift+Enter で実行。
http://qiita.com/hiroeorz@github/items/ecb39ed4042ebdc0a957

1.C:/python/ フォルダ下に、dataフォルダ以下を作成

data/train/apple   ←りんご画像約20個をダウンロード
    /train/orange   ←オレンジ画像約20個をダウンロード
    /test/apple    ←りんご画像約5個をダウンロード
    /test/orange   ←オレンジ画像約5個をダウンロード

この4つのフォルダを作成し、 Google画像検索で出てきた画像を、 それぞれダウンロード。

ファイル名はなんでもよい。 画質もむちゃくちゃ。

2.Anaconda Promptを起動後、jupyter notebookの起動

WindowsのスタートボタンからAnaconda Promptを起動後、

jupyter notebook

と入力して、jupyter notebookを起動 画面右上の方の、「New」>「Python3」で新しいタブを開く

3.fruit.pyのコードをコピペして、Shift+Enter で実行。

以下のコードをコピペして、Shift+Enterで実行。

https://gist.github.com/adash333/20151dd19e3d3275edbd0b121e036c64

上記コードは、下記サイトの、fruit.py のコードを全部コピペして、最後に、モデル可視化のコードを加えたものです。

Kerasによる、ものすごくシンプルな画像分類(りんごとオレンジ) http://qiita.com/hiroeorz@github/items/ecb39ed4042ebdc0a957

以上で、
(1)約20枚ずつのりんごとオレンジの画像から学習(train)し、
(2)その学習したモデルから、別の5枚のりんご画像と5枚のオレンジ画像を、りんごかオレンジかを予測させてみた
(3)今回は、その10枚の予測の正解率は100%であった
結果の図の例
ということになります。

(動画の解説はここまで)

メモ

さらに、以下を組み合わせたい

Kerasで画像分類を試してみる170109
http://yujikawa11.hatenablog.com/entry/2017/01/09/153436

→求めていたのはこれ! 上記サイトのソースコードを以下にコピペ。

# code from http://yujikawa11.hatenablog.com/entry/2017/01/09/153436

from keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions
from keras.preprocessing import image
import numpy as np
import sys

"""
ImageNetで学習済みのVGG16モデルを使って入力画像を予測
"""
# モデルの読み込み
model = VGG16(weights='imagenet')
# 入力画像のロード
filename = "./dog.jpg"
img = image.load_img(filename, target_size=(224, 224))
# 入力画像の行列化
x = image.img_to_array(img)
# 4次元テンソル
x = np.expand_dims(x, axis=0)
# 予測
preds = model.predict(preprocess_input(x))
results = decode_predictions(preds, top=5)[0]
# 結果出力
for result in results:
    print(result)

上記コードを少し変更

# original code from http://yujikawa11.hatenablog.com/entry/2017/01/09/153436

from keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions
from keras.preprocessing import image
import numpy as np
import sys


# 入力画像のロード
filename = "./dog.jpg"
img = image.load_img(filename, target_size=(25, 25))
# 入力画像の行列化
x = image.img_to_array(img)
# 4次元テンソル
x = np.expand_dims(x, axis=0)
# 予測
preds = model.predict(preprocess_input(x))
results = decode_predictions(preds, top=5)[0]
# 結果出力
for result in results:
    print(result)

画像をkerasに読み込ませる方法

画像をコンピューターに認識させるとき コンピューターが理解できるように変換する必要がある

画像の大きさ 例えば、縦24×横24の小さな正方形の集まりとして

それぞれの小さな正方形について、RGB red green blue の値が、0から255まで、256段階

これを、numpy配列に直して、数字の配列に変換することにより、初めて、kerasが、画像を認識することができるようになる。

具体的には、

from keras.preprocessing import image import numpy as np

のあと、

画像のアドレスを指定

作成中

PIL(Pillow)

PIL

PIL(Python Imaging Library)

https://librabuch.jp/blog/2013/05/python_pillow_pil/
Python 3.5 対応画像処理ライブラリ Pillow (PIL) の使い方

today 2013-05-06 Mon person Takahiro Ikeuchi

http://www.mwsoft.jp/programming/computer_vision_with_python/1_1_pil.html
Pillow(Python Imaging Library)のインストールと簡単なサンプルコード

http://qiita.com/Tatejimaru137/items/44646c9bb3799768fa81

http://www.lifewithpython.com/2013/09/pil.html?m=1

keras, model Predict

https://teratail.com/questions/78782
kerasで学習したモデルを実際に試す方法 cloudspider 2017/06/02 20:18 投稿

Kerasで画像分類を試してみる170109
http://yujikawa11.hatenablog.com/entry/2017/01/09/153436
→求めていたのはこれ!

https://www.google.co.jp/url?sa=t&source=web&rct=j&url=https://www.slideshare.net/mobile/yasuyukisugai/3nnkeras&ved=0ahUKEwjjgueEo7bVAhXKJZQKHfoJDMc4ChAWCCwwBA&usg=AFQjCNE5Hev5h7APupU2mfRTOWVW_8-Pdg
mnistで、自分の画像を画像処理するコードの記載がある!

http://qiita.com/tsunaki/items/608ff3cd941d82cd656b keras(tensorflow)で花の画像から名前を特定 tsunaki 2017年02月03日に更新

Keras tips: 様々な画像の前処理をカンタンにやってくれる 20191115
keras.preprocessingのまとめ
http://www.mathgram.xyz/entry/keras/preprocess/img

【超初心者による】Keras(Tensorflowベース)でCNNを使った画像認識 rinigo 2017年03月29日に投稿
http://qiita.com/rinigo/items/66cf94abf7c665a0c7e2 →画像の前処理については、これが一番わかりやすいかも。

KerasでImageNetのハンバーガーと自転車を分類した http://qiita.com/juntaki/items/9a13a3d2217ca223cf03

KerasによるVGG-16を用いた画像分類

Home keras KerasによるVGG-16を用いた画像分類 最終更新: 2017-03-20 15:40
https://kivantium.net/keras-vgg16

chainerの場合

chainerで画像を読み込む際のtips
http://qiita.com/ysasaki6023/items/fa2fe9c2336677821583

作成中

kerasで自前データで機械学習.1501730716.txt.gz · 最終更新: 2018/10/07 (外部編集)