Blip dynamic input resolution (#30722)
* blip with interpolated pos encoding * feat: Add interpolate_pos_encoding option to other models from `BLIP` family. * include check for textual generated content in tests
This commit is contained in:
committed by
GitHub
parent
a4e530e3c8
commit
f63d822242
@@ -1381,6 +1381,20 @@ class BlipModelIntegrationTest(unittest.TestCase):
|
||||
[30522, 1037, 3861, 1997, 1037, 2450, 1998, 2014, 3899, 2006, 1996, 3509, 102],
|
||||
)
|
||||
|
||||
def test_inference_interpolate_pos_encoding(self):
|
||||
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(torch_device)
|
||||
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
||||
processor.image_processor.size = {"height": 500, "width": 500}
|
||||
|
||||
image = prepare_img()
|
||||
inputs = processor(images=image, return_tensors="pt").to(torch_device)
|
||||
|
||||
predictions = model.generate(**inputs, interpolate_pos_encoding=True)
|
||||
generated_text = processor.batch_decode(predictions, skip_special_tokens=True)[0].strip()
|
||||
|
||||
self.assertEqual(predictions[0].tolist(), [30522, 1037, 2450, 3564, 2006, 1996, 3509, 2007, 1037, 3899, 102])
|
||||
self.assertEqual(generated_text, "a woman sitting on the beach with a dog")
|
||||
|
||||
def test_inference_vqa(self):
|
||||
model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base").to(torch_device)
|
||||
processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
||||
|
||||
@@ -882,6 +882,22 @@ class Blip2ModelIntegrationTest(unittest.TestCase):
|
||||
)
|
||||
self.assertEqual(generated_text, "it's not a city, it's a beach")
|
||||
|
||||
def test_inference_interpolate_pos_encoding(self):
|
||||
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
|
||||
model = Blip2ForConditionalGeneration.from_pretrained(
|
||||
"Salesforce/blip2-opt-2.7b", torch_dtype=torch.float16
|
||||
).to(torch_device)
|
||||
processor.image_processor.size = {"height": 500, "width": 500}
|
||||
|
||||
image = prepare_img()
|
||||
inputs = processor(images=image, return_tensors="pt").to(torch_device)
|
||||
|
||||
predictions = model.generate(**inputs, interpolate_pos_encoding=True)
|
||||
generated_text = processor.batch_decode(predictions, skip_special_tokens=True)[0].strip()
|
||||
|
||||
self.assertEqual(predictions[0].tolist(), [2, 102, 693, 8, 2335, 15, 5, 4105, 50118])
|
||||
self.assertEqual(generated_text, "a woman and dog on the beach")
|
||||
|
||||
def test_inference_opt_batched_beam_search(self):
|
||||
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
|
||||
model = Blip2ForConditionalGeneration.from_pretrained(
|
||||
|
||||
@@ -612,3 +612,24 @@ class InstructBlipModelIntegrationTest(unittest.TestCase):
|
||||
generated_text,
|
||||
"The image depicts a man ironing clothes on the back of a yellow van in the middle of a busy city street. The man is wearing a yellow shirt with a bright yellow tie, and he is using an ironing board to complete his task. The image is unusual due to the fact that it shows a man ironing clothes on the back of a van in the middle of a busy city street. It is possible that the man is trying to save money by doing his laundry on the back of the van, but it is also possible that he is trying to save time by doing his laundry on the back of the van in the middle of a busy city street. Regardless of the reason for the man's actions, it is clear that he is trying to save time by doing his laundry on the back of the van in the middle of a busy city street.",
|
||||
)
|
||||
|
||||
def test_inference_interpolate_pos_encoding(self):
|
||||
processor = InstructBlipProcessor.from_pretrained("Salesforce/instructblip-flan-t5-xl")
|
||||
model = InstructBlipForConditionalGeneration.from_pretrained(
|
||||
"Salesforce/instructblip-flan-t5-xl",
|
||||
torch_dtype=torch.bfloat16,
|
||||
low_cpu_mem_usage=True,
|
||||
).to(torch_device)
|
||||
processor.image_processor.size = {"height": 500, "width": 500}
|
||||
|
||||
image = prepare_img()
|
||||
prompt = "What's in the image?"
|
||||
inputs = processor(images=image, text=prompt, return_tensors="pt").to(torch_device)
|
||||
|
||||
predictions = model.generate(**inputs, interpolate_pos_encoding=True)
|
||||
generated_text = processor.batch_decode(predictions, skip_special_tokens=True)[0].strip()
|
||||
|
||||
self.assertEqual(
|
||||
predictions[0].tolist(), [0, 37, 1023, 753, 3, 9, 2335, 3823, 30, 8, 2608, 28, 3, 9, 1782, 5, 1]
|
||||
)
|
||||
self.assertEqual(generated_text, "The image features a woman sitting on the beach with a dog.")
|
||||
|
||||
Reference in New Issue
Block a user