python diffusers StableDiffusionXLPipeline 离线使用

  

下载sd_xl_base_1.0.safetensors

https://huggingface.co/runwayml/stable-diffusion-v1-5/tree/main

我这下载后放到项目 models 里

model_path = "./models/v1-5-pruned-emaonly.safetensors"
# model_path = "./models/v1-5-pruned.safetensors"
# modelId = "runwayml/stable-diffusion-v1-5"
if Util.isMac():
    # from_single_file
    pipe = StableDiffusionPipeline.from_single_file(model_path, original_config_file='./models/v1-inference.yaml', cache_dir='./cache/', use_safetensors=True)
    pipe = pipe.to("mps")
    pipe.enable_attention_slicing()
else:
    pipe = StableDiffusionPipeline.from_single_file(model_path, torch_dtype=torch.float16, use_safetensors=True)
    pipe.to("cuda")
    pipe.enable_model_cpu_offload()
    pipe.enable_attention_slicing()

修改package

Diffusers 里代码写死了会访问 huggingface,需要手工改掉



import sysconfig; 

def repalceStringInFile(srcStr, dstStr, filePath):
    # Read in the file
    with open(filePath, 'r') as file:
        filedata = file.read()

    # Replace the target string
    filedata = filedata.replace(srcStr, dstStr)

    # Write the file out again
    with open(filePath, 'w') as file:
        file.write(filedata)

sitedir=sysconfig.get_paths()["purelib"]
ckPtFilePath=f"{sitedir}/diffusers/pipelines/stable_diffusion/convert_from_ckpt.py"
print(ckPtFilePath)

srcStr="https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml"
dstStr="http://xxxxxx/github/CompVis_stable-diffusion_main_configs_stable-diffusion_v1-inference.yaml"

repalceStringInFile(srcStr, dstStr, ckPtFilePath)

srcStr="https://raw.githubusercontent.com/Stability-AI/stablediffusion/main/configs/stable-diffusion/v2-inference-v.yaml"
dstStr="http://xxxxxx/github/Stability-AI_stablediffusion_main_configs_stable-diffusion_v2-inference-v.yaml"
repalceStringInFile(srcStr, dstStr, ckPtFilePath)


srcStr="https://raw.githubusercontent.com/Stability-AI/generative-models/main/configs/inference/sd_xl_base.yaml"
dstStr="http://xxxxxx/github/Stability-AI_generative-models_main_configs_inference_sd_xl_base.yaml"
repalceStringInFile(srcStr, dstStr, ckPtFilePath)


srcStr="https://raw.githubusercontent.com/Stability-AI/generative-models/main/configs/inference/sd_xl_refiner.yaml"
dstStr="http://xxxxxx/github/Stability-AI_generative-models_main_configs_inference_sd_xl_refiner.yaml"
repalceStringInFile(srcStr, dstStr, ckPtFilePath)


ckPtFilePath=f"{sitedir}/diffusers/loaders.py"
print(ckPtFilePath)
srcStr="text_encoder=text_encoder,\n            vae=vae,"
dstStr="text_encoder=text_encoder,\n            local_files_only=True,\n            vae=vae,"
repalceStringInFile(srcStr, dstStr, ckPtFilePath)


ckPtFilePath=f"{sitedir}/diffusers/pipelines/stable_diffusion/convert_from_ckpt.py"
print(ckPtFilePath)
srcStr="has_projection=True, **config_kwargs"
dstStr="has_projection=True, local_files_only=local_files_only, **config_kwargs"
repalceStringInFile(srcStr, dstStr, ckPtFilePath)

http://xxxxxx 需要自己下载github里的文件,找地方放,然后替换链接

local_files_only是让取本地缓存,这个缓存在当前用户文件夹下

ubuntu

/home/fxbox/.cache/huggingface/hub

mac

/Users/linzhiji/.cache/huggingface/hub

把能翻墙的机器里的缓存,拷贝到不能翻墙的机器里

参考:

Stable Diffusion 图像生成 攻略三 - 知乎

### 使用 Diffusers 库与 Stable Diffusion #### 安装环境准备 为了使用 `diffusers` 库来操作 Stable Diffusion,首先需要准备好开发环境。可以通过克隆 GitHub 上的官方仓库并设置 Docker 镜像来进行: ```bash git clone https://github.com/huggingface/diffusers.git cd diffusers pip install -e . ``` 这允许更加灵活地查看和修改库中的源码[^1]。 #### 导入必要的组件 接着,在 Python 环境下通过如下命令加载所需的工具包: ```python from diffusers import DiffusionPipeline import torch ``` 这里引入了 `DiffusionPipeline` 类,它提供了创建图像生成流水线的能力,这对于实现扩散过程至关重要[^2]。 #### 加载预训练模型 对于想要快速上手的人来说,可以直接利用 Hugging Face 提供的预训练权重文件。这些文件通常是以 `.ckpt` 或者 `.safetensors` 格式存储,并应被放置于指定路径 `/models/stable-diffusion` 下面的一个特定子目录里[^4]。例如: ```python model_id = "CompVis/stable-diffusion-v1-4" device = "cuda" if torch.cuda.is_available() else "cpu" pipeline = DiffusionPipeline.from_pretrained(model_id).to(device) ``` 这段代码会自动下载并缓存所选版本的 Stable Diffusion 模型到本地机器上。 #### 执行推理任务 一旦完成了上述准备工作之后,就可以开始尝试生成新的图片了。下面是一个简单的例子展示如何输入一段描述性的文字作为提示词(prompt),从而得到一张由 AI 绘制的艺术作品: ```python prompt = "A beautiful landscape painting with mountains and rivers." image = pipeline(prompt).images[0] image.show() ``` 以上就是使用 `diffusers` 库配合 Stable Diffusion 实现图文转换的基本流程介绍。需要注意的是,由于该模型体积较大(约含860M参数量),因此建议在配置有至少10GB显存空间以上的 GPU 设备上来运行此类应用[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值