thygate / stable-diffusion-webui-depthmap-script

High Resolution Depth Maps for Stable Diffusion WebUI
MIT License
1.73k stars 159 forks source link

Tiling setting #444

Closed SirNate0 closed 6 months ago

SirNate0 commented 6 months ago

Is it possible to have the script honor the Tiling setting for the image? The image itself seems to tile very nicely. However, the depth image has a distinct curve to it that does not tile. Using a high-pass filter after the fact mostly fixes it, but it is not perfect, leaving a slight seam.

00003-2024-05-29_11-46-56 (best quality), (high quality), stone wall texture, gray 3835511387 00004-2024-05-29_11-47-15 (best quality), (high quality), stone wall texture, gray 3835511387 With high-pass filter (and offset by 1/2 to show the seam) Offset-Krita-Gaussian-High-Pass--00004-2024-05-29_11-47-15 (best quality), (high quality), stone wall texture, gray 3835511387

SirNate0 commented 6 months ago

While not seamless, if I turn off "BOOST (multi-resolution merging)" and turn on "Match net size to input size", the result does not have the broad sloping depth to the corner. depthmap-0004-offset

SirNate0 commented 6 months ago

To actually make it seamless, it's possible to use the same approach as sd_hijack.py and simply force all the Conv2d layers to have the circular padding mode. E.g. adding this to load_models() in depthmap_generation.py, though I've only tested it on the res101 model (and it failed with BOOST enabled - probably have to make the same adjustment to the BOOST model, though it might be more complicated since I think it is breaking the image into different patches...)

    def apply_circular(enable):
        def flatten(el):
            flattened = [flatten(children) for children in el.children()]
            res = [el]
            for c in flattened:
                res += c
            return res
        layers = flatten(model)
        for layer in [layer for layer in layers if type(layer) == torch.nn.Conv2d]:
            layer.padding_mode = 'circular' if enable else 'zeros'
    apply_circular(True)

forced-tiling-offset

semjon00 commented 6 months ago

Wow, this is impressive! I didn't even think of that, it indeed works without finetuning! A niche but very cool and creative usecase. I am adding this now.

semjon00 commented 6 months ago

I wrote code that adds a new checkbox for the tiling mode, but my results are somewhat lacking...

semjon00 commented 6 months ago

Ok, this feature has just hit the main branch. It still has some seams... It would be awesome if you could take a look (investigate 😄) and identify the reason...

semjon00 commented 6 months ago

@SirNate0