IDEA-Research / Grounded-Segment-Anything

Grounded SAM: Marrying Grounding DINO with Segment Anything & Stable Diffusion & Recognize Anything - Automatically Detect , Segment and Generate Anything
https://arxiv.org/abs/2401.14159
Apache License 2.0
14.93k stars 1.38k forks source link

Error Encountered While Executing Grounding-Dino-Sam Notebook with Custom Data - Class Label Mismatch Leads to TypeError #371

Open puranjay123 opened 1 year ago

puranjay123 commented 1 year ago

Hi, @rentainhe

I am trying to run grounding-dino-sam notebook when I tried with custom data ,it is giving error(stated below):

What I think and found: I found out that When the labels I have written and the labels detected by the model even mismatch by a little bit the code throws an error stated below. In a normal scenario there should be multiple labels and if there is a mismatch in Class labels and labels getting out from the model it should simply ignore the mismatched labels. When I try with default class the output is getting just fine CLASSES = ['car', 'dog', 'person', 'nose', 'chair', 'shoe', 'ear'] when I used my CLASSES labels it is throwing error stated below: CLASSES = ['dog' , 'paper' , 'cup' , 'Snack' , 'Candy' , 'Bag', 'Wrapper'] here is the link of my collab notebook here in which I encountered the error.

here is the list of the original collab notebook: here

here is the link for images that I used: here

TypeError   

      Traceback (most recent call last)
[<ipython-input-114-225db28cada8>](https://localhost:8080/#) in <cell line: 17>()
     15 # annotate image with detections
     16 box_annotator = sv.BoxAnnotator()
---> 17 labels = [
     18     f"{CLASSES[class_id]} {confidence:0.2f}"
     19     for _, _, confidence, class_id, _

[<ipython-input-114-225db28cada8>](https://localhost:8080/#) in <listcomp>(.0)
     16 box_annotator = sv.BoxAnnotator()
     17 labels = [
---> 18     f"{CLASSES[class_id]} {confidence:0.2f}"
     19     for _, _, confidence, class_id, _
     20     in detections]

TypeError: list indices must be integers or slices, not NoneType`
3942368 commented 11 months ago

I also had a similar error as you. I don't know if my experience will be helpful, but I'm writing this in hopes of your luck. My case is as follows: CLASSES =["van","car"]

Grounddino generated a "van car" phrase that I did not specify, and an error occurred because it was not in CLASSES =["van","car"].

My solution is to change 'van car' to 'van'.

The following code was inserted at line 201 of the groundingdino/util/inference.py file.

        phrases_copy = phrases.copy()
        for index_phrase,phrase in enumerate(phrases_copy):
            if phrase.find(' ') != -1:
                phrases[index_phrase] = phrase[:phrase.find(' ')]
yxchng commented 10 months ago

@3942368 why this occur?

3942368 commented 10 months ago

@yxchng When gronding-dino thinks there are different labels in the same image area, it creates a new label.

In my case, 'car' and 'van'were created in the exact same area of ​​the image, and gronding-dino created a new label called 'van car' that I did not specify.

So an error occurred.

In my case, the' van' and 'car' labels caused the problem. The superior probably had 'snacks', 'candy' that caused the problem.

Anyway, the new label created by gronding-dino is created as a combination of the labels I created, so my idea is to change the label created by gronding-dino to the label I created