Giter Club home page Giter Club logo

sdedit's Introduction

SDEdit: Guided Image Synthesis and Editing with Stochastic Differential Equations


Project | Paper | Colab

PyTorch implementation of SDEdit: Guided Image Synthesis and Editing with Stochastic Differential Equations (ICLR 2022).

Chenlin Meng, Yutong He, Yang Song, Jiaming Song, Jiajun Wu, Jun-Yan Zhu, Stefano Ermon

Stanford and CMU

Recently, SDEdit has also been applied to text-guided image editing with large-scale text-to-image models. Notable examples include Stable Diffusion's img2img function (see here), GLIDE, and distilled-SD. The below example comes from distilled-SD.

Overview

The key intuition of SDEdit is to "hijack" the reverse stochastic process of SDE-based generative models, as illustrated in the figure below. Given an input image for editing, such as a stroke painting or an image with color strokes, we can add a suitable amount of noise to make its artifacts undetectable, while still preserving the overall structure of the image. We then initialize the reverse SDE with this noisy input, and simulate the reverse process to obtain a denoised image of high quality. The final output is realistic while resembling the overall image structure of the input.

Getting Started

The code will automatically download pretrained SDE (VP) PyTorch models on CelebA-HQ, LSUN bedroom, and LSUN church outdoor.

Data format

We save the image and the corresponding mask in an array format [image, mask], where "image" is the image with range [0,1] in the PyTorch tensor format, "mask" is the corresponding binary mask (also the PyTorch tensor format) specifying the editing region. We provide a few examples, and functions/process_data.py will automatically download the examples to the colab_demo folder.

Re-training the model

Here is the PyTorch implementation for training the model.

Stroke-based image generation

Given an input stroke painting, our goal is to generate a realistic image that shares the same structure as the input painting. SDEdit can synthesize multiple diverse outputs for each input on LSUN bedroom, LSUN church and CelebA-HQ datasets.

To generate results on LSUN datasets, please run

python main.py --exp ./runs/ --config bedroom.yml --sample -i images --npy_name lsun_bedroom1 --sample_step 3 --t 500  --ni
python main.py --exp ./runs/ --config church.yml --sample -i images --npy_name lsun_church --sample_step 3 --t 500  --ni

Stroke-based image editing

Given an input image with user strokes, we want to manipulate a natural input image based on the user's edit. SDEdit can generate image edits that are both realistic and faithful (to the user edit), while avoid introducing undesired changes.

To perform stroke-based image editing, run
python main.py --exp ./runs/  --config church.yml --sample -i images --npy_name lsun_edit --sample_step 3 --t 500  --ni

Additional results

References

If you find this repository useful for your research, please cite the following work.

@inproceedings{
      meng2022sdedit,
      title={{SDE}dit: Guided Image Synthesis and Editing with Stochastic Differential Equations},
      author={Chenlin Meng and Yutong He and Yang Song and Jiaming Song and Jiajun Wu and Jun-Yan Zhu and Stefano Ermon},
      booktitle={International Conference on Learning Representations},
      year={2022},
}

This implementation is based on / inspired by:

Here are also some of the interesting follow-up works of SDEdit:

sdedit's People

Contributors

chenlin9 avatar junyanz avatar kellyyutonghe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sdedit's Issues

License?

Really cool project!! What is the license the code is released? Could you include it in the repo?

Generate hd images

Hey @chenlin9 and @junyanz,
Awesome work! I have a question about training and generating high-resolution images (1024x1024).

How can I train on FFHQ or CelebA-HQ. I have looked for the config file but couldn't find any config for training 1024x1024 resolution. All the config files are for images with sizes 256x256. I would like to train the model on my custom dataset to generate 1024x1024 images. Would it be possible to provide the config file for CelebA-HQ or can you elaborate on how can I train for such a dataset to generate 1024x1024?

Thanks!

HTTP Error Occurs When Downloading Process Data

It seems that the url for processed data is no longer usable:

ERROR - main.py - 2023-12-11 10:45:23,843 - Traceback (most recent call last):
File "/home/chenqm/projects/cross-domain-trajectory-editing-private/SDEdit/main.py", line 104, in main
runner.image_editing_sample()
File "/home/chenqm/projects/cross-domain-trajectory-editing-private/SDEdit/runners/image_editing.py", line 102, in image_editing_sample
download_process_data(path="colab_demo")
File "/home/chenqm/projects/cross-domain-trajectory-editing-private/SDEdit/functions/process_data.py", line 8, in download_process_data
torch.hub.download_url_to_file('https://image-editing-test-12345.s3-us-west-2.amazonaws.com/colab_examples/lsun_bedroom1.pth', os.path.join(path, 'lsun_bedroom1.pth'))
File "/home/chenqm/anaconda3/envs/jax_diffuser/lib/python3.9/site-packages/torch/hub.py", line 621, in download_url_to_file
u = urlopen(req)
File "/home/chenqm/anaconda3/envs/jax_diffuser/lib/python3.9/urllib/request.py", line 214, in urlopen
return opener.open(url, data, timeout)
File "/home/chenqm/anaconda3/envs/jax_diffuser/lib/python3.9/urllib/request.py", line 523, in open
response = meth(req, response)
File "/home/chenqm/anaconda3/envs/jax_diffuser/lib/python3.9/urllib/request.py", line 632, in http_response
response = self.parent.error(
File "/home/chenqm/anaconda3/envs/jax_diffuser/lib/python3.9/urllib/request.py", line 561, in error
return self._call_chain(*args)
File "/home/chenqm/anaconda3/envs/jax_diffuser/lib/python3.9/urllib/request.py", line 494, in _call_chain
result = func(*args)
File "/home/chenqm/anaconda3/envs/jax_diffuser/lib/python3.9/urllib/request.py", line 641, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

image

urllib.error.HTTPError: HTTP Error 403: Forbidden

Hi, we cannot download the lsun_bedroom1.pth using the official command python main.py --exp ./runs/ --config bedroom.yml --sample -i images --npy_name lsun_bedroom1 --sample_step 3 --t 500 --ni. How could we fix this error? The traceback is posted below.

Thank you very much.

  File "main.py", line 104, in main
    runner.image_editing_sample()
  File "/mnt/zff/code/diffusion_model/sdedit/sdedit/runners/image_editing.py", line 113, in image_editing_sample
    download_process_data(path="colab_demo")
  File "/mnt/zff/code/diffusion_model/sdedit/sdedit/functions/process_data.py", line 8, in download_process_data
    torch.hub.download_url_to_file('https://image-editing-test-12345.s3-us-west-2.amazonaws.com/colab_examples/lsun_bedroom1.pth', os.path.join(path, 'lsun_bedroom1.pth'))
  File "/miniconda3/envs/t1/lib/python3.8/site-packages/torch/hub.py", line 611, in download_url_to_file
    u = urlopen(req)
  File "/miniconda3/envs/t1/lib/python3.8/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/miniconda3/envs/t1/lib/python3.8/urllib/request.py", line 531, in open
    response = meth(req, response)
  File "/miniconda3/envs/t1/lib/python3.8/urllib/request.py", line 640, in http_response
    response = self.parent.error(
  File "/miniconda3/envs/t1/lib/python3.8/urllib/request.py", line 569, in error
    return self._call_chain(*args)
  File "/miniconda3/envs/t1/lib/python3.8/urllib/request.py", line 502, in _call_chain
    result = func(*args)
  File "/miniconda3/envs/t1/lib/python3.8/urllib/request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

Wrong repository for training the models?

HI @chenlin9

First of all, congratulations on such a fantastic project. I had a doubt regarding the training models. As I understood, you employed a trained SDE model to perturb the target image into a noise image, and after that, you reverse the stochastic process to get the final image. In that case, we should use the repository of @yang-song https://github.com/yang-song/score_sde (this one for PyTorch implementation) to train SDE models instead of https://github.com/ermongroup/ddim right?

Thank you so much.

Pretrained models now missing

Hi @chenlin9
Great work, but seems that all pretrained checkpoints are down, since all rely on AWS, i.e.:
https://image-editing-test-12345.s3-us-west-2.amazonaws.com/checkpoints/bedroom.ckpt

Several works depend on these, would you mind uploading them again?

Personally, I'm looking for LSUN Bedrooms, but I see a lot of people requesting CelebA-HQ too.

Thanks.

Error while Downloading Pre-trained Model Weights

Hi,

Thank you for the code implementation of the nice work! I was encountering an error while trying to download your pre-trained model weights. Here is the error:

image

Could you please fix it? Thanks in advance!

Best,

How to change the step length or the total denoising steps (N), so that the image generation process could be faster?

Hi there, thank you for releasing the code!

I tried several ways as below to change the step length (delta t) and the total denosing steps but none of them works:

  1. Changing the num_timesteps hyperparameter in the config file and changing thetotal_noise_levels accordingly.
  2. Changing the step length of the enumeration of i in the SDEditing demonstration function

It seems the synthesized images remain noised after the hyperparameters are tuned. Is there a way to modify N safely? Or is it possible to accelerate the generation process by other means?

Thank you!

HTTP Error in Colab Demo

The following error occurs in the colab demo:

Downloading: "https://image-editing-test-12345.s3-us-west-2.amazonaws.com/checkpoints/church_outdoor.ckpt" to /root/.cache/torch/hub/checkpoints/church_outdoor.ckpt

HTTPError Traceback (most recent call last)
in <cell line: 5>()
3 data_name = "lsun_church"
4 sample_step = 3
----> 5 model, betas, num_timesteps, logvar = load_model(dataset, category, "church.yml")
6 total_noise_levels = 500
7 SDEditing(betas, logvar, model, data_name, sample_step, total_noise_levels, n=3)

8 frames
/usr/lib/python3.10/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
641 class HTTPDefaultErrorHandler(BaseHandler):
642 def http_error_default(self, req, fp, code, msg, hdrs):
--> 643 raise HTTPError(req.full_url, code, msg, hdrs, fp)
644
645 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 403: Forbidden

Input image&mask shape and dtype.

Thanks for sharing the implementtion.

Just want to ask how do you prepare the input images?
# [mask, img] = torch.load("colab_demo/{}.pth".format(name))
Since "colab_demo" url is out of date, I prepare a single image and draw a mask, and read by PIL.Image.
However, it has err when run into this line: tvu.save_image(x0, os.path.join(self.args.image_folder, f'original_input.png'))

I am wondering what is correct shape of x0?
Or how do you prepare these .pth files?
torch.hub.download_url_to_file('https://image-editing-test-12345.s3-us-west-2.amazonaws.com/colab_examples/lsun_church.pth', os.path.join(path, 'lsun_church.pth'))

It should be very easy to prepare them, I guess.

I get link Forbidden error when I try to generate images of stoke based

File "/home/dell/anaconda3/lib/python3.9/urllib/request.py", line 641, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

I get the following error when I try to generate results, can you let me know what is the reason for it? Is it due to not having the right link for pre trained model?

How can I test Image compositing

Thanks for your share of wonderful experiments.

I'm currently testing on re-generate of experiments on paper,

everything goes well, but I can't find image compositing on our distribution,
Is there any ways to do that?

Paper release

Hi! I'm very impressed with the results on the project webpage. When are you going to release the paper itself?

TypeError in Colab demo

All 3 blocks for image generation produce this error:
TypeError: image_editing_denoising_step_flexible_mask() got an unexpected keyword argument 'return_pred_xstart'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.