pesser / stable-diffusion

MIT License
1.03k stars 401 forks source link

Repeated inpainting leads to saturated pixels #22

Open matigekunstintelligentie opened 2 years ago

matigekunstintelligentie commented 2 years ago

Repeated inpainting leads to saturated pixels. Quick and dirty example:

import subprocess
import os
import numpy as np
from PIL import Image, ImageDraw
import shutil

directory = lambda x: "./Diffusion/Diffusion_{}/".format(x)

for i in range(240):
    if i!=0:
        if os.path.exists(directory(i)):
            shutil.rmtree(directory(i))
for i in range(240):
    im = Image.new('RGB', (512, 512), (0, 0, 0))
    draw = ImageDraw.Draw(im)

    x = np.random.randint(512-128)
    y = np.random.randint(512-128)

    draw.rectangle([(x,y),(x+128,y+128)], fill=(255, 255, 255))
    im.save('{}Diffusion_mask.png'.format(directory(i)))

    os.mkdir(directory(i+1))
    subprocess.run('python scripts/inpaint.py --steps 20 --indir {} --outdir {}'.format(directory(i),directory(i+1)), shell=True)

    im = Image.open('{}Diffusion.png'.format(directory(i+1)))

    # pixels = 2
    # im = im.crop((pixels, pixels, 512-pixels, 512-pixels))
    # im = im.resize((512,512), resample=Image.BICUBIC, box=None, reducing_gap=None)
    # im.save('{}Diffusion.png'.format(directory(i+1)))
    im.save('./DiffusionOut/{0:06d}.png'.format(i))
    if i!=0:
        shutil.rmtree(directory(i))

Add folders/files: ./Diffusion/Diffusion_0/Diffusion.png ./DiffusionOut/

In scripts/inpainting changing

inpainted = inpainted.cpu().numpy().transpose(0,2,3,1)[0]*255

To

inpainted = np.round(inpainted.cpu().numpy().transpose(0,2,3,1)[0]*255)

Fixes the issue I think