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

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

新規登録して質問してみよう
ただいま回答率
85.49%
Python 3.x

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

Q&A

解決済

1回答

4448閲覧

keras ImageDataGeneratorを用いた時のエラー listdir: embedded null character in path について

yohehe

総合スコア48

Python 3.x

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

0グッド

0クリップ

投稿2019/05/27 09:26

CNNを用いた画像認識を勉強しているのですが、ImageDataGeneratorを用いてデータ拡張を行なって画像の学習を行いたいのですが、
listdir: embedded null character in path というエラーが出現してしまい、

エラーへの対処の仕方がわからず困っております。

python

1 2 3#CNN  4#X.shape:32*32*3のRGBデータ,17500枚の画像データ(17500, 32, 32, 3) 少数化(1./255)はしていない。 5#y:(17500,)の[0,1]の2値データになっております。 6 7from sklearn.model_selection import train_test_split 8 9X_train,X_test,y_train,y_test=train_test_split(X,y, 10 test_size=0.3, 11 random_state=0) 12 13#onehot vectorに変換 14from keras.utils import np_utils 15y_train=np_utils.to_categorical(y_train) 16y_test=np_utils.to_categorical(y_test) 17 18#shape 19print("X_train:",X_train.shape) 20#X_train: (12250, 32, 32, 3) 21print("X_test:",X_test.shape) 22#X_test: (5250, 32, 32, 3) 23 24from keras.layers import Conv2D,MaxPooling2D,Flatten,Dense 25from keras.models import Sequential 26from keras.optimizers import Adagrad 27 28model=Sequential() 29 30model.add(Conv2D(32,(3,3),activation="relu",input_shape=(32,32,3))) 31model.add(MaxPooling2D((2,2))) 32model.add(Conv2D(64,(3,3),activation="relu")) 33model.add(MaxPooling2D((2,2))) 34model.add(Conv2D(64,(3,3),activation="relu")) 35model.add(Flatten()) 36model.add(Dense(64,activation="relu")) 37model.add(Dense(units=n_classes,activation="sigmoid")) 38model.summary() 39model.compile(loss="binary_crossentropy",optimizer="RMSprop",metrics=["accuracy"]) 40 41#データ拡張の導入 42 43from keras.preprocessing.image import ImageDataGenerator 44 45train_datagen=ImageDataGenerator(rotation_range=40, 46 width_shift_range=0.2, 47 height_shift_range=0.2, 48 shear_range=0.2, 49 horizontal_flip=True,fill_mode="nearest") 50 51test_datagen=ImageDataGenerator(rescale=1./255) 52 53#上記まではエラーは発生しません 54 55#以下のコードにおいてエラーが出現します。 56 57train_generator=train_datagen.flow_from_directory(X_train,target_size(32,32),batch_size=32,class_mode="binary") 58 59validation_generator=test_datagen.flow_from_directory(X_test, 60 target_size=(32,32), 61 batch_size=32, 62 class_mode="binary") 63 64model.fit_generator(train_generator,steps_per_epoch=2000,epochs=50, 65 validation_data=validation_generator,validation_steps=800) 66 67#上記を出力すると、 68 69/anaconda3/lib/python3.6/site-packages/keras_preprocessing/image.py in __init__(self, directory, image_data_generator, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, data_format, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation, dtype) 70 1873 if not classes: 71 1874 classes = [] 72-> 1875 for subdir in sorted(os.listdir(directory)): 73 1876 if os.path.isdir(os.path.join(directory, subdir)): 74 1877 classes.append(subdir) 75 76ValueError: listdir: embedded null character in path 77 78コード

上記のエラーが出現します。
ImageDataGenratorを用いず、Xを1./255で少数化および正規化を行なって、CNNでの分類を行なった際には動作しているため、nullはないと思うのですが、
ImageDataGeneratorの使い方が間違っているのでしょうか。

アドバイスいただけますと幸いです。
よろしくお願いいたします。

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

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

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

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

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

tiitoi

2019/05/27 09:38

flow_from_directory() の第一引数にはデータセットがあるディレクトリを指定するはずですが、質問のコードでは配列が指定されているのはおかしくないでしょうか。 https://keras.io/ja/preprocessing/image/
yohehe

2019/05/27 09:49

tiitoi様、ご指摘ありがとうございます。 リンクを読むと、引数のディレクトリの指定が配列になっていました。 io.imread()で画像データを読み込んでいる場合は、リンクにあるように.flow(X,y)の方を用いてコードを修正します。
tiitoi

2019/05/27 09:56

> flow(X,y)の方を用いてコードを修正します。 その方針でよいと思います。
yohehe

2019/05/27 10:00

tiitoi様、flow(X,y)に設定し直して、エラーなく動作し始めました。動作の検証をして解決済にしようと思います。 ありがとうございました。
guest

回答1

0

自己解決

python

1 2from sklearn.model_selection import train_test_split 3 4X_train,X_test,y_train,y_test=train_test_split(X,y, 5 test_size=0.3, 6 random_state=0) 7 8#onehot-vector 9from keras.utils import np_utils 10y_train=np_utils.to_categorical(y_train) 11y_test=np_utils.to_categorical(y_test) 12 13#データ拡張を導入する場合にもCNNのコンパイルまでは通常通り行う。 14 15from keras.preprocessing.image import ImageDataGenerator 16 17train_datagen=ImageDataGenerator(rotation_range=40, 18 width_shift_range=0.2, 19 height_shift_range=0.2, 20 shear_range=0.2, 21 horizontal_flip=True,fill_mode="nearest") 22 23train_datagen.fit(X_train) 24model.fit_generator(train_datagen.flow(X_train,y_train,batch_size=32), 25 steps_per_epoch=len(X_train)/32,epochs=10) 26 27 28#epochsなどは動作確認のため適当ですが、上記で動作し始めました。 29 30#flow_from_directoryを用いる場合は、array化されたデータではなく、画像データのディレクトリを指定する部分が間違っておりました。 31train_datagen.flow_from_directory(X_train,target_size(32,32),batch_size=32,class_mode="binary") 32 33 34

投稿2019/05/27 09:58

編集2019/05/27 10:01
yohehe

総合スコア48

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問