如何在TF2中使用TF1 x的 pb模型

2021-10-09 13:23:44 字數 1310 閱讀 9802

在tf1.x中,使用凍結的.pb模型方法比較簡單,可以參看:

隨著tf2的推進,以後,用tf2的也會越來越多。但是,有時候還是會需要在tf2中使用到tf1.x的模型。對於.pb模型的使用,該如何做呢?直接上code

def

wrap_frozen_graph

(graph_def, inputs, outputs, print_graph=

false):

def_imports_graph_def()

: tf.compat.v1.import_graph_def(graph_def, name="")

) tf.nest.map_structure(import_graph.as_graph_element, inputs)

, tf.nest.map_structure(import_graph.as_graph_element, outputs)

)def

load_pb()

:# load frozen graph using tensorflow 1.x functions

with tf.io.gfile.gfile(

"model_path.pb"

,"rb"

)as f:

graph_def = tf.compat.v1.graphdef(

) loaded = graph_def.parsefromstring(f.read())

# wrap frozen graph to concretefunctions

frozen_func = wrap_frozen_graph(

graph_def=graph_def,

inputs=

"input:0"

,# input tensor name of your model

outputs=

"output:0"

,# output tensor name of your model

print_graph=

true

)return frozen_func

model = load_pb(

)input_var = tf.constant(your_var, dtype=tf.float32)

# does not support tf.variable here

output_var = model(input_var)

目前,根據個人的嘗試,在tf2中載入的.pb模型,只能由於推理,不能用於求梯度。

參考:

無監督學習與TF2

閱讀antonio gulli deep learning with tensorflow 2 and keras second edition 第十章 unsupervised learning筆記 使用tf2能實現哪些無無監督的演算法呢?可以使用tf2來實現pca kmeans 之前我們主要使用...

ROS學習之tf 2 編寫乙個TF廣播器(C )

ros.tf2.編寫乙個 tf廣播器 c 宣告 本教程來自於ros.wiki,本人在整理過程中可能出現一些錯誤和問題,有問題可以去檢視官網版本也可以諮詢本人 1.1在接下來的兩篇教程中,我們將編寫 來重現 tf入門教程中的演示。之後,以下教程將重點介紹使用更高階的 tf功能擴充套件演示。在我們開始之...

tf2報錯cudnn初始化錯誤

報錯如下 failed to get convolution algorithm.this is probably because cudnn failed to initialize首先檢查自己的cudnn是否安裝成功,在一切安裝設定都正確的情況下,就是視訊記憶體不足導致的。原因 tf2.0對rt...