2026/4/6 2:05:46
网站建设
项目流程
在线网站建设询问报价,网店代运营销售,青海公路建设市场信用信息服务网站,seo快速收录快速排名文章目录【记录】LLM#xff5c;魔搭社区加载本地模型 Qwen-Image-Layered前言一、准备环境二、在线加载#xff08;网络可用时#xff09;三、常见报错与解决四、运行效果结语【记录】LLM#xff5c;魔搭社区加载本地模型 Qwen-Image-Layered 运行环境#xff1a; CPU魔搭社区加载本地模型 Qwen-Image-Layered前言一、准备环境二、在线加载网络可用时三、常见报错与解决四、运行效果结语【记录】LLM魔搭社区加载本地模型 Qwen-Image-Layered运行环境CPUIntel® Xeon® Gold 6133 CPU 2.50GHzGPUNVIDIA GeForce RTX 4090系统Ubuntu 24.04.2 LTS成文验证时间2026/01/07若之后接口更新本方法无法使用请在评论区留言我看到之后可能会更新新版。本文主要适用于 Linux 场景但 Windows 场景或 MacOS 也只要略微调整终端指令改动不大也可以参考本文。对应的模型Qwen-Image-Layered · 模型库若无特殊指出本文中提到的指令都是在终端直接运行的。前言网上有不少用 HuggingFaceEmbeddings 的示例但那是“文本嵌入模型”的用法Qwen-Image-Layered 是一个“图像分层生成/分解”的扩散模型应该用 diffusers 的专用 Pipeline 来加载。如果你像我一样误入歧途跑去 HF 页面逐个下载文件、或者用 Embedding 类去加载它十有八九会报错例如 Unrecognized model 或 peft 版本过低等。下面给出在 Linux 下“从魔搭社区下载到本地然后离线加载”的最稳妥流程并补充网络受限/限流时的镜像与 Token 方案。性能提示该模型的生成速度相对较慢运行时间较长。在 RTX Pro 6000 96GB VRAM 上峰值显存占用可以达到 45GB, 1024 px 生成时间需要 120s。根据一些 RTX 4090 用户的反馈该工作流几乎会占满所有显存。建议显存较小的用户使用 FP8 版本以降低显存占用。参考Qwen-Image-Layered ComfyUI 工作流使用指南 | ComfyUI WikiFP8 版本指路Qwen-Image-Layered ComfyUI 工作流使用指南 | ComfyUI Wiki。如果你和我一样不知道怎么用 fp8 版本那你可以不用然后继续往下看。一、准备环境建议使用独立虚拟环境可选python -m venv ~/.venvs/qwen-imgsource~/.venvs/qwen-img/bin/activate python -V# 建议 Python 3.12安装依赖含关键版本约束避免已知兼容性问题首先跟着这篇博客去安装 pytorch【安装】PyTorch查看并调整 Cuda 版本以适应 PyTorch 安装的指南_cuda11.2 能装 11.1 的 pytorch 吗-CSDN 博客安装好之后安装以下依赖# 步骤 1:安装所需包pipinstall-U pip pipinstalltransformers4.57.3 pipinstallgithttps://github.com/huggingface/diffusers pipinstallpython-pptx torch pillow pipinstall-Uaccelerate0.26.0\diffusers0.30.0huggingface_hub0.23.0peft0.17.0Pillow pipinstallpsd-tools# 步骤 2:验证 CUDA 可用性(GPU 用户)python -cimport torch; print(torch.cuda.is_available())说明peft0.17.0 很关键否则 diffusers 初始化会直接报错你可能见过 peft0.15.1 的报错。有 NVIDIA GPU 时建议安装与你 CUDA 匹配的 PyTorch 轮子以便使用 GPU bfloat16/float16 推理。二、在线加载网络可用时如果你倾向于在线拉取首次下载后续走本地缓存建议配置镜像与 Token降低 429 限流概率。配置镜像与 Token可选但强烈建议# 镜像国内常用exportHF_ENDPOINThttps://hf-mirror.com# Token在 Hugging Face 个人设置中创建 Read 权限 TokenexportHF_TOKENhf_xxx_your_token_hereToken 获取方式参考这篇博客 镜像站也能被限流用 Unsloth 踩坑实录加个 Hugging Face Token下载速度直接起飞_to continue using our service, create a hf account-CSDN 博客代码中登录或显式传入 Token两种选其一fromhuggingface_hubimportlogin login(tokenhf_xxx_your_token_here)或pipeQwenImageLayeredPipeline.from_pretrained(Qwen/Qwen-Image-Layered,tokenhf_xxx_your_token_here,cache_dir./hf_cache# 指定本地缓存目录)常见 429Too Many Requests限流处理一定要使用 Token匿名访问配额极低。降低并发下载数量如首次拉取时不要并行开太多进程/线程。命中 429 时遵循返回的 Retry-After 再重试。下载成功后会走本地缓存后续不再重复请求。4完整代码fromdiffusersimportQwenImageLayeredPipelineimporttorchfromPILimportImagedefpick_best_gpu():best_i,best_free0,-1foriinrange(torch.cuda.device_count()):torch.cuda.set_device(i)free,totaltorch.cuda.mem_get_info()iffreebest_free:best_i,best_freei,freereturnbest_i gpu_idxpick_best_gpu()devicetorch.device(fcuda:{gpu_idx})pipelineQwenImageLayeredPipeline.from_pretrained(Qwen/Qwen-Image-Layered)pipelinepipeline.to(cuda,torch.bfloat16)pipeline.set_progress_bar_config(disableNone)imageImage.open(test.jpg).convert(RGBA)inputs{image:image,generator:torch.Generator(devicedevice).manual_seed(777),true_cfg_scale:4.0,negative_prompt: ,num_inference_steps:50,num_images_per_prompt:1,layers:4,resolution:640,# Using different bucket (640, 1024) to determine the resolution. For this version, 640 is recommendedcfg_normalize:True,# Whether enable cfg normalization.use_en_prompt:True,# Automatic caption language if user does not provide caption}withtorch.inference_mode():outputpipeline(**inputs)output_imageoutput.images[0]fori,imageinenumerate(output_image):image.save(f{i}.png)如果你的显存不足的话可以开均衡模式这样子的话单张显卡不会爆掉fromdiffusersimportQwenImageLayeredPipelineimporttorchfromPILimportImagefromaccelerateimportAccelerator# 1. 移除手动选择 GPU 的代码# 2. 使用 device_mapbalanced 自动分配显存# balanced 策略会尽量均匀分配auto 策略可能会优先填满第一张pipelineQwenImageLayeredPipeline.from_pretrained(Qwen/Qwen-Image-Layered,torch_dtypetorch.bfloat16,device_mapbalanced,# 关键参数自动切分模型到所有可用 GPU# 如果模型不支持直接 device_map请参考下文的“备选方案”)# 注意使用了 device_map 后不要再调用 pipeline.to(cuda)否则会破坏分配# 3. 启用显存优化可选如果显存依然紧张# pipeline.enable_model_cpu_offload() # 单卡跑大模型时用这个# pipeline.enable_vae_slicing() # 降低 VAE 解码时的显存imageImage.open(test.jpg).convert(RGBA)inputs{image:image,# generator 不需要手动指定 device或者指定为 cuda 即可pipeline 会自动处理generator:torch.Generator(devicecuda).manual_seed(777),true_cfg_scale:4.0,negative_prompt: ,num_inference_steps:50,num_images_per_prompt:1,layers:4,resolution:1024,cfg_normalize:True,use_en_prompt:True,}withtorch.inference_mode():outputpipeline(**inputs)output_imageoutput.images[0]fori,imginenumerate(output_image):img.save(f{i}.png)三、常见报错与解决报错ImportError: peft0.17.0 is required … but found peft0.15.1解决升级 peftpipinstall-Upeft0.17.0报错429 Client Error: Too Many Requests尤其使用 hf-mirror.com 时解决设置 Token并尽量减少并发必要时等待片刻再试。exportHF_ENDPOINThttps://hf-mirror.comexportHF_TOKENhf_xxx_your_token_here也可在 from_pretrained 里传入 token… 或使用 huggingface_hub.login。报错Cannot load model … model is not cached locally and an error occurred while trying to fetch metadata解决要么在离线场景下用 local_files_onlyTrue 并确保本地目录完整要么联网Token 让 from_pretrained 能拉取元数据。报错torch.OutOfMemoryError: CUDA out of memory. Tried to allocate 108.00 MiB. GPU 0 has a total capacity of 23.55 GiB of which 56.56 MiB is free. Including non-PyTorch memory, this process has 23.45 GiB memory in use. Of the allocated memory 22.88 GiB is allocated by PyTorch, and 198.48 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONFexpandable_segments:True to avoid fragmentation. See documentation for Memory Management (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)解决必须换一个大的显卡。该模型的生成速度相对较慢运行时间较长。在 RTX Pro 6000 96GB VRAM 上峰值显存占用可以达到45GB,1024 px 生成时间需要 120s。根据一些 RTX 4090 用户的反馈该工作流几乎会占满所有显存。建议显存较小的用户使用 FP8 版本以降低显存占用。或者请用我这篇文章中提到的均衡模式的代码。报错Could not import module ‘Qwen2_5_VLForConditionalGeneration’. Are this object’s requirements defined correctly?解决很可能是因为 pytorch 的版本和 pytorch vision 的版本不一致参考这篇博客安装一下【安装】PyTorch查看并调整 Cuda 版本以适应 PyTorch 安装的指南_cuda11.2 能装 11.1 的 pytorch 吗-CSDN 博客结果异常输出不是多张 RGBA 图层检查输入是否已 convert(“RGBA”)确保使用了正确的 PipelineQwenImageLayeredPipeline且模型目录完整。四、运行效果这里选了别人发给我的一张图原图就不公开了。是一个手账类型的图。设置 640 像素的时候效果是这样马赛克是我自己打的因为我不想有什么粉丝争端。可以看到文字背景分离的不是很好然后图片也有一点模糊大概跑了 23 分钟设置 1024 像素的时候效果是这样清楚很多效果也很好跑了 39 分钟结语Qwen-Image-Layered 是“图像分层”的扩散模型用 diffusers 的 QwenImageLayeredPipeline 加载最合适。“魔搭社区加载本地模型”的关键在于先把完整模型目录落到本地包含 model_index.json 等然后 from_pretrained 指向该目录并加上 local_files_onlyTrue。网络可用时配合镜像与 HF_TOKEN 在线拉取成功后走缓存网络不佳或需离线时直接走本地目录。如果你在不同显卡/不同版本组合下遇到其他问题欢迎在评论区反馈。本账号所有文章均为原创欢迎转载请注明文章出处https://shandianchengzi.blog.csdn.net/article/details/156690977。百度和各类采集站皆不可信搜索请谨慎鉴别。技术类文章一般都有时效性本人习惯不定期对自己的博文进行修正和更新因此请访问出处以查看本文的最新版本。