我正在尝试从caffe库中的blob数据创建一个xtensor数组。在caffe中,使用函数mutable_cpu_data()返回指向数据的指针,例如通过float* data = output->mutable_cpu_data();返回。使用xtensor可以做到这一点吗?如果是,你能提供一个例子吗?我已经找到了使用OpenCV Mat的例子,但是xtensor很像numpy,这使得像数据一样的矩阵操作变得容易得多。
发布于 2018-10-17 15:57:22
您可以使用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-11 01:36:11
你可以这样做,但你需要你的数据的大小...
尝试一下,如下所述。
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);https://stackoverflow.com/questions/52740327
复制相似问题