blip image2promt stable diffusion base

提供商ifmain
分类image-generation
许可证Apache-2.0
下载量22
星标0

简介

这是一个基于 BLIP 视觉语言模型的小巧工具,专门用于将图片“反向翻译”为 Stable Diffusion 可识别的提示词(Prompt)。很多开发者在尝试 AI 绘画时,常面临‘心中有图但写不出词’的痛点,该模型正好填补了这一空白。它能分析图像内容并生成描述性的文本,帮助用户快速拆解参考图的构图和元素,从而在 SD 中实现精准的风格迁移或图像复刻。上手门槛极低,是图像生成工作流中非常实用的辅助插件。

核心亮点

  • 实现图片到提示词的反向推导
  • 适配 Stable Diffusion 基础模型
  • 辅助快速拆解参考图视觉元素
  • 低门槛提升 AI 绘画出图精准度

使用方法

安装依赖
# 安装 Hugging Face transformers
pip install transformers torch
SDK 使用
# 使用 transformers 加载模型
from transformers import AutoModel, AutoTokenizer

model = AutoModel.from_pretrained("ifmain/blip-image2promt-stable-diffusion-base")
tokenizer = AutoTokenizer.from_pretrained("ifmain/blip-image2promt-stable-diffusion-base")

Hugging Face 下载

我们推荐使用命令行或者 Hugging Face Hub SDK 来进行模型的下载。

操作指引:在下载前,请先通过如下命令安装 huggingface_hub:

操作指引
pip install -U huggingface_hub

命令行下载

下载完整模型库

下载完整模型库
huggingface-cli download ifmain/blip-image2promt-stable-diffusion-base

下载单个文件到指定本地文件夹(以下载 config.json 到当前路径下 ./dir 目录为例)

下载单个文件到指定本地文件夹(以下载 config.json 到当前路径下 ./dir 目录为例)
huggingface-cli download ifmain/blip-image2promt-stable-diffusion-base config.json --local-dir ./dir

更多命令行下载选项,可参见官方文档

SDK 下载

SDK 下载
# 模型下载
from huggingface_hub import snapshot_download
model_dir = snapshot_download('ifmain/blip-image2promt-stable-diffusion-base')

Git 下载

请确保 lfs 已经被正确安装

Git 下载
git lfs install
git clone https://huggingface.co/ifmain/blip-image2promt-stable-diffusion-base

如果您希望跳过 lfs 大文件下载,可以使用如下命令

跳过 LFS
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/ifmain/blip-image2promt-stable-diffusion-base

模型文件托管在 Hugging Face Hub,使用 HF CLI / SDK / Git 直接下载,不经过本站。

PyTorch / Transformers 使用

安装 Transformers

安装 Transformers
pip install -U transformers torch

模型加载和推理

模型加载和推理
from transformers import AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained('ifmain/blip-image2promt-stable-diffusion-base')
tokenizer = AutoTokenizer.from_pretrained('ifmain/blip-image2promt-stable-diffusion-base')

完整文档

来源: HuggingFace

---
datasets:

  • Ar4ikov/civitai-sd-337k

language:
  • en

pipeline_tag: image-to-text
base_model: Salesforce/blip-image-captioning-base
---

Licence

license inherited from Salesforce/blip-image-captioning-base

Overview

ifmain/blip-image2promt-stable-diffusion-base is a model based on Salesforce/blip-image-captioning-base, trained on the Ar4ikov/civitai-sd-337k dataset (2K images). This model is designed to generate text descriptions of images in the style of prompts for use with Stable Diffusion models.

I used my Blip training code: BLIP-Easy-Trainer

Example Usage

python
import torch
import requests
from PIL import Image
from transformers import BlipProcessor, BlipForConditionalGeneration
import re

def prepare(text):
text = text.replace('. ','.').replace(' .','.')
text = text.replace('( ','(').replace(' (','(')
text = text.replace(') ',')').replace(' )',')')
text = text.replace(': ',':').replace(' :',':')
text = text.replace('_ ','_').replace(' _','_')
text = text.replace(',(())','').replace('(()),','')
for i in range(10):
text = text.replace(')))','))').replace('(((','((')
text = re.sub(r'<[^>]*>', '', text)
return text

path_to_model = "ifmain/blip-image2promt-stable-diffusion-base"

processor = BlipProcessor.from_pretrained(path_to_model)
model = BlipForConditionalGeneration.from_pretrained(path_to_model, torch_dtype=torch.float16).to("cuda")

img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')

unconditional image captioning

inputs = processor(raw_image, return_tensors="pt").to("cuda", torch.float16)

out = model.generate(**inputs, max_new_tokens=100)

out_txt = processor.decode(out[0], skip_special_tokens=True)

print(prepare(out_txt)) # woman sitting on the beach at sunset, rear view,((happy)),((happy)),((dog)),((mixed)),(()),((

Addition

This model support SFW and NSFW content