model.fitがValueErrorになる

実現したいこと

オートエンコーダを使えるようになりたい。
エラーの意味が理解できるようになりたい。

発生している問題・分からないこと

python初心者です。
以下のサイトを見て異常検知AIを作ろうとしたのですが以下のエラーメッセージが発生しました。
https://qiita.com/michelle0915/items/28bc5b844bd0d7ab597b
学習用の画像として大きさ300×225のRGB画像を使用したいです。

エラーメッセージ

error

1(0,) 2Epoch 1/50 3--------------------------------------------------------------------------- 4ValueError Traceback (most recent call last) 5Cell In[3], line 76 6 74 # 学習実行 7 75 print(train.shape) 8---> 76 model.fit( 9 77 train, 10 78 train, 11 79 batch_size=BATCH_SIZE, 12 80 epochs=EPOCHS 13 81 ) 14 15File ~\anaconda3\Lib\site-packages\keras\src\utils\traceback_utils.py:122, in filter_traceback.<locals>.error_handler(*args, **kwargs) 16 119 filtered_tb = _process_traceback_frames(e.__traceback__) 17 120 # To get the full stack trace, call: 18 121 # `keras.config.disable_traceback_filtering()` 19--> 122 raise e.with_traceback(filtered_tb) from None 20 123 finally: 21 124 del filtered_tb 22 23File ~\anaconda3\Lib\site-packages\keras\src\models\functional.py:280, in Functional._adjust_input_rank(self, flat_inputs) 24 278 adjusted.append(ops.expand_dims(x, axis=-1)) 25 279 continue 26--> 280 raise ValueError( 27 281 f"Invalid input shape for input {x}. Expected shape " 28 282 f"{ref_shape}, but input has incompatible shape {x.shape}" 29 283 ) 30 284 # Add back metadata. 31 285 for i in range(len(flat_inputs)): 32 33ValueError: Exception encountered when calling Functional.call(). 34 35Invalid input shape for input Tensor("data:0", shape=(8,), dtype=float32). Expected shape (None, 300, 225, 3), but input has incompatible shape (8,) 36 37Arguments received by Functional.call(): 38 • inputs=tf.Tensor(shape=(8,), dtype=float32) 39 • training=True 40 • mask=None 41

該当のソースコード

from tensorflow.keras.layers import Input, Conv2D, Flatten, Dense, Conv2DTranspose, Reshape, Activation, LeakyReLU from tensorflow.keras.models import Model from tensorflow.keras import backend as K from tensorflow.keras.optimizers import Adam import numpy as np import cv2 import glob # 学習データの読み込み&前処理 train_images = glob.glob('retraining_images/*') train = [] for i in train_images: image = cv2.imread(i) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) train.append(image) train = np.array(train) train = train.astype('float32') / 255 # 学習用ハイパーパラメータ LEARNING_RATE = 0.0005 BATCH_SIZE = 8 Z_DIM = 100 EPOCHS = 50 # エンコーダ encoder_input = Input(shape=(300,225,3), name='encoder_input') x = encoder_input x = Conv2D(filters=32, kernel_size=3, strides=1, padding='same', name='encoder_conv_0')(x) x = LeakyReLU()(x) x = Conv2D(filters=32, kernel_size=3, strides=1, padding='same', name='encoder_conv_0_1')(x) x = LeakyReLU()(x) x = Conv2D(filters=64, kernel_size=3, strides=2, padding='same', name='encoder_conv_1')(x) x = LeakyReLU()(x) x = Conv2D(filters=64, kernel_size=3, strides=2, padding='same', name='encoder_conv_2')(x) x = LeakyReLU()(x) x = Conv2D(filters=64, kernel_size=3, strides=1, padding='same', name='encoder_conv_3')(x) x = LeakyReLU()(x) shape_before_flattening = K.int_shape(x)[1:] x = Flatten()(x) encoder_output = Dense(Z_DIM, name='encoder_output')(x) encoder = Model(encoder_input, encoder_output) # デコーダ decoder_input = Input(shape=(Z_DIM,), name='decoder_input') x = Dense(np.prod(shape_before_flattening))(decoder_input) x = Reshape(shape_before_flattening)(x) x = Conv2DTranspose(filters=64, kernel_size=3, strides=1, padding='same', name='decoder_conv_t_0')(x) x = LeakyReLU()(x) x = Conv2DTranspose(filters=64, kernel_size=3, strides=2, padding='same', name='decoder_conv_t_1')(x) x = LeakyReLU()(x) x = Conv2DTranspose(filters=32, kernel_size=3, strides=2, padding='same', name='decoder_conv_t_2')(x) x = LeakyReLU()(x) x = Conv2DTranspose(filters=32, kernel_size=3, strides=1, padding='same', name='decoder_conv_t_2_5')(x) x = LeakyReLU()(x) x = Conv2DTranspose(filters=3, kernel_size=3, strides=1, padding='same', name='decoder_conv_t_3')(x) x = Activation('sigmoid')(x) decoder_output = x decoder = Model(decoder_input, decoder_output) # エンコーダ/デコーダ連結 model_input = encoder_input model_output = decoder(encoder_output) model = Model(model_input, model_output) # 学習用設定設定(最適化関数、損失関数) optimizer = Adam(learning_rate=LEARNING_RATE) def r_loss(y_true, y_pred): return K.mean(K.square(y_true - y_pred), axis=[1,2,3]) model.compile(optimizer=optimizer, loss=r_loss) # 学習実行 print(train.shape) model.fit( train, train, batch_size=BATCH_SIZE, epochs=EPOCHS )

試したこと・調べたこと

上記の詳細・結果

サイトでは300×300の画像を使用しているので自分が使用したい画像(300×225)に合わせて変更しました。

補足

opencv-python:4.9.0.80
tensorflow:2.16.1
anaconda3 jupyter notebook

コメントを投稿

0 コメント