使用keras load_model模块加载自定义网络层时注意的问题

本文总结了在使用Keras自定义网络层时遇到的模型保存问题,重点在于如何确保模型结构和权重的完整恢复,以及如何正确加载自定义层。推荐设置save_weights_only=False并提供自定义层参数。

引言

最近在使用keras自定义网络层时,保存模型时出现了很多bug,困扰了好几天。现在把他总结如下。
keras作为一个封装好的文件直接可以被调用,一直受到初学者的追捧,但是,封装好的模块可能满足不了需求,keras自定义网络层时必须遵守‘八股文’规则所以很受人诟病,下面总结一下在自定义网络层时的bug。

解决方案

  • 在使用keras的回调函数checkpoint方式保存模型时要设置参数save_weights_only = False,否则默认只保存权重,不保存网络结构,在调用时还要重新加载网络结构才能使用.hdf5文件,详情参考
    https://zhuanlan.zhihu.com/p/86886620
  • 这里默认你已经创造好自己的模型,并已经训练好了,保存了’.hdf5’文件,在重新调用模型时(加载hdf5文件)需要引入你自定义的网络层(from xxx import xxx),再者使用
load_model(filepath,custom_objects={'TCN':TCN,'Attention_layer':Attention_layer(W_regularizer=None, b_regularizer=None,W_constraint=None, b_constraint=None,bias=True))
  • 需要注意的是:要加上网络层自定义的参数值
import numpy as np import tensorflow as tf from tensorflow.keras.models import load_model, Model from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.utils import to_categorical from tensorflow.keras.datasets import cifar100 import matplotlib.pyplot as plt import cv2 # 加载保存的ResNet-34模型 model = load_model('base_resnet34_cifar100.h5')import numpy as np import tensorflow as tf from tensorflow.keras.models import load_model, Model from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.utils import to_categorical from tensorflow.keras.datasets import cifar100 import matplotlib.pyplot as plt import cv2 # 加载保存的ResNet-34模型 model = load_model('base_resnet34_cifar100.h5')//报错Traceback (most recent call last): //报错 File "c:/Users/赵培伊/Desktop/大三下文件存放处/神经网络代码/大作业测试111.py", line 11, in <module> model = load_model('base_resnet34_cifar100.h5') File "C:\anaconda\envs\tensorflow2.4\lib\site-packages\tensorflow\python\keras\saving\save.py", line 206, in load_model return hdf5_format.load_model_from_hdf5(filepath, custom_objects, File "C:\anaconda\envs\tensorflow2.4\lib\site-packages\tensorflow\python\keras\saving\hdf5_format.py", line 183, in load_model_from_hdf5 model = model_config_lib.model_from_config(model_config, File "C:\anaconda\envs\tensorflow2.4\lib\site-packages\tensorflow\python\keras\saving\model_config.py", line 64, in model_from_config return deserialize(config, custom_objects=custom_objects) File "C:\anaconda\envs\tensorflow2.4\lib\site-packages\tensorflow\python\keras\layers\serialization.py", line 173, in deserialize return generic_utils.deserialize_keras_object( File "C:\anaconda\envs\tensorflow2.4\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 354, in deserialize_keras_object return cls.from_config( File "C:\anaconda\envs\tensorflow2.4\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 668, in from_config input_tensors, output_tensors, created_layers = reconstruct_from_config( File "C:\anaconda\envs\tensorflow2.4\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 1275, in reconstruct_from_config process_layer(layer_data) File "C:\anaconda\envs\tensorflow2.4\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 1257, in process_layer layer = deserialize_layer(layer_data, custom_objects=custom_objects) File "C:\anaconda\envs\tensorflow2.4\lib\site-packages\tensorflow\python\keras\layers\serialization.py", line 173, in deserialize return generic_utils.deserialize_keras_object( File "C:\anaconda\envs\tensorflow2.4\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 360, in deserialize_keras_object return cls.from_config(cls_config) File "C:\anaconda\envs\tensorflow2.4\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 720, in from_config return cls(**config) File "C:\anaconda\envs\tensorflow2.4\lib\site-packages\tensorflow\python\keras\engine\input_layer.py", line 118, in __init__ raise ValueError('Unrecognized keyword arguments:', kwargs.keys()) ValueError: ('Unrecognized keyword arguments:', dict_keys(['batch_shape']))
07-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值