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

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

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

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

CNN (Convolutional Neural Network)

CNN (Convolutional Neural Network)は、全結合層のみではなく畳み込み層とプーリング層で構成されるニューラルネットワークです。画像認識において優れた性能を持ち、畳み込みニューラルネットワークとも呼ばれています。

Python 3.x

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

Q&A

解決済

1回答

1147閲覧

flow_from_directoryを用いて画像を読み込む方法

otatatatat

総合スコア3

Keras

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

CNN (Convolutional Neural Network)

CNN (Convolutional Neural Network)は、全結合層のみではなく畳み込み層とプーリング層で構成されるニューラルネットワークです。画像認識において優れた性能を持ち、畳み込みニューラルネットワークとも呼ばれています。

Python 3.x

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

0グッド

0クリップ

投稿2022/10/06 02:40

編集2022/10/06 02:42

前提

Kerasの公式ブログを参考に、画像のノイズ除去のCNNを構築しようとしています。https://keras.io/examples/vision/autoencoder/
ブログではmnistから画像を読み込んでいるところを、ディレクトリから読み込むように変更したところ、以下のエラーが発生しました。

実現したいこと

ImageDataGenerator, flow_from_directoryを用いて画像をディレクトリから読み込みたい。

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

Found 0 images belonging to 0 classes. Found 0 images belonging to 0 classes. Found 0 images belonging to 0 classes. Found 0 images belonging to 0 classes. 2022-10-06 11:36:48.762114: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found 2022-10-06 11:36:48.769260: W tensorflow/stream_executor/cuda/cuda_driver.cc:263] failed call to cuInit: UNKNOWN ERROR (303) 2022-10-06 11:36:48.774327: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: DESKTOP 2022-10-06 11:36:48.776891: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: DESKTOP- 2022-10-06 11:36:48.781744: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. Model: "model" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_1 (InputLayer) [(None, 1024, 1024, 1)] 0 _________________________________________________________________ conv2d (Conv2D) (None, 1024, 1024, 32) 320 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 512, 512, 32) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 512, 512, 32) 9248 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 256, 256, 32) 0 _________________________________________________________________ conv2d_transpose (Conv2DTran (None, 512, 512, 32) 9248 _________________________________________________________________ conv2d_transpose_1 (Conv2DTr (None, 1024, 1024, 32) 9248 _________________________________________________________________ conv2d_2 (Conv2D) (None, 1024, 1024, 1) 289 ================================================================= Total params: 28,353 Trainable params: 28,353 Non-trainable params: 0 _________________________________________________________________ Traceback (most recent call last): File "c:\Users\a_\Desktop\Python\Keras\CNN_denoising.py", line 194, in <module> autoencoder.fit( File "C:\Users\a_\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1137, in fit data_handler = data_adapter.get_data_handler( File "C:\Users\a_\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1394, in get_data_handler return DataHandler(*args, **kwargs) File "C:\Users\a_\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1149, in __init__ self._adapter = adapter_cls( File "C:\Users\a_\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 797, in __init__ raise ValueError("`y` argument is not supported when using " ValueError: `y` argument is not supported when using python generator as input.

該当のソースコード

Python

1train_data_dir = "D:/python/Object"#ノイズなし訓練画像 2img_height = 1024 3img_width = 1024 4batch_size = 32 5 6train_datagen = ImageDataGenerator(rescale=1.0 / 255) 7train_data = train_datagen.flow_from_directory( 8 directory = train_data_dir, target_size = (img_height, img_width), color_mode='grayscale', 9 class_mode='input', batch_size=batch_size, shuffle=True 10) 11 12noisy_train_data_dir = "D:/python/Result"#ノイズあり訓練画像 13noisy_train_datagen = ImageDataGenerator(rescale=1.0 / 255) 14noisy_train_data = noisy_train_datagen.flow_from_directory( 15 directory = noisy_train_data_dir, target_size = (img_height, img_width), color_mode='grayscale', 16 class_mode='input', batch_size=batch_size, shuffle=True 17) 18 19test_data_dir = "D:/python/Object"#ノイズなしテスト画像 20test_datagen = ImageDataGenerator(rescale=1.0 / 255) 21test_data = test_datagen.flow_from_directory( 22 directory = test_data_dir, target_size = (img_height, img_width), color_mode='grayscale', 23 class_mode='input', batch_size=batch_size, shuffle=True 24) 25 26noisy_test_data_dir = "D:/python/Result"#ノイズありテスト画像 27noisy_test_datagen = ImageDataGenerator(rescale=1.0 / 255) 28noisy_test_data = noisy_test_datagen.flow_from_directory( 29 directory = noisy_test_data_dir, target_size = (img_height, img_width), color_mode='grayscale', 30 class_mode='input', batch_size=batch_size, shuffle=True 31) 32 33 34autoencoder.fit( 35 train_data, 36 train_data, 37 epochs=50, 38 batch_size=128, 39 shuffle=True, 40 validation_data=(test_data, test_data), 41)

試したこと

ImageDataGeneratorを用いず、リストに画像を読み込ませる方法では正常に動いているようです。
引数class_mode を Noneやbinaryなどに変更してみたが同様のエラーがでました。

補足情報(FW/ツールのバージョンなど)

windows10 pro
Python 3.10.4
tensorflow 2.10.0

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

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

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

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

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

guest

回答1

0

ベストアンサー

エラーValueError: y argument is not supported when using python generator as input.を読んで分かる通り,ImageDataGeneratorの問題ではなく,autoencoder.fit()の際に入力と出力を各個別に渡したことが原因です.

Kerasのリファレンスを読んでもわかる通り,次のようにしなければなりません.

Python

1seed = 1 # 共通の乱数seedを使うことでshuffleしても同じ順に取り出されるようにする. 2train_datagen = ImageDataGenerator(rescale=1.0 / 255, seed = seed) 3train_data = train_datagen.flow_from_directory( 4 directory = train_data_dir, target_size = (img_height, img_width), color_mode='grayscale', 5 class_mode='input', batch_size=batch_size, shuffle=True, seed = seed 6) 7 8autoencoder.fit( 9 train_data, 10 epochs=50, 11 batch_size=128, 12 shuffle=True, 13 # validation_data=(test_data, test_data), # ここも同様に変更してください. 14)

投稿2022/10/06 04:24

編集2022/10/06 08:10
PondVillege

総合スコア1579

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

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

otatatatat

2022/10/06 08:03

回答ありがとうございます。 教えて頂いたように実行したところ、 ValueError: Layer model expects 1 input(s), but it received 2 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, None, None, None) dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=(None, None, None, None) dtype=float32>] というエラーがでました。解決の心当たりありますでしょうか?
PondVillege

2022/10/06 08:11

失礼しました,class_mode='input'の設定を見逃していました.これがあれば上のようにするだけで十分です.
otatatatat

2022/10/06 08:32

ありがとうございます。 学習は回ったのですが、 14049/Unknown - 16s 1ms/step - loss: 0.0000e+00Traceback (most recent call last): と無意味な学習をしてしまいます。もう少し考えてわからなかったら別で質問させていただきます。 ありがとうございました。
PondVillege

2022/10/06 08:39 編集

かしこまりました.別の質問の際には,本質問からの継承であることをリンクなどで明示して,新しく質問ページを立てるようにしてください.その際にはモデルのsummaryではなく,コードの方も記述をお願いします.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問