2022-10-24 03:31:41 +03:00
# This file is part of sygil-webui (https://github.com/Sygil-Dev/sygil-webui/).
2022-09-26 16:02:48 +03:00
2022-10-24 03:17:50 +03:00
# Copyright 2022 Sygil-Dev team.
2022-09-26 16:02:48 +03:00
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
2023-06-23 05:58:20 +03:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse , os , sys , re , requests , json , time
2022-09-07 01:50:14 +03:00
2022-09-07 22:19:00 +03:00
import cv2
2022-10-02 20:22:27 +03:00
from logger import logger , set_logger_verbosity , quiesce_logger
2022-09-09 08:24:46 +03:00
from perlin import perlinNoise
2022-09-07 01:50:14 +03:00
from frontend . frontend import draw_gradio_ui
from frontend . job_manager import JobManager , JobInfo
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
from frontend . image_metadata import ImageMetadata
2022-09-07 01:50:14 +03:00
from frontend . ui_functions import resize_image
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
parser = argparse . ArgumentParser ( formatter_class = argparse . ArgumentDefaultsHelpFormatter )
2023-06-23 05:58:20 +03:00
parser . add_argument (
" --ckpt " ,
type = str ,
default = " models/ldm/stable-diffusion-v1/model.ckpt " ,
help = " path to checkpoint of model " ,
)
parser . add_argument (
" --cli " ,
type = str ,
help = " don ' t launch web server, take Python function kwargs from this file. " ,
default = None ,
)
parser . add_argument (
" --config " ,
type = str ,
default = " configs/stable-diffusion/v1-inference.yaml " ,
help = " path to config which constructs model " ,
)
parser . add_argument (
" --defaults " ,
type = str ,
help = " path to configuration file providing UI defaults, uses same format as cli parameter " ,
default = " configs/webui/webui.yaml " ,
)
parser . add_argument (
" --esrgan-cpu " , action = " store_true " , help = " run ESRGAN on cpu " , default = False
)
parser . add_argument (
" --esrgan-gpu " ,
type = int ,
help = " run ESRGAN on specific gpu (overrides --gpu) " ,
default = 0 ,
)
parser . add_argument (
" --extra-models-cpu " ,
action = " store_true " ,
help = " run extra models (GFGPAN/ESRGAN) on cpu " ,
default = False ,
)
parser . add_argument (
" --extra-models-gpu " ,
action = " store_true " ,
help = " run extra models (GFGPAN/ESRGAN) on gpu " ,
default = False ,
)
parser . add_argument (
" --gfpgan-cpu " , action = " store_true " , help = " run GFPGAN on cpu " , default = False
)
parser . add_argument (
" --gfpgan-dir " ,
type = str ,
help = " GFPGAN directory " ,
default = ( " ./models/gfpgan " if os . path . exists ( " ./models/gfpgan " ) else " ./GFPGAN " ) ,
) # i disagree with where you're putting it but since all guidefags are doing it this way, there you go
parser . add_argument (
" --gfpgan-gpu " ,
type = int ,
help = " run GFPGAN on specific gpu (overrides --gpu) " ,
default = 0 ,
)
parser . add_argument (
" --gpu " , type = int , help = " choose which GPU to use if you have multiple " , default = 0
)
parser . add_argument (
" --grid-format " ,
type = str ,
help = " png for lossless png files; jpg:quality for lossy jpeg; webp:quality for lossy webp, or webp:-compression for lossless webp " ,
default = " jpg:95 " ,
)
parser . add_argument (
" --inbrowser " ,
action = " store_true " ,
help = " automatically launch the interface in a new tab on the default browser " ,
default = False ,
)
parser . add_argument (
" --ldsr-dir " ,
type = str ,
help = " LDSR directory " ,
default = ( " ./models/ldsr " if os . path . exists ( " ./models/ldsr " ) else " ./LDSR " ) ,
)
parser . add_argument (
" --n_rows " ,
type = int ,
default = - 1 ,
help = " rows in the grid; use -1 for autodetect and 0 for n_rows to be same as batch_size (default: -1) " ,
)
parser . add_argument (
" --no-half " ,
action = " store_true " ,
help = " do not switch the model to 16-bit floats " ,
default = False ,
)
parser . add_argument (
" --no-progressbar-hiding " ,
action = " store_true " ,
help = " do not hide progressbar in gradio UI (we hide it because it slows down ML if you have hardware accleration in browser) " ,
default = False ,
)
parser . add_argument (
" --no-verify-input " ,
action = " store_true " ,
help = " do not verify input to check if it ' s too long " ,
default = False ,
)
parser . add_argument (
" --optimized-turbo " ,
action = " store_true " ,
help = " alternative optimization mode that does not save as much VRAM but runs siginificantly faster " ,
)
parser . add_argument (
" --optimized " ,
action = " store_true " ,
help = " load the model onto the device piecemeal instead of all at once to reduce VRAM usage at the cost of performance " ,
)
parser . add_argument (
" --outdir_scn2img " ,
type = str ,
nargs = " ? " ,
help = " dir to write scn2img results to (overrides --outdir) " ,
default = None ,
)
parser . add_argument (
" --outdir_img2img " ,
type = str ,
nargs = " ? " ,
help = " dir to write img2img results to (overrides --outdir) " ,
default = None ,
)
parser . add_argument (
" --outdir_imglab " ,
type = str ,
nargs = " ? " ,
help = " dir to write imglab results to (overrides --outdir) " ,
default = None ,
)
parser . add_argument (
" --outdir_txt2img " ,
type = str ,
nargs = " ? " ,
help = " dir to write txt2img results to (overrides --outdir) " ,
default = None ,
)
parser . add_argument (
" --outdir " , type = str , nargs = " ? " , help = " dir to write results to " , default = None
)
parser . add_argument (
" --filename_format " , type = str , nargs = " ? " , help = " filenames format " , default = None
)
parser . add_argument (
" --port " ,
type = int ,
help = " choose the port for the gradio webserver to use " ,
default = 7860 ,
)
parser . add_argument (
" --precision " ,
type = str ,
help = " evaluate at this precision " ,
choices = [ " full " , " autocast " ] ,
default = " autocast " ,
)
parser . add_argument (
" --realesrgan-dir " ,
type = str ,
help = " RealESRGAN directory " ,
default = (
" ./models/realesrgan "
if os . path . exists ( " ./models/realesrgan " )
else " ./RealESRGAN "
) ,
)
parser . add_argument (
" --realesrgan-model " ,
type = str ,
help = " Upscaling model for RealESRGAN " ,
default = ( " RealESRGAN_x4plus " ) ,
)
parser . add_argument (
" --save-metadata " ,
action = " store_true " ,
help = " Store generation parameters in the output png. Drop saved png into Image Lab to read parameters " ,
default = False ,
)
parser . add_argument (
" --share-password " ,
type = str ,
help = " Sharing is open by default, use this to set a password. Username: webui " ,
default = None ,
)
parser . add_argument (
" --share " ,
action = " store_true " ,
help = " Should share your server on gradio.app, this allows you to use the UI from your mobile app " ,
default = False ,
)
parser . add_argument (
" --skip-grid " ,
action = " store_true " ,
help = " do not save a grid, only individual samples. Helpful when evaluating lots of samples " ,
default = False ,
)
parser . add_argument (
" --skip-save " ,
action = " store_true " ,
help = " do not save indiviual samples. For speed measurements. " ,
default = False ,
)
parser . add_argument (
" --no-job-manager " ,
action = " store_true " ,
help = " Don ' t use the experimental job manager on top of gradio " ,
default = False ,
)
parser . add_argument (
" --max-jobs " ,
type = int ,
help = " Maximum number of concurrent ' generate ' commands " ,
default = 1 ,
)
parser . add_argument (
" --tiling " , action = " store_true " , help = " Generate tiling images " , default = False
)
parser . add_argument (
" -v " ,
" --verbosity " ,
action = " count " ,
default = 0 ,
help = " The default logging level is ERROR or higher. This value increases the amount of logging seen in your screen " ,
)
parser . add_argument (
" -q " ,
" --quiet " ,
action = " count " ,
default = 0 ,
help = " The default logging level is ERROR or higher. This value decreases the amount of logging seen in your screen " ,
)
parser . add_argument (
" --bridge " ,
action = " store_true " ,
help = " don ' t launch web server, but make this instance into a Horde bridge. " ,
default = False ,
)
parser . add_argument (
" --horde_api_key " ,
action = " store " ,
required = False ,
type = str ,
help = " The API key corresponding to the owner of this Horde instance " ,
)
parser . add_argument (
" --horde_name " ,
action = " store " ,
required = False ,
type = str ,
help = " The server name for the Horde. It will be shown to the world and there can be only one. " ,
)
parser . add_argument (
" --horde_url " ,
action = " store " ,
required = False ,
type = str ,
help = " The SH Horde URL. Where the bridge will pickup prompts and send the finished generations. " ,
)
parser . add_argument (
" --horde_priority_usernames " ,
type = str ,
action = " append " ,
required = False ,
help = " Usernames which get priority use in this horde instance. The owner ' s username is always in this list. " ,
)
parser . add_argument (
" --horde_max_power " ,
type = int ,
required = False ,
help = " How much power this instance has to generate pictures. Min: 2 " ,
)
parser . add_argument (
" --horde_sfw " ,
action = " store_true " ,
required = False ,
help = " Set to true if you do not want this worker generating NSFW images. " ,
)
parser . add_argument (
" --horde_blacklist " ,
nargs = " + " ,
required = False ,
help = " List the words that you want to blacklist. " ,
)
parser . add_argument (
" --horde_censorlist " ,
nargs = " + " ,
required = False ,
help = " List the words that you want to censor. " ,
)
parser . add_argument (
" --horde_censor_nsfw " ,
action = " store_true " ,
required = False ,
help = " Set to true if you want this bridge worker to censor NSFW images. " ,
)
2022-09-07 01:50:14 +03:00
opt = parser . parse_args ( )
2023-06-23 05:58:20 +03:00
# Should not be needed anymore
# os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID" # see issue #152
2022-09-07 01:50:14 +03:00
# all selected gpus, can probably be done nicer
2023-06-23 05:58:20 +03:00
# if opt.extra_models_gpu:
2022-09-07 01:50:14 +03:00
# gpus = set([opt.gpu, opt.esrgan_gpu, opt.gfpgan_gpu])
# os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(str(g) for g in set(gpus))
2023-06-23 05:58:20 +03:00
# else:
2022-09-07 01:50:14 +03:00
# os.environ["CUDA_VISIBLE_DEVICES"] = str(opt.gpu)
import gradio as gr
import k_diffusion as K
import math
import mimetypes
import numpy as np
import pynvml
import random
import threading , asyncio
import time
import torch
import torch . nn as nn
import yaml
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
import copy
2023-06-23 05:58:20 +03:00
from typing import List , Union , Callable
2022-09-07 01:50:14 +03:00
from pathlib import Path
from collections import namedtuple
2022-09-14 23:48:56 +03:00
from functools import partial
2022-09-07 01:50:14 +03:00
2022-09-11 14:47:34 +03:00
# tell the user which GPU the code is actually using
2023-06-23 05:58:20 +03:00
if os . getenv ( " SD_WEBUI_DEBUG " , " False " ) . lower ( ) in ( " true " , " 1 " , " y " ) :
2022-09-11 14:47:34 +03:00
gpu_in_use = opt . gpu
# prioritize --esrgan-gpu and --gfpgan-gpu over --gpu, as stated in the option info
if opt . esrgan_gpu != opt . gpu :
gpu_in_use = opt . esrgan_gpu
elif opt . gfpgan_gpu != opt . gpu :
gpu_in_use = opt . gfpgan_gpu
2023-06-23 05:58:20 +03:00
print (
" Starting on GPU {selected_gpu_name} " . format (
selected_gpu_name = torch . cuda . get_device_name ( gpu_in_use )
)
)
2022-09-07 01:50:14 +03:00
2023-06-23 05:58:20 +03:00
from contextlib import nullcontext
2022-09-07 01:50:14 +03:00
from einops import rearrange , repeat
from itertools import islice
from omegaconf import OmegaConf
2022-09-10 01:54:19 +03:00
from PIL import Image , ImageFont , ImageDraw , ImageFilter , ImageOps , ImageChops
2022-09-07 01:50:14 +03:00
from io import BytesIO
import base64
import re
from torch import autocast
from ldm . models . diffusion . ddim import DDIMSampler
from ldm . models . diffusion . plms import PLMSSampler
from ldm . util import instantiate_from_config
2023-06-23 05:58:20 +03:00
2022-09-09 20:33:04 +03:00
# add global options to models
def patch_conv ( * * patch ) :
cls = torch . nn . Conv2d
init = cls . __init__
2023-06-23 05:58:20 +03:00
2022-09-09 20:33:04 +03:00
def __init__ ( self , * args , * * kwargs ) :
return init ( self , * args , * * kwargs , * * patch )
2023-06-23 05:58:20 +03:00
2022-09-09 20:33:04 +03:00
cls . __init__ = __init__
2023-06-23 05:58:20 +03:00
2022-09-09 20:33:04 +03:00
if opt . tiling :
2023-06-23 05:58:20 +03:00
patch_conv ( padding_mode = " circular " )
2022-09-09 20:33:04 +03:00
print ( " patched for tiling " )
2022-09-07 01:50:14 +03:00
try :
# this silences the annoying "Some weights of the model checkpoint were not used when initializing..." message at start.
from transformers import logging
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
logging . set_verbosity_error ( )
except :
pass
2023-06-23 05:58:20 +03:00
from diffusers . pipelines . stable_diffusion . safety_checker import (
StableDiffusionSafetyChecker ,
)
2022-09-11 12:32:36 +03:00
from transformers import AutoFeatureExtractor
# load safety model
safety_model_id = " CompVis/stable-diffusion-safety-checker "
safety_feature_extractor = None
safety_checker = None
2022-09-07 01:50:14 +03:00
# this is a fix for Windows users. Without it, javascript files will be served with text/html content-type and the bowser will not show any UI
mimetypes . init ( )
2023-06-23 05:58:20 +03:00
mimetypes . add_type ( " application/javascript " , " .js " )
2022-09-07 01:50:14 +03:00
# some of those options should not be changed at all because they would break the model, so I removed them from options.
opt_C = 4
opt_f = 8
2023-06-23 05:58:20 +03:00
LANCZOS = Image . Resampling . LANCZOS if hasattr ( Image , " Resampling " ) else Image . LANCZOS
2022-09-07 01:50:14 +03:00
invalid_filename_chars = ' <>: " / \ |?* \n '
GFPGAN_dir = opt . gfpgan_dir
RealESRGAN_dir = opt . realesrgan_dir
LDSR_dir = opt . ldsr_dir
if opt . optimized_turbo :
opt . optimized = True
if opt . no_job_manager :
job_manager = None
else :
job_manager = JobManager ( opt . max_jobs )
2023-06-23 05:58:20 +03:00
opt . max_jobs + = 1 # Leave a free job open for button clicks
2022-09-07 01:50:14 +03:00
# should probably be moved to a settings menu in the UI at some point
2023-06-23 05:58:20 +03:00
grid_format = [ s . lower ( ) for s in opt . grid_format . split ( " : " ) ]
2022-09-07 01:50:14 +03:00
grid_lossless = False
grid_quality = 100
2023-06-23 05:58:20 +03:00
if grid_format [ 0 ] == " png " :
grid_ext = " png "
grid_format = " png "
elif grid_format [ 0 ] in [ " jpg " , " jpeg " ] :
2022-09-07 01:50:14 +03:00
grid_quality = int ( grid_format [ 1 ] ) if len ( grid_format ) > 1 else 100
2023-06-23 05:58:20 +03:00
grid_ext = " jpg "
grid_format = " jpeg "
elif grid_format [ 0 ] == " webp " :
2022-09-07 01:50:14 +03:00
grid_quality = int ( grid_format [ 1 ] ) if len ( grid_format ) > 1 else 100
2023-06-23 05:58:20 +03:00
grid_ext = " webp "
grid_format = " webp "
if grid_quality < 0 : # e.g. webp:-100 for lossless mode
2022-09-07 01:50:14 +03:00
grid_lossless = True
grid_quality = abs ( grid_quality )
def chunk ( it , size ) :
it = iter ( it )
return iter ( lambda : tuple ( islice ( it , size ) ) , ( ) )
def load_model_from_config ( config , ckpt , verbose = False ) :
print ( f " Loading model from { ckpt } " )
pl_sd = torch . load ( ckpt , map_location = " cpu " )
if " global_step " in pl_sd :
print ( f " Global Step: { pl_sd [ ' global_step ' ] } " )
sd = pl_sd [ " state_dict " ]
model = instantiate_from_config ( config . model )
m , u = model . load_state_dict ( sd , strict = False )
if len ( m ) > 0 and verbose :
print ( " missing keys: " )
print ( m )
if len ( u ) > 0 and verbose :
print ( " unexpected keys: " )
print ( u )
model . cuda ( )
model . eval ( )
return model
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
def load_sd_from_config ( ckpt , verbose = False ) :
print ( f " Loading model from { ckpt } " )
pl_sd = torch . load ( ckpt , map_location = " cpu " )
if " global_step " in pl_sd :
print ( f " Global Step: { pl_sd [ ' global_step ' ] } " )
sd = pl_sd [ " state_dict " ]
return sd
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
def crash ( e , s ) :
global model
global device
2023-06-23 05:58:20 +03:00
print ( s , " \n " , e )
2022-09-07 01:50:14 +03:00
try :
del model
del device
except :
try :
del device
except :
pass
pass
2023-06-23 05:58:20 +03:00
print ( " exiting...calling os._exit(0) " )
2022-09-07 01:50:14 +03:00
t = threading . Timer ( 0.25 , os . _exit , args = [ 0 ] )
t . start ( )
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
class MemUsageMonitor ( threading . Thread ) :
stop_flag = False
max_usage = 0
total = - 1
def __init__ ( self , name ) :
threading . Thread . __init__ ( self )
self . name = name
def run ( self ) :
try :
pynvml . nvmlInit ( )
except :
2023-06-23 05:58:20 +03:00
print (
f " [ { self . name } ] Unable to initialize NVIDIA management. No memory stats. \n "
)
2022-09-07 01:50:14 +03:00
return
print ( f " [ { self . name } ] Recording max memory usage... \n " )
2022-09-11 04:41:29 +03:00
# check if we're using a scoped-down GPU environment (pynvml does not listen to CUDA_VISIBLE_DEVICES)
# so that we can measure memory on the correct GPU
try :
isinstance ( int ( os . environ [ " CUDA_VISIBLE_DEVICES " ] ) , int )
2023-06-23 05:58:20 +03:00
handle = pynvml . nvmlDeviceGetHandleByIndex (
int ( os . environ [ " CUDA_VISIBLE_DEVICES " ] )
)
2022-09-11 04:41:29 +03:00
except ( KeyError , ValueError ) as pynvmlHandleError :
2023-06-23 05:58:20 +03:00
if os . getenv ( " SD_WEBUI_DEBUG " , " False " ) . lower ( ) in ( " true " , " 1 " , " y " ) :
2022-09-11 14:47:34 +03:00
print ( " [MemMon][WARNING] " , pynvmlHandleError )
2023-06-23 05:58:20 +03:00
print (
" [MemMon][INFO] " ,
" defaulting to monitoring memory on the default gpu (set via --gpu flag) " ,
)
2022-09-11 04:41:29 +03:00
handle = pynvml . nvmlDeviceGetHandleByIndex ( opt . gpu )
2022-09-07 01:50:14 +03:00
self . total = pynvml . nvmlDeviceGetMemoryInfo ( handle ) . total
while not self . stop_flag :
m = pynvml . nvmlDeviceGetMemoryInfo ( handle )
self . max_usage = max ( self . max_usage , m . used )
# print(self.max_usage)
time . sleep ( 0.1 )
print ( f " [ { self . name } ] Stopped recording. \n " )
pynvml . nvmlShutdown ( )
def read ( self ) :
return self . max_usage , self . total
def stop ( self ) :
self . stop_flag = True
def read_and_stop ( self ) :
self . stop_flag = True
return self . max_usage , self . total
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
class CFGMaskedDenoiser ( nn . Module ) :
def __init__ ( self , model ) :
super ( ) . __init__ ( )
self . inner_model = model
def forward ( self , x , sigma , uncond , cond , cond_scale , mask , x0 , xi ) :
x_in = x
x_in = torch . cat ( [ x_in ] * 2 )
sigma_in = torch . cat ( [ sigma ] * 2 )
cond_in = torch . cat ( [ uncond , cond ] )
uncond , cond = self . inner_model ( x_in , sigma_in , cond = cond_in ) . chunk ( 2 )
denoised = uncond + ( cond - uncond ) * cond_scale
if mask is not None :
assert x0 is not None
img_orig = x0
2023-06-23 05:58:20 +03:00
mask_inv = 1.0 - mask
2022-09-07 01:50:14 +03:00
denoised = ( img_orig * mask_inv ) + ( mask * denoised )
return denoised
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
class CFGDenoiser ( nn . Module ) :
def __init__ ( self , model ) :
super ( ) . __init__ ( )
self . inner_model = model
def forward ( self , x , sigma , uncond , cond , cond_scale ) :
x_in = torch . cat ( [ x ] * 2 )
sigma_in = torch . cat ( [ sigma ] * 2 )
cond_in = torch . cat ( [ uncond , cond ] )
uncond , cond = self . inner_model ( x_in , sigma_in , cond = cond_in ) . chunk ( 2 )
return uncond + ( cond - uncond ) * cond_scale
class KDiffusionSampler :
def __init__ ( self , m , sampler ) :
self . model = m
self . model_wrap = K . external . CompVisDenoiser ( m )
self . schedule = sampler
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
def get_sampler_name ( self ) :
return self . schedule
2023-06-23 05:58:20 +03:00
def sample (
self ,
S ,
conditioning ,
batch_size ,
shape ,
verbose ,
unconditional_guidance_scale ,
unconditional_conditioning ,
eta ,
x_T ,
img_callback : Callable = None ,
) :
2022-09-07 01:50:14 +03:00
sigmas = self . model_wrap . get_sigmas ( S )
x = x_T * sigmas [ 0 ]
model_wrap_cfg = CFGDenoiser ( self . model_wrap )
2023-06-23 05:58:20 +03:00
samples_ddim = K . sampling . __dict__ [ f " sample_ { self . schedule } " ] (
model_wrap_cfg ,
x ,
sigmas ,
extra_args = {
" cond " : conditioning ,
" uncond " : unconditional_conditioning ,
" cond_scale " : unconditional_guidance_scale ,
} ,
disable = False ,
callback = partial ( KDiffusionSampler . img_callback_wrapper , img_callback ) ,
)
2022-09-07 01:50:14 +03:00
return samples_ddim , None
2022-09-14 23:48:56 +03:00
@classmethod
def img_callback_wrapper ( cls , callback : Callable , * args ) :
2023-06-23 05:58:20 +03:00
""" Converts a KDiffusion callback to the standard img_callback """
2022-09-14 23:48:56 +03:00
if callback :
arg_dict = args [ 0 ]
2023-06-23 05:58:20 +03:00
callback ( image_sample = arg_dict [ " denoised " ] , iter_num = arg_dict [ " i " ] )
2022-09-07 01:50:14 +03:00
def create_random_tensors ( shape , seeds ) :
xs = [ ]
for seed in seeds :
torch . manual_seed ( seed )
# randn results depend on device; gpu and cpu get different results for same seed;
# the way I see it, it's better to do this on CPU, so that everyone gets same result;
# but the original script had it like this so i do not dare change it for now because
# it will break everyone's seeds.
xs . append ( torch . randn ( shape , device = device ) )
x = torch . stack ( xs )
return x
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
def torch_gc ( ) :
torch . cuda . empty_cache ( )
torch . cuda . ipc_collect ( )
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
def load_LDSR ( checking = False ) :
2023-06-23 05:58:20 +03:00
model_name = " model "
yaml_name = " project "
model_path = os . path . join ( LDSR_dir , model_name + " .ckpt " )
yaml_path = os . path . join ( LDSR_dir , yaml_name + " .yaml " )
2022-09-07 01:50:14 +03:00
if not os . path . isfile ( model_path ) :
2023-06-23 05:58:20 +03:00
raise Exception ( " LDSR model not found at path " + model_path )
2022-09-07 01:50:14 +03:00
if not os . path . isfile ( yaml_path ) :
2023-06-23 05:58:20 +03:00
raise Exception ( " LDSR model not found at path " + yaml_path )
if checking is True :
2022-09-07 01:50:14 +03:00
return True
sys . path . append ( os . path . abspath ( LDSR_dir ) )
from LDSR import LDSR
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
LDSRObject = LDSR ( model_path , yaml_path )
return LDSRObject
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
def load_GFPGAN ( checking = False ) :
2023-06-23 05:58:20 +03:00
model_name = " GFPGANv1.3 "
model_path = os . path . join ( GFPGAN_dir , model_name + " .pth " )
2022-09-07 01:50:14 +03:00
if not os . path . isfile ( model_path ) :
2023-06-23 05:58:20 +03:00
raise Exception ( " GFPGAN model not found at path " + model_path )
if checking is True :
2022-09-07 01:50:14 +03:00
return True
sys . path . append ( os . path . abspath ( GFPGAN_dir ) )
from gfpgan import GFPGANer
if opt . gfpgan_cpu or opt . extra_models_cpu :
2023-06-23 05:58:20 +03:00
instance = GFPGANer (
model_path = model_path ,
upscale = 1 ,
arch = " clean " ,
channel_multiplier = 2 ,
bg_upsampler = None ,
device = torch . device ( " cpu " ) ,
)
2022-09-07 01:50:14 +03:00
elif opt . extra_models_gpu :
2023-06-23 05:58:20 +03:00
instance = GFPGANer (
model_path = model_path ,
upscale = 1 ,
arch = " clean " ,
channel_multiplier = 2 ,
bg_upsampler = None ,
device = torch . device ( f " cuda: { opt . gfpgan_gpu } " ) ,
)
2022-09-07 01:50:14 +03:00
else :
2023-06-23 05:58:20 +03:00
instance = GFPGANer (
model_path = model_path ,
upscale = 1 ,
arch = " clean " ,
channel_multiplier = 2 ,
bg_upsampler = None ,
device = torch . device ( f " cuda: { opt . gpu } " ) ,
)
2022-09-07 01:50:14 +03:00
return instance
2023-06-23 05:58:20 +03:00
def load_RealESRGAN ( model_name : str , checking = False ) :
2022-09-07 01:50:14 +03:00
from basicsr . archs . rrdbnet_arch import RRDBNet
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
RealESRGAN_models = {
2023-06-23 05:58:20 +03:00
" RealESRGAN_x4plus " : RRDBNet (
num_in_ch = 3 ,
num_out_ch = 3 ,
num_feat = 64 ,
num_block = 23 ,
num_grow_ch = 32 ,
scale = 4 ,
) ,
" RealESRGAN_x4plus_anime_6B " : RRDBNet (
num_in_ch = 3 , num_out_ch = 3 , num_feat = 64 , num_block = 6 , num_grow_ch = 32 , scale = 4
) ,
2022-09-07 01:50:14 +03:00
}
2023-06-23 05:58:20 +03:00
model_path = os . path . join ( RealESRGAN_dir , model_name + " .pth " )
2022-09-07 01:50:14 +03:00
if not os . path . isfile ( model_path ) :
2023-06-23 05:58:20 +03:00
raise Exception ( model_name + " .pth not found at path " + model_path )
if checking is True :
2022-09-07 01:50:14 +03:00
return True
sys . path . append ( os . path . abspath ( RealESRGAN_dir ) )
from realesrgan import RealESRGANer
if opt . esrgan_cpu or opt . extra_models_cpu :
2023-06-23 05:58:20 +03:00
instance = RealESRGANer (
scale = 2 ,
model_path = model_path ,
model = RealESRGAN_models [ model_name ] ,
pre_pad = 0 ,
half = False ,
) # cpu does not support half
instance . device = torch . device ( " cpu " )
instance . model . to ( " cpu " )
2022-09-07 01:50:14 +03:00
elif opt . extra_models_gpu :
2023-06-23 05:58:20 +03:00
instance = RealESRGANer (
scale = 2 ,
model_path = model_path ,
model = RealESRGAN_models [ model_name ] ,
pre_pad = 0 ,
half = not opt . no_half ,
gpu_id = opt . esrgan_gpu ,
)
2022-09-07 01:50:14 +03:00
else :
2023-06-23 05:58:20 +03:00
instance = RealESRGANer (
scale = 2 ,
model_path = model_path ,
model = RealESRGAN_models [ model_name ] ,
pre_pad = 0 ,
half = not opt . no_half ,
)
2022-09-07 01:50:14 +03:00
instance . model . name = model_name
return instance
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
GFPGAN = None
if os . path . exists ( GFPGAN_dir ) :
try :
GFPGAN = load_GFPGAN ( checking = True )
print ( " Found GFPGAN " )
except Exception :
import traceback
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
print ( " Error loading GFPGAN: " , file = sys . stderr )
print ( traceback . format_exc ( ) , file = sys . stderr )
RealESRGAN = None
2023-06-23 05:58:20 +03:00
def try_loading_RealESRGAN ( model_name : str , checking = False ) :
2022-09-07 01:50:14 +03:00
global RealESRGAN
if os . path . exists ( RealESRGAN_dir ) :
try :
2023-06-23 05:58:20 +03:00
RealESRGAN = load_RealESRGAN (
model_name , checking
) # TODO: Should try to load both models before giving up
if checking is True :
2022-09-07 01:50:14 +03:00
print ( " Found RealESRGAN " )
return True
2023-06-23 05:58:20 +03:00
print ( " Loaded RealESRGAN with model " + RealESRGAN . model . name )
2022-09-07 01:50:14 +03:00
except Exception :
import traceback
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
print ( " Error loading RealESRGAN: " , file = sys . stderr )
print ( traceback . format_exc ( ) , file = sys . stderr )
2023-06-23 05:58:20 +03:00
try_loading_RealESRGAN ( " RealESRGAN_x4plus " , checking = True )
2022-09-07 01:50:14 +03:00
LDSR = None
2023-06-23 05:58:20 +03:00
def try_loading_LDSR ( model_name : str , checking = False ) :
2022-09-07 01:50:14 +03:00
global LDSR
if os . path . exists ( LDSR_dir ) :
try :
2023-06-23 05:58:20 +03:00
LDSR = load_LDSR (
checking = True
) # TODO: Should try to load both models before giving up
if checking is True :
2022-09-07 01:50:14 +03:00
print ( " Found LDSR " )
return True
print ( " Latent Diffusion Super Sampling (LDSR) model loaded " )
except Exception :
import traceback
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
print ( " Error loading LDSR: " , file = sys . stderr )
print ( traceback . format_exc ( ) , file = sys . stderr )
else :
2023-06-23 05:58:20 +03:00
print (
" LDSR not found at path, please make sure you have cloned the LDSR repo to ./models/ldsr/ "
)
try_loading_LDSR ( " model " , checking = True )
2022-09-07 01:50:14 +03:00
def load_SD_model ( ) :
if opt . optimized :
sd = load_sd_from_config ( opt . ckpt )
li , lo = [ ] , [ ]
for key , v_ in sd . items ( ) :
2023-06-23 05:58:20 +03:00
sp = key . split ( " . " )
if ( sp [ 0 ] ) == " model " :
if " input_blocks " in sp :
2022-09-07 01:50:14 +03:00
li . append ( key )
2023-06-23 05:58:20 +03:00
elif " middle_block " in sp :
2022-09-07 01:50:14 +03:00
li . append ( key )
2023-06-23 05:58:20 +03:00
elif " time_embed " in sp :
2022-09-07 01:50:14 +03:00
li . append ( key )
else :
lo . append ( key )
for key in li :
2023-06-23 05:58:20 +03:00
sd [ " model1. " + key [ 6 : ] ] = sd . pop ( key )
2022-09-07 01:50:14 +03:00
for key in lo :
2023-06-23 05:58:20 +03:00
sd [ " model2. " + key [ 6 : ] ] = sd . pop ( key )
2022-09-07 01:50:14 +03:00
config = OmegaConf . load ( " optimizedSD/v1-inference.yaml " )
2023-06-23 05:58:20 +03:00
device = (
torch . device ( f " cuda: { opt . gpu } " )
if torch . cuda . is_available ( )
else torch . device ( " cpu " )
)
2022-09-07 01:50:14 +03:00
model = instantiate_from_config ( config . modelUNet )
_ , _ = model . load_state_dict ( sd , strict = False )
model . cuda ( )
model . eval ( )
model . turbo = opt . optimized_turbo
modelCS = instantiate_from_config ( config . modelCondStage )
_ , _ = modelCS . load_state_dict ( sd , strict = False )
modelCS . cond_stage_model . device = device
modelCS . eval ( )
modelFS = instantiate_from_config ( config . modelFirstStage )
_ , _ = modelFS . load_state_dict ( sd , strict = False )
modelFS . eval ( )
del sd
if not opt . no_half :
model = model . half ( )
modelCS = modelCS . half ( )
modelFS = modelFS . half ( )
2023-06-23 05:58:20 +03:00
return model , modelCS , modelFS , device , config
2022-09-07 01:50:14 +03:00
else :
config = OmegaConf . load ( opt . config )
model = load_model_from_config ( config , opt . ckpt )
2023-06-23 05:58:20 +03:00
device = (
torch . device ( f " cuda: { opt . gpu } " )
if torch . cuda . is_available ( )
else torch . device ( " cpu " )
)
2022-09-07 01:50:14 +03:00
model = ( model if opt . no_half else model . half ( ) ) . to ( device )
2023-06-23 05:58:20 +03:00
return model , device , config
2022-09-07 01:50:14 +03:00
if opt . optimized :
2023-06-23 05:58:20 +03:00
model , modelCS , modelFS , device , config = load_SD_model ( )
2022-09-07 01:50:14 +03:00
else :
2023-06-23 05:58:20 +03:00
model , device , config = load_SD_model ( )
2022-09-07 01:50:14 +03:00
def load_embeddings ( fp ) :
if fp is not None and hasattr ( model , " embedding_manager " ) :
model . embedding_manager . load ( fp . name )
def get_font ( fontsize ) :
fonts = [ " arial.ttf " , " DejaVuSans.ttf " ]
for font_name in fonts :
try :
return ImageFont . truetype ( font_name , fontsize )
except OSError :
2023-06-23 05:58:20 +03:00
pass
2022-09-07 01:50:14 +03:00
# ImageFont.load_default() is practically unusable as it only supports
# latin1, so raise an exception instead if no usable font was found
raise Exception ( f " No usable font found (tried { ' , ' . join ( fonts ) } ) " )
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
def image_grid ( imgs , batch_size , force_n_rows = None , captions = None ) :
if force_n_rows is not None :
rows = force_n_rows
elif opt . n_rows > 0 :
rows = opt . n_rows
elif opt . n_rows == 0 :
rows = batch_size
else :
rows = math . sqrt ( len ( imgs ) )
rows = round ( rows )
cols = math . ceil ( len ( imgs ) / rows )
w , h = imgs [ 0 ] . size
2023-06-23 05:58:20 +03:00
grid = Image . new ( " RGB " , size = ( cols * w , rows * h ) , color = " black " )
2022-09-07 01:50:14 +03:00
fnt = get_font ( 30 )
for i , img in enumerate ( imgs ) :
grid . paste ( img , box = ( i % cols * w , i / / cols * h ) )
2023-06-23 05:58:20 +03:00
if captions and i < len ( captions ) :
d = ImageDraw . Draw ( grid )
size = d . textbbox (
( 0 , 0 ) , captions [ i ] , font = fnt , stroke_width = 2 , align = " center "
)
d . multiline_text (
( i % cols * w + w / 2 , i / / cols * h + h - size [ 3 ] ) ,
captions [ i ] ,
font = fnt ,
fill = ( 255 , 255 , 255 ) ,
stroke_width = 2 ,
stroke_fill = ( 0 , 0 , 0 ) ,
anchor = " mm " ,
align = " center " ,
)
2022-09-07 01:50:14 +03:00
return grid
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
def seed_to_int ( s ) :
if type ( s ) is int :
return s
2023-06-23 05:58:20 +03:00
if s is None or s == " " :
2022-09-07 01:50:14 +03:00
return random . randint ( 0 , 2 * * 32 - 1 )
n = abs ( int ( s ) if s . isdigit ( ) else random . Random ( s ) . randint ( 0 , 2 * * 32 - 1 ) )
while n > = 2 * * 32 :
n = n >> 32
return n
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
def draw_prompt_matrix ( im , width , height , all_prompts ) :
def wrap ( text , d , font , line_length ) :
2023-06-23 05:58:20 +03:00
lines = [ " " ]
2022-09-07 01:50:14 +03:00
for word in text . split ( ) :
2023-06-23 05:58:20 +03:00
line = f " { lines [ - 1 ] } { word } " . strip ( )
2022-09-07 01:50:14 +03:00
if d . textlength ( line , font = font ) < = line_length :
lines [ - 1 ] = line
else :
lines . append ( word )
2023-06-23 05:58:20 +03:00
return " \n " . join ( lines )
2022-09-07 01:50:14 +03:00
def draw_texts ( pos , x , y , texts , sizes ) :
for i , ( text , size ) in enumerate ( zip ( texts , sizes ) ) :
active = pos & ( 1 << i ) != 0
if not active :
2023-06-23 05:58:20 +03:00
text = " \u0336 " . join ( text ) + " \u0336 "
d . multiline_text (
( x , y + size [ 1 ] / 2 ) ,
text ,
font = fnt ,
fill = color_active if active else color_inactive ,
anchor = " mm " ,
align = " center " ,
)
2022-09-07 01:50:14 +03:00
y + = size [ 1 ] + line_spacing
fontsize = ( width + height ) / / 25
line_spacing = fontsize / / 2
fnt = get_font ( fontsize )
color_active = ( 0 , 0 , 0 )
color_inactive = ( 153 , 153 , 153 )
pad_top = height / / 4
pad_left = width * 3 / / 4 if len ( all_prompts ) > 2 else 0
cols = im . width / / width
rows = im . height / / height
prompts = all_prompts [ 1 : ]
result = Image . new ( " RGB " , ( im . width + pad_left , im . height + pad_top ) , " white " )
result . paste ( im , ( pad_left , pad_top ) )
d = ImageDraw . Draw ( result )
boundary = math . ceil ( len ( prompts ) / 2 )
prompts_horiz = [ wrap ( x , d , fnt , width ) for x in prompts [ : boundary ] ]
prompts_vert = [ wrap ( x , d , fnt , pad_left ) for x in prompts [ boundary : ] ]
2023-06-23 05:58:20 +03:00
sizes_hor = [
( x [ 2 ] - x [ 0 ] , x [ 3 ] - x [ 1 ] )
for x in [ d . multiline_textbbox ( ( 0 , 0 ) , x , font = fnt ) for x in prompts_horiz ]
]
sizes_ver = [
( x [ 2 ] - x [ 0 ] , x [ 3 ] - x [ 1 ] )
for x in [ d . multiline_textbbox ( ( 0 , 0 ) , x , font = fnt ) for x in prompts_vert ]
]
2022-09-07 01:50:14 +03:00
hor_text_height = sum ( [ x [ 1 ] + line_spacing for x in sizes_hor ] ) - line_spacing
ver_text_height = sum ( [ x [ 1 ] + line_spacing for x in sizes_ver ] ) - line_spacing
for col in range ( cols ) :
x = pad_left + width * col + width / 2
y = pad_top / 2 - hor_text_height / 2
draw_texts ( col , x , y , prompts_horiz , sizes_hor )
for row in range ( rows ) :
x = pad_left / 2
y = pad_top + height * row + height / 2 - ver_text_height / 2
draw_texts ( row , x , y , prompts_vert , sizes_ver )
return result
def check_prompt_length ( prompt , comments ) :
""" this function tests if prompt is too long, and if so, adds a message to comments """
tokenizer = ( model if not opt . optimized else modelCS ) . cond_stage_model . tokenizer
max_length = ( model if not opt . optimized else modelCS ) . cond_stage_model . max_length
2023-06-23 05:58:20 +03:00
info = ( model if not opt . optimized else modelCS ) . cond_stage_model . tokenizer (
[ prompt ] ,
truncation = True ,
max_length = max_length ,
return_overflowing_tokens = True ,
padding = " max_length " ,
return_tensors = " pt " ,
)
ovf = info [ " overflowing_tokens " ] [ 0 ]
2022-09-07 01:50:14 +03:00
overflowing_count = ovf . shape [ 0 ]
if overflowing_count == 0 :
return
vocab = { v : k for k , v in tokenizer . get_vocab ( ) . items ( ) }
overflowing_words = [ vocab . get ( int ( x ) , " " ) for x in ovf ]
2023-06-23 05:58:20 +03:00
overflowing_text = tokenizer . convert_tokens_to_string ( " " . join ( overflowing_words ) )
2022-09-07 01:50:14 +03:00
2023-06-23 05:58:20 +03:00
comments . append (
f " Warning: too many input tokens; some ( { len ( overflowing_words ) } ) have been truncated: \n { overflowing_text } \n "
)
2022-09-07 01:50:14 +03:00
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
2023-06-23 05:58:20 +03:00
def save_sample (
image ,
sample_path_i ,
filename ,
jpg_sample ,
write_info_files ,
write_sample_info_to_log_file ,
prompt_matrix ,
init_img ,
uses_loopback ,
uses_random_seed_loopback ,
skip_save ,
skip_grid ,
sort_samples ,
sampler_name ,
ddim_eta ,
n_iter ,
batch_size ,
i ,
denoising_strength ,
resize_mode ,
skip_metadata = False ,
) :
""" saves the image according to selected parameters. Expects to find generation parameters on image, set by ImageMetadata.set_on_image() """
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
metadata = ImageMetadata . get_from_image ( image )
if not skip_metadata and metadata is None :
2023-06-23 05:58:20 +03:00
print (
" No metadata passed in to save. Set metadata on the image before calling save_sample using the ImageMetadata.set_on_image() function. "
)
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
skip_metadata = True
2022-09-07 01:50:14 +03:00
filename_i = os . path . join ( sample_path_i , filename )
if not jpg_sample :
if opt . save_metadata and not skip_metadata :
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
image . save ( f " { filename_i } .png " , pnginfo = metadata . as_png_info ( ) )
2022-09-07 01:50:14 +03:00
else :
image . save ( f " { filename_i } .png " )
else :
2023-06-23 05:58:20 +03:00
image . save ( f " { filename_i } .jpg " , " jpeg " , quality = 100 , optimize = True )
2022-09-07 01:50:14 +03:00
if write_info_files or write_sample_info_to_log_file :
# toggles differ for txt2img vs. img2img:
offset = 0 if init_img is None else 2
toggles = [ ]
if prompt_matrix :
toggles . append ( 0 )
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
if metadata . normalize_prompt_weights :
2022-09-07 01:50:14 +03:00
toggles . append ( 1 )
if init_img is not None :
if uses_loopback :
toggles . append ( 2 )
if uses_random_seed_loopback :
toggles . append ( 3 )
if not skip_save :
toggles . append ( 2 + offset )
if not skip_grid :
toggles . append ( 3 + offset )
if sort_samples :
toggles . append ( 4 + offset )
if write_info_files :
toggles . append ( 5 + offset )
if write_sample_info_to_log_file :
2023-06-23 05:58:20 +03:00
toggles . append ( 6 + offset )
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
if metadata . GFPGAN :
2022-09-07 01:50:14 +03:00
toggles . append ( 7 + offset )
info_dict = dict (
target = " txt2img " if init_img is None else " img2img " ,
2023-06-23 05:58:20 +03:00
prompt = metadata . prompt ,
ddim_steps = metadata . steps ,
toggles = toggles ,
sampler_name = sampler_name ,
ddim_eta = ddim_eta ,
n_iter = n_iter ,
batch_size = batch_size ,
cfg_scale = metadata . cfg_scale ,
seed = metadata . seed ,
width = metadata . width ,
height = metadata . height ,
2022-09-07 01:50:14 +03:00
)
if init_img is not None :
# Not yet any use for these, but they bloat up the files:
2023-06-23 05:58:20 +03:00
# info_dict["init_img"] = init_img
# info_dict["init_mask"] = init_mask
2022-09-07 01:50:14 +03:00
info_dict [ " denoising_strength " ] = denoising_strength
info_dict [ " resize_mode " ] = resize_mode
if write_info_files :
with open ( f " { filename_i } .yaml " , " w " , encoding = " utf8 " ) as f :
yaml . dump ( info_dict , f , allow_unicode = True , width = 10000 )
if write_sample_info_to_log_file :
ignore_list = [ " prompt " , " target " , " toggles " , " ddim_eta " , " batch_size " ]
2023-06-23 05:58:20 +03:00
rename_dict = {
" ddim_steps " : " steps " ,
" n_iter " : " number " ,
" sampler_name " : " sampler " ,
} # changes the name of parameters to match with dynamic parameters
2022-09-07 01:50:14 +03:00
sample_log_path = os . path . join ( sample_path_i , " log.yaml " )
2023-06-23 05:58:20 +03:00
log_dump = info_dict . get (
" prompt "
) # making sure the first item that is listed in the txt is the prompt text
2022-09-07 01:50:14 +03:00
for key , value in info_dict . items ( ) :
if key in ignore_list :
continue
found_key = rename_dict . get ( key )
2023-06-23 05:58:20 +03:00
if (
key == " cfg_scale "
) : # adds zeros to to cfg_scale necessary for dynamic params
2022-09-07 01:50:14 +03:00
value = str ( value ) . zfill ( 2 )
if found_key :
key = found_key
log_dump + = f " { key } { value } "
2023-06-23 05:58:20 +03:00
log_dump = (
log_dump + " \n "
) # space at the end for dynamic params to accept the last param
2022-09-07 01:50:14 +03:00
with open ( sample_log_path , " a " , encoding = " utf8 " ) as log_file :
log_file . write ( log_dump )
2023-06-23 05:58:20 +03:00
def get_next_sequence_number ( path , prefix = " " ) :
2022-09-07 01:50:14 +03:00
"""
Determines and returns the next sequence number to use when saving an
image in the specified directory .
If a prefix is given , only consider files whose names start with that
prefix , and strip the prefix from filenames before extracting their
sequence number .
The sequence starts at 0.
"""
2022-10-02 20:22:27 +03:00
# Because when running in bridge-mode, we do not have a dir
2023-06-23 05:58:20 +03:00
if opt . bridge :
return 0
2022-09-07 01:50:14 +03:00
result = - 1
for p in Path ( path ) . iterdir ( ) :
2023-06-23 05:58:20 +03:00
if p . name . endswith ( ( " .png " , " .jpg " ) ) and p . name . startswith ( prefix ) :
tmp = p . name [ len ( prefix ) : ]
2022-09-07 01:50:14 +03:00
try :
2023-06-23 05:58:20 +03:00
result = max ( int ( tmp . split ( " - " ) [ 0 ] ) , result )
2022-09-07 01:50:14 +03:00
except ValueError :
pass
return result + 1
def oxlamon_matrix ( prompt , seed , n_iter , batch_size ) :
2023-06-23 05:58:20 +03:00
pattern = re . compile ( r " (, \ s) { 2,} " )
2022-09-07 01:50:14 +03:00
class PromptItem :
def __init__ ( self , text , parts , item ) :
self . text = text
self . parts = parts
if item :
2023-06-23 05:58:20 +03:00
self . parts . append ( item )
2022-09-07 01:50:14 +03:00
def clean ( txt ) :
2023-06-23 05:58:20 +03:00
return re . sub ( pattern , " , " , txt )
2022-09-07 01:50:14 +03:00
2023-06-23 05:58:20 +03:00
def getrowcount ( txt ) :
for data in re . finditer ( " .*? \\ ((.*?) \\ ).* " , txt ) :
2022-09-07 01:50:14 +03:00
if data :
return len ( data . group ( 1 ) . split ( " | " ) )
break
return None
2023-06-23 05:58:20 +03:00
def repliter ( txt ) :
for data in re . finditer ( " .*? \\ ((.*?) \\ ).* " , txt ) :
2022-09-07 01:50:14 +03:00
if data :
r = data . span ( 1 )
for item in data . group ( 1 ) . split ( " | " ) :
2023-06-23 05:58:20 +03:00
yield (
clean ( txt [ : r [ 0 ] - 1 ] + item . strip ( ) + txt [ r [ 1 ] + 1 : ] ) ,
item . strip ( ) ,
)
2022-09-07 01:50:14 +03:00
break
2023-06-23 05:58:20 +03:00
def iterlist ( items ) :
2022-09-07 01:50:14 +03:00
outitems = [ ]
for item in items :
for newitem , newpart in repliter ( item . text ) :
2023-06-23 05:58:20 +03:00
outitems . append ( PromptItem ( newitem , item . parts . copy ( ) , newpart ) )
2022-09-07 01:50:14 +03:00
return outitems
2023-06-23 05:58:20 +03:00
def getmatrix ( prompt ) :
dataitems = [ PromptItem ( prompt [ 1 : ] . strip ( ) , [ ] , None ) ]
2022-09-07 01:50:14 +03:00
while True :
2023-06-23 05:58:20 +03:00
newdataitems = iterlist ( dataitems )
if len ( newdataitems ) == 0 :
2022-09-07 01:50:14 +03:00
return dataitems
dataitems = newdataitems
2023-06-23 05:58:20 +03:00
def classToArrays ( items , seed , n_iter ) :
2022-09-07 01:50:14 +03:00
texts = [ ]
parts = [ ]
seeds = [ ]
for item in items :
itemseed = seed
for i in range ( n_iter ) :
2023-06-23 05:58:20 +03:00
texts . append ( item . text )
parts . append ( f " Seed: { itemseed } \n " + " \n " . join ( item . parts ) )
seeds . append ( itemseed )
2022-09-07 01:50:14 +03:00
itemseed + = 1
return seeds , texts , parts
2023-06-23 05:58:20 +03:00
all_seeds , all_prompts , prompt_matrix_parts = classToArrays (
getmatrix ( prompt ) , seed , n_iter
)
2022-09-07 01:50:14 +03:00
n_iter = math . ceil ( len ( all_prompts ) / batch_size )
needrows = getrowcount ( prompt )
if needrows :
xrows = math . sqrt ( len ( all_prompts ) )
xrows = round ( xrows )
# if columns is to much
cols = math . ceil ( len ( all_prompts ) / xrows )
2023-06-23 05:58:20 +03:00
if cols > needrows * 4 :
2022-09-07 01:50:14 +03:00
needrows * = 2
return all_seeds , n_iter , prompt_matrix_parts , all_prompts , needrows
2023-06-23 05:58:20 +03:00
def perform_masked_image_restoration (
image ,
init_img ,
init_mask ,
mask_blur_strength ,
mask_restore ,
use_RealESRGAN ,
RealESRGAN ,
) :
2022-09-11 20:18:17 +03:00
if not mask_restore :
2022-09-10 18:37:42 +03:00
return image
else :
init_mask = init_mask . filter ( ImageFilter . GaussianBlur ( mask_blur_strength ) )
2023-06-23 05:58:20 +03:00
init_mask = init_mask . convert ( " L " )
init_img = init_img . convert ( " RGB " )
image = image . convert ( " RGB " )
2022-09-10 18:37:42 +03:00
if use_RealESRGAN and RealESRGAN is not None :
output , img_mode = RealESRGAN . enhance ( np . array ( init_mask , dtype = np . uint8 ) )
init_mask = Image . fromarray ( output )
2023-06-23 05:58:20 +03:00
init_mask = init_mask . convert ( " L " )
2022-09-07 01:50:14 +03:00
2022-09-10 18:37:42 +03:00
output , img_mode = RealESRGAN . enhance ( np . array ( init_img , dtype = np . uint8 ) )
init_img = Image . fromarray ( output )
2023-06-23 05:58:20 +03:00
init_img = init_img . convert ( " RGB " )
2022-09-10 18:37:42 +03:00
image = Image . composite ( init_img , image , init_mask )
return image
2022-09-10 00:44:30 +03:00
def perform_color_correction ( img_rgb , correction_target_lab , do_color_correction ) :
try :
from skimage import exposure
except :
print ( " Install scikit-image to perform color correction " )
return img_rgb
2022-09-11 20:18:17 +03:00
2023-06-23 05:58:20 +03:00
if not do_color_correction :
return img_rgb
if correction_target_lab is None :
return img_rgb
2022-09-10 00:44:30 +03:00
2023-06-23 05:58:20 +03:00
return Image . fromarray (
cv2 . cvtColor (
exposure . match_histograms (
cv2 . cvtColor ( np . asarray ( img_rgb ) , cv2 . COLOR_RGB2LAB ) ,
2022-09-10 00:44:30 +03:00
correction_target_lab ,
2023-06-23 05:58:20 +03:00
channel_axis = 2 ,
) ,
cv2 . COLOR_LAB2RGB ,
) . astype ( " uint8 " )
2022-09-11 20:18:17 +03:00
)
2022-09-07 01:50:14 +03:00
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
def process_images (
2023-06-23 05:58:20 +03:00
outpath ,
func_init ,
func_sample ,
prompt ,
seed ,
sampler_name ,
skip_grid ,
skip_save ,
batch_size ,
n_iter ,
steps ,
cfg_scale ,
width ,
height ,
prompt_matrix ,
filter_nsfw ,
use_GFPGAN ,
use_RealESRGAN ,
realesrgan_model_name ,
fp ,
ddim_eta = 0.0 ,
do_not_save_grid = False ,
normalize_prompt_weights = True ,
init_img = None ,
init_mask = None ,
keep_mask = False ,
mask_blur_strength = 3 ,
mask_restore = False ,
denoising_strength = 0.75 ,
resize_mode = None ,
uses_loopback = False ,
uses_random_seed_loopback = False ,
sort_samples = True ,
write_info_files = True ,
write_sample_info_to_log_file = False ,
jpg_sample = False ,
variant_amount = 0.0 ,
variant_seed = None ,
imgProcessorTask = False ,
job_info : JobInfo = None ,
do_color_correction = False ,
correction_target = None ,
) :
2022-09-07 01:50:14 +03:00
""" this is the main loop that both txt2img and img2img use; it calls func_init once inside all the scopes and func_sample once per batch """
2022-09-11 12:32:36 +03:00
def numpy_to_pil ( images ) :
"""
Convert a numpy image or a batch of images to a PIL image .
"""
if images . ndim == 3 :
images = images [ None , . . . ]
images = ( images * 255 ) . round ( ) . astype ( " uint8 " )
pil_images = [ Image . fromarray ( image ) for image in images ]
return pil_images
# load replacement of nsfw content
def load_replacement ( x ) :
try :
hwc = x . shape
y = Image . open ( " images/nsfw.jpeg " ) . convert ( " RGB " ) . resize ( ( hwc [ 1 ] , hwc [ 0 ] ) )
2023-06-23 05:58:20 +03:00
y = ( np . array ( y ) / 255.0 ) . astype ( x . dtype )
2022-09-11 12:32:36 +03:00
assert y . shape == x . shape
return y
except Exception :
return x
# check and replace nsfw content
def check_safety ( x_image ) :
global safety_feature_extractor , safety_checker
if safety_feature_extractor is None :
2023-06-23 05:58:20 +03:00
safety_feature_extractor = AutoFeatureExtractor . from_pretrained (
safety_model_id
)
safety_checker = StableDiffusionSafetyChecker . from_pretrained (
safety_model_id
)
safety_checker_input = safety_feature_extractor (
numpy_to_pil ( x_image ) , return_tensors = " pt "
)
x_checked_image , has_nsfw_concept = safety_checker (
images = x_image , clip_input = safety_checker_input . pixel_values
)
2022-09-11 12:32:36 +03:00
for i in range ( len ( has_nsfw_concept ) ) :
if has_nsfw_concept [ i ] :
x_checked_image [ i ] = load_replacement ( x_checked_image [ i ] )
return x_checked_image , has_nsfw_concept
2023-06-23 05:58:20 +03:00
prompt = prompt or " "
2022-09-07 01:50:14 +03:00
torch_gc ( )
# start time after garbage collection (or before?)
start_time = time . time ( )
2023-06-23 05:58:20 +03:00
mem_mon = MemUsageMonitor ( " MemMon " )
2022-09-07 01:50:14 +03:00
mem_mon . start ( )
if hasattr ( model , " embedding_manager " ) :
load_embeddings ( fp )
2022-10-02 20:22:27 +03:00
if not opt . bridge :
os . makedirs ( outpath , exist_ok = True )
2022-09-07 01:50:14 +03:00
sample_path = os . path . join ( outpath , " samples " )
2022-10-02 20:22:27 +03:00
if not opt . bridge :
os . makedirs ( sample_path , exist_ok = True )
2022-09-07 01:50:14 +03:00
2023-06-23 05:58:20 +03:00
if " | " not in prompt and prompt . startswith ( " @ " ) :
2022-09-07 01:50:14 +03:00
prompt = prompt [ 1 : ]
2023-06-23 05:58:20 +03:00
negprompt = " "
if " ### " in prompt :
prompt , negprompt = prompt . split ( " ### " , 1 )
2022-09-11 23:39:00 +03:00
prompt = prompt . strip ( )
negprompt = negprompt . strip ( )
2022-09-07 01:50:14 +03:00
comments = [ ]
prompt_matrix_parts = [ ]
simple_templating = False
add_original_image = True
if prompt_matrix :
if prompt . startswith ( " @ " ) :
simple_templating = True
add_original_image = not ( use_RealESRGAN or use_GFPGAN )
2023-06-23 05:58:20 +03:00
all_seeds , n_iter , prompt_matrix_parts , all_prompts , frows = oxlamon_matrix (
prompt , seed , n_iter , batch_size
)
2022-09-07 01:50:14 +03:00
else :
all_prompts = [ ]
prompt_matrix_parts = prompt . split ( " | " )
combination_count = 2 * * ( len ( prompt_matrix_parts ) - 1 )
for combination_num in range ( combination_count ) :
current = prompt_matrix_parts [ 0 ]
for n , text in enumerate ( prompt_matrix_parts [ 1 : ] ) :
2023-06-23 05:58:20 +03:00
if combination_num & ( 2 * * n ) > 0 :
2022-09-07 01:50:14 +03:00
current + = ( " " if text . strip ( ) . startswith ( " , " ) else " , " ) + text
all_prompts . append ( current )
n_iter = math . ceil ( len ( all_prompts ) / batch_size )
all_seeds = len ( all_prompts ) * [ seed ]
2023-06-23 05:58:20 +03:00
print (
f " Prompt matrix will create { len ( all_prompts ) } images using a total of { n_iter } batches. "
)
2022-09-07 01:50:14 +03:00
else :
if not opt . no_verify_input :
try :
check_prompt_length ( prompt , comments )
except :
import traceback
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
print ( " Error verifying input: " , file = sys . stderr )
print ( traceback . format_exc ( ) , file = sys . stderr )
all_prompts = batch_size * n_iter * [ prompt ]
all_seeds = [ seed + x for x in range ( len ( all_prompts ) ) ]
original_seeds = all_seeds . copy ( )
precision_scope = autocast if opt . precision == " autocast " else nullcontext
if job_info :
output_images = job_info . images
else :
output_images = [ ]
grid_captions = [ ]
stats = [ ]
2023-06-23 05:58:20 +03:00
with torch . no_grad ( ) , precision_scope ( " cuda " ) , (
model . ema_scope ( ) if not opt . optimized else nullcontext ( )
) :
2022-09-07 01:50:14 +03:00
init_data = func_init ( )
2023-06-23 05:58:20 +03:00
time . time ( )
2022-09-07 01:50:14 +03:00
# if variant_amount > 0.0 create noise from base seed
base_x = None
if variant_amount > 0.0 :
2023-06-23 05:58:20 +03:00
target_seed_randomizer = seed_to_int ( " " ) # random seed
torch . manual_seed (
seed
) # this has to be the single starting seed (not per-iteration)
base_x = create_random_tensors (
[ opt_C , height / / opt_f , width / / opt_f ] , seeds = [ seed ]
)
2022-09-07 01:50:14 +03:00
# we don't want all_seeds to be sequential from starting seed with variants,
# since that makes the same variants each time,
# so we add target_seed_randomizer as a random offset
for si in range ( len ( all_seeds ) ) :
all_seeds [ si ] + = target_seed_randomizer
for n in range ( n_iter ) :
if job_info and job_info . should_stop . is_set ( ) :
print ( " Early exit requested " )
break
print ( f " Iteration: { n + 1 } / { n_iter } " )
2023-06-23 05:58:20 +03:00
prompts = all_prompts [ n * batch_size : ( n + 1 ) * batch_size ]
captions = prompt_matrix_parts [ n * batch_size : ( n + 1 ) * batch_size ]
seeds = all_seeds [ n * batch_size : ( n + 1 ) * batch_size ]
current_seeds = original_seeds [ n * batch_size : ( n + 1 ) * batch_size ]
2022-09-07 01:50:14 +03:00
if job_info :
2023-06-23 05:58:20 +03:00
job_info . job_status = (
f " Processing Iteration { n + 1 } / { n_iter } . Batch size { batch_size } "
)
2022-09-14 23:48:56 +03:00
job_info . rec_steps_imgs . clear ( )
2023-06-23 05:58:20 +03:00
for idx , ( p , s ) in enumerate ( zip ( prompts , seeds ) ) :
2022-09-07 01:50:14 +03:00
job_info . job_status + = f " \n Item { idx } : Seed { s } \n Prompt: { p } "
2022-09-11 22:21:38 +03:00
print ( f " Current prompt: { p } " )
2022-09-07 01:50:14 +03:00
if opt . optimized :
modelCS . to ( device )
2023-06-23 05:58:20 +03:00
uc = ( model if not opt . optimized else modelCS ) . get_learned_conditioning (
len ( prompts ) * [ negprompt ]
)
2022-09-07 01:50:14 +03:00
if isinstance ( prompts , tuple ) :
prompts = list ( prompts )
# split the prompt if it has : for weighting
# TODO for speed it might help to have this occur when all_prompts filled??
2023-06-23 05:58:20 +03:00
weighted_subprompts = split_weighted_subprompts (
prompts [ 0 ] , normalize_prompt_weights
)
2022-09-07 01:50:14 +03:00
# sub-prompt weighting used if more than 1
if len ( weighted_subprompts ) > 1 :
2023-06-23 05:58:20 +03:00
c = torch . zeros_like (
uc
) # i dont know if this is correct.. but it works
2022-09-07 01:50:14 +03:00
for i in range ( 0 , len ( weighted_subprompts ) ) :
# note if alpha negative, it functions same as torch.sub
2023-06-23 05:58:20 +03:00
c = torch . add (
c ,
(
model if not opt . optimized else modelCS
) . get_learned_conditioning ( weighted_subprompts [ i ] [ 0 ] ) ,
alpha = weighted_subprompts [ i ] [ 1 ] ,
)
else : # just behave like usual
c = ( model if not opt . optimized else modelCS ) . get_learned_conditioning (
prompts
)
2022-09-07 01:50:14 +03:00
shape = [ opt_C , height / / opt_f , width / / opt_f ]
if opt . optimized :
2023-06-23 05:58:20 +03:00
mem = torch . cuda . memory_allocated ( ) / 1e6
2022-09-07 01:50:14 +03:00
modelCS . to ( " cpu " )
2023-06-23 05:58:20 +03:00
while torch . cuda . memory_allocated ( ) / 1e6 > = mem :
2022-09-07 01:50:14 +03:00
time . sleep ( 1 )
2022-09-11 20:18:17 +03:00
cur_variant_amount = variant_amount
2022-09-07 01:50:14 +03:00
if variant_amount == 0.0 :
# we manually generate all input noises because each one should have a specific seed
x = create_random_tensors ( shape , seeds = seeds )
2023-06-23 05:58:20 +03:00
else : # we are making variants
2022-09-07 01:50:14 +03:00
# using variant_seed as sneaky toggle,
# when not None or '' use the variant_seed
# otherwise use seeds
2023-06-23 05:58:20 +03:00
if variant_seed is not None and variant_seed != " " :
2022-09-07 01:50:14 +03:00
specified_variant_seed = seed_to_int ( variant_seed )
torch . manual_seed ( specified_variant_seed )
2023-06-23 05:58:20 +03:00
target_x = create_random_tensors (
shape , seeds = [ specified_variant_seed ]
)
2022-09-07 01:50:14 +03:00
# with a variant seed we would end up with the same variant as the basic seed
# does not change. But we can increase the steps to get an interesting result
# that shows more and more deviation of the original image and let us adjust
# how far we will go (using 10 iterations with variation amount set to 0.02 will
# generate an icreasingly variated image which is very interesting for movies)
2023-06-23 05:58:20 +03:00
cur_variant_amount + = n * variant_amount
2022-09-07 01:50:14 +03:00
else :
target_x = create_random_tensors ( shape , seeds = seeds )
# finally, slerp base_x noise to target_x noise for creating a variant
2023-06-23 05:58:20 +03:00
x = slerp (
device , max ( 0.0 , min ( 1.0 , cur_variant_amount ) ) , base_x , target_x
)
2022-09-07 01:50:14 +03:00
2022-09-14 23:48:56 +03:00
# If optimized then use first stage for preview and store it on cpu until needed
if opt . optimized :
step_preview_model = modelFS
step_preview_model . cpu ( )
else :
step_preview_model = model
def sample_iteration_callback ( image_sample : torch . Tensor , iter_num : int ) :
2023-06-23 05:58:20 +03:00
""" Called from the sampler every iteration """
2022-09-14 23:48:56 +03:00
if job_info :
job_info . active_iteration_cnt = iter_num
2023-06-23 05:58:20 +03:00
record_periodic_image = job_info . rec_steps_enabled and (
0 == iter_num % job_info . rec_steps_intrvl
)
if (
record_periodic_image
or job_info . refresh_active_image_requested . is_set ( )
) :
2022-09-14 23:48:56 +03:00
preview_start_time = time . time ( )
if opt . optimized :
step_preview_model . to ( device )
decoded_batch : List [ torch . Tensor ] = [ ]
# Break up batch to save VRAM
for sample in image_sample :
2023-06-23 05:58:20 +03:00
sample = sample [
None , :
] # expands the tensor as if it still had a batch dimension
decoded_sample = step_preview_model . decode_first_stage (
sample
) [ 0 ]
decoded_sample = torch . clamp (
( decoded_sample + 1.0 ) / 2.0 , min = 0.0 , max = 1.0
)
2022-09-14 23:48:56 +03:00
decoded_sample = decoded_sample . cpu ( )
decoded_batch . append ( decoded_sample )
batch_size = len ( decoded_batch )
if opt . optimized :
step_preview_model . cpu ( )
images : List [ Image . Image ] = [ ]
# Convert tensor to image (copied from code below)
for ddim in decoded_batch :
2023-06-23 05:58:20 +03:00
x_sample = 255.0 * rearrange ( ddim . numpy ( ) , " c h w -> h w c " )
2022-09-14 23:48:56 +03:00
x_sample = x_sample . astype ( np . uint8 )
image = Image . fromarray ( x_sample )
images . append ( image )
caption = f " Iter { iter_num } "
2023-06-23 05:58:20 +03:00
grid = image_grid (
images ,
len ( images ) ,
force_n_rows = 1 ,
captions = [ caption ] * len ( images ) ,
)
2022-09-14 23:48:56 +03:00
# Save the images if recording steps, and append existing saved steps
if job_info . rec_steps_enabled :
2023-06-23 05:58:20 +03:00
gallery_img_size = tuple (
int ( 0.25 * dim ) for dim in images [ 0 ] . size
)
job_info . rec_steps_imgs . append (
grid . resize ( gallery_img_size )
)
2022-09-14 23:48:56 +03:00
# Notify the requester that the image is updated
if job_info . refresh_active_image_requested . is_set ( ) :
if job_info . rec_steps_enabled :
2023-06-23 05:58:20 +03:00
grid_rows = (
None
if batch_size == 1
else len ( job_info . rec_steps_imgs )
)
grid = image_grid (
imgs = job_info . rec_steps_imgs [ : : - 1 ] ,
batch_size = 1 ,
force_n_rows = grid_rows ,
)
2022-09-14 23:48:56 +03:00
job_info . active_image = grid
job_info . refresh_active_image_done . set ( )
job_info . refresh_active_image_requested . clear ( )
preview_elapsed_timed = time . time ( ) - preview_start_time
if preview_elapsed_timed / job_info . rec_steps_intrvl > 1 :
print (
2023-06-23 05:58:20 +03:00
f " Warning: Preview generation is slowing image generation. It took { preview_elapsed_timed : .2f } s to generate progress images for batch of { batch_size } images! "
)
2022-09-14 23:48:56 +03:00
# Interrupt current iteration?
if job_info . stop_cur_iter . is_set ( ) :
job_info . stop_cur_iter . clear ( )
raise StopIteration ( )
try :
2023-06-23 05:58:20 +03:00
samples_ddim = func_sample (
init_data = init_data ,
x = x ,
conditioning = c ,
unconditional_conditioning = uc ,
sampler_name = sampler_name ,
img_callback = sample_iteration_callback ,
)
2022-09-14 23:48:56 +03:00
except StopIteration :
print ( " Skipping iteration " )
job_info . job_status = " Skipping iteration "
continue
2022-09-07 01:50:14 +03:00
if opt . optimized :
modelFS . to ( device )
2022-09-07 21:00:04 +03:00
for i in range ( len ( samples_ddim ) ) :
2023-06-23 05:58:20 +03:00
x_samples_ddim = (
model if not opt . optimized else modelFS
) . decode_first_stage ( samples_ddim [ i ] . unsqueeze ( 0 ) )
2022-09-07 21:00:04 +03:00
x_sample = torch . clamp ( ( x_samples_ddim + 1.0 ) / 2.0 , min = 0.0 , max = 1.0 )
2022-09-07 01:50:14 +03:00
2022-09-11 12:32:36 +03:00
if filter_nsfw :
x_samples_ddim_numpy = x_sample . cpu ( ) . permute ( 0 , 2 , 3 , 1 ) . numpy ( )
2023-06-23 05:58:20 +03:00
x_checked_image , has_nsfw_concept = check_safety (
x_samples_ddim_numpy
)
2022-09-11 12:32:36 +03:00
x_sample = torch . from_numpy ( x_checked_image ) . permute ( 0 , 3 , 1 , 2 )
2022-09-07 01:50:14 +03:00
2023-06-23 05:58:20 +03:00
sanitized_prompt = (
prompts [ i ]
. replace ( " " , " _ " )
. translate ( { ord ( x ) : " " for x in invalid_filename_chars } )
)
if variant_seed is not None and variant_seed != " " :
2022-09-07 01:50:14 +03:00
if variant_amount == 0.0 :
seed_used = f " { current_seeds [ i ] } - { variant_seed } "
else :
seed_used = f " { seed } - { variant_seed } "
else :
2023-06-23 05:58:20 +03:00
seed_used = f " { current_seeds [ i ] } "
2022-09-07 01:50:14 +03:00
if sort_samples :
2023-06-23 05:58:20 +03:00
sanitized_prompt = sanitized_prompt [ : 128 ] # 200 is too long
2022-09-07 01:50:14 +03:00
sample_path_i = os . path . join ( sample_path , sanitized_prompt )
2022-10-02 20:22:27 +03:00
if not opt . bridge :
os . makedirs ( sample_path_i , exist_ok = True )
2022-09-07 01:50:14 +03:00
base_count = get_next_sequence_number ( sample_path_i )
2023-06-23 05:58:20 +03:00
filename = (
opt . filename_format
or " [STEPS]_[SAMPLER]_[SEED]_[VARIANT_AMOUNT] "
)
2022-09-07 01:50:14 +03:00
else :
sample_path_i = sample_path
base_count = get_next_sequence_number ( sample_path_i )
2023-06-23 05:58:20 +03:00
filename = (
opt . filename_format
or " [STEPS]_[SAMPLER]_[SEED]_[VARIANT_AMOUNT]_[PROMPT] "
)
2022-09-09 20:20:59 +03:00
2023-06-23 05:58:20 +03:00
# Add new filenames tags here
2022-09-09 20:20:59 +03:00
filename = f " { base_count : 05 } - " + filename
filename = filename . replace ( " [STEPS] " , str ( steps ) )
filename = filename . replace ( " [CFG] " , str ( cfg_scale ) )
filename = filename . replace ( " [PROMPT] " , sanitized_prompt [ : 128 ] )
2023-06-23 05:58:20 +03:00
filename = filename . replace (
" [PROMPT_SPACES] " ,
prompts [ i ] . translate ( { ord ( x ) : " " for x in invalid_filename_chars } ) [
: 128
] ,
)
2022-09-09 20:20:59 +03:00
filename = filename . replace ( " [WIDTH] " , str ( width ) )
filename = filename . replace ( " [HEIGHT] " , str ( height ) )
filename = filename . replace ( " [SAMPLER] " , sampler_name )
filename = filename . replace ( " [SEED] " , seed_used )
2023-06-23 05:58:20 +03:00
filename = filename . replace (
" [VARIANT_AMOUNT] " , f " { cur_variant_amount : .2f } "
)
2022-09-07 01:50:14 +03:00
2023-06-23 05:58:20 +03:00
x_sample = 255.0 * rearrange (
x_sample [ 0 ] . cpu ( ) . numpy ( ) , " c h w -> h w c "
)
2022-09-07 01:50:14 +03:00
x_sample = x_sample . astype ( np . uint8 )
2023-06-23 05:58:20 +03:00
metadata = ImageMetadata (
prompt = prompts [ i ] ,
seed = seeds [ i ] ,
height = height ,
width = width ,
steps = steps ,
cfg_scale = cfg_scale ,
normalize_prompt_weights = normalize_prompt_weights ,
denoising_strength = denoising_strength ,
GFPGAN = use_GFPGAN ,
)
2022-09-07 01:50:14 +03:00
image = Image . fromarray ( x_sample )
2023-06-23 05:58:20 +03:00
image = perform_color_correction (
image , correction_target , do_color_correction
)
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
ImageMetadata . set_on_image ( image , metadata )
2022-09-07 01:50:14 +03:00
original_sample = x_sample
original_filename = filename
if use_GFPGAN and GFPGAN is not None and not use_RealESRGAN :
2023-06-23 05:58:20 +03:00
skip_save = True # #287 >_>
2022-09-07 01:50:14 +03:00
torch_gc ( )
2023-06-23 05:58:20 +03:00
cropped_faces , restored_faces , restored_img = GFPGAN . enhance (
original_sample [ : , : , : : - 1 ] ,
has_aligned = False ,
only_center_face = False ,
paste_back = True ,
)
gfpgan_sample = restored_img [ : , : , : : - 1 ]
2022-09-07 01:50:14 +03:00
gfpgan_image = Image . fromarray ( gfpgan_sample )
2023-06-23 05:58:20 +03:00
gfpgan_image = perform_color_correction (
gfpgan_image , correction_target , do_color_correction
)
2022-09-10 18:37:42 +03:00
gfpgan_image = perform_masked_image_restoration (
2023-06-23 05:58:20 +03:00
gfpgan_image ,
init_img ,
init_mask ,
mask_blur_strength ,
mask_restore ,
use_RealESRGAN = False ,
RealESRGAN = None ,
2022-09-11 20:18:17 +03:00
)
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
gfpgan_metadata = copy . copy ( metadata )
gfpgan_metadata . GFPGAN = True
2023-06-23 05:58:20 +03:00
ImageMetadata . set_on_image ( gfpgan_image , gfpgan_metadata )
gfpgan_filename = original_filename + " -gfpgan "
save_sample (
gfpgan_image ,
sample_path_i ,
gfpgan_filename ,
jpg_sample ,
write_info_files ,
write_sample_info_to_log_file ,
prompt_matrix ,
init_img ,
uses_loopback ,
uses_random_seed_loopback ,
skip_save ,
skip_grid ,
sort_samples ,
sampler_name ,
ddim_eta ,
n_iter ,
batch_size ,
i ,
denoising_strength ,
resize_mode ,
skip_metadata = False ,
)
output_images . append ( gfpgan_image ) # 287
# if simple_templating:
2022-09-07 01:50:14 +03:00
# grid_captions.append( captions[i] + "\ngfpgan" )
if use_RealESRGAN and RealESRGAN is not None and not use_GFPGAN :
2023-06-23 05:58:20 +03:00
skip_save = True # #287 >_>
2022-09-07 01:50:14 +03:00
torch_gc ( )
2023-06-23 05:58:20 +03:00
output , img_mode = RealESRGAN . enhance ( original_sample [ : , : , : : - 1 ] )
esrgan_filename = original_filename + " -esrgan4x "
esrgan_sample = output [ : , : , : : - 1 ]
2022-09-07 01:50:14 +03:00
esrgan_image = Image . fromarray ( esrgan_sample )
2023-06-23 05:58:20 +03:00
esrgan_image = perform_color_correction (
esrgan_image , correction_target , do_color_correction
)
2022-09-10 18:37:42 +03:00
esrgan_image = perform_masked_image_restoration (
2023-06-23 05:58:20 +03:00
esrgan_image ,
init_img ,
init_mask ,
mask_blur_strength ,
mask_restore ,
use_RealESRGAN ,
RealESRGAN ,
)
ImageMetadata . set_on_image ( esrgan_image , metadata )
save_sample (
esrgan_image ,
sample_path_i ,
esrgan_filename ,
jpg_sample ,
write_info_files ,
write_sample_info_to_log_file ,
prompt_matrix ,
init_img ,
uses_loopback ,
uses_random_seed_loopback ,
skip_save ,
skip_grid ,
sort_samples ,
sampler_name ,
ddim_eta ,
n_iter ,
batch_size ,
i ,
denoising_strength ,
resize_mode ,
skip_metadata = False ,
2022-09-10 18:37:42 +03:00
)
2023-06-23 05:58:20 +03:00
output_images . append ( esrgan_image ) # 287
# if simple_templating:
2022-09-07 01:50:14 +03:00
# grid_captions.append( captions[i] + "\nesrgan" )
2023-06-23 05:58:20 +03:00
if (
use_RealESRGAN
and RealESRGAN is not None
and use_GFPGAN
and GFPGAN is not None
) :
skip_save = True # #287 >_>
2022-09-07 01:50:14 +03:00
torch_gc ( )
2023-06-23 05:58:20 +03:00
cropped_faces , restored_faces , restored_img = GFPGAN . enhance (
x_sample [ : , : , : : - 1 ] ,
has_aligned = False ,
only_center_face = False ,
paste_back = True ,
)
gfpgan_sample = restored_img [ : , : , : : - 1 ]
output , img_mode = RealESRGAN . enhance ( gfpgan_sample [ : , : , : : - 1 ] )
gfpgan_esrgan_filename = original_filename + " -gfpgan-esrgan4x "
gfpgan_esrgan_sample = output [ : , : , : : - 1 ]
2022-09-07 01:50:14 +03:00
gfpgan_esrgan_image = Image . fromarray ( gfpgan_esrgan_sample )
2023-06-23 05:58:20 +03:00
gfpgan_esrgan_image = perform_color_correction (
gfpgan_esrgan_image , correction_target , do_color_correction
)
2022-09-10 18:37:42 +03:00
gfpgan_esrgan_image = perform_masked_image_restoration (
2023-06-23 05:58:20 +03:00
gfpgan_esrgan_image ,
init_img ,
init_mask ,
mask_blur_strength ,
mask_restore ,
use_RealESRGAN ,
RealESRGAN ,
2022-09-10 18:37:42 +03:00
)
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
ImageMetadata . set_on_image ( gfpgan_esrgan_image , metadata )
2023-06-23 05:58:20 +03:00
save_sample (
gfpgan_esrgan_image ,
sample_path_i ,
gfpgan_esrgan_filename ,
jpg_sample ,
write_info_files ,
write_sample_info_to_log_file ,
prompt_matrix ,
init_img ,
uses_loopback ,
uses_random_seed_loopback ,
skip_save ,
skip_grid ,
sort_samples ,
sampler_name ,
ddim_eta ,
n_iter ,
batch_size ,
i ,
denoising_strength ,
resize_mode ,
skip_metadata = False ,
)
output_images . append ( gfpgan_esrgan_image ) # 287
# if simple_templating:
2022-09-07 01:50:14 +03:00
# grid_captions.append( captions[i] + "\ngfpgan_esrgan" )
# this flag is used for imgProcessorTasks like GoBig, will return the image without saving it
2023-06-23 05:58:20 +03:00
if imgProcessorTask is True :
2022-09-07 01:50:14 +03:00
output_images . append ( image )
2022-09-10 18:37:42 +03:00
image = perform_masked_image_restoration (
2023-06-23 05:58:20 +03:00
image ,
init_img ,
init_mask ,
mask_blur_strength ,
mask_restore ,
2022-09-10 18:37:42 +03:00
# RealESRGAN image already processed in if-case above.
2023-06-23 05:58:20 +03:00
use_RealESRGAN = False ,
RealESRGAN = None ,
2022-09-10 18:37:42 +03:00
)
2022-09-07 01:50:14 +03:00
if not skip_save :
2023-06-23 05:58:20 +03:00
save_sample (
image ,
sample_path_i ,
filename ,
jpg_sample ,
write_info_files ,
write_sample_info_to_log_file ,
prompt_matrix ,
init_img ,
uses_loopback ,
uses_random_seed_loopback ,
skip_save ,
skip_grid ,
sort_samples ,
sampler_name ,
ddim_eta ,
n_iter ,
batch_size ,
i ,
denoising_strength ,
resize_mode ,
False ,
)
2022-09-07 01:50:14 +03:00
if add_original_image or not simple_templating :
output_images . append ( image )
if simple_templating :
2023-06-23 05:58:20 +03:00
grid_captions . append ( captions [ i ] )
2022-09-07 01:50:14 +03:00
2022-09-14 23:48:56 +03:00
# Save the progress images?
if job_info :
2023-06-23 05:58:20 +03:00
if job_info . rec_steps_enabled and (
job_info . rec_steps_to_file or job_info . rec_steps_to_gallery
) :
2022-09-14 23:48:56 +03:00
steps_grid = image_grid ( job_info . rec_steps_imgs , 1 )
if job_info . rec_steps_to_gallery :
2023-06-23 05:58:20 +03:00
gallery_img_size = tuple ( 2 * dim for dim in image . size )
output_images . append ( steps_grid . resize ( gallery_img_size ) )
2022-09-14 23:48:56 +03:00
if job_info . rec_steps_to_file :
steps_grid_filename = f " { original_filename } _step_grid "
2023-06-23 05:58:20 +03:00
save_sample (
steps_grid ,
sample_path_i ,
steps_grid_filename ,
jpg_sample ,
write_info_files ,
write_sample_info_to_log_file ,
prompt_matrix ,
init_img ,
uses_loopback ,
uses_random_seed_loopback ,
skip_save ,
skip_grid ,
sort_samples ,
sampler_name ,
ddim_eta ,
n_iter ,
batch_size ,
i ,
denoising_strength ,
resize_mode ,
False ,
)
2022-09-14 23:48:56 +03:00
2022-09-07 01:50:14 +03:00
if opt . optimized :
2023-06-23 05:58:20 +03:00
mem = torch . cuda . memory_allocated ( ) / 1e6
2022-09-07 01:50:14 +03:00
modelFS . to ( " cpu " )
2023-06-23 05:58:20 +03:00
while torch . cuda . memory_allocated ( ) / 1e6 > = mem :
2022-09-07 01:50:14 +03:00
time . sleep ( 1 )
if ( prompt_matrix or not skip_grid ) and not do_not_save_grid :
grid = None
if prompt_matrix :
if simple_templating :
2023-06-23 05:58:20 +03:00
grid = image_grid (
output_images ,
batch_size ,
force_n_rows = frows ,
captions = grid_captions ,
)
2022-09-07 01:50:14 +03:00
else :
2023-06-23 05:58:20 +03:00
grid = image_grid (
output_images ,
batch_size ,
force_n_rows = 1 << ( ( len ( prompt_matrix_parts ) - 1 ) / / 2 ) ,
)
2022-09-07 01:50:14 +03:00
try :
2023-06-23 05:58:20 +03:00
grid = draw_prompt_matrix (
grid , width , height , prompt_matrix_parts
)
2022-09-07 01:50:14 +03:00
except :
import traceback
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
print ( " Error creating prompt_matrix text: " , file = sys . stderr )
print ( traceback . format_exc ( ) , file = sys . stderr )
2023-06-23 05:58:20 +03:00
elif len ( output_images ) > 0 and ( batch_size > 1 or n_iter > 1 ) :
2022-09-07 01:50:14 +03:00
grid = image_grid ( output_images , batch_size )
if grid is not None :
2023-06-23 05:58:20 +03:00
grid_count = get_next_sequence_number ( outpath , " grid- " )
2022-09-07 01:50:14 +03:00
grid_file = f " grid- { grid_count : 05 } - { seed } _ { prompts [ i ] . replace ( ' ' , ' _ ' ) . translate ( { ord ( x ) : ' ' for x in invalid_filename_chars } ) [ : 128 ] } . { grid_ext } "
2023-06-23 05:58:20 +03:00
grid . save (
os . path . join ( outpath , grid_file ) ,
grid_format ,
quality = grid_quality ,
lossless = grid_lossless ,
optimize = True ,
)
2022-09-23 14:32:36 +03:00
if prompt_matrix :
output_images . append ( grid )
2022-09-07 01:50:14 +03:00
2023-06-23 05:58:20 +03:00
time . time ( )
2022-09-07 01:50:14 +03:00
mem_max_used , mem_total = mem_mon . read_and_stop ( )
2023-06-23 05:58:20 +03:00
time_diff = time . time ( ) - start_time
2022-09-07 01:50:14 +03:00
args_and_names = {
" seed " : seed ,
" width " : width ,
" height " : height ,
" steps " : steps ,
" cfg_scale " : cfg_scale ,
" sampler " : sampler_name ,
}
2023-06-23 05:58:20 +03:00
full_string = f " { prompt } \n " + " " . join ( [ f " { k } : " for k , v in args_and_names . items ( ) ] )
2022-09-07 01:50:14 +03:00
info = {
2023-06-23 05:58:20 +03:00
" text " : full_string ,
" entities " : [
{
" entity " : str ( v ) ,
" start " : full_string . find ( f " { k } : " ) ,
" end " : full_string . find ( f " { k } : " ) + len ( f " { k } " ) ,
}
for k , v in args_and_names . items ( )
] ,
}
# info = f"""
# {prompt} --seed {seed} --W {width} --H {height} -s {steps} -C {cfg_scale} --sampler {sampler_name} {', Denoising strength: '+str(denoising_strength) if init_img is not None else ''}{', GFPGAN' if use_GFPGAN and GFPGAN is not None else ''}{', '+realesrgan_model_name if use_RealESRGAN and RealESRGAN is not None else ''}{', Prompt Matrix Mode.' if prompt_matrix else ''}""".strip()
stats = f """
2022-09-07 01:50:14 +03:00
Took { round ( time_diff , 2 ) } s total ( { round ( time_diff / ( len ( all_prompts ) ) , 2 ) } s per image )
2023-06-23 05:58:20 +03:00
Peak memory usage : { - ( mem_max_used / / - 1_048_576 ) } MiB / { - ( mem_total / / - 1_048_576 ) } MiB / { round ( mem_max_used / mem_total * 100 , 3 ) } % """
2022-09-07 01:50:14 +03:00
for comment in comments :
2023-06-23 05:58:20 +03:00
info [ " text " ] + = " \n \n " + comment
2022-09-07 01:50:14 +03:00
2023-06-23 05:58:20 +03:00
# mem_mon.stop()
# del mem_mon
2022-09-07 01:50:14 +03:00
torch_gc ( )
return output_images , seed , info , stats
2022-10-02 20:22:27 +03:00
def txt2img (
2023-06-23 05:58:20 +03:00
prompt : str ,
ddim_steps : int = 50 ,
sampler_name : str = " k_lms " ,
toggles : List [ int ] = [ 1 , 4 ] ,
realesrgan_model_name : str = " " ,
ddim_eta : float = 0.0 ,
n_iter : int = 1 ,
batch_size : int = 1 ,
cfg_scale : float = 5.0 ,
seed : Union [ int , str , None ] = None ,
height : int = 512 ,
width : int = 512 ,
fp = None ,
variant_amount : float = 0.0 ,
variant_seed : int = None ,
job_info : JobInfo = None ,
) :
2022-09-07 01:50:14 +03:00
outpath = opt . outdir_txt2img or opt . outdir or " outputs/txt2img-samples "
err = False
seed = seed_to_int ( seed )
prompt_matrix = 0 in toggles
normalize_prompt_weights = 1 in toggles
skip_save = 2 not in toggles
skip_grid = 3 not in toggles
sort_samples = 4 in toggles
write_info_files = 5 in toggles
write_to_one_file = 6 in toggles
jpg_sample = 7 in toggles
2022-09-11 12:32:36 +03:00
filter_nsfw = 8 in toggles
use_GFPGAN = 9 in toggles
use_RealESRGAN = 10 in toggles
2022-09-10 00:44:30 +03:00
do_color_correction = False
correction_target = None
2023-06-23 05:58:20 +03:00
ModelLoader ( [ " model " ] , True , False )
2022-09-07 01:50:14 +03:00
if use_GFPGAN and not use_RealESRGAN :
2023-06-23 05:58:20 +03:00
ModelLoader ( [ " GFPGAN " ] , True , False )
ModelLoader ( [ " RealESRGAN " ] , False , True )
2022-09-07 01:50:14 +03:00
if use_RealESRGAN and not use_GFPGAN :
2023-06-23 05:58:20 +03:00
ModelLoader ( [ " GFPGAN " ] , False , True )
ModelLoader ( [ " RealESRGAN " ] , True , False , realesrgan_model_name )
2022-09-07 01:50:14 +03:00
if use_RealESRGAN and use_GFPGAN :
2023-06-23 05:58:20 +03:00
ModelLoader ( [ " GFPGAN " , " RealESRGAN " ] , True , False , realesrgan_model_name )
if sampler_name == " PLMS " :
2022-09-07 01:50:14 +03:00
sampler = PLMSSampler ( model )
2023-06-23 05:58:20 +03:00
elif sampler_name == " DDIM " :
2022-09-07 01:50:14 +03:00
sampler = DDIMSampler ( model )
2023-06-23 05:58:20 +03:00
elif sampler_name == " k_dpm_2_a " :
sampler = KDiffusionSampler ( model , " dpm_2_ancestral " )
elif sampler_name == " k_dpm_2 " :
sampler = KDiffusionSampler ( model , " dpm_2 " )
elif sampler_name == " k_euler_a " :
sampler = KDiffusionSampler ( model , " euler_ancestral " )
elif sampler_name == " k_euler " :
sampler = KDiffusionSampler ( model , " euler " )
elif sampler_name == " k_heun " :
sampler = KDiffusionSampler ( model , " heun " )
elif sampler_name == " k_lms " :
sampler = KDiffusionSampler ( model , " lms " )
2022-09-07 01:50:14 +03:00
else :
raise Exception ( " Unknown sampler: " + sampler_name )
def init ( ) :
pass
2023-06-23 05:58:20 +03:00
def sample (
init_data ,
x ,
conditioning ,
unconditional_conditioning ,
sampler_name ,
img_callback : Callable = None ,
) :
samples_ddim , _ = sampler . sample (
S = ddim_steps ,
conditioning = conditioning ,
batch_size = int ( x . shape [ 0 ] ) ,
shape = x [ 0 ] . shape ,
verbose = False ,
unconditional_guidance_scale = cfg_scale ,
unconditional_conditioning = unconditional_conditioning ,
eta = ddim_eta ,
x_T = x ,
img_callback = img_callback ,
)
2022-09-07 01:50:14 +03:00
return samples_ddim
try :
output_images , seed , info , stats = process_images (
outpath = outpath ,
func_init = init ,
func_sample = sample ,
prompt = prompt ,
seed = seed ,
sampler_name = sampler_name ,
skip_save = skip_save ,
skip_grid = skip_grid ,
batch_size = batch_size ,
n_iter = n_iter ,
steps = ddim_steps ,
cfg_scale = cfg_scale ,
width = width ,
height = height ,
prompt_matrix = prompt_matrix ,
2022-09-11 12:32:36 +03:00
filter_nsfw = filter_nsfw ,
2022-09-07 01:50:14 +03:00
use_GFPGAN = use_GFPGAN ,
use_RealESRGAN = use_RealESRGAN ,
realesrgan_model_name = realesrgan_model_name ,
fp = fp ,
ddim_eta = ddim_eta ,
normalize_prompt_weights = normalize_prompt_weights ,
sort_samples = sort_samples ,
write_info_files = write_info_files ,
write_sample_info_to_log_file = write_to_one_file ,
jpg_sample = jpg_sample ,
variant_amount = variant_amount ,
variant_seed = variant_seed ,
job_info = job_info ,
2022-09-10 00:44:30 +03:00
do_color_correction = do_color_correction ,
2023-06-23 05:58:20 +03:00
correction_target = correction_target ,
2022-09-07 01:50:14 +03:00
)
del sampler
return output_images , seed , info , stats
except RuntimeError as e :
err = e
err_msg = f ' CRASHED:<br><textarea rows= " 5 " style= " color:white;background: black;width: -webkit-fill-available;font-family: monospace;font-size: small;font-weight: bold; " > { str ( e ) } </textarea><br><br>Please wait while the program restarts. '
stats = err_msg
2023-06-23 05:58:20 +03:00
return [ ] , seed , " err " , stats
2022-09-07 01:50:14 +03:00
finally :
if err :
2023-06-23 05:58:20 +03:00
crash ( err , " !!Runtime error (txt2img)!! " )
2022-09-07 01:50:14 +03:00
class Flagging ( gr . FlaggingCallback ) :
def setup ( self , components , flagging_dir : str ) :
pass
def flag ( self , flag_data , flag_option = None , flag_index = None , username = None ) :
import csv
os . makedirs ( " log/images " , exist_ok = True )
# those must match the "txt2img" function !! + images, seed, comment, stats !! NOTE: changes to UI output must be reflected here too
2023-06-23 05:58:20 +03:00
(
prompt ,
ddim_steps ,
sampler_name ,
toggles ,
ddim_eta ,
n_iter ,
batch_size ,
cfg_scale ,
seed ,
height ,
width ,
fp ,
variant_amount ,
variant_seed ,
images ,
seed ,
comment ,
stats ,
) = flag_data
2022-09-07 01:50:14 +03:00
filenames = [ ]
2023-06-23 05:58:20 +03:00
with open ( " log/log.csv " , " a " , encoding = " utf8 " , newline = " " ) as file :
2022-09-07 01:50:14 +03:00
import time
import base64
at_start = file . tell ( ) == 0
writer = csv . writer ( file )
if at_start :
writer . writerow ( [ " sep=, " ] )
2023-06-23 05:58:20 +03:00
writer . writerow (
[
" prompt " ,
" seed " ,
" width " ,
" height " ,
" sampler " ,
" toggles " ,
" n_iter " ,
" n_samples " ,
" cfg_scale " ,
" steps " ,
" filename " ,
]
)
2022-09-07 01:50:14 +03:00
filename_base = str ( int ( time . time ( ) * 1000 ) )
for i , filedata in enumerate ( images ) :
2023-06-23 05:58:20 +03:00
filename = (
" log/images/ "
+ filename_base
+ ( " " if len ( images ) == 1 else " - " + str ( i + 1 ) )
+ " .png "
)
2022-09-07 01:50:14 +03:00
if filedata . startswith ( " data:image/png;base64, " ) :
2023-06-23 05:58:20 +03:00
filedata = filedata [ len ( " data:image/png;base64, " ) : ]
2022-09-07 01:50:14 +03:00
with open ( filename , " wb " ) as imgfile :
2023-06-23 05:58:20 +03:00
imgfile . write ( base64 . decodebytes ( filedata . encode ( " utf-8 " ) ) )
2022-09-07 01:50:14 +03:00
filenames . append ( filename )
2023-06-23 05:58:20 +03:00
writer . writerow (
[
prompt ,
seed ,
width ,
height ,
sampler_name ,
toggles ,
n_iter ,
batch_size ,
cfg_scale ,
ddim_steps ,
filenames [ 0 ] ,
]
)
2022-09-07 01:50:14 +03:00
print ( " Logged: " , filenames [ 0 ] )
2023-06-23 05:58:20 +03:00
def blurArr ( a , r = 8 ) :
im1 = Image . fromarray ( ( a * 255 ) . astype ( np . int8 ) , " L " )
im2 = im1 . filter ( ImageFilter . GaussianBlur ( radius = r ) )
out = np . array ( im2 ) / 255
2022-09-09 08:24:46 +03:00
return out
2023-06-23 05:58:20 +03:00
def img2img (
prompt : str ,
image_editor_mode : str ,
mask_mode : str ,
mask_blur_strength : int ,
mask_restore : bool ,
ddim_steps : int ,
sampler_name : str ,
toggles : List [ int ] ,
realesrgan_model_name : str ,
n_iter : int ,
cfg_scale : float ,
denoising_strength : float ,
seed : int ,
height : int ,
width : int ,
resize_mode : int ,
init_info : any = None ,
init_info_mask : any = None ,
fp = None ,
job_info : JobInfo = None ,
) :
2022-09-07 22:19:00 +03:00
# print([prompt, image_editor_mode, init_info, init_info_mask, mask_mode,
# mask_blur_strength, ddim_steps, sampler_name, toggles,
# realesrgan_model_name, n_iter, cfg_scale,
# denoising_strength, seed, height, width, resize_mode,
# fp])
2022-09-07 01:50:14 +03:00
outpath = opt . outdir_img2img or opt . outdir or " outputs/img2img-samples "
seed = seed_to_int ( seed )
batch_size = 1
prompt_matrix = 0 in toggles
normalize_prompt_weights = 1 in toggles
loopback = 2 in toggles
random_seed_loopback = 3 in toggles
skip_save = 4 not in toggles
skip_grid = 5 not in toggles
sort_samples = 6 in toggles
write_info_files = 7 in toggles
write_sample_info_to_log_file = 8 in toggles
jpg_sample = 9 in toggles
2022-09-10 00:44:30 +03:00
do_color_correction = 10 in toggles
2022-09-11 12:32:36 +03:00
filter_nsfw = 11 in toggles
use_GFPGAN = 12 in toggles
use_RealESRGAN = 13 in toggles
2023-06-23 05:58:20 +03:00
ModelLoader ( [ " model " ] , True , False )
2022-09-07 01:50:14 +03:00
if use_GFPGAN and not use_RealESRGAN :
2023-06-23 05:58:20 +03:00
ModelLoader ( [ " GFPGAN " ] , True , False )
ModelLoader ( [ " RealESRGAN " ] , False , True )
2022-09-07 01:50:14 +03:00
if use_RealESRGAN and not use_GFPGAN :
2023-06-23 05:58:20 +03:00
ModelLoader ( [ " GFPGAN " ] , False , True )
ModelLoader ( [ " RealESRGAN " ] , True , False , realesrgan_model_name )
2022-09-07 01:50:14 +03:00
if use_RealESRGAN and use_GFPGAN :
2023-06-23 05:58:20 +03:00
ModelLoader ( [ " GFPGAN " , " RealESRGAN " ] , True , False , realesrgan_model_name )
if sampler_name == " DDIM " :
2022-09-07 01:50:14 +03:00
sampler = DDIMSampler ( model )
2023-06-23 05:58:20 +03:00
elif sampler_name == " k_dpm_2_a " :
sampler = KDiffusionSampler ( model , " dpm_2_ancestral " )
elif sampler_name == " k_dpm_2 " :
sampler = KDiffusionSampler ( model , " dpm_2 " )
elif sampler_name == " k_euler_a " :
sampler = KDiffusionSampler ( model , " euler_ancestral " )
elif sampler_name == " k_euler " :
sampler = KDiffusionSampler ( model , " euler " )
elif sampler_name == " k_heun " :
sampler = KDiffusionSampler ( model , " heun " )
elif sampler_name == " k_lms " :
sampler = KDiffusionSampler ( model , " lms " )
2022-09-07 01:50:14 +03:00
else :
raise Exception ( " Unknown sampler: " + sampler_name )
2023-06-23 05:58:20 +03:00
if image_editor_mode == " Mask " :
2022-09-07 01:50:14 +03:00
init_img = init_info_mask [ " image " ]
2023-06-23 05:58:20 +03:00
init_img_transparency = (
ImageOps . invert ( init_img . split ( ) [ - 1 ] )
. convert ( " L " )
. point ( lambda x : 255 if x > 0 else 0 , mode = " 1 " )
)
2022-09-07 01:50:14 +03:00
init_img = init_img . convert ( " RGB " )
init_img = resize_image ( resize_mode , init_img , width , height )
2022-09-07 22:19:00 +03:00
init_img = init_img . convert ( " RGB " )
2022-09-07 01:50:14 +03:00
init_mask = init_info_mask [ " mask " ]
2023-06-23 05:58:20 +03:00
init_mask = ImageChops . lighter (
init_img_transparency , init_mask . convert ( " L " )
) . convert ( " RGBA " )
2022-09-07 22:19:00 +03:00
init_mask = init_mask . convert ( " RGB " )
2022-09-07 01:50:14 +03:00
init_mask = resize_image ( resize_mode , init_mask , width , height )
init_mask = init_mask . convert ( " RGB " )
2022-09-07 22:19:00 +03:00
keep_mask = mask_mode == 0
2022-09-07 01:50:14 +03:00
init_mask = init_mask if keep_mask else ImageOps . invert ( init_mask )
else :
2022-09-07 22:19:00 +03:00
init_img = init_info
2022-09-07 01:50:14 +03:00
init_mask = None
keep_mask = False
2023-06-23 05:58:20 +03:00
assert 0.0 < = denoising_strength < = 1.0 , " can only work with strength in [0.0, 1.0] "
2022-09-07 01:50:14 +03:00
t_enc = int ( denoising_strength * ddim_steps )
def init ( ) :
image = init_img . convert ( " RGB " )
image = resize_image ( resize_mode , image , width , height )
2023-06-23 05:58:20 +03:00
# image = image.convert("RGB")
2022-09-07 01:50:14 +03:00
image = np . array ( image ) . astype ( np . float32 ) / 255.0
image = image [ None ] . transpose ( 0 , 3 , 1 , 2 )
image = torch . from_numpy ( image )
mask_channel = None
2022-09-09 08:24:46 +03:00
if image_editor_mode == " Mask " :
2022-09-07 01:50:14 +03:00
alpha = init_mask . convert ( " RGBA " )
alpha = resize_image ( resize_mode , alpha , width / / 8 , height / / 8 )
mask_channel = alpha . split ( ) [ 1 ]
mask = None
if mask_channel is not None :
mask = np . array ( mask_channel ) . astype ( np . float32 ) / 255.0
2023-06-23 05:58:20 +03:00
mask = 1 - mask
2022-09-07 01:50:14 +03:00
mask = np . tile ( mask , ( 4 , 1 , 1 ) )
mask = mask [ None ] . transpose ( 0 , 1 , 2 , 3 )
mask = torch . from_numpy ( mask ) . to ( device )
if opt . optimized :
modelFS . to ( device )
2023-06-23 05:58:20 +03:00
# let's try and find where init_image is 0's
# shape is probably (3,width,height)?
2022-09-09 08:24:46 +03:00
2022-09-11 20:18:17 +03:00
if image_editor_mode == " Uncrop " :
2023-06-23 05:58:20 +03:00
_image = image . numpy ( ) [ 0 ]
_mask = np . ones ( ( _image . shape [ 1 ] , _image . shape [ 2 ] ) )
# compute bounding box
cmax = np . max ( _image , axis = 0 )
rowmax = np . max ( cmax , axis = 0 )
colmax = np . max ( cmax , axis = 1 )
rowwhere = np . where ( rowmax > 0 ) [ 0 ]
colwhere = np . where ( colmax > 0 ) [ 0 ]
rowstart = rowwhere [ 0 ]
rowend = rowwhere [ - 1 ] + 1
colstart = colwhere [ 0 ]
colend = colwhere [ - 1 ] + 1
print ( " bounding box: " , rowstart , rowend , colstart , colend )
# this is where noise will get added
PAD_IMG = 16
boundingbox = np . zeros ( shape = ( height , width ) )
boundingbox [
colstart + PAD_IMG : colend - PAD_IMG ,
rowstart + PAD_IMG : rowend - PAD_IMG ,
] = 1
boundingbox = blurArr ( boundingbox , 4 )
# this is the mask for outpainting
PAD_MASK = 24
boundingbox2 = np . zeros ( shape = ( height , width ) )
boundingbox2 [
colstart + PAD_MASK : colend - PAD_MASK ,
rowstart + PAD_MASK : rowend - PAD_MASK ,
] = 1
boundingbox2 = blurArr ( boundingbox2 , 4 )
# noise=np.random.randn(*_image.shape)
noise = np . array (
[ perlinNoise ( height , width , height / 64 , width / 64 ) for i in range ( 3 ) ]
)
_mask * = 1 - boundingbox2
# convert 0,1 to -1,1
_image = 2.0 * _image - 1.0
# add noise
boundingbox = np . tile ( boundingbox , ( 3 , 1 , 1 ) )
_image = _image * boundingbox + noise * ( 1 - boundingbox )
# resize mask
_mask = (
np . array (
resize_image (
resize_mode ,
Image . fromarray ( _mask * 255 ) ,
width / / 8 ,
height / / 8 ,
)
)
/ 255
)
# convert back to torch tensor
init_image = torch . from_numpy (
np . expand_dims ( _image , axis = 0 ) . astype ( np . float32 )
) . to ( device )
mask = torch . from_numpy ( _mask . astype ( np . float32 ) ) . to ( device )
2022-09-09 08:24:46 +03:00
else :
2023-06-23 05:58:20 +03:00
init_image = 2.0 * image - 1.0
2022-09-09 08:24:46 +03:00
2022-09-07 01:50:14 +03:00
init_image = init_image . to ( device )
2023-06-23 05:58:20 +03:00
init_image = repeat ( init_image , " 1 ... -> b ... " , b = batch_size )
init_latent = (
model if not opt . optimized else modelFS
) . get_first_stage_encoding (
( model if not opt . optimized else modelFS ) . encode_first_stage ( init_image )
) # move to latent space
2022-09-11 20:18:17 +03:00
2022-09-07 01:50:14 +03:00
if opt . optimized :
2023-06-23 05:58:20 +03:00
mem = torch . cuda . memory_allocated ( ) / 1e6
2022-09-07 01:50:14 +03:00
modelFS . to ( " cpu " )
2023-06-23 05:58:20 +03:00
while torch . cuda . memory_allocated ( ) / 1e6 > = mem :
2022-09-07 01:50:14 +03:00
time . sleep ( 1 )
2023-06-23 05:58:20 +03:00
return (
init_latent ,
mask ,
)
2022-09-07 01:50:14 +03:00
2023-06-23 05:58:20 +03:00
def sample (
init_data ,
x ,
conditioning ,
unconditional_conditioning ,
sampler_name ,
img_callback : Callable = None ,
) :
2022-09-07 01:50:14 +03:00
t_enc_steps = t_enc
obliterate = False
if ddim_steps == t_enc_steps :
t_enc_steps = t_enc_steps - 1
obliterate = True
2023-06-23 05:58:20 +03:00
if sampler_name != " DDIM " :
2022-09-07 01:50:14 +03:00
x0 , z_mask = init_data
sigmas = sampler . model_wrap . get_sigmas ( ddim_steps )
noise = x * sigmas [ ddim_steps - t_enc_steps - 1 ]
xi = x0 + noise
# Obliterate masked image
if z_mask is not None and obliterate :
random = torch . randn ( z_mask . shape , device = xi . device )
2023-06-23 05:58:20 +03:00
xi = ( z_mask * noise ) + ( ( 1 - z_mask ) * xi )
2022-09-07 01:50:14 +03:00
2023-06-23 05:58:20 +03:00
sigma_sched = sigmas [ ddim_steps - t_enc_steps - 1 : ]
2022-09-07 01:50:14 +03:00
model_wrap_cfg = CFGMaskedDenoiser ( sampler . model_wrap )
2023-06-23 05:58:20 +03:00
samples_ddim = K . sampling . __dict__ [ f " sample_ { sampler . get_sampler_name ( ) } " ] (
model_wrap_cfg ,
xi ,
sigma_sched ,
extra_args = {
" cond " : conditioning ,
" uncond " : unconditional_conditioning ,
" cond_scale " : cfg_scale ,
" mask " : z_mask ,
" x0 " : x0 ,
" xi " : xi ,
} ,
disable = False ,
callback = partial ( KDiffusionSampler . img_callback_wrapper , img_callback ) ,
)
2022-09-07 01:50:14 +03:00
else :
x0 , z_mask = init_data
2023-06-23 05:58:20 +03:00
sampler . make_schedule (
ddim_num_steps = ddim_steps , ddim_eta = 0.0 , verbose = False
)
z_enc = sampler . stochastic_encode (
x0 , torch . tensor ( [ t_enc_steps ] * batch_size ) . to ( device )
)
2022-09-07 01:50:14 +03:00
# Obliterate masked image
if z_mask is not None and obliterate :
random = torch . randn ( z_mask . shape , device = z_enc . device )
2023-06-23 05:58:20 +03:00
z_enc = ( z_mask * random ) + ( ( 1 - z_mask ) * z_enc )
# decode it
samples_ddim = sampler . decode (
z_enc ,
conditioning ,
t_enc_steps ,
unconditional_guidance_scale = cfg_scale ,
unconditional_conditioning = unconditional_conditioning ,
z_mask = z_mask ,
x0 = x0 ,
)
2022-09-07 01:50:14 +03:00
return samples_ddim
2022-09-10 00:44:30 +03:00
correction_target = None
2022-09-07 01:50:14 +03:00
if loopback :
output_images , info = None , None
history = [ ]
initial_seed = None
2022-09-10 00:44:30 +03:00
# turn on color correction for loopback to prevent known issue of color drift
do_color_correction = True
2022-09-07 22:19:00 +03:00
2022-09-07 01:50:14 +03:00
for i in range ( n_iter ) :
2022-09-07 22:19:00 +03:00
if do_color_correction and i == 0 :
2023-06-23 05:58:20 +03:00
correction_target = cv2 . cvtColor (
np . asarray ( init_img . copy ( ) ) , cv2 . COLOR_RGB2LAB
)
2022-09-07 22:19:00 +03:00
2022-09-07 01:50:14 +03:00
output_images , seed , info , stats = process_images (
outpath = outpath ,
func_init = init ,
func_sample = sample ,
prompt = prompt ,
seed = seed ,
sampler_name = sampler_name ,
skip_save = skip_save ,
skip_grid = skip_grid ,
batch_size = 1 ,
n_iter = 1 ,
steps = ddim_steps ,
cfg_scale = cfg_scale ,
width = width ,
height = height ,
prompt_matrix = prompt_matrix ,
2022-09-11 12:32:36 +03:00
filter_nsfw = filter_nsfw ,
2022-09-07 01:50:14 +03:00
use_GFPGAN = use_GFPGAN ,
2023-06-23 05:58:20 +03:00
use_RealESRGAN = False , # Forcefully disable upscaling when using loopback
2022-09-07 01:50:14 +03:00
realesrgan_model_name = realesrgan_model_name ,
fp = fp ,
do_not_save_grid = True ,
normalize_prompt_weights = normalize_prompt_weights ,
init_img = init_img ,
init_mask = init_mask ,
keep_mask = keep_mask ,
mask_blur_strength = mask_blur_strength ,
2022-09-10 00:07:14 +03:00
mask_restore = mask_restore ,
2022-09-07 01:50:14 +03:00
denoising_strength = denoising_strength ,
resize_mode = resize_mode ,
uses_loopback = loopback ,
uses_random_seed_loopback = random_seed_loopback ,
sort_samples = sort_samples ,
write_info_files = write_info_files ,
write_sample_info_to_log_file = write_sample_info_to_log_file ,
jpg_sample = jpg_sample ,
2022-09-11 20:18:17 +03:00
job_info = job_info ,
2022-09-10 00:44:30 +03:00
do_color_correction = do_color_correction ,
2023-06-23 05:58:20 +03:00
correction_target = correction_target ,
2022-09-07 01:50:14 +03:00
)
if initial_seed is None :
initial_seed = seed
init_img = output_images [ 0 ]
2022-09-07 22:19:00 +03:00
2022-09-07 01:50:14 +03:00
if not random_seed_loopback :
seed = seed + 1
else :
seed = seed_to_int ( None )
denoising_strength = max ( denoising_strength * 0.95 , 0.1 )
history . append ( init_img )
if not skip_grid :
2023-06-23 05:58:20 +03:00
grid_count = get_next_sequence_number ( outpath , " grid- " )
2022-09-07 01:50:14 +03:00
grid = image_grid ( history , batch_size , force_n_rows = 1 )
grid_file = f " grid- { grid_count : 05 } - { seed } _ { prompt . replace ( ' ' , ' _ ' ) . translate ( { ord ( x ) : ' ' for x in invalid_filename_chars } ) [ : 128 ] } . { grid_ext } "
2023-06-23 05:58:20 +03:00
grid . save (
os . path . join ( outpath , grid_file ) ,
grid_format ,
quality = grid_quality ,
lossless = grid_lossless ,
optimize = True ,
)
2022-09-07 01:50:14 +03:00
output_images = history
seed = initial_seed
else :
2022-09-10 00:44:30 +03:00
if do_color_correction :
2023-06-23 05:58:20 +03:00
correction_target = cv2 . cvtColor (
np . asarray ( init_img . copy ( ) ) , cv2 . COLOR_RGB2LAB
)
2022-09-10 00:44:30 +03:00
2022-09-07 01:50:14 +03:00
output_images , seed , info , stats = process_images (
outpath = outpath ,
func_init = init ,
func_sample = sample ,
prompt = prompt ,
seed = seed ,
sampler_name = sampler_name ,
skip_save = skip_save ,
skip_grid = skip_grid ,
batch_size = batch_size ,
n_iter = n_iter ,
steps = ddim_steps ,
cfg_scale = cfg_scale ,
width = width ,
height = height ,
prompt_matrix = prompt_matrix ,
2022-09-11 12:32:36 +03:00
filter_nsfw = filter_nsfw ,
2022-09-07 01:50:14 +03:00
use_GFPGAN = use_GFPGAN ,
use_RealESRGAN = use_RealESRGAN ,
realesrgan_model_name = realesrgan_model_name ,
fp = fp ,
normalize_prompt_weights = normalize_prompt_weights ,
init_img = init_img ,
init_mask = init_mask ,
keep_mask = keep_mask ,
mask_blur_strength = mask_blur_strength ,
denoising_strength = denoising_strength ,
2022-09-10 00:07:14 +03:00
mask_restore = mask_restore ,
2022-09-07 01:50:14 +03:00
resize_mode = resize_mode ,
uses_loopback = loopback ,
sort_samples = sort_samples ,
write_info_files = write_info_files ,
write_sample_info_to_log_file = write_sample_info_to_log_file ,
jpg_sample = jpg_sample ,
2022-09-10 00:44:30 +03:00
job_info = job_info ,
do_color_correction = do_color_correction ,
2023-06-23 05:58:20 +03:00
correction_target = correction_target ,
2022-09-07 01:50:14 +03:00
)
del sampler
return output_images , seed , info , stats
2023-06-23 05:58:20 +03:00
prompt_parser = re . compile (
"""
2022-09-07 01:50:14 +03:00
( ? P < prompt > # capture group for 'prompt'
( ? : \\\: | [ ^ : ] ) + # match one or more non ':' characters or escaped colons '\:'
) # end 'prompt'
( ? : # non-capture group
: + # match one or more ':' characters
( ? P < weight > # capture group for 'weight'
2022-09-16 22:42:41 +03:00
- ? \d * \. { 0 , 1 } \d + # match positive or negative integer or decimal number
2022-09-07 01:50:14 +03:00
) ? # end weight capture group, make optional
\s * # strip spaces after weight
| # OR
$ # else, if no ':' then match end of line
) # end non-capture group
2023-06-23 05:58:20 +03:00
""" ,
re . VERBOSE ,
)
2022-09-07 01:50:14 +03:00
# grabs all text up to the first occurrence of ':' as sub-prompt
# takes the value following ':' as weight
# if ':' has no value defined, defaults to 1.0
# repeats until no text remaining
def split_weighted_subprompts ( input_string , normalize = True ) :
2023-06-23 05:58:20 +03:00
parsed_prompts = [
( match . group ( " prompt " ) . replace ( " \\ : " , " : " ) , float ( match . group ( " weight " ) or 1 ) )
for match in re . finditer ( prompt_parser , input_string )
]
2022-09-07 01:50:14 +03:00
if not normalize :
return parsed_prompts
weight_sum = sum ( map ( lambda x : x [ 1 ] , parsed_prompts ) )
if weight_sum == 0 :
2023-06-23 05:58:20 +03:00
print (
" Warning: Subprompt weights add up to zero. Discarding and using even weights instead. "
)
2022-09-07 01:50:14 +03:00
equal_weight = 1 / ( len ( parsed_prompts ) or 1 )
return [ ( x [ 0 ] , equal_weight ) for x in parsed_prompts ]
return [ ( x [ 0 ] , x [ 1 ] / weight_sum ) for x in parsed_prompts ]
2023-06-23 05:58:20 +03:00
def slerp ( device , t , v0 : torch . Tensor , v1 : torch . Tensor , DOT_THRESHOLD = 0.9995 ) :
2022-09-07 01:50:14 +03:00
v0 = v0 . detach ( ) . cpu ( ) . numpy ( )
v1 = v1 . detach ( ) . cpu ( ) . numpy ( )
dot = np . sum ( v0 * v1 / ( np . linalg . norm ( v0 ) * np . linalg . norm ( v1 ) ) )
if np . abs ( dot ) > DOT_THRESHOLD :
v2 = ( 1 - t ) * v0 + t * v1
else :
theta_0 = np . arccos ( dot )
sin_theta_0 = np . sin ( theta_0 )
theta_t = theta_0 * t
sin_theta_t = np . sin ( theta_t )
s0 = np . sin ( theta_0 - theta_t ) / sin_theta_0
s1 = sin_theta_t / sin_theta_0
v2 = s0 * v0 + s1 * v1
v2 = torch . from_numpy ( v2 ) . to ( device )
return v2
2023-06-23 05:58:20 +03:00
def imgproc (
image ,
image_batch ,
imgproc_prompt ,
imgproc_toggles ,
imgproc_upscale_toggles ,
imgproc_realesrgan_model_name ,
imgproc_sampling ,
imgproc_steps ,
imgproc_height ,
imgproc_width ,
imgproc_cfg ,
imgproc_denoising ,
imgproc_seed ,
imgproc_gfpgan_strength ,
imgproc_ldsr_steps ,
imgproc_ldsr_pre_downSample ,
imgproc_ldsr_post_downSample ,
) :
2022-09-07 01:50:14 +03:00
outpath = opt . outdir_imglab or opt . outdir or " outputs/imglab-samples "
output = [ ]
images = [ ]
2023-06-23 05:58:20 +03:00
def processGFPGAN ( image , strength ) :
2022-09-07 01:50:14 +03:00
image = image . convert ( " RGB " )
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
metadata = ImageMetadata . get_from_image ( image )
2023-06-23 05:58:20 +03:00
cropped_faces , restored_faces , restored_img = GFPGAN . enhance (
np . array ( image , dtype = np . uint8 ) ,
has_aligned = False ,
only_center_face = False ,
paste_back = True ,
)
2022-09-07 01:50:14 +03:00
result = Image . fromarray ( restored_img )
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
if metadata :
metadata . GFPGAN = True
ImageMetadata . set_on_image ( image , metadata )
2022-09-07 01:50:14 +03:00
if strength < 1.0 :
result = Image . blend ( image , result , strength )
return result
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
def processRealESRGAN ( image ) :
2023-06-23 05:58:20 +03:00
if " x2 " in imgproc_realesrgan_model_name :
2022-09-07 01:50:14 +03:00
# downscale to 1/2 size
2023-06-23 05:58:20 +03:00
modelMode = imgproc_realesrgan_model_name . replace ( " x2 " , " x4 " )
2022-09-07 01:50:14 +03:00
else :
modelMode = imgproc_realesrgan_model_name
image = image . convert ( " RGB " )
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
metadata = ImageMetadata . get_from_image ( image )
2022-09-07 01:50:14 +03:00
RealESRGAN = load_RealESRGAN ( modelMode )
result , res = RealESRGAN . enhance ( np . array ( image , dtype = np . uint8 ) )
result = Image . fromarray ( result )
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
ImageMetadata . set_on_image ( result , metadata )
2023-06-23 05:58:20 +03:00
if " x2 " in imgproc_realesrgan_model_name :
2022-09-07 01:50:14 +03:00
# downscale to 1/2 size
2023-06-23 05:58:20 +03:00
result = result . resize ( ( result . width / / 2 , result . height / / 2 ) , LANCZOS )
2022-09-07 01:50:14 +03:00
return result
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
def processGoBig ( image ) :
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
metadata = ImageMetadata . get_from_image ( image )
2023-06-23 05:58:20 +03:00
result = processRealESRGAN (
image ,
)
if " x4 " in imgproc_realesrgan_model_name :
# downscale to 1/2 size
result = result . resize ( ( result . width / / 2 , result . height / / 2 ) , LANCZOS )
2022-09-07 01:50:14 +03:00
2023-06-23 05:58:20 +03:00
# make sense of parameters
2022-09-07 01:50:14 +03:00
n_iter = 1
batch_size = 1
seed = seed_to_int ( imgproc_seed )
ddim_steps = int ( imgproc_steps )
2023-06-23 05:58:20 +03:00
resize_mode = 0 # need to add resize mode to form, or infer correct resolution from file name
2022-09-07 01:50:14 +03:00
width = int ( imgproc_width )
height = int ( imgproc_height )
cfg_scale = float ( imgproc_cfg )
denoising_strength = float ( imgproc_denoising )
skip_save = True
skip_grid = True
prompt = imgproc_prompt
t_enc = int ( denoising_strength * ddim_steps )
sampler_name = imgproc_sampling
2023-06-23 05:58:20 +03:00
if sampler_name == " DDIM " :
2022-09-07 01:50:14 +03:00
sampler = DDIMSampler ( model )
2023-06-23 05:58:20 +03:00
elif sampler_name == " k_dpm_2_a " :
sampler = KDiffusionSampler ( model , " dpm_2_ancestral " )
elif sampler_name == " k_dpm_2 " :
sampler = KDiffusionSampler ( model , " dpm_2 " )
elif sampler_name == " k_euler_a " :
sampler = KDiffusionSampler ( model , " euler_ancestral " )
elif sampler_name == " k_euler " :
sampler = KDiffusionSampler ( model , " euler " )
elif sampler_name == " k_heun " :
sampler = KDiffusionSampler ( model , " heun " )
elif sampler_name == " k_lms " :
sampler = KDiffusionSampler ( model , " lms " )
2022-09-07 01:50:14 +03:00
else :
raise Exception ( " Unknown sampler: " + sampler_name )
pass
init_img = result
2022-09-10 00:07:14 +03:00
mask_restore = False
2023-06-23 05:58:20 +03:00
assert (
0.0 < = denoising_strength < = 1.0
) , " can only work with strength in [0.0, 1.0] "
2022-09-07 01:50:14 +03:00
def init ( ) :
image = init_img . convert ( " RGB " )
image = resize_image ( resize_mode , image , width , height )
image = np . array ( image ) . astype ( np . float32 ) / 255.0
image = image [ None ] . transpose ( 0 , 3 , 1 , 2 )
image = torch . from_numpy ( image )
if opt . optimized :
modelFS . to ( device )
2023-06-23 05:58:20 +03:00
init_image = 2.0 * image - 1.0
2022-09-07 01:50:14 +03:00
init_image = init_image . to ( device )
2023-06-23 05:58:20 +03:00
init_image = repeat ( init_image , " 1 ... -> b ... " , b = batch_size )
init_latent = (
model if not opt . optimized else modelFS
) . get_first_stage_encoding (
( model if not opt . optimized else modelFS ) . encode_first_stage ( init_image )
) # move to latent space
2022-09-07 01:50:14 +03:00
if opt . optimized :
2023-06-23 05:58:20 +03:00
mem = torch . cuda . memory_allocated ( ) / 1e6
2022-09-07 01:50:14 +03:00
modelFS . to ( " cpu " )
2023-06-23 05:58:20 +03:00
while torch . cuda . memory_allocated ( ) / 1e6 > = mem :
2022-09-07 01:50:14 +03:00
time . sleep ( 1 )
2023-06-23 05:58:20 +03:00
return ( init_latent , )
2022-09-07 01:50:14 +03:00
2023-06-23 05:58:20 +03:00
def sample (
init_data ,
x ,
conditioning ,
unconditional_conditioning ,
sampler_name ,
img_callback : Callable = None ,
) :
if sampler_name != " DDIM " :
( x0 , ) = init_data
2022-09-07 01:50:14 +03:00
sigmas = sampler . model_wrap . get_sigmas ( ddim_steps )
noise = x * sigmas [ ddim_steps - t_enc - 1 ]
xi = x0 + noise
2023-06-23 05:58:20 +03:00
sigma_sched = sigmas [ ddim_steps - t_enc - 1 : ]
2022-09-07 01:50:14 +03:00
model_wrap_cfg = CFGDenoiser ( sampler . model_wrap )
2023-06-23 05:58:20 +03:00
samples_ddim = K . sampling . __dict__ [
f " sample_ { sampler . get_sampler_name ( ) } "
] (
model_wrap_cfg ,
xi ,
sigma_sched ,
extra_args = {
" cond " : conditioning ,
" uncond " : unconditional_conditioning ,
" cond_scale " : cfg_scale ,
} ,
disable = False ,
callback = partial (
KDiffusionSampler . img_callback_wrapper , img_callback
) ,
)
2022-09-07 01:50:14 +03:00
else :
2023-06-23 05:58:20 +03:00
( x0 , ) = init_data
sampler . make_schedule (
ddim_num_steps = ddim_steps , ddim_eta = 0.0 , verbose = False
)
z_enc = sampler . stochastic_encode (
x0 , torch . tensor ( [ t_enc ] * batch_size ) . to ( device )
)
# decode it
samples_ddim = sampler . decode (
z_enc ,
conditioning ,
t_enc ,
unconditional_guidance_scale = cfg_scale ,
unconditional_conditioning = unconditional_conditioning ,
)
2022-09-07 01:50:14 +03:00
return samples_ddim
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
def split_grid ( image , tile_w = 512 , tile_h = 512 , overlap = 64 ) :
2023-06-23 05:58:20 +03:00
Grid = namedtuple (
" Grid " , [ " tiles " , " tile_w " , " tile_h " , " image_w " , " image_h " , " overlap " ]
)
2022-09-07 01:50:14 +03:00
w = image . width
h = image . height
now = tile_w - overlap # non-overlap width
noh = tile_h - overlap
cols = math . ceil ( ( w - overlap ) / now )
rows = math . ceil ( ( h - overlap ) / noh )
grid = Grid ( [ ] , tile_w , tile_h , w , h , overlap )
for row in range ( rows ) :
row_images = [ ]
y = row * noh
if y + tile_h > = h :
y = h - tile_h
for col in range ( cols ) :
x = col * now
2023-06-23 05:58:20 +03:00
if x + tile_w > = w :
2022-09-07 01:50:14 +03:00
x = w - tile_w
tile = image . crop ( ( x , y , x + tile_w , y + tile_h ) )
row_images . append ( [ x , tile_w , tile ] )
grid . tiles . append ( [ y , tile_h , row_images ] )
return grid
def combine_grid ( grid ) :
def make_mask_image ( r ) :
r = r * 255 / grid . overlap
r = r . astype ( np . uint8 )
2023-06-23 05:58:20 +03:00
return Image . fromarray ( r , " L " )
2022-09-07 01:50:14 +03:00
2023-06-23 05:58:20 +03:00
mask_w = make_mask_image (
np . arange ( grid . overlap , dtype = np . float )
. reshape ( ( 1 , grid . overlap ) )
. repeat ( grid . tile_h , axis = 0 )
)
mask_h = make_mask_image (
np . arange ( grid . overlap , dtype = np . float )
. reshape ( ( grid . overlap , 1 ) )
. repeat ( grid . image_w , axis = 1 )
)
2022-09-07 01:50:14 +03:00
combined_image = Image . new ( " RGB " , ( grid . image_w , grid . image_h ) )
for y , h , row in grid . tiles :
combined_row = Image . new ( " RGB " , ( grid . image_w , h ) )
for x , w , tile in row :
if x == 0 :
combined_row . paste ( tile , ( 0 , 0 ) )
continue
2023-06-23 05:58:20 +03:00
combined_row . paste (
tile . crop ( ( 0 , 0 , grid . overlap , h ) ) , ( x , 0 ) , mask = mask_w
)
combined_row . paste (
tile . crop ( ( grid . overlap , 0 , w , h ) ) , ( x + grid . overlap , 0 )
)
2022-09-07 01:50:14 +03:00
if y == 0 :
combined_image . paste ( combined_row , ( 0 , 0 ) )
continue
2023-06-23 05:58:20 +03:00
combined_image . paste (
combined_row . crop ( ( 0 , 0 , combined_row . width , grid . overlap ) ) ,
( 0 , y ) ,
mask = mask_h ,
)
combined_image . paste (
combined_row . crop ( ( 0 , grid . overlap , combined_row . width , h ) ) ,
( 0 , y + grid . overlap ) ,
)
2022-09-07 01:50:14 +03:00
return combined_image
grid = split_grid ( result , tile_w = width , tile_h = height , overlap = 64 )
work = [ ]
work_results = [ ]
for y , h , row in grid . tiles :
for tiledata in row :
work . append ( tiledata [ 2 ] )
batch_count = math . ceil ( len ( work ) / batch_size )
2023-06-23 05:58:20 +03:00
print (
f " GoBig upscaling will process a total of { len ( work ) } images tiled as { len ( grid . tiles [ 0 ] [ 2 ] ) } x { len ( grid . tiles ) } in a total of { batch_count } batches. "
)
2022-09-07 01:50:14 +03:00
for i in range ( batch_count ) :
2023-06-23 05:58:20 +03:00
init_img = work [ i * batch_size : ( i + 1 ) * batch_size ] [ 0 ]
2022-09-07 01:50:14 +03:00
output_images , seed , info , stats = process_images (
2023-06-23 05:58:20 +03:00
outpath = outpath ,
func_init = init ,
func_sample = sample ,
prompt = prompt ,
seed = seed ,
sampler_name = sampler_name ,
skip_save = skip_save ,
skip_grid = skip_grid ,
batch_size = batch_size ,
n_iter = n_iter ,
steps = ddim_steps ,
cfg_scale = cfg_scale ,
width = width ,
height = height ,
prompt_matrix = None ,
filter_nsfw = False ,
use_GFPGAN = None ,
use_RealESRGAN = None ,
realesrgan_model_name = None ,
fp = None ,
normalize_prompt_weights = False ,
init_img = init_img ,
init_mask = None ,
keep_mask = False ,
mask_blur_strength = None ,
denoising_strength = denoising_strength ,
mask_restore = mask_restore ,
resize_mode = resize_mode ,
uses_loopback = False ,
sort_samples = True ,
write_info_files = True ,
write_sample_info_to_log_file = False ,
jpg_sample = False ,
imgProcessorTask = True ,
)
# if initial_seed is None:
2022-09-07 01:50:14 +03:00
# initial_seed = seed
2023-06-23 05:58:20 +03:00
# seed = seed + 1
2022-09-07 01:50:14 +03:00
work_results . append ( output_images [ 0 ] )
image_index = 0
for y , h , row in grid . tiles :
for tiledata in row :
tiledata [ 2 ] = work_results [ image_index ]
image_index + = 1
combined_image = combine_grid ( grid )
2023-06-23 05:58:20 +03:00
len ( os . listdir ( outpath ) ) - 1
2022-09-07 01:50:14 +03:00
del sampler
torch . cuda . empty_cache ( )
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
ImageMetadata . set_on_image ( combined_image , metadata )
2022-09-07 01:50:14 +03:00
return combined_image
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
def processLDSR ( image ) :
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
metadata = ImageMetadata . get_from_image ( image )
2023-06-23 05:58:20 +03:00
result = LDSR . superResolution (
image ,
int ( imgproc_ldsr_steps ) ,
str ( imgproc_ldsr_pre_downSample ) ,
str ( imgproc_ldsr_post_downSample ) ,
)
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
ImageMetadata . set_on_image ( result , metadata )
return result
2023-06-23 05:58:20 +03:00
if image_batch is not None :
if image is not None :
print (
" Batch detected and single image detected, please only use one of the two. Aborting. "
)
2022-09-07 01:50:14 +03:00
return None
2023-06-23 05:58:20 +03:00
# convert file to pillow image
2022-09-07 01:50:14 +03:00
for img in image_batch :
image = Image . fromarray ( np . array ( Image . open ( img ) ) )
images . append ( image )
2023-06-23 05:58:20 +03:00
elif image is not None :
if image_batch is not None :
print (
" Batch detected and single image detected, please only use one of the two. Aborting. "
)
2022-09-07 01:50:14 +03:00
return None
else :
images . append ( image )
if len ( images ) > 0 :
print ( " Processing images... " )
2023-06-23 05:58:20 +03:00
# pre load models not in loop
2022-09-07 01:50:14 +03:00
if 0 in imgproc_toggles :
2023-06-23 05:58:20 +03:00
ModelLoader ( [ " RealESGAN " , " LDSR " ] , False , True ) # Unload unused models
ModelLoader ( [ " GFPGAN " ] , True , False ) # Load used models
2022-09-07 01:50:14 +03:00
if 1 in imgproc_toggles :
2023-06-23 05:58:20 +03:00
if imgproc_upscale_toggles == 0 :
ModelLoader ( [ " GFPGAN " , " LDSR " ] , False , True ) # Unload unused models
ModelLoader (
[ " RealESGAN " ] , True , False , imgproc_realesrgan_model_name
) # Load used models
elif imgproc_upscale_toggles == 1 :
ModelLoader ( [ " GFPGAN " , " LDSR " ] , False , True ) # Unload unused models
ModelLoader ( [ " RealESGAN " , " model " ] , True , False ) # Load used models
elif imgproc_upscale_toggles == 2 :
ModelLoader (
[ " model " , " GFPGAN " , " RealESGAN " ] , False , True
) # Unload unused models
ModelLoader ( [ " LDSR " ] , True , False ) # Load used models
elif imgproc_upscale_toggles == 3 :
ModelLoader ( [ " GFPGAN " , " LDSR " ] , False , True ) # Unload unused models
ModelLoader (
[ " RealESGAN " , " model " ] , True , False , imgproc_realesrgan_model_name
) # Load used models
2022-09-07 01:50:14 +03:00
for image in images :
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
metadata = ImageMetadata . get_from_image ( image )
2022-09-07 01:50:14 +03:00
if 0 in imgproc_toggles :
2023-06-23 05:58:20 +03:00
# recheck if GFPGAN is loaded since it's the only model that can be loaded in the loop as well
ModelLoader ( [ " GFPGAN " ] , True , False ) # Load used models
image = processGFPGAN ( image , imgproc_gfpgan_strength )
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
if metadata :
metadata . GFPGAN = True
ImageMetadata . set_on_image ( image , metadata )
2023-06-23 05:58:20 +03:00
outpathDir = os . path . join ( outpath , " GFPGAN " )
2022-09-07 01:50:14 +03:00
os . makedirs ( outpathDir , exist_ok = True )
batchNumber = get_next_sequence_number ( outpathDir )
2023-06-23 05:58:20 +03:00
outFilename = str ( batchNumber ) + " - " + " result "
2022-09-07 01:50:14 +03:00
if 1 not in imgproc_toggles :
output . append ( image )
2023-06-23 05:58:20 +03:00
save_sample (
image ,
outpathDir ,
outFilename ,
False ,
None ,
None ,
None ,
None ,
None ,
False ,
None ,
None ,
None ,
None ,
None ,
None ,
None ,
None ,
None ,
False ,
)
2022-09-07 01:50:14 +03:00
if 1 in imgproc_toggles :
if imgproc_upscale_toggles == 0 :
image = processRealESRGAN ( image )
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
ImageMetadata . set_on_image ( image , metadata )
2023-06-23 05:58:20 +03:00
outpathDir = os . path . join ( outpath , " RealESRGAN " )
2022-09-07 01:50:14 +03:00
os . makedirs ( outpathDir , exist_ok = True )
batchNumber = get_next_sequence_number ( outpathDir )
2023-06-23 05:58:20 +03:00
outFilename = str ( batchNumber ) + " - " + " result "
2022-09-07 01:50:14 +03:00
output . append ( image )
2023-06-23 05:58:20 +03:00
save_sample (
image ,
outpathDir ,
outFilename ,
False ,
None ,
None ,
None ,
None ,
None ,
False ,
None ,
None ,
None ,
None ,
None ,
None ,
None ,
None ,
None ,
False ,
)
2022-09-07 01:50:14 +03:00
elif imgproc_upscale_toggles == 1 :
image = processGoBig ( image )
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
ImageMetadata . set_on_image ( image , metadata )
2023-06-23 05:58:20 +03:00
outpathDir = os . path . join ( outpath , " GoBig " )
2022-09-07 01:50:14 +03:00
os . makedirs ( outpathDir , exist_ok = True )
batchNumber = get_next_sequence_number ( outpathDir )
2023-06-23 05:58:20 +03:00
outFilename = str ( batchNumber ) + " - " + " result "
2022-09-07 01:50:14 +03:00
output . append ( image )
2023-06-23 05:58:20 +03:00
save_sample (
image ,
outpathDir ,
outFilename ,
False ,
None ,
None ,
None ,
None ,
None ,
False ,
None ,
None ,
None ,
None ,
None ,
None ,
None ,
None ,
None ,
False ,
)
2022-09-07 01:50:14 +03:00
elif imgproc_upscale_toggles == 2 :
image = processLDSR ( image )
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
ImageMetadata . set_on_image ( image , metadata )
2023-06-23 05:58:20 +03:00
outpathDir = os . path . join ( outpath , " LDSR " )
2022-09-07 01:50:14 +03:00
os . makedirs ( outpathDir , exist_ok = True )
batchNumber = get_next_sequence_number ( outpathDir )
2023-06-23 05:58:20 +03:00
outFilename = str ( batchNumber ) + " - " + " result "
2022-09-07 01:50:14 +03:00
output . append ( image )
2023-06-23 05:58:20 +03:00
save_sample (
image ,
outpathDir ,
outFilename ,
False ,
None ,
None ,
None ,
None ,
None ,
False ,
None ,
None ,
None ,
None ,
None ,
None ,
None ,
None ,
None ,
False ,
)
2022-09-07 01:50:14 +03:00
elif imgproc_upscale_toggles == 3 :
image = processGoBig ( image )
2023-06-23 05:58:20 +03:00
ModelLoader (
[ " model " , " GFPGAN " , " RealESGAN " ] , False , True
) # Unload unused models
ModelLoader ( [ " LDSR " ] , True , False ) # Load used models
2022-09-07 01:50:14 +03:00
image = processLDSR ( image )
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
ImageMetadata . set_on_image ( image , metadata )
2023-06-23 05:58:20 +03:00
outpathDir = os . path . join ( outpath , " GoLatent " )
2022-09-07 01:50:14 +03:00
os . makedirs ( outpathDir , exist_ok = True )
batchNumber = get_next_sequence_number ( outpathDir )
2023-06-23 05:58:20 +03:00
outFilename = str ( batchNumber ) + " - " + " result "
2022-09-07 01:50:14 +03:00
output . append ( image )
2023-06-23 05:58:20 +03:00
save_sample (
image ,
outpathDir ,
outFilename ,
None ,
None ,
None ,
None ,
None ,
None ,
False ,
None ,
None ,
None ,
None ,
None ,
None ,
None ,
None ,
None ,
False ,
)
2022-09-07 01:50:14 +03:00
2023-06-23 05:58:20 +03:00
# LDSR is always unloaded to avoid memory issues
# ModelLoader(['LDSR'],False,True)
# print("Reloading default models...")
# ModelLoader(['model','RealESGAN','GFPGAN'],True,False) # load back models
2022-09-07 01:50:14 +03:00
print ( " Done. " )
return output
2023-06-23 05:58:20 +03:00
def ModelLoader (
models , load = False , unload = False , imgproc_realesrgan_model_name = " RealESRGAN_x4plus "
) :
# get global variables
2022-09-07 01:50:14 +03:00
global_vars = globals ( )
2023-06-23 05:58:20 +03:00
# check if m is in globals
2022-09-07 01:50:14 +03:00
if unload :
for m in models :
if m in global_vars :
2023-06-23 05:58:20 +03:00
# if it is, delete it
2022-09-07 01:50:14 +03:00
del global_vars [ m ]
if opt . optimized :
2023-06-23 05:58:20 +03:00
if m == " model " :
del global_vars [ m + " FS " ]
del global_vars [ m + " CS " ]
if m == " model " :
m = " Stable Diffusion "
print ( " Unloaded " + m )
2022-09-07 01:50:14 +03:00
if load :
for m in models :
2023-06-23 05:58:20 +03:00
if (
m not in global_vars
or m in global_vars
and type ( global_vars [ m ] ) == bool
) :
# if it isn't, load it
if m == " GFPGAN " :
2022-09-07 01:50:14 +03:00
global_vars [ m ] = load_GFPGAN ( )
2023-06-23 05:58:20 +03:00
elif m == " model " :
2022-09-07 01:50:14 +03:00
sdLoader = load_SD_model ( )
global_vars [ m ] = sdLoader [ 0 ]
if opt . optimized :
2023-06-23 05:58:20 +03:00
global_vars [ m + " CS " ] = sdLoader [ 1 ]
global_vars [ m + " FS " ] = sdLoader [ 2 ]
elif m == " RealESRGAN " :
2022-09-07 01:50:14 +03:00
global_vars [ m ] = load_RealESRGAN ( imgproc_realesrgan_model_name )
2023-06-23 05:58:20 +03:00
elif m == " LDSR " :
2022-09-07 01:50:14 +03:00
global_vars [ m ] = load_LDSR ( )
2023-06-23 05:58:20 +03:00
if m == " model " :
m = " Stable Diffusion "
print ( " Loaded " + m )
2022-09-07 01:50:14 +03:00
torch_gc ( )
def run_GFPGAN ( image , strength ) :
2023-06-23 05:58:20 +03:00
ModelLoader ( [ " LDSR " , " RealESRGAN " ] , False , True )
ModelLoader ( [ " GFPGAN " ] , True , False )
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
metadata = ImageMetadata . get_from_image ( image )
2022-09-07 01:50:14 +03:00
image = image . convert ( " RGB " )
2023-06-23 05:58:20 +03:00
cropped_faces , restored_faces , restored_img = GFPGAN . enhance (
np . array ( image , dtype = np . uint8 ) ,
has_aligned = False ,
only_center_face = False ,
paste_back = True ,
)
2022-09-07 01:50:14 +03:00
res = Image . fromarray ( restored_img )
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
metadata . GFPGAN = True
ImageMetadata . set_on_image ( res , metadata )
2022-09-07 01:50:14 +03:00
if strength < 1.0 :
res = Image . blend ( image , res , strength )
return res
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
def run_RealESRGAN ( image , model_name : str ) :
2023-06-23 05:58:20 +03:00
ModelLoader ( [ " GFPGAN " , " LDSR " ] , False , True )
ModelLoader ( [ " RealESRGAN " ] , True , False )
2022-09-07 01:50:14 +03:00
if RealESRGAN . model . name != model_name :
2023-06-23 05:58:20 +03:00
try_loading_RealESRGAN ( model_name )
2022-09-07 01:50:14 +03:00
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
metadata = ImageMetadata . get_from_image ( image )
2022-09-07 01:50:14 +03:00
image = image . convert ( " RGB " )
output , img_mode = RealESRGAN . enhance ( np . array ( image , dtype = np . uint8 ) )
res = Image . fromarray ( output )
Metadata cleanup - Maintain metadata within UI (#845)
* Metadata cleanup - Maintain metadata within UI
This commit, when combined with Gradio 3.2.1b1+, maintains image
metadata as an image is passed throughout the UI. For example,
if you generate an image, send it to Image Lab, upscale it, fix faces,
and then drag the resulting image back in to Image Lab, it will still
remember the image generation parameters.
When the image is saved, the metadata will be stripped from it if
save-metadata is not enabled. If the image is saved by *dragging*
out of the UI on to the filesystem it may maintain its metadata.
Note: I have ran into UI responsiveness issues with upgrading Gradio.
Seems there may be some Gradio queue management issues. *Without* the
gradio update this commit will maintain current functionality, but
will not keep meetadata when dragging an image between UI components.
* Move ImageMetadata into its own file
Cleans up webui, enables webui_streamlit et al to use it as well.
* Fix typo
2022-09-09 19:27:21 +03:00
ImageMetadata . set_on_image ( res , metadata )
2022-09-07 01:50:14 +03:00
return res
if opt . defaults is not None and os . path . isfile ( opt . defaults ) :
try :
with open ( opt . defaults , " r " , encoding = " utf8 " ) as f :
user_defaults = yaml . safe_load ( f )
except ( OSError , yaml . YAMLError ) as e :
print ( f " Error loading defaults file { opt . defaults } : " , e , file = sys . stderr )
print ( " Falling back to program defaults. " , file = sys . stderr )
user_defaults = { }
else :
user_defaults = { }
# make sure these indicies line up at the top of txt2img()
txt2img_toggles = [
2023-06-23 05:58:20 +03:00
" Create prompt matrix (separate multiple prompts using |, and get all combinations of them) " ,
" Normalize Prompt Weights (ensure sum of weights add up to 1.0) " ,
" Save individual images " ,
" Save grid " ,
" Sort samples by prompt " ,
" Write sample info files " ,
" write sample info to log file " ,
" jpg samples " ,
" Filter NSFW content " ,
2022-09-07 01:50:14 +03:00
]
if GFPGAN is not None :
2023-06-23 05:58:20 +03:00
txt2img_toggles . append ( " Fix faces using GFPGAN " )
2022-09-07 01:50:14 +03:00
if RealESRGAN is not None :
2023-06-23 05:58:20 +03:00
txt2img_toggles . append ( " Upscale images using RealESRGAN " )
2022-09-07 01:50:14 +03:00
txt2img_defaults = {
2023-06-23 05:58:20 +03:00
" prompt " : " " ,
" ddim_steps " : 50 ,
" toggles " : [ 1 , 2 , 3 ] ,
" sampler_name " : " k_lms " ,
" ddim_eta " : 0.0 ,
" n_iter " : 1 ,
" batch_size " : 1 ,
" cfg_scale " : 7.5 ,
" seed " : " " ,
" height " : 512 ,
" width " : 512 ,
" fp " : None ,
" variant_amount " : 0.0 ,
" variant_seed " : " " ,
" submit_on_enter " : " Yes " ,
" realesrgan_model_name " : " RealESRGAN_x4plus " ,
2022-09-07 01:50:14 +03:00
}
2023-06-23 05:58:20 +03:00
if " txt2img " in user_defaults :
txt2img_defaults . update ( user_defaults [ " txt2img " ] )
2022-09-07 01:50:14 +03:00
2023-06-23 05:58:20 +03:00
txt2img_toggle_defaults = [ txt2img_toggles [ i ] for i in txt2img_defaults [ " toggles " ] ]
2022-09-07 01:50:14 +03:00
imgproc_defaults = {
2023-06-23 05:58:20 +03:00
" prompt " : " " ,
" ddim_steps " : 50 ,
" sampler_name " : " k_lms " ,
" cfg_scale " : 7.5 ,
" seed " : " " ,
" height " : 512 ,
" width " : 512 ,
" denoising_strength " : 0.30 ,
2022-09-07 01:50:14 +03:00
}
2023-06-23 05:58:20 +03:00
imgproc_mode_toggles = [ " Fix Faces " , " Upscale " ]
2022-09-07 01:50:14 +03:00
2023-06-23 05:58:20 +03:00
# sample_img2img = "assets/stable-samples/img2img/sketch-mountains-input.jpg"
# sample_img2img = sample_img2img if os.path.exists(sample_img2img) else None
2022-09-07 22:19:00 +03:00
sample_img2img = None
2022-09-07 01:50:14 +03:00
# make sure these indicies line up at the top of img2img()
img2img_toggles = [
2023-06-23 05:58:20 +03:00
" Create prompt matrix (separate multiple prompts using |, and get all combinations of them) " ,
" Normalize Prompt Weights (ensure sum of weights add up to 1.0) " ,
" Loopback (use images from previous batch when creating next batch) " ,
" Random loopback seed " ,
" Save individual images " ,
" Save grid " ,
" Sort samples by prompt " ,
" Write sample info files " ,
" Write sample info to one file " ,
" jpg samples " ,
" Color correction (always enabled on loopback mode) " ,
" Filter NSFW content " ,
2022-09-07 01:50:14 +03:00
]
# removed for now becuase of Image Lab implementation
if GFPGAN is not None :
2023-06-23 05:58:20 +03:00
img2img_toggles . append ( " Fix faces using GFPGAN " )
2022-09-07 01:50:14 +03:00
if RealESRGAN is not None :
2023-06-23 05:58:20 +03:00
img2img_toggles . append ( " Upscale images using RealESRGAN " )
2022-09-07 01:50:14 +03:00
img2img_mask_modes = [
" Keep masked area " ,
" Regenerate only masked area " ,
]
img2img_resize_modes = [
" Just resize " ,
" Crop and resize " ,
" Resize and fill " ,
]
img2img_defaults = {
2023-06-23 05:58:20 +03:00
" prompt " : " " ,
" ddim_steps " : 50 ,
" toggles " : [ 1 , 4 , 5 ] ,
" sampler_name " : " k_lms " ,
" ddim_eta " : 0.0 ,
" n_iter " : 1 ,
" batch_size " : 1 ,
" cfg_scale " : 5.0 ,
" denoising_strength " : 0.75 ,
" mask_mode " : 0 ,
" mask_restore " : False ,
" resize_mode " : 0 ,
" seed " : " " ,
" height " : 512 ,
" width " : 512 ,
" fp " : None ,
" mask_blur_strength " : 1 ,
" realesrgan_model_name " : " RealESRGAN_x4plus " ,
" image_editor_mode " : " Mask " ,
2022-09-07 01:50:14 +03:00
}
2023-06-23 05:58:20 +03:00
if " img2img " in user_defaults :
img2img_defaults . update ( user_defaults [ " img2img " ] )
2022-09-07 01:50:14 +03:00
2023-06-23 05:58:20 +03:00
img2img_toggle_defaults = [ img2img_toggles [ i ] for i in img2img_defaults [ " toggles " ] ]
img2img_image_mode = " sketch "
2022-09-07 01:50:14 +03:00
Scene-to-Image Prompt Layering System (#1179)
# Summary of the change
- new Scene-to-Image tab
- new scn2img function
- functions for loading and running monocular_depth_estimation with
tensorflow
# Description
(relevant motivation, which issue is fixed)
Related to discussion #925
> Would it be possible to have a layers system where we could do have
foreground, mid, and background objects which relate to one another and
share the style? So we could say generate a landscape, one another layer
generate a castle, and on another layer generate a crowd of people.
To make this work I made a prompt-based layering system in a new
"Scene-to-Image" tab.
You write a a multi-line prompt that looks like markdown, where each
section declares one layer.
It is hierarchical, so each layer can have their own child layers.
Examples: https://imgur.com/a/eUxd5qn
![](https://i.imgur.com/L61w00Q.png)
In the frontend you can find a brief documentation for the syntax,
examples and reference for the various arguments.
Here a short summary:
Sections with "prompt" and child layers are img2img, without child
layers they are txt2img.
Without "prompt" they are just images, useful for mask selection, image
composition, etc.
Images can be initialized with "color", resized with "resize" and their
position specified with "pos".
Rotation and rotation center are "rotation" and "center".
Mask can automatically be selected by color or by estimated depth based
on https://huggingface.co/spaces/atsantiago/Monocular_Depth_Filter.
![](https://i.imgur.com/8rMHWmZ.png)
# Additional dependencies that are required for this change
For mask selection by monocular depth estimation tensorflow is required
and the model must be cloned to ./src/monocular_depth_estimation/
Changes in environment.yaml:
- einops>=0.3.0
- tensorflow>=2.10.0
Einops must be allowed to be newer for tensorflow to work.
# Checklist:
- [x] I have changed the base branch to `dev`
- [x] I have performed a self-review of my own code
- [x] I have commented my code in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
Co-authored-by: hlky <106811348+hlky@users.noreply.github.com>
2022-10-02 20:23:37 +03:00
from scn2img import get_scn2img , scn2img_define_args
2023-06-23 05:58:20 +03:00
# avoid circular import, by passing all necessary types, functions
Scene-to-Image Prompt Layering System (#1179)
# Summary of the change
- new Scene-to-Image tab
- new scn2img function
- functions for loading and running monocular_depth_estimation with
tensorflow
# Description
(relevant motivation, which issue is fixed)
Related to discussion #925
> Would it be possible to have a layers system where we could do have
foreground, mid, and background objects which relate to one another and
share the style? So we could say generate a landscape, one another layer
generate a castle, and on another layer generate a crowd of people.
To make this work I made a prompt-based layering system in a new
"Scene-to-Image" tab.
You write a a multi-line prompt that looks like markdown, where each
section declares one layer.
It is hierarchical, so each layer can have their own child layers.
Examples: https://imgur.com/a/eUxd5qn
![](https://i.imgur.com/L61w00Q.png)
In the frontend you can find a brief documentation for the syntax,
examples and reference for the various arguments.
Here a short summary:
Sections with "prompt" and child layers are img2img, without child
layers they are txt2img.
Without "prompt" they are just images, useful for mask selection, image
composition, etc.
Images can be initialized with "color", resized with "resize" and their
position specified with "pos".
Rotation and rotation center are "rotation" and "center".
Mask can automatically be selected by color or by estimated depth based
on https://huggingface.co/spaces/atsantiago/Monocular_Depth_Filter.
![](https://i.imgur.com/8rMHWmZ.png)
# Additional dependencies that are required for this change
For mask selection by monocular depth estimation tensorflow is required
and the model must be cloned to ./src/monocular_depth_estimation/
Changes in environment.yaml:
- einops>=0.3.0
- tensorflow>=2.10.0
Einops must be allowed to be newer for tensorflow to work.
# Checklist:
- [x] I have changed the base branch to `dev`
- [x] I have performed a self-review of my own code
- [x] I have commented my code in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
Co-authored-by: hlky <106811348+hlky@users.noreply.github.com>
2022-10-02 20:23:37 +03:00
# and variables to get_scn2img, which will return scn2img function.
scn2img = get_scn2img (
2023-06-23 05:58:20 +03:00
MemUsageMonitor ,
save_sample ,
get_next_sequence_number ,
seed_to_int ,
txt2img ,
txt2img_defaults ,
img2img ,
img2img_defaults ,
opt ,
Scene-to-Image Prompt Layering System (#1179)
# Summary of the change
- new Scene-to-Image tab
- new scn2img function
- functions for loading and running monocular_depth_estimation with
tensorflow
# Description
(relevant motivation, which issue is fixed)
Related to discussion #925
> Would it be possible to have a layers system where we could do have
foreground, mid, and background objects which relate to one another and
share the style? So we could say generate a landscape, one another layer
generate a castle, and on another layer generate a crowd of people.
To make this work I made a prompt-based layering system in a new
"Scene-to-Image" tab.
You write a a multi-line prompt that looks like markdown, where each
section declares one layer.
It is hierarchical, so each layer can have their own child layers.
Examples: https://imgur.com/a/eUxd5qn
![](https://i.imgur.com/L61w00Q.png)
In the frontend you can find a brief documentation for the syntax,
examples and reference for the various arguments.
Here a short summary:
Sections with "prompt" and child layers are img2img, without child
layers they are txt2img.
Without "prompt" they are just images, useful for mask selection, image
composition, etc.
Images can be initialized with "color", resized with "resize" and their
position specified with "pos".
Rotation and rotation center are "rotation" and "center".
Mask can automatically be selected by color or by estimated depth based
on https://huggingface.co/spaces/atsantiago/Monocular_Depth_Filter.
![](https://i.imgur.com/8rMHWmZ.png)
# Additional dependencies that are required for this change
For mask selection by monocular depth estimation tensorflow is required
and the model must be cloned to ./src/monocular_depth_estimation/
Changes in environment.yaml:
- einops>=0.3.0
- tensorflow>=2.10.0
Einops must be allowed to be newer for tensorflow to work.
# Checklist:
- [x] I have changed the base branch to `dev`
- [x] I have performed a self-review of my own code
- [x] I have commented my code in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
Co-authored-by: hlky <106811348+hlky@users.noreply.github.com>
2022-10-02 20:23:37 +03:00
)
scn2img_toggles = [
2023-06-23 05:58:20 +03:00
" Clear Cache " ,
" Output intermediate images " ,
" Save individual images " ,
" Write sample info files " ,
" Write sample info to one file " ,
" jpg samples " ,
Scene-to-Image Prompt Layering System (#1179)
# Summary of the change
- new Scene-to-Image tab
- new scn2img function
- functions for loading and running monocular_depth_estimation with
tensorflow
# Description
(relevant motivation, which issue is fixed)
Related to discussion #925
> Would it be possible to have a layers system where we could do have
foreground, mid, and background objects which relate to one another and
share the style? So we could say generate a landscape, one another layer
generate a castle, and on another layer generate a crowd of people.
To make this work I made a prompt-based layering system in a new
"Scene-to-Image" tab.
You write a a multi-line prompt that looks like markdown, where each
section declares one layer.
It is hierarchical, so each layer can have their own child layers.
Examples: https://imgur.com/a/eUxd5qn
![](https://i.imgur.com/L61w00Q.png)
In the frontend you can find a brief documentation for the syntax,
examples and reference for the various arguments.
Here a short summary:
Sections with "prompt" and child layers are img2img, without child
layers they are txt2img.
Without "prompt" they are just images, useful for mask selection, image
composition, etc.
Images can be initialized with "color", resized with "resize" and their
position specified with "pos".
Rotation and rotation center are "rotation" and "center".
Mask can automatically be selected by color or by estimated depth based
on https://huggingface.co/spaces/atsantiago/Monocular_Depth_Filter.
![](https://i.imgur.com/8rMHWmZ.png)
# Additional dependencies that are required for this change
For mask selection by monocular depth estimation tensorflow is required
and the model must be cloned to ./src/monocular_depth_estimation/
Changes in environment.yaml:
- einops>=0.3.0
- tensorflow>=2.10.0
Einops must be allowed to be newer for tensorflow to work.
# Checklist:
- [x] I have changed the base branch to `dev`
- [x] I have performed a self-review of my own code
- [x] I have commented my code in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
Co-authored-by: hlky <106811348+hlky@users.noreply.github.com>
2022-10-02 20:23:37 +03:00
]
2023-06-23 05:58:20 +03:00
scn2img_defaults = { " prompt " : " " , " seed " : " " , " toggles " : [ 1 , 2 , 3 ] }
Scene-to-Image Prompt Layering System (#1179)
# Summary of the change
- new Scene-to-Image tab
- new scn2img function
- functions for loading and running monocular_depth_estimation with
tensorflow
# Description
(relevant motivation, which issue is fixed)
Related to discussion #925
> Would it be possible to have a layers system where we could do have
foreground, mid, and background objects which relate to one another and
share the style? So we could say generate a landscape, one another layer
generate a castle, and on another layer generate a crowd of people.
To make this work I made a prompt-based layering system in a new
"Scene-to-Image" tab.
You write a a multi-line prompt that looks like markdown, where each
section declares one layer.
It is hierarchical, so each layer can have their own child layers.
Examples: https://imgur.com/a/eUxd5qn
![](https://i.imgur.com/L61w00Q.png)
In the frontend you can find a brief documentation for the syntax,
examples and reference for the various arguments.
Here a short summary:
Sections with "prompt" and child layers are img2img, without child
layers they are txt2img.
Without "prompt" they are just images, useful for mask selection, image
composition, etc.
Images can be initialized with "color", resized with "resize" and their
position specified with "pos".
Rotation and rotation center are "rotation" and "center".
Mask can automatically be selected by color or by estimated depth based
on https://huggingface.co/spaces/atsantiago/Monocular_Depth_Filter.
![](https://i.imgur.com/8rMHWmZ.png)
# Additional dependencies that are required for this change
For mask selection by monocular depth estimation tensorflow is required
and the model must be cloned to ./src/monocular_depth_estimation/
Changes in environment.yaml:
- einops>=0.3.0
- tensorflow>=2.10.0
Einops must be allowed to be newer for tensorflow to work.
# Checklist:
- [x] I have changed the base branch to `dev`
- [x] I have performed a self-review of my own code
- [x] I have commented my code in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
Co-authored-by: hlky <106811348+hlky@users.noreply.github.com>
2022-10-02 20:23:37 +03:00
2023-06-23 05:58:20 +03:00
if " scn2img " in user_defaults :
scn2img_defaults . update ( user_defaults [ " scn2img " ] )
Scene-to-Image Prompt Layering System (#1179)
# Summary of the change
- new Scene-to-Image tab
- new scn2img function
- functions for loading and running monocular_depth_estimation with
tensorflow
# Description
(relevant motivation, which issue is fixed)
Related to discussion #925
> Would it be possible to have a layers system where we could do have
foreground, mid, and background objects which relate to one another and
share the style? So we could say generate a landscape, one another layer
generate a castle, and on another layer generate a crowd of people.
To make this work I made a prompt-based layering system in a new
"Scene-to-Image" tab.
You write a a multi-line prompt that looks like markdown, where each
section declares one layer.
It is hierarchical, so each layer can have their own child layers.
Examples: https://imgur.com/a/eUxd5qn
![](https://i.imgur.com/L61w00Q.png)
In the frontend you can find a brief documentation for the syntax,
examples and reference for the various arguments.
Here a short summary:
Sections with "prompt" and child layers are img2img, without child
layers they are txt2img.
Without "prompt" they are just images, useful for mask selection, image
composition, etc.
Images can be initialized with "color", resized with "resize" and their
position specified with "pos".
Rotation and rotation center are "rotation" and "center".
Mask can automatically be selected by color or by estimated depth based
on https://huggingface.co/spaces/atsantiago/Monocular_Depth_Filter.
![](https://i.imgur.com/8rMHWmZ.png)
# Additional dependencies that are required for this change
For mask selection by monocular depth estimation tensorflow is required
and the model must be cloned to ./src/monocular_depth_estimation/
Changes in environment.yaml:
- einops>=0.3.0
- tensorflow>=2.10.0
Einops must be allowed to be newer for tensorflow to work.
# Checklist:
- [x] I have changed the base branch to `dev`
- [x] I have performed a self-review of my own code
- [x] I have commented my code in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
Co-authored-by: hlky <106811348+hlky@users.noreply.github.com>
2022-10-02 20:23:37 +03:00
2023-06-23 05:58:20 +03:00
scn2img_toggle_defaults = [ scn2img_toggles [ i ] for i in scn2img_defaults [ " toggles " ] ]
Scene-to-Image Prompt Layering System (#1179)
# Summary of the change
- new Scene-to-Image tab
- new scn2img function
- functions for loading and running monocular_depth_estimation with
tensorflow
# Description
(relevant motivation, which issue is fixed)
Related to discussion #925
> Would it be possible to have a layers system where we could do have
foreground, mid, and background objects which relate to one another and
share the style? So we could say generate a landscape, one another layer
generate a castle, and on another layer generate a crowd of people.
To make this work I made a prompt-based layering system in a new
"Scene-to-Image" tab.
You write a a multi-line prompt that looks like markdown, where each
section declares one layer.
It is hierarchical, so each layer can have their own child layers.
Examples: https://imgur.com/a/eUxd5qn
![](https://i.imgur.com/L61w00Q.png)
In the frontend you can find a brief documentation for the syntax,
examples and reference for the various arguments.
Here a short summary:
Sections with "prompt" and child layers are img2img, without child
layers they are txt2img.
Without "prompt" they are just images, useful for mask selection, image
composition, etc.
Images can be initialized with "color", resized with "resize" and their
position specified with "pos".
Rotation and rotation center are "rotation" and "center".
Mask can automatically be selected by color or by estimated depth based
on https://huggingface.co/spaces/atsantiago/Monocular_Depth_Filter.
![](https://i.imgur.com/8rMHWmZ.png)
# Additional dependencies that are required for this change
For mask selection by monocular depth estimation tensorflow is required
and the model must be cloned to ./src/monocular_depth_estimation/
Changes in environment.yaml:
- einops>=0.3.0
- tensorflow>=2.10.0
Einops must be allowed to be newer for tensorflow to work.
# Checklist:
- [x] I have changed the base branch to `dev`
- [x] I have performed a self-review of my own code
- [x] I have commented my code in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
Co-authored-by: hlky <106811348+hlky@users.noreply.github.com>
2022-10-02 20:23:37 +03:00
2022-09-07 01:50:14 +03:00
help_text = """
## Mask/Crop
* The masking / cropping is very temperamental .
* It may take some time for the image to show when switching from Crop to Mask .
* If the image doesn ' t appear after switching to Mask, switch back to Crop and then back again to Mask
* If the mask appears distorted ( the brush is weirdly shaped instead of round ) , switch back to Crop and then back again to Mask .
## Advanced Editor
* For now the button needs to be clicked twice the first time .
* Once you have edited your image , you _need_ to click the save button for the next step to work .
* Clear the image from the crop editor ( click the x )
* Click " Get Image from Advanced Editor " to get the image you saved . If it doesn ' t work, try opening the editor and saving again.
If it keeps not working , try switching modes again , switch tabs , clear the image or reload .
"""
2023-06-23 05:58:20 +03:00
2022-09-07 01:50:14 +03:00
def show_help ( ) :
2023-06-23 05:58:20 +03:00
return [
gr . update ( visible = False ) ,
gr . update ( visible = True ) ,
gr . update ( value = help_text ) ,
]
2022-09-07 01:50:14 +03:00
def hide_help ( ) :
return [ gr . update ( visible = True ) , gr . update ( visible = False ) , gr . update ( value = " " ) ]
2023-06-23 05:58:20 +03:00
demo = draw_gradio_ui (
opt ,
user_defaults = user_defaults ,
txt2img = txt2img ,
img2img = img2img ,
imgproc = imgproc ,
scn2img = scn2img ,
txt2img_defaults = txt2img_defaults ,
txt2img_toggles = txt2img_toggles ,
txt2img_toggle_defaults = txt2img_toggle_defaults ,
show_embeddings = hasattr ( model , " embedding_manager " ) ,
img2img_defaults = img2img_defaults ,
img2img_toggles = img2img_toggles ,
img2img_toggle_defaults = img2img_toggle_defaults ,
img2img_mask_modes = img2img_mask_modes ,
img2img_resize_modes = img2img_resize_modes ,
sample_img2img = sample_img2img ,
imgproc_defaults = imgproc_defaults ,
imgproc_mode_toggles = imgproc_mode_toggles ,
scn2img_defaults = scn2img_defaults ,
scn2img_toggles = scn2img_toggles ,
scn2img_toggle_defaults = scn2img_toggle_defaults ,
scn2img_define_args = scn2img_define_args ,
RealESRGAN = RealESRGAN ,
GFPGAN = GFPGAN ,
LDSR = LDSR ,
run_GFPGAN = run_GFPGAN ,
run_RealESRGAN = run_RealESRGAN ,
job_manager = job_manager ,
)
2022-09-07 01:50:14 +03:00
class ServerLauncher ( threading . Thread ) :
def __init__ ( self , demo ) :
threading . Thread . __init__ ( self )
2023-06-23 05:58:20 +03:00
self . name = " Gradio Server Thread "
2022-09-07 01:50:14 +03:00
self . demo = demo
def run ( self ) :
loop = asyncio . new_event_loop ( )
asyncio . set_event_loop ( loop )
gradio_params = {
2023-06-23 05:58:20 +03:00
" inbrowser " : opt . inbrowser ,
" server_name " : " 0.0.0.0 " ,
" server_port " : opt . port ,
" share " : opt . share ,
" show_error " : True ,
2022-09-07 01:50:14 +03:00
}
if not opt . share :
demo . queue ( concurrency_count = opt . max_jobs )
if opt . share and opt . share_password :
2023-06-23 05:58:20 +03:00
gradio_params [ " auth " ] = ( " webui " , opt . share_password )
2022-09-07 01:50:14 +03:00
# Check to see if Port 7860 is open
port_status = 1
while port_status != 0 :
try :
self . demo . launch ( * * gradio_params )
2023-06-23 05:58:20 +03:00
except OSError :
print (
f " Error: Port: { opt . port } is not open yet. Please wait, this may take upwards of 60 seconds... "
)
2022-09-07 01:50:14 +03:00
time . sleep ( 10 )
else :
port_status = 0
def stop ( self ) :
2023-06-23 05:58:20 +03:00
self . demo . close ( ) # this tends to hang
2022-09-07 01:50:14 +03:00
def launch_server ( ) :
server_thread = ServerLauncher ( demo )
server_thread . start ( )
try :
while server_thread . is_alive ( ) :
time . sleep ( 60 )
except ( KeyboardInterrupt , OSError ) as e :
2023-06-23 05:58:20 +03:00
crash ( e , " Shutting down... " )
2022-09-07 01:50:14 +03:00
def run_headless ( ) :
2023-06-23 05:58:20 +03:00
with open ( opt . cli , " r " , encoding = " utf8 " ) as f :
2022-09-07 01:50:14 +03:00
kwargs = yaml . safe_load ( f )
2023-06-23 05:58:20 +03:00
target = kwargs . pop ( " target " )
if target == " txt2img " :
2022-09-07 01:50:14 +03:00
target_func = txt2img
2023-06-23 05:58:20 +03:00
elif target == " img2img " :
2022-09-07 01:50:14 +03:00
target_func = img2img
raise NotImplementedError ( )
else :
2023-06-23 05:58:20 +03:00
raise ValueError ( f " Unknown target: { target } " )
2022-09-07 01:50:14 +03:00
prompts = kwargs . pop ( " prompt " )
prompts = prompts if type ( prompts ) is list else [ prompts ]
for i , prompt_i in enumerate ( prompts ) :
print ( f " ===== Prompt { i + 1 } / { len ( prompts ) } : { prompt_i } ===== " )
output_images , seed , info , stats = target_func ( prompt = prompt_i , * * kwargs )
2023-06-23 05:58:20 +03:00
print ( f " Seed: { seed } " )
2022-09-07 01:50:14 +03:00
print ( info )
print ( stats )
print ( )
2023-06-23 05:58:20 +03:00
2022-10-02 20:22:27 +03:00
@logger.catch
2023-06-23 05:58:20 +03:00
def run_bridge (
interval ,
api_key ,
horde_name ,
horde_url ,
priority_usernames ,
horde_max_pixels ,
horde_nsfw ,
horde_censor_nsfw ,
horde_blacklist ,
horde_censorlist ,
) :
2022-10-02 20:22:27 +03:00
current_id = None
current_payload = None
loop_retry = 0
while True :
2022-10-04 13:07:09 +03:00
if loop_retry > 10 and current_id :
2023-06-23 05:58:20 +03:00
logger . error (
f " Exceeded retry count { loop_retry } for generation id { current_id } . Aborting generation! "
)
2022-10-04 13:07:09 +03:00
current_id = None
current_payload = None
current_generation = None
loop_retry = 0
elif current_id :
2023-06-23 05:58:20 +03:00
logger . debug (
f " Retrying ( { loop_retry } /10) for generation id { current_id } ... "
)
2022-10-02 20:22:27 +03:00
gen_dict = {
" name " : horde_name ,
" max_pixels " : horde_max_pixels ,
" priority_usernames " : priority_usernames ,
" nsfw " : horde_nsfw ,
2022-10-05 15:55:04 +03:00
" blacklist " : horde_blacklist ,
2022-10-02 20:22:27 +03:00
}
headers = { " apikey " : api_key }
if current_id :
loop_retry + = 1
else :
try :
2023-06-23 05:58:20 +03:00
pop_req = requests . post (
horde_url + " /api/v2/generate/pop " , json = gen_dict , headers = headers
)
2022-10-02 20:22:27 +03:00
except requests . exceptions . ConnectionError :
2023-06-23 05:58:20 +03:00
logger . warning (
f " Server { horde_url } unavailable during pop. Waiting 10 seconds... "
)
2022-10-02 20:22:27 +03:00
time . sleep ( 10 )
continue
except requests . exceptions . JSONDecodeError ( ) :
2023-06-23 05:58:20 +03:00
logger . warning (
f " Server { horde_url } unavailable during pop. Waiting 10 seconds... "
)
2022-10-02 20:22:27 +03:00
time . sleep ( 10 )
continue
try :
pop = pop_req . json ( )
except json . decoder . JSONDecodeError :
2023-06-23 05:58:20 +03:00
logger . error (
f " Could not decode response from { horde_url } as json. Please inform its administrator! "
)
2022-10-02 20:22:27 +03:00
time . sleep ( interval )
continue
2023-06-23 05:58:20 +03:00
if pop is None :
logger . error (
f " Something has gone wrong with { horde_url } . Please inform its administrator! "
)
2022-10-02 20:22:27 +03:00
time . sleep ( interval )
continue
if not pop_req . ok :
2023-06-23 05:58:20 +03:00
pop [ " message " ]
logger . warning (
f " During gen pop, server { horde_url } responded with status code { pop_req . status_code } : { pop [ ' message ' ] } . Waiting for 10 seconds... "
)
if " errors " in pop :
2022-10-02 20:22:27 +03:00
logger . warning ( f " Detailed Request Errors: { pop [ ' errors ' ] } " )
time . sleep ( 10 )
continue
if not pop . get ( " id " ) :
2023-06-23 05:58:20 +03:00
skipped_info = pop . get ( " skipped " )
2022-10-02 20:22:27 +03:00
if skipped_info and len ( skipped_info ) :
skipped_info = f " Skipped Info: { skipped_info } . "
else :
2023-06-23 05:58:20 +03:00
skipped_info = " "
logger . debug (
f " Server { horde_url } has no valid generations to do for us. { skipped_info } "
)
2022-10-02 20:22:27 +03:00
time . sleep ( interval )
continue
2023-06-23 05:58:20 +03:00
current_id = pop [ " id " ]
2022-10-02 20:22:27 +03:00
logger . debug ( f " Request with id { current_id } picked up. Initiating work... " )
2023-06-23 05:58:20 +03:00
current_payload = pop [ " payload " ]
if " toggles " in current_payload and current_payload [ " toggles " ] is None :
2022-10-02 20:22:27 +03:00
logger . error ( f " Received Bad payload: { pop } " )
current_id = None
current_payload = None
current_generation = None
2022-10-03 11:26:26 +03:00
loop_retry = 0
2022-10-02 20:22:27 +03:00
time . sleep ( 10 )
continue
2023-06-23 05:58:20 +03:00
current_payload [ " toggles " ] = current_payload . get ( " toggles " , [ 1 , 4 ] )
2022-10-03 11:26:26 +03:00
# In bridge-mode, matrix is prepared on the horde and split in multiple nodes
2023-06-23 05:58:20 +03:00
if 0 in current_payload [ " toggles " ] :
current_payload [ " toggles " ] . remove ( 0 )
if 8 not in current_payload [ " toggles " ] :
2022-10-05 15:55:04 +03:00
if horde_censor_nsfw and not horde_nsfw :
2023-06-23 05:58:20 +03:00
current_payload [ " toggles " ] . append ( 8 )
elif any ( word in current_payload [ " prompt " ] for word in horde_censorlist ) :
current_payload [ " toggles " ] . append ( 8 )
2022-10-02 20:22:27 +03:00
images , seed , info , stats = txt2img ( * * current_payload )
buffer = BytesIO ( )
# We send as WebP to avoid using all the horde bandwidth
images [ 0 ] . save ( buffer , format = " WebP " , quality = 90 )
# logger.info(info)
submit_dict = {
" id " : current_id ,
" generation " : base64 . b64encode ( buffer . getvalue ( ) ) . decode ( " utf8 " ) ,
" api_key " : api_key ,
" seed " : seed ,
" max_pixels " : horde_max_pixels ,
}
current_generation = seed
2023-06-23 05:58:20 +03:00
while current_id and current_generation is not None :
2022-10-02 20:22:27 +03:00
try :
2023-06-23 05:58:20 +03:00
submit_req = requests . post (
horde_url + " /api/v2/generate/submit " ,
json = submit_dict ,
headers = headers ,
)
2022-10-02 20:22:27 +03:00
try :
submit = submit_req . json ( )
except json . decoder . JSONDecodeError :
2023-06-23 05:58:20 +03:00
logger . error (
f " Something has gone wrong with { horde_url } during submit. Please inform its administrator! (Retry { loop_retry } /10) "
)
2022-10-02 20:22:27 +03:00
time . sleep ( interval )
continue
if submit_req . status_code == 404 :
2023-06-23 05:58:20 +03:00
logger . warning (
" The generation we were working on got stale. Aborting! "
)
2022-10-02 20:22:27 +03:00
elif not submit_req . ok :
2023-06-23 05:58:20 +03:00
logger . warning (
f " During gen submit, server { horde_url } responded with status code { submit_req . status_code } : { submit [ ' message ' ] } . Waiting for 10 seconds... (Retry { loop_retry } /10) "
)
if " errors " in submit :
2022-10-02 20:22:27 +03:00
logger . warning ( f " Detailed Request Errors: { submit [ ' errors ' ] } " )
time . sleep ( 10 )
continue
else :
2023-06-23 05:58:20 +03:00
logger . info (
f ' Submitted generation with id { current_id } and contributed for { submit_req . json ( ) [ " reward " ] } '
)
2022-10-02 20:22:27 +03:00
current_id = None
current_payload = None
current_generation = None
2022-10-03 11:26:26 +03:00
loop_retry = 0
2022-10-02 20:22:27 +03:00
except requests . exceptions . ConnectionError :
2023-06-23 05:58:20 +03:00
logger . warning (
f " Server { horde_url } unavailable during submit. Waiting 10 seconds... (Retry { loop_retry } /10) "
)
2022-10-02 20:22:27 +03:00
time . sleep ( 10 )
continue
time . sleep ( interval )
2023-06-23 05:58:20 +03:00
if __name__ == " __main__ " :
2022-10-02 20:22:27 +03:00
set_logger_verbosity ( opt . verbosity )
quiesce_logger ( opt . quiet )
if opt . cli :
2022-09-07 01:50:14 +03:00
run_headless ( )
2022-10-02 20:22:27 +03:00
if opt . bridge :
try :
import bridgeData as cd
2022-10-11 13:23:04 +03:00
except ModuleNotFoundError as e :
2023-06-23 05:58:20 +03:00
logger . warning (
" No bridgeData found. Falling back to default where no CLI args are set. "
)
2022-10-11 13:23:04 +03:00
logger . warning ( str ( e ) )
except SyntaxError as e :
2023-06-23 05:58:20 +03:00
logger . warning (
" bridgeData found, but is malformed. Falling back to default where no CLI args are set. "
)
2022-10-11 13:23:04 +03:00
logger . warning ( str ( e ) )
except Exception as e :
2022-10-02 20:22:27 +03:00
logger . warning ( " No bridgeData found, use default where no CLI args are set " )
2022-10-11 13:23:04 +03:00
logger . warning ( str ( e ) )
finally :
2023-06-23 05:58:20 +03:00
try : # check if cd exists (i.e. bridgeData loaded properly)
2022-10-11 13:23:04 +03:00
cd
2023-06-23 05:58:20 +03:00
except : # if not, create defaults
2022-10-11 13:23:04 +03:00
class temp ( object ) :
def __init__ ( self ) :
random . seed ( )
self . horde_url = " https://stablehorde.net "
# Give a cool name to your instance
self . horde_name = f " Automated Instance # { random . randint ( - 100000000 , 100000000 ) } "
# The api_key identifies a unique user in the horde
self . horde_api_key = " 0000000000 "
# Put other users whose prompts you want to prioritize.
# The owner's username is always included so you don't need to add it here, unless you want it to have lower priority than another user
self . horde_priority_usernames = [ ]
self . horde_max_power = 8
self . nsfw = True
2023-06-23 05:58:20 +03:00
2022-10-11 13:23:04 +03:00
cd = temp ( )
2022-10-02 20:22:27 +03:00
horde_api_key = opt . horde_api_key if opt . horde_api_key else cd . horde_api_key
horde_name = opt . horde_name if opt . horde_name else cd . horde_name
horde_url = opt . horde_url if opt . horde_url else cd . horde_url
2023-06-23 05:58:20 +03:00
horde_priority_usernames = (
opt . horde_priority_usernames
if opt . horde_priority_usernames
else cd . horde_priority_usernames
)
horde_max_power = (
opt . horde_max_power if opt . horde_max_power else cd . horde_max_power
)
2022-10-02 20:22:27 +03:00
try :
2023-06-23 05:58:20 +03:00
horde_nsfw = not opt . horde_sfw if opt . horde_sfw else cd . horde_nsfw
2022-10-02 20:22:27 +03:00
except AttributeError :
horde_nsfw = True
2022-10-05 15:55:04 +03:00
try :
2023-06-23 05:58:20 +03:00
horde_censor_nsfw = (
opt . horde_censor_nsfw if opt . horde_censor_nsfw else cd . horde_censor_nsfw
)
2022-10-05 15:55:04 +03:00
except AttributeError :
horde_censor_nsfw = False
try :
2023-06-23 05:58:20 +03:00
horde_blacklist = (
opt . horde_blacklist if opt . horde_blacklist else cd . horde_blacklist
)
2022-10-05 15:55:04 +03:00
except AttributeError :
horde_blacklist = [ ]
try :
2023-06-23 05:58:20 +03:00
horde_censorlist = (
opt . horde_censorlist if opt . horde_censorlist else cd . horde_censorlist
)
2022-10-05 15:55:04 +03:00
except AttributeError :
horde_censorlist = [ ]
2022-10-02 20:22:27 +03:00
if horde_max_power < 2 :
horde_max_power = 2
2023-06-23 05:58:20 +03:00
horde_max_pixels = 64 * 64 * 8 * horde_max_power
logger . info (
f " Joining Horde with parameters: API Key ' { horde_api_key } ' . Server Name ' { horde_name } ' . Horde URL ' { horde_url } ' . Max Pixels { horde_max_pixels } "
)
2022-10-02 20:22:27 +03:00
try :
2023-06-23 05:58:20 +03:00
run_bridge (
1 ,
horde_api_key ,
horde_name ,
horde_url ,
horde_priority_usernames ,
horde_max_pixels ,
horde_nsfw ,
horde_censor_nsfw ,
horde_blacklist ,
horde_censorlist ,
)
2022-10-02 20:22:27 +03:00
except KeyboardInterrupt :
2023-06-23 05:58:20 +03:00
logger . info ( " Keyboard Interrupt Received. Ending Bridge " )
2022-10-02 20:22:27 +03:00
else :
2023-06-23 05:58:20 +03:00
launch_server ( )