NielsRogge / Transformers-Tutorials

This repository contains demos I made with the Transformers library by HuggingFace.
MIT License
9.17k stars 1.42k forks source link

Error in SegmentAnything finetuning notebook #290

Closed srib closed 1 year ago

srib commented 1 year ago

Bug:

In the notebook on finetuning SegmentAnything, the following code (in Cell 11) throws an error:

example = train_dataset[0]
for k,v in example.items():
  print(k,v.shape)

Here is the ValueError thrown:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[<ipython-input-29-8c49af5cb2d0>](https://localhost:8080/#) in <cell line: 7>()
      5 prompt = get_bounding_box(ground_truth_mask)
      6 print(prompt)
----> 7 inputs = train_dataset.processor(image, input_boxes=[prompt], return_tensors="pt")

1 frames
[/usr/local/lib/python3.10/dist-packages/transformers/models/sam/processing_sam.py](https://localhost:8080/#) in _check_and_preprocess_points(self, input_points, input_labels, input_boxes)
    233                 or not isinstance(input_boxes[0][0], list)
    234             ):
--> 235                 raise ValueError("Input boxes must be a list of list of list of floating integers.")
    236             input_boxes = [np.array(box).astype(np.float32) for box in input_boxes]
    237         else:

ValueError: Input boxes must be a list of list of list of floating integers.

Fix: The fix would be to pass the input_boxes as a nested list in SamDataset. (In Cell 10)

inputs = self.processor(image, input_boxes=[[prompt]], return_tensors="pt")
srib commented 1 year ago

@NielsRogge Happy to submit a PR. Please let me know if you prefer.

NielsRogge commented 1 year ago

Thanks a lot for reporting, have fixed it here.