5

I am trying to create a xtensor array from a blob data in caffe library. A pointer to data is returned using the function mutable_cpu_data() in caffe for example by float* data = output->mutable_cpu_data();. Is this possible with xtensor? If yes, could you please provide an example. I've found examples which use OpenCV Mat but xtensor is a lot like numpy which makes the operating on the matrix like data much easier.

max
  • 2,627
  • 1
  • 24
  • 44

2 Answers2

2

You can use the xt::adapt function from xadapt.hpp, but you need to provide a shape:

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);

The advantage compared to the solution provided by Naidu is that you don't copy the data buffer, it is adapted "in place".

johan mabille
  • 414
  • 2
  • 4
0

You can do that, but you need the size of your data....

Try as said below.

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);
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34