google-coral / pycoral

Python API for ML inferencing and transfer-learning on Coral devices
https://coral.ai
Apache License 2.0
351 stars 145 forks source link

Unable to cast Python instance to C++ type #38

Open mzignis opened 3 years ago

mzignis commented 3 years ago

Hi, I'm trying to check dma-buffer and use it to run my model (on google coral dev board), but when I try call a few functions from pycoral.utils.edgetpu, pybind return error:

Traceback (most recent call last): File "", line 1, in RuntimeError: Unable to cast Python instance to C++ type (compile in debug mode for details)

My code: import pycoral.utils.edgetpu as edgetpu interpreter = edgetpu.make_interpreter('model_edgetpu.tflite') edgetpu.supports_dmabuf(interpreter)

I've try to use all pycoral versions (1.0.0, 1.0.1 and 2.0.0) but in each of it there is the same issue

hjonnala commented 3 years ago

Hi, can you please try the code snippet below

import pycoral.utils.edgetpu as edgetpu
interpreter = edgetpu.make_interpreter('model_edgetpu.tflite')
edgetpu.supports_dmabuf(interpreter._native_handle())
mzignis commented 3 years ago

Sorry for response, its return True right now but I still have problem to run invoke_with_dmabuffer. I use GstAllocator to allocate memory in DMA but my code crash on invoke...

import os
import gi

gi.require_version('GstAllocators', '1.0')
from gi.repository import GstAllocators

import pycoral.utils.edgetpu as edgetpu

interpreter = edgetpu.make_interpreter('model_edgetpu.tflite')
interpreter.allocate_tensors()

print(interpreter)
print(edgetpu.supports_dmabuf(interpreter._native_handle()))

f = os.open('test.jpg', os.O_RDONLY)
print('File:', f)

dma_allocator = GstAllocators.DmaBufAllocator.new()
memory = GstAllocators.DmaBufAllocator.alloc(dma_allocator, f, 6400)
print(memory)
print(GstAllocators.is_dmabuf_memory(memory), GstAllocators.is_fd_memory(memory), GstAllocators.is_phys_memory(memory))
print(memory.size)

fd = GstAllocators.dmabuf_memory_get_fd(memory)
print(type(fd), fd)

edgetpu.invoke_with_dmabuffer(interpreter._native_handle(), fd, memory.size)

Output:

<tflite_runtime.interpreter.Interpreter object at 0xffff837e4eb8>
True
File: 22
<Gst.Memory object at 0xffff830f32e8 (GstMemory at 0x1a267890)>
True True False
6400
<class 'int'> 22
Traceback (most recent call last):
  File "test.py", line 28, in <module>
    edgetpu.invoke_with_dmabuffer(interpreter._native_handle(), fd, memory.size)
RuntimeError: InvokeWithDmaBuffer failed: Failed to execute request. Could not map pages : 5 (Invalid argument)Node number 1 (EdgeTpuDelegateForCustomOp) failed to invoke.
hjonnala commented 3 years ago

Have you checked map_buffer true of false. I think it needs to False to invoke_with_dambuufer. https://github.com/google-coral/pycoral/blob/master/pycoral/utils/edgetpu.py#L189

interpreter_handle = interpreter._native_handle()

map_buffer = not GstAllocators.is_dmabuf_memory(memory) or not edgetpu.supports_dmabuf(interpreter_handle)
print('map_buffer', map_buffer )
hjonnala commented 3 years ago

Hi @mzignis are you still having issue?

mzignis commented 3 years ago

Hi, yes I was trying few other things with dma buffer but still there is the same issue. Do you have any idea what can case that?

hjonnala commented 3 years ago

can you please share the model that you are using?

mzignis commented 3 years ago

I cannot share my custom model, but I also try run models from this repo (examples) with dma buff and there is the same issue.