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

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

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

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

Python 3.x

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

Q&A

1回答

4649閲覧

keras 画像認識時のエラー

atsuo

総合スコア10

Keras

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

Python 3.x

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

0グッド

0クリップ

投稿2018/10/16 02:16

##https://toxweblog.toxbe.com/2017/06/03/keras-face-learning/
現在こちらのブログを参考にさせていただき、kerasで乃木坂46の顔認証を行うシステムを作成しています。

前述させていただいたブログで公開されている、ソースコードとデーターセットほぼそのまま引用させていただいて、kerasで顔認証を試しているのですが、顔を切り出して予測する段階でうまく行かずに困っています。

発生している問題・エラーメッセージ

Using TensorFlow backend. line 0_the_others,0 line akimoto,1 line hashimoto,2 line ikoma,3 line ikuta,4 line nishino,5 line shiraishi,6 2018-10-16 10:38:44.454517: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA Traceback (most recent call last): File "prediction.py", line 58, in <module> main() File "prediction.py", line 49, in main preds = model.predict(imgarray, batch_size=imgarray.shape[0]) File "/home/okamoto/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages/keras/engine/training.py", line 1152, in predict x, _, _ = self._standardize_user_data(x) File "/home/okamoto/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages/keras/engine/training.py", line 754, in _standardize_user_data exception_prefix='input') File "/home/okamoto/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages/keras/engine/training_utils.py", line 126, in standardize_input_data 'with shape ' + str(data_shape)) ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (0, 1)

prediction.py

python

1from keras.models import Sequential, load_model 2from keras.preprocessing.image import load_img, img_to_array 3import numpy as np 4 5import argparse 6import cv2 7from PIL import Image 8 9def faceDetectionFromPath(path, size): 10 cvImg = cv2.imread(path) 11 cascade_path = './haarcascade_frontalface_alt.xml' 12 cascade = cv2.CascadeClassifier(cascade_path) 13 facerect = cascade.detectMultiScale(cvImg, scaleFactor=1.1, minNeighbors=1, minSize=(1, 1)) 14 faceData = [] 15 for rect in facerect: 16 faceImg = cvImg[rect[1]:rect[1]+rect[3],rect[0]:rect[0]+rect[2]] 17 resized = cv2.resize(faceImg,None, fx=float(size/faceImg.shape[0]),fy=float(size/faceImg.shape[1])) 18 CV_im_RGB = resized[:, :, ::-1].copy() 19 pilImg=Image.fromarray(CV_im_RGB) 20 faceData.append(pilImg) 21 22 return faceData 23 24def main(): 25 parser = argparse.ArgumentParser() 26 parser.add_argument('--model', '-m', default='.\lib\mymodel.h5', required=True) 27 parser.add_argument('--testpath', '-t', default='.\lib\images\shiraishi.jpg') 28 args = parser.parse_args() 29 30 num_classes = 7 31 img_rows, img_cols = 128, 128 32 33 ident = [""] * num_classes 34 for line in open("whoiswho.txt", "r"): 35 print("line ", line) 36 dirname = line.split(",")[0] 37 label = line.split(",")[1] 38 ident[int(label)] = dirname 39 40 model = load_model(args.model) 41 faceImgs = faceDetectionFromPath(args.testpath, img_rows) 42 imgarray = [] 43 for faceImg in faceImgs: 44 faceImg.show() 45 imgarray.append(img_to_array(faceImg)) 46 imgarray = np.array(imgarray) / 255.0 47 imgarray.astype('float32') 48 49 preds = model.predict(imgarray, batch_size=imgarray.shape[0]) 50 for pred in preds: 51 predR = np.round(pred) 52 for pre_i in np.arange(len(predR)): 53 54 if predR[pre_i] == 1: 55 print("he/she is {}".format(ident[pre_i])) 56 57if __name__ == '__main__': 58 main()

こちらのエラーに対して、原因や対処法がわかる方、ご教授いただければ幸いです。
乃木坂が好きでこちらを試させていただきましたが、プログラミング初心者の私には早すぎた段階だったのかもしれません(泣)

ubuntu16-04

一応、私でもうまく動かせた画像を読み込む?コードと学習データを読み込み保存するコードも貼り付けておきますので、なにかわかる方がいらっしゃいましたら、ご享受くださいませ。

load_images.py

python

1import numpy as np 2import glob 3from keras.preprocessing.image import load_img, img_to_array 4import re 5 6def load_images_from_labelFolder(path, img_width, img_height, train_test_ratio=(9,1)): 7 pathsAndLabels = [] 8 label_i = 0 9 data_list = glob.glob(path + '/*') 10 print('path', path+'//*') 11 datatxt = open('whoiswho.txt' ,'w') 12 print('data_list', data_list) 13 for dataFolderName in data_list: 14 pathsAndLabels.append([dataFolderName, label_i]) 15 pattern = r".*//(.*)" #pathが階層を色々含んでいるので、画像が入っているフォルダ名だけを取っている。 16 matchOB = re.finditer(pattern, dataFolderName) 17 directoryname = "" 18 if matchOB: 19 for a in matchOB: 20 directoryname += a.groups()[0] 21 datatxt.write(directoryname + "," + str(label_i) + "\n") # 誰が何番かテキストで保存しとく 22 label_i = label_i + 1 23 datatxt.close() 24 allData = [] 25 for pathAndLabel in pathsAndLabels: # 全画像のパスとそのラベルをタプルでリストにし 26 path = pathAndLabel[0] 27 label = pathAndLabel[1] 28 imagelist = glob.glob(path + "//*") 29 for imgName in imagelist: 30 allData.append((imgName, label)) 31 allData = np.random.permutation(allData) # シャッフルする。 32 33 train_x = [] 34 train_y = [] 35 for (imgpath, label) in allData: #kerasが提供しているpreprocessing.imageでは画像の前処理のメソッドがある。 36 img = load_img(imgpath, target_size=(img_width,img_height)) # 画像を読み込む 37 imgarry = img_to_array(img) # 画像ファイルを学習のためにarrayに変換する。 38 train_x.append(imgarry) 39 train_y.append(label) 40 41 threshold = (train_test_ratio[0]*len(train_x))//(train_test_ratio[0]+train_test_ratio[1]) 42 test_x = np.array(train_x[threshold:]) 43 test_y = np.array(train_y[threshold:]) 44 train_x = np.array(train_x[:threshold]) 45 train_y = np.array(train_y[:threshold]) 46 47 return (train_x, train_y), (test_x, test_y) 48 49 50if __name__ == '__main__': 51 (train_x,train_y),(_,_) = load_images_from_labelFolder('/home/okamoto/lib/images', 128, 128) 52 53print('train_x.shape:',train_x.shape)

train.py

python

1from __future__ import print_function 2import keras 3from keras.models import Sequential, load_model 4from keras.layers import Dense, Dropout, Flatten 5from keras.layers import Conv2D, MaxPooling2D, BatchNormalization, ZeroPadding2D 6from keras import backend as K 7import argparse 8from load_images import load_images_from_labelFolder 9 10batch_size = 20 11num_classes = 7 12epoch = 30 13 14img_rows, img_cols = 128, 128 15 16 17parser = argparse.ArgumentParser() 18parser.add_argument('--path', '-p', default='./images') 19args = parser.parse_args() 20 21 22(x_train, y_train), (x_test, y_test) = load_images_from_labelFolder(args.path,img_cols, img_rows, train_test_ratio=(6,1)) 23 24print('x_train', x_train) 25 26if K.image_data_format() == 'channels_first': 27 x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 28 x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 29 input_shape = (1, img_rows, img_cols) 30else: 31 x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 3) 32 x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 3) 33 input_shape = (img_rows, img_cols, 3) 34 35x_train = x_train.astype('float32') 36x_test = x_test.astype('float32') 37x_train /= 255 38x_test /= 255 39 40 41# convert class vectors to binary class matrices 42y_train = keras.utils.to_categorical(y_train, num_classes) 43y_test = keras.utils.to_categorical(y_test, num_classes) 44 45model = Sequential() 46 47model.add(Conv2D(32, kernel_size=(3,3), 48 activation='relu', 49 input_shape=input_shape)) 50model.add(MaxPooling2D(pool_size=(2, 2), strides=2)) 51model.add(Dropout(0.2)) 52 53model.add(ZeroPadding2D(padding=(1, 1))) 54model.add(Conv2D(96, kernel_size=(3,3),activation='relu')) 55model.add(MaxPooling2D(pool_size=(2, 2), strides=2)) 56model.add(Dropout(0.2)) 57 58model.add(ZeroPadding2D(padding=(1, 1))) 59model.add(Conv2D(96, kernel_size=(3,3))) 60model.add(MaxPooling2D(pool_size=(2, 2), strides=2)) 61model.add(Flatten()) 62 63model.add(Dense(units=1024, activation='relu')) 64model.add(Dropout(rate=0.5)) 65model.add(Dense(units=num_classes, activation='softmax')) 66 67model.compile(loss=keras.losses.categorical_crossentropy, 68 optimizer=keras.optimizers.Adam(), 69 metrics=['accuracy']) 70 71model.fit(x_train, y_train, 72 batch_size=batch_size, 73 epochs=epoch, 74 verbose=0, 75 validation_data=(x_test, y_test)) 76 77score = model.evaluate(x_test, y_test, verbose=0) 78print('Test loss:', score[0]) 79print('Test accuracy:', score[1]) 80 81model.save('mymodel.h5') # 学習済みモデルを保存

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

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

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

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

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

t_obara

2018/10/16 02:44

エラーメッセージを読み、それがどの様な意味なのかを自分なりに考え、それを踏まえてどの様な対処をしてみたのかをご提示されるとよろしいかと思います。
atsuo

2018/10/16 02:59

ご指摘ありがとうございます。一応私なりに調べてみたところ、与えられているデータの次元が期待している次元と違うという情報を得ることができたのですが、どの段階で次元が変わってしまっているのか検討もつかない状況です。
t_obara

2018/10/16 03:05

であれば、質問をする場合、その様なタイトルにすべきですし、困っているではなく、次元が云々を明示すべきです。
tiitoi

2018/10/16 03:13

predict() 直前の imgarray.shape の値を print するとどうなっていますか?
atsuo

2018/10/16 03:53

tiitoi様 if __name__ == '__main__':の直前にprint("imgarray.shape",imgarray.shape) を入れましたが、NameError: name 'imgarray' is not definedというエラーが出てしまいます・・・。
atsuo

2018/10/16 04:00

t_obara様 たしかにその通りですね。回答していただくという立場にもかかわらず質問内容が明確でなかったことを反省します。ご指摘ありがとうございました。踏まえて質問内容を訂正させていただきましたのでもしよろしければご一緒に考えていただけると幸いです。
guest

回答1

0

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (0, 1)

got array with shape (0, 1) と出ているので、モデルに与えられた入力データがおかしいというエラーです。
画像の読み込みもしくは顔の検出ができていないのではないでしょうか?

あと、以下はおかしいと思います。dsize=(128, 128) のようにリサイズするサイズを指定するべきです。

cv2.resize(faceImg,None, fx=float(size/faceImg.shape[0]),fy=float(size/faceImg.shape[1]))

以下、確認したパスから画像を読み込み、顔を切り出してバッチで取得するコードです。

python

1import os 2import cv2 3import matplotlib.pyplot as plt 4import numpy as np 5from keras.models import Sequential, load_model 6from keras.preprocessing.image import load_img, img_to_array 7 8class FaceExtractor: 9 def __init__(self): 10 assert os.path.isfile('./haarcascade_frontalface_alt.xml'), 'カスケード定義ファイルがない' 11 self.detector = cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml') 12 13 def extract(self, path, size): 14 # 画像を読み込む。 15 img = cv2.imread(path) 16 # 検出する。 17 rects = self.detector.detectMultiScale( 18 img, scaleFactor=1.1, minNeighbors=1, minSize=(1, 1)) 19 # BGR -> RGB 20 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 21 # 顔画像を切り出す。 22 face_imgs = [] 23 for x, y, w, h in rects: 24 face_img = img[y:y + h, x: x + w] 25 face_img = cv2.resize(face_img, size) 26 face_imgs.append(face_img) 27 return np.array(face_imgs) 28 29extractor = FaceExtractor() 30face_imgs = extractor.extract('nogizaka.jpg', size=(128, 128)) 31 32print(face_imgs.shape) # (7, 128, 128, 3) 33fig, axes = plt.subplots(2, 3, figsize=(8, 5)) 34for ax, img in zip(axes.ravel(), face_imgs): 35 ax.imshow(img) 36 ax.axis('off') 37plt.show()

イメージ説明
切り出し結果

あとここもおかしいです。
astype() は配列のコピーを作成し、型を変換して返す関数なので、
以下のようにしても、imgarray 自体の型は変わりません。

imgarray.astype('float32')

上記に記載したサンプルコードのあとに以下のようにすると、(7, 128, 128, 3) の float32 型 numpy 配列が得られるのでこの x を predict() に流してください。
バッチサイズは指定しなくていいです。

x = np.array(face_imgs) / 255. x = x.astype(np.float32) y_pred = model.predict(x)

投稿2018/10/16 04:53

編集2018/10/16 05:03
tiitoi

総合スコア21956

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問