ComputeLibrary icon indicating copy to clipboard operation
ComputeLibrary copied to clipboard

How to copy an image ptr to a tensor?

Open jay2002 opened this issue 9 years ago • 13 comments

e.g., copy opencv's mat ptr to a tensor (image)

jay2002 avatar Apr 06 '17 09:04 jay2002

Untested and written from memory but something like this should do some basic copying:

Tensor tensor;
Mat mat;

Window window;
window.use_tensor_dimensions(tensor->info());

Iterator out(&tensor, window)

execute_window_loop(window, [&](const Coordinates &id)
{
    *reinterpret_cast<UNDERLYING_DATA_TYPE*>(out.ptr()) =
        mat.ptr<UNDERLYING_DATA_TYPE>(id.y())[id.x()];
}, out);

You need to replace the UNDERLYING_DATA_TYPE with the actual type of the tensor, e.g. int16_t. This also assumes that the tensor and the matrix really have the same size. If not it will (silently) fail. The code could further be optimised by copying larger blocks of memory instead of one element at a time.

mpflanzer avatar Apr 06 '17 16:04 mpflanzer

@AnthonyARM Hi, I've been redirected here by issue #8 . I've successfully set my Tensor using the above snippet. However, when I was trying to run CLConvolutionLayer later, I failed to apply this snippet to CLTensor. This is my code:

CLTensor tensor;

...

Window window;
window.use_tensor_dimensions(tensor.info());
Iterator it(&tensor, window);
execute_window_loop(window, [&](const Coordinates &id){
    // test code, this works fine for Tensor not CLTensor
    *reinterpret_cast<float *>(iIt.ptr()) = 1.0;
}, it);

Could anyone tell me what's wrong with this code? It crashed after launching.

yangyanzhan avatar Apr 12 '17 10:04 yangyanzhan

Hello @yangyanzhan,

Can you please map the tensor data to the host address space using tensor->map() before (otherwise, the iterator will try to dereference a nullptr) and then call tensor->unmap() after the initialization so that the GPU can use it.

tensor->map();
Window window;
window.use_tensor_dimensions(tensor.info());
Iterator it(&tensor, window);
execute_window_loop(window, [&](const Coordinates &id){
    *reinterpret_cast<float *>(iIt.ptr()) = 1.0; 
}, it);
tensor->unmap();

GeorgeARM avatar Apr 12 '17 10:04 GeorgeARM

@GeorgeARM Thank you very much. I've successfully set values for CLTensor. However, the result of NEConvolutionLayer and CLConvolutionLayer is different. And I know the result of NEConvolutionLayer is correct since I've compared it with the result of Caffe's convolution layer. Is there something wrong? The allocation and assignment procedure is the same between these two layers.

yangyanzhan avatar Apr 12 '17 12:04 yangyanzhan

@yangyanzhan Can you please provide more information on the ConvolutionLayer that you are trying to run? In more detail, what are the shapes of the tensors that you are passing and what are the strides, padding etc?

GeorgeARM avatar Apr 12 '17 12:04 GeorgeARM

@GeorgeARM Hi, George. These are the shapes of the four arguments for CLConvolution's configure function:

TensorShape iShape(224, 224, 3);
TensorShape wShape(7, 7, 3, 64);
TensorShape bShape(64);
TensorShape oShape(112, 112, 64);

And the stride and padding info is as follows:

PadStrideInfo info(2, 2, 3, 3);

I set CLTensor exactly like Tensor except changing some type from NE to CL and add some map/unmap invocations.

And I add "CLScheduler::get().sync();" after invoking CLConvolutionLayer's run function. And I set up OpenCL environment like this:

    std::vector<cl::Platform> platforms;
    cl::Platform::get(&platforms);
    cl::Platform platform = platforms[0];
    std::vector<cl::Device> devices;
    platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
    cl::Device device = devices[0];
    cl::Context context({device});
    cl::CommandQueue queue(context);
    CLKernelLibrary::get().init("./cl_kernels/", context, device);
    CLScheduler::get().init(context, queue);

yangyanzhan avatar Apr 12 '17 12:04 yangyanzhan

@GeorgeARM Hi, George. I were experimenting my code on Xiaomi MI 5 android device with the above snippets and this device is armed with Adreno GPU. I think that's the source of my problem since ACL is optimized targeting Mali GPU. I'll stick to ACL NEON backend in this case.

yangyanzhan avatar Apr 13 '17 03:04 yangyanzhan

@yangyanzhan The OpenCL kernels are optimized for Mali but I would expect them to run on any GPU. I ran a quick internal test and it does not seem to fail but still haven't checked all the aspects of it. Is it a small difference what you see or completely difference results?

GeorgeARM avatar Apr 13 '17 08:04 GeorgeARM

@GeorgeARM Thank you, George. They are completely different results. I'm afraid to waste your time. So could you please checkout it out if there are some errors with my code? I want to eliminate my own errors before checking if ACL has any bugs so that I won't lead you to a false direction. :) And my Xiaomi MI 5 is equipped with Adreno (TM) 530.

yangyanzhan avatar Apr 13 '17 12:04 yangyanzhan

@yangyanzhan don't worry we have to look at it. Can you please post you full test case code here? Skip the parts where you read values from a file (if you such a thing).

GeorgeARM avatar Apr 13 '17 13:04 GeorgeARM

@GeorgeARM Hi, George, here's my full code:

void setup_opencl() {
    std::vector<cl::Platform> platforms;
    cl::Platform::get(&platforms);
    cl::Platform platform = platforms[0];
    std::vector<cl::Device> devices;
    platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
    cl::Device device = devices[0];
    cl::Context context({device});
    cl::CommandQueue queue(context);
    CLKernelLibrary::get().init("./cl_kernels/", context, device);
    CLScheduler::get().init(context, queue);
}

...

// Here's my code in my forward function:

setup_opencl();

PadStrideInfo info(2, 2, 3, 3);

TensorShape iShape(224, 224, 3);
TensorShape wShape(7, 7, 3, 64);
TensorShape bShape(64);
TensorShape oShape(112, 112, 64);

CLTensor iTensor, wTensor, bTensor, oTensor;

iTensor.allocator()->init(TensorInfo(iShape, Format::F32));
wTensor.allocator()->init(TensorInfo(wShape, Format::F32));
bTensor.allocator()->init(TensorInfo(bShape, Format::F32));
oTensor.allocator()->init(TensorInfo(oShape, Format::F32));

iTensor.allocator()->allocate();
wTensor.allocator()->allocate();
bTensor.allocator()->allocate();
oTensor.allocator()->allocate();

CLConvolutionLayer layer;
layer.configure(&iTensor, &wTensor, &bTensor, &oTensor, info);

iTensor.map();
wTensor.map();
bTensor.map();

Window iWindow;
iWindow.use_tensor_dimensions(iTensor.info());
Window wWindow;
wWindow.use_tensor_dimensions(wTensor.info());
Window bWindow;
bWindow.use_tensor_dimensions(bTensor.info());
Iterator iIt(&iTensor, iWindow);
Iterator wIt(&wTensor, wWindow);
Iterator bIt(&bTensor, bWindow);

execute_window_loop(iWindow, [&](const Coordinates &id){
    ... read in caffe data here
}, iIt);
execute_window_loop(wWindow, [&](const Coordinates &id){
    ... read in caffe data here
}, wIt);
execute_window_loop(bWindow, [&](const Coordinates &id){
    ... read in caffe data here
}, bIt);

iTensor.unmap();
wTensor.unmap();
bTensor.unmap();

layer.run();
CLScheduler::get().sync();

oTensor.map();
Window oWindow;
oWindow.use_tensor_dimensions(oTensor.info());
Iterator oIt(&oTensor, oWindow);
execute_window_loop(oWindow, [&](const Coordinates &id){
    ... write to caffe data here
}, oIt);
oTensor.unmap();

yangyanzhan avatar Apr 13 '17 13:04 yangyanzhan

Hi @yangyanzhan,

many thanks for sharing the code. Unfortunately we are not able to reproduce the issue :( . We have tried to use the same tensor shapes but both NEON and OpenCL return the same result.

Could you try to re-run your test using the latest release of the library please?

Thanks, Gian Marco

gmiodice avatar Apr 27 '17 15:04 gmiodice

Please re-open if the issue is still present in the latest release

AnthonyBarbier avatar May 04 '17 13:05 AnthonyBarbier