解決Keras 中加入lambda層無(wú)法正常載入模型問題
剛剛解決了這個(gè)問題,現(xiàn)在記錄下來
問題描述
當(dāng)使用lambda層加入自定義的函數(shù)后,訓(xùn)練沒有bug,載入保存模型則顯示Nonetype has no attribute 'get'
問題解決方法:
這個(gè)問題是由于缺少config信息導(dǎo)致的。lambda層在載入的時(shí)候需要一個(gè)函數(shù),當(dāng)使用自定義函數(shù)時(shí),模型無(wú)法找到這個(gè)函數(shù),也就構(gòu)建不了。
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)
補(bǔ)充知識(shí):含有Lambda自定義層keras模型,保存遇到的問題及解決方案
一,許多應(yīng)用,keras含有的層已經(jīng)不能滿足要求,需要透過Lambda自定義層來實(shí)現(xiàn)一些layer,這個(gè)情況下,只能保存模型的權(quán)重,無(wú)法使用model.save來保存模型。
保存時(shí)會(huì)報(bào)
TypeError: can't pickle _thread.RLock objects
二,解決方案,為了便于后續(xù)的部署,可以轉(zhuǎn)成tensorflow的PB進(jìn)行部署。
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])
以上這篇解決Keras 中加入lambda層無(wú)法正常載入模型問題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python3使用smtplib實(shí)現(xiàn)發(fā)送郵件功能
這篇文章主要為大家詳細(xì)介紹了python3使用smtplib實(shí)現(xiàn)發(fā)送郵件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Python中參數(shù)打包和解包的實(shí)現(xiàn)
在Python中,打包和解包參數(shù)是一種操作方式,可以將多個(gè)參數(shù)打包成一個(gè)元組或字典,也可以將一個(gè)元組或字典解包成多個(gè)參數(shù),本文就來介紹一下如何使用2023-09-09詳解使用python爬取抖音app視頻(appium可以操控手機(jī))
這篇文章主要介紹了詳解使用python爬取抖音app視頻(appium可以操控手機(jī)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Python機(jī)器學(xué)習(xí)NLP自然語(yǔ)言處理基本操作詞袋模型
本文是Python機(jī)器學(xué)習(xí)NLP自然語(yǔ)言處理系列文章,帶大家開啟一段學(xué)習(xí)自然語(yǔ)言處理 (NLP) 的旅程。本篇文章主要學(xué)習(xí)NLP自然語(yǔ)言處理基本操作之詞袋模型2021-09-09Python實(shí)現(xiàn)的簡(jiǎn)單線性回歸算法實(shí)例分析
這篇文章主要介紹了Python實(shí)現(xiàn)的簡(jiǎn)單線性回歸算法,結(jié)合實(shí)例形式分析了線性回歸算法相關(guān)原理、功能、用法與操作注意事項(xiàng),需要的朋友可以參考下2018-12-12