2023-05-16 05:50:01 +00:00
# Copyright 2023 Lincoln D. Stein and the InvokeAI Team
import argparse
import shlex
from argparse import ArgumentParser
2023-07-02 01:45:14 +00:00
# note that this includes both old sampler names and new scheduler names
# in order to be able to parse both 2.0 and 3.0-pre-nodes versions of invokeai.init
2023-05-16 05:50:01 +00:00
SAMPLER_CHOICES = [
" ddim " ,
" ddpm " ,
" deis " ,
" lms " ,
2023-06-17 18:00:16 +00:00
" lms_k " ,
2023-05-16 05:50:01 +00:00
" pndm " ,
" heun " ,
" heun_k " ,
" euler " ,
" euler_k " ,
" euler_a " ,
" kdpm_2 " ,
" kdpm_2_a " ,
" dpmpp_2s " ,
2023-06-17 18:00:16 +00:00
" dpmpp_2s_k " ,
2023-05-16 05:50:01 +00:00
" dpmpp_2m " ,
" dpmpp_2m_k " ,
2023-06-18 20:38:15 +00:00
" dpmpp_2m_sde " ,
" dpmpp_2m_sde_k " ,
" dpmpp_sde " ,
" dpmpp_sde_k " ,
2023-05-16 05:50:01 +00:00
" unipc " ,
2023-07-02 01:45:14 +00:00
" k_dpm_2_a " ,
" k_dpm_2 " ,
" k_dpmpp_2_a " ,
" k_dpmpp_2 " ,
" k_euler_a " ,
" k_euler " ,
" k_heun " ,
" k_lms " ,
" plms " ,
2023-05-16 05:50:01 +00:00
]
PRECISION_CHOICES = [
" auto " ,
" float32 " ,
" autocast " ,
" float16 " ,
]
2023-07-28 13:46:44 +00:00
2023-05-16 05:50:01 +00:00
class FileArgumentParser ( ArgumentParser ) :
"""
Supports reading defaults from an init file .
"""
2023-07-28 13:46:44 +00:00
2023-05-16 05:50:01 +00:00
def convert_arg_line_to_args ( self , arg_line ) :
return shlex . split ( arg_line , comments = True )
legacy_parser = FileArgumentParser (
2023-07-28 13:46:44 +00:00
description = """
2023-05-16 05:50:01 +00:00
Generate images using Stable Diffusion .
Use - - web to launch the web interface .
Use - - from_file to load prompts from a file path or standard input ( " - " ) .
Otherwise you will be dropped into an interactive command prompt ( type - h for help . )
Other command - line arguments are defaults that can usually be overridden
prompt the command prompt .
""" ,
2023-07-28 13:46:44 +00:00
fromfile_prefix_chars = " @ " ,
)
general_group = legacy_parser . add_argument_group ( " General " )
model_group = legacy_parser . add_argument_group ( " Model selection " )
file_group = legacy_parser . add_argument_group ( " Input/output " )
web_server_group = legacy_parser . add_argument_group ( " Web server " )
render_group = legacy_parser . add_argument_group ( " Rendering " )
postprocessing_group = legacy_parser . add_argument_group ( " Postprocessing " )
deprecated_group = legacy_parser . add_argument_group ( " Deprecated options " )
2023-05-16 05:50:01 +00:00
2023-07-28 13:46:44 +00:00
deprecated_group . add_argument ( " --laion400m " )
deprecated_group . add_argument ( " --weights " ) # deprecated
general_group . add_argument ( " --version " , " -V " , action = " store_true " , help = " Print InvokeAI version number " )
2023-05-16 05:50:01 +00:00
model_group . add_argument (
2023-07-28 13:46:44 +00:00
" --root_dir " ,
2023-05-16 05:50:01 +00:00
default = None ,
help = ' Path to directory containing " models " , " outputs " and " configs " . If not present will read from environment variable INVOKEAI_ROOT. Defaults to ~/invokeai. ' ,
)
model_group . add_argument (
2023-07-28 13:46:44 +00:00
" --config " ,
" -c " ,
" -config " ,
dest = " conf " ,
default = " ./configs/models.yaml " ,
help = " Path to configuration file for alternate models. " ,
2023-05-16 05:50:01 +00:00
)
model_group . add_argument (
2023-07-28 13:46:44 +00:00
" --model " ,
2023-05-16 05:50:01 +00:00
help = ' Indicates which diffusion model to load (defaults to " default " stanza in configs/models.yaml) ' ,
)
model_group . add_argument (
2023-07-28 13:46:44 +00:00
" --weight_dirs " ,
nargs = " + " ,
2023-05-16 05:50:01 +00:00
type = str ,
2023-07-28 13:46:44 +00:00
help = " List of one or more directories that will be auto-scanned for new model weights to import " ,
2023-05-16 05:50:01 +00:00
)
model_group . add_argument (
2023-07-28 13:46:44 +00:00
" --png_compression " ,
" -z " ,
2023-05-16 05:50:01 +00:00
type = int ,
default = 6 ,
2023-07-28 13:46:44 +00:00
choices = range ( 0 , 9 ) ,
dest = " png_compression " ,
help = " level of PNG compression, from 0 (none) to 9 (maximum). Default is 6. " ,
2023-05-16 05:50:01 +00:00
)
model_group . add_argument (
2023-07-28 13:46:44 +00:00
" -F " ,
" --full_precision " ,
dest = " full_precision " ,
action = " store_true " ,
help = " Deprecated way to set --precision=float32 " ,
2023-05-16 05:50:01 +00:00
)
model_group . add_argument (
2023-07-28 13:46:44 +00:00
" --max_loaded_models " ,
dest = " max_loaded_models " ,
2023-05-16 05:50:01 +00:00
type = int ,
default = 2 ,
2023-07-28 13:46:44 +00:00
help = " Maximum number of models to keep in memory for fast switching, including the one in GPU " ,
2023-05-16 05:50:01 +00:00
)
model_group . add_argument (
2023-07-28 13:46:44 +00:00
" --free_gpu_mem " ,
dest = " free_gpu_mem " ,
action = " store_true " ,
help = " Force free gpu memory before final decoding " ,
2023-05-16 05:50:01 +00:00
)
model_group . add_argument (
2023-07-28 13:46:44 +00:00
" --sequential_guidance " ,
dest = " sequential_guidance " ,
action = " store_true " ,
help = " Calculate guidance in serial instead of in parallel, lowering memory requirement " " at the expense of speed " ,
2023-05-16 05:50:01 +00:00
)
model_group . add_argument (
2023-07-28 13:46:44 +00:00
" --xformers " ,
2023-05-16 05:50:01 +00:00
action = argparse . BooleanOptionalAction ,
default = True ,
2023-07-28 13:46:44 +00:00
help = " Enable/disable xformers support (default enabled if installed) " ,
2023-05-16 05:50:01 +00:00
)
model_group . add_argument (
2023-07-28 13:46:44 +00:00
" --always_use_cpu " , dest = " always_use_cpu " , action = " store_true " , help = " Force use of CPU even if GPU is available "
2023-05-16 05:50:01 +00:00
)
model_group . add_argument (
2023-07-28 13:46:44 +00:00
" --precision " ,
dest = " precision " ,
2023-05-16 05:50:01 +00:00
type = str ,
choices = PRECISION_CHOICES ,
2023-07-28 13:46:44 +00:00
metavar = " PRECISION " ,
2023-05-16 05:50:01 +00:00
help = f ' Set model precision. Defaults to auto selected based on device. Options: { " , " . join ( PRECISION_CHOICES ) } ' ,
2023-07-28 13:46:44 +00:00
default = " auto " ,
2023-05-16 05:50:01 +00:00
)
model_group . add_argument (
2023-07-28 13:46:44 +00:00
" --ckpt_convert " ,
2023-05-16 05:50:01 +00:00
action = argparse . BooleanOptionalAction ,
2023-07-28 13:46:44 +00:00
dest = " ckpt_convert " ,
2023-05-16 05:50:01 +00:00
default = True ,
2023-07-28 13:46:44 +00:00
help = " Deprecated option. Legacy ckpt files are now always converted to diffusers when loaded. " ,
2023-05-16 05:50:01 +00:00
)
model_group . add_argument (
2023-07-28 13:46:44 +00:00
" --internet " ,
2023-05-16 05:50:01 +00:00
action = argparse . BooleanOptionalAction ,
2023-07-28 13:46:44 +00:00
dest = " internet_available " ,
2023-05-16 05:50:01 +00:00
default = True ,
2023-07-28 13:46:44 +00:00
help = " Indicate whether internet is available for just-in-time model downloading (default: probe automatically). " ,
2023-05-16 05:50:01 +00:00
)
model_group . add_argument (
2023-07-28 13:46:44 +00:00
" --nsfw_checker " ,
" --safety_checker " ,
2023-05-16 05:50:01 +00:00
action = argparse . BooleanOptionalAction ,
2023-07-28 13:46:44 +00:00
dest = " safety_checker " ,
2023-05-16 05:50:01 +00:00
default = False ,
2023-07-28 13:46:44 +00:00
help = " Check for and blur potentially NSFW images. Use --no-nsfw_checker to disable. " ,
2023-05-16 05:50:01 +00:00
)
model_group . add_argument (
2023-07-28 13:46:44 +00:00
" --autoimport " ,
2023-05-16 05:50:01 +00:00
default = None ,
type = str ,
2023-07-28 13:46:44 +00:00
help = " Check the indicated directory for .ckpt/.safetensors weights files at startup and import directly " ,
2023-05-16 05:50:01 +00:00
)
model_group . add_argument (
2023-07-28 13:46:44 +00:00
" --autoconvert " ,
2023-05-16 05:50:01 +00:00
default = None ,
type = str ,
2023-07-28 13:46:44 +00:00
help = " Check the indicated directory for .ckpt/.safetensors weights files at startup and import as optimized diffuser models " ,
2023-05-16 05:50:01 +00:00
)
model_group . add_argument (
2023-07-28 13:46:44 +00:00
" --patchmatch " ,
2023-05-16 05:50:01 +00:00
action = argparse . BooleanOptionalAction ,
default = True ,
2023-07-28 13:46:44 +00:00
help = " Load the patchmatch extension for outpainting. Use --no-patchmatch to disable. " ,
2023-05-16 05:50:01 +00:00
)
file_group . add_argument (
2023-07-28 13:46:44 +00:00
" --from_file " ,
dest = " infile " ,
2023-05-16 05:50:01 +00:00
type = str ,
2023-07-28 13:46:44 +00:00
help = " If specified, load prompts from this file " ,
2023-05-16 05:50:01 +00:00
)
file_group . add_argument (
2023-07-28 13:46:44 +00:00
" --outdir " ,
" -o " ,
2023-05-16 05:50:01 +00:00
type = str ,
2023-07-28 13:46:44 +00:00
help = " Directory to save generated images and a log of prompts and seeds. Default: ROOTDIR/outputs " ,
default = " outputs " ,
2023-05-16 05:50:01 +00:00
)
file_group . add_argument (
2023-07-28 13:46:44 +00:00
" --prompt_as_dir " ,
" -p " ,
action = " store_true " ,
help = " Place images in subdirectories named after the prompt. " ,
2023-05-16 05:50:01 +00:00
)
render_group . add_argument (
2023-07-28 13:46:44 +00:00
" --fnformat " ,
default = " {prefix} . {seed} .png " ,
2023-05-16 05:50:01 +00:00
type = str ,
2023-07-28 13:46:44 +00:00
help = " Overwrite the filename format. You can use any argument as wildcard enclosed in curly braces. Default is {prefix} . {seed} .png " ,
2023-05-16 05:50:01 +00:00
)
2023-07-28 13:46:44 +00:00
render_group . add_argument ( " -s " , " --steps " , type = int , default = 50 , help = " Number of steps " )
2023-05-16 05:50:01 +00:00
render_group . add_argument (
2023-07-28 13:46:44 +00:00
" -W " ,
" --width " ,
2023-05-16 05:50:01 +00:00
type = int ,
2023-07-28 13:46:44 +00:00
help = " Image width, multiple of 64 " ,
2023-05-16 05:50:01 +00:00
)
render_group . add_argument (
2023-07-28 13:46:44 +00:00
" -H " ,
" --height " ,
2023-05-16 05:50:01 +00:00
type = int ,
2023-07-28 13:46:44 +00:00
help = " Image height, multiple of 64 " ,
2023-05-16 05:50:01 +00:00
)
render_group . add_argument (
2023-07-28 13:46:44 +00:00
" -C " ,
" --cfg_scale " ,
2023-05-16 05:50:01 +00:00
default = 7.5 ,
type = float ,
help = ' Classifier free guidance (CFG) scale - higher numbers cause generator to " try " harder. ' ,
)
render_group . add_argument (
2023-07-28 13:46:44 +00:00
" --sampler " ,
" -A " ,
" -m " ,
dest = " sampler_name " ,
2023-05-16 05:50:01 +00:00
type = str ,
choices = SAMPLER_CHOICES ,
2023-07-28 13:46:44 +00:00
metavar = " SAMPLER_NAME " ,
2023-05-16 05:50:01 +00:00
help = f ' Set the default sampler. Supported samplers: { " , " . join ( SAMPLER_CHOICES ) } ' ,
2023-07-28 13:46:44 +00:00
default = " k_lms " ,
2023-05-16 05:50:01 +00:00
)
render_group . add_argument (
2023-07-28 13:46:44 +00:00
" --log_tokenization " , " -t " , action = " store_true " , help = " shows how the prompt is split into tokens "
2023-05-16 05:50:01 +00:00
)
render_group . add_argument (
2023-07-28 13:46:44 +00:00
" -f " ,
" --strength " ,
2023-05-16 05:50:01 +00:00
type = float ,
2023-07-28 13:46:44 +00:00
help = " img2img strength for noising/unnoising. 0.0 preserves image exactly, 1.0 replaces it completely " ,
2023-05-16 05:50:01 +00:00
)
render_group . add_argument (
2023-07-28 13:46:44 +00:00
" -T " ,
" -fit " ,
" --fit " ,
2023-05-16 05:50:01 +00:00
action = argparse . BooleanOptionalAction ,
2023-07-28 13:46:44 +00:00
help = " If specified, will resize the input image to fit within the dimensions of width x height (512x512 default) " ,
2023-05-16 05:50:01 +00:00
)
2023-07-28 13:46:44 +00:00
render_group . add_argument ( " --grid " , " -g " , action = argparse . BooleanOptionalAction , help = " generate a grid " )
2023-05-16 05:50:01 +00:00
render_group . add_argument (
2023-07-28 13:46:44 +00:00
" --embedding_directory " ,
" --embedding_path " ,
dest = " embedding_path " ,
default = " embeddings " ,
2023-05-16 05:50:01 +00:00
type = str ,
2023-07-28 13:46:44 +00:00
help = " Path to a directory containing .bin and/or .pt files, or a single .bin/.pt file. You may use subdirectories. (default is ROOTDIR/embeddings) " ,
2023-05-16 05:50:01 +00:00
)
render_group . add_argument (
2023-07-28 13:46:44 +00:00
" --lora_directory " ,
dest = " lora_path " ,
default = " loras " ,
2023-05-16 05:50:01 +00:00
type = str ,
2023-07-28 13:46:44 +00:00
help = " Path to a directory containing LoRA files; subdirectories are not supported. (default is ROOTDIR/loras) " ,
2023-05-16 05:50:01 +00:00
)
render_group . add_argument (
2023-07-28 13:46:44 +00:00
" --embeddings " ,
2023-05-16 05:50:01 +00:00
action = argparse . BooleanOptionalAction ,
default = True ,
2023-07-28 13:46:44 +00:00
help = " Enable embedding directory (default). Use --no-embeddings to disable. " ,
2023-05-16 05:50:01 +00:00
)
2023-07-28 13:46:44 +00:00
render_group . add_argument ( " --enable_image_debugging " , action = " store_true " , help = " Generates debugging image to display " )
2023-05-16 05:50:01 +00:00
render_group . add_argument (
2023-07-28 13:46:44 +00:00
" --karras_max " ,
2023-05-16 05:50:01 +00:00
type = int ,
default = None ,
2023-07-28 13:46:44 +00:00
help = " control the point at which the K* samplers will shift from using the Karras noise schedule (good for low step counts) to the LatentDiffusion noise schedule (good for high step counts). Set to 0 to use LatentDiffusion for all step values, and to a high value (e.g. 1000) to use Karras for all step values. [29]. " ,
2023-05-16 05:50:01 +00:00
)
# Restoration related args
postprocessing_group . add_argument (
2023-07-28 13:46:44 +00:00
" --no_restore " ,
dest = " restore " ,
action = " store_false " ,
help = " Disable face restoration with GFPGAN or codeformer " ,
2023-05-16 05:50:01 +00:00
)
postprocessing_group . add_argument (
2023-07-28 13:46:44 +00:00
" --no_upscale " ,
dest = " esrgan " ,
action = " store_false " ,
help = " Disable upscaling with ESRGAN " ,
2023-05-16 05:50:01 +00:00
)
postprocessing_group . add_argument (
2023-07-28 13:46:44 +00:00
" --esrgan_bg_tile " ,
2023-05-16 05:50:01 +00:00
type = int ,
default = 400 ,
2023-07-28 13:46:44 +00:00
help = " Tile size for background sampler, 0 for no tile during testing. Default: 400. " ,
2023-05-16 05:50:01 +00:00
)
postprocessing_group . add_argument (
2023-07-28 13:46:44 +00:00
" --esrgan_denoise_str " ,
2023-05-16 05:50:01 +00:00
type = float ,
default = 0.75 ,
2023-07-28 13:46:44 +00:00
help = " esrgan denoise str. 0 is no denoise, 1 is max denoise. Default: 0.75 " ,
2023-05-16 05:50:01 +00:00
)
postprocessing_group . add_argument (
2023-07-28 13:46:44 +00:00
" --gfpgan_model_path " ,
2023-05-16 05:50:01 +00:00
type = str ,
2023-07-28 13:46:44 +00:00
default = " ./models/gfpgan/GFPGANv1.4.pth " ,
help = " Indicates the path to the GFPGAN model " ,
2023-05-16 05:50:01 +00:00
)
web_server_group . add_argument (
2023-07-28 13:46:44 +00:00
" --web " ,
dest = " web " ,
action = " store_true " ,
help = " Start in web server mode. " ,
2023-05-16 05:50:01 +00:00
)
web_server_group . add_argument (
2023-07-28 13:46:44 +00:00
" --web_develop " ,
dest = " web_develop " ,
action = " store_true " ,
help = " Start in web server development mode. " ,
2023-05-16 05:50:01 +00:00
)
web_server_group . add_argument (
" --web_verbose " ,
action = " store_true " ,
help = " Enables verbose logging " ,
)
web_server_group . add_argument (
" --cors " ,
nargs = " * " ,
type = str ,
help = " Additional allowed origins, comma-separated " ,
)
web_server_group . add_argument (
2023-07-28 13:46:44 +00:00
" --host " ,
2023-05-16 05:50:01 +00:00
type = str ,
2023-07-28 13:46:44 +00:00
default = " 127.0.0.1 " ,
help = " Web server: Host or IP to listen on. Set to 0.0.0.0 to accept traffic from other devices on your network. " ,
2023-05-16 05:50:01 +00:00
)
2023-07-28 13:46:44 +00:00
web_server_group . add_argument ( " --port " , type = int , default = " 9090 " , help = " Web server: Port to listen on " )
2023-05-16 05:50:01 +00:00
web_server_group . add_argument (
2023-07-28 13:46:44 +00:00
" --certfile " ,
2023-05-16 05:50:01 +00:00
type = str ,
default = None ,
2023-07-28 13:46:44 +00:00
help = " Web server: Path to certificate file to use for SSL. Use together with --keyfile " ,
2023-05-16 05:50:01 +00:00
)
web_server_group . add_argument (
2023-07-28 13:46:44 +00:00
" --keyfile " ,
2023-05-16 05:50:01 +00:00
type = str ,
default = None ,
2023-07-28 13:46:44 +00:00
help = " Web server: Path to private key file to use for SSL. Use together with --certfile " ,
2023-05-16 05:50:01 +00:00
)
web_server_group . add_argument (
2023-07-28 13:46:44 +00:00
" --gui " ,
dest = " gui " ,
action = " store_true " ,
help = " Start InvokeAI GUI " ,
2023-05-16 05:50:01 +00:00
)