4

我正在尝试xtensor从库中的 blob 数据创建一个数组caffe。使用例如 bymutable_cpu_data()中的函数返回指向数据的指针。这可能吗?如果是,请您举个例子。我发现了一些使用 OpenCV 的例子,但它们很相似,这使得像数据这样的矩阵上的操作变得更加容易。caffefloat* data = output->mutable_cpu_data();xtensorMatxtensornumpy

4

2 回答 2

2

您可以使用 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);

与 Naidu 提供的解决方案相比,优势在于您不复制数据缓冲区,它是“就地”改编的。

于 2018-10-17T07:57:22.090 回答
0

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

尝试如下所述。

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);
于 2018-10-10T17:36:10.890 回答