創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買多久送多久,劃算不套路!
成都創(chuàng)新互聯(lián)致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營銷,包括成都網(wǎng)站建設(shè)、做網(wǎng)站、SEO優(yōu)化、網(wǎng)絡(luò)推廣、整站優(yōu)化營銷策劃推廣、電子商務(wù)、移動(dòng)互聯(lián)網(wǎng)營銷等。成都創(chuàng)新互聯(lián)為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制及解決方案,成都創(chuàng)新互聯(lián)核心團(tuán)隊(duì)十載專注互聯(lián)網(wǎng)開發(fā),積累了豐富的網(wǎng)站經(jīng)驗(yàn),為廣大企業(yè)客戶提供一站式企業(yè)網(wǎng)站建設(shè)服務(wù),在網(wǎng)站建設(shè)行業(yè)內(nèi)樹立了良好口碑。不懂Keras模型轉(zhuǎn)成tensorflow中.pb的方法?其實(shí)想解決這個(gè)問題也不難,下面讓小編帶著大家一起學(xué)習(xí)怎么去解決,希望大家閱讀完這篇文章后大所收獲。
Keras的.h6模型轉(zhuǎn)成tensorflow的.pb格式模型,方便后期的前端部署。直接上代碼
from keras.models import Model from keras.layers import Dense, Dropout from keras.applications.mobilenet import MobileNet from keras.applications.mobilenet import preprocess_input from keras.preprocessing.image import load_img, img_to_array import tensorflow as tf from keras import backend as K import os base_model = MobileNet((None, None, 3), alpha=1, include_top=False, pooling='avg', weights=None) x = Dropout(0.75)(base_model.output) x = Dense(10, activation='softmax')(x) model = Model(base_model.input, x) model.load_weights('mobilenet_weights.h6') def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True): from tensorflow.python.framework.graph_util import convert_variables_to_constants graph = session.graph with graph.as_default(): freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or [])) output_names = output_names or [] output_names += [v.op.name for v in tf.global_variables()] input_graph_def = graph.as_graph_def() if clear_devices: for node in input_graph_def.node: node.device = "" frozen_graph = convert_variables_to_constants(session, input_graph_def, output_names, freeze_var_names) return frozen_graph output_graph_name = 'NIMA.pb' output_fld = '' #K.set_learning_phase(0) print('input is :', model.input.name) print ('output is:', model.output.name) sess = K.get_session() frozen_graph = freeze_session(K.get_session(), output_names=[model.output.op.name]) from tensorflow.python.framework import graph_io graph_io.write_graph(frozen_graph, output_fld, output_graph_name, as_text=False) print('saved the constant graph (ready for inference) at: ', os.path.join(output_fld, output_graph_name))