• python diffusers StableDiffusionXLPipeline 离线使用


      

    下载sd_xl_base_1.0.safetensors

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

    我这下载后放到项目 models 里

    1. model_path = "./models/v1-5-pruned-emaonly.safetensors"
    2. # model_path = "./models/v1-5-pruned.safetensors"
    3. # modelId = "runwayml/stable-diffusion-v1-5"
    4. if Util.isMac():
    5. # from_single_file
    6. pipe = StableDiffusionPipeline.from_single_file(model_path, original_config_file='./models/v1-inference.yaml', cache_dir='./cache/', use_safetensors=True)
    7. pipe = pipe.to("mps")
    8. pipe.enable_attention_slicing()
    9. else:
    10. pipe = StableDiffusionPipeline.from_single_file(model_path, torch_dtype=torch.float16, use_safetensors=True)
    11. pipe.to("cuda")
    12. pipe.enable_model_cpu_offload()
    13. pipe.enable_attention_slicing()

    修改package

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

    1. import sysconfig;
    2. def repalceStringInFile(srcStr, dstStr, filePath):
    3. # Read in the file
    4. with open(filePath, 'r') as file:
    5. filedata = file.read()
    6. # Replace the target string
    7. filedata = filedata.replace(srcStr, dstStr)
    8. # Write the file out again
    9. with open(filePath, 'w') as file:
    10. file.write(filedata)
    11. sitedir=sysconfig.get_paths()["purelib"]
    12. ckPtFilePath=f"{sitedir}/diffusers/pipelines/stable_diffusion/convert_from_ckpt.py"
    13. print(ckPtFilePath)
    14. srcStr="https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml"
    15. dstStr="http://xxxxxx/github/CompVis_stable-diffusion_main_configs_stable-diffusion_v1-inference.yaml"
    16. repalceStringInFile(srcStr, dstStr, ckPtFilePath)
    17. srcStr="https://raw.githubusercontent.com/Stability-AI/stablediffusion/main/configs/stable-diffusion/v2-inference-v.yaml"
    18. dstStr="http://xxxxxx/github/Stability-AI_stablediffusion_main_configs_stable-diffusion_v2-inference-v.yaml"
    19. repalceStringInFile(srcStr, dstStr, ckPtFilePath)
    20. srcStr="https://raw.githubusercontent.com/Stability-AI/generative-models/main/configs/inference/sd_xl_base.yaml"
    21. dstStr="http://xxxxxx/github/Stability-AI_generative-models_main_configs_inference_sd_xl_base.yaml"
    22. repalceStringInFile(srcStr, dstStr, ckPtFilePath)
    23. srcStr="https://raw.githubusercontent.com/Stability-AI/generative-models/main/configs/inference/sd_xl_refiner.yaml"
    24. dstStr="http://xxxxxx/github/Stability-AI_generative-models_main_configs_inference_sd_xl_refiner.yaml"
    25. repalceStringInFile(srcStr, dstStr, ckPtFilePath)
    26. ckPtFilePath=f"{sitedir}/diffusers/loaders.py"
    27. print(ckPtFilePath)
    28. srcStr="text_encoder=text_encoder,\n vae=vae,"
    29. dstStr="text_encoder=text_encoder,\n local_files_only=True,\n vae=vae,"
    30. repalceStringInFile(srcStr, dstStr, ckPtFilePath)
    31. ckPtFilePath=f"{sitedir}/diffusers/pipelines/stable_diffusion/convert_from_ckpt.py"
    32. print(ckPtFilePath)
    33. srcStr="has_projection=True, **config_kwargs"
    34. dstStr="has_projection=True, local_files_only=local_files_only, **config_kwargs"
    35. repalceStringInFile(srcStr, dstStr, ckPtFilePath)

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

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

    ubuntu

    /home/fxbox/.cache/huggingface/hub

    mac

    /Users/linzhiji/.cache/huggingface/hub

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

    参考:

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

  • 相关阅读:
    截取字符串中的部分信息
    线程池核心原理浅析
    2023-10-10 LeetCode每日一题(奖励最顶尖的 K 名学生)
    【K8S】亲和、反亲和、污点、容忍
    Vue2路由知识大杂烩, 一下给你全整明白(上)
    mannose-Biotin|甘露糖-生物素|甘露糖-聚乙二醇-生物素|生物素-PEG-甘露糖
    Springioc注释使用
    docker 容器数据在盘与盘之间迁移
    【Leetcode】1754. Largest Merge Of Two Strings
    异常检测 | MATLAB实现基于支持向量机和孤立森林的数据异常检测(结合t-SNE降维和DBSCAN聚类)
  • 原文地址:https://blog.csdn.net/linzhiji/article/details/132852861