質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

19006閲覧

keras モジュールインポート時のエラー

---stax---

総合スコア148

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2018/06/19 14:47

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('----------')

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

いちばん簡単なのは自分で実装してしまうことでしょうか。(コピペしただけですけど)
importからlist_pictures外しておいてくださいね。

python

1# coding:utf-8 2import os 3import re 4import keras 5import numpy as np 6from keras.models import load_model 7from keras.preprocessing.image import array_to_img, img_to_array, load_img 8 9 10def list_pictures(directory, ext='jpg|jpeg|bmp|png|ppm'): 11 return [os.path.join(root, f) 12 for root, _, files in os.walk(directory) for f in files 13 if re.match(r'([\w]+.(?:' + ext + '))', f.lower())] 14 15model = load_model('mnist_model.h5') 16 17for picture in list_pictures(r'C:\Users\Desktop\pic'): 18 X = [] 19 img = img_to_array( 20 load_img(picture, target_size=(28, 28), grayscale=True)) 21 X.append(img) 22 23 X = np.asarray(X) 24 X = X.astype('float32') 25 X = X / 255.0 26 27 features = model.predict(X) 28 29 print('----------') 30 print(picture) 31 print(features.argmax()) 32 print('----------') 33

keras-preprocessing/keras_preprocessing/image.py

投稿2018/06/19 21:33

編集2018/06/20 09:39
wakame

総合スコア1170

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問