Stable Diffusion ONNX修复管道固定输出512*512图像问题求助
问题原因与解决方案
你遇到的问题核心是:OnnxStableDiffusionInpaintPipeline默认会输出模型训练时的512x512尺寸,即便你调整了输入图像和mask的大小,如果不在管道调用时显式指定输出尺寸,管道会忽略输入尺寸,沿用默认值。
具体修复步骤:
- 调用
pipe时必须显式传入height和width参数,对应你设定的目标分辨率 - 确保传入的尺寸是64的整数倍(Stable Diffusion模型下采样机制要求,否则会报错或输出异常),你用的576和384刚好符合要求
- 补上你定义但未使用的
negative参数,取消注释strength参数(inpainting需要这个参数控制重绘强度)
修改后的inpaint_with_prompt方法代码:
def inpaint_with_prompt( self, image: cv2.typing.MatLike | Image.Image, mask: cv2.typing.MatLike | Image.Image, height: int, width: int, prompt: str = '', negative: str = '', steps: int = 10, cfg: float = 7.5, noise: float = 0.75 ): pipe = self.pipe_inpaint image = image.resize((width, height)) mask = mask.resize((width, height)) output_image = pipe( prompt, image, mask, height=height, # 指定输出高度 width=width, # 指定输出宽度 strength=noise, # 启用重绘强度控制 guidance_scale=cfg, negative_prompt=negative, # 传入负面提示词 num_inference_steps=steps # 指定推理步数 ) return output_image
额外注意点:
如果后续需要使用非64倍数的尺寸,可先通过以下函数转换:
def round_to_multiple_of_64(n): return ((n + 63) // 64) * 64
将height和width转换成64的倍数后再传入管道。
内容的提问来源于stack exchange,提问作者cudnn_account
相关产品推荐
相关产品推荐

