MCPcopy Create free account
hub / github.com/OAID/Tengine / PrintTopLabels

Function PrintTopLabels

examples/tensorflow_wrapper/label_image/label_image.cpp:139–167  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

137}
138
139static int PrintTopLabels(const vector<TF_Tensor*>& outputs, const char* labels_file)
140{
141 vector<string> labels;
142 int read_labels_status = ReadLabelsFile(labels_file, &labels);
143 if(read_labels_status < 0)
144 return -1;
145
146 int label_count = labels.size();
147 int N = std::min<int>(label_count, 5);
148
149 float* data = ( float* )TF_TensorData(outputs[0]);
150
151 vector<pair<int, float>> scores;
152 for(int i = 0; i < label_count; i++)
153 {
154 scores.push_back(pair<int, float>({i, data[i]}));
155 }
156
157 sort(scores.begin(), scores.end(),
158 [](const pair<int, float>& left, const pair<int, float>& right) { return left.second > right.second; });
159
160 for(int pos = 0; pos < N; pos++)
161 {
162 const int label_index = scores[pos].first;
163 const float score = scores[pos].second;
164 cout << std::fixed << std::setprecision(4) << score << " - \"" << labels[label_index] << "\"" << endl;
165 }
166 return 0;
167}
168
169/* To make tensor release happy...*/
170static void dummy_deallocator(void* data, size_t len, void* arg) {}

Callers 1

mainFunction · 0.70

Calls 4

ReadLabelsFileFunction · 0.85
beginMethod · 0.80
endMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected