diffusers icon indicating copy to clipboard operation
diffusers copied to clipboard

StableDiffusionInpaintPipeline - unassigned variable bug when inferencing with multiple images and masks

Open zxydi1992 opened this issue 3 years ago • 0 comments

Describe the bug

The StableDiffusionInpaintPipeline seems to support torch.Tensor as inputs. However, the code contains a bug that made the inference fail.

The bug is located at line 654 and line 675 in pipeline_stable_diffusion_inpaint.py

Error message:

Traceback (most recent call last): File "/opt/conda/envs/diffusers/lib/python3.8/site-packages/torch/multiprocessing/spawn.py", line 69, in _wrap fn(i, *args) File "/home/XXXXX/sd_inpaint.py", line 142, in main inpainted = pipe(prompt=[prompt] * len(images), image=images, mask_image=masks).images File "/opt/conda/envs/diffusers/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context return func(*args, **kwargs) File "/opt/conda/envs/diffusers/lib/python3.8/site-packages/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py", line 629, in call mask, UnboundLocalError: local variable 'mask' referenced before assignment

Reasoning: In line 654, the code does not define 'mask' variable if the input type is not torch.Tensor; while at line 675, mask is used in the arguments to the function call.

Besides, line 678 should change to num_images_per_prompt, because mask is already in batch form, the piece mask = mask.repeat(batch_size, 1, 1, 1) in line 538 would make the output mask longer than we need in the first axis.

Suggest fix: since prepare_mask_and_masked_image function already supports torch.Tensor as inputs, we can remove the if statement in line 654.

Reproduction

No response

Logs

No response

System Info

diffusers==0.8.1 python=3.8.5

zxydi1992 avatar Nov 29 '22 00:11 zxydi1992