Given the output of a model run, and the name of a file containing the labels this prints out the top five highest-scoring values.
| 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. |
| 233 | Status PrintTopLabels(const std::vector<Tensor>& outputs, |
| 234 | const string& labels_file_name) { |
| 235 | std::vector<string> labels; |
| 236 | size_t label_count; |
| 237 | Status read_labels_status = |
| 238 | ReadLabelsFile(labels_file_name, &labels, &label_count); |
| 239 | if (!read_labels_status.ok()) { |
| 240 | LOG(ERROR) << read_labels_status; |
| 241 | return read_labels_status; |
| 242 | } |
| 243 | const int how_many_labels = std::min(5, static_cast<int>(label_count)); |
| 244 | Tensor indices; |
| 245 | Tensor scores; |
| 246 | TF_RETURN_IF_ERROR(GetTopLabels(outputs, how_many_labels, &indices, &scores)); |
| 247 | tensorflow::TTypes<float>::Flat scores_flat = scores.flat<float>(); |
| 248 | tensorflow::TTypes<int32>::Flat indices_flat = indices.flat<int32>(); |
| 249 | for (int pos = 0; pos < how_many_labels; ++pos) { |
| 250 | const int label_index = indices_flat(pos); |
| 251 | const float score = scores_flat(pos); |
| 252 | LOG(INFO) << labels[label_index] << " (" << label_index << "): " << score; |
| 253 | } |
| 254 | return Status::OK(); |
| 255 | } |
| 256 | |
| 257 | // This is a testing function that returns whether the top label index is the |
| 258 | // one that's expected. |
no test coverage detected