snowzach / doods2

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

EdgeTPU tflite models, labels off by one #108

Open peacefulhill opened 1 month ago

peacefulhill commented 1 month ago

Tested more or less all coral.ai EdgeTPU models, which all shares the same label file. I have the same problem with all models. The labels are off by one, and the first label is considered "unknown" which is supposed to be "person". Example, beginning of labels: person bicycle car In the detection, bicycles becomes person, cars become bicycles and persons become unknown. This problem does not appear for tflite models (not EdgeTPU version)

It can be sort of worked around assuming you don't care about detecting persons. If the first row with person is deleted. It all works, persons still become unknown but all other labels work.

Example, with all labels in the file: image

Example, with person removed from label file: image

And finally the default tflite (not for EdgeTPU, person at the beginning of label also here): image

peacefulhill commented 1 month ago

Just after finally writing an issue, I noticed the mentioning of "labelsStartFromZero: true", but I can't seem to find where to put it, my best guess is within the doods.detectors as it must be model specific, but e.g. this config does not fix my issue above:

- name: coral_lite3
  type: tflite
  modelFile: /share/doods/models/efficientdet_lite3_512_ptq_edgetpu.tflite
  labelFile: /share/doods/models/coco_labels.txt
  labelsStartFromZero: true
  hwAccel: true

Edit: I have studied the code and from what I can tell, the example config above really should work, but I can't get the config to take and use 0 indexing for the labels. FWIW I am running the doods2 as an HAOS addon. It is actually working really well apart from the 0 vs 1 indexing issue I have. Also the hwAccel boolean works very similar to the labelsStartFromZero and I don't have any issues with the hwAccel setting, it works fine (after adding privileged: true to the server config).

peacefulhill commented 1 month ago

Ok, so I found a difference in how the default label file is constructed and the ones you download from coral.ai. The default labels include index numbers, e.g. 0 person 1 bicycle 2 car

If I add such index number to the label files for the EdgeTPU models, it does work. However I still consider the labelsStartFromZero: true not to be working as intended. If I have misunderstood the indended use of this variable, fell free to close the issue. It would be really nice if the labels that you download from coral.ai for the EdgeTPU would just work, but adding index numbers is of course fairly straight forward as a workaround.

snowzach commented 2 weeks ago

Yeah, you have labelsStartFromZero in the right place. I just reviewed all the code and it looks right. Are you sure you didn't have the number in the file when you tried it?

peacefulhill commented 2 weeks ago

Yes, I am sure as at that testing stage I had not yet understood adding index was possible. I instead temporarily worked around the issue by removing the first row. I couldn’t find any obvious issues in the code either. It should be possible to reproduce with cora.ai downloadable models. However I will not push for further investigation as I am happy with just adding index. Thanks for confirming I was using ghe variable correctly.