c++ - TensorFlow图像识别输出格式c++

标签 c++ tensorflow

我使用 tensorflow 进行图像识别,来自 main.cc , 使用命令:

bazel-bin/tensorflow/examples/label_image/label_image

终端当前输出格式为:

2017-04-19: I tensorflow/.../main.cc:206] military uniform (653): 0.834307

2017-04-19: I tensorflow/.../main.cc:206] mortarboard (668): 0.0218693

2017-04-19: I tensorflow/.../main.cc:206] academic gown (401): 0.010358

2017-04-19: I tensorflow/.../main.cc:206] pickelhaube (716): 0.00800809

2017-04-19: I tensorflow/.../main.cc:206] bulletproof vest (466): 0.00535086

输出函数为:

// this prints out the top five highest-scoring values.
Status PrintTopLabels(const std::vector<Tensor>& outputs,
                      string labels_file_name) {
  std::vector<string> labels;
  size_t label_count;
  Status read_labels_status =
      ReadLabelsFile(labels_file_name, &labels, &label_count);
  if (!read_labels_status.ok()) {
    LOG(ERROR) << read_labels_status;
    return read_labels_status;
  }
  const int how_many_labels = std::min(5, static_cast<int>(label_count));
  Tensor indices;
  Tensor scores;
  TF_RETURN_IF_ERROR(GetTopLabels(outputs, how_many_labels, &indices, &scores));
  tensorflow::TTypes<float>::Flat scores_flat = scores.flat<float>();
  tensorflow::TTypes<int32>::Flat indices_flat = indices.flat<int32>();
  for (int pos = 0; pos < how_many_labels; ++pos) {
    const int label_index = indices_flat(pos);
    const float score = scores_flat(pos);
    LOG(INFO) << labels[label_index] << " (" << label_index << "): " << score;
  }
  return Status::OK();
}

问题是,我希望输出是这样的列表:

military uniform, mortarboard, academic gown, pickelhaube, bulletproof vest

有可能有这样的输出吗?

最佳答案

是的,我认为这是可能的。替换:

LOG(INFO) << labels[label_index] << " (" << label_index << "): " << score;

std::cout << label[label_index] << ",";

好的,现在多了一个逗号,超出了我们的需要。所以我们之前检查它是否是最后一个概念,如果是,我们就把逗号去掉。

希望对您有所帮助。

关于c++ - TensorFlow图像识别输出格式c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43498491/

相关文章:

python - ModuleNotFoundError: 没有名为 'tensorflow_hub' 的模块

tensorflow - KERAS:如何使用与权重所需形状相同的张量显式设置 Conv2D 层的权重?

c++ - 使用运算符重载 c++ 进行监控

c++ - Win32 : How to hide 3rd party windows in taskbar by hWnd

c++ - OpenCV模板匹配参数

python - TensorFlow - 如果存在则恢复

tensorflow - 在 TensorFlow 中微调 Inception 模型

python - Keras LSTM - 使用生成器中的 Tensorflow 数据集 API 提供序列数据

c++ - Linux 的注册表等效项是什么?

c++ - 未解析的外部符号 __imp__glewInit VS __imp__glewInit@0