RuntimeError: Error(s) in loading state_dict for BrushNetModel — mismatch between model definition and weights
📝 Issue Body:
I'm trying to run the app.py interface and load the weights for PowerPaint_BrushnetE, but I'm encountering the following error when attempting to load the weights:
RuntimeError: Error(s) in loading state_dict for BrushNetModel: Unexpected key(s) in state_dict: "brushnet_down_blocks.0.bias", "brushnet_down_blocks.0.weight", ... ... 🔍 Details: Model repo: zhuangjunhao/PowerPaint_v2
File attempting to load: (downloaded via OpenXLab or HuggingFace mirror)PowerPaint_BrushnetE
Script used: from the repoapp.py
Error stage: During when trying to controller.load_weight()model.load_state_dict(...)
Environment:
Python 3.10
torch 2.x (please fill exact version)
diffusers, , and other dependencies as defined in the repotransformers
你好,请问解决了吗
I solved this problem with "pip install safetensors<0.5"
解决了, 是 权重类型不匹配导致, from safetensors.torch import load_model,load_file unet = UNet2DConditionModel.from_pretrained( os.path.join(self.checkpoint_dir, "runwayml_stable-diffusion-v1-5"), subfolder="unet", revision=None, torch_dtype=weight_dtype, local_files_only=local_files_only, ) 加载的时候权重类型是torch.float16,但是PowerPaint_Brushnet/diffusion_pytorch_model.safetensors 原始类型是torch.float32(有一个key对应的是torch.float16),load_model()函数会检查模型与权重对应的值类型是否一样,导致加载错误。 以下代码可以正常加载(原理是内部执行自动类型转换加载) brushnet_weights_path = os.path.join( checkpoint_dir, "PowerPaint_Brushnet/diffusion_pytorch_model.safetensors", ) weights = load_file(brushnet_weights_path) self.pipe.brushnet.load_state_dict(weights, strict=True)
解决了,谢谢!
解决了, 是 权重类型不匹配导致, from safetensors.torch import load_model,load_file unet = UNet2DConditionModel.from_pretrained( os.path.join(self.checkpoint_dir, "runwayml_stable-diffusion-v1-5"), subfolder="unet", revision=None, torch_dtype=weight_dtype, local_files_only=local_files_only, ) 加载的时候权重类型是torch.float16,但是PowerPaint_Brushnet/diffusion_pytorch_model.safetensors 原始类型是torch.float32(有一个key对应的是torch.float16),load_model()函数会检查模型与权重对应的值类型是否一样,导致加载错误。 以下代码可以正常加载(原理是内部执行自动类型转换加载) brushnet_weights_path = os.path.join( checkpoint_dir, "PowerPaint_Brushnet/diffusion_pytorch_model.safetensors", ) weights = load_file(brushnet_weights_path) self.pipe.brushnet.load_state_dict(weights, strict=True)
想问runwayml_stable-diffusion-v1-5是从哪里下载的?
https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-v1-5/tree/main/unet
elf.pipe.brushnet.load_state_dict
请问大佬这块是哪里修改的?app.py文件的191行吗?
elf.pipe.brushnet.load_state_dict
请问大佬这块是哪里修改的?app.py文件的191行吗?
188-191 加载PowerPaint_Brushnet/diffusion_pytorch_model.safetensors时候
elf.pipe.brushnet.load_state_dict
请问大佬这块是哪里修改的?app.py文件的191行吗?
188-191 加载PowerPaint_Brushnet/diffusion_pytorch_model.safetensors时候
好的,谢谢,已跑通,不过我是在远程服务器搭建的,目前上传图片,点击Run。右侧结果会出现Error。。。请问您见过这种情况吗?
elf.pipe.brushnet.load_state_dict
请问大佬这块是哪里修改的?app.py文件的191行吗?
188-191 加载PowerPaint_Brushnet/diffusion_pytorch_model.safetensors时候
好的,谢谢,已跑通,不过我是在远程服务器搭建的,目前上传图片,点击Run。右侧结果会出现Error。。。请问您见过这种情况吗?
你看看终端错误是什么,cuda不足或者其他的问题,web端看不到具体的错误
貌似就是一些警告,然后后台有个进度条在跑,不过此时页面已经出现error。
貌似就是一些警告,然后后台有个进度条在跑,不过此时页面已经出现error。 那就是gradio接口显示问题,图片应该已经在lunix服务器内,跟gpt交互一下让他检查下,我之前跑的是没问题的
嗯嗯,我觉得应该是显示问题。我把step设置为1,后台处理完,前端UI能显示。所以如果处理时间过长,结果没出来,前端UI就出现error,感觉是一个等待时间超时了。再打扰下仁兄,1)我发现上传的图片分辨率变了,不清楚是否是等比例resize;2)另外就是linux服务器处理的图片在哪里?我搜了项目目录里是没有的。
- 分辨率变化貌似长宽必须是64的倍数,否则会强制resize,你可以生成之后resize成原尺寸。2. 抱歉,代码并没有存储结果到本地。def predict( 内的 result = self.pipe( promptA=promptA, promptB=promptB, promptU=prompt, tradoff=fitting_degree, tradoff_nag=fitting_degree, image=input_image["image"].convert("RGB"), mask=input_image["mask"].convert("RGB"), num_inference_steps=ddim_steps, generator=torch.Generator("cuda").manual_seed(seed), brushnet_conditioning_scale=1.0, negative_promptA=negative_promptA, negative_promptB=negative_promptB, negative_promptU=negative_prompt, guidance_scale=scale, width=H, height=W, ).images[0] 是结果,保存一下就行 image.save(save_path) 可打印type(result)看其type然后对应代码存储。 3. 另外生成的图片在具体细节纹理部分跟原图可能有一点点差异。
恩恩,非常感谢。用它搞一些去雾试了试效果,初步还可以。不过再调下参数试试。主要是用object removal inpainting这个功能,step肯定是越多质量越好,Guidance Scale和seed还没找出什么规律,感觉调了几个参数都差不多。。。
貌似就是一些警告,然后后台有个进度条在跑,不过此时页面已经出现error。
那就是gradio接口显示问题,图片应该已经在lunix服务器内,跟gpt交互一下让他检查下,我之前跑的是没问题的