Analyzes the output of the Inception graph to retrieve the highest scores and their positions in the tensor, which correspond to categories.
| 256 | // Analyzes the output of the Inception graph to retrieve the highest scores and |
| 257 | // their positions in the tensor, which correspond to categories. |
| 258 | Status GetTopLabels(const std::vector<Tensor>& outputs, int how_many_labels, |
| 259 | Tensor* out_indices, Tensor* out_scores) { |
| 260 | const Tensor& unsorted_scores_tensor = outputs[0]; |
| 261 | auto unsorted_scores_flat = unsorted_scores_tensor.flat<float>(); |
| 262 | std::vector<std::pair<int, float>> scores; |
| 263 | for (int i = 0; i < unsorted_scores_flat.size(); ++i) { |
| 264 | scores.push_back(std::pair<int, float>({i, unsorted_scores_flat(i)})); |
| 265 | } |
| 266 | std::sort(scores.begin(), scores.end(), |
| 267 | [](const std::pair<int, float>& left, |
| 268 | const std::pair<int, float>& right) { |
| 269 | return left.second > right.second; |
| 270 | }); |
| 271 | scores.resize(how_many_labels); |
| 272 | Tensor sorted_indices(tensorflow::DT_INT32, {scores.size()}); |
| 273 | Tensor sorted_scores(tensorflow::DT_FLOAT, {scores.size()}); |
| 274 | for (int i = 0; i < scores.size(); ++i) { |
| 275 | sorted_indices.flat<int>()(i) = scores[i].first; |
| 276 | sorted_scores.flat<float>()(i) = scores[i].second; |
| 277 | } |
| 278 | *out_indices = sorted_indices; |
| 279 | *out_scores = sorted_scores; |
| 280 | return Status::OK(); |
| 281 | } |
| 282 | |
| 283 | // Takes a file name, and loads a list of labels from it, one per line, and |
| 284 | // returns a vector of the strings. It pads with empty strings so the length |