训练过程后加载具有自定义损失函数的 coatnet 模型时出错

问题描述 投票:0回答:0

我在 TF 的 CoAtNet0 上做迁移学习,

首先我从 keras_cv_attention_models.coatnet 加载模型, 具有自定义损失函数,并在几个时期后保存。 比我用我的代码加载保存模型以将某些层设置为可训练的。 几个时期后,我再次保存了模型,并尝试使用与以前完全相同的代码加载它,只是将更多层设置为 trainble,

但现在我得到了那个错误:

Traceback (most recent call last):
  File "train.py", line 88, in <module>
    new_model = tf.keras.models.load_model(saved_model_path, custom_objects=
  File "/home/.local/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2749, in canonicalize_function_inputs
    raise TypeError(f"{self.signature_summary()} missing 1 required "
TypeError: Exception encountered when calling layer "stack_3_block_1_mhsa_pos_emb" (type beit>MultiHeadRelativePositionalEmbedding).

f(attention_scores, training, training, training, training, *, training, training) missing 1 required argument: training.

Call arguments received:
  • args=('tf.Tensor(shape=(None, 6, 196, 196), dtype=float32)',)
  • kwargs={'training': 'False'}
Exception ignored in: <function Pool.__del__ at 0x7f789376b8b0>
Traceback (most recent call last):
  File "/usr/lib/python3.8/multiprocessing/pool.py", line 268, in __del__
  File "/usr/lib/python3.8/multiprocessing/queues.py", line 362, in put
AttributeError: 'NoneType' object has no attribute 'dumps'

我首先加载模型:

model_base = coatnet.CoAtNet0(input_shape=(224, 224, 3))

训练后,下一次加载,我这样做,加载自定义损失:

new_model = tf.keras.models.load_model(saved_model_path, custom_objects=
        {'focal_loss': custom_loss(gamma=2.0, alpha=0.25, ls=0.125, classes=3.0)})

在又一个时期之后,我尝试用上面的两个单独的模型再次加载模型,并得到了我提到的错误。

我将不胜感激

python tensorflow keras deep-learning computer-vision
© www.soinside.com 2019 - 2024. All rights reserved.