質問編集履歴
2
コードが抜けていたので修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -20,7 +20,7 @@
|
|
20
20
|
|
21
21
|
# わからないこと
|
22
22
|
|
23
|
-
jupyter notebook上でkerasで配布されているサンプルコード[mnist_cnn.py](https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py)を実行し
|
23
|
+
Qiitaの[記事](https://qiita.com/nagayosi/items/0034e5e82813b05e41df)を読みつつ、jupyter notebook上でkerasで配布されているサンプルコードを[mnist_cnn.py](https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py)を実行してみました。サンプルコード自体はうまく作成できましたが```model.predict```でエラーが出て進みません。
|
24
24
|
|
25
25
|
|
26
26
|
|
@@ -200,14 +200,286 @@
|
|
200
200
|
|
201
201
|
- モデルをロードして、用意した画像の分析結果を得る
|
202
202
|
|
203
|
-
画像はkaggleのDatasetより拝借。[https://www.kaggle.com/scolianni/mnistasjpg](https://www.kaggle.com/scolianni/mnistasjpg)のtestSample.zipからimg_1.jpgを
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
203
|
+
画像はkaggleのDatasetより拝借。[https://www.kaggle.com/scolianni/mnistasjpg](https://www.kaggle.com/scolianni/mnistasjpg)のtestSample.zipからimg_1.jpgを利用して、predictを実行してみましたが、エラーが発生
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
```
|
208
|
+
|
209
|
+
import keras
|
210
|
+
|
211
|
+
from keras.datasets import mnist
|
212
|
+
|
213
|
+
from keras.models import model_from_json
|
214
|
+
|
215
|
+
from keras.utils import np_utils
|
216
|
+
|
217
|
+
from keras.preprocessing import image
|
218
|
+
|
219
|
+
from PIL import Image
|
220
|
+
|
221
|
+
import matplotlib.pyplot as plt
|
222
|
+
|
223
|
+
import numpy
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
# modelのload
|
228
|
+
|
229
|
+
model = model_from_json(open('tutorial_mnist.json').read())
|
230
|
+
|
231
|
+
model.load_weights('tutorial_mnist.h5')
|
232
|
+
|
233
|
+
model.compile(loss=keras.losses.categorical_crossentropy,
|
234
|
+
|
235
|
+
optimizer=keras.optimizers.Adadelta(),
|
236
|
+
|
237
|
+
metrics=['accuracy'])
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
# predict
|
242
|
+
|
243
|
+
filepath = "/Users/atg/work/python/tensorflow/jupyter/mnist_test_data/img_1.jpg"
|
244
|
+
|
245
|
+
img = Image.open(filepath).convert('RGB') ## Gray->L, RGB->RGB
|
246
|
+
|
247
|
+
img = img.resize((28, 28))
|
248
|
+
|
249
|
+
x = numpy.array(img, dtype=numpy.float32)
|
250
|
+
|
251
|
+
x = x / 255.
|
252
|
+
|
253
|
+
x = x[None, ...]
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
pred = model.predict(x, batch_size=1, verbose=0)
|
258
|
+
|
259
|
+
score = numpy.max(pred)
|
260
|
+
|
261
|
+
pred_label = np.argmax(pred)
|
262
|
+
|
263
|
+
print("pred", pred)
|
264
|
+
|
265
|
+
print("score", score)
|
266
|
+
|
267
|
+
print("pred_label", pred_label)
|
268
|
+
|
269
|
+
```
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
* 発生したエラー
|
274
|
+
|
275
|
+
```
|
276
|
+
|
277
|
+
---------------------------------------------------------------------------
|
278
|
+
|
279
|
+
ValueError Traceback (most recent call last)
|
280
|
+
|
281
|
+
<ipython-input-24-b0c45db13636> in <module>()
|
282
|
+
|
283
|
+
23 x = x[None, ...]
|
284
|
+
|
285
|
+
24
|
286
|
+
|
287
|
+
---> 25 pred = model.predict(x, batch_size=1, verbose=0)
|
288
|
+
|
289
|
+
26 score = numpy.max(pred)
|
290
|
+
|
291
|
+
27 pred_label = np.argmax(pred)
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
~/work/python/tensorflow/lib/python3.6/site-packages/keras/engine/training.py in predict(self, x, batch_size, verbose, steps)
|
296
|
+
|
297
|
+
1150 'argument.')
|
298
|
+
|
299
|
+
1151 # Validate user data.
|
300
|
+
|
301
|
+
-> 1152 x, _, _ = self._standardize_user_data(x)
|
302
|
+
|
303
|
+
1153 if self.stateful:
|
304
|
+
|
305
|
+
1154 if x[0].shape[0] > batch_size and x[0].shape[0] % batch_size != 0:
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
~/work/python/tensorflow/lib/python3.6/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
|
310
|
+
|
311
|
+
752 feed_input_shapes,
|
312
|
+
|
313
|
+
753 check_batch_axis=False, # Don't enforce the batch size.
|
314
|
+
|
315
|
+
--> 754 exception_prefix='input')
|
316
|
+
|
317
|
+
755
|
318
|
+
|
319
|
+
756 if y is not None:
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
~/work/python/tensorflow/lib/python3.6/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
|
324
|
+
|
325
|
+
134 ': expected ' + names[i] + ' to have shape ' +
|
326
|
+
|
327
|
+
135 str(shape) + ' but got array with shape ' +
|
328
|
+
|
329
|
+
--> 136 str(data_shape))
|
330
|
+
|
331
|
+
137 return data
|
332
|
+
|
333
|
+
138
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
ValueError: Error when checking input: expected conv2d_1_input to have shape (28, 28, 1) but got array with shape (28, 28, 3)
|
338
|
+
|
339
|
+
```
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
期待している次元が違うというエラーということで、reshapeで次元数を変更してみました。
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
- 変更したコード
|
350
|
+
|
351
|
+
```
|
352
|
+
|
353
|
+
import keras
|
354
|
+
|
355
|
+
from keras.datasets import mnist
|
356
|
+
|
357
|
+
from keras.models import model_from_json
|
358
|
+
|
359
|
+
from keras.utils import np_utils
|
360
|
+
|
361
|
+
from keras.preprocessing import image
|
362
|
+
|
363
|
+
from PIL import Image
|
364
|
+
|
365
|
+
import matplotlib.pyplot as plt
|
366
|
+
|
367
|
+
import numpy
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
# modelのload
|
372
|
+
|
373
|
+
model = model_from_json(open('tutorial_mnist.json').read())
|
374
|
+
|
375
|
+
model.load_weights('tutorial_mnist.h5')
|
376
|
+
|
377
|
+
model.compile(loss=keras.losses.categorical_crossentropy,
|
378
|
+
|
379
|
+
optimizer=keras.optimizers.Adadelta(),
|
380
|
+
|
381
|
+
metrics=['accuracy'])
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
# predict
|
386
|
+
|
387
|
+
filepath = "/Users/atg/work/python/tensorflow/jupyter/mnist_test_data/img_1.jpg"
|
388
|
+
|
389
|
+
img = Image.open(filepath).convert('RGB') ## Gray->L, RGB->RGB
|
390
|
+
|
391
|
+
img = img.resize((28, 28))
|
392
|
+
|
393
|
+
x = numpy.array(img, dtype=numpy.float32)
|
394
|
+
|
395
|
+
x = x / 255.
|
396
|
+
|
397
|
+
x = x[None, ...]
|
398
|
+
|
399
|
+
x = numpy.reshape(x, [28,28,1])
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
pred = model.predict(x, batch_size=1, verbose=0)
|
404
|
+
|
405
|
+
score = numpy.max(pred)
|
406
|
+
|
407
|
+
pred_label = np.argmax(pred)
|
408
|
+
|
409
|
+
print("pred", pred)
|
410
|
+
|
411
|
+
print("score", score)
|
412
|
+
|
413
|
+
print("pred_label", pred_label)
|
414
|
+
|
415
|
+
```
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
- エラー
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
```
|
424
|
+
|
425
|
+
---------------------------------------------------------------------------
|
426
|
+
|
427
|
+
ValueError Traceback (most recent call last)
|
428
|
+
|
429
|
+
<ipython-input-29-200996341dd9> in <module>()
|
430
|
+
|
431
|
+
22 x = x / 255.
|
432
|
+
|
433
|
+
23 x = x[None, ...]
|
434
|
+
|
435
|
+
---> 24 x = numpy.reshape(x, [28,28,1])
|
436
|
+
|
437
|
+
25
|
438
|
+
|
439
|
+
26 pred = model.predict(x, batch_size=1, verbose=0)
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
~/work/python/tensorflow/lib/python3.6/site-packages/numpy/core/fromnumeric.py in reshape(a, newshape, order)
|
444
|
+
|
445
|
+
255 [5, 6]])
|
446
|
+
|
447
|
+
256 """
|
448
|
+
|
449
|
+
--> 257 return _wrapfunc(a, 'reshape', newshape, order=order)
|
450
|
+
|
451
|
+
258
|
452
|
+
|
453
|
+
259
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
~/work/python/tensorflow/lib/python3.6/site-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
|
458
|
+
|
459
|
+
50 def _wrapfunc(obj, method, *args, **kwds):
|
460
|
+
|
461
|
+
51 try:
|
462
|
+
|
463
|
+
---> 52 return getattr(obj, method)(*args, **kwds)
|
464
|
+
|
465
|
+
53
|
466
|
+
|
467
|
+
54 # An AttributeError occurs if the object does not have
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
ValueError: cannot reshape array of size 2352 into shape (28,28,1)
|
472
|
+
|
473
|
+
|
474
|
+
|
475
|
+
```
|
476
|
+
|
477
|
+
|
478
|
+
|
479
|
+
今度はサイズでエラーが発生しましたが、そもそもpredictを行う前の画像の読み込みからモデルに適した形に変換する処理の考え方が間違っているのではと思い、teratailに質問を記載しました。
|
480
|
+
|
481
|
+
|
482
|
+
|
483
|
+
モデル作成まではQiitaや海外のフォーラムでも見かけるのですが、実際に作成したモデルで予測させる際は見るサイトで方法が変わり、かつサンプルコードを写経してもうまくいかず、だんだん何が正しい方法なのかがわからなくなってきました。
|
484
|
+
|
485
|
+
参考になるサイトや方法がありましたら、ご教授頂きたいです。
|
1
日本語がおかしいところを修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -20,7 +20,7 @@
|
|
20
20
|
|
21
21
|
# わからないこと
|
22
22
|
|
23
|
-
jupyter notebook上でkerasで配布されているサンプルコード
|
23
|
+
jupyter notebook上でkerasで配布されているサンプルコード[mnist_cnn.py](https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py)を実行し、作成したモデルを使って```model.predict```をしてみたところエラーが出てうまく進みませんでした。
|
24
24
|
|
25
25
|
|
26
26
|
|