MNISTを用いた認識をwebの資料などを見ながら試してみました
そこで自分が書いた文字も認識できるのかをやってみようと思い、以下のサイトを見ながら
動作するかどうか試してみました
MNIST vs 俺 (俺の手書き文字を正しく認識できるか)
下部に記載している②のコードを実行しようとすると以下のようにエラーとなります
ImportError Traceback (most recent call last) <ipython-input-82-c08a6616303b> in <module>() 2 import numpy as np 3 from keras.models import load_model ----> 4 from keras.preprocessing.image import array_to_img, img_to_array,list_pictures, load_img 5 6 ImportError: cannot import name 'list_pictures'
from keras.preprocessing.image import array_to_img, img_to_array,list_pictures, load_img
↑からlist_picturesだけを取り除くとエラーは発生しないのですが
kerasのリファレンス等を見てもlist_picturesというものは発見できず行き詰っています
kerasのバージョンは2.2.0です
更新などで使えなくなったのでしょうか?
初歩的な質問ですがアドバイス宜しくお願い致します
①kerasのサンプルコード
python
1'''Trains a simple convnet on the MNIST dataset. 2Gets to 99.25% test accuracy after 12 epochs 3(there is still a lot of margin for parameter tuning). 416 seconds per epoch on a GRID K520 GPU. 5''' 6 7from __future__ import print_function 8import keras 9from keras.datasets import mnist 10from keras.models import Sequential 11from keras.layers import Dense, Dropout, Flatten 12from keras.layers import Conv2D, MaxPooling2D 13from keras import backend as K 14 15batch_size = 128 16num_classes = 10 17epochs = 10 18 19# input image dimensions 20img_rows, img_cols = 28, 28 21 22# the data, split between train and test sets 23(x_train, y_train), (x_test, y_test) = mnist.load_data() 24 25if K.image_data_format() == 'channels_first': 26 x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 27 x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 28 input_shape = (1, img_rows, img_cols) 29else: 30 x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 31 x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 32 input_shape = (img_rows, img_cols, 1) 33 34x_train = x_train.astype('float32') 35x_test = x_test.astype('float32') 36x_train /= 255 37x_test /= 255 38print('x_train shape:', x_train.shape) 39print(x_train.shape[0], 'train samples') 40print(x_test.shape[0], 'test samples') 41 42# convert class vectors to binary class matrices 43y_train = keras.utils.to_categorical(y_train, num_classes) 44y_test = keras.utils.to_categorical(y_test, num_classes) 45 46model = Sequential() 47model.add(Conv2D(32, kernel_size=(3, 3), 48 activation='relu', 49 input_shape=input_shape)) 50model.add(Conv2D(64, (3, 3), activation='relu')) 51model.add(MaxPooling2D(pool_size=(2, 2))) 52model.add(Dropout(0.25)) 53model.add(Flatten()) 54model.add(Dense(128, activation='relu')) 55model.add(Dropout(0.5)) 56model.add(Dense(num_classes, activation='softmax')) 57 58model.compile(loss=keras.losses.categorical_crossentropy, 59 optimizer=keras.optimizers.Adadelta(), 60 metrics=['accuracy']) 61 62model.fit(x_train, y_train, 63 batch_size=batch_size, 64 epochs=epochs, 65 verbose=1, 66 validation_data=(x_test, y_test)) 67score = model.evaluate(x_test, y_test, verbose=0) 68print('Test loss:', score[0]) 69print('Test accuracy:', score[1]) 70 71model.save('mnist_model.h5')
②自分で書いた文字を評価するコード
python
1# coding:utf-8 2import keras 3import numpy as np 4from keras.models import load_model 5from keras.preprocessing.image import array_to_img, img_to_array,list_pictures, load_img 6 7 8model = load_model('mnist_model.h5') 9 10for picture in list_pictures(r'C:\Users\Desktop\pic'): 11 X = [] 12 img = img_to_array( 13 load_img(picture, target_size=(28, 28), grayscale=True)) 14 X.append(img) 15 16 X = np.asarray(X) 17 X = X.astype('float32') 18 X = X / 255.0 19 20 features = model.predict(X) 21 22 print('----------') 23 print(picture) 24 print(features.argmax()) 25 print('----------')
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。