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.
| 263 | // Given the output of a model run, and the name of a file containing the labels |
| 264 | // this prints out the top five highest-scoring values. |
| 265 | Status PrintTopDetections(const std::vector<Tensor>& outputs, |
| 266 | const string& labels_file_name, |
| 267 | const int num_boxes, |
| 268 | const int num_detections, |
| 269 | const string& image_file_name, |
| 270 | Tensor* original_tensor) { |
| 271 | std::vector<float> locations; |
| 272 | size_t label_count; |
| 273 | Status read_labels_status = |
| 274 | ReadLocationsFile(labels_file_name, &locations, &label_count); |
| 275 | if (!read_labels_status.ok()) { |
| 276 | LOG(ERROR) << read_labels_status; |
| 277 | return read_labels_status; |
| 278 | } |
| 279 | CHECK_EQ(label_count, num_boxes * 8); |
| 280 | |
| 281 | const int how_many_labels = |
| 282 | std::min(num_detections, static_cast<int>(label_count)); |
| 283 | Tensor indices; |
| 284 | Tensor scores; |
| 285 | TF_RETURN_IF_ERROR( |
| 286 | GetTopDetections(outputs, how_many_labels, &indices, &scores)); |
| 287 | |
| 288 | tensorflow::TTypes<float>::Flat scores_flat = scores.flat<float>(); |
| 289 | |
| 290 | tensorflow::TTypes<int32>::Flat indices_flat = indices.flat<int32>(); |
| 291 | |
| 292 | const Tensor& encoded_locations = outputs[1]; |
| 293 | auto locations_encoded = encoded_locations.flat<float>(); |
| 294 | |
| 295 | LOG(INFO) << original_tensor->DebugString(); |
| 296 | const int image_width = original_tensor->shape().dim_size(1); |
| 297 | const int image_height = original_tensor->shape().dim_size(0); |
| 298 | |
| 299 | tensorflow::TTypes<uint8>::Flat image_flat = original_tensor->flat<uint8>(); |
| 300 | |
| 301 | LOG(INFO) << "===== Top " << how_many_labels << " Detections ======"; |
| 302 | for (int pos = 0; pos < how_many_labels; ++pos) { |
| 303 | const int label_index = indices_flat(pos); |
| 304 | const float score = scores_flat(pos); |
| 305 | |
| 306 | float decoded_location[4]; |
| 307 | DecodeLocation(&locations_encoded(label_index * 4), |
| 308 | &locations[label_index * 8], decoded_location); |
| 309 | |
| 310 | float left = decoded_location[0] * image_width; |
| 311 | float top = decoded_location[1] * image_height; |
| 312 | float right = decoded_location[2] * image_width; |
| 313 | float bottom = decoded_location[3] * image_height; |
| 314 | |
| 315 | LOG(INFO) << "Detection " << pos << ": " |
| 316 | << "L:" << left << " " |
| 317 | << "T:" << top << " " |
| 318 | << "R:" << right << " " |
| 319 | << "B:" << bottom << " " |
| 320 | << "(" << label_index << ") score: " << DecodeScore(score); |
| 321 | |
| 322 | DrawBox(image_width, image_height, left, top, right, bottom, &image_flat); |
no test coverage detected