-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Florence 2 model series by Microsoft (#42)
* initial implementation * simplify * add other florence 2 * add florence 2 test * Bump version: 0.1.1 → 0.1.2 * update readme * update quickstart
- Loading branch information
Showing
8 changed files
with
507 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
import torch | ||
|
||
import xinfer | ||
|
||
|
||
@pytest.fixture | ||
def model(): | ||
return xinfer.create_model( | ||
"microsoft/Florence-2-base-ft", device="cpu", dtype="float32" | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def test_image(): | ||
return str(Path(__file__).parent.parent / "test_data" / "test_image_1.jpg") | ||
|
||
|
||
def test_florence2_initialization(model): | ||
assert model.model_id == "microsoft/Florence-2-base-ft" | ||
assert model.device == "cpu" | ||
assert model.dtype == torch.float32 | ||
|
||
|
||
def test_florence2_inference(model, test_image): | ||
prompt = "<CAPTION>" | ||
result = model.infer(test_image, prompt) | ||
|
||
assert isinstance(result, str) | ||
assert len(result) > 0 | ||
|
||
|
||
def test_florence2_batch_inference(model, test_image): | ||
prompt = "<CAPTION>" | ||
result = model.infer_batch([test_image, test_image], [prompt, prompt]) | ||
|
||
assert isinstance(result, list) | ||
assert len(result) == 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import torch | ||
from transformers import AutoModelForCausalLM, AutoProcessor | ||
|
||
from ..model_registry import ModelInputOutput, register_model | ||
from ..models import BaseModel, track_inference | ||
|
||
|
||
@register_model( | ||
"microsoft/Florence-2-large", "transformers", ModelInputOutput.IMAGE_TEXT_TO_TEXT | ||
) | ||
@register_model( | ||
"microsoft/Florence-2-base", | ||
"transformers", | ||
ModelInputOutput.IMAGE_TEXT_TO_TEXT, | ||
) | ||
@register_model( | ||
"microsoft/Florence-2-large-ft", | ||
"transformers", | ||
ModelInputOutput.IMAGE_TEXT_TO_TEXT, | ||
) | ||
@register_model( | ||
"microsoft/Florence-2-base-ft", | ||
"transformers", | ||
ModelInputOutput.IMAGE_TEXT_TO_TEXT, | ||
) | ||
class Florence2(BaseModel): | ||
def __init__( | ||
self, | ||
model_id: str, | ||
device: str = "cpu", | ||
dtype: str = "float32", | ||
): | ||
super().__init__(model_id, device, dtype) | ||
self.load_model() | ||
|
||
def load_model(self): | ||
self.model = AutoModelForCausalLM.from_pretrained( | ||
self.model_id, trust_remote_code=True | ||
).to(self.device, self.dtype) | ||
self.model.eval() | ||
self.model = torch.compile(self.model, mode="max-autotune") | ||
self.processor = AutoProcessor.from_pretrained( | ||
self.model_id, trust_remote_code=True | ||
) | ||
|
||
@track_inference | ||
def infer(self, image: str, prompt: str = None, **generate_kwargs) -> str: | ||
output = self.infer_batch([image], [prompt], **generate_kwargs) | ||
return output[0] | ||
|
||
@track_inference | ||
def infer_batch( | ||
self, images: list[str], prompts: list[str] = None, **generate_kwargs | ||
) -> list[str]: | ||
images = self.parse_images(images) | ||
inputs = self.processor(text=prompts, images=images, return_tensors="pt").to( | ||
self.device, self.dtype | ||
) | ||
|
||
if "max_new_tokens" not in generate_kwargs: | ||
generate_kwargs["max_new_tokens"] = 1024 | ||
if "num_beams" not in generate_kwargs: | ||
generate_kwargs["num_beams"] = 3 | ||
|
||
with torch.inference_mode(): | ||
generated_ids = self.model.generate( | ||
input_ids=inputs["input_ids"], | ||
pixel_values=inputs["pixel_values"], | ||
**generate_kwargs, | ||
) | ||
|
||
generated_text = self.processor.batch_decode( | ||
generated_ids, skip_special_tokens=False | ||
) | ||
|
||
parsed_answers = [ | ||
self.processor.post_process_generation( | ||
text, task=prompt, image_size=(img.width, img.height) | ||
).get(prompt) | ||
for text, prompt, img in zip(generated_text, prompts, images) | ||
] | ||
|
||
return parsed_answers |