| 43 | } |
| 44 | |
| 45 | bool FileInputYielder::NextItem(GrapplerItem* item) { |
| 46 | if (filenames_.size() == bad_inputs_) { |
| 47 | // All the input files are bad, give up. |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | if (current_file_ >= filenames_.size()) { |
| 52 | if (current_iteration_ >= max_iterations_) { |
| 53 | return false; |
| 54 | } else { |
| 55 | ++current_iteration_; |
| 56 | current_file_ = 0; |
| 57 | bad_inputs_ = 0; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | const string& filename = filenames_[current_file_]; |
| 62 | ++current_file_; |
| 63 | |
| 64 | if (!Env::Default()->FileExists(filename).ok()) { |
| 65 | LOG(WARNING) << "Skipping non existent file " << filename; |
| 66 | // Attempt to process the next item on the list |
| 67 | bad_inputs_ += 1; |
| 68 | return NextItem(item); |
| 69 | } |
| 70 | |
| 71 | LOG(INFO) << "Loading model from " << filename; |
| 72 | |
| 73 | MetaGraphDef metagraph; |
| 74 | Status s = ReadBinaryProto(Env::Default(), filename, &metagraph); |
| 75 | if (!s.ok()) { |
| 76 | s = ReadTextProto(Env::Default(), filename, &metagraph); |
| 77 | } |
| 78 | if (!s.ok()) { |
| 79 | LOG(WARNING) << "Failed to read MetaGraphDef from " << filename << ": " |
| 80 | << s.ToString(); |
| 81 | // Attempt to process the next item on the list |
| 82 | bad_inputs_ += 1; |
| 83 | return NextItem(item); |
| 84 | } |
| 85 | |
| 86 | if (metagraph.collection_def().count("train_op") == 0 || |
| 87 | !metagraph.collection_def().at("train_op").has_node_list() || |
| 88 | metagraph.collection_def().at("train_op").node_list().value_size() == 0) { |
| 89 | LOG(ERROR) << "No train op specified"; |
| 90 | bad_inputs_ += 1; |
| 91 | metagraph = MetaGraphDef(); |
| 92 | return NextItem(item); |
| 93 | } else { |
| 94 | std::unordered_set<string> train_ops; |
| 95 | for (const string& val : |
| 96 | metagraph.collection_def().at("train_op").node_list().value()) { |
| 97 | train_ops.insert(NodeName(val)); |
| 98 | } |
| 99 | std::unordered_set<string> train_ops_found; |
| 100 | for (auto& node : metagraph.graph_def().node()) { |
| 101 | if (train_ops.find(node.name()) != train_ops.end()) { |
| 102 | train_ops_found.insert(node.name()); |