前提・実現したいこと
機械学習初心者です。
Kerasでモデルを作成しています。
どの食べ物かを判定するAIで,フィッティングを行っています。
しかし画像ファイルが数千枚あり,少量の画像ならしっかりフィッティングできるのですが大量の画像だとエラーが出てしまいます。詳しい方ご教授お願いします。
発生している問題・エラーメッセージ
Epoch 1/10 Traceback (most recent call last): File "C:\Users\ユーザ\Documents\work\train2.py", line 51, in <module> main() File "C:\Users\ユーザ\Documents\work\train2.py", line 41, in main model.fit_generator( File "C:\anaconda3\lib\site-packages\keras\engine\training.py", line 1975, in fit_generator return self.fit( File "C:\anaconda3\lib\site-packages\keras\engine\training.py", line 1184, in fit tmp_logs = self.train_function(iterator) File "C:\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py", line 885, in __call__ result = self._call(*args, **kwds) File "C:\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py", line 950, in _call return self._stateless_fn(*args, **kwds) File "C:\anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 3039, in __call__ return graph_function._call_flat( File "C:\anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 1963, in _call_flat return self._build_call_outputs(self._inference_function.call( File "C:\anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 591, in call outputs = execute.execute( File "C:\anaconda3\lib\site-packages\tensorflow\python\eager\execute.py", line 59, in quick_execute tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 11520 values, but the requested shape requires a multiple of 12544 [[node sequential/flatten/Reshape (defined at Users\ユーザ\Documents\work\train2.py:41) ]] [Op:__inference_train_function_759] Function call stack: train_function
該当のソースコード
Python
1from keras.models import Sequential 2from keras.layers import Activation, Dense 3from keras.layers import Conv2D, MaxPooling2D, Flatten 4from keras.preprocessing.image import ImageDataGenerator 5 6def main(): 7 model = Sequential() 8 model.add(Conv2D(64,(3,3), input_shape = (64,64,3))) 9 model.add(Activation("relu")) 10 model.add(MaxPooling2D(pool_size = (2,2))) 11 12 model.add(Conv2D(64,(3,3))) 13 model.add(Activation("relu")) 14 model.add(MaxPooling2D(pool_size = (2,2))) 15 16 model.add(Flatten()) 17 model.add(Dense(256)) 18 model.add(Activation("relu")) 19 model.add(Dense(2)) 20 model.add(Activation("softmax")) 21 # model.summary() 22 23 model.compile( 24 optimizer = "adam", 25 loss = "categorical_crossentropy", 26 metrics = ["accuracy"]) 27 28 train_datagen = ImageDataGenerator(rescale = 1./255) 29 test_datagen = ImageDataGenerator(rescale = 1./255) 30 31 train_generator = train_datagen.flow_from_directory( 32 "data/images",#ここに数千枚の画像 33 target_size = (64,64), 34 batch_size = 10) 35 36 validation_generator = test_datagen.flow_from_directory( 37 "data/test",#ここに数百枚の画像 38 target_size = (64,64), 39 batch_size = 10) 40 41 model.fit_generator( 42 train_generator, 43 epochs = 5, 44 steps_per_epoch = 10, 45 validation_data = validation_generator, 46 validation_steps = 10) 47 48 model.save("model.h5") 49 50if __name__ == "__main__": 51 main()
試したこと
エポック数を高くしてみた
補足情報(FW/ツールのバージョンなど)
anaconda3