【问题标题】:how to fill xtensor array with data using a pointer如何使用指针用数据填充 xtensor 数组
【发布时间】:2018-10-10 12:29:51
【问题描述】:

我正在尝试从 caffe 库中的 blob 数据创建一个 xtensor 数组。使用caffe 中的函数mutable_cpu_data() 返回指向数据的指针,例如float* data = output->mutable_cpu_data();xtensor 可以做到这一点吗?如果是,请您举个例子。我找到了使用 OpenCV Mat 的示例,但 xtensor 很像 numpy,这使得对矩阵等数据的操作更加容易。

【问题讨论】:

    标签: c++ caffe xtensor


    【解决方案1】:

    您可以使用 xadapt.hpp 中的 xt::adapt 函数,但您需要提供一个形状:

    float* data = output->mutable_cpu_data();
    size_t size = size_of_data;
    // For a 1D tensor for instance
    xt::static_shape<std::size_t, 1> sh = { size_of_data};
    // Parameters of adapt are:
    // - the 1D buffer to adapt
    // - the size of the buffer
    // - the ownership flag (should the adaptor destroy your buffer upon deletion, here
    //   probably not)
    // - the shape
    auto a = xt::adapt(data, size_of_data, false sh);
    

    与耐度提供的方案相比,优势在于不复制数据缓冲区,它是“就地”适配的。

    【讨论】:

      【解决方案2】:

      您可以这样做,但您需要数据的大小......

      尝试如下所述。

      float* data = output->mutable_cpu_data();
      
      //CONVERT YOUR DATA TO FLOAT VECTOR
      //I assume the size of your array could be 4.
      //Replace 4 with intended size of array.
      std::vector<float> fData(data, data+4); 
      
      //INITIALIZE XARRAY
      xt::xarray<float> a(fData);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-03
        • 1970-01-01
        • 1970-01-01
        • 2017-11-07
        • 1970-01-01
        • 2016-08-06
        • 1970-01-01
        相关资源
        最近更新 更多