Renames all nodes not uses as graph inputs or outputs to short numerical forms.
| 29 | // Renames all nodes not uses as graph inputs or outputs to short numerical |
| 30 | // forms. |
| 31 | Status ObfuscateNames(const GraphDef& input_graph_def, |
| 32 | const TransformFuncContext& context, |
| 33 | GraphDef* output_graph_def) { |
| 34 | std::unordered_set<string> required_nodes; |
| 35 | for (const string& input : context.input_names) { |
| 36 | required_nodes.insert(input); |
| 37 | } |
| 38 | for (const string& output : context.output_names) { |
| 39 | required_nodes.insert(output); |
| 40 | } |
| 41 | |
| 42 | const string valid_chars = |
| 43 | "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 44 | const int64 chars_size = valid_chars.size(); |
| 45 | |
| 46 | std::map<string, string> new_names; |
| 47 | int64 name_index = 0; |
| 48 | for (const NodeDef& input_node : input_graph_def.node()) { |
| 49 | const string& old_name = input_node.name(); |
| 50 | string new_name; |
| 51 | if (required_nodes.count(old_name)) { |
| 52 | new_name = old_name; |
| 53 | } else { |
| 54 | do { |
| 55 | int64 remaining = name_index; |
| 56 | new_name = ""; |
| 57 | while (true) { |
| 58 | const int64 remainder = (remaining % chars_size); |
| 59 | const char current_char = valid_chars[remainder]; |
| 60 | new_name = current_char + new_name; |
| 61 | remaining /= chars_size; |
| 62 | if (remaining <= 0) { |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | ++name_index; |
| 67 | } while (required_nodes.count(new_name)); |
| 68 | } |
| 69 | new_names[old_name] = new_name; |
| 70 | } |
| 71 | |
| 72 | output_graph_def->Clear(); |
| 73 | for (const NodeDef& input_node : input_graph_def.node()) { |
| 74 | NodeDef* node = output_graph_def->mutable_node()->Add(); |
| 75 | *node = input_node; |
| 76 | const string& old_name = input_node.name(); |
| 77 | node->set_name(new_names[old_name]); |
| 78 | node->mutable_input()->Clear(); |
| 79 | for (const string& input_name : input_node.input()) { |
| 80 | string prefix; |
| 81 | string input_node_name; |
| 82 | string suffix; |
| 83 | NodeNamePartsFromInput(input_name, &prefix, &input_node_name, &suffix); |
| 84 | if (new_names.count(input_node_name) == 0) { |
| 85 | return errors::InvalidArgument("No node named ", input_node_name, |
| 86 | " for input to ", old_name); |
| 87 | } |
| 88 | string new_input_name = prefix + new_names[input_node_name] + suffix; |