🚀 圖像文本轉文本模型
本項目是基於transformers
庫的圖像文本轉文本模型,藉助microsoft/Florence-2-large
基礎模型,可實現圖像描述等功能,在藝術領域有一定應用價值。
🚀 快速開始
本項目是一個圖像文本轉文本的模型,以下是使用該模型的快速開始步驟。
📦 安裝指南
在使用模型之前,需要安裝必要的依賴庫,可使用以下命令進行安裝:
pip install -q datasets flash_attn timm einops
💻 使用示例
基礎用法
以下代碼展示瞭如何加載模型、處理輸入並生成圖像描述:
from transformers import AutoModelForCausalLM, AutoProcessor, AutoConfig
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = AutoModelForCausalLM.from_pretrained("gokaygokay/Florence-2-Flux-Large", trust_remote_code=True).to(device).eval()
processor = AutoProcessor.from_pretrained("gokaygokay/Florence-2-Flux-Large", trust_remote_code=True)
def run_example(task_prompt, text_input, image):
prompt = task_prompt + text_input
if image.mode != "RGB":
image = image.convert("RGB")
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
generated_ids = model.generate(
input_ids=inputs["input_ids"],
pixel_values=inputs["pixel_values"],
max_new_tokens=1024,
num_beams=3,
repetition_penalty=1.10,
)
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
parsed_answer = processor.post_process_generation(generated_text, task=task_prompt, image_size=(image.width, image.height))
return parsed_answer
from PIL import Image
import requests
import copy
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
image = Image.open(requests.get(url, stream=True).raw)
answer = run_example("<DESCRIPTION>", "Describe this image in great detail.", image)
final_answer = answer["<DESCRIPTION>"]
print(final_answer)
📄 許可證
本項目採用Apache-2.0
許可證。
🔍 模型信息
屬性 |
詳情 |
模型類型 |
圖像文本轉文本模型 |
基礎模型 |
microsoft/Florence-2-large |
訓練數據集 |
kadirnar/fluxdev_controlnet_16k |
庫名稱 |
transformers |
任務標籤 |
image-text-to-text |
相關標籤 |
art |