Skip to content

Conversation

EvgenyKashin
Copy link
Contributor

Artifact-Free Panorama Generation with circular padding

This pull request addresses the issue mentioned in #4024. The proposed solution aims to achieve artifact-free panorama generation by seamlessly aligning the content on the left and right sides of the generated image, creating a proper “panorama”, viewable in 360 panoramas viewers.

In the existing MultiDiffusion approach, the image is cropped from left to right with a specified stride. To enable circular padding and support the desired 360-degree scenario, this PR introduces additional crops after the rightmost point of the image. Circular padding allows the model to “see” the transition from the rightmost part of the image to the leftmost part, maintaining consistency in 360-sense.

Furthermore, modifications are made to the decoding of latents in the case of StableDiffusion. Without these modifications, the decoded latents would not match in the RGB space, even if they align well in the latent space. Thus, the decoding of latents also incorporates circular padding to ensure consistent results.

A panorama image generated without circular padding: image

A panorama image generated with circular padding: image

To maintain backward compatibility and preserve the previous behaviour, the proposed circular padding functionality is implemented as an optional argument “circular_padding”, which is set to False by default.

Usage example:

import torch
from diffusers import StableDiffusionPanoramaPipeline, DDIMScheduler

model_ckpt = "stabilityai/stable-diffusion-2-base"
scheduler = DDIMScheduler.from_pretrained(model_ckpt, subfolder="scheduler")
pipe = StableDiffusionPanoramaPipeline.from_pretrained(model_ckpt, scheduler=scheduler, torch_dtype=torch.float16)
pipe = pipe.to("cuda")

prompt = "an indoor equirectangular panorama of a living room"

image = pipe(prompt, width=1024, height=512,
    circular_padding=True).images[0]

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Jul 10, 2023

The documentation is not available anymore as the PR was closed or merged.

@EvgenyKashin EvgenyKashin changed the title Add circular padding for artifact-free StableDiffusionPanoramaPipeline [WIP] Add circular padding for artifact-free StableDiffusionPanoramaPipeline Jul 10, 2023
@EvgenyKashin
Copy link
Contributor Author

EvgenyKashin commented Jul 10, 2023

Visualization of a concatenation of the most right and the most left parts of a generated panorama images: No circular padding (default):
image

Circular padding, but without decoding padding in the VAE (without decode_latents_with_padding method):
image

Circular padding full (circular_padding=True):
image

@EvgenyKashin
Copy link
Contributor Author

In general, it's an easy fix for the stitching artifact problem, but maybe I've missed an easier way to do it with the current pipeline. Perhaps this is too narrow case to add this to the main.

I've also noticed tests for this pipeline that I break with my changes (even with default circular_padding=False option, that is just the same as current main in my local tests):
18 failed, 5 passed, 5 skipped, 2 warnings

I've tried "cuda" and "cpu", different batch_size and view_batch_size, it works fine. I couldn't understand why it's failing tests yet.

@EvgenyKashin
Copy link
Contributor Author

@omerbt @sayakpaul @Isotr0py guys, what do you think?

@sayakpaul
Copy link
Member

I will defer to @omerbt for this one. But I like what I see :)

@omerbt
Copy link
Contributor

omerbt commented Jul 11, 2023

Looks great!

@sayakpaul
Copy link
Member

Thanks @omerbt!

@EvgenyKashin, let's make sure the tests on the CI pass. Then I think we can approach merging.

@EvgenyKashin
Copy link
Contributor Author

EvgenyKashin commented Jul 11, 2023

Thanks, guys. @sayakpaul I've fixed one place in the code. Basically, I decided to save one line originally, but one more check was required for small images case (like in test cases). I've also added a few simple test cases for circular padding.

@EvgenyKashin
Copy link
Contributor Author

Sorry, originally tagged the wrong person, @sayakpaul

Copy link
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me! Thank you.

Left some minor nits. One thing we could do is add a note about circular_padding to the panorama docs: https://github.com/huggingface/diffusers/blob/main/docs/source/en/api/pipelines/panorama.mdx.

@EvgenyKashin
Copy link
Contributor Author

Thanks for the review @sayakpaul, that's improving the PR! I've rewritten the doc-string, added information to the .mdx and removed the slow test.

@EvgenyKashin EvgenyKashin changed the title [WIP] Add circular padding for artifact-free StableDiffusionPanoramaPipeline Add circular padding for artifact-free StableDiffusionPanoramaPipeline Jul 12, 2023
@EvgenyKashin
Copy link
Contributor Author

Could we merge it?

@sayakpaul
Copy link
Member

Looks amazing @EvgenyKashin! Thanks much for iterating. I just have one more ask here: #4025 (comment) and then we will ship this 🚀.

@EvgenyKashin
Copy link
Contributor Author

@sayakpaul could you approve two images merging to the documentation-images https://huggingface.co/datasets/huggingface/documentation-images/discussions/132 ?

@sayakpaul
Copy link
Member

Will merge after the CI passes.

@EvgenyKashin
Copy link
Contributor Author

@sayakpaul thanks, ready to ship now 🚀

@sayakpaul sayakpaul merged commit af48bf2 into huggingface:main Jul 12, 2023
@sayakpaul
Copy link
Member

Thanks very much for your valuable contributions!

orpatashnik pushed a commit to orpatashnik/diffusers that referenced this pull request Aug 1, 2023
huggingface#4025)

* Add circular padding option

* Fix style with black

* Fix corner case with small image size

* Add circular padding test cases

* Fix docstring

* Improve docstring for circular padding, remove slow test case

* Update docs for circular padding argument

* Add images comparison for circular padding
orpatashnik pushed a commit to orpatashnik/diffusers that referenced this pull request Aug 1, 2023
huggingface#4025)

* Add circular padding option

* Fix style with black

* Fix corner case with small image size

* Add circular padding test cases

* Fix docstring

* Improve docstring for circular padding, remove slow test case

* Update docs for circular padding argument

* Add images comparison for circular padding
orpatashnik pushed a commit to orpatashnik/diffusers that referenced this pull request Aug 1, 2023
huggingface#4025)

* Add circular padding option

* Fix style with black

* Fix corner case with small image size

* Add circular padding test cases

* Fix docstring

* Improve docstring for circular padding, remove slow test case

* Update docs for circular padding argument

* Add images comparison for circular padding
yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
huggingface#4025)

* Add circular padding option

* Fix style with black

* Fix corner case with small image size

* Add circular padding test cases

* Fix docstring

* Improve docstring for circular padding, remove slow test case

* Update docs for circular padding argument

* Add images comparison for circular padding
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
huggingface#4025)

* Add circular padding option

* Fix style with black

* Fix corner case with small image size

* Add circular padding test cases

* Fix docstring

* Improve docstring for circular padding, remove slow test case

* Update docs for circular padding argument

* Add images comparison for circular padding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants