snowzach / doods2

API for detecting objects in images and video streams using Tensorflow
MIT License
237 stars 30 forks source link

Coral USB error: Could not load EdgeTPU detector #48

Closed TrungLam closed 2 years ago

TrungLam commented 2 years ago

Hi there,

I need help with my doods2 setup. I'm trying to start doods2 with my coral usb device and it's failing to create the tflite detector.

$ docker run -it -p 7070:8080 --privileged --device /dev/bus/usb -v /path_example/doods2/config.yaml:/opt/doods/config.yaml -v /path_example/doods2/
models:/opt/doods/models snowzach/doods2:latest
2022-07-18 00:37:58,763 - doods.doods - ERROR - Could not create detector tflite/default: Could not load EdgeTPU detector
2022-07-18 00:37:59,030 - uvicorn.error - INFO - Started server process [1]
2022-07-18 00:37:59,031 - uvicorn.error - INFO - Waiting for application startup.
2022-07-18 00:37:59,033 - uvicorn.error - INFO - Application startup complete.
2022-07-18 00:37:59,034 - uvicorn.error - INFO - Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)

config.yaml

server:
  host: 0.0.0.0
  port: 8080
  metrics: true
logging:
  level: all
doods:
  log: detections
  boxes:
    enabled: True
    boxColor: [0, 255, 0]
    boxThickness: 1
    fontScale: 1.2
    fontColor: [0, 255, 0]
    fontThickness: 1
  regions:
    enabled: True
    boxColor: [255, 0, 255]
    boxThickness: 1
    fontScale: 1.2
    fontColor: [255, 0, 255]
    fontThickness: 1
  globals:
    enabled: True
    fontScale: 1.2
    fontColor: [255, 255, 0]
    fontThickness: 1
  detectors:
    - name: default
      type: tflite
      modelFile: /opt/doods/models/ssdlite_mobiledet_coco_qat_postprocess_edgetpu.tflite
      labelFile: /opt/doods/models/coco_labels.txt
      hwAccel: true
$ lsusb
Bus 002 Device 011: ID 18d1:9302 Google Inc. 

I'm running on a Raspberry Pi 4B. I've tried to connect the coral device straight into the USB2.0 and USB3.0 pi slots as well as connected to a powered USB3.0 hub.

When I attach the shell of the container and check for libedgetpu.so.1.0, I can see it present.

root@2a030aef58c4:/usr/lib# ls /usr/lib/aarch64-linux-gnu/libedgetpu.so.1.0 -ltr
-rw-r--r-- 1 root root 1135880 Jul  9  2021 /usr/lib/aarch64-linux-gnu/libedgetpu.so.1.0
TrungLam commented 2 years ago

I removed the second try block to see what the ValueError looks like:

            try:
                interpreter = Interpreter(model_path=self.config.model,
                    experimental_delegates=[load_delegate('libedgetpu.so.1.0')])
            # This might fail the first time as this seems to load the drivers for the EdgeTPU the first time.
            # Doing it again will load the driver.
            except ValueError:
                interpreter = Interpreter(model_path=self.config.model,
                    experimental_delegates=[load_delegate('libedgetpu.so.1.0')])

And got

2022-07-18 05:11:37,708 - doods.doods - ERROR - Could not create detector tflite/default: Mmap of '10' at offset '0' failed with error '19'.
snowzach commented 2 years ago

So when you first plug in the coral, run lsusb and it should show the device with a certain name. The first time you run doods it will load some driver into the Coral and when you run lsusb again it will have a different name. I can't remember what the names were. Can you confirm that bit is working?

TrungLam commented 2 years ago

Thanks for taking a look!

I replugged the coral device, and before running doods, its:

Bus 002 Device 008: ID 174c:55aa ASMedia Technology Inc. ASM1051E SATA 6Gb/s bridge, ASM1053E SATA 6Gb/s bridge, ASM1153 SATA 3Gb/s bridge, ASM1153E SATA 6Gb/s bridge
Bus 002 Device 007: ID 174c:55aa ASMedia Technology Inc. ASM1051E SATA 6Gb/s bridge, ASM1053E SATA 6Gb/s bridge, ASM1153 SATA 3Gb/s bridge, ASM1153E SATA 6Gb/s bridge
Bus 002 Device 005: ID 152d:0562 JMicron Technology Corp. / JMicron USA Technology Corp. JMS567 SATA 6Gb/s bridge
Bus 002 Device 003: ID 0bda:0411 Realtek Semiconductor Corp. Hub
Bus 002 Device 012: ID 1a6e:089a Global Unichip Corp. 
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 005: ID 0bda:5411 Realtek Semiconductor Corp. RTS5411 Hub
Bus 001 Device 003: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Run doods:

2022-07-18 15:52:24,448 - doods.doods - ERROR - Could not create detector tflite/default: Could not load EdgeTPU detector: Mmap of '10' at offset '0' failed with error '19'.
Mmap of '10' at offset '0' failed with error '19'.
2022-07-18 15:52:24,755 - uvicorn.error - INFO - Started server process [1]
2022-07-18 15:52:24,757 - uvicorn.error - INFO - Waiting for application startup.
2022-07-18 15:52:24,757 - uvicorn.error - INFO - Application startup complete.
2022-07-18 15:52:24,758 - uvicorn.error - INFO - Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)

lsub result:

Bus 002 Device 008: ID 174c:55aa ASMedia Technology Inc. ASM1051E SATA 6Gb/s bridge, ASM1053E SATA 6Gb/s bridge, ASM1153 SATA 3Gb/s bridge, ASM1153E SATA 6Gb/s bridge
Bus 002 Device 007: ID 174c:55aa ASMedia Technology Inc. ASM1051E SATA 6Gb/s bridge, ASM1053E SATA 6Gb/s bridge, ASM1153 SATA 3Gb/s bridge, ASM1153E SATA 6Gb/s bridge
Bus 002 Device 005: ID 152d:0562 JMicron Technology Corp. / JMicron USA Technology Corp. JMS567 SATA 6Gb/s bridge
Bus 002 Device 003: ID 0bda:0411 Realtek Semiconductor Corp. Hub
Bus 002 Device 013: ID 18d1:9302 Google Inc. 
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 005: ID 0bda:5411 Realtek Semiconductor Corp. RTS5411 Hub
Bus 001 Device 003: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
snowzach commented 2 years ago

So it is working.. The Global Unichip is the "before" and the Google Inc is the "after" the driver has been loaded. From what I can see error 19 is "No such device" though which is pretty confusing. I am completely guessing at this point but make sure libusb and maybe libusb-1 are installed on your docker host system. You can also try setting the environment UDEV=1 in the docker container.

TrungLam commented 2 years ago
$ dpkg -l | grep libusb
ii  libusb-0.1-4:arm64                   2:0.1.12-32                      arm64        userspace USB programming library
ii  libusb-1.0-0:arm64                   2:1.0.24-3                       arm64        userspace USB programming library```

And running with UDEV=1 didn't work.

I tried running the container with the coral device on my laptop and it succeeds with little issue.

TrungLam commented 2 years ago

I have a second Pi 4B that's running home assistant core os. I spun up a doods2 addon from your repo: https://github.com/snowzach/hassio-addons/tree/master/doods2

And I get the same error :(. I had your original doods container working on the same Pi 4b for a long time before I recently move home assistant core os onto it.

I'm going to try on distro hopping on my Pi 3 tomorrow and seeing if it's an OS/conflicting kernel/pkg issue.

TrungLam commented 2 years ago

It's working on my Pi 3B+ with fresh install of Rasbian OS Lite. I don't know if I should be happy or sad haha.

Summary of devices I tried: Pi 4B 8GB Rasbian OS Lite with OMV installed - Failed Pi 4B 4GB Home Assistant Core OS - Failed

Laptop Arch Linux - Succeed Pi 3B+ 1GB Fresh Rasbian OS Lite - Succeed

snowzach commented 2 years ago

That's really strange. There must be some sort of issue with the OS. I have a rasberry pi 4, I'll have to tinker with it. Gotta be some sore of USB library issue.

MintyTrebor commented 2 years ago

I had the same problem - for me it was a permissions issue. Once I gave access to the docker user to /dev/bus/usb on host and restarted doods2 container it worked straight away.

snowzach commented 2 years ago

I'm going to close this one. Re-Open if still an issue.