前期工作,把各个包安装好
- h5转pb
这里采用的是先读取模型,然后再将模型转为pb格式
def h5_to_pb(model):
# model = retinaface_model.build_model()
model.summary()
full_model = tf.function(lambda Input: model(Input))
full_model = full_model.get_concrete_function(tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype))
# Get frozen ConcreteFunction
frozen_func = convert_variables_to_constants_v2(full_model)
frozen_func.graph.as_graph_def()
layers = [op.name for op in frozen_func.graph.get_operations()]
print("-" * 50)
print("Frozen model layers: ")
for layer in layers:
print(layer)
tf.io.write_graph(graph_or_graph_def=frozen_func.graph,
logdir="./models",
name="model.pb",
as_text=False)
- pb转onnx
如果只有一个输出可以不需要那么多output,只需要附上最后的节点即可
可以通过以下函数查看输入输出节点名称
def create_graph(path):
with tf.compat.v1.gfile.FastGFile(path, 'rb') as f:
graph_def = tf.compat.v1.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')
tensor_name_list = [tensor.name for tensor in tf.compat.v1.get_default_graph().as_graph_def().node]
print([tensor.name for tensor in tf.compat.v1.get_default_graph().as_graph_def().node])
# print("input_name:" + tensor_name_list[0])
# print("output_name:" + tensor_name_list[-1])
python -m tf2onnx.convert --input ./models/model.pb --inputs Input:0 --output ./models/model.onnx --outputs Identity:0,Identity_1:0,Identity_2:0,Identity_3:0,Ident ity_4:0,Identity_5:0,Identity_6:0,Identity_7:0,Identity_8:0 --opset 11



