快訊!我的新書登上天瓏網路書店11月份暢銷榜第一名啦!看過的都說讚!歡迎大家前往訂購!
>>>> AI 職場超神助手:ChatGPT 與生成式 AI 一鍵搞定工作難題 <<<<

ControlNet使用的錯誤排解

無法載入ControlNet

錯誤訊息如下:

*** Error loading script: face_manipulation_extras.py
Traceback (most recent call last):
File “C:\Users\clairechang\stable-diffusion-webui-master\modules\scripts.py”, line 515, in load_scripts
script_module = script_loading.load_module(scriptfile.path)
File “C:\Users\clairechang\stable-diffusion-webui-master\modules\script_loading.py”, line 13, in load_module
module_spec.loader.exec_module(module)
File “”, line 883, in exec_module
File “”, line 241, in call_with_frames_removed File “C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-face-manipulation-extras\scripts\face_manipulation_extras.py”, line 4, in from face_manipulation.main import process, alignImage File “C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-face-manipulation-extras\face_manipulation\main.py”, line 6, in from face_manipulation.zerodim.network.training import Model File “C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-face-manipulation-extras\face_manipulation\zerodim\network\training.py”, line 18, in from face_manipulation.zerodim.network.modules import BetaVAEGenerator, BetaVAEEncoder File “C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-face-manipulation-extras\face_manipulation\zerodim\network\modules.py”, line 10, in from face_manipulation.zerodim.stylegan2_pytorch.model import ConstantInput, ToRGB, ModulatedConv2d, FusedLeakyReLU File “C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-face-manipulation-extras\face_manipulation\zerodim\stylegan2_pytorch\model.py”, line 11, in from face_manipulation.zerodim.stylegan2_pytorch.op import FusedLeakyReLU, fused_leaky_relu, upfirdn2d, conv2d_gradfix File “C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-face-manipulation-extras\face_manipulation\zerodim\stylegan2_pytorch\op__init_.py”, line 1, in
from .fused_act import FusedLeakyReLU, fused_leaky_relu
File “C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-face-manipulation-extras\face_manipulation\zerodim\stylegan2_pytorch\op\fused_act.py”, line 19, in
fused = load(
File “C:\Users\clairechang\AppData\Local\Programs\Python\Python310\myenv\lib\site-packages\torch\utils\cpp_extension.py”, line 1308, in load
return _jit_compile(
File “C:\Users\clairechang\AppData\Local\Programs\Python\Python310\myenv\lib\site-packages\torch\utils\cpp_extension.py”, line 1710, in _jit_compile
_write_ninja_file_and_build_library(
File “C:\Users\clairechang\AppData\Local\Programs\Python\Python310\myenv\lib\site-packages\torch\utils\cpp_extension.py”, line 1823, in _write_ninja_file_and_build_library
_run_ninja_build(
File “C:\Users\clairechang\AppData\Local\Programs\Python\Python310\myenv\lib\site-packages\torch\utils\cpp_extension.py”, line 2077, in _run_ninja_build
vc_env = distutils._msvccompiler._get_vc_env(plat_spec)
File “C:\Users\clairechang\AppData\Local\Programs\Python\Python310\myenv\lib\site-packages\setuptools\msvc.py”, line 230, in msvc14_get_vc_env
return _msvc14_get_vc_env(plat_spec)
File “C:\Users\clairechang\AppData\Local\Programs\Python\Python310\myenv\lib\site-packages\setuptools\msvc.py”, line 187, in _msvc14_get_vc_env
raise distutils.errors.DistutilsPlatformError(“Unable to find vcvarsall.bat”)
distutils.errors.DistutilsPlatformError: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build Tools”: https://visualstudio.microsoft.com/visual-cpp-build-tools/

這個錯誤訊息顯示在運行 face_manipulation_extras.py 腳本時,遇到了一個與編譯 C++ 擴展模組相關的問題,具體來說是缺少 Microsoft Visual C++ 編譯工具。解決這個問題的方法如下:

  1. 安裝 Microsoft C++ Build Tools:前往 Microsoft Visual C++ Build Tools 的官方網站,下載並安裝最新版本的 C++ 編譯工具。這是你系統編譯某些擴展模組所必需的。
  2. 設定 Visual Studio 的環境變數:安裝完成後,必須確保 vcvarsall.bat 已正確配置到你的系統環境變數中,通常這是自動處理的。你可以嘗試重新啟動你的終端機或電腦來使變更生效。

模型版本不相容

錯誤訊息如下:

*** Error running process: C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-controlnet\scripts\controlnet.py
Traceback (most recent call last):
File “C:\Users\clairechang\stable-diffusion-webui-master\modules\scripts.py”, line 832, in process
script.process(p, *script_args)
File “C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-controlnet\scripts\controlnet.py”, line 1228, in process
self.controlnet_hack(p)
File “C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-controlnet\scripts\controlnet.py”, line 1213, in controlnet_hack
self.controlnet_main_entry(p)
File “C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-controlnet\scripts\controlnet.py”, line 898, in controlnet_main_entry
Script.check_sd_version_compatible(unit)
File “C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-controlnet\scripts\controlnet.py”, line 827, in check_sd_version_compatible
raise Exception(f”ControlNet model {unit.model}({cnet_sd_version}) is not compatible with sd model({sd_version})”)
Exception: ControlNet model control_v11p_sd15_inpaint ebff9138 is not compatible with sd model(StableDiffusionVersion.SDXL)

所選擇的模型與ControlNet的模型不相容。可以確認一下所選擇的 Stable Diffusion 1.x 版本的模型(例如 v1-5-pruned.ckpt 或類似名稱的模型)和ControlNet的版本是否有對應。

關於版本,我們可以根據CivitAI網站去找到適合你的版本的模型

然後關於ControlNet的模型,

SD2.X可在此下載: https://huggingface.co/thibaud/controlnet-sd21/tree/main

SD1.X可在此下載: https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/tree/main

將下載的 .ckpt 模型文件放入 Stable Diffusion WebUI 的 models/Stable-diffusion 資料夾中。通常你按了Control Type的按鈕之後,Model應該要自動跳到相對應的模型,如果按了Control Type後Model還是空的,很大的可能你所抓的模型的版本不一致,所以web-ui才會抓不到。

尺寸不一樣的錯誤

*** Error running process: C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-controlnet\scripts\controlnet.py
Traceback (most recent call last):
File “C:\Users\clairechang\stable-diffusion-webui-master\modules\scripts.py”, line 832, in process
script.process(p, *script_args)
File “C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-controlnet\scripts\controlnet.py”, line 1228, in process
self.controlnet_hack(p)
File “C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-controlnet\scripts\controlnet.py”, line 1213, in controlnet_hack
self.controlnet_main_entry(p)
File “C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-controlnet\scripts\controlnet.py”, line 919, in controlnet_main_entry
model_net, control_model_type = Script.load_control_model(p, unet, unit.model)
File “C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-controlnet\scripts\controlnet.py”, line 436, in load_control_model
control_model = Script.build_control_model(p, unet, model)
File “C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-controlnet\scripts\controlnet.py”, line 465, in build_control_model
control_model = build_model_by_guess(state_dict, unet, model_path)
File “C:\Users\clairechang\stable-diffusion-webui-master\extensions\sd-webui-controlnet\scripts\controlnet_model_guess.py”, line 218, in build_model_by_guess
p_new = p + unet_state_dict[key].clone().cpu()
RuntimeError: The size of tensor a (1024) must match the size of tensor b (768) at non-singleton dimension 1

此錯誤可能是由於所使用的 ControlNet 模型與你正在運行的 Stable Diffusion 模型之間的尺寸不匹配。Stable Diffusion 1.x 模型的特徵圖通常是 768×768,而 SDXL(Stable Diffusion 2.x)模型的特徵圖大小通常是 1024×1024。因此,如果你在 SDXL 模型中使用了為 Stable Diffusion 1.x 設計的 ControlNet 模型,會導致張量尺寸不匹配。


17年資歷女工程師,專精於動畫、影像辨識以及即時串流程式開發。經常組織活動,邀請優秀的女性分享她們的技術專長,並在眾多場合分享自己的技術知識,也活躍於非營利組織,辦理活動來支持特殊兒及其家庭。期待用技術改變世界。

如果你認同我或想支持我的努力,歡迎請我喝一杯咖啡!讓我更有動力分享知識!