File size: 2,666 Bytes
3e7a3bb 3474710 3e7a3bb 3474710 3e7a3bb 3474710 3e7a3bb 3474710 3e7a3bb 3474710 3e7a3bb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
---
datasets:
- Ar4ikov/civitai-sd-337k
language:
- en
pipeline_tag: image-to-text
base_model: nlpconnect/vit-gpt2-image-captioning
license: apache-2.0
---
# Overview
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.
Training was conducted using the [Vit-GPT-Easy-Trainer](https://github.com/ifmain/Vit-GPT-Easy-Trainer) code.
# Example Usage
```python
from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
import torch
from PIL import Image
import re
import requests
def prepare(text):
text = re.sub(r'<[^>]*>', '', text)
text = ','.join(list(set(text.split(',')))[:-1])
for i in range(5):
if text[0]==',' or text[0]==' ':
text=text[1:]
return text
path_to_model = "ifmain/vit-gpt2-image2promt-stable-diffusion"
model = VisionEncoderDecoderModel.from_pretrained(path_to_model)
feature_extractor = ViTImageProcessor.from_pretrained(path_to_model)
tokenizer = AutoTokenizer.from_pretrained(path_to_model)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
max_length = 256
num_beams = 4
gen_kwargs = {"max_length": max_length, "num_beams": num_beams}
def predict_step(image_paths):
images = []
for image_path in image_paths:
if 'http' in image_path:
i_image = Image.open(requests.get(image_path, stream=True).raw).convert('RGB')
else:
i_image = Image.open(image_path).convert('RGB')
images.append(i_image)
pixel_values = feature_extractor(images=images, return_tensors="pt").pixel_values
pixel_values = pixel_values.to(device)
output_ids = model.generate(pixel_values, **gen_kwargs)
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
preds = [prepare(pred).strip() for pred in preds]
return preds
img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
result = predict_step([img_url]) # ['red shirt, chromatic aberration, light emitting object, barefoot, best quality, ocean background, 1girl, 8k wallpaper, intricate details, chromatic light, light, ocean, backpack, ultra-detailed, ocean light,masterpiece']
print(result)
```
## Additional Information
This model supports both SFW and NSFW content. |