wyhuai / DDNM

[ICLR 2023 Oral] Zero-Shot Image Restoration Using Denoising Diffusion Null-Space Model
MIT License
1.16k stars 83 forks source link

inpainting with arbitrary size #73

Closed YuQiao0303 closed 5 months ago

YuQiao0303 commented 5 months ago

Hi authors, thank you for this amazing work!

I try to inpaint an image of arbitrary size with random mask. Here's the command I use: cd DDNM/hq_demo python main.py --resize_y --config confs/inet256.yml --path_y data/datasets/gts/inet256/orange.png --class 950 --deg "inpainting" --scale 4 -i orange_cond_inpainting

However it raises error as: NotImplementedError: degradation type not supported

Any suggestions? Thank you!

wyhuai commented 5 months ago

Maybe try to change "--scale 4" to "--scale 1"?

YuQiao0303 commented 5 months ago

Maybe try to change "--scale 4" to "--scale 1"?

Thank you so much! I've figured it out.

  1. As you say, change --'scale 4' to '--scale 1'.

  2. And then, in hq_dmeo/guided_diffusion/gaussian_diffusion.py, add the following configuration for inpainting:

                        # normal DDNM sampling
                        if t_cur < t_last:  
                            with th.no_grad():
                                if model_kwargs['deg']=='inpainting': # add this
                                    A = lambda z: z*mask[:,:,h_l:h_r,w_l:w_r]
                                    Ap = A
                                    A_temp = A
                                    model_kwargs['A'] = A
                                    model_kwargs['Ap'] = Ap
                                image_before_step = image_after_step.clone()
                                out = self.p_sample( 
                                    model,
                                    image_after_step,
                                    t_last_t,
                                    clip_denoised=clip_denoised,
                                    denoised_fn=denoised_fn,
                                    cond_fn=cond_fn,
                                    model_kwargs=model_kwargs,
                                    conf=conf,
                                    x0_t=x0_t
                                )
  3. You may also want to change elif model_kwargs['deg']=='inpainting' and conf.name=='face256' into elif model_kwargs['deg']=='inpainting': for inpainting other things than faces

  4. Finally, make sure you upload a mask with the same size as the input image:

    mask = model_kwargs.get('gt_keep_mask') # It should be the same size as GT: [1,3,desired_h,desired_w],torch.float32, values are 0 or 1