Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,70 @@
|
|
1 |
-
---
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
datasets:
|
3 |
+
- Ar4ikov/civitai-sd-337k
|
4 |
+
language:
|
5 |
+
- en
|
6 |
+
pipeline_tag: image-to-text
|
7 |
+
base_model: nlpconnect/vit-gpt2-image-captioning
|
8 |
+
license: apache-2.0
|
9 |
+
---
|
10 |
+
|
11 |
+
# Overview
|
12 |
+
|
13 |
+
The `ifmain/vit-gpt2-image2promt-stable-diffusion` model builds upon [nlpconnect/vit-gpt2-image-captioning](https://huggingface.co/nlpconnect/vit-gpt2-image-captioning) and is trained on the [Ar4ikov/civitai-sd-337k](https://huggingface.co/datasets/Ar4ikov/civitai-sd-337k) dataset, which includes 2,000 images. This model is specifically designed to generate text descriptions of images in a format suitable for prompts used with Stable Diffusion models.
|
14 |
+
|
15 |
+
Training was conducted using the [Vit-GPT-Easy-Trainer](https://github.com/ifmain/Vit-GPT-Easy-Trainer) code.
|
16 |
+
|
17 |
+
# Example Usage
|
18 |
+
|
19 |
+
```python
|
20 |
+
from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
|
21 |
+
import torch
|
22 |
+
from PIL import Image
|
23 |
+
import re
|
24 |
+
|
25 |
+
def prepare(text):
|
26 |
+
text = text.replace('. ', '.').replace(' .', '.')
|
27 |
+
text = text.replace('( ', '(').replace(' (', '(')
|
28 |
+
text = text.replace(') ', ')').replace(' )', ')')
|
29 |
+
text = text.replace(': ', ':').replace(' :', ':')
|
30 |
+
text = text.replace('_ ', '_').replace(' _', '_')
|
31 |
+
text = text.replace(',(())', '').replace('(()),', '')
|
32 |
+
for i in range(10):
|
33 |
+
text = text.replace(')))', '))').replace('(((', '((')
|
34 |
+
text = re.sub(r'<[^>]*>', '', text)
|
35 |
+
return text
|
36 |
+
|
37 |
+
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
38 |
+
feature_extractor = ViTImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
39 |
+
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
40 |
+
|
41 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
42 |
+
model.to(device)
|
43 |
+
|
44 |
+
max_length = 16
|
45 |
+
num_beams = 4
|
46 |
+
gen_kwargs = {"max_length": max_length, "num_beams": num_beams}
|
47 |
+
def predict_step(image_paths):
|
48 |
+
images = []
|
49 |
+
for image_path in image_paths:
|
50 |
+
i_image = Image.open(image_path)
|
51 |
+
if i_image.mode != "RGB":
|
52 |
+
i_image = i_image.convert(mode="RGB")
|
53 |
+
|
54 |
+
images.append(i_image)
|
55 |
+
|
56 |
+
pixel_values = feature_extractor(images=images, return_tensors="pt").pixel_values
|
57 |
+
pixel_values = pixel_values.to(device)
|
58 |
+
|
59 |
+
output_ids = model.generate(pixel_values, **gen_kwargs)
|
60 |
+
|
61 |
+
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
62 |
+
preds = [prepare(pred).strip() for pred in preds]
|
63 |
+
return preds
|
64 |
+
|
65 |
+
predict_step(['doctor.e16ba4e4.jpg']) # ['a woman in a hospital bed with a woman in a hospital bed']
|
66 |
+
```
|
67 |
+
|
68 |
+
## Additional Information
|
69 |
+
|
70 |
+
This model supports both SFW and NSFW content.
|