Analyzes the output of the Inception graph to retrieve the highest scores and their positions in the tensor, which correspond to categories.
| 204 | // Analyzes the output of the Inception graph to retrieve the highest scores and |
| 205 | // their positions in the tensor, which correspond to categories. |
| 206 | Status GetTopLabels(const std::vector<Tensor>& outputs, int how_many_labels, |
| 207 | Tensor* indices, Tensor* scores) { |
| 208 | auto root = tensorflow::Scope::NewRootScope(); |
| 209 | using namespace ::tensorflow::ops; // NOLINT(build/namespaces) |
| 210 | |
| 211 | string output_name = "top_k"; |
| 212 | TopK(root.WithOpName(output_name), outputs[0], how_many_labels); |
| 213 | // This runs the GraphDef network definition that we've just constructed, and |
| 214 | // returns the results in the output tensors. |
| 215 | tensorflow::GraphDef graph; |
| 216 | TF_RETURN_IF_ERROR(root.ToGraphDef(&graph)); |
| 217 | |
| 218 | std::unique_ptr<tensorflow::Session> session( |
| 219 | tensorflow::NewSession(tensorflow::SessionOptions())); |
| 220 | TF_RETURN_IF_ERROR(session->Create(graph)); |
| 221 | // The TopK node returns two outputs, the scores and their original indices, |
| 222 | // so we have to append :0 and :1 to specify them both. |
| 223 | std::vector<Tensor> out_tensors; |
| 224 | TF_RETURN_IF_ERROR(session->Run({}, {output_name + ":0", output_name + ":1"}, |
| 225 | {}, &out_tensors)); |
| 226 | *scores = out_tensors[0]; |
| 227 | *indices = out_tensors[1]; |
| 228 | return Status::OK(); |
| 229 | } |
| 230 | |
| 231 | // Given the output of a model run, and the name of a file containing the labels |
| 232 | // this prints out the top five highest-scoring values. |
no test coverage detected