はじめに
仮説検証を学習していきたいと思います。
(1) どうなりたいのか(狙う結果)
(2) 何によって確かめるのか(指標)?
(3) そのために何をするのか?
(4) なぜ狙う効果が得られるのか?
ポイント
- 仮説検証の内容は、具体的な数値にしましょう
- 各項目で、内容が一致しない事態が発生しないようにしましょう
仮説検証を学習していきたいと思います。
(1) どうなりたいのか(狙う結果)
(2) 何によって確かめるのか(指標)?
(3) そのために何をするのか?
(4) なぜ狙う効果が得られるのか?
以下のサイトを見てTensorFlow Object Detection APIをWindowsで使ってみようと思います。
TensorFlow Object Detection APIは、TensorFlowを利用して、画像に写っている物体を検出するためのフレームワークです。
インストール手順・実行方法は、Samurai Blog様のブログが素晴らしい記事を書いていますので、そちらを参考にするといいと思います。
(ただし、注意点にも記載しているTensorFlowのバージョンには注意)
https://www.sejuku.net/blog/56695
TensorFlowのバージョンはv1.12以上である必要があります。かなり新しいTensorFlowバージョンでなければ動かないため、注意が必要です。(2019年4月28日時点)
古いTensorflow Versionでは、以下のように正しく動きません。
ImportError Traceback (most recent call last) <ipython-input-1-34f5cdda911a> in <module> 18 19 if StrictVersion(tf.__version__) < StrictVersion('1.12.0'): ---> 20 raise ImportError('Please upgrade your TensorFlow installation to v1.12.*.') ImportError: Please upgrade your TensorFlow installation to v1.12.*.
以下の記事を見ながら作業を進めていきます。
TensorFlow Object Detection APIをWindowsで使ってみた - Qiita
python - protoc object_detection/protos/*.proto: No such file or directory - Stack Overflow
for /f %i in ('dir /b object_detection\protos\*.proto') do protoc object_detection\protos\%i --python_out=.
(tensorflow_gpu) C:\Users\uv2ut\Jupyter_Project_GPU\TensorFlow_Object_Detection_API\models-master\research>python object_detection/builders/model_builder_test.py ......C:\Users\uv2ut\Anaconda3\envs\tensorflow_gpu\lib\site-packages\numpy\lib\type_check.py:546: DeprecationWarning: np.asscalar(a) is deprecated since NumPy v1.16, use a.item() instead 'a.item() instead', DeprecationWarning, stacklevel=1) .C:\Users\uv2ut\Anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\python\util\tf_inspect.py:55: DeprecationWarning: inspect.getargspec() is deprecated since Python 3.0, use inspect.signature() or inspect.getfullargspec() if d.decorator_argspec is not None), _inspect.getargspec(target)) ......... ---------------------------------------------------------------------- Ran 16 tests in 0.092s OK
サンプルノートブックファイルは以下にあります。
(任意のフォルダー)\research\object_detection\object_detection_tutorial.ipynb
上記のサンプルノートブックファイルが参照する画像ファイルは以下のフォルダーにあります。
(任意のフォルダー)\research\object_detection\test_images
私の環境下では、以下のファイルにサンプルコードの画像ファイルがありました。
C:\Users\uv2ut\obj-detect\models-master\research\object_detection\test_images
[侍エンジニア塾] [TensorFlow] WindowsでObject Detection APIを試そう
【物体認識】TensorflowのObject Detection APIをWindows ローカルでやるとハマる所【ラジコン自動運転】
matplotlibでグラフを書きます。
import matplotlib.pyplot as plt plt.style.use('seaborn-whitegrid') import numpy as np %matplotlib inline # numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None) # num: 生成する配列(ndarray)の要素数を指定。 # endpoint: 生成する数列において、stopを要素に含むかどうかを指定。Trueなら含み、Falseなら含まない。 # retstep: 生成された配列(ndarray)の後に公差を表示するかどうかを指定。Trueなら表示し、Falseなら非表示。 # dtype: 出力された配列(ndarray)のデータ型を指定。指定がない場合、データ型はfloat。 x = np.linspace(0, 10, 1000) print(x) print(type(x)) # figureの縦横の大きさ fig = plt.figure() # figure内の枠の大きさとどこに配置している。subplot(行の数,列の数,何番目に配置しているか) ax = plt.axes() plt.plot(x, np.sin(x)) plt.plot(x, np.cos(x))
[ 0. 0.01001001 0.02002002 0.03003003 0.04004004 0.05005005 0.06006006 0.07007007 0.08008008 0.09009009 0.1001001 0.11011011 ・・・ 9.90990991 9.91991992 9.92992993 9.93993994 9.94994995 9.95995996 9.96996997 9.97997998 9.98998999 10. ] <class 'numpy.ndarray'>
matplotlib.pyplot.subplotを使って規則正しくグラフを書きます。
2×2で4枚のグラフを描きます。
プロット番号は、左上が 1 で、右方向、下方向へいくにしたがい値が大きくなります。 たとえば、2 × 2 のグリッドの場合は
1 2
3 4
となります。
%matplotlib inline import matplotlib.pyplot as plt plt.style.use('seaborn-whitegrid') import numpy as np x = np.linspace(-3, 3, 20) y1 = x y2 = x ** 2 y3 = x ** 3 y4 = x ** 4 fig = plt.figure() # 左上 plt.subplot(2,2,1) plt.plot(x,y1) # 右上 plt.subplot(2,2,2) plt.plot(x, y2) # 左下 plt.subplot(2,2,3) plt.plot(x,y3) # 右下 plt.subplot(2,2,4) plt.plot(x,y4)
グリッド上に規則正しくグラフを配置する場合はsubplotメソッドを使います。
subplot(行数, 列数, プロット番号)
行数 × 列数 のグリッドに figure が分割され、プロット番号の位置に座標軸が作成されます。
NumPyのnp.linspaceは、線形に等間隔な数列を生成する関数です。
同様の数列をnp.arangeで生成することもできますが、np.linspaceを使用したほうがコード量を減らすことができます。
numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None)
params:
パラメータ名 | 型 | 概要 |
---|---|---|
start | intまたはfloat | 数列の始点を指定します。 |
stop | intまたはfloat | 数列の終点を指定します。 |
num | int | (省略可能)初期値50 生成する配列(ndarray)の要素数を指定します。 |
endpoint | bool値 | (省略可能)初期値True 生成する数列において、stopを要素に含むかどうか指定します。Trueなら含み、Falseなら含みません。 |
retstep | boo値 | (省略可能)初期値False 生成された配列(ndarray)の後に公差を表示するかどうか指定します。Trueなら表示し、Falseなら非表示です。 |
dtype | dtype | (省略可能)初期値None 出力された配列(ndarray)のデータ型を指定します。dtypeが指定されない場合、データ型は基本的にはfloatになっている。 |
returns:
num等分された等差数列を要素とするndarrayが返されます。
C++で、関数型プログラミングでいうfilter関数(抽出の処理を行う関数のこと)を使う場合は、以下のテンプレート関数を使うといいです。
以下のコード例では、ラムダ式と挿入イテレータが使用されています。
挿入イテレータ(insert iterator)は、イテレータに対する代入処理を挿入処理に置き換える特殊な出力イテレータです。
#include <iostream> #include <vector> #include <numeric> #include <algorithm> using namespace std; void getCharVector(std::vector<char> &someVector, const std::string someString) { // 1文字つずつ取り出してvector<char>に格納する for (auto var : someString) { someVector.push_back(var); } } int main(void) { std::vector<char> temp; getCharVector(temp, "a1a2a3a4a5a6a7a8a9a10"); std::vector<char> filtered; // copy_ifテンプレート関数は、条件を満たした要素のみをコピーする機能 std::copy_if(temp.begin(), temp.end(), std::back_inserter(filtered), [](char c) { return c == 'a'; }); std::for_each(filtered.begin(), filtered.end(), [](char c) {std::cout << c << std::endl; }); // std::count_if とは、第3引数で与えられる単項関数(Unary function)の条件を満たす要素が配列やコンテナにいくつ含まれているか返すテンプレート関数 int sum = std::count_if(temp.begin(), temp.end(), [](char c) { return c == 'a'; }); std::cout << "total : " << sum << std::endl; }
a a a a a a a a a a total : 10
template関数を使えば、数列で文字列でも、同じfilter関数で処理できます。
#include <iostream> #include <algorithm> #include <vector> #include <cassert> template<class Container, class Predicate> Container filter(const Container& c, Predicate f) { Container r; std::copy_if(begin(c), end(c), std::back_inserter(r), f); return r; } int main(void) { // 配列をfileter template関数に渡す auto target = filter(std::vector<int>{2, 3, 4}, [](int i) { return i < 4; }); for (auto var : target) { std::cout << var << std::endl; } // std::vector::back関数は末尾要素への参照を取得する関数 assert(3 == target.back()); // 文字列をfileter template関数に渡す auto targetString = filter(std::string("a::b_c()"), isalpha); // 変数の方を調べます(std::string型で返ってきます) std::cout << typeid(targetString).name() << std::endl; for (auto var : targetString) { std::cout << var << std::endl; } assert("abc" == targetString); }
2 3 NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE a b c
公式ドキュメントを読みましょう。
サンプルもあります。
https://github.com/keras-team/keras/tree/master/examples
適当な5次元のデータ配列を入力すると、その和が2.5より大きい場合に、1と教えてくれるニュートラルネットワークを作成します。
適当なデータセットを作ります。以下は1000行、5列の2次元のデータを作成する。
random.randの1つ目の引数に行数、2つ目の引数に列数を指定します。
ここで、1つの行について考えると、列は5つあるため、1000個の5次元のデータを作成したと考えることもできます。
data = np.randam.rand(1000, 5)
array([[0.3940429 , 0.79998594, 0.58371434, 0.4746385 , 0.57960131], [0.48199409, 0.29579691, 0.6393789 , 0.79172043, 0.35938036], [0.32953348, 0.44838049, 0.60369065, 0.96091877, 0.61703259], ..., [0.40231469, 0.69186734, 0.09979942, 0.57616445, 0.99702868], [0.41718706, 0.92310164, 0.66852297, 0.69960452, 0.15955488], [0.25724677, 0.94933856, 0.18957415, 0.22025431, 0.52682667]])
import numpy as np from keras.utils import np_utils # dataは5次元のデータ data = np.random.rand(1000, 5) # sum_dataは1次元のデータ sum_data = (np.sum(data, axis=1) > 2.5) * 1
one-hotなラベルを作成します。
one-hot(ワン・ホット)は1つだけHigh(1)であり、他はLow(0)であるようなデータのことです。
from keras.utils import np_utils sum_data = (np.sum(data, axis=1) > 2.5) * 1 labels = np_utils.to_categorical(sum_data)
array([[0., 1.], [1., 0.], [1., 0.], ..., [0., 1.], [1., 0.], [0., 1.]], dtype=float32)
Sequentialは層を積み上げるだけのモデル。
今回は中間層(入力層・隠れ層・出力層)とする
入力5個、出力2個のニュートラルネットワークを作成します。
from keras.models import Sequential from keras.layers import Dense, Activation model = Sequential()
Denseというレイヤー(通常の全結合ニューラルネットワークレイヤー)を使用する。
5個のノートから、それぞれ10本矢印が出てきて、次の層のノードが10個であることを示す。 ノード数は入力データの次元数と一致する必要がある。
model.add(Dense(10, input_dim=5))
出力に活性化関数を適用します。'relu'はランプ関数です。
活性化関数は、入力信号の総和がどのように活性化するかを決定する役割を持ちます。
これは、次の層に渡す値を整えるような役割をします。
model.add(Activation('relu'))
次は隠れ層―出力層。10個のノードから(活性化関数が適用されたあと)それぞれ値がやってくる。出力は2次元で、ノードにやってきた10個の値に対して重み付き和とソフトマックス関数を使用します。
ソフトマックス関数は、活性化関数の1つで、出力層で使われる関数です。
model.add(Dense(2, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
学習の前にコンパイルを行います。
1つめは最適化関数、2つめは損失関数、3つめは評価指標のリスト。
model.compile('rmsprop', 'categorical_crossentropy', metrics=['accuracy'])
nb_epoch は回数。validation_split は検証に使うデータの割合(全体のデータを学習に使わず、8割のみを学習に使って、残りの2割を評価に使う)。
model.fit(data, labels, nb_epoch=300, validation_split=0.2)
予測精度を判別します。
test = np.random.rand(1000, 5) ## 予測配列 predict = np.argmax(model.predict(test), axis=1) ## 正解配列 real = (np.sum(test, axis=1) > 2.5) * 1 sum(predict == real) / 1000.0
from keras.utils import np_utils from keras.models import Sequential from keras.layers import Dense, Activation import numpy as np data = np.random.rand(1000,5) data_sum = (np.sum(data, axis=1) > 2.5) * 1 labels = np_utils.to_categorical(data_sum) model = Sequential([Dense(10, input_dim=5), Activation('relu'), Dense(2, activation='softmax')]) model.compile('rmsprop', 'categorical_crossentropy', metrics=['accuracy']) model.fit(data, labels, nb_epoch=100, validation_split=0.2) test = np.random.rand(1000, 5) predict = np.argmax(model.predict(test), axis=1) real = (np.sum(test, axis=1) > 2.5) * 1 print(sum(predict == real) / 1000.0)
0.979
Pythonで画像データを取得してみたいと思います。 flickr の画像取得は、flickr API を使用します。
https://www.flickr.com/services/api/
flickrのAPIを使用して画像の情報を取得します。
コマンドプロンプト上から python [コードのファイル名] monkey と打つと画像情報を取得できます。
import flickrapi from urllib.request import urlretrieve from pprint import pprint import os, time, sys # APIキーの情報 key = "d21bac140e0245aed437c6c812f27d44" secret = "b5e79704c13a3d15" wait_time = 1 # コマンドライン引数の 1 番目の値を取得 animalname = sys.argv[1] # 画像を保存するディレクトリを指定 savedir = "./" + animalname flickr = flickrapi.FlickrAPI(key, secret, format='parsed-json') result = flickr.photos.search( # 検索キーワード text = animalname, # 取得するデータ件数 per_page = 2, # 検索するデータの種類(ここでは、写真) media = 'photos', # データの並び順(関連順) sort = 'relevance', # UI コンテンツを表示しない safe_search = 1, # 取得したいオプションの値(url_q->画像のアドレスが入っている情報、licence -> ライセンス情報) extras = 'url_q, licence' ) photos = result['photos'] # 戻り値を表示する # pprint(photos)
{'page': 1, 'pages': 498092, 'perpage': 2, 'photo': [{'farm': 4, 'height_q': '150', 'id': '15557905405', 'isfamily': 0, 'isfriend': 0, 'ispublic': 1, 'owner': '101295317@N06', 'secret': '05357b440d', 'server': '3928', 'title': 'monkey', 'url_q': 'https://live.staticflickr.com/3928/15557905405_05357b440d_q.jpg', 'width_q': '150'}, {'farm': 1, 'height_q': '150', 'id': '297887549', 'isfamily': 0, 'isfriend': 0, 'ispublic': 1, 'owner': '12345287@N00', 'secret': '2dc0ee273f', 'server': '110', 'title': 'monkey', 'url_q': 'https://live.staticflickr.com/110/297887549_2dc0ee273f_q.jpg', 'width_q': '150'}], 'total': '996183'}
次に、画像を実際にダウンロードします。
コマンドプロンプト上から python [コードのファイル名] monkey と打つと画像データがダウンロードされます。
import flickrapi from urllib.request import urlretrieve from pprint import pprint import os, time, sys # APIキーの情報 key = "d21bac140e0245aed437c6c812f27d44" secret = "b5e79704c13a3d15" wait_time = 1 # コマンドライン引数の 1 番目の値を取得 animalname = sys.argv[1] # 画像を保存するディレクトリを指定 savedir = "./" + animalname flickr = flickrapi.FlickrAPI(key, secret, format='parsed-json') result = flickr.photos.search( # 検索キーワード text = animalname, # 取得するデータ件数 per_page = 400, # 検索するデータの種類(ここでは、写真) media = 'photos', # データの並び順(関連順) sort = 'relevance', # UI コンテンツを表示しない safe_search = 1, # 取得したいオプションの値(url_q->画像のアドレスが入っている情報、licence -> ライセンス情報) extras = 'url_q, licence' ) photos = result['photos'] for i, photo in enumerate(photos['photo']): url_q = photo['url_q'] filepath = savedir + '/' + photo['id'] + '.jpg' # ファイルが重複していたらスキップする if os.path.exists(filepath): continue # データをダウンロードする urlretrieve(url_q, filepath) time.sleep(wait_time)