解决Keras 中加入lambda层无法正常载入模型问题
作者:机器玄学实践者 时间:2022-02-21 03:41:11
刚刚解决了这个问题,现在记录下来
问题描述
当使用lambda层加入自定义的函数后,训练没有bug,载入保存模型则显示Nonetype has no attribute 'get'
问题解决方法:
这个问题是由于缺少config信息导致的。lambda层在载入的时候需要一个函数,当使用自定义函数时,模型无法找到这个函数,也就构建不了。
m = load_model(path,custom_objects={"reduce_mean":self.reduce_mean,"slice":self.slice})
其中,reduce_mean 和slice定义如下
def slice(self,x, turn):
""" Define a tensor slice function
"""
return x[:, turn, :, :]
def reduce_mean(self, X):
return K.mean(X, axis=-1)
补充知识:含有Lambda自定义层keras模型,保存遇到的问题及解决方案
一,许多应用,keras含有的层已经不能满足要求,需要透过Lambda自定义层来实现一些layer,这个情况下,只能保存模型的权重,无法使用model.save来保存模型。
保存时会报
TypeError: can't pickle _thread.RLock objects
二,解决方案,为了便于后续的部署,可以转成tensorflow的PB进行部署。
from keras.models import load_model
import tensorflow as tf
import os, sys
from keras import backend as K
from tensorflow.python.framework import graph_util, graph_io
def h5_to_pb(h5_weight_path, output_dir, out_prefix="output_", log_tensorboard=True):
if not os.path.exists(output_dir):
os.mkdir(output_dir)
h5_model = build_model()
h5_model.load_weights(h5_weight_path)
out_nodes = []
for i in range(len(h5_model.outputs)):
out_nodes.append(out_prefix + str(i + 1))
tf.identity(h5_model.output[i], out_prefix + str(i + 1))
model_name = os.path.splitext(os.path.split(h5_weight_path)[-1])[0] + '.pb'
sess = K.get_session()
init_graph = sess.graph.as_graph_def()
main_graph = graph_util.convert_variables_to_constants(sess, init_graph, out_nodes)
graph_io.write_graph(main_graph, output_dir, name=model_name, as_text=False)
if log_tensorboard:
from tensorflow.python.tools import import_pb_to_tensorboard
import_pb_to_tensorboard.import_to_tensorboard(os.path.join(output_dir, model_name), output_dir)
def build_model():
inputs = Input(shape=(784,), name='input_img')
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
y = Dense(10, activation='softmax')(x)
h5_model = Model(inputs=inputs, outputs=y)
return h5_model
if __name__ == '__main__':
if len(sys.argv) == 3:
# usage: python3 h5_to_pb.py h5_weight_path output_dir
h5_to_pb(h5_weight_path=sys.argv[1], output_dir=sys.argv[2])
来源:https://blog.csdn.net/weixin_39673686/article/details/90697587
标签:Keras,lambda,载入,模型
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL 数据库存储和分支
2011-03-11 19:01:00
Python中List.count()方法的使用教程
2023-08-12 11:56:15
PHP封装的PDO数据库操作类实例
2023-11-18 04:54:31
使用ewebeditor可能会重复提交数据两次的解决办法
2009-01-09 12:41:00
wiondws XP下安装MySQL图文教程
2007-06-15 09:29:00
![](https://img.aspxhome.com/file/uploadpic/20076/15/2007615151328962.gif)
python3 爬取图片的实例代码
2021-08-22 13:37:57
在Python的Django框架的视图中使用Session的方法
2023-06-22 07:10:15
php 方便水印和缩略图的图形类
2023-10-15 15:31:28
吴恩达机器学习练习:SVM支持向量机
2023-10-30 11:49:53
![](https://img.aspxhome.com/file/2023/5/81695_0s.png)
python中使用PIL制作并验证图片验证码
2023-06-05 11:28:33
python批量从es取数据的方法(文档数超过10000)
2022-03-23 01:21:48
Python的迭代器和生成器
2022-04-29 17:56:09
ASP.NET(AJAX+JSON)实现对象调用
2023-07-19 12:29:33
学习ASP.NET八天入门:第六天
2007-08-07 13:48:00
![](https://img.aspxhome.com/file/UploadPic/20078/7/200787135226497s.gif)
SQL Server SA权限最新入侵方法
2009-03-25 16:56:00
基于python实现模拟数据结构模型
2022-11-12 23:44:01
用Dreamweaver MX巧妙格式化表格
2008-03-18 16:39:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113211174s.jpg)
HTML5本地存储初探(二)
2010-03-07 15:47:00
![](https://img.aspxhome.com/file/UploadPic/20103/7/20100131-rmki9pjq85xy9nbt436gkdfb7i-94s.png)
Python 获取windows桌面路径的5种方法小结
2023-04-01 09:06:47
DreamWeaver批处理提高篇
2007-12-03 11:34:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113447246s.gif)