質問編集履歴

5

題名変更

2018/11/15 11:15

投稿

FALLOT
FALLOT

スコア16

test CHANGED
@@ -1 +1 @@
1
- kerasでCNNをはじめてやる画像認識(入力層)
1
+ kerasでCNNをはじめてやる画像認識(入力層の4次元テンソルなのに3次元のまま どうすれば追加可能か
test CHANGED
File without changes

4

質問内容の変更

2018/11/15 11:15

投稿

FALLOT
FALLOT

スコア16

test CHANGED
File without changes
test CHANGED
@@ -164,192 +164,192 @@
164
164
 
165
165
  print(filepath)
166
166
 
167
+ #image =np.reshape(image, A)
168
+
169
+ print(image.shape)
170
+
171
+ print('\n')
172
+
173
+ image_list.append(image / 255.)
174
+
175
+
176
+
177
+
178
+
179
+ # kerasに渡すためにnumpy配列に変換。
180
+
181
+ image_list = np.array(image_list)
182
+
183
+
184
+
185
+
186
+
187
+
188
+
189
+
190
+
191
+ # ラベルの配列を1と0からなるラベル配列に変更
192
+
193
+ # 0 -> [1,0], 1 -> [0,1] という感じ。
194
+
195
+ Y = to_categorical(label_list) #わからない
196
+
197
+
198
+
199
+ print("入力データの確認")
200
+
201
+ print(image_list.shape)
202
+
203
+ #np.savetxt("check/input_data_pixel.csv",image_list,delimiter=",")
204
+
205
+ print("ラベルデータの確認")
206
+
207
+ print(Y.shape)
208
+
209
+ #np.savetxt("check/label_data.csv",Y,delimiter=",")
210
+
211
+
212
+
213
+
214
+
215
+ # モデルを生成してニューラルネットを構築
216
+
217
+ model = Sequential()
218
+
219
+
220
+
221
+ model.add(Conv2D(32,3,input_shape=(x,y,1),kernel_initializer='random_uniform',bias_initializer='zeros'))
222
+
223
+ model.add(Activation('relu'))
224
+
225
+ model.add(Conv2D(32,3,kernel_initializer='random_uniform',bias_initializer='zeros'))
226
+
227
+ model.add(Activation('relu'))
228
+
229
+
230
+
231
+ model.add(Conv2D(64,3,kernel_initializer='random_uniform',bias_initializer='zeros'))
232
+
233
+ model.add(Activation('relu'))
234
+
235
+
236
+
237
+ model.add(Flatten())
238
+
239
+ model.add(Dense(810))
240
+
241
+ model.add(Activation('relu'))
242
+
243
+ model.add(Dropout(1.0))
244
+
245
+
246
+
247
+
248
+
249
+ model.add(Dense(output))
250
+
251
+ model.add(Activation("softmax"))
252
+
253
+
254
+
255
+ # オプティマイザ(最適化)にAdamを使用
256
+
257
+ opt = Adam(lr=LR)
258
+
259
+ # モデルをコンパイル
260
+
261
+ model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) #わからない
262
+
263
+ #CSVに各エポックの学習結果の保存
264
+
265
+ csv_logger = CSVLogger('result/training.csv')
266
+
267
+ # 学習を実行。20%はテストに使用。
268
+
269
+ history = model.fit(image_list, Y, nb_epoch=B,verbose=1,callbacks=[csv_logger], batch_size=BATCH_SIZE, validation_split=0.2)
270
+
271
+
272
+
273
+
274
+
275
+
276
+
277
+ # テスト用ディレクトリ(./data/train/)の画像でチェック。正解率を表示する。
278
+
279
+ total = 0.
280
+
281
+ ok_count = 0.
282
+
283
+ #最終の学習結果の表示
284
+
285
+ loss, accuracy = model.evaluate(image_list, Y)
286
+
287
+ print("\nloss:{} accuracy:{}".format(loss, accuracy))
288
+
289
+ #最終の学習結果を書き込む
290
+
291
+ fp = open("result/RESULT.txt","w")
292
+
293
+ fp.write("\nloss:{} accuracy:{}".format(loss, accuracy))
294
+
295
+ fp.close()
296
+
297
+ #正解率の書き込み
298
+
299
+ f = open("result/ANSWER.txt","w")
300
+
301
+
302
+
303
+ for dir in os.listdir("data/train"):
304
+
305
+ if dir == ".DS_Store":
306
+
307
+ continue
308
+
309
+
310
+
311
+ dir1 = "data/test/" + dir
312
+
313
+ label = 0
314
+
315
+
316
+
317
+ if dir == "a": # 左下に最大応力:ラベル0
318
+
319
+ label = 0
320
+
321
+ elif dir == "b": # 右下に最大応力:ラベル1
322
+
323
+ label = 1
324
+
325
+ elif dir == "c": # 右下に最大応力:ラベル1
326
+
327
+ label = 2
328
+
329
+ elif dir == "d": # 右下に最大応力:ラベル1
330
+
331
+ label = 3
332
+
333
+
334
+
335
+
336
+
337
+
338
+
339
+ for file in os.listdir(dir1):
340
+
341
+ if file != ".DS_Store":
342
+
343
+ label_list.append(label)
344
+
345
+ filepath = dir1 + "/" + file
346
+
347
+ image = np.array(Image.open(filepath).resize((x, y)))
348
+
349
+ print(filepath)
350
+
167
351
  image =np.reshape(image, A)
168
352
 
169
- print(image.shape)
170
-
171
- print('\n')
172
-
173
- image_list.append(image / 255.)
174
-
175
-
176
-
177
-
178
-
179
- # kerasに渡すためにnumpy配列に変換。
180
-
181
- image_list = np.array(image_list)
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
- # ラベルの配列を1と0からなるラベル配列に変更
192
-
193
- # 0 -> [1,0], 1 -> [0,1] という感じ。
194
-
195
- Y = to_categorical(label_list) #わからない
196
-
197
-
198
-
199
- print("入力データの確認")
200
-
201
- print(image_list.shape)
202
-
203
- #np.savetxt("check/input_data_pixel.csv",image_list,delimiter=",")
204
-
205
- print("ラベルデータの確認")
206
-
207
- print(Y.shape)
208
-
209
- #np.savetxt("check/label_data.csv",Y,delimiter=",")
210
-
211
-
212
-
213
-
214
-
215
- # モデルを生成してニューラルネットを構築
216
-
217
- model = Sequential()
218
-
219
-
220
-
221
- model.add(Conv2D(32,3,input_shape=(x,y,1),kernel_initializer='random_uniform',bias_initializer='zeros'))
222
-
223
- model.add(Activation('relu'))
224
-
225
- model.add(Conv2D(32,3,kernel_initializer='random_uniform',bias_initializer='zeros'))
226
-
227
- model.add(Activation('relu'))
228
-
229
-
230
-
231
- model.add(Conv2D(64,3,kernel_initializer='random_uniform',bias_initializer='zeros'))
232
-
233
- model.add(Activation('relu'))
234
-
235
-
236
-
237
- model.add(Flatten())
238
-
239
- model.add(Dense(810))
240
-
241
- model.add(Activation('relu'))
242
-
243
- model.add(Dropout(1.0))
244
-
245
-
246
-
247
-
248
-
249
- model.add(Dense(output))
250
-
251
- model.add(Activation("softmax"))
252
-
253
-
254
-
255
- # オプティマイザ(最適化)にAdamを使用
256
-
257
- opt = Adam(lr=LR)
258
-
259
- # モデルをコンパイル
260
-
261
- model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) #わからない
262
-
263
- #CSVに各エポックの学習結果の保存
264
-
265
- csv_logger = CSVLogger('result/training.csv')
266
-
267
- # 学習を実行。20%はテストに使用。
268
-
269
- history = model.fit(image_list, Y, nb_epoch=B,verbose=1,callbacks=[csv_logger], batch_size=BATCH_SIZE, validation_split=0.2)
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
- # テスト用ディレクトリ(./data/train/)の画像でチェック。正解率を表示する。
278
-
279
- total = 0.
280
-
281
- ok_count = 0.
282
-
283
- #最終の学習結果の表示
284
-
285
- loss, accuracy = model.evaluate(image_list, Y)
286
-
287
- print("\nloss:{} accuracy:{}".format(loss, accuracy))
288
-
289
- #最終の学習結果を書き込む
290
-
291
- fp = open("result/RESULT.txt","w")
292
-
293
- fp.write("\nloss:{} accuracy:{}".format(loss, accuracy))
294
-
295
- fp.close()
296
-
297
- #正解率の書き込み
298
-
299
- f = open("result/ANSWER.txt","w")
300
-
301
-
302
-
303
- for dir in os.listdir("data/train"):
304
-
305
- if dir == ".DS_Store":
306
-
307
- continue
308
-
309
-
310
-
311
- dir1 = "data/test/" + dir
312
-
313
- label = 0
314
-
315
-
316
-
317
- if dir == "a": # 左下に最大応力:ラベル0
318
-
319
- label = 0
320
-
321
- elif dir == "b": # 右下に最大応力:ラベル1
322
-
323
- label = 1
324
-
325
- elif dir == "c": # 右下に最大応力:ラベル1
326
-
327
- label = 2
328
-
329
- elif dir == "d": # 右下に最大応力:ラベル1
330
-
331
- label = 3
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
- for file in os.listdir(dir1):
340
-
341
- if file != ".DS_Store":
342
-
343
- label_list.append(label)
344
-
345
- filepath = dir1 + "/" + file
346
-
347
- image = np.array(Image.open(filepath).resize((x, y)))
348
-
349
- print(filepath)
350
-
351
- image =np.reshape(image, A)
352
-
353
353
  result = model.predict_classes(np.array([image / 255.]))
354
354
 
355
355
  print("label:", label, "result:", result[0])

3

質問内容の変更

2018/11/15 10:47

投稿

FALLOT
FALLOT

スコア16

test CHANGED
File without changes
test CHANGED
@@ -20,6 +20,12 @@
20
20
 
21
21
  data_format: 文字列で,"channels_last"(デフォルト)"(batch, height, width, channels)"
22
22
 
23
+ TensorFlowでは(サンプル数, 画像の行数, 画像の列数, チャネル数)
24
+
25
+ バックエンドでTFを使っています.
26
+
27
+
28
+
23
29
  という形なので,ミニバッチを付け足せばいいと思うのですがどうすればよいですか?
24
30
 
25
31
 

2

質問内容の変更

2018/11/15 10:47

投稿

FALLOT
FALLOT

スコア16

test CHANGED
@@ -1 +1 @@
1
- kerasでCNNをはじめてやる画像認識
1
+ kerasでCNNをはじめてやる画像認識(入力層)
test CHANGED
@@ -12,11 +12,15 @@
12
12
 
13
13
 
14
14
 
15
-
16
-
17
- ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (4000, 8100)
15
+ ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (4000, 90, 90)
16
+
17
+
18
+
18
-
19
+ おそらく,
20
+
19
-
21
+ data_format: 文字列で,"channels_last"(デフォルト)"(batch, height, width, channels)"
22
+
23
+ という形なので,ミニバッチを付け足せばいいと思うのですがどうすればよいですか?
20
24
 
21
25
 
22
26
 

1

画像データの説明

2018/11/15 08:39

投稿

FALLOT
FALLOT

スコア16

test CHANGED
File without changes
test CHANGED
@@ -2,12 +2,18 @@
2
2
 
3
3
 
4
4
 
5
+ 画像データはグレースケールにして二値化しています.
6
+
7
+
8
+
5
9
  白丸が横方向に移動して内部の最大応力が発生した箇所を白丸ごとにラベリングしました.
6
10
 
7
11
  MLPでは経験者なのですが,CNNは初心者なので何がなんだかわかんない状況です.以下のエラーが出ておりどこを修正すればいいのかご教授願います.
8
12
 
9
13
 
10
14
 
15
+
16
+
11
17
  ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (4000, 8100)
12
18
 
13
19