带有自定义 objects 的 Keras load_model 无法正常工作
Keras load_model with custom objects doesn't work properly
设置
如标题中所述,在尝试加载保存的模型时,我的自定义损失函数出现问题。我的损失如下:
def weighted_cross_entropy(weights):
weights = K.variable(weights)
def loss(y_true, y_pred):
y_pred = K.clip(y_pred, K.epsilon(), 1-K.epsilon())
loss = y_true * K.log(y_pred) * weights
loss = -K.sum(loss, -1)
return loss
return loss
weighted_loss = weighted_cross_entropy([0.1,0.9])
所以在训练过程中,我使用 weighted_loss
函数作为损失函数,一切正常。训练完成后,我使用来自 keras API.
的标准 model.save
函数将模型保存为 .h5
文件
问题
当我尝试通过
加载模型时
model = load_model(path,custom_objects={"weighted_loss":weighted_loss})
我收到一条 ValueError
消息,告诉我损失未知。
错误
错误消息如下所示:
File "...\predict.py", line 29, in my_script
"weighted_loss": weighted_loss})
File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\engine\saving.py", line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)
File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\engine\saving.py", line 312, in _deserialize_model
sample_weight_mode=sample_weight_mode)
File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\engine\training.py", line 139, in compile
loss_function = losses.get(loss)
File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\losses.py", line 133, in get
return deserialize(identifier)
File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\losses.py", line 114, in deserialize
printable_module_name='loss function')
File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\utils\generic_utils.py", line 165, in deserialize_keras_object
':' + function_name)
ValueError: Unknown loss function:loss
问题
我该如何解决这个问题?可能是我的包装损失定义的原因吗?所以keras
不知道,如何处理weights
变量?
您的损失函数的名称是 loss
(即 def loss(y_true, y_pred):
)。因此,加载回模型时,您需要指定 'loss'
作为其名称:
model = load_model(path, custom_objects={'loss': weighted_loss})
有关演示 使用自定义损失函数或模型保存和加载 Keras 模型 的完整示例,请查看以下 GitHub 要点文件:
使用包装器定义的自定义损失函数:
https://gist.github.com/ashkan-abbasi66/a81fe4c4d588e2c187180d5bae734fde
通过子类化定义的自定义损失函数:
https://gist.github.com/ashkan-abbasi66/327efe2dffcf9788847d26de934ef7bd
自定义模型:
https://gist.github.com/ashkan-abbasi66/d5a525d33600b220fa7b095f7762cb5b
注意:
我用 Tensorflow 2.5 在 Python 3.8 上测试了上面的例子。
设置
如标题中所述,在尝试加载保存的模型时,我的自定义损失函数出现问题。我的损失如下:
def weighted_cross_entropy(weights):
weights = K.variable(weights)
def loss(y_true, y_pred):
y_pred = K.clip(y_pred, K.epsilon(), 1-K.epsilon())
loss = y_true * K.log(y_pred) * weights
loss = -K.sum(loss, -1)
return loss
return loss
weighted_loss = weighted_cross_entropy([0.1,0.9])
所以在训练过程中,我使用 weighted_loss
函数作为损失函数,一切正常。训练完成后,我使用来自 keras API.
model.save
函数将模型保存为 .h5
文件
问题
当我尝试通过
加载模型时model = load_model(path,custom_objects={"weighted_loss":weighted_loss})
我收到一条 ValueError
消息,告诉我损失未知。
错误
错误消息如下所示:
File "...\predict.py", line 29, in my_script
"weighted_loss": weighted_loss})
File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\engine\saving.py", line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)
File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\engine\saving.py", line 312, in _deserialize_model
sample_weight_mode=sample_weight_mode)
File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\engine\training.py", line 139, in compile
loss_function = losses.get(loss)
File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\losses.py", line 133, in get
return deserialize(identifier)
File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\losses.py", line 114, in deserialize
printable_module_name='loss function')
File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\utils\generic_utils.py", line 165, in deserialize_keras_object
':' + function_name)
ValueError: Unknown loss function:loss
问题
我该如何解决这个问题?可能是我的包装损失定义的原因吗?所以keras
不知道,如何处理weights
变量?
您的损失函数的名称是 loss
(即 def loss(y_true, y_pred):
)。因此,加载回模型时,您需要指定 'loss'
作为其名称:
model = load_model(path, custom_objects={'loss': weighted_loss})
有关演示 使用自定义损失函数或模型保存和加载 Keras 模型 的完整示例,请查看以下 GitHub 要点文件:
使用包装器定义的自定义损失函数: https://gist.github.com/ashkan-abbasi66/a81fe4c4d588e2c187180d5bae734fde
通过子类化定义的自定义损失函数: https://gist.github.com/ashkan-abbasi66/327efe2dffcf9788847d26de934ef7bd
自定义模型: https://gist.github.com/ashkan-abbasi66/d5a525d33600b220fa7b095f7762cb5b
注意: 我用 Tensorflow 2.5 在 Python 3.8 上测试了上面的例子。