質問するログイン新規登録

質問編集履歴

1

記法を最新のKerasに合わせ、警告がなくなるように修正し、少し見やすくしました。

2017/09/05 05:38

投稿

mizu4423
mizu4423

スコア10

title CHANGED
File without changes
body CHANGED
@@ -5,47 +5,26 @@
5
5
  訓練したモデルは判別率9割ほどにも関わらず、どの画像を与えても、分類するカテゴリーの一番最初の物にしかならない。(for文が回ってない?)
6
6
 
7
7
  実行結果
8
- $ python3 getboar-checker.py sample.JPG STC_0232.JPG STC_0280.JPG STC_0141.JPG STC_1442.JPG STC_0253.JPG STC_0278.JPG STC_5392.JPG STC_0929.
8
+ $ python3 getboar-checker.py sample.JPG STC_0232.JPG STC_0280.JPG STC_0141.JPG STC_1442.JPG
9
- JPG
10
-
11
9
  Using TensorFlow backend.
12
- /Users/nakagawa/Documents/pyprogram/getboar_keras2.py:27: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(32, (3, 3), input_shape=(50, 50, 3...
13
- , padding="same")`
14
-   model.add(Conv2D(32, 3, 3, border_mode='same', input_shape=in_shape))
15
- /Users/nakagawa/Documents/pyprogram/getboar_keras2.py:31: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(64, (3, 3), padding="same")`
16
-   model.add(Conv2D(64, 3, 3, border_mode='same'))
17
- /Users/nakagawa/Documents/pyprogram/getboar_keras2.py:33: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(64, (3, 3))`
18
-   model.add(Conv2D(64, 3, 3))
19
10
  + 入力: sample.JPG
20
- | 動物: boar2
11
+ | 動物: nothing
21
12
  | 状態: 0
22
13
  + 入力: STC_0232.JPG
23
- | 動物: boar2
14
+ | 動物: nothing
24
-
25
15
  | 状態: 0
26
16
  + 入力: STC_0280.JPG
27
- | 動物: boar2
17
+ | 動物: nothing
28
18
  | 状態: 0
29
19
  + 入力: STC_0141.JPG
30
- | 動物: boar2
20
+ | 動物: nothing
31
21
  | 状態: 0
32
22
  + 入力: STC_1442.JPG
33
- | 動物: boar2
23
+ | 動物: nothing
34
24
  | 状態: 0
35
- + 入力: STC_0253.JPG
36
- | 動物: boar2
37
- | 状態: 0
38
- + 入力: STC_0278.JPG
39
- | 動物: boar2
40
- | 状態: 0
41
- + 入力: STC_5392.JPG
42
- | 動物: boar2
43
- | 状態: 0
44
- + 入力: STC_0929.JPG
45
- | 動物: boar2
46
- | 状態: 0
47
25
 
48
26
  ###該当のソースコード
27
+
49
28
  ```Python 3.x
50
29
  import getboar_keras2 as getboar
51
30
  import sys, os
@@ -60,7 +39,7 @@
60
39
 
61
40
  #入力画像をNumpy配列にする
62
41
  image_size = 50
63
- categories = ["boar2", "others", "normal"]
42
+ categories = ["nothing", "others", "boar2"]
64
43
  state = [0, 1, 2]
65
44
 
66
45
  X = []
@@ -76,7 +55,7 @@
76
55
 
77
56
  #CNNのモデルを構築
78
57
  model = getboar.build_model(X.shape[1:])
79
- model.load_weights("./image/getboar2-model.hdf5")
58
+ model.load_weights("./image/hogetboar-model.hdf5")
80
59
 
81
60
  #データを予測
82
61
  html = ""
@@ -110,24 +89,30 @@
110
89
  ###試したこと
111
90
  参考書のサンプルコードを自分の使っているデータセットを使った場合でやってみたが、結果は変わらなかったため、サンプルコードが間違っている可能性も考えられる。
112
91
  画像データを減らしてやってみたが、効果なし。
92
+ binary_crossentropyをcategorical_crossentropyにしたが、効果なし。
113
93
 
114
94
  ###補足情報(言語/FW/ツール等のバージョンなど)
115
- Python3.6.1を使用。
95
+ Python3.6.1
96
+ tensorflow (1.3.0)
97
+ tensorflow-tensorboard (0.1.5)
98
+ Keras (2.0.8)
99
+ numpy (1.13.1)
116
100
 
117
101
  参考書のサンプルコードなど(ch07に入ってます)
118
102
  http://www.socym.co.jp/download/1079/src.zip
119
103
 
120
104
  また、以下の2つのプログラムによって、Numpyのデータ生成と、訓練を行った。
121
105
 
122
- (1)データをNumpy配列に変換し、チューニングするプログラム
106
+ (1)データをNumpy配列に変換し、チューニングするプログラム(getboar-makedata2.py)
123
107
 
108
+ ```Python 3.x
124
109
  from PIL import Image
125
110
  import os, glob
126
111
  import numpy as np
127
112
  import random, math
128
113
 
129
114
  root_dir = "./image/"
130
- categories = ["normal", "boar2", "others"]
115
+ categories = ["nothing", "boar2", "others"]
131
116
  nb_classes = len(categories)
132
117
  image_size = 50
133
118
 
@@ -173,10 +158,11 @@
173
158
  X_train, y_train = make_sample(train, True)
174
159
  X_test, y_test = make_sample(test, False)
175
160
  xy = (X_train, X_test, y_train, y_test)
176
- np.save("./image/getboar2.npy", xy)
161
+ np.save("./image/hogetboar.npy", xy)
177
162
  print("ok,", len(y_train))
178
-
163
+ ```
179
- (2)CNNで学習させるプログラム
164
+ Python (2)CNNで学習させるプログラム(getboar-keras2.py)
165
+ ```Python 3.x
180
166
  from keras.models import Sequential
181
167
  from keras.layers import Conv2D, MaxPooling2D
182
168
  from keras.layers import Activation, Dropout, Flatten, Dense
@@ -184,28 +170,32 @@
184
170
  import numpy as np
185
171
 
186
172
  root_dir = "./image/"
187
- categories = ["boar2", "others", "normal"]
173
+ categories = ["nothing", "boar2", "others"]
188
174
  nb_classes = len(categories)
189
175
  image_size = 50
190
176
 
177
+ # データ読み込み
191
178
  def main():
192
- X_train, X_test, y_train, y_test = np.load("./image/getboar2.npy")
179
+ X_train, X_test, y_train, y_test = np.load("./image/hogetboar.npy")
180
+ #データを正規化
193
181
  X_train = X_train.astype("float") / 256
194
182
  X_test = X_test.astype("float") / 256
195
183
  y_train = np_utils.to_categorical(y_train, nb_classes)
196
184
  y_test = np_utils.to_categorical(y_test, nb_classes)
185
+ #モデルを訓練 & 評価
197
186
  model = model_train(X_train, y_train)
198
187
  model_eval(model, X_test, y_test)
199
188
 
189
+ #モデル構築
200
190
  def build_model(in_shape):
201
191
  model = Sequential()
202
- model.add(Conv2D(32, 3, 3, border_mode='same', input_shape=in_shape))
192
+ model.add(Conv2D(32, (3, 3), padding='same', input_shape=in_shape))
203
193
  model.add(Activation('relu'))
204
194
  model.add(MaxPooling2D(pool_size=(2, 2)))
205
195
  model.add(Dropout(0.25))
206
- model.add(Conv2D(64, 3, 3, border_mode='same'))
196
+ model.add(Conv2D(64, (3, 3), padding='same'))
207
197
  model.add(Activation('relu'))
208
- model.add(Conv2D(64, 3, 3))
198
+ model.add(Conv2D(64, (3, 3)))
209
199
  model.add(MaxPooling2D(pool_size=(2, 2)))
210
200
  model.add(Dropout(0.25))
211
201
  model.add(Flatten())
@@ -214,21 +204,24 @@
214
204
  model.add(Dropout(0.5))
215
205
  model.add(Dense(nb_classes))
216
206
  model.add(Activation('softmax'))
217
- model.compile(loss = 'binary_crossentropy', optimizer = 'rmsprop', metrics=['accuracy'])
207
+ model.compile(loss = 'categorical_crossentropy', optimizer = 'rmsprop', metrics=['accuracy'])
218
208
  return model
219
209
 
210
+ # 訓練
220
211
  def model_train(X, y):
221
212
  model = build_model(X.shape[1:])
222
- model.fit(X, y, batch_size=32, nb_epoch=30)
213
+ model.fit(X, y, batch_size=32, epochs=30)
223
-
214
+ #モデル保存
224
- hdf5_file = "./image/getboar2-model.hdf5"
215
+ hdf5_file = "./image/hogetboar-model.hdf5"
225
216
  model.save_weights(hdf5_file)
226
217
  return model
227
218
 
219
+ #モデル評価
228
- def model_eval(model, X, y):
220
+ def model_eval(model, X, y):
229
221
  score = model.evaluate(X, y)
230
222
  print('loss=', score[0])
231
223
  print('accuracy=', score[1])
232
224
 
233
225
  if __name__ == "__main__":
234
- main()
226
+ main()
227
+ ```