【问题标题】:Example code for populating a tensor?填充张量的示例代码?
【发布时间】:2016-11-10 03:37:04
【问题描述】:

我不知道如何填充张量。 API 没有很好地记录...

我想要一个类似这样的函数。

std::tuple<std::string, double> CNNLocalizer::runImage()
{
  std::tuple<std::string, double> result;
  result.get<0> = "none";
  result.get<1> = 0.0;

  if (g_got_image_)
  {
    std::string label;

    // create a tensorflow::Tensor with the image information
    tensorflow::TensorShape image_shape;
    image_shape.AddDim(g_img_height_);
    image_shape.AddDim(g_img_width_);

    tensorflow::Tensor input_image(tensorflow::DT_INT8, image_shape);
    // I have no idea how to make this work right now.  Copying data is very confusing..
    for (uint i = 0; i < g_img_height_; i++)
    {
      for (uint j = 0; j < g_img_width_; j++)
      {
        // ??  Populate a matrix or something?
      }
    }
    // Copy the matrix into the tensor?
    // input_image.matrix<float>()() = z;
  }

  return result;
}

知道如何填充张量的矩阵部分吗?我找到了一个 matrix() 函数,它返回某种类型定义的 Eigen Tensor 对象 - 这是要走的路吗?

任何帮助将不胜感激!谢谢!

【问题讨论】:

    标签: c++ tensorflow


    【解决方案1】:

    您可以使用Tensor::FromProto。否则,您最好使用Tensor::flat&lt;int8&gt;。有CSVPNG 的示例。

    【讨论】:

    • 您能提供更多信息吗?作为参考,我有一个来自 JPG 的 cv::Mat ,我想将其放入张量中以遍历图表。我将自己挖掘一下,但如果您有参考代码或任何更多链接,我将不胜感激!
    • 如果你想不知道数据表示,如果它只是一个二维矩阵,只需使用上面的auto eigen_matrix = input_image.matrix&lt;float&gt;();,然后使用eigen_matrix(i, j) = cv_mat[i][j]。通常,在运算符中,您应该使用op_kernel_context-&gt;allocate_output() 分配输出。
    猜你喜欢
    • 1970-01-01
    • 2020-07-01
    • 2018-03-24
    • 2022-08-19
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多