MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / PrintTopLabels

Function PrintTopLabels

tensorflow/examples/label_image/main.cc:233–255  ·  view source on GitHub ↗

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.

Source from the content-addressed store, hash-verified

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.
233Status 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.

Callers 1

mainFunction · 0.70

Calls 4

ReadLabelsFileFunction · 0.70
GetTopLabelsFunction · 0.70
minFunction · 0.50
okMethod · 0.45

Tested by

no test coverage detected