Fabrice-TIERCELIN's picture
Image Guidance Scale
4a53f08 verified
raw
history blame
10.4 kB
from diffusers import (
ControlNetModel,
DiffusionPipeline,
StableDiffusionControlNetPipeline,
)
import gradio as gr
import numpy as np
import os
import time
import math
import random
import imageio
from PIL import Image, ImageFilter
import torch
max_64_bit_int = 2**63 - 1
device = "cuda" if torch.cuda.is_available() else "cpu"
controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11e_sd15_ip2p", torch_dtype = torch.float32)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", safety_checker = None, controlnet = controlnet, torch_dtype = torch.float32
)
pipe = pipe.to(device)
def check(
source_img,
prompt,
negative_prompt,
denoising_steps,
num_inference_steps,
guidance_scale,
image_guidance_scale,
randomize_seed,
seed,
progress = gr.Progress()):
if source_img is None:
raise gr.Error("Please provide an image.")
if prompt is None or prompt == "":
raise gr.Error("Please provide a prompt input.")
def pix2pix(
source_img,
prompt,
negative_prompt,
denoising_steps,
num_inference_steps,
guidance_scale,
image_guidance_scale,
randomize_seed,
seed,
progress = gr.Progress()):
check(
source_img,
prompt,
negative_prompt,
denoising_steps,
num_inference_steps,
guidance_scale,
image_guidance_scale,
randomize_seed,
seed
)
start = time.time()
progress(0, desc = "Preparing data...")
if negative_prompt is None:
negative_prompt = ""
if denoising_steps is None:
denoising_steps = 0
if num_inference_steps is None:
num_inference_steps = 20
if guidance_scale is None:
guidance_scale = 5
if image_guidance_scale is None:
image_guidance_scale = 1.5
if randomize_seed:
seed = random.randint(0, max_64_bit_int)
random.seed(seed)
#pipe = pipe.manual_seed(seed)
try:
imageio.imwrite("data.png", source_img)
except:
raise gr.Error("Can't read input image. You can try to first save your image in another format (.webp, .png, .jpeg, .bmp...).")
# Input image
try:
input_image = Image.open("data.png").convert("RGB")
except:
raise gr.Error("Can't open input image. You can try to first save your image in another format (.webp, .png, .jpeg, .bmp...).")
original_height, original_width, dummy_channel = np.array(input_image).shape
output_width = original_width
output_height = original_height
mask_image = Image.new(mode = input_image.mode, size = (output_width, output_height), color = "white")
limitation = "";
# Limited to 1 million pixels
if 1024 * 1024 < output_width * output_height:
factor = ((1024 * 1024) / (output_width * output_height))**0.5
output_width = math.floor(output_width * factor)
output_height = math.floor(output_height * factor)
limitation = " Due to technical limitation, the image have been downscaled and then upscaled.";
# Width and height must be multiple of 8
output_width = output_width - (output_width % 8)
output_height = output_height - (output_height % 8)
progress(None, desc = "Processing...")
output_image = pipe(
seeds=[seed],
width = output_width,
height = output_height,
prompt = prompt,
negative_prompt = negative_prompt,
image = input_image,
mask_image = mask_image,
num_inference_steps = num_inference_steps,
guidance_scale = guidance_scale,
image_guidance_scale = image_guidance_scale,
denoising_steps = denoising_steps,
show_progress_bar = True
).images[0]
if limitation != "":
output_image = output_image.resize((original_width, original_height))
end = time.time()
secondes = int(end - start)
minutes = secondes // 60
secondes = secondes - (minutes * 60)
hours = minutes // 60
minutes = minutes - (hours * 60)
return [
output_image,
"Start again to get a different result. The new image is " + str(output_width) + " pixels large and " + str(output_height) + " pixels high, so an image of " + f'{output_width * output_height:,}' + " pixels. The image have been generated in " + str(hours) + " h, " + str(minutes) + " min, " + str(secondes) + " sec." + limitation
]
with gr.Blocks() as interface:
gr.Markdown(
"""
<p style="text-align: center;"><b><big><big><big>Instruct Pix2Pix demo</big></big></big></b></p>
<p style="text-align: center;">Modifies your image using a textual instruction, up to 1 million pixels, freely, without account, without watermark, without installation, which can be downloaded</p>
<br/>
<br/>
πŸš€ Powered by <i>SD 1.5</i> and <i>ControlNet</i>
<br/>
<ul>
<li>To change the <b>view angle</b> of your image, I recommend to use <i>Zero123</i>,</li>
<li>To <b>upscale</b> your image, I recommend to use <i>Ilaria Upscaler</i>,</li>
<li>To <b>slightly change</b> your image, I recommend to use <i>Image-to-Image SDXL</i>,</li>
<li>To change <b>one detail</b> on your image, I recommend to use <i>Inpaint SDXL</i>,</li>
<li>To remove the <b>background</b> of your image, I recommend to use <i>BRIA</i>,</li>
<li>To enlarge the <b>viewpoint</b> of your image, I recommend to use <i>Uncrop</i>,</li>
<li>To make a <b>tile</b> of your image, I recommend to use <i>Make My Image Tile</i>,</li>
</ul>
<br/>
🐌 Slow process... ~1 hour. If this space does not work or you want a faster run, use <i>Instruct Pix2Pix</i> available on terrapretapermaculture's <i>ControlNet-v1-1</i> space (last tab) or on <i>Dezgo</i> site.<br>You can duplicate this space on a free account, it works on CPU.<br/>
<a href='https://huggingface.co/spaces/Fabrice-TIERCELIN/Instruct-Pix2Pix?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14'></a>
<br/>
βš–οΈ You can use, modify and share the generated images but not for commercial uses.
"""
)
with gr.Column():
source_img = gr.Image(label = "Your image", sources = ["upload"], type = "numpy")
prompt = gr.Textbox(label = 'Prompt', info = "Instruct what to change in the image", placeholder = 'Order the AI what to change in the image')
with gr.Accordion("Advanced options", open = False):
negative_prompt = gr.Textbox(label = 'Negative prompt', placeholder = 'Describe what you do NOT want to see in the image', value = 'Watermark')
denoising_steps = gr.Slider(minimum = 0, maximum = 1000, value = 0, step = 1, label = "Denoising", info = "lower=irrelevant result, higher=relevant result")
num_inference_steps = gr.Slider(minimum = 10, maximum = 500, value = 20, step = 1, label = "Number of inference steps", info = "lower=faster, higher=image quality")
guidance_scale = gr.Slider(minimum = 1, maximum = 13, value = 5, step = 0.1, label = "Classifier-Free Guidance Scale", info = "lower=image quality, higher=follow the prompt")
image_guidance_scale = gr.Slider(minimum = 1, value = 1.5, step = 0.1, label = "Image Guidance Scale", info = "lower=image quality, higher=follow the image")
randomize_seed = gr.Checkbox(label = "\U0001F3B2 Randomize seed (not working, always checked)", value = True, info = "If checked, result is always different")
seed = gr.Slider(minimum = 0, maximum = max_64_bit_int, step = 1, randomize = True, label = "Seed (if not randomized)")
submit = gr.Button("Modify", variant = "primary")
modified_image = gr.Image(label = "Modified image")
information = gr.Label(label = "Information")
submit.click(check, inputs = [
source_img,
prompt,
negative_prompt,
denoising_steps,
num_inference_steps,
guidance_scale,
image_guidance_scale,
randomize_seed,
seed
], outputs = [], queue = False, show_progress = False).success(pix2pix, inputs = [
source_img,
prompt,
negative_prompt,
denoising_steps,
num_inference_steps,
guidance_scale,
image_guidance_scale,
randomize_seed,
seed
], outputs = [
modified_image,
information
], scroll_to_output = True)
gr.Examples(
inputs = [
source_img,
prompt,
negative_prompt,
denoising_steps,
num_inference_steps,
guidance_scale,
image_guidance_scale,
randomize_seed,
seed
],
outputs = [
modified_image,
information
],
examples = [
[
"Example1.webp",
"What if it's snowing?",
"Watermark",
1,
20,
5,
1.5,
True,
42
],
[
"Example2.png",
"What if this woman had brown hair?",
"Watermark",
1,
20,
5,
1.5,
True,
42
],
[
"Example3.jpeg",
"Replace the house by a windmill",
"Watermark",
1,
20,
5,
1.5,
True,
42
],
],
cache_examples = False,
)
interface.queue().launch()