Tensorflow 2(keras)做的模型,如何制作成nb文件?

@ThinkBird 你可以试一下我这个代码

from keras.models import load_model
import tensorflow as tf
import argparse
import sys
import os 
import os.path as osp
from keras import backend as K


def h5_to_pb(h5_model,output_dir,model_name,out_prefix = "output_"):
	if osp.exists(output_dir) == False:
		os.mkdir(output_dir)
	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))
	sess = K.get_session()
	from tensorflow.python.framework import graph_util,graph_io
	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 __name__ == "__main__" :

	parser = argparse.ArgumentParser()
	parser.add_argument("--model_path", help="the h5 model path ")
	parser.add_argument("--model_output_path", help="the pb model outout path ")
	args = parser.parse_args()

	if args.model_path :
		h5_model_path = args.model_path
	else :
		sys.exit("model_path not found ! Please use this format : --model_path")

	if args.model_output_path :
		cut_out = args.model_output_path.rfind('/')
		h5_model_name = args.model_output_path[cut_out+1:]
		h5_model_dir = args.model_output_path[:cut_out]
	else:
		cut_out = h5_model_path.rfind('/')
		suffix_data = h5_model_path[cut_out+1:]
		h5_model_name =  suffix_data[:-2] + 'pb'
		h5_model_dir = './'

	h5_model = load_model(h5_model_path)
	h5_to_pb(h5_model, h5_model_dir, h5_model_name)

但是TF2做的模型不保证能转换,有一些新的接口是不支持的