第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 keras/tensorflow h5/json 轉(zhuǎn)換為 tensorflow pb 時遇到問題

將 keras/tensorflow h5/json 轉(zhuǎn)換為 tensorflow pb 時遇到問題

紅顏莎娜 2023-09-05 20:32:08
我使用 keras(張量流后端)訓(xùn)練了一個網(wǎng)絡(luò),并將模型保存為 json,權(quán)重保存為 h5。我現(xiàn)在嘗試將其轉(zhuǎn)換為單個張量流 pb 文件,它抱怨輸出節(jié)點(diǎn)的名稱。系統(tǒng)信息:Tensorflow 2.3.0 Keras 2.4.3 Cuda 10.1 Cudnn 7轉(zhuǎn)換腳本非常簡單:import jsonfrom tensorflow import kerasfrom keras import backend as Kimport tensorflow as tfjson_file = "my-trained-model.json"h5_file = "my-trained-model.h5"Output_Path = "./trained_models/"Frozen_pb_File = "my-trained-model.pb"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.compat.v1.global_variables()).difference(keep_var_names or []))        output_names = output_names or []        output_names += [v.op.name for v in tf.compat.v1.global_variables()]        # Graph -> GraphDef ProtoBuf        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_graphwith open(json_file, 'r') as json_file:    model = keras.models.model_from_json(json_file.read())model.load_weights(h5_file)model.summary()# get output node namesOutputNames = [out.op.name for out in model.outputs]print("\nOutput Names:\n", OutputNames)  # this prints "concatenate/concat" as the only output node name# freeze the modelfrozen_graph = freeze_session(tf.compat.v1.keras.backend.get_session(), output_names=OutputNames)# save the output files# this is the .pb file (a binary file)tf.io.write_graph(frozen_graph, Output_Path, Frozen_pb_File, as_text=False)當(dāng)我運(yùn)行這個時,AssertionError: concatenate/concat is not in graph因此,由于某種原因,它正在讀取“concatenate/concat”的輸出節(jié)點(diǎn)名稱。下面給出模型總結(jié),可以看到輸出節(jié)點(diǎn)是“concatenate”;但是,即使我將輸出節(jié)點(diǎn)名稱硬編碼為“連接”,我也會收到類似的斷言錯誤:AssertionError: concatenate is not in graph
查看完整描述

1 回答

?
繁華開滿天機(jī)

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個贊

看起來這都是由于嘗試凍結(jié) TensorFlow 2.3 模型而引起的。顯然,Tensorflow 2.0+ 已棄用“凍結(jié)”概念,轉(zhuǎn)而采用“保存模型”概念。一旦發(fā)現(xiàn)這一點(diǎn),我就能夠立即將 h5/json 保存到已保存的模型 pb 中。


我仍然不確定這種格式是否針對推理進(jìn)行了優(yōu)化,所以我將對此進(jìn)行一些跟進(jìn),但由于我的問題是關(guān)于我看到的錯誤,我想我會發(fā)布導(dǎo)致問題的原因。


作為參考,這是我的 python 腳本,用于將 keras h5/json 文件轉(zhuǎn)換為 Tensorflow 保存的模型格式。


import os

from keras.models import model_from_json

import tensorflow as tf

import genericpath

from genericpath import *


def splitext(p):

    p = os.fspath(p)

    if isinstance(p, bytes):

        sep = b'/'

        extsep = b'.'

    else:

        sep = '/'

        extsep = '.'

    return genericpath._splitext(p, sep, None, extsep)


def load_model(path,custom_objects={},verbose=0):

    from keras.models import model_from_json


    path = splitext(path)[0]

    with open('%s.json' % path,'r') as json_file:

        model_json = json_file.read()

    model = model_from_json(model_json, custom_objects=custom_objects)

    model.load_weights('%s.h5' % path)

    # if verbose: print 'Loaded from %s' % path

    return model



json_file = "model.json"  # the h5 file should be "model.h5"


model = load_model(json_file) # load the json/h5 pair

model.save('my_saved_model') # this is a directory name to store the saved model


查看完整回答
反對 回復(fù) 2023-09-05
  • 1 回答
  • 0 關(guān)注
  • 237 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號