Open Dexter-Wang opened 7 months ago
Do you have a minimal reproduction demonstrating this issue? There is a lot of unrelated code in the snippet you posted.
cc @aliabid94 re: perf improvements.
import torch from diffusers import StableDiffusionImg2ImgPipeline
modelID1 = "pagebrain/dreamshaper-v8"
Img2Img1 = StableDiffusionImg2ImgPipeline.from_pretrained(modelID1, torch_dtype=torch.float16,
safety_checker=None, requires_safety_checker=False).to("cuda")
'''--- ImageToImage --------------------------------------------- '''
def ImageToImage0(imgIn=None, prompt="", strength=0.75, seed=0):
if (imgIn==None or prompt==""): return None
gene = torch.Generator(device="cuda").manual_seed(0)
imgOut = Img2Img1(prompt=prompt, image=imgIn, generator=gene, strength=strength, guidance_scale=7.5).images[0]
return imgOut
'''--- Create UI --------------------------------------------- ''' from PIL import ImageFilter import gradio as gr
def ImgToInput(imgOut, imgIn):
if (imgOut==None): return None
imgIn["background"]=imgOut.copy()
return imgIn
def ImageToImage(imgIn, prompt):
if (imgIn==None): return None
imgIn = imgIn["composite"].copy()
imgOut = ImageToImage0(imgIn, prompt)
return imgOut
def ImageBlur(imgIn, radius=30):
imgIn = imgIn["composite"].copy()
imgOut = imgIn.filter(ImageFilter.GaussianBlur(radius=radius))
return imgOut
with gr.Blocks() as MainForm:
Prompt = gr.Textbox(value="A beautiful country side landscape, romantic", label='Prompt')
with gr.Tab("ImageToImage"):
with gr.Row():
with gr.Column():
IgClear = gr.Button("Clear")
IgImgIn = gr.ImageEditor(type='pil', image_mode="RGB", width=512, height=512, interactive=True, )
with gr.Column():
Ig2ImgGN = gr.Button("Generate")
IgImgOut = gr.Image(type='pil', width=512, height=512, )
with gr.Tab("Preprocess"):
with gr.Row():
with gr.Column():
PrClear = gr.Button("Clear")
PrImgIn = gr.ImageEditor(type='pil', image_mode="RGB", width=512, height=512, interactive=True, )
BlurBtn = gr.Button("Blur")
with gr.Column():
PrImgOut = gr.Image(type='pil', width=512, height=512, )
with gr.Row():
PrToIgIn = gr.Button("To IamgeToImage Input")
#--- ImgToImg ------
IgClear.click(fn=lambda:[None,None], inputs=[], outputs=[IgImgIn,IgImgOut])
Ig2ImgGN.click(fn=ImageToImage, inputs=[IgImgIn,Prompt], outputs=IgImgOut)
#--- Preprocess ------
PrClear.click(fn=lambda:[None,None], inputs=[], outputs=[PrImgIn,PrImgOut])
BlurBtn.click (fn=ImageBlur, inputs=PrImgIn, outputs=PrImgOut)
PrToIgIn.click (fn=ImgToInput, inputs=[PrImgOut,IgImgIn], outputs=IgImgIn)
MainForm.launch()
Thank you for your quick reply. Above is a simplify code. Black image happen less. Following is the working flow to make Black image happen.
however, ImageEditor is a very good design.
I also put the above code at Colab, Following is the address, if you need it.
https://colab.research.google.com/drive/1QhhGt955SkCrlGc8TJyLYDZkiJ2-EGDr
Describe the bug
gr.ImageEditor is very useful, but upload a image takes 5 second sometimes.
There is another issue as bellow. Transfer Image from the gr.Image at one Tab to gr.ImageEditor at another Tab, it got black image quite often.
Have you searched existing issues? 🔎
Reproduction
import torch from diffusers import StableDiffusionImg2ImgPipeline
from diffusers import DiffusionPipeline
NegPrompt ="""low res, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry, ugly"""
modelID1 = "pagebrain/dreamshaper-v8"
Img2Img1 = StableDiffusionImg2ImgPipeline.from_pretrained(modelID1, torch_dtype=torch.float16, safety_checker=None, requires_safety_checker=False).to("cuda")
'''--- ImageToImage --------------------------------------------- '''
def ImageToImage0(imgIn=None, prompt="", strength=0.75, seed=0): if (imgIn==None or prompt==""): return None
gene = torch.Generator(device="cuda").manual_seed(0) imgOut = Img2Img1(prompt=prompt, negative_prompt=NegPrompt, image=imgIn, generator=gene, strength=strength, guidance_scale=7.5).images[0]
return imgOut
'''--- TextToImage --------------------------------------------- ''' Text2Img1 = DiffusionPipeline.from_pretrained(modelID1, torch_dtype=torch.float16, safety_checker=None, requires_safety_checker=False).to("cuda")
def TextToImage0(prompt=""): if (prompt==""): return None image = Text2Img1(prompt, negative_prompt=NegPrompt).images[0]
return image
'''--- Create UI --------------------------------------------- ''' import numpy as np from PIL import ImageFilter import gradio as gr
def ImgToInput(imgOut, imgIn):
if (imgOut==None): return None imgIn["background"]=imgOut.copy()
return imgIn
def ImageToImage(imgIn, prompt, strength):
if (imgIn==None): return None imgIn = imgIn["composite"].copy()
imgOut = ImageToImage0(imgIn, prompt, strength)
return imgOut
def TextToImage(prompt): if (prompt==None): return None imgOut = TextToImage0(prompt)
return imgOut
def ImageBlur(imgIn, radius=30):
imgIn = imgIn["composite"].copy()
imgOut = imgIn.filter(ImageFilter.GaussianBlur(radius=radius))
return imgOut
Text1 = "A beautiful country side landscape, romantic" with gr.Blocks() as MainForm:
Prompt = gr.Textbox(value=Text1, label='Prompt')
with gr.Tab("ImageToImage"): with gr.Row():
with gr.Column():
IgClear = gr.Button("Clear")
IgImgIn = gr.ImageEditor(type='pil', image_mode="RGB", width=512, height=512, interactive=True, )
Strength = gr.Slider(0.1, 1.0, 0.75, step=0.01, label='Strength')
with gr.Column():
Ig2ImgGN = gr.Button("Generate") IgImgOut = gr.Image(type='pil', width=512, height=512, )
IgToInput = gr.Button("To Input")
MainForm.launch()
Screenshot
Logs
System Info
Severity
I can work around it