diffusers icon indicating copy to clipboard operation
diffusers copied to clipboard

Image output tiling for seamless textures with Stable Diffusion

Open torrinworx opened this issue 3 years ago • 90 comments

Is your feature request related to a problem? Please describe. Currently there is no way to create seamless textures with Stable Diffusion, a crucial feature that is missing.

Describe the solution you'd like Something similar to this pull on the sd-webui repo: https://github.com/sd-webui/stable-diffusion-webui/pull/911

A simple argument in the StableDiffusionPipeline that would enable seamless texture generation for 3D applications.

torrinworx avatar Sep 19 '22 02:09 torrinworx

Hi @torrinworx! As the https://github.com/sd-webui/stable-diffusion-webui/pull/911 PR suggests, you can make the Stable Diffusion models tile-able by patching the torch.nn.Conv2d before loading the pipeline:

# add global options to models
def patch_conv(**patch):
    cls = torch.nn.Conv2d
    init = cls.__init__
    def __init__(self, *args, **kwargs):
        return init(self, *args, **kwargs, **patch)
    cls.__init__ = __init__

patch_conv(padding_mode='circular')
print("patched for tiling")

Native support for tiling in diffusers is unlikely to come, as it would either be hacky, or complicate the model the model design with additional arguments :)

anton-l avatar Sep 21 '22 14:09 anton-l

FYI: In my app, I toggle to be tile-able or not after pipe loading by change padding_mode in each layer.

  1. First, save conv layers and their original padding modes
  2. Change padding mode on each generation

shirayu avatar Sep 21 '22 16:09 shirayu

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

github-actions[bot] avatar Oct 19 '22 15:10 github-actions[bot]

Could we make this a community pipeline maybe? :-)

patrickvonplaten avatar Oct 20 '22 17:10 patrickvonplaten

is it possible for flax maybe like this: https://flax.readthedocs.io/en/latest/api_reference/_autosummary/flax.linen.Conv.html?highlight=circular

update: I test it working 🎉 3

camenduru avatar Oct 20 '22 17:10 camenduru

Hi @torrinworx! As the sd-webui/stable-diffusion-webui#911 PR suggests, you can make the Stable Diffusion models tile-able by patching the torch.nn.Conv2d before loading the pipeline:

# add global options to models
def patch_conv(**patch):
    cls = torch.nn.Conv2d
    init = cls.__init__
    def __init__(self, *args, **kwargs):
        return init(self, *args, **kwargs, **patch)
    cls.__init__ = __init__

patch_conv(padding_mode='circular')
print("patched for tiling")

Native support for tiling in diffusers is unlikely to come, as it would either be hacky, or complicate the model the model design with additional arguments :)

hello, i follow your advice , add this snippet before loading the pipeline, but there is an error when loading pipeline:

pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
    model_path,
    revision="fp16", 
    torch_dtype=torch.float16,
    use_auth_token=True
)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
[<ipython-input-29-796abcd4f547>](https://localhost:8080/#) in <module>
      6     revision="fp16",
      7     torch_dtype=torch.float16,
----> 8     use_auth_token=True
      9 )
     10 pipe = pipe.to(device)

8 frames
[<ipython-input-19-17c9e93293f0>](https://localhost:8080/#) in __init__(self, *args, **kwargs)
      4     init = cls.__init__
      5     def __init__(self, *args, **kwargs):
----> 6         return init(self, *args, **kwargs, **patch)
      7     cls.__init__ = __init__
      8 

TypeError: __init__() got multiple values for keyword argument 'padding_mode'

can you tell me why?

uyo9ko avatar Oct 23 '22 03:10 uyo9ko

@anton-l, I think we can allow to adapt the conv mode with a nice diffusers API here no?

patrickvonplaten avatar Nov 12 '22 23:11 patrickvonplaten

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

github-actions[bot] avatar Dec 07 '22 15:12 github-actions[bot]

Linking some first PRs here:

  • https://github.com/huggingface/diffusers/pull/1521
  • https://github.com/huggingface/diffusers/pull/1441

patrickvonplaten avatar Dec 12 '22 11:12 patrickvonplaten

cc @patil-suraj @anton-l does any of you have time to look into tiling?

patrickvonplaten avatar Dec 12 '22 11:12 patrickvonplaten

Note this must be applied to both the diffusion model and the decoder (VAE).

keturn avatar Dec 28 '22 19:12 keturn

That's the plan @keturn, still experimenting with it.

patil-suraj avatar Dec 29 '22 11:12 patil-suraj

begone, stalebot!

keturn avatar Jan 22 '23 19:01 keturn

Ok it does not look like @patil-suraj you'll have time for this anytime soon no? Is someone else maybe interested in picking this one up (might be a bit difficult as a first PR though): @pcuenca @williamberman @yiyixuxu maybe?

patrickvonplaten avatar Jan 22 '23 20:01 patrickvonplaten

up

camenduru avatar Jan 23 '23 00:01 camenduru

I don't have much bandwidth for it this week, but I will try to look into https://github.com/huggingface/diffusers/pull/1521 next week

patil-suraj avatar Jan 25 '23 13:01 patil-suraj

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

github-actions[bot] avatar Feb 19 '23 15:02 github-actions[bot]

it's still an open issue, stalebot

keturn avatar Feb 22 '23 22:02 keturn

This seems like an interesting add-on to the WebUI: https://github.com/tjm35/asymmetric-tiling-sd-webui Any insight on how to add X and Y padding into the current diffusers implementation?

AmanKishore avatar Feb 24 '23 21:02 AmanKishore

Since we have this: https://github.com/huggingface/diffusers/pull/1441 added now think we can close this one no?

patrickvonplaten avatar Mar 06 '23 18:03 patrickvonplaten

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

github-actions[bot] avatar Mar 31 '23 15:03 github-actions[bot]

I’m interested in this as well. Is there at least a workable hack? Then we can look into making into usable code.

jtoy avatar Jun 02 '23 13:06 jtoy

I’m interested in this as well. Is there at least a workable hack? Then we can look into making into usable code.

Implementation in colab notebook: (Inspect the setup cell and copy paste) https://colab.research.google.com/drive/1E5Fa2Tu04g3kb443WnrhbWNhoMzcijoj?authuser=3#scrollTo=Z_pZ9zpFJvbY

shamelesslyAI avatar Jun 02 '23 18:06 shamelesslyAI

@patrickvonplaten I think this one was closed prematurely - this is to create seamless, tiling outputs - not tiled VAE decode. Can we re-open?

psychedelicious avatar Jul 06 '23 12:07 psychedelicious

Sure!

patrickvonplaten avatar Jul 12 '23 18:07 patrickvonplaten

this is to create seamless, tiling outputs

Would also be super interested in this one!

pietrobolcato avatar Jul 14 '23 00:07 pietrobolcato

is there any progress about creating seamless, tiling outputs in diffusers? @patrickvonplaten

icech avatar Aug 04 '23 05:08 icech

Are there any pointers of a working implementation?

patrickvonplaten avatar Aug 04 '23 10:08 patrickvonplaten

Automatic1111 had has it for a long time. — Sent from my mobileOn Aug 4, 2023, at 5:47 AM, Patrick von Platen @.***> wrote: Are there any pointers of a working implementation?

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: @.***>

jtoy avatar Aug 04 '23 11:08 jtoy

Are there any pointers of a working implementation?

I want to know if it's possible to implement a tiled mode control flag, just like enable_vae_tiled(), so that everyone can use the tiled mode more conveniently. Shirayu provided a feasible approach, but it's not elegant enough. Link to the discussion

icech avatar Aug 04 '23 11:08 icech