質問内容
オートエンコーダを用いた画像の異常検知をしていたところ際に学習まではうまくいったのですが、実際に異常箇所を特定するところで、以下のようなエラーが発生してしまいました。
また、resize_original_images内に存在する画像データはすべて(300, 300, 3)のshapeに統一されています。
実際のコード
files = glob.glob("/content/drive/MyDrive/resize_original_images/*.jpg") evaluation_image=[] for l in files: images = cv2.imread(l) images = cv2.cvtColor(images, cv2.COLOR_BGR2RGB) evaluation_image.append(images) evaluation_image = np.array(evaluation_image) evaluation_image = evaluation_image.astype('float32')/255 for e in evaluation_image: z_points = encoder.predict(e) reconst_images = decoder.predict(z_points) # 元画像との差分を計算 diff_images = np.absolute(reconst_images - e) print(diff_images)
エラーコード
/usr/local/lib/python3.8/dist-packages/tensorflow/python/data/ops/structured_function.py:264: UserWarning: Even though the `tf.config.experimental_run_functions_eagerly` option is set, this option does not apply to tf.data functions. To force eager execution of tf.data functions, please use `tf.data.experimental.enable_debug_mode()`. warnings.warn( --------------------------------------------------------------------------- /usr/local/lib/python3.8/dist-packages/tensorflow/python/data/ops/structured_function.py:264: UserWarning: Even though the `tf.config.experimental_run_functions_eagerly` option is set, this option does not apply to tf.data functions. To force eager execution of tf.data functions, please use `tf.data.experimental.enable_debug_mode()`. warnings.warn( --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-8-4e904f052eee> in <module> 9 evaluation_image = evaluation_image.astype('float32')/255 10 for e in evaluation_image: ---> 11 z_points = encoder.predict(e) 12 reconst_images = decoder.predict(z_points) 13 # 元画像との差分を計算 1 frames /usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs) 65 except Exception as e: # pylint: disable=broad-except 66 filtered_tb = _process_traceback_frames(e.__traceback__) ---> 67 raise e.with_traceback(filtered_tb) from None 68 finally: 69 del filtered_tb /usr/local/lib/python3.8/dist-packages/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name) 262 if spec_dim is not None and dim is not None: 263 if spec_dim != dim: --> 264 raise ValueError(f'Input {input_index} of layer "{layer_name}" is ' 265 'incompatible with the layer: ' 266 f'expected shape={spec.shape}, ' ValueError: Input 0 of layer "model_6" is incompatible with the layer: expected shape=(None, 300, 300, 3), found shape=(32, 300, 3)
質問に記載のエラーメッセージよりも上に「Traceback」と書かれてたら、そこから下をできるだけ省略せずに質問に記載してください
(ここに書くのではなく、質問を編集して追記する)
ユーザー名等の個人情報は伏せ字でいいですが、それ以外はできるだけそのまま記載してください
コメントありがとうございます。
編集の方させていただきました。
エラーが出てる「z_points = encoder.predict(e)」のすぐ上に(インデントを合わせて)下記を追加して実行したら、何て表示されますでしょうか?
print(e.shape)
> ValueError: Input 0 of layer "model_6" is incompatible with the layer: expected shape=(None, 300, 300, 3), found shape=(32, 300, 3)
は、それが「数字, 300, 300, 3」じゃないといけないのに、そうなってないのが原因だと思います
> resize_original_images内に存在する画像データはすべて(300, 300, 3)のshapeに統一されています。
コードから想像すると、「evaluation_image」に含まれる画像から一枚ずつ取り出して「encoder」で推論させようとしてるようですが、一枚の画像の場合もshapeが「1, 300, 300, 3」となってる必要があります
print(e.shape)と実行すると
(300, 300, 3)
(300, 300, 3)
(300, 300, 3)
(300, 300, 3)
(300, 300, 3)
(300, 300, 3)
(300, 300, 3)
(300, 300, 3)
(300, 300, 3)
と結果が出ます
> print(e.shape)と実行すると (300, 300, 3)
前のコメントにも書きましたが、それがエラーの原因です
「300, 300, 3」ではなく「1, 300, 300, 3」となってる必要があります
この場合,次の2択になります
1. z_points = encoder.predict(np.array([e])) とする
2. forループを使って画像1枚ずつpredictするのではなく,画像全部であるevaluation_imageを使ってencoder.predict(evaluation_image)とする.
いずれもモデルに渡したshapeはNHWC形式なので動きます.
ありがとうございます。
何とか実行することができました。
回答1件
あなたの回答
tips
プレビュー