Overview
The ifmain/vit-gpt2-image2promt-stable-diffusion
model builds upon nlpconnect/vit-gpt2-image-captioning and is trained on the 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 code.
Example Usage
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.
- Downloads last month
- 56
Model tree for ifmain/vit-gpt2-image2promt-stable-diffusion
Base model
nlpconnect/vit-gpt2-image-captioning