Update README.md
Browse files
README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
---
|
| 2 |
license: mit
|
|
|
|
|
|
|
| 3 |
datasets:
|
| 4 |
- google/imageinwords
|
| 5 |
language:
|
|
@@ -7,4 +9,102 @@ language:
|
|
| 7 |
library_name: transformers
|
| 8 |
pipeline_tag: image-to-text
|
| 9 |
---
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: mit
|
| 3 |
+
base_model:
|
| 4 |
+
- microsoft/Florence-2-large-ft
|
| 5 |
datasets:
|
| 6 |
- google/imageinwords
|
| 7 |
language:
|
|
|
|
| 9 |
library_name: transformers
|
| 10 |
pipeline_tag: image-to-text
|
| 11 |
---
|
| 12 |
+
|
| 13 |
+
# Florence-2-large-ft trained on task MORE_DETAILED_CAPTION with imageinwords
|
| 14 |
+
|
| 15 |
+
## Usage
|
| 16 |
+
|
| 17 |
+
### Single Image
|
| 18 |
+
|
| 19 |
+
```python
|
| 20 |
+
from PIL import Image
|
| 21 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 22 |
+
|
| 23 |
+
name = "yayayaaa/florence-2-large-ft-moredetailed"
|
| 24 |
+
model = AutoModelForCausalLM.from_pretrained(name, trust_remote_code=True).to("cuda").eval()
|
| 25 |
+
processor = AutoProcessor.from_pretrained(name, trust_remote_code=True)
|
| 26 |
+
|
| 27 |
+
prompt = "<MORE_DETAILED_CAPTION>"
|
| 28 |
+
filename = 'test.jpg'
|
| 29 |
+
image = Image.open(filename).convert('RGB')
|
| 30 |
+
|
| 31 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt").to("cuda")
|
| 32 |
+
|
| 33 |
+
generated_ids = model.generate(
|
| 34 |
+
input_ids=inputs["input_ids"],
|
| 35 |
+
pixel_values=inputs["pixel_values"],
|
| 36 |
+
max_new_tokens=1024,
|
| 37 |
+
do_sample=False,
|
| 38 |
+
|
| 39 |
+
)
|
| 40 |
+
print(processor.batch_decode(generated_ids, skip_special_tokens=True)[0])
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
### Batch
|
| 44 |
+
|
| 45 |
+
Install accelerate for device_map
|
| 46 |
+
|
| 47 |
+
Can do batch_size=28 for 24GB VRAM
|
| 48 |
+
|
| 49 |
+
```python
|
| 50 |
+
import sys
|
| 51 |
+
import torch
|
| 52 |
+
from tqdm import tqdm
|
| 53 |
+
from pathlib import Path
|
| 54 |
+
from PIL import Image
|
| 55 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 56 |
+
from torch.utils.data import Dataset, DataLoader
|
| 57 |
+
|
| 58 |
+
task = "<MORE_DETAILED_CAPTION>"
|
| 59 |
+
name="yayayaaa/florence-2-large-ft-moredetailed"
|
| 60 |
+
model = AutoModelForCausalLM.from_pretrained(name, trust_remote_code=True, device_map="auto")
|
| 61 |
+
processor = AutoProcessor.from_pretrained(name, trust_remote_code=True, device_map="auto")
|
| 62 |
+
|
| 63 |
+
#model.language_model.generate = torch.compile(model.language_model.generate)
|
| 64 |
+
|
| 65 |
+
# BATCH SIZE
|
| 66 |
+
batch_size=28
|
| 67 |
+
|
| 68 |
+
directory = Path(sys.argv[1])
|
| 69 |
+
patterns = ['**/*.jpg', '**/*.jpeg', '**/*.png']
|
| 70 |
+
patterns += [p.upper() for p in patterns]
|
| 71 |
+
filenames = [str(fn) for pattern in patterns for fn in directory.glob(pattern)]
|
| 72 |
+
|
| 73 |
+
class Data(Dataset):
|
| 74 |
+
def __init__(self, data):
|
| 75 |
+
self.name="Data"
|
| 76 |
+
self.data=data
|
| 77 |
+
|
| 78 |
+
def __len__(self):
|
| 79 |
+
return len(self.data)
|
| 80 |
+
|
| 81 |
+
def __getitem__(self, idx):
|
| 82 |
+
return self.data[idx]
|
| 83 |
+
|
| 84 |
+
data = DataLoader(Data(filenames), batch_size=batch_size, num_workers=0, shuffle=False)
|
| 85 |
+
|
| 86 |
+
@torch.inference_mode()
|
| 87 |
+
def process_images(images):
|
| 88 |
+
inputs = processor(text=[task]*len(images), images=images, return_tensors="pt").to("cuda")
|
| 89 |
+
|
| 90 |
+
generated_ids = model.generate(
|
| 91 |
+
input_ids=inputs["input_ids"],
|
| 92 |
+
pixel_values=inputs["pixel_values"],
|
| 93 |
+
max_new_tokens=1024,
|
| 94 |
+
do_sample=False,
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
| 98 |
+
return generated_texts
|
| 99 |
+
|
| 100 |
+
for batch in tqdm(data):
|
| 101 |
+
images = [Image.open(filename).convert("RGB") for filename in batch]
|
| 102 |
+
|
| 103 |
+
generated_texts = process_images(images)
|
| 104 |
+
|
| 105 |
+
for i, caption in enumerate(generated_texts):
|
| 106 |
+
filename = Path(batch[i])
|
| 107 |
+
#print(caption)
|
| 108 |
+
with open(filename.with_suffix(".txt"), "w") as text_file:
|
| 109 |
+
text_file.write(caption)
|
| 110 |
+
```
|