|
| 1 | +from deepmd.env import tf |
| 2 | +import re |
| 3 | +import numpy as np |
| 4 | +def transform(args): |
| 5 | + raw_graph = load_graph(args.raw_model) |
| 6 | + old_graph = load_graph(args.old_model) |
| 7 | + print("%d ops in the raw graph\n%d ops in the old graph" %(len(raw_graph.as_graph_def().node),len(old_graph.as_graph_def().node))) |
| 8 | + new_graph_def = transform_graph(raw_graph,old_graph) |
| 9 | + with tf.gfile.GFile(args.output, mode='wb') as f: |
| 10 | + f.write(new_graph_def.SerializeToString()) |
| 11 | + print("the output model is saved in %s" % args.output) |
| 12 | + |
| 13 | +def load_graph(graphName): |
| 14 | + graph_def = tf.GraphDef() |
| 15 | + with open(graphName,"rb") as f: |
| 16 | + graph_def.ParseFromString(f.read()) |
| 17 | + with tf.Graph().as_default() as graph: |
| 18 | + tf.import_graph_def(graph_def,name = "") |
| 19 | + return graph |
| 20 | + |
| 21 | +def transform_graph(raw_graph,old_graph): |
| 22 | + precision_dict = {\ |
| 23 | + 1:(np.float32, "float32"),\ |
| 24 | + 2:(np.float64, "float64"),\ |
| 25 | + 19:(np.float16, "float16")\ |
| 26 | + } |
| 27 | + old_graph_def = old_graph.as_graph_def() |
| 28 | + raw_graph_def = raw_graph.as_graph_def() |
| 29 | + raw_graph_node = load_transform_node(raw_graph_def) |
| 30 | + old_graph_node = load_transform_node(old_graph_def) |
| 31 | + |
| 32 | + if len(raw_graph_node) != len(old_graph_node): |
| 33 | + raise RuntimeError("raw graph and old graph has different network structure") |
| 34 | + |
| 35 | + for node in raw_graph_def.node: |
| 36 | + if node.name in raw_graph_node.keys(): |
| 37 | + if precision_dict[old_graph_node[node.name].dtype][1] == "float16" or precision_dict[raw_graph_node[node.name].dtype][1] == "float16": |
| 38 | + raise RuntimeError("float16 conversions not currently supported") |
| 39 | + |
| 40 | + check_dim(raw_graph_node, old_graph_node, node.name) |
| 41 | + |
| 42 | + if re.fullmatch("final_layer_type_\d+/bias",node.name) == None: |
| 43 | + tensor_value = np.frombuffer(old_graph_node[node.name].tensor_content,dtype = precision_dict[old_graph_node[node.name].dtype][0]) |
| 44 | + tensor_value = tensor_value.astype(dtype=precision_dict[raw_graph_node[node.name].dtype][0]) |
| 45 | + node.attr["value"].tensor.tensor_content = tensor_value.tostring() |
| 46 | + |
| 47 | + else: |
| 48 | + if precision_dict[old_graph_node[node.name].dtype][1] == "float64": |
| 49 | + tensor_value = (np.array(old_graph_node[node.name].double_val)).astype(precision_dict[raw_graph_node[node.name].dtype][0]) |
| 50 | + node.attr["value"].CopyFrom(tf.AttrValue(tensor=tf.make_tensor_proto(tensor_value,precision_dict[raw_graph_node[node.name].dtype][0], [1]))) |
| 51 | + |
| 52 | + elif precision_dict[old_graph_node[node.name].dtype][1] == "float32": |
| 53 | + tensor_value = (np.array(old_graph_node[node.name].float_val)).astype(precision_dict[raw_graph_node[node.name].dtype][0]) |
| 54 | + node.attr["value"].CopyFrom(tf.AttrValue(tensor=tf.make_tensor_proto(tensor_value, precision_dict[raw_graph_node[node.name].dtype][0], [1]))) |
| 55 | + |
| 56 | + elif precision_dict[old_graph_node[node.name].dtype][1] == "float16": |
| 57 | + tensor_value = (np.array(old_graph_node[node.name].half_val)).astype(precision_dict[raw_graph_node[node.name].dtype][0]) |
| 58 | + node.attr["value"].CopyFrom(tf.AttrValue(tensor=tf.make_tensor_proto(tensor_value, precision_dict[raw_graph_node[node.name].dtype][0], [1]))) |
| 59 | + |
| 60 | + print("%s is passed from old graph(%s) to raw graph(%s)" % (node.name,precision_dict[old_graph_node[node.name].dtype][1],precision_dict[raw_graph_node[node.name].dtype][1])) |
| 61 | + |
| 62 | + return raw_graph_def |
| 63 | + |
| 64 | +def check_dim(raw_graph_node, old_graph_node, node_name): |
| 65 | + raw_graph_dim = raw_graph_node[node_name].tensor_shape |
| 66 | + old_graph_dim = old_graph_node[node_name].tensor_shape |
| 67 | + if raw_graph_dim != old_graph_dim: |
| 68 | + raise RuntimeError("old graph and raw graph has different"+node_name+" dim") |
| 69 | + |
| 70 | + |
| 71 | +def load_transform_node(graph): |
| 72 | + transform_node = {} |
| 73 | + transform_node_pattern = "\ |
| 74 | +filter_type_\d+/matrix_\d+_\d+|\ |
| 75 | +filter_type_\d+/bias_\d+_\d+|\ |
| 76 | +filter_type_\d+/idt_\d+_\d+|\ |
| 77 | +layer_\d+_type_\d+/matrix|\ |
| 78 | +layer_\d+_type_\d+/bias|\ |
| 79 | +layer_\d+_type_\d+/idt|\ |
| 80 | +final_layer_type_\d+/bias|\ |
| 81 | +final_layer_type_\d+/matrix\ |
| 82 | +" |
| 83 | + for node in graph.node: |
| 84 | + if re.fullmatch(transform_node_pattern,node.name) != None: |
| 85 | + transform_node[node.name] = node.attr["value"].tensor |
| 86 | + return transform_node |
0 commit comments