dgenerate submodules
dgenerate.arguments module
Argument parsing for the dgenerate command line tool.
- exception dgenerate.arguments.DgenerateHelpException[source]
Bases:
ExceptionRaised by
parse_args()andparse_known_args()when--helpis encountered andhelp_raises=True
- exception dgenerate.arguments.DgenerateUsageError[source]
Bases:
ExceptionRaised by
parse_args()andparse_known_args()on argument usage errors.
- class dgenerate.arguments.DgenerateArguments[source]
Bases:
RenderLoopConfigRepresents dgenerates parsed command line arguments, can be used as a configuration object for
dgenerate.renderloop.RenderLoop.- cache_memory_constraints: Sequence[str] | None = None
- control_net_cache_memory_constraints: Sequence[str] | None = None
See:
dgenerate.pipelinewrapper.CONTROL_NET_CACHE_MEMORY_CONSTRAINTS
- guidance_scales: _types.Floats
List of floating point guidance scales, this corresponds to the
--guidance-scalesargument of the dgenerate command line tool.
- image_processor_cuda_memory_constraints: Sequence[str] | None = None
See:
dgenerate.imageprocessors.IMAGE_PROCESSOR_CUDA_MEMORY_CONSTRAINTS
- image_processor_memory_constraints: Sequence[str] | None = None
See:
dgenerate.imageprocessors.IMAGE_PROCESSOR_MEMORY_CONSTRAINTS
- inference_steps: _types.Integers
List of inference steps values, this corresponds to the
--inference-stepsargument of the dgenerate command line tool.
- pipeline_cache_memory_constraints: Sequence[str] | None = None
See:
dgenerate.pipelinewrapper.PIPELINE_CACHE_MEMORY_CONSTRAINTS
- plugin_module_paths: Sequence[str]
Plugin module paths
--plugin-modules
- prompts: _types.Prompts
List of prompt objects, this corresponds to the
--promptsargument of the dgenerate command line tool.
- seeds: _types.Integers
List of integer seeds, this corresponds to the
--seedsargument of the dgenerate command line tool.
- text_encoder_cache_memory_constraints: Sequence[str] | None = None
See:
dgenerate.pipelinewrapper.TEXT_ENCODER_CACHE_MEMORY_CONSTRAINTS
- unet_cache_memory_constraints: Sequence[str] | None = None
See:
dgenerate.pipelinewrapper.UNET_CACHE_MEMORY_CONSTRAINTS
- vae_cache_memory_constraints: Sequence[str] | None = None
- verbose: bool = False
Enable debug output?
-v/--verbose
- dgenerate.arguments.config_attribute_name_to_option(name)[source]
Convert an attribute name of
DgenerateArgumentsinto its command line option name.- Parameters:
name – the attribute name
- Returns:
the command line argument name as a string
- dgenerate.arguments.is_valid_option(option: str)[source]
Check if an option string is a valid option name in the parser.
- Parameters:
option – The option name, short or long opt.
- Returns:
TrueorFalse
- dgenerate.arguments.parse_args(args: Sequence[str] | None = None, throw: bool = True, log_error: bool = True, help_raises: bool = False) DgenerateArguments | None[source]
Parse dgenerates command line arguments and return a configuration object.
- Parameters:
args – arguments list, as in args taken from sys.argv, or in that format
throw – throw
DgenerateUsageErroron error? defaults toTruelog_error – Write ERROR diagnostics with
dgenerate.messages?help_raises –
--helpraisesdgenerate.arguments.DgenerateHelpException? WhenTrue, this will occur even ifthrow=False
- Raises:
- Returns:
DgenerateArguments. Ifthrow=FalsethenNonewill be returned on errors.
- dgenerate.arguments.parse_device(args: Sequence[str] | None = None, throw_unknown: bool = False, log_error: bool = False) tuple[str | None, list[str]][source]
Retrieve the
-d/--deviceargument value- Parameters:
args – command line arguments
throw_unknown – Raise
DgenerateUsageErrorif any other specified argument is not a valid dgenerate argument? This treats the primary model argument as optional, and only goes into effect if the specific argument is detected.log_error – Write ERROR diagnostics with
dgenerate.messages?
- Raises:
DgenerateUsageError – If no argument value was provided.
- Returns:
(value |
None, unknown_args_list)
- dgenerate.arguments.parse_directives_help(args: Sequence[str] | None = None, throw_unknown: bool = False, log_error: bool = False) tuple[list[str] | None, list[str]][source]
Retrieve the
--directives-helpargument value- Parameters:
args – command line arguments
throw_unknown – Raise
DgenerateUsageErrorif any other specified argument is not a valid dgenerate argument? This treats the primary model argument as optional, and only goes into effect if the specific argument is detected.log_error – Write ERROR diagnostics with
dgenerate.messages?
- Returns:
(values |
None, unknown_args_list)
- dgenerate.arguments.parse_functions_help(args: Sequence[str] | None = None, throw_unknown: bool = False, log_error: bool = False) tuple[list[str] | None, list[str]][source]
Retrieve the
--functions-helpargument value- Parameters:
args – command line arguments
throw_unknown – Raise
DgenerateUsageErrorif any other specified argument is not a valid dgenerate argument? This treats the primary model argument as optional, and only goes into effect if the specific argument is detected.log_error – Write ERROR diagnostics with
dgenerate.messages?
- Returns:
(values |
None, unknown_args_list)
- dgenerate.arguments.parse_image_processor_help(args: Sequence[str] | None = None, throw_unknown: bool = False, log_error: bool = False) tuple[list[str] | None, list[str]][source]
Retrieve the
--image-processor-helpargument value- Parameters:
args – command line arguments
throw_unknown – Raise
DgenerateUsageErrorif any other specified argument is not a valid dgenerate argument? This treats the primary model argument as optional, and only goes into effect if the specific argument is detected.log_error – Write ERROR diagnostics with
dgenerate.messages?
- Returns:
(values |
None, unknown_args_list)
- dgenerate.arguments.parse_known_args(args: Sequence[str] | None = None, throw: bool = True, log_error: bool = True, no_model: bool = True, no_help: bool = True, help_raises: bool = False) tuple[DgenerateArguments, list[str]] | None[source]
Parse only known arguments off the command line.
Ignores dgenerates only required argument
model_pathby default.No logical validation is preformed,
DgenerateArguments.check()is not called by this function, only argument parsing and simple type validation is preformed by this function.- Parameters:
args – arguments list, as in args taken from sys.argv, or in that format
throw – throw
DgenerateUsageErroron error? defaults toTruelog_error – Write ERROR diagnostics with
dgenerate.messages?no_model – Remove the
model_pathargument from the parser.no_help – Remove the
--helpargument from the parser.help_raises –
--helpraisesdgenerate.arguments.DgenerateHelpException? WhenTrue, this will occur even ifthrow=False
- Raises:
DgenerateUsageError – on argument error (simple type validation only)
- Returns:
(
DgenerateArguments, unknown_args_list). Ifthrow=FalsethenNonewill be returned on errors.
- dgenerate.arguments.parse_plugin_modules(args: Sequence[str] | None = None, throw_unknown: bool = False, log_error: bool = False) tuple[list[str] | None, list[str]][source]
Retrieve the
--plugin-modulesargument value- Parameters:
args – command line arguments
throw_unknown – Raise
DgenerateUsageErrorif any other specified argument is not a valid dgenerate argument? This treats the primary model argument as optional, and only goes into effect if the specific argument is detected.log_error – Write ERROR diagnostics with
dgenerate.messages?
- Raises:
DgenerateUsageError – If no argument values were provided.
- Returns:
(values |
None, unknown_args_list)
- dgenerate.arguments.parse_prompt_weighter_help(args: Sequence[str] | None = None, throw_unknown: bool = False, log_error: bool = False) tuple[list[str] | None, list[str]][source]
Retrieve the
--prompt-weighter-helpargument value- Parameters:
args – command line arguments
throw_unknown – Raise
DgenerateUsageErrorif any other specified argument is not a valid dgenerate argument? This treats the primary model argument as optional, and only goes into effect if the specific argument is detected.log_error – Write ERROR diagnostics with
dgenerate.messages?
- Returns:
(values |
None, unknown_args_list)
- dgenerate.arguments.parse_sub_command(args: Sequence[str] | None = None) tuple[str | None, list[str]][source]
Retrieve the
--sub-commandargument value- Parameters:
args – command line arguments
- Raises:
DgenerateUsageError – If no argument value was provided.
- Returns:
(value |
None, unknown_args_list)
- dgenerate.arguments.parse_sub_command_help(args: Sequence[str] | None = None, throw_unknown: bool = False, log_error: bool = False) tuple[list[str] | None, list[str]][source]
Retrieve the
--sub-command-helpargument value- Parameters:
args – command line arguments
throw_unknown – Raise
DgenerateUsageErrorif any other specified argument is not a valid dgenerate argument? This treats the primary model argument as optional, and only goes into effect if the specific argument is detected.log_error – Write ERROR diagnostics with
dgenerate.messages?
- Returns:
(values |
None, unknown_args_list)
- dgenerate.arguments.parse_templates_help(args: Sequence[str] | None = None, throw_unknown: bool = False, log_error: bool = False) tuple[list[str] | None, list[str]][source]
Retrieve the
--templates-helpargument value- Parameters:
args – command line arguments
throw_unknown – Raise
DgenerateUsageErrorif any other specified argument is not a valid dgenerate argument? This treats the primary model argument as optional, and only goes into effect if the specific argument is detected.log_error – Write ERROR diagnostics with
dgenerate.messages?
- Returns:
(values |
None, unknown_args_list)
- dgenerate.arguments.parse_verbose(args: Sequence[str] | None = None, throw_unknown: bool = False, log_error: bool = False) tuple[bool, list[str]][source]
Retrieve the
-v/--verboseargument value- Parameters:
args – command line arguments
throw_unknown – Raise
DgenerateUsageErrorif any other specified argument is not a valid dgenerate argument? This treats the primary model argument as optional, and only goes into effect if the specific argument is detected.log_error – Write ERROR diagnostics with
dgenerate.messages?
- Returns:
(value, unknown_args_list)
dgenerate.batchprocess module
Batch processing / dgenerate config scripts.
- exception dgenerate.batchprocess.BatchProcessError[source]
Bases:
ExceptionThrown by
BatchProcessor.run_file()andBatchProcessor.run_string()when an error in a batch processing script is encountered.
- exception dgenerate.batchprocess.ConfigRunnerPluginArgumentError[source]
Bases:
PluginArgumentErrorThrown when a
dgenerate.batchprocess.ConfigRunnerPluginplugin is not instantiated with correct arguments.
- exception dgenerate.batchprocess.ConfigRunnerPluginNotFoundError[source]
Bases:
PluginNotFoundErrorThrown when
ConfigRunnerPluginLoadercannot find anydgenerate.batchprocess.ConfigRunnerPluginimplementation for a specified name.
- class dgenerate.batchprocess.BatchProcessor(invoker: Callable[[Sequence[str]], int], name: str, version: tuple[int, int, int] | str, template_variables: dict[str, Any] | None = None, reserved_template_variables: set[str] | None = None, template_functions: dict[str, Callable[[Any], Any]] | None = None, directives: dict[str, Callable[[list], None]] | None = None, builtins: dict[str, Callable[[Any], Any]] | None = None, injected_args: Sequence[str] | None = None)[source]
Bases:
objectImplements dgenerates batch processing scripts in a generified manner.
This is the bare-bones implementation of the shell with nothing implemented for you except for:
\env\set\sete\setp\unset\unset_env\print\echo
If you wish to create this object to run a dgenerate configuration, use
dgenerate.batchprocess.ConfigRunner- __init__(invoker: Callable[[Sequence[str]], int], name: str, version: tuple[int, int, int] | str, template_variables: dict[str, Any] | None = None, reserved_template_variables: set[str] | None = None, template_functions: dict[str, Callable[[Any], Any]] | None = None, directives: dict[str, Callable[[list], None]] | None = None, builtins: dict[str, Callable[[Any], Any]] | None = None, injected_args: Sequence[str] | None = None)[source]
- Parameters:
invoker – A function for invoking lines recognized as shell commands, should return a return code.
name – The name of this batch processor, currently used in the version check directive and messages
version – Version for version check hash bang directive.
template_variables – Live template variables, the initial environment, this dictionary will be modified during runtime.
reserved_template_variables – These template variable names cannot be set with the
\setor\setpdirective, or un-defined with the\unsetdirective.template_functions – Functions available to Jinja2
directives – batch processing directive handlers, for:
\directives. This is a dictionary of names to functions which accept a single parameter, a list of directive arguments, and return a return code.builtins – builtin functions available as template functions and
\setpfunctions. A safe default collection of functions is used if this is not specified. Builtins may be overridden by functions defined intemplate_functionsinjected_args – Arguments to be injected at the end of user specified arguments for every shell invocation. If
-v/--verboseis present ininjected_argsdebugging output will be enabled globally while the config runs, and not just for invocations. Passing-v/--verbosealso disables handling of unhandled nonSystemExitexceptions raised by config directive implementations, a stack trace will be printed when these exceptions are encountered.
- static default_builtins() dict[str, Callable[[Any], Any]][source]
Return the default builtins available as template functions.
- render_template(string: str, stream: bool = False) str | Iterator[str][source]
Render a template from a string
- Parameters:
string – the string containing the Jinja2 template.
stream – Stream the results of generating this template line by line?
- Returns:
rendered string
- run_file(stream: Iterator[str])[source]
Process a batch processing script from a file stream.
Technically, from an iterator over lines of text.
- Raises:
- Parameters:
stream – A filestream in text read mode
- run_string(string: str)[source]
Process a batch processing script from a string
- Raises:
- Parameters:
string – a string containing the script
- user_define(name: str, value)[source]
Define a template variable as if you were the user.
- Raises:
BatchProcessError – if the specified variable name cannot be defined by the user due to not being a valid identifier string, being the name of a template function, being the name of a reserved template variable, or being the name of a builtin function.
- Parameters:
name – Variable name
value – Assigned value
- user_define_check(name: str)[source]
Check if a template variable can be defined by the user, raise if not.
- Raises:
BatchProcessError – if the specified variable name cannot be defined by the user due to not being a valid identifier string, being the name of a template function, being the name of a reserved template variable, or being the name of a builtin function.
- Parameters:
name – Variable name
- user_undefine(name: str)[source]
Undefine a template variable as if you were the user.
- Raises:
BatchProcessError – if the specified variable name cannot be undefined by the user due to not being a valid identifier string, being the name of a template function, being the name of a reserved template variable, being the name of a builtin function, or a non existing template variable.
- Parameters:
name – Variable name
- user_undefine_check(name: str)[source]
Check if a template variable can be undefined by the user, raise if not.
- Raises:
BatchProcessError – if the specified variable name cannot be undefined by the user due to not being a valid identifier string, being the name of a template function, being the name of a reserved template variable, being the name of a builtin function, or a non existing template variable.
- Parameters:
name – Variable name
- builtins: dict[str, Callable[[Any], Any]]
Safe python builtins that are always available as template functions and also usable with
\setpThey may be overridden by functions defined in
dgenerate.batchprocess.BatchProcessor.template_functions
- property current_line: int
The current line number in the file being processed.
- directives: dict[str, Callable[[Sequence[str]], int]] | None
Batch process directives, shell commands starting with a backslash.
Dictionary of callable(list) -> int.
The function should return a return code, 0 for success, anything else for failure.
- property directives_builtins_help: dict[str, str]
Returns a dictionary of help strings for directives that are built into the
BatchProcessorbase class.
- property executing_text: None | str
The text / command line that is currently being executed, or that was last executed.
- expand_vars: Callable[[str], str]
A function for expanding environmental variables, defaults to
dgenerate.textprocessing.shell_expandvars()
- injected_args: Sequence[str]
Shell arguments to inject at the end of every invocation.
- invoker: Callable[[Sequence[str]], int]
Invoker function, responsible for executing lines recognized as shell commands.
- name: str
Name of this batch processor, currently used in the hash bang version check directive and messages.
- reserved_template_variables: set[str]
These template variables cannot be set with the
\set,\sete, or\setpdirective, or un-defined with the\unsetdirective.
- template_functions: dict[str, Callable[[Any], Any]]
Functions available when templating is occurring.
- template_variables: dict[str, Any]
Live template variables.
- version: tuple[int, int, int]
Version tuple for the version check hash bang directive.
- class dgenerate.batchprocess.CivitAILinksDirective(**kwargs)[source]
Bases:
ConfigRunnerPlugin
- class dgenerate.batchprocess.ConfigRunner(injected_args: Sequence[str] | None = None, render_loop: RenderLoop | None = None, plugin_loader: ConfigRunnerPluginLoader = None, version: tuple[int, int, int] | str = '3.10.1', throw: bool = False)[source]
Bases:
BatchProcessorA
BatchProcessorthat can run dgenerate batch processing configs from a string or file.- __init__(injected_args: Sequence[str] | None = None, render_loop: RenderLoop | None = None, plugin_loader: ConfigRunnerPluginLoader = None, version: tuple[int, int, int] | str = '3.10.1', throw: bool = False)[source]
- Raises:
dgenerate.plugin.ModuleFileNotFoundError – If a module path parsed from
--plugin-modulesininjected_argscould not be found on disk.- Parameters:
injected_args – dgenerate command line arguments in the form of a list, see: shlex module, or sys.argv. These arguments will be injected at the end of every dgenerate invocation in the config.
--plugin-modulesare parsed frominjected_argsand added toplugin_loader. If-v/--verboseis present ininjected_argsdebugging output will be enabled globally while the config runs, and not just for invocations.render_loop – RenderLoop instance, if
Noneis provided one will be created.plugin_loader – Batch processor plugin loader, if one is not provided one will be created.
version – Config version for
#! dgenerate x.x.xversion checks, defaults todgenerate.__version__throw – Whether to throw exceptions from
dgenerate.invoker.invoke_dgenerate()or handle them. If you set this toTrueexceptions will propagate out of dgenerate invocations instead of adgenerate.batchprocess.BatchProcessErrorbeing raised by the createddgenerate.batchprocess.BatchProcessor. A line number where the error occurred can be obtained usingdgenerate.batchprocess.BatchProcessor.current_line.
- generate_directives_help(directive_names: Collection[str] | None = None)[source]
Generate the help string for
--directives-help- Parameters:
directive_names – Display help for specific directives, if
Noneor[]is specified, display all.- Raises:
ValueError – if given directive names could not be found
- Returns:
help string
- generate_functions_help(function_names: Collection[str] | None = None)[source]
Generate the help string for
--functions-help- Parameters:
function_names – Display help for specific functions, if
Noneor[]is specified, display all.- Raises:
ValueError – if given directive names could not be found
- Returns:
help string
- generate_template_variables_help(variable_names: Collection[str] | None = None, show_values: bool = True)[source]
Generate a help string describing available template variables, their types, and values for use in batch processing.
This is used for
--templates-help- Parameters:
variable_names – Display help for specific variables, if
Noneor[]is specified, display all.show_values – Show the value of the template variable or just the name?
- Raises:
ValueError – if given variable names could not be found
- Returns:
a human-readable description of all template variables
- property plugin_module_paths: frozenset[str]
Set of plugin module paths if they were injected into the config runner by
--plugin-modulesor used in a\import_pluginsstatement in a config.- Returns:
a set of paths, may be empty but not
None
- class dgenerate.batchprocess.ConfigRunnerPlugin(loaded_by_name: str, config_runner: ConfigRunner | None = None, render_loop: RenderLoop | None = None, **kwargs)[source]
Bases:
PluginAbstract base class for config runner plugin implementations.
- __init__(loaded_by_name: str, config_runner: ConfigRunner | None = None, render_loop: RenderLoop | None = None, **kwargs)[source]
- Parameters:
loaded_by_name – The name the plugin was loaded by, will be passed by the loader.
argument_error_type – This exception type will be raised upon argument errors (invalid arguments) when loading a plugin using a
PluginLoaderimplementation. It should match theargument_error_typegiven to thePluginLoaderimplementation being used to load the inheritor of this class.kwargs – Additional arguments that may arise when using an
ARGSstatic signature definition with multipleNAMESin your implementation.
- register_directive(name, implementation: Callable[[Sequence[str]], int])[source]
Register a config directive implementation on the
dgenerate.batchprocess.ConfigRunnerinstance.Your directive should return a return code, 0 for success and anything else for failure.
Returning non zero will cause
BatchProcessErrorto be raised from the runner, halting execution of the config.Any non-exiting exception will be eaten and rethrown as
BatchProcessError, also halting execution of the config.- Raises:
RuntimeError – if a config directive with the same name already exists
- Parameters:
name – directive name
implementation – implementation callable
- register_template_function(name, implementation: Callable)[source]
Register a config template function implementation on the
dgenerate.batchprocess.ConfigRunnerinstance.- Raises:
RuntimeError – if a template function with the same name already exists
- Parameters:
name – function name
implementation – implementation callable
- set_template_variable(name, value)[source]
Set a template variable on the
dgenerate.batchprocess.ConfigRunnerinstance.- Parameters:
name – variable name
value – variable value
- update_template_variables(values)[source]
Update multiple template variable values on the
dgenerate.batchprocess.ConfigRunnerinstance.- Parameters:
values – variable values, dictionary of names to values
- property config_runner: ConfigRunner | None
Provides access to the currently instantiated
dgenerate.batchprocess.ConfigRunnerobject running the config file that this directive is being invoked in.
- property injected_args: Sequence[str]
Return any arguments injected into the config from the command line.
If none were injected an empty sequence will be returned.
- Returns:
command line arguments
- property plugin_module_paths: frozenset[str]
Set of plugin module paths if they were injected into the config runner by
--plugin-modulesor used in a\import_pluginsstatement in a config.- Returns:
a set of paths, may be empty but not
None
- property render_loop: RenderLoop | None
Provides access to the currently instantiated
dgenerate.renderloop.RenderLoopobject.This object will have been used for any previous invocation of dgenerate in a config file.
- class dgenerate.batchprocess.ConfigRunnerPluginLoader[source]
Bases:
PluginLoaderLoads
dgenerate.batchprocess.ConfigRunnerPluginplugins.- load(uri: str, **kwargs) ConfigRunnerPlugin[source]
Load an plugin using a URI string containing its name and arguments.
- Parameters:
uri – The URI string
kwargs – default argument values, will be override by arguments specified in the URI
- Raises:
ValueError – If uri is
NoneRuntimeError – If a plugin is discovered to be using a reserved argument name upon loading it.
PluginArgumentError – If there is an error in the loading arguments for the plugin.
PluginNotFoundError – If the plugin name mentioned in the URI could not be found.
- Returns:
plugin instance
- class dgenerate.batchprocess.ImageProcessDirective(**kwargs)[source]
Bases:
ConfigRunnerPlugin
dgenerate.filecache module
On disk file cache implementation and primitives.
- class dgenerate.filecache.CachedFile(data_dict)[source]
Bases:
objectRepresents the path of a file in a
FileCache- metadata: dict[str, str]
Optional metadata for the file stored in the database.
- path: str
The path to the file on disk.
- class dgenerate.filecache.FileCache(db_path: str, cache_dir: str)[source]
Bases:
objectA cache system that stores files and their metadata.
- __init__(db_path: str, cache_dir: str)[source]
Initializes the
FileCacheobject with a key-value store located atdb_pathand a cache directory atcache_dir. If the cache directory doesn’t exist, it creates it.- Parameters:
db_path – The path to the key-value store database.
cache_dir – The directory where the cache files are stored.
- add(key: str, file_data: bytes | Iterable[bytes], metadata: Dict[str, str] = None, ext: str | None = None) CachedFile | None[source]
Adds a file to the cache. If a file with the same key already exists, it overwrites the existing file. Otherwise, it creates a new file with a unique filename.
- Parameters:
key – The key associated with the file.
file_data – The data of the file in bytes, or an iterable of binary chunks.
metadata – The metadata of the file.
ext – The extension of the file.
- Returns:
A
CachedFileobject representing the added file.
- delete_older_than(timedelta: timedelta) Iterator[CachedFile][source]
Deletes items from the key-value store that are older than the specified timedelta, yielding each key and its corresponding
CachedFileobject.
- get(key) CachedFile | None[source]
Retrieves the
CachedFileobject for the specified key from the key-value store, or returns None if the key does not exist.- Parameters:
key – The key associated with the file.
- Returns:
A
CachedFileobject representing the file, orNoneif the key does not exist.
- items() Iterator[CachedFile][source]
Yields all items in the key-value store as
CachedFileobjects.
- class dgenerate.filecache.KeyValueStore(db_path: str)[source]
Bases:
objectA key-value store using SQLite3 for storage.
- __init__(db_path: str)[source]
Initialize the key-value store.
- Parameters:
db_path – The path to the SQLite3 database file.
- delete_older_than(timedelta: timedelta) list[tuple[str, str]][source]
Delete all keys and their associated values that were created more than a certain time ago.
- Parameters:
timedelta – The age of the keys to delete.
- Returns:
The keys and values that were deleted.
- get(key: str, default=None)[source]
Get the value associated with a key.
- Parameters:
key – The key to get the value for.
default – The default value to return if the key is not found.
- Returns:
The value associated with the key, or the default value if the key is not found.
- class dgenerate.filecache.WebFileCache(db_path: str, cache_dir: str, expiry_delta: timedelta = datetime.timedelta(seconds=43200))[source]
Bases:
FileCacheA cache system that stores files and their metadata downloaded from the web.
- __init__(db_path: str, cache_dir: str, expiry_delta: timedelta = datetime.timedelta(seconds=43200))[source]
Initializes the
WebFileCacheobject with a key-value store located atdb_path, a cache directory atcache_dir, and an expiry delta. If the cache directory doesn’t exist, it creates it. It also attempts to clear old files.- Parameters:
db_path – The path to the key-value store database.
cache_dir – The directory where the cache files are stored.
expiry_delta – The time delta for file expiry.
- download(url, mime_acceptable_desc: str | None = None, mimetype_is_supported: ~typing.Callable[[str], bool] | None = None, unknown_mimetype_exception=<class 'ValueError'>, overwrite: bool = False, tqdm_pbar=<class 'tqdm.std.tqdm'>)[source]
Downloads a file and/or returns a file path from the cache. If the mimetype of the file is not supported, it raises an exception.
- Raises:
requests.RequestException – Can raise any exception raised by
requests.getfor request related errors.- Parameters:
url – The URL of the file.
mime_acceptable_desc – A description of acceptable mimetypes for use in exceptions.
mimetype_is_supported – A function that determines if a mimetype is supported for downloading.
unknown_mimetype_exception – The exception type to raise when an unknown mimetype is encountered.
overwrite – Always overwrite any previously cached file?
tqdm_pbar – tqdm progress bar type, if set to None no progress bar will be used. Defaults to tqdm.tqdm
- Returns:
The path to the downloaded file.
- static is_downloadable_url(string) bool[source]
Does a string represent a URL that can be downloaded by this web cache implementation?
- Parameters:
string – the string
- Returns:
TrueorFalse
- request_mimetype(url) str[source]
Requests the mimetype of a file at a URL. If the file exists in the cache, a known mimetype is returned without connecting to the internet. Otherwise, it connects to the internet to retrieve the mimetype. This action does not update the cache.
- Raises:
HTTPError – On http status errors.
- Parameters:
url – The URL of the file.
- Returns:
The mimetype of the file.
dgenerate.filelock module
Thread / Multiprocess safe file locking utilities.
- dgenerate.filelock.suffix_path_maker(filenames: str | Iterable[str], suffix: str) Callable[[str | None, int | None], str | Iterable[str]][source]
To be used with
touch_avoid_duplicate(), a pathmaker implementation that appends a suffix and a number to a filename or list of files when a duplicate is detected for any of them in the directory.- Parameters:
filenames – Original filename, or a list of filenames
suffix – Suffix to append if needed, a trailing number will be appended
- Returns:
- dgenerate.filelock.temp_file_lock(path)[source]
Multiprocess synchronization utility.
Get a lock on an empty file as a context manager, delete the lock file if possible when done.
- Parameters:
path – Path where the lock file will be created.
- Returns:
Lock as a context manager
- dgenerate.filelock.touch_avoid_duplicate(directory: str, path_maker: Callable[[str | None, int | None], str | Iterable[str]], lock_name: str = '.lock', return_list=False)[source]
Generate a filename in a directory and avoid duplicates using a file lock in that directory with a known name. Use to ensure duplicate checking in a directory is multiprocess safe, at least for processes using this function to write to the same directory.
- Parameters:
return_list – Always return a list even if generated paths is only of length
1, defaults toFalse, which means that a single string will be returned if only one path was generated by the pathmakerdirectory – The directory to create the lockfile in
path_maker – Callback that generates paths until a non-existent path is found, first argument is the base filename and the second is attempt number. On the first attempt to create the files both arguments will be none, in which case the callback should return a single filename or iterable of filenames to touch with duplicate avoidance. Calls to the callback thereafter will have non None values for both arguments and the callback should take the passed base filename and apply a suffix using the attempt number.
lock_name – Name of the lock file to be used as a mutex
- Returns:
Unique path that has been touched (created but empty), or a tuple of paths if the path maker requested duplicate checks on multiple files
dgenerate.files module
Utilities for file like objects.
- class dgenerate.files.GCFile(file)[source]
Bases:
objectFile object wrapper, close file on garbage collection
- class dgenerate.files.PeekReader(iterator: Iterator[str])[source]
Bases:
objectRead from a file like iterator object while peeking at the next line.
This is an iterable reader wrapper that yields the tuple
(current_line, next_line)next_line will be
Noneif the next line is the end of iterator / file.
- class dgenerate.files.TerminalLineReader(file: BinaryIO | Callable[[], IO])[source]
Bases:
objectReads lines from a binary stream, typically stdout or stderr of a subprocess.
Breaks on newlines and carriage return, preserves newlines and carriage return in the output as is.
- __init__(file: BinaryIO | Callable[[], IO])[source]
- Parameters:
file – Binary IO object, or a function that returns one.
- property file: BinaryIO
The current file object being read.
- pushback_byte: bytes | None
Byte on the stack which will be prepended to the next line if needed.
Should be set to
Noneif file was provided a callable and the underlying reader has changed to a new instance.
dgenerate.image module
Image operations commonly used by dgenerate.
- dgenerate.image.align_by(iterable: Iterable[int], align: int) tuple[source]
Align all elements by a value and return a tuple
- Parameters:
iterable – Elements to align
align – The alignment value,
Noneindicates no alignment.
- Returns:
tuple(…)
- dgenerate.image.copy_img(img: Image)[source]
Copy a
PIL.Image.Imagewhile preserving its filename attribute.- Parameters:
img – the image
- Returns:
a copy of the image
- dgenerate.image.get_filename(img: Image)[source]
Get the
PIL.Image.Image.filenameattribute or “NO_FILENAME” if it does not exist.- Parameters:
img –
PIL.Image.Image- Returns:
filename string or “NO_FILENAME”
- dgenerate.image.is_aligned(iterable: Iterable[int], align: int) bool[source]
Check if all elements are aligned by a specific value.
- Parameters:
iterable – Elements to test
align – The alignment value,
Noneindicates no alignment.
- Returns:
bool
- dgenerate.image.is_power_of_two(iterable: Iterable[int]) bool[source]
Check if all elements are a power of 2.
- Parameters:
iterable – Elements to test
- Returns:
bool
- dgenerate.image.letterbox_image(img, box_size: tuple[int, int], box_is_padding: bool = False, box_color: str | int | float | tuple[int, int, int] | tuple[float, float, float] | None = None, inner_size: tuple[int, int] = None, aspect_correct: bool = True)[source]
Letterbox an image on to a colored background.
- Parameters:
img – The image to letterbox
box_size – Size of the outer letterbox
box_is_padding – The
letterbox_sizeargument should be interpreted as padding?box_color – What color to use for the letter box background, the default is black. This should be specified as a HEX color code, or as a 3 tuple of integer or floating point RGB values, or as a single integer or float representing all color channels.
inner_size – The size of the inner image
aspect_correct – Should the size of the inner image be aspect correct?
- Returns:
The letterboxed image
- dgenerate.image.nearest_power_of_two(iterable: Iterable[int]) tuple[source]
Round all elements to the nearest power of two and return a tuple.
- Parameters:
iterable – Elements to round
- Returns:
tuple(…)
- dgenerate.image.resize_image(img: Image, size: tuple[int, int] | None, aspect_correct: bool = True, align: int | None = 8)[source]
Resize a
PIL.Image.Imageand return a copy.This function always returns a copy even if the images size did not change.
The new image dimension will always be forcefully aligned by
align, specifyingNoneor1indicates no alignment.The filename attribute is preserved.
- Parameters:
img – the image to resize
size – requested new size for the image, may be
None.aspect_correct – preserve aspect ratio?
align – Force alignment by this amount of pixels.
- Returns:
the resized image
- dgenerate.image.resize_image_calc(old_size: tuple[int, int], new_size: tuple[int, int] | None, aspect_correct: bool = True, align: int | None = 8)[source]
Calculate the new dimensions for a requested resize of an image..
- Parameters:
old_size – The old image size
new_size – The new image size
aspect_correct – preserve aspect ratio?
align – Ensure returned size is aligned to this value.
- Returns:
calculated new size
dgenerate.image_process module
Implements the behaviors of dgenerates image-process sub-command and \image_process config directive.
- exception dgenerate.image_process.ImageProcessHelpException[source]
Bases:
ExceptionRaised by
parse_args()when--helpis encountered andhelp_raises=True
- exception dgenerate.image_process.ImageProcessRenderLoopConfigError[source]
Bases:
ExceptionRaised by
ImageProcessRenderLoopConfig.check()on validation errors.
- exception dgenerate.image_process.ImageProcessUsageError[source]
Bases:
ExceptionThrown by
parse_args()on usage errors.
- class dgenerate.image_process.AnimationETAEvent(origin, frame_index: int, total_frames: int, eta: timedelta)[source]
Bases:
EventCommon event stream object produced by the
events()event stream of a render loop.Occurs when there is an update about the estimated finish time of an animation being generated.
- eta: timedelta
Current estimated time to complete the animation.
- frame_index: int
Frame index at which the ETA was calculated.
- total_frames: int
Total frames needed for the animation to complete.
- class dgenerate.image_process.AnimationFileFinishedEvent(origin: ImageProcessRenderLoop, path: str, starting_event: StartingAnimationFileEvent)[source]
Bases:
EventGenerated in the event stream of
ImageProcessRenderLoop.events()Occurs when an animation (video or animated image) has finished being written to disk.
- __init__(origin: ImageProcessRenderLoop, path: str, starting_event: StartingAnimationFileEvent)[source]
- path: str
Path on disk where the video/animated image was saved.
- starting_event: StartingAnimationFileEvent
Animation
StartingAnimationFileEventrelated to this file finished event.
- class dgenerate.image_process.AnimationFinishedEvent(origin, starting_event: StartingAnimationEvent)[source]
Bases:
EventCommon event stream object produced by the
events()event stream of a render loop.Occurs when a sequence of images that belong to an animation are done generating.
This occurs whether an animation was written to disk or not.
- __init__(origin, starting_event: StartingAnimationEvent)[source]
- starting_event: StartingAnimationEvent
Animation
StartingAnimationEventrelated to this file finished event.
- class dgenerate.image_process.ImageFileSavedEvent(origin: ImageProcessRenderLoop, generated_event, path)[source]
Bases:
EventGenerated in the event stream of
ImageProcessRenderLoop.events()Occurs when an image file is written to disk.
- __init__(origin: ImageProcessRenderLoop, generated_event, path)[source]
- generated_event: ImageGeneratedEvent
The
ImageGeneratedEventfor the image that was saved.
- path: str
Path to the saved image.
- class dgenerate.image_process.ImageGeneratedEvent(origin: ImageProcessRenderLoop, image: Image, generation_step: int, suggested_directory: str, suggested_filename: str, is_animation_frame=False, frame_index: int | None = None)[source]
Bases:
EventGenerated in the event stream of
ImageProcessRenderLoop.events()Occurs when an image is generated (but not saved yet).
- __init__(origin: ImageProcessRenderLoop, image: Image, generation_step: int, suggested_directory: str, suggested_filename: str, is_animation_frame=False, frame_index: int | None = None)[source]
- frame_index: int | None
The frame index if this is an animation frame.
- generation_step: int
The current generation step. (zero indexed)
- image: Image
The generated image.
- is_animation_frame: bool
Is this image a frame in an animation?
- suggested_directory: str
A suggested directory path for saving this image in.
A value of
'.'may be present, this indicates the current working directory.
- suggested_filename: str
A suggested filename for saving this image as. This filename will be unique to the render loop run / configuration. This is just the filename, it will not contain a directory name.
- class dgenerate.image_process.ImageProcessArgs[source]
Bases:
ImageProcessRenderLoopConfigConfiguration object for
ImageProcessRenderLoop- plugin_module_paths: Sequence[str]
- class dgenerate.image_process.ImageProcessRenderLoop(config: ImageProcessRenderLoopConfig = None, image_processor_loader: ImageProcessorLoader | None = None)[source]
Bases:
objectImplements the behavior of the
image-processsub-command as well as\image_processdirective.- __init__(config: ImageProcessRenderLoopConfig = None, image_processor_loader: ImageProcessorLoader | None = None)[source]
- events() Generator[ImageGeneratedEvent | StartingAnimationEvent | StartingAnimationFileEvent | AnimationFileFinishedEvent | ImageFileSavedEvent | AnimationFinishedEvent | StartingGenerationStepEvent | AnimationETAEvent, None, None][source]
Run the render loop, and iterate over a stream of event objects produced by the render loop.
Event objects are of the union type
RenderLoopEventThe exceptions mentioned here are those you may encounter upon iterating, they will not occur upon simple acquisition of the event stream iterator.
- Raises:
dgenerate.OutOfMemoryError – if the execution device runs out of memory
ImageProcessRenderLoopConfigError – on config errors
- Returns:
RenderLoopEventStream
- run()[source]
Run the render loop, this calls
ImageProcessRenderLoopConfig.check()prior to running.- Raises:
dgenerate.OutOfMemoryError – if the execution device runs out of memory
ImageProcessRenderLoopConfigError – on config errors
- disable_writes: bool = False
Disable or enable all writes to disk, if you intend to only ever use the event stream of the render loop when using dgenerate as a library, this is a useful option.
RenderLoop.last_imagesandlast_animationswill not be available if writes to disk are disabled.
- image_processor_loader: ImageProcessorLoader
The loader responsible for loading user specified image processors
- message_header: str = 'image-process'
Used as the header for messages written via
dgenerate.messages
- property written_animations: Iterable[str]
Iterable over animation filenames written by the last run
- property written_images: Iterable[str]
Iterable over image filenames written by the last run
- class dgenerate.image_process.ImageProcessRenderLoopConfig[source]
Bases:
SetFromMixin- check(attribute_namer: Callable[[str], str] = None)[source]
Preforms logical validation on the configuration.
- align: int = 8
Forced image alignment, corresponds to
-al/--align
- device: str = 'cuda'
Rendering device, corresponds to
-d/--device
- frame_end: int | None = None
Optional zero indexed inclusive frame slice end, corresponds to
-fe/--frame-end
- frame_format: str = 'png'
Animation frame format, corresponds to
-ff/-frame-format
- frame_start: int = 0
Zero indexed inclusive frame slice start, corresponds to
-fs/--frame-start
- input: Sequence[str]
Input file paths.
- no_animation_file: bool = False
Disable animated file output when rendering an animation? mutually exclusive with
no_frames. Corresponds to-naf/--no-animation-file
- no_aspect: bool = False
Disable aspect correction? corresponds to
-na/--no-aspect
- no_frames: bool = False
Disable frame output when rendering an animation? mutually exclusive with
no_animation. Corresponds to-nf/--no-frames
- output: Sequence[str] | None = None
Output file paths, corresponds to
-o/--output
- output_overwrite: bool = False
Should existing files be overwritten? corresponds to
-ox/--output-overwrite
- processors: Sequence[str] | None = None
Image processor URIs, corresponds to
-p/--processors
- resize: tuple[int, int] | None = None
Naive resizing value (LANCZOS), corresponds to
-r/--resize
- class dgenerate.image_process.StartingAnimationEvent(origin, total_frames: int, fps: float, frame_duration: float)[source]
Bases:
EventCommon event stream object produced by the
events()event stream of a render loop.Occurs when a sequence of images that belong to an animation are about to start being generated.
This occurs whether an animation is going to be written to disk or not.
- fps: float
FPS of the generated file.
- frame_duration: float
Frame duration of the generated file, (the time a frame is visible in milliseconds)
- total_frames: int
Number of frames written.
- class dgenerate.image_process.StartingAnimationFileEvent(origin, path: str, total_frames: int, fps: float, frame_duration: float)[source]
Bases:
EventCommon event stream object produced by the
events()event stream of a render loop.Occurs when a sequence of images that belong to an animation are about to start being written to a file.
- fps: float
FPS of the generated file.
- frame_duration: float
Frame duration of the generated file, (the time a frame is visible in milliseconds)
- path: str
File path where the animation will reside.
- total_frames: int
Number of frames written.
- class dgenerate.image_process.StartingGenerationStepEvent(origin, generation_step: int, total_steps: int)[source]
Bases:
EventCommon event stream object produced by the
events()event stream of a render loop.Occurs when a generation step is starting, a generation step may produce multiple images and or an animation.
- generation_step: int
The generation step number.
- total_steps: int
The total number of steps that are needed to complete the render loop.
- dgenerate.image_process.invoke_image_process(args: Sequence[str], render_loop: ImageProcessRenderLoop | None = None, throw: bool = False, log_error: bool = True, help_raises: bool = False, help_name: str = 'image-process', help_desc: str | None = None) int[source]
Invoke image-process using its command line arguments and return a return code.
image-process is invoked in the current process, this method does not spawn a subprocess.
- Parameters:
args – image-process command line arguments in the form of a list, see: shlex module, or sys.argv
render_loop –
ImageProcessRenderLoopinstance, if None is provided one will be created.throw – Whether to throw known exceptions or handle them.
log_error – Write ERROR diagnostics with
dgenerate.messages?help_raises –
--helpraisesImageProcessHelpException? WhenTrue, this will occur even ifthrow=Falsehelp_name – name used in the
--helpoutputhelp_desc – description used in the
--helpoutput, ifNoneis provided a default value will be used.
- Raises:
- Returns:
integer return-code, anything other than 0 is failure
- dgenerate.image_process.invoke_image_process_events(args: Sequence[str], render_loop: ImageProcessRenderLoop | None = None, throw: bool = False, log_error: bool = True, help_raises: bool = False, help_name: str = 'image-process', help_desc: str | None = None) Generator[ImageProcessExitEvent | ImageGeneratedEvent | StartingAnimationEvent | StartingAnimationFileEvent | AnimationFileFinishedEvent | ImageFileSavedEvent | AnimationFinishedEvent | StartingGenerationStepEvent | AnimationETAEvent, None, None][source]
Invoke image-process using its command line arguments and return a stream of events.
image-process is invoked in the current process, this method does not spawn a subprocess.
The exceptions mentioned here are those you may encounter upon iterating, they will not occur upon simple acquisition of the event stream iterator.
- Parameters:
args – image-process command line arguments in the form of a list, see: shlex module, or sys.argv
render_loop –
ImageProcessRenderLoopinstance, if None is provided one will be created.throw – Whether to throw known exceptions or handle them.
log_error – Write ERROR diagnostics with
dgenerate.messages?help_raises –
--helpraisesImageProcessHelpException? WhenTrue, this will occur even ifthrow=Falsehelp_name – name used in the
--helpoutputhelp_desc – description used in the
--helpoutput, ifNoneis provided a default value will be used.
- Raises:
- Returns:
InvokeImageProcessEventStream
- dgenerate.image_process.parse_args(args: Sequence[str] | None = None, help_name='image-process', help_desc=None, throw: bool = True, log_error: bool = True, help_raises: bool = False) ImageProcessArgs | None[source]
Parse and validate the arguments used for
image-process, which is a dgenerate sub-command as well as config directive.- Parameters:
args – command line arguments
help_name – program name displayed in
--helpoutput.help_desc – program description displayed in
--helpoutput.throw – throw
ImageProcessUsageErroron error? defaults toTruelog_error – Write ERROR diagnostics with
dgenerate.messages?help_raises –
--helpraisesImageProcessHelpException? WhenTrue, this will occur even ifthrow=False
- Raises:
- Returns:
parsed arguments object
dgenerate.imageprocessors module
Image processors implemented by dgenerate.
This includes many image processing tasks useful for creating diffusion input images, or for postprocessing.
- dgenerate.imageprocessors.IMAGE_PROCESSOR_CUDA_MEMORY_CONSTRAINTS = ['processor_size > (available * 0.70)']
Cache constraint expressions for when to attempt to fully clear cuda VRAM before moving an image processor on to a cuda device, syntax provided via
dgenerate.memory.cuda_memory_constraints()If any of these constraints are met, an effort is made to clear modules off the GPU which are cached for fast repeat usage but are okay to flush, prior to moving an image processor to the GPU.
The only available extra variable is:
pipeline_size(the estimated size of the image processor module that needs to enter VRAM, in bytes)
- dgenerate.imageprocessors.IMAGE_PROCESSOR_MEMORY_CONSTRAINTS = ['processor_size > (available * 0.70)']
Cache constraint expressions for when to attempt to fully clear CPU side ram before the initial loading of an image processor module into ram, syntax provided via
dgenerate.memory.memory_constraints()If any of these constraints are met, an effort is made to clear modules out of cpu side ram which are cached for fast repeat usage but are okay to flush, prior to loading an image processor model.
The only available extra variable is:
pipeline_size(the estimated size of the image processor module that needs to enter ram, in bytes)
Image processors implemented by dgenerate.
This includes many image processing tasks useful for creating diffusion input images, or for postprocessing.
- exception dgenerate.imageprocessors.ImageProcessorArgumentError[source]
Bases:
PluginArgumentError,ImageProcessorErrorRaised when an image processor receives invalid arguments.
- exception dgenerate.imageprocessors.ImageProcessorError[source]
Bases:
ExceptionGeneric image processor error base exception.
- exception dgenerate.imageprocessors.ImageProcessorHelpUsageError[source]
Bases:
ExceptionRaised on argument parse errors in
image_processor_help()
- exception dgenerate.imageprocessors.ImageProcessorImageModeError[source]
Bases:
ImageProcessorErrorRaised when an image processor cannot support a PIL images reported mode.
A mode being a mode string such as
RGB,BGR, etc.
- exception dgenerate.imageprocessors.ImageProcessorNotFoundError[source]
Bases:
PluginNotFoundError,ImageProcessorErrorRaised when a reference to an unknown image processor name is made.
- class dgenerate.imageprocessors.AnylineProcessor(gaussian_sigma: float = 2.0, intensity_threshold: int = 2, detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessoranyline, MistoLine Control Every Line image preprocessor, see: https://huggingface.co/TheMistoAI/MistoLine
This is an edge detector based on TEED.
The “gaussian-sigma” argument is the gaussian filter sigma value.
The “intensity-threshold” argument is the pixel value intensity threshold.
The “detect-resolution” argument is the resolution the image is resized to internal to the processor before detection is run on it. It should be a single dimension for example: “detect-resolution=512” or the X/Y dimensions seperated by an “x” character, like so: “detect-resolution=1024x512”. If you do not specify this argument, the detector runs on the input image at its full resolution. After processing the image will be resized to whatever you have requested dgenerate resize it to via –output-size or –resize/–align in the case of the image-process sub-command, if you have not requested any resizing the output will be resized back to the original size of the input image.
The “detect-aspect” argument determines if the image resize requested by “detect_resolution” before detection runs is aspect correct, this defaults to true.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- __init__(gaussian_sigma: float = 2.0, intensity_threshold: int = 2, detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
- Parameters:
gaussian_sigma – gaussian filter sigma value
intensity_threshold – pixel value intensity threshold
detect_resolution – the input image is resized to this dimension before being processed, providing
Noneindicates it is not to be resized. If there is no resize requested during the processing action viaresize_resolutionit will be resized back to its original size.detect_aspect – if the input image is resized by
detect_resolutionordetect_alignbefore processing, will it be an aspect correct resize?detect_align – the input image is forcefully aligned to this amount of pixels before being processed.
pre_resize – process the image before it is resized, or after? default is
False(after).kwargs – forwarded to base class
- impl_post_resize(image: Image)[source]
Post resize.
- Parameters:
image – image
- Returns:
possibly an anyline detected image, or the input image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Pre resize.
- Parameters:
image – image to process
resize_resolution – resize resolution
- Returns:
possibly an anyline detected image, or the input image
- NAMES = ['anyline']
- class dgenerate.imageprocessors.CannyEdgeDetectProcessor(lower: int = 50, upper: int = 100, aperture_size: int = 3, L2_gradient: bool = False, blur: bool = False, gray: bool = False, threshold_algo: str | None = None, sigma: float = 0.33, detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorProcess the input image with the Canny edge detection algorithm.
The “lower” argument indicates the lower threshold value for the algorithm, and the “upper” argument indicates the upper threshold. “aperture-size” is the size of Sobel kernel used for find image gradients, it must be an odd integer from 3 to 7. “L2-gradient” specifies the equation for finding gradient magnitude, if True a more accurate equation is used. See: https://docs.opencv.org/3.4/da/d22/tutorial_py_canny.html.
If “blur” is true, apply a 3x3 gaussian blur before processing. If “gray” is true, convert the image to the cv2 “GRAY” format before processing, which does not happen automatically unless you are using a “threshold_algo” value, OpenCV is capable of edge detection on colored images, however you may find better results by converting to its internal grayscale format before processing, or you may not, it depends.
If “threshold_algo” is one of (“otsu”, “triangle”, “median”) try to calculate the lower and upper threshold automatically using cv2.threshold or cv2.median in the case of “median”. “sigma” scales the range of the automatic threshold calculation done when a value for “threshold_algo” is selected. “pre-resize” is a boolean value determining if the processing should take place before or after the image is resized by dgenerate.
The “detect-resolution” argument is the resolution the image is resized to internal to the processor before detection is run on it. It should be a single dimension for example: “detect-resolution=512” or the X/Y dimensions seperated by an “x” character, like so: “detect-resolution=1024x512”. If you do not specify this argument, the detector runs on the input image at its full resolution. After processing the image will be resized to whatever you have requested dgenerate resize it to via –output-size or –resize/–align in the case of the image-process sub-command, if you have not requested any resizing the output will be resized back to the original size of the input image.
The “detect-aspect” argument determines if the image resize requested by “detect-resolution” before detection runs is aspect correct, this defaults to true.
The “detect-align” argument determines the pixel alignment of the image resize requested by “detect-resolution”, it defaults to 1 indicating no requested alignment.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- __init__(lower: int = 50, upper: int = 100, aperture_size: int = 3, L2_gradient: bool = False, blur: bool = False, gray: bool = False, threshold_algo: str | None = None, sigma: float = 0.33, detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
- Parameters:
lower – lower threshold for canny edge detection
upper – upper threshold for canny edge detection
aperture_size – aperture size, an odd integer from 3 to 7
L2_gradient – Use L2_gradient? https://docs.opencv.org/3.4/da/d22/tutorial_py_canny.html
blur – apply a 3x3 gaussian blur before processing?
gray – convert to cv2.GRAY format before processing?
threshold_algo – optional auto thresholding algorithm. One of “otsu”, “triangle”, or “median”. the lower, and upper threshold values are determined automagically from the image content if this argument is supplied a value.
sigma – scales the range of the automatic threshold calculation
detect_resolution – the input image is resized to this dimension before being processed, providing
Noneindicates it is not to be resized. If there is no resize requested during the processing action viaresize_resolutionit will be resized back to its original size.detect_aspect – if the input image is resized by
detect_resolutionordetect_alignbefore processing, will it be an aspect correct resize?detect_align – the input image is forcefully aligned to this amount of pixels before being processed.
pre_resize – process the image before it is resized, or after? default is
False(after).kwargs – forwarded to base class
- impl_post_resize(image: Image)[source]
Post resize, canny edge detection may or may not occur here depending on the boolean value of the processor argument “pre-resize”
- Parameters:
image – image to process
- Returns:
possibly a canny edge detected image, or the input image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Pre resize, canny edge detection may or may not occur here depending on the boolean value of the processor argument “pre-resize”
- Parameters:
image – image to process
resize_resolution – purely informational, is unused by this processor
- Returns:
possibly a canny edge detected image, or the input image
- to(device) CannyEdgeDetectProcessor[source]
Does nothing for this processor. :param device: the device :return: this processor
- HIDE_ARGS = ['device', 'model-offload']
- NAMES = ['canny']
- class dgenerate.imageprocessors.HEDProcessor(scribble: bool = False, safe: bool = False, detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorHED detection (holistically-nested edge detection), this is an edge detection algorithm that can produced something akin to thick lineart.
The “scribble” argument determines whether scribble mode is enabled, this produces thicker lines.
The “safe” argument enables or disables numerically safe / more precise stepping.
The “detect-resolution” argument is the resolution the image is resized to internal to the processor before detection is run on it. It should be a single dimension for example: “detect-resolution=512” or the X/Y dimensions seperated by an “x” character, like so: “detect-resolution=1024x512”. If you do not specify this argument, the detector runs on the input image at its full resolution. After processing the image will be resized to whatever you have requested dgenerate resize it to via –output-size or –resize/–align in the case of the image-process sub-command, if you have not requested any resizing the output will be resized back to the original size of the input image.
The “detect-aspect” argument determines if the image resize requested by “detect_resolution” before detection runs is aspect correct, this defaults to true.
The “detect-align” argument determines the pixel alignment of the image resize requested by “detect_resolution”, it defaults to 1 indicating no requested alignment.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- __init__(scribble: bool = False, safe: bool = False, detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
- Parameters:
scribble – determines whether or not scribble mode is enabled, this produces thicker lines
safe – enables numerically safe / more precise stepping
detect_resolution – the input image is resized to this dimension before being processed, providing
Noneindicates it is not to be resized. If there is no resize requested during the processing action viaresize_resolutionit will be resized back to its original size.detect_aspect – if the input image is resized by
detect_resolutionordetect_alignbefore processing, will it be an aspect correct resize?detect_align – the input image is forcefully aligned to this amount of pixels before being processed.
pre_resize – process the image before it is resized, or after? default is
False(after).kwargs – forwarded to base class
- impl_post_resize(image: Image)[source]
Post resize.
- Parameters:
image – image
- Returns:
possibly an HED depth detected image, or the input image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Pre resize.
- Parameters:
image – image to process
resize_resolution – resize resolution
- Returns:
possibly an HED depth detected image, or the input image
- NAMES = ['hed']
- class dgenerate.imageprocessors.ImageProcessor(loaded_by_name: str, device: str | None = None, output_file: str | None = None, output_overwrite: bool = False, model_offload: bool = False, **kwargs)[source]
Bases:
PluginAbstract base class for image processor implementations.
- __init__(loaded_by_name: str, device: str | None = None, output_file: str | None = None, output_overwrite: bool = False, model_offload: bool = False, **kwargs)[source]
- Parameters:
loaded_by_name – The name the processor was loaded by
device – the device the processor will run on, for example: cpu, cuda, cuda:1. Specifying
Nonecauses the device to default to cpu.output_file – output a debug image to this path
output_overwrite – can the debug image output path be overwritten?
model_offload – if
True, any torch modules that the processor has registered are offloaded to the CPU immediately after processing an imagekwargs – child class forwarded arguments
- get_alignment() int | None[source]
Get required input image alignment, which will be forcefully applied.
If this function returns
None, specific alignment is not required and will never be forced.- Returns:
integer or
None
- impl_post_resize(image: Image)[source]
Implementation of post_resize that does nothing. Inheritor must implement.
This method should not be invoked directly, use the class method
ImageProcessor.call_post_resize()to invoke it.- Parameters:
image – image to process
- Returns:
the processed image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Implementation of pre_resize that does nothing. Inheritor must implement.
This method should not be invoked directly, use the class method
ImageProcessor.call_pre_resize()to invoke it.- Parameters:
image – image to process
resize_resolution – image will be resized to this resolution after this process is complete. If None is passed no resize is going to occur. It is not the duty of the inheritor to resize the image, in fact it should NEVER be resized.
- Returns:
the processed image
- post_resize(image: Image) Image[source]
Invoke a processors
ImageProcessor.impl_post_resize()method.Implements important behaviors depending on if the image was modified.
This is the only appropriate way to invoke a processor manually.
The original image will be closed if the implementation returns a new image instead of modifying it in place, you should not count on the original image being open and usable once this function completes though it is safe to use the input image in a
withcontext, if you need to retain a copy, pass a copy.- Raises:
dgenerate.OutOfMemoryError – if the execution device runs out of memory
dgenerate.ImageProcessorImageModeError – if a passed image has an invalid format
- Parameters:
self –
ImageProcessorimplementation instanceimage – the image to pass
- Returns:
processed image, may be the same image or a copy.
- pre_resize(image: Image, resize_resolution: tuple[int, int] | None = None) Image[source]
Invoke a processors
ImageProcessor.impl_pre_resize()method.Implements important behaviors depending on if the image was modified.
This is the only appropriate way to invoke a processor manually.
The original image will be closed if the implementation returns a new image instead of modifying it in place, you should not count on the original image being open and usable once this function completes though it is safe to use the input image in a
withcontext, if you need to retain a copy, pass a copy.- Raises:
dgenerate.OutOfMemoryError – if the execution device runs out of memory
dgenerate.ImageProcessorImageModeError – if a passed image has an invalid format
- Parameters:
self –
ImageProcessorimplementation instanceimage – the image to pass
resize_resolution – the size that the image is going to be resized to after this step, or None if it is not being resized.
- Returns:
processed image, may be the same image or a copy.
- process(image: Image, resize_resolution: tuple[int, int] | None = None, aspect_correct: bool = True, align: int | None = 8)[source]
Preform image processing on an image, including the requested resizing step.
Invokes the image processor pre and post resizing with appropriate arguments and correct resource management.
The original image will be closed if the implementation returns a new image instead of modifying it in place, you should not count on the original image being open and usable once this function completes though it is safe to use the input image in a
withcontext, if you need to retain a copy, pass a copy.- Raises:
dgenerate.OutOfMemoryError – if the execution device runs out of memory
dgenerate.ImageProcessorImageModeError – if a passed image has an invalid format
- Parameters:
image – image to process
resize_resolution – image will be resized to this dimension by this method.
aspect_correct – Should the resize operation be aspect correct?
align – Align by this amount of pixels, if the input image is not aligned to this amount of pixels, it will be aligned by resizing. Passing
Noneor1disables alignment.
- Returns:
the processed image
- register_module(module)[source]
Register
torch.nn.Moduleobjects.These will be brought on to the cpu during finalization.
All of these modules can be cast to a specific device with
ImageProcessor.to- Parameters:
module – the module
- set_size_estimate(size_bytes: int)[source]
Set the estimated size of this model in bytes for memory management heuristics, this is intended to be used by implementors of the
ImageProcessorplugin class.For the best memory optimization, this value should be set very shortly before the model even enters CPU side ram, IE: before it is loaded at all.
- Raises:
ValueError – if
size_bytesis less than zero.- Parameters:
size_bytes – the size in bytes
- to(device: device | str) ImageProcessor[source]
Move all
torch.nn.Modulemodules registered to this image processor to a specific device.- Raises:
dgenerate.OutOfMemoryError – if there is not enough memory on the specified device
- Parameters:
device – The device string, or torch device object
- Returns:
the image processor itself
- property device: str
The rendering device requested for this processor.
Torch modules associated with the processor will not be on this device until the processor is used.
- Returns:
device string, for example “cuda”, “cuda:N”, or “cpu”
- property image_modes: list[str]
Returns a list of PIL image modes that this processor can handle.
This may be overridden by implementers
- Returns:
['RGB']
- property modules_device: device
The rendering device that this processors modules currently exist on.
This will change with calls to
ImageProcessor.to()and possibly when the processor is used.- Returns:
torch.device, usingstr()on this object will yield a device string such as “cuda”, “cuda:N”, or “cpu”
- property size_estimate: int
Get the estimated size of this model in bytes.
- Returns:
size bytes
- class dgenerate.imageprocessors.ImageProcessorChain(image_processors: Iterable[ImageProcessor] | None = None)[source]
Bases:
ImageProcessorImplements chainable image processors.
Chains processing steps together in a sequence.
- __init__(image_processors: Iterable[ImageProcessor] | None = None)[source]
- Parameters:
image_processors – optional initial image processors to fill the chain, accepts an iterable
- add_processor(image_processor: ImageProcessor)[source]
Add a imageprocessor implementation to the chain.
- Parameters:
image_processor –
dgenerate.imageprocessors.imageprocessor.ImageProcessor
- impl_post_resize(image: Image)[source]
Invoke post_resize on all image processors in this image processor chain in turn.
Every subsequent invocation receives the last processed image as its argument.
This method should not be invoked directly, use the class method
dgenerate.imageprocessors.imageprocessor.ImageProcessor.post_resize()to invoke it.- Parameters:
image – initial image to process
- Returns:
the processed image, possibly affected by every imageprocessor in the chain
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Invoke pre_resize on all image processors in this imageprocessor chain in turn.
Every subsequent invocation receives the last processed image as its argument.
This method should not be invoked directly, use the class method
dgenerate.imageprocessors.imageprocessor.ImageProcessor.pre_resize()to invoke it.- Parameters:
image – initial image to process
resize_resolution – the size which the image will be resized to after this step, this is only information for the image processors and the image will not be resized by this method. Image processors should never resize images as it is the responsibility of dgenerate to do that for the user.
- Returns:
the processed image, possibly affected by every image processor in the chain
- to(device: device | str) ImageProcessorChain[source]
Move all
torch.nn.Modulemodules registered to this image processor to a specific device.- Raises:
dgenerate.OutOfMemoryError – if there is not enough memory on the specified device
- Parameters:
device – The device string, or torch device object
- Returns:
the image processor itself
- HIDDEN = True
- class dgenerate.imageprocessors.ImageProcessorLoader[source]
Bases:
PluginLoaderLoads
dgenerate.imageprocessor.ImageProcessorplugins.- load(uri: str | Iterable[str], device: str = 'cpu', **kwargs) ImageProcessor | ImageProcessorChain | None[source]
Load an image processor or multiple image processors. They are loaded by URI, which is their name and any module arguments, for example:
canny;lower=50;upper=100Specifying multiple processors with a list will create an image processor chain object.
- Raises:
RuntimeError – if more than one class was found using the provided name mentioned in the URI.
ImageProcessorNotFoundError – if the name mentioned in the URI could not be found.
ImageProcessorArgumentError – if the URI contained invalid arguments.
- Parameters:
uri – Processor URI or list of URIs
device – Request a specific rendering device, default is CPU
kwargs – Default argument values, will be overriden by arguments specified in the URI
- Returns:
dgenerate.imageprocessors.ImageProcessorordgenerate.imageprocessors.ImageProcessorChain
- class dgenerate.imageprocessors.ImageProcessorMixin(image_processor: ImageProcessor | None = None, *args, **kwargs)[source]
Bases:
objectMixin functionality for objects that can do image processing such as implementors of
dgenerate.mediainput.AnimationReaderThis object can also be instantiated and used alone.
This object implements resizing functionality identical to an image processor in the absense or disabled state of the image processor.
Which is used for among other things, frame slicing with an image processor involved.
- __init__(image_processor: ImageProcessor | None = None, *args, **kwargs)[source]
- Parameters:
processor – the processor implementation that will be doing the image processing.
args – mixin forwarded args
kwargs – mixin forwarded kwargs
- process_image(image: Image, resize_resolution: tuple[int, int] | None = None, aspect_correct: bool = True, align: int | None = 8)[source]
Preform image processing on an image, including the requested resizing step.
Invokes the assigned image processor on an image.
If no processor is assigned or the processor is disabled, only necessary resizing will be preformed based on the given arguments.
The original image will be closed if the processor returns a new image instead of modifying it in place, you should not count on the original image being open and usable once this function completes with a processor assigned and the processor enabled, though it is safe to use the input image in a
withcontext, if you need to retain a copy, pass a copy.- Parameters:
image – image to process
resize_resolution – image will be resized to this dimension by this method.
aspect_correct – Should the resize operation be aspect correct?
align – Align by this amount of pixels, if the input image is not aligned to this amount of pixels, it will be aligned by resizing. Passing
Noneor1disables alignment.
- Returns:
the processed image, processed by the processor assigned in the constructor.
- image_processor: ImageProcessor | None = None
Current image processor.
Images will still be resized as needed/requested if this is not assigned.
- image_processor_enabled: bool
Enable or disable image processing.
Images will still be resized as needed/requested with this disabled.
- class dgenerate.imageprocessors.LeresDepthProcessor(threshold_near: int = 0, threshold_far: int = 0, boost: bool = False, detect_resolution: str | None = None, detect_aspect: bool = True, pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorLeReS depth detector.
The “threshold-near” argument is the near threshold, think the low threshold of canny.
The “threshold-far” argument is the far threshold, think the high threshold of canny.
The “boost” argument determines if monocular depth boost is used.
The “detect-resolution” argument is the resolution the image is resized to internal to the processor before detection is run on it. It should be a single dimension for example: “detect-resolution=512” or the X/Y dimensions seperated by an “x” character, like so: “detect-resolution=1024x512”. If you do not specify this argument, the detector runs on the input image at its full resolution. After processing the image will be resized to whatever you have requested dgenerate resize it to via –output-size or –resize/–align in the case of the image-process sub-command, if you have not requested any resizing the output will be resized back to the original size of the input image.
The “detect-aspect” argument determines if the image resize requested by “detect-resolution” before detection runs is aspect correct, this defaults to true.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- __init__(threshold_near: int = 0, threshold_far: int = 0, boost: bool = False, detect_resolution: str | None = None, detect_aspect: bool = True, pre_resize: bool = False, **kwargs)[source]
- Parameters:
threshold_near – argument is the near threshold, think the low threshold of canny
threshold_far – argument is the far threshold, think the high threshold of canny
boost – argument determines if monocular depth boost is used
detect_resolution – the input image is resized to this dimension before being processed, providing
Noneindicates it is not to be resized. If there is no resize requested during the processing action viaresize_resolutionit will be resized back to its original size.detect_aspect – if the input image is resized by
detect_resolutionordetect_alignbefore processing, will it be an aspect correct resize?detect_align – the input image is forcefully aligned to this amount of pixels before being processed.
pre_resize – process the image before it is resized, or after? default is
False(after).kwargs – forwarded to base class
- get_alignment()[source]
Get required input image alignment, which will be forcefully applied.
If this function returns
None, specific alignment is not required and will never be forced.- Returns:
integer or
None
- impl_post_resize(image: Image)[source]
Post resize.
- Parameters:
image – image
- Returns:
possibly a LeReS depth detected image, or the input image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Pre resize.
- Parameters:
image – image to process
resize_resolution – resize resolution
- Returns:
possibly a LeReS depth detected image, or the input image
- NAMES = ['leres']
- class dgenerate.imageprocessors.LetterboxProcessor(box_size: str, box_is_padding: bool = False, box_color: str | None = None, inner_size: str | None = None, aspect_correct: bool = True, pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorLetterbox an image.
The “box-size” argument is the size of the outer letterbox
The “box-is-padding” argument can be used to indicate that “box-size” should be interpreted as padding.
The “box-color” argument specifies the color to use for the letter box background, the default is black. This should be specified as a HEX color code.
The “inner-size” argument specifies the size of the inner image.
The “aspect-correct” argument can be used to determine if the aspect ratio of the inner image is maintained or not.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- __init__(box_size: str, box_is_padding: bool = False, box_color: str | None = None, inner_size: str | None = None, aspect_correct: bool = True, pre_resize: bool = False, **kwargs)[source]
- Parameters:
box_size – Size of the outer letterbox
box_is_padding – The
letterbox_sizeargument should be interpreted as padding?box_color – What color to use for the letter box background, the default is black. This should be specified in as a HEX color code.
inner_size – The size of the inner image
aspect_correct – Should the size of the inner image be aspect correct?
- impl_post_resize(image: Image)[source]
Letterbox operation is preformed by this method if
pre_resizeconstructor argument wasFalse.- Parameters:
image – image to process
- Returns:
the letterboxed image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Letterbox operation is preformed by this method if
pre_resizeconstructor argument wasTrue.- Parameters:
image – image to process
resize_resolution – purely informational, is unused by this imageprocessor
- Returns:
the letterboxed image
- to(device) LetterboxProcessor[source]
Does nothing for this processor. :param device: the device :return: this processor
- HIDE_ARGS = ['device', 'model-offload']
- NAMES = ['letterbox']
- class dgenerate.imageprocessors.LineArtAnimeProcessor(detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorAnime line art generator, generate anime line art from an image.
The “detect-resolution” argument is the resolution the image is resized to internal to the processor before detection is run on it. It should be a single dimension for example: “detect-resolution=512” or the X/Y dimensions seperated by an “x” character, like so: “detect-resolution=1024x512”. If you do not specify this argument, the detector runs on the input image at its full resolution. After processing the image will be resized to whatever you have requested dgenerate resize it to via –output-size or –resize/–align in the case of the image-process sub-command, if you have not requested any resizing the output will be resized back to the original size of the input image.
The “detect-aspect” argument determines if the image resize requested by “detect-resolution” before detection runs is aspect correct, this defaults to true.
The “detect-align” argument determines the pixel alignment of the image resize requested by “detect-resolution”, it defaults to 1 indicating no requested alignment.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- __init__(detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
- Parameters:
detect_resolution – the input image is resized to this dimension before being processed, providing
Noneindicates it is not to be resized. If there is no resize requested during the processing action viaresize_resolutionit will be resized back to its original size.detect_aspect – if the input image is resized by
detect_resolutionordetect_alignbefore processing, will it be an aspect correct resize?detect_align – the input image is forcefully aligned to this amount of pixels before being processed.
pre_resize – process the image before it is resized, or after? default is
False(after).kwargs – forwarded to base class
- impl_post_resize(image: Image)[source]
Post resize.
- Parameters:
image – image
- Returns:
possibly a lineart image, or the input image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Pre resize.
- Parameters:
image – image to process
resize_resolution – resize resolution
- Returns:
possibly a lineart image, or the input image
- NAMES = ['lineart-anime']
- class dgenerate.imageprocessors.LineArtProcessor(course: bool = False, detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorLine art generator, generate line art from an image.
The “course” argument determines whether to use the course model or the normal model.
The “detect-resolution” argument is the resolution the image is resized to internal to the processor before detection is run on it. It should be a single dimension for example: “detect-resolution=512” or the X/Y dimensions seperated by an “x” character, like so: “detect-resolution=1024x512”. If you do not specify this argument, the detector runs on the input image at its full resolution. After processing the image will be resized to whatever you have requested dgenerate resize it to via –output-size or –resize/–align in the case of the image-process sub-command, if you have not requested any resizing the output will be resized back to the original size of the input image.
The “detect-aspect” argument determines if the image resize requested by “detect-resolution” before detection runs is aspect correct, this defaults to true.
The “detect-align” argument determines the pixel alignment of the image resize requested by “detect-resolution”, it defaults to 1 indicating no requested alignment.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- __init__(course: bool = False, detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
- Parameters:
course – determines whether to use the course model or the normal model
detect_resolution – the input image is resized to this dimension before being processed, providing
Noneindicates it is not to be resized. If there is no resize requested during the processing action viaresize_resolutionit will be resized back to its original size.detect_aspect – if the input image is resized by
detect_resolutionordetect_alignbefore processing, will it be an aspect correct resize?detect_align – the input image is forcefully aligned to this amount of pixels before being processed.
pre_resize – process the image before it is resized, or after? default is
False(after).kwargs – forwarded to base class
- impl_post_resize(image: Image)[source]
Post resize.
- Parameters:
image – image
- Returns:
possibly a lineart image, or the input image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Pre resize.
- Parameters:
image – image to process
resize_resolution – resize resolution
- Returns:
possibly a lineart image, or the input image
- NAMES = ['lineart']
- class dgenerate.imageprocessors.LineArtStandardProcessor(gaussian_sigma: float = 6.0, intensity_threshold: int = 8, detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorStandard lineart detector, generate lineart from an image.
The “gaussian-sigma” argument is the gaussian filter sigma value.
The “intensity-threshold” argument is the pixel value intensity threshold.
The “detect-resolution” argument is the resolution the image is resized to internal to the processor before detection is run on it. It should be a single dimension for example: “detect-resolution=512” or the X/Y dimensions seperated by an “x” character, like so: “detect-resolution=1024x512”. If you do not specify this argument, the detector runs on the input image at its full resolution. After processing the image will be resized to whatever you have requested dgenerate resize it to via –output-size or –resize/–align in the case of the image-process sub-command, if you have not requested any resizing the output will be resized back to the original size of the input image.
The “detect-aspect” argument determines if the image resize requested by “detect-resolution” before detection runs is aspect correct, this defaults to true.
The “detect-align” argument determines the pixel alignment of the image resize requested by “detect-resolution”, it defaults to 1 indicating no requested alignment.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- __init__(gaussian_sigma: float = 6.0, intensity_threshold: int = 8, detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
- Parameters:
gaussian_sigma – gaussian filter sigma value
intensity_threshold – pixel value intensity threshold
detect_resolution – the input image is resized to this dimension before being processed, providing
Noneindicates it is not to be resized. If there is no resize requested during the processing action viaresize_resolutionit will be resized back to its original size.detect_aspect – if the input image is resized by
detect_resolutionordetect_alignbefore processing, will it be an aspect correct resize?detect_align – the input image is forcefully aligned to this amount of pixels before being processed.
pre_resize – process the image before it is resized, or after? default is
False(after).kwargs – forwarded to base class
- impl_post_resize(image: Image)[source]
Post resize.
- Parameters:
image – image
- Returns:
possibly a lineart image, or the input image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Pre resize.
- Parameters:
image – image to process
resize_resolution – resize resolution
- Returns:
possibly a lineart image, or the input image
- to(device) LineArtStandardProcessor[source]
Does nothing for this processor. :param device: the device :return: this processor
- HIDE_ARGS = ['device', 'model-offload']
- NAMES = ['lineart-standard']
- class dgenerate.imageprocessors.MLSDProcessor(threshold_score: float = 0.1, threshold_distance: float = 0.1, detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorMachine Learning Model for Detecting Wireframes. Wireframe edge detector, this processor overlays lines on to the edges of objects in an image.
The “threshold-score” argument is the score threshold.
The “threshold-distance” argument is the distance threshold.
The “detect-resolution” argument is the resolution the image is resized to internal to the processor before detection is run on it. It should be a single dimension for example: “detect-resolution=512” or the X/Y dimensions seperated by an “x” character, like so: “detect-resolution=1024x512”. If you do not specify this argument, the detector runs on the input image at its full resolution. After processing the image will be resized to whatever you have requested dgenerate resize it to via –output-size or –resize/–align in the case of the image-process sub-command, if you have not requested any resizing the output will be resized back to the original size of the input image.
The “detect-aspect” argument determines if the image resize requested by “detect-resolution” before detection runs is aspect correct, this defaults to true.
The “detect-align” argument determines the pixel alignment of the image resize requested by “detect-resolution”, it defaults to 1 indicating no requested alignment.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- __init__(threshold_score: float = 0.1, threshold_distance: float = 0.1, detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
- Parameters:
threshold_score – score threshold
threshold_distance – distance threshold
detect_resolution – the input image is resized to this dimension before being processed, providing
Noneindicates it is not to be resized. If there is no resize requested during the processing action viaresize_resolutionit will be resized back to its original size.detect_aspect – if the input image is resized by
detect_resolutionordetect_alignbefore processing, will it be an aspect correct resize?detect_align – the input image is forcefully aligned to this amount of pixels before being processed.
pre_resize – process the image before it is resized, or after? default is
False(after).kwargs – forwarded to base class
- impl_post_resize(image: Image)[source]
Post resize.
- Parameters:
image – image
- Returns:
possibly a wireframe detected image, or the input image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Pre resize.
- Parameters:
image – image to process
resize_resolution – resize resolution
- Returns:
possibly a wireframe detected image, or the input image
- NAMES = ['mlsd']
- class dgenerate.imageprocessors.MidasDepthProcessor(normals: bool = False, alpha: float = 6.283185307179586, background_threshold: float = 0.1, detect_resolution: str | None = None, detect_aspect: bool = True, pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorMiDaS depth detector and normal map generation.
The “normals” argument determines if this processor produces a normal map or a depth image.
The “alpha” argument is related to normal map generation.
The “background_threshold” argument is related to normal map generation.
The “detect-resolution” argument is the resolution the image is resized to internal to the processor before detection is run on it. It should be a single dimension for example: “detect-resolution=512” or the X/Y dimensions seperated by an “x” character, like so: “detect-resolution=1024x512”. If you do not specify this argument, the detector runs on the input image at its full resolution. After processing the image will be resized to whatever you have requested dgenerate resize it to via –output-size or –resize/–align in the case of the image-process sub-command, if you have not requested any resizing the output will be resized back to the original size of the input image.
The “detect-aspect” argument determines if the image resize requested by “detect_resolution” before detection runs is aspect correct, this defaults to true.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- __init__(normals: bool = False, alpha: float = 6.283185307179586, background_threshold: float = 0.1, detect_resolution: str | None = None, detect_aspect: bool = True, pre_resize: bool = False, **kwargs)[source]
- Parameters:
normals – Return a generated normals image instead of a depth image?
detect_resolution – the input image is resized to this dimension before being processed, providing
Noneindicates it is not to be resized. If there is no resize requested during the processing action viaresize_resolutionit will be resized back to its original size.detect_aspect – if the input image is resized by
detect_resolutionordetect_alignbefore processing, will it be an aspect correct resize?pre_resize – process the image before it is resized, or after? default is
False(after).kwargs – forwarded to base class
- get_alignment()[source]
Get required input image alignment, which will be forcefully applied.
If this function returns
None, specific alignment is not required and will never be forced.- Returns:
integer or
None
- impl_post_resize(image: Image)[source]
Post resize.
- Parameters:
image – image
- Returns:
possibly a MiDaS depth detected image, or the input image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Pre resize.
- Parameters:
image – image to process
resize_resolution – resize resolution
- Returns:
possibly a MiDaS depth detected image, or the input image
- NAMES = ['midas']
- class dgenerate.imageprocessors.MirrorFlipProcessor(pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorImplements the “mirror” and “flip” PIL.ImageOps operations as an image imageprocessor
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- impl_post_resize(image: Image)[source]
Mirrors or flips the image depending on what name was used to invoke this imageprocessor implementation.
Executes if
pre_resizeconstructor argument wasFalse.- Parameters:
image – image to process
- Returns:
the mirrored or flipped image.
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Mirrors or flips the image depending on what name was used to invoke this imageprocessor implementation.
Executes if
pre_resizeconstructor argument wasTrue.- Parameters:
image – image to process
resize_resolution – purely informational, is unused by this imageprocessor
- Returns:
the mirrored or flipped image.
- to(device) MirrorFlipProcessor[source]
Does nothing for this processor. :param device: the device :return: this processor
- HIDE_ARGS = ['device', 'model-offload']
- NAMES = ['mirror', 'flip']
- class dgenerate.imageprocessors.NormalBaeProcessor(detect_resolution: str | None = None, detect_aspect: bool = True, pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorNormal Bae Detector, generate a normal map from an image.
The “detect-resolution” argument is the resolution the image is resized to internal to the processor before detection is run on it. It should be a single dimension for example: “detect-resolution=512” or the X/Y dimensions seperated by an “x” character, like so: “detect-resolution=1024x512”. If you do not specify this argument, the detector runs on the input image at its full resolution. After processing the image will be resized to whatever you have requested dgenerate resize it to via –output-size or –resize/–align in the case of the image-process sub-command, if you have not requested any resizing the output will be resized back to the original size of the input image.
The “detect-aspect” argument determines if the image resize requested by “detect-resolution” before detection runs is aspect correct, this defaults to true.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- __init__(detect_resolution: str | None = None, detect_aspect: bool = True, pre_resize: bool = False, **kwargs)[source]
- Parameters:
detect_resolution – the input image is resized to this dimension before being processed, providing
Noneindicates it is not to be resized. If there is no resize requested during the processing action viaresize_resolutionit will be resized back to its original size.detect_aspect – if the input image is resized by
detect_resolutionordetect_alignbefore processing, will it be an aspect correct resize?pre_resize – process the image before it is resized, or after? default is
False(after).kwargs – forwarded to base class
- impl_post_resize(image: Image)[source]
Post resize.
- Parameters:
image – image
- Returns:
possibly a normal map image, or the input image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Pre resize.
- Parameters:
image – image to process
resize_resolution – resize resolution
- Returns:
possibly a normal map image, or the input image
- NAMES = ['normal-bae']
- class dgenerate.imageprocessors.OpenPoseProcessor(include_body: bool = True, include_hand: bool = False, include_face: bool = False, detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorGenerate an OpenPose rigging from the input image (of a human/humanoid) for use with a ControlNet.
“include-body” is a boolean value indicating if a body rigging should be generated.
“include-hand” is a boolean value indicating if a detailed hand/finger rigging should be generated.
“include-face” is a boolean value indicating if a detailed face rigging should be generated.
The “detect-resolution” argument is the resolution the image is resized to internal to the processor before detection is run on it. It should be a single dimension for example: “detect-resolution=512” or the X/Y dimensions seperated by an “x” character, like so: “detect-resolution=1024x512”. If you do not specify this argument, the detector runs on the input image at its full resolution. After processing the image will be resized to whatever you have requested dgenerate resize it to via –output-size or –resize/–align in the case of the image-process sub-command, if you have not requested any resizing the output will be resized back to the original size of the input image.
The “detect-aspect” argument determines if the image resize requested by “detect-resolution” before detection runs is aspect correct, this defaults to true.
The “detect-align” argument determines the pixel alignment of the image resize requested by “detect-resolution”, it defaults to 1 indicating no requested alignment.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- __init__(include_body: bool = True, include_hand: bool = False, include_face: bool = False, detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
- Parameters:
include_body – generate a body rig?
include_hand – include detailed hand rigging?
include_face – include detailed face rigging?
pre_resize – process the image before it is resized, or after? default is after (False)
detect_resolution – the input image is resized to this dimension before being processed, providing
Noneindicates it is not to be resized. If there is no resize requested during the processing action viaresize_resolutionit will be resized back to its original size.detect_aspect – if the input image is resized by
detect_resolutionordetect_alignbefore processing, will it be an aspect correct resize?detect_align – the input image is forcefully aligned to this amount of pixels before being processed.
pre_resize – process the image before it is resized, or after? default is
False(after).kwargs – forwarded to base class
- impl_post_resize(image: Image)[source]
Post resize, OpenPose rig generation may or may not occur here depending on the boolean value of the processor argument “pre-resize”
- Parameters:
image – image to process
- Returns:
possibly an OpenPose rig image, or the input image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Pre resize, OpenPose rig generation may or may not occur here depending on the boolean value of the processor argument “pre-resize”
- Parameters:
image – image to process
resize_resolution – purely informational, is unused by this processor
- Returns:
possibly an OpenPose rig image, or the input image
- NAMES = ['openpose']
- class dgenerate.imageprocessors.PidiNetProcessor(apply_filter: bool = False, safe: bool = False, detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorPidiNet (Pixel Difference Networks for Efficient Edge Detection) edge detector.
The “apply-filter” argument enables possibly crisper edges / less noise.
The “safe” argument enables or disables numerically safe / more precise stepping.
The “detect-resolution” argument is the resolution the image is resized to internal to the processor before detection is run on it. It should be a single dimension for example: “detect-resolution=512” or the X/Y dimensions seperated by an “x” character, like so: “detect-resolution=1024x512”. If you do not specify this argument, the detector runs on the input image at its full resolution. After processing the image will be resized to whatever you have requested dgenerate resize it to via –output-size or –resize/–align in the case of the image-process sub-command, if you have not requested any resizing the output will be resized back to the original size of the input image.
The “detect-aspect” argument determines if the image resize requested by “detect-resolution” before detection runs is aspect correct, this defaults to true.
The “detect-align” argument determines the pixel alignment of the image resize requested by “detect-resolution”, it defaults to 1 indicating no requested alignment.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- __init__(apply_filter: bool = False, safe: bool = False, detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
- Parameters:
apply_filter – enables possibly crisper edges / less noise
safe – enables numerically safe / more precise stepping
detect_resolution – the input image is resized to this dimension before being processed, providing
Noneindicates it is not to be resized. If there is no resize requested during the processing action viaresize_resolutionit will be resized back to its original size.detect_aspect – if the input image is resized by
detect_resolutionordetect_alignbefore processing, will it be an aspect correct resize?detect_align – the input image is forcefully aligned to this amount of pixels before being processed.
pre_resize – process the image before it is resized, or after? default is
False(after).kwargs – forwarded to base class
- impl_post_resize(image: Image)[source]
Post resize.
- Parameters:
image – image
- Returns:
possibly an edge detected image, or the input image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Pre resize.
- Parameters:
image – image to process
resize_resolution – resize resolution
- Returns:
possibly an edge detected image, or the input image
- NAMES = ['pidi']
- class dgenerate.imageprocessors.PosterizeProcessor(bits: int, pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorPosterize the input image with PIL.ImageOps.posterize.
Accepts the argument ‘bits’, an integer value from 1 to 8.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- __init__(bits: int, pre_resize: bool = False, **kwargs)[source]
- Parameters:
bits – required argument, integer value from 1 to 8
kwargs – forwarded to base class
- impl_post_resize(image: Image)[source]
Posterize operation is preformed by this method if
pre_resizeconstructor argument wasFalse.- Parameters:
image – image to process
- Returns:
the posterized image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Posterize operation is preformed by this method if
pre_resizeconstructor argument wasTrue.- Parameters:
image – image to process
resize_resolution – purely informational, is unused by this imageprocessor
- Returns:
the posterized image
- to(device) PosterizeProcessor[source]
Does nothing for this processor. :param device: the device :return: this processor
- HIDE_ARGS = ['device', 'model-offload']
- NAMES = ['posterize']
- class dgenerate.imageprocessors.SegmentAnythingProcessor(detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorSegment Anything Model, this processor attempts to creates cutouts for every distinct objects in an image.
The “detect-resolution” argument is the resolution the image is resized to internal to the processor before detection is run on it. It should be a single dimension for example: “detect-resolution=512” or the X/Y dimensions seperated by an “x” character, like so: “detect-resolution=1024x512”. If you do not specify this argument, the detector runs on the input image at its full resolution. After processing the image will be resized to whatever you have requested dgenerate resize it to via –output-size or –resize/–align in the case of the image-process sub-command, if you have not requested any resizing the output will be resized back to the original size of the input image.
The “detect-aspect” argument determines if the image resize requested by “detect-resolution” before detection runs is aspect correct, this defaults to true.
The “detect-align” argument determines the pixel alignment of the image resize requested by “detect-resolution”, it defaults to 1 indicating no requested alignment.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- __init__(detect_resolution: str | None = None, detect_aspect: bool = True, detect_align: int = 1, pre_resize: bool = False, **kwargs)[source]
- Parameters:
detect_resolution – the input image is resized to this dimension before being processed, providing
Noneindicates it is not to be resized. If there is no resize requested during the processing action viaresize_resolutionit will be resized back to its original size.detect_aspect – if the input image is resized by
detect_resolutionordetect_alignbefore processing, will it be an aspect correct resize?detect_align – the input image is forcefully aligned to this amount of pixels before being processed.
pre_resize – process the image before it is resized, or after? default is
False(after).kwargs – forwarded to base class
- impl_post_resize(image: Image)[source]
Post resize.
- Parameters:
image – image
- Returns:
possibly a segment-anything image, or the input image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Pre resize.
- Parameters:
image – image to process
resize_resolution – resize resolution
- Returns:
possibly a segment-anything image, or the input image
- NAMES = ['sam']
- class dgenerate.imageprocessors.SimpleColorProcessor(pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorImplements the “grayscale” and “invert” PIL.ImageOps operations as an image imageprocessor.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- impl_post_resize(image: Image)[source]
Invert or grayscale the image depending on which name was used to invoke this imageprocessor.
Executes if
pre_resizeconstructor argument wasFalse.- Parameters:
image – image to process
- Returns:
the inverted or grayscale image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Invert or grayscale the image depending on which name was used to invoke this imageprocessor.
Executes if
pre_resizeconstructor argument wasTrue.- Parameters:
image – image to process
resize_resolution – purely informational, is unused by this imageprocessor
- Returns:
the inverted or grayscale image
- to(device) SimpleColorProcessor[source]
Does nothing for this processor. :param device: the device :return: this processor
- HIDE_ARGS = ['device', 'model-offload']
- NAMES = ['grayscale', 'invert']
- class dgenerate.imageprocessors.SolarizeProcessor(threshold: int = 128, pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorSolarize the input image with PIL.ImageOps.solarize.
Accepts the argument “threshold” which is an integer value from 0 to 255.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- __init__(threshold: int = 128, pre_resize: bool = False, **kwargs)[source]
- Parameters:
threshold – integer value from 0 to 255, default is 128
kwargs – forwarded to base class
- impl_post_resize(image: Image)[source]
Solarize operation is preformed by this method if
pre_resizeconstructor argument wasFalse.- Parameters:
image – image to process
- Returns:
the solarized image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Solarize operation is preformed by this method if
pre_resizeconstructor argument wasTrue.- Parameters:
image – image to process
resize_resolution – purely informational, is unused by this imageprocessor
- Returns:
the solarized image
- to(device) SolarizeProcessor[source]
Does nothing for this processor. :param device: the device :return: this processor
- HIDE_ARGS = ['device', 'model-offload']
- NAMES = ['solarize']
- class dgenerate.imageprocessors.TEEDProcessor(safe: bool = True, detect_resolution: str | None = None, detect_aspect: bool = True, pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorteed, a (tiny efficient edge detector).
The “safe” argument enables or disables numerically safe / more precise stepping.
The “detect-resolution” argument is the resolution the image is resized to internal to the processor before detection is run on it. It should be a single dimension for example: “detect-resolution=512” or the X/Y dimensions seperated by an “x” character, like so: “detect-resolution=1024x512”. If you do not specify this argument, the detector runs on the input image at its full resolution. After processing the image will be resized to whatever you have requested dgenerate resize it to via –output-size or –resize/–align in the case of the image-process sub-command, if you have not requested any resizing the output will be resized back to the original size of the input image.
The “detect-aspect” argument determines if the image resize requested by “detect_resolution” before detection runs is aspect correct, this defaults to true.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- __init__(safe: bool = True, detect_resolution: str | None = None, detect_aspect: bool = True, pre_resize: bool = False, **kwargs)[source]
- Parameters:
safe – enables or disables numerically safe / more precise stepping
detect_resolution – the input image is resized to this dimension before being processed, providing
Noneindicates it is not to be resized. If there is no resize requested during the processing action viaresize_resolutionit will be resized back to its original size.detect_aspect – if the input image is resized by
detect_resolutionordetect_alignbefore processing, will it be an aspect correct resize?pre_resize – process the image before it is resized, or after? default is
False(after).kwargs – forwarded to base class
- impl_post_resize(image: Image)[source]
Post resize.
- Parameters:
image – image
- Returns:
possibly a teed edge detected image, or the input image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Pre resize.
- Parameters:
image – image to process
resize_resolution – resize resolution
- Returns:
possibly a teed edge detected image, or the input image
- NAMES = ['teed']
- class dgenerate.imageprocessors.UpscalerProcessor(model: str, tile: int | str = 512, overlap: int = 32, force_tiling: bool = False, dtype: str = 'float32', pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorImplements tiled upscaling with chaiNNer compatible upscaler models.
The “model” argument should be a path to a chaiNNer compatible upscaler model on disk, such as a model downloaded from https://openmodeldb.info/, or an HTTP/HTTPS URL that points to a raw model file.
For example: “upscaler;model=https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth”
Downloaded models are cached in the dgenerate web cache on disk until the cache expiry time for the file is met.
The “tile” argument can be used to specify the tile size for tiled upscaling, it must be divisible by 2, and defaults to 512. Specifying ‘auto’ indicates that this value should be calculated based off available GPU memory if applicable. Specifying 0 disables tiling entirely.
The “overlap” argument can be used to specify the overlap amount of each tile in pixels, it must be greater than or equal to 0, and defaults to 32.
The “force-tiling” argument can be used to force external image tiling for upscaler model architectures which discourage the use of external tiling (SCUNEt and MixDehazeNet currently), this may mean that the model needs information about the whole image to achieve a good result. External tiling breaks up the image into tiles before feeding it to the model and reassembles the images output by the model, this is not the default behavior when a model specifies that tiling is discouraged, tiling is only on by default for models where external tiling is fully supported. Only use this if you run into memory issues with models that discourage external tiling, in the case that the model discourages its use, using it may result in substandard image output.
The “dtype” argument can be used to specify the datatype to use to for the model in memory, it can be either “float32” or “float16”. Using “float16” will result in a smaller memory footprint if supported.
The “pre-resize” argument is a boolean value determining if the processing should take place before or after the image is resized by dgenerate.
Example: “upscaler;model=my-model.pth;tile=256;overlap=16”
- __init__(model: str, tile: int | str = 512, overlap: int = 32, force_tiling: bool = False, dtype: str = 'float32', pre_resize: bool = False, **kwargs)[source]
- Parameters:
model – chaiNNer compatible upscaler model on disk, or at a URL
tile – specifies the tile size for tiled upscaling, it must be divisible by 2, and defaults to 512. Specifying ‘auto’ indicates that this value should be calculated based off available GPU memory if applicable. Specifying 0 disable tiling entirely.
overlap – the overlap amount of each tile in pixels, it must be greater than or equal to 0, and defaults to 32.
dtype – the datatype to use to for the model in memory, it may be either “float32” or “float16”. using float16 will result in a smaller memory footprint if supported.
force_tiling – Force external image tiling for model architectures that discourage it.
pre_resize – process the image before it is resized, or after? default is
False(after).kwargs – forwarded to base class
- impl_post_resize(image: Image)[source]
Implementation of post_resize that does nothing. Inheritor must implement.
This method should not be invoked directly, use the class method
ImageProcessor.call_post_resize()to invoke it.- Parameters:
image – image to process
- Returns:
the processed image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None) Image[source]
Implementation of pre_resize that does nothing. Inheritor must implement.
This method should not be invoked directly, use the class method
ImageProcessor.call_pre_resize()to invoke it.- Parameters:
image – image to process
resize_resolution – image will be resized to this resolution after this process is complete. If None is passed no resize is going to occur. It is not the duty of the inheritor to resize the image, in fact it should NEVER be resized.
- Returns:
the processed image
- NAMES = ['upscaler']
- class dgenerate.imageprocessors.ZoeDepthProcessor(gamma_corrected: bool = False, detect_resolution: str | None = None, detect_aspect: bool = True, pre_resize: bool = False, **kwargs)[source]
Bases:
ImageProcessorzoe depth detector, a SOTA depth estimation model which produces high-quality depth maps.
The “gamma-corrected” argument determines if gamma correction is preformed on the produced depth math.
The “detect-resolution” argument is the resolution the image is resized to internal to the processor before detection is run on it. It should be a single dimension for example: “detect-resolution=512” or the X/Y dimensions seperated by an “x” character, like so: “detect-resolution=1024x512”. If you do not specify this argument, the detector runs on the input image at its full resolution. After processing the image will be resized to whatever you have requested dgenerate resize it to via –output-size or –resize/–align in the case of the image-process sub-command, if you have not requested any resizing the output will be resized back to the original size of the input image.
The “detect-aspect” argument determines if the image resize requested by “detect_resolution” before detection runs is aspect correct, this defaults to true.
The “pre-resize” argument determines if the processing occurs before or after dgenerate resizes the image. This defaults to False, meaning the image is processed after dgenerate is done resizing it.
- __init__(gamma_corrected: bool = False, detect_resolution: str | None = None, detect_aspect: bool = True, pre_resize: bool = False, **kwargs)[source]
- Parameters:
gamma_corrected – preform gamma correction on the depth map?
detect_resolution – the input image is resized to this dimension before being processed, providing
Noneindicates it is not to be resized. If there is no resize requested during the processing action viaresize_resolutionit will be resized back to its original size.detect_aspect – if the input image is resized by
detect_resolutionordetect_alignbefore processing, will it be an aspect correct resize?pre_resize – process the image before it is resized, or after? default is
False(after).kwargs – forwarded to base class
- impl_post_resize(image: Image)[source]
Post resize.
- Parameters:
image – image
- Returns:
possibly a zoe depth detected image, or the input image
- impl_pre_resize(image: Image, resize_resolution: tuple[int, int] | None)[source]
Pre resize.
- Parameters:
image – image to process
resize_resolution – resize resolution
- Returns:
possibly a zoe depth detected image, or the input image
- NAMES = ['zoe']
- dgenerate.imageprocessors.image_processor_help(names: Sequence[str], plugin_module_paths: Sequence[str] | None = None, throw=False, log_error=True)[source]
Implements
--image-processor-helpcommand line option- Parameters:
names – arguments (processor names, or empty list)
plugin_module_paths – extra plugin module paths to search
throw – throw on error? or simply print to stderr and return a return code.
log_error – log errors to stderr?
- Raises:
- Returns:
return-code, anything other than 0 is failure
dgenerate.invoker module
Functions to invoke dgenerate inside the current process using its command line arguments.
- class dgenerate.invoker.DgenerateExitEvent(origin, return_code: int)[source]
Bases:
EventGenerated in the event stream created by
invoke_dgenerate_events()Exit with return code event for
invoke_dgenerate_events()- return_code: int
- dgenerate.invoker.invoke_dgenerate(args: Sequence[str], render_loop: RenderLoop | None = None, throw: bool = False, log_error: bool = True, help_raises: bool = False) int[source]
Invoke dgenerate using its command line arguments and return a return code.
dgenerate is invoked in the current process, this method does not spawn a subprocess.
Meta arguments such as
--file,--shell,--no-stdin, and--consoleare not supported- Parameters:
args – dgenerate command line arguments in the form of a list, see: shlex module, or sys.argv
render_loop –
dgenerate.renderloop.RenderLoopinstance, ifNoneis provided one will be created.throw – Whether to throw known exceptions or handle them.
log_error – Write ERROR diagnostics with
dgenerate.messages?help_raises –
--helpraisesdgenerate.arguments.DgenerateHelpException? WhenTrue, this will occur even ifthrow=False
- Raises:
EnvironmentError –
- Returns:
integer return-code, anything other than 0 is failure
- dgenerate.invoker.invoke_dgenerate_events(args: Sequence[str], render_loop: RenderLoop | None = None, throw: bool = False, log_error: bool = True, help_raises: bool = False) Generator[ImageGeneratedEvent | StartingAnimationEvent | StartingAnimationFileEvent | AnimationFileFinishedEvent | ImageFileSavedEvent | AnimationFinishedEvent | StartingGenerationStepEvent | AnimationETAEvent | DgenerateExitEvent, None, None][source]
Invoke dgenerate using its command line arguments and return a stream of events, you must iterate over this stream of events to progress through the execution of dgenerate.
dgenerate is invoked in the current process, this method does not spawn a subprocess.
Meta arguments such as
--file,--shell,--no-stdin, and--consoleare not supportedThe exceptions mentioned here are those you may encounter upon iterating, they will not occur upon simple acquisition of the event stream iterator.
- Parameters:
args – dgenerate command line arguments in the form of a list, see: shlex module, or sys.argv
render_loop –
dgenerate.renderloop.RenderLoopinstance, ifNoneis provided one will be created.throw – Whether to throw known exceptions or handle them.
log_error – Write ERROR diagnostics with
dgenerate.messages?help_raises –
--helpraisesdgenerate.arguments.DgenerateHelpException? WhenTrue, this will occur even ifthrow=False
- Raises:
EnvironmentError –
- Returns:
- dgenerate.invoker.InvokeDgenerateEventStream
Event stream produced by
invoke_dgenerate_events()alias of
Generator[Union[ImageGeneratedEvent,StartingAnimationEvent,StartingAnimationFileEvent,AnimationFileFinishedEvent,ImageFileSavedEvent,AnimationFinishedEvent,StartingGenerationStepEvent,AnimationETAEvent,DgenerateExitEvent],None,None]
- dgenerate.invoker.InvokeDgenerateEvents
Events yield-able by
invoke_dgenerate_events()alias of
Union[ImageGeneratedEvent,StartingAnimationEvent,StartingAnimationFileEvent,AnimationFileFinishedEvent,ImageFileSavedEvent,AnimationFinishedEvent,StartingGenerationStepEvent,AnimationETAEvent,DgenerateExitEvent]
dgenerate.mediainput module
Media input, handles reading videos/animations and static images, and creating readers from image seed URIs.
Also provides media download capabilities and temporary caching of web based files.
Provides information about supported input formats.
- exception dgenerate.mediainput.FrameStartOutOfBounds[source]
Bases:
ValueErrorRaised by
MultiMediaReaderwhen the providedframe_startframe slicing value is calculated to be out of bounds.
- exception dgenerate.mediainput.ImageSeedArgumentError[source]
Bases:
ImageSeedErrorRaised when image seed URI keyword arguments receive invalid values.
- exception dgenerate.mediainput.ImageSeedError[source]
Bases:
ExceptionRaised on image seed parsing and loading errors.
- exception dgenerate.mediainput.ImageSeedFileNotFoundError[source]
Bases:
ImageSeedErrorRaised when a file on disk in an image seed could not be found.
- exception dgenerate.mediainput.ImageSeedParseError[source]
Bases:
ImageSeedErrorRaised on image seed syntactic parsing error.
- exception dgenerate.mediainput.ImageSeedSizeMismatchError[source]
Bases:
ImageSeedErrorRaised when the constituent image sources of an image seed specification are mismatched in dimension.
- exception dgenerate.mediainput.MediaIdentificationError[source]
Bases:
ExceptionRaised when a media file is being loaded and it fails to load due to containing invalid or unexpected data.
- exception dgenerate.mediainput.UnknownMimetypeError[source]
Bases:
ExceptionRaised when an unsupported mimetype is encountered
- class dgenerate.mediainput.AnimatedImageReader(file: str | BinaryIO, file_source: str, resize_resolution: tuple[int, int] | None = None, aspect_correct: bool = True, align: int | None = 8, image_processor: ImageProcessor = None)[source]
Bases:
ImageProcessorMixin,AnimationReaderImplementation of
AnimationReaderthat reads animated image formats using Pillow.All frames from this animation reader will be aligned by 8 pixels by default.
- __init__(file: str | BinaryIO, file_source: str, resize_resolution: tuple[int, int] | None = None, aspect_correct: bool = True, align: int | None = 8, image_processor: ImageProcessor = None)[source]
- Parameters:
file – a file path or binary file stream
file_source – the source filename for the animated image, should be the filename. this is for informational purpose when reading from a stream or a cached file and should be provided in every case even if it is a symbolic value only. It should possess a file extension.
PIL.Image.Imageobjects produced by the reader will have this value set to their filename attribute.resize_resolution – Progressively resize each frame to this resolution while reading. The provided resolution will be aligned by
alignif it is notNone.aspect_correct – Should resize operations be aspect correct?
align – Align by this amount of pixels, if the input file is not aligned to this amount of pixels, it will be aligned by resizing. Passing
Noneor1disables alignment.image_processor – optionally process every frame with this image processor
- Raises:
MediaIdentificationError – If the animated image data is an unknown format or corrupt.
- class dgenerate.mediainput.AnimationReader(width: int, height: int, fps: float, frame_duration: float, total_frames: int, **kwargs)[source]
Bases:
objectAbstract base class for animation readers.
- __init__(width: int, height: int, fps: float, frame_duration: float, total_frames: int, **kwargs)[source]
- Parameters:
width – width of the animation, X dimension
height – height of the animation, Y dimension
fps – frames per second
frame_duration – frame duration in milliseconds
total_frames – total frames in the animation
kwargs – for mixins
- property fps: float
Frames per second.
- property frame_duration: float
Duration of each frame in milliseconds.
- property height: int
Height dimension, (Y dimension).
- property size: tuple[int, int]
returns (width, height) as a tuple.
- property total_frames: int
Total number of frames that can be read.
- property width: int
Width dimension, (X dimension).
- class dgenerate.mediainput.ImageSeed(image: Image | None = None, mask_image: Image | None = None, control_images: Sequence[Image] | None = None, floyd_image: Image | None = None)[source]
Bases:
objectAn ImageSeed with attached image data
- __init__(image: Image | None = None, mask_image: Image | None = None, control_images: Sequence[Image] | None = None, floyd_image: Image | None = None)[source]
- control_images: Sequence[Image] | None
Control guidance images, or None.
- floyd_image: Image | None
An optional image from a Deep Floyd IF stage, used for disambiguation in the case of using Deep Floyd for img2img and inpainting, where the un-varied input image is needed as a parameter for both stages. This image is used to define the image that was generated by Deep Floyd in a previous stage and to be used in the next stage, where
ImageSeed.imagedefines the img2img image that you want a variation of.This image will never be assigned a value when
ImageSeed.control_imageshas a a value. As that is considered incorrect –image-seeds
- fps: float | None = None
Frames per second in the case that
ImageSeed.is_animation_frameis True
- frame_duration: float | None = None
Duration of a frame in milliseconds in the case that
ImageSeed.is_animation_frameis True
- frame_index: int | None = None
Frame index in the case that
ImageSeed.is_animation_frameis True
- image: Image | None
An optional image used for img2img mode, or inpainting mode in combination with
ImageSeed.mask_image
- is_animation_frame: bool
Is this part of an animation?
- mask_image: Image | None
An optional inpaint mask image, may be None
- total_frames: int | None = None
Total frame count in the case that
ImageSeed.is_animation_frameis True
- class dgenerate.mediainput.ImageSeedInfo(is_animation: bool, total_frames: int | None, fps: float | None, frame_duration: float | None)[source]
Bases:
objectInformation acquired about an
--image-seedsuri- __init__(is_animation: bool, total_frames: int | None, fps: float | None, frame_duration: float | None)[source]
- fps: float | None
Animation frames per second in the case that
ImageSeedInfo.is_animationis True
- frame_duration: float | None
Animation frame duration in milliseconds in the case that
ImageSeedInfo.is_animationis True
- is_animation: bool
Does this image seed specification result in an animation?
- total_frames: int | None
Animation frame count in the case that
ImageSeedInfo.is_animationis True
- class dgenerate.mediainput.ImageSeedParseResult[source]
Bases:
objectThe result of parsing an
--image-seedsuri- get_control_image_paths() Sequence[str] | None[source]
Return
ImageSeedParseResult.seed_pathifImageSeedParseResult.is_single_specisTrue.If the image seed is not a single specification, return
ImageSeedParseResult.control_path.If
ImageSeedParseResult.control_pathis not set and the image seed is not a single specification, returnNone.- Returns:
list of resource paths, or None
- aspect_correct: bool | None = None
Aspect correct resize setting override from the aspect image seed keyword argument, if this is None it was not specified. This value if defined should override any globally defined aspect correct resize setting.
- control_path: str | Sequence[str] | None = None
Optional controlnet guidance path, or a sequence of controlnet guidance paths. This field is only used when the secondary syntax of
--image-seedsis encountered.In parses such as:
--image-seeds "img2img.png;control=control.png"--image-seeds "img2img.png;control=control1.png, control2.png"--image-seeds "img2img.png;mask=mask.png;control=control.png"--image-seeds "img2img.png;mask=mask.png;control=control1.png, control2.png"
- floyd_path: str | None = None
Optional path to a result from a Deep Floyd IF stage, used only for img2img and inpainting mode with Deep Floyd. This is the only way to specify the image that was output by a stage in that case.
the arguments floyd and control are mutually exclusive.
In parses such as:
--image-seeds "img2img.png;floyd=stage1-output.png"--image-seeds "img2img.png;mask=mask.png;floyd=stage1-output.png"
- frame_end: int | None = None
Optional end frame specification for per-image seed slicing.
- frame_start: int | None = None
Optional start frame specification for per-image seed slicing.
- property is_single_spec: bool
Is this
--image-seedsuri a single resource or resource group specification existing within the seed_path attribute of this object?For instance could it be a single img2img image definition, or a controlnet guidance image or sequence of controlnet guidance images?
This requires that
mask_path,control_path, andfloyd_pathare all undefined.Possible parses which trigger this condition are:
--image-seeds "img2img.png"--image-seeds "control-image.png"--image-seeds "control-image1.png, control-image2.png"
Since this is an ambiguous parse, it must be resolved later with the help of other specified arguments. Such as by the specification of
--control-nets, which makes the intention unambiguous.- Returns:
bool
- mask_path: str | None = None
Optional path to an inpaint mask, may be an HTTP/HTTPS URL or file path.
- resize_resolution: tuple[int, int] | None = None
Per image user specified resize resolution for all components of the
--image-seedspecification.This field available in parses such as:
--image-seeds "img2img.png;512x512"--image-seeds "img2img.png;mask.png;512x512"--image-seeds "img2img.png;control=control.png;resize=512x512"--image-seeds "img2img.png;control=control1.png, control2.png;resize=512x512"--image-seeds "img2img.png;mask=mask.png;control=control.png;resize=512x512"--image-seeds "img2img.png;mask=mask.png;control=control1.png, control2.png;resize=512x512"
This should override any globally defined resize value.
- seed_path: str | Sequence[str]
The seed path, contains an image path that will be used for img2img operations or the base image in inpaint operations. Or a controlnet guidance path, or a sequence of controlnet guidance paths. A path being a file path, or an HTTP/HTTPS URL.
- class dgenerate.mediainput.MediaReader(path: str, image_processor: ~dgenerate.imageprocessors.imageprocessor.ImageProcessor | None = None, resize_resolution: tuple[int, int] | None = None, aspect_correct: bool = True, align: int | None = 8, frame_start: int = 0, frame_end: int | None = None, path_opener: ~typing.Callable[[str], ~typing.BinaryIO] = <function fetch_media_data_stream>)[source]
Bases:
AnimationReaderThin wrapper around
MultiMediaReaderwhich simply reads from a single file instead of multiple files simultaneously.The interface provided by this object is that of
AnimationReaderThis object can read any media supported by dgenerate for input and supports frame slicing animated media formats and image processors.
Static images are treated as an animation with a single frame.
With the default path opener, URLs will be downloaded, dgenerates temporary web cache will be utilized.
All images produced from this reader will be aligned to 8 pixels by default.
- __init__(path: str, image_processor: ~dgenerate.imageprocessors.imageprocessor.ImageProcessor | None = None, resize_resolution: tuple[int, int] | None = None, aspect_correct: bool = True, align: int | None = 8, frame_start: int = 0, frame_end: int | None = None, path_opener: ~typing.Callable[[str], ~typing.BinaryIO] = <function fetch_media_data_stream>)[source]
- Raises:
ValueError – if
frame_start>frame_endFrameStartOutOfBounds – if
frame_start>total_frames - 1
- Parameters:
path – File path or URL
resize_resolution – Resize resolution
aspect_correct – Aspect correct resize enabled?
align – Images which are read are aligned to this amount of pixels,
Noneor1will disable alignment.image_processor – Optional image image processor associated with the file
frame_start – inclusive frame slice start frame
frame_end – inclusive frame slice end frame
path_opener – opens a binary file stream from paths.
- property frame_end: int
Frame slice end value (inclusive)
- property frame_index: int
Current frame index while reading.
- property frame_start: int
Frame slice start value (inclusive)
- class dgenerate.mediainput.MediaReaderSpec(path: str, image_processor: ImageProcessor | None = None, resize_resolution: tuple[int, int] | None = None, aspect_correct: bool = True, align: int | None = 8)[source]
Bases:
objectUsed by
MultiMediaReaderto define resource paths.- __init__(path: str, image_processor: ImageProcessor | None = None, resize_resolution: tuple[int, int] | None = None, aspect_correct: bool = True, align: int | None = 8)[source]
- Parameters:
path – File path or URL
resize_resolution – Resize resolution
aspect_correct – Aspect correct resize enabled?
align – Images which are read are aligned to this amount of pixels,
Noneor1will disable alignment.image_processor – Optional image image processor associated with the file
- align: int | None = 8
Images which are read are aligned to this amount of pixels,
Noneor1will disable alignment.
- aspect_correct: bool = True
Aspect correct resize enabled?
- image_processor: ImageProcessor | None = None
Optional image image processor associated with the file
- path: str
File path (or HTTP/HTTPS URL with default
path_opener)
- resize_resolution: tuple[int, int] | None = None
Optional resize resolution.
- class dgenerate.mediainput.MockImageAnimationReader(img: Image, resize_resolution: tuple[int, int] | None = None, aspect_correct: bool = True, align: int | None = 8, image_repetitions: int = 1, image_processor: ImageProcessor = None)[source]
Bases:
ImageProcessorMixin,AnimationReaderImplementation of
AnimationReaderthat repeats a single PIL image as many times as desired in order to mock/emulate an animation.All frame images from this animation reader will be aligned by 8 pixels by default.
- __init__(img: Image, resize_resolution: tuple[int, int] | None = None, aspect_correct: bool = True, align: int | None = 8, image_repetitions: int = 1, image_processor: ImageProcessor = None)[source]
- Parameters:
img – source image to copy for each frame, the image is immediately copied once upon construction of the mock reader, and then once per frame thereafter. Your copy of the image can be disposed of after the construction of this object.
resize_resolution – the source image will be resized to this dimension with a maintained aspect ratio. This occurs once upon construction, a copy is then yielded for each frame that is read. The provided resolution will be aligned by
alignif it is notNone.aspect_correct – Should resize operations be aspect correct?
align – Align by this amount of pixels, if the input file is not aligned to this amount of pixels, it will be aligned by resizing. Passing
Noneor1disables alignment.image_repetitions – number of frames that this mock reader provides using a copy of the source image.
image_processor – optionally process the initial image with this image processor, this occurs once.
- property total_frames: int
Settable total_frames property.
- Returns:
frame count
- class dgenerate.mediainput.MultiMediaReader(specs: list[~dgenerate.mediainput.MediaReaderSpec], frame_start: int = 0, frame_end: int | None = None, path_opener: ~typing.Callable[[str], ~typing.BinaryIO] = <function fetch_media_data_stream>)[source]
Bases:
objectZips together multiple automatically created
AnimationReaderimplementations and allows enumeration over their reads, which are collected into a list of a defined order.Images when zipped together with animated files will be repeated over the total amount of frames.
The animation with the lowest amount of frames determines the total amount of frames that can be read when animations are involved.
If all paths point to images, then
MultiMediaReader.total_frameswill be 1.There is no guarantee that images read from the individual readers are the same size and you must handle that condition.
- __init__(specs: list[~dgenerate.mediainput.MediaReaderSpec], frame_start: int = 0, frame_end: int | None = None, path_opener: ~typing.Callable[[str], ~typing.BinaryIO] = <function fetch_media_data_stream>)[source]
- Raises:
ValueError – if
frame_start>frame_endFrameStartOutOfBounds – if
frame_start>total_frames - 1
- Parameters:
specs – list of
MediaReaderSpecframe_start – inclusive frame slice start frame
frame_end – inclusive frame slice end frame
path_opener – opens a binary file stream from paths mentioned by
MediaReaderSpec
- height(idx) int[source]
Height dimension, (Y dimension) of a specific reader index.
- Returns:
height
- size(idx) tuple[int, int][source]
returns (width, height) as a tuple of a specific reader index.
- Returns:
(width, height)
- property fps: float | None
Frames per second, this will be None if there is only a single frame
- property frame_duration: float | None
Duration of a frame in milliseconds, this will be None if there is only a single frame
- property frame_end: int
Frame slice end value (inclusive)
- property frame_index: int
Current frame index while reading.
- property frame_start: int
Frame slice start value (inclusive)
- property total_frames: int
Total number of frames readable from this reader.
- class dgenerate.mediainput.VideoReader(file: str | BinaryIO, file_source: str, resize_resolution: tuple[int, int] | None = None, aspect_correct: bool = True, align: int | None = 8, image_processor: ImageProcessor = None)[source]
Bases:
ImageProcessorMixin,AnimationReaderImplementation
AnimationReaderthat reads Video files with PyAV.All frame images from this animation reader will be aligned by 8 pixels by default.
- __init__(file: str | BinaryIO, file_source: str, resize_resolution: tuple[int, int] | None = None, aspect_correct: bool = True, align: int | None = 8, image_processor: ImageProcessor = None)[source]
- Parameters:
file – a file path or binary file stream
file_source – the source filename for the video data, should be the filename. this is for informational purpose when reading from a stream or a cached file and should be provided in every case even if it is a symbolic value only. It should possess a file extension as it is used to determine file format when reading from a byte stream.
PIL.Image.Imageobjects produced by the reader will have this value set to their filename attribute.resize_resolution – Progressively resize each frame to this resolution while reading. The provided resolution will be aligned by
alignif it is notNone.aspect_correct – Should resize operations be aspect correct?
align – Align by this amount of pixels, if the input file is not aligned to this amount of pixels, it will be aligned by resizing. Passing
Noneor1disables alignment.image_processor – optionally process every frame with this image processor
- Raises:
ValueError – if file_source lacks a file extension, it is needed to determine the video file format.
MediaIdentificationError – If the video data is an unknown format or corrupt.
- dgenerate.mediainput.create_animation_reader(mimetype: str, file_source: str, file: BinaryIO, resize_resolution: tuple[int, int] | None = None, aspect_correct: bool = True, align: int | None = 8, image_processor: ImageProcessor | None = None) AnimationReader[source]
Create an animation reader object from mimetype specification and binary file stream.
Images will return a
MockImageAnimationReaderwith a total_frames value of 1, which can then be adjusted by you.VideoReaderorAnimatedImageReaderwill be returned for Video files and Animated Images respectively.- Raises:
UnknownMimetypeError – on unknown
mimetypevalueMediaIdentificationError – If the file data is an unknown format or corrupt.
- Parameters:
mimetype – one of
get_supported_mimetypes()file – the binary file stream
file_source – the source filename for videos/animated images, should be the filename. this is for informational purpose and should be provided in every case even if it is a symbolic value only. It should possess a file extension.
PIL.Image.Imageobjects produced by the reader will have this value set to their filename attribute.resize_resolution – Progressively resize each frame to this resolution while reading. The provided resolution will be aligned by
alignpixels.align – Align by this amount of pixels, if the input file is not aligned to this amount of pixels, it will be aligned by resizing. Passing
Noneor1disables alignment.aspect_correct – Should resize operations be aspect correct?
image_processor – optionally process every frame with this image processor
- Returns:
- dgenerate.mediainput.create_image(path_or_file: BinaryIO | str, file_source: str, resize_resolution: tuple[int, int] | None = None, aspect_correct: bool = True, align: int | None = 8) Image[source]
Create an RGB format PIL image from a file path or binary file stream. The image is oriented according to any EXIF directives. Image is aligned to
alignin every case, specifyingNoneor1foraligndisables alignment.- Raises:
MediaIdentificationError – If the image data is an unknown format or corrupt.
- Parameters:
path_or_file – file path or binary IO object
file_source –
PIL.Image.Image.filenameis set to this valueresize_resolution – Optional resize resolution
aspect_correct – preserve aspect ratio when resize_resolution is specified?
align – Align the image by this amount of pixels,
Noneor1indicates no alignment.
- Returns:
PIL.Image.Image
- dgenerate.mediainput.create_web_cache_file(url, mime_acceptable_desc: str | None = None, mimetype_is_supported: ~typing.Callable[[str], bool] | None = <function mimetype_is_supported>) tuple[str, str][source]
Download a file from a url and add it to dgenerates temporary web cache that is available to all concurrent dgenerate processes.
If the file exists in the cache already, return information for the existing file.
- Parameters:
url – The url
mime_acceptable_desc – a string describing what mimetype values are acceptable which is used when
UnknownMimetypeErroris raised. IfNoneis provided, this string will be generated usingget_supported_mimetypes()mimetype_is_supported – a function that test if a mimetype string is supported, if you supply the value
Noneall mimetypes are considered supported.
- Raises:
UnknownMimetypeError – if a mimetype is considered not supported
requests.RequestException – Can raise any exception raised by
requests.getfor request related errors.
- Returns:
tuple(mimetype_str, filepath)
- dgenerate.mediainput.fetch_media_data_stream(uri: str) tuple[str, BinaryIO][source]
Get an open stream to a local file, or file at an HTTP or HTTPS URL, with caching for web files.
Caching for downloaded files is multiprocess safe, multiple processes using this module can share the cache simultaneously, the last process alive clears the cache when it exits.
- Parameters:
uri – Local file path or URL
- Raises:
UnknownMimetypeError – If a remote file serves an unsupported mimetype value
- Returns:
(mime-type string, BinaryIO)
- dgenerate.mediainput.frame_slice_count(total_frames: int, frame_start: int, frame_end: int | None = None) int[source]
Calculate the number of frames resulting from frame slicing.
- Parameters:
total_frames – Total frames being sliced from
frame_start – The start frame
frame_end – The end frame
- Returns:
int
- dgenerate.mediainput.get_control_image_info(uri: str | ImageSeedParseResult, frame_start: int = 0, frame_end: int | None = None) ImageSeedInfo[source]
Get an informational object from a dgenerate
--image-seedsuri that is known to be a control image/video specification.This can consist of a single resource path or a list of comma separated image and video resource paths, which may be files on disk or remote files (http / https).
This method is to be used when it is known that there is only a control image/video specification in the path, and it handles this specification syntax:
--image-seeds "control1.png"--image-seeds "control1.png, control2.png"
- Parameters:
uri – The path string or
ImageSeedParseResultframe_start – slice start
frame_end – slice end
- Returns:
- dgenerate.mediainput.get_image_seed_info(uri: str | ImageSeedParseResult, frame_start: int = 0, frame_end: int | None = None) ImageSeedInfo[source]
Get an informational object from a dgenerate
--image-seedsuri.This method is used to obtain information about an
--image-seedsuri in the case that the image source mentioned is to be used for img2img / inpaint operations, and handles this syntax:--image-seeds "img2img.png;mask.png"--image-seeds "img2img.png;mask.png;512x512"
Additionally, control net image sources are handled via the secondary syntax:
--image-seeds "img2img.png;control=control1.png, control2.png"--image-seeds "img2img.png;control=control1.png, control2.png;resize=512x512"--image-seeds "img2img.png;mask=mask.png;control=control1.png, control2.png"--image-seeds "img2img.png;mask=mask.png;control=control1.png, control2.png;resize=512x512"
- Parameters:
uri – The uri string or
ImageSeedParseResultframe_start – slice start
frame_end – slice end
- Returns:
- dgenerate.mediainput.get_supported_animated_image_mimetypes() list[str][source]
Get a list of mimetypes that are considered to be supported animated image mimetypes.
- Returns:
list of mimetype strings.
- dgenerate.mediainput.get_supported_image_mimetypes() list[str][source]
Get all supported
--image-seedsimage mimetypes, including animated image mimetypes- Returns:
list of strings
- dgenerate.mediainput.get_supported_mimetypes() list[str][source]
Get all supported
--image-seedsmimetypes, video mimetype may contain a wildcard.- Returns:
list of strings
- dgenerate.mediainput.get_supported_static_image_mimetypes() list[str][source]
Get a list of mimetypes that are considered to be supported static image mimetypes.
- Returns:
list of mimetype strings.
- dgenerate.mediainput.get_supported_video_mimetypes() list[str][source]
Get all supported
--image-seedsvideo mimetypes, may contain a wildcard- Returns:
list of strings
- dgenerate.mediainput.get_web_cache_directory() str[source]
Get the default web cache directory or the value of the environmental variable
DGENERATE_WEB_CACHE- Returns:
string (directory path)
- dgenerate.mediainput.guess_mimetype(filename) str | None[source]
Guess the mimetype of a filename.
The filename does not need to exist on disk.
- Parameters:
filename – the file name
- Returns:
mimetype string or
None
- dgenerate.mediainput.is_downloadable_url(string) bool[source]
Does a string represent a URL that can be downloaded from by
dgenerate.mediainput?- Parameters:
string – the string
- Returns:
TrueorFalse
- dgenerate.mediainput.iterate_control_image(uri: str | ImageSeedParseResult, frame_start: int = 0, frame_end: int | None = None, resize_resolution: tuple[int, int] | None = None, aspect_correct: bool = True, align: int | None = 8, image_processor: ImageProcessor | Sequence[ImageProcessor] | None = None) Iterator[ImageSeed][source]
Parse and load a control image/video in an
--image-seedsuri and return an iterator that producesImageSeedobjects while progressively reading that file.One or more
ImageSeedobjects may be yielded depending on whether an animation is being read.This can consist of a single resource path or a list of comma separated image and video resource paths, which may be files on disk or remote files (http / https).
This method is to be used when it is known that there is only a controlnet guidance resource specification in the path, and it handles this specification syntax:
--image-seeds "control1.png"--image-seeds "control1.png, control2.png"--image-seeds "control1.png, control2.png;512x512"--image-seeds "control1.png, control2.png;resize=512x512"--image-seeds "control1.png, control2.png;frame-start=2"--image-seeds "control1.png, control2.png;frame-start=2;frame-end=10"--image-seeds "control1.png, control2.png;resize=512x512;frame-start=2;frame-end=10"
The image or images read will be available from the
ImageSeed.control_imagesattribute.- Parameters:
uri –
--image-seedsuri orImageSeedParseResultframe_start – starting frame, inclusive value
frame_end – optional end frame, inclusive value
resize_resolution – optional global resize resolution. The URI syntax of image seeds allows for overriding this value.
aspect_correct – should the global resize operation be aspect correct by default? The URI syntax for image seeds allows for overriding this value with the aspect keyword argument.
align – Images which are read are aligned to this amount of pixels,
Noneor1will disable alignment.image_processor – optional
dgenerate.imageprocessors.ImageProcessoror list of them. A list is used to specify processors for individual images in a multi guidance image specification such as uri = “img1.png, img2.png”. In the case that a multi guidance image specification is used and only one processor is given, that processor will be used on only the first image / video in the specification. Images in a guidance specification with no corresponding processor value will have their processor set toNone, specifying extra processors as compared to control guidance image sources will causeValueErrorto be raised.
- Raises:
ValueError – if there are more image_processor values than there are control guidance image sources in the URI.
- Returns:
an iterator over
ImageSeedobjects
- dgenerate.mediainput.iterate_image_seed(uri: str | ImageSeedParseResult, frame_start: int = 0, frame_end: int | None = None, resize_resolution: tuple[int, int] | None = None, aspect_correct: bool = True, align: int | None = 8, seed_image_processor: ImageProcessor | None = None, mask_image_processor: ImageProcessor | None = None, control_image_processor: ImageProcessor | Sequence[ImageProcessor] | None = None) Iterator[ImageSeed][source]
Parse and load images/videos in an
--image-seedsuri and return an iterator that producesImageSeedobjects while progressively reading those files.This method is used to iterate over an
--image-seedsuri in the case that the image source mentioned is to be used for img2img / inpaint operations, and handles this syntax:--image-seeds "img2img.png"--image-seeds "img2img.png;mask.png"--image-seeds "img2img.png;mask.png;512x512"
Additionally, controlnet guidance resources are handled via the secondary syntax:
--image-seeds "img2img.png;control=control1.png, control2.png"--image-seeds "img2img.png;control=control1.png, control2.png;resize=512x512"--image-seeds "img2img.png;mask=mask.png;control=control1.png, control2.png"--image-seeds "img2img.png;mask=mask.png;control=control1.png, control2.png;resize=512x512"--image-seeds "img2img.png;mask=mask.png;control=control1.png;frame-start=2"--image-seeds "img2img.png;mask=mask.png;control=control1.png;frame-start=2;frame-end=5"
Deep Floyd img2img and inpainting mode are handled via a tertiary syntax:
--image-seeds "img2img.png;floyd=stage1-image.png"--image-seeds "img2img.png;mask=mask.png;floyd=stage2-image.png"
Note that all keyword arguments mentioned above can be used together, with exception of “control” and “floyd” which are mutually exclusive arguments.
One or more
ImageSeedobjects may be yielded depending on whether an animation is being read.- Parameters:
uri –
--image-seedsuri orImageSeedParseResultframe_start – starting frame, inclusive value
frame_end – optional end frame, inclusive value
resize_resolution – optional global resize resolution. The URI syntax of image seeds allows for overriding this value.
aspect_correct – should the global resize operation be aspect correct by default? The URI syntax for image seeds allows for overriding this value with the aspect keyword argument.
align – Images which are read are aligned to this amount of pixels,
Noneor1will disable alignment.seed_image_processor – optional
dgenerate.imageprocessors.ImageProcessormask_image_processor – optional
dgenerate.imageprocessors.ImageProcessorcontrol_image_processor – optional
dgenerate.imageprocessors.ImageProcessoror list of them. A list is used to specify processors for individual images in a multi guidance image specification such as uri = “img2img.png;control=img1.png, img2.png”. In the case that a multi guidance image specification is used and only one processor is given, that processor will be used on only the first image / video in the specification. Images in a guidance specification with no corresponding processor value will have their processor set toNone, specifying extra processors as compared to control guidance image sources will causeValueErrorto be raised.
- Raises:
ValueError – if there are more control_image_processor values than there are control guidance image sources in the URI.
- Returns:
an iterator over
ImageSeedobjects
- dgenerate.mediainput.mimetype_is_animated_image(mimetype: str) bool[source]
Check if a mimetype is one that dgenerate considers an animated image
- Parameters:
mimetype – The mimetype string
- Returns:
bool
- dgenerate.mediainput.mimetype_is_static_image(mimetype: str) bool[source]
Check if a mimetype is one that dgenerate considers a static image
- Parameters:
mimetype – The mimetype string
- Returns:
bool
- dgenerate.mediainput.mimetype_is_supported(mimetype: str) bool[source]
Check if dgenerate supports a given input mimetype
- Parameters:
mimetype – The mimetype string
- Returns:
bool
- dgenerate.mediainput.mimetype_is_video(mimetype: str) bool[source]
Check if a mimetype is a video mimetype supported by dgenerate
- Parameters:
mimetype – The mimetype string
- Returns:
bool
- dgenerate.mediainput.parse_image_seed_uri(uri: str, align: int | None = 8) ImageSeedParseResult[source]
Parse an
--image-seedsuri into its constituentsAll URI related errors raised by this function derive from
ImageSeedError.- Raises:
ValueError – if
align < 1ImageSeedParseError – on syntactical parsing errors
ImageSeedArgumentError – on image seed URI argument errors
ImageSeedFileNotFoundError – when a file mentioned in an image seed does not exist on disk
- Parameters:
uri –
--image-seedsurialign – do not allow per image seed resize resolutions that are not aligned to this value, setting this value to 1 or
Nonedisables alignment checks.
- Returns:
- dgenerate.mediainput.request_mimetype(url) str[source]
Request the mimetype of a file at a URL, if the file exists in the cache, a known mimetype is returned without connecting to the internet. Otherwise, connect to the internet to retrieve the mimetype, this action does not update the cache.
- Parameters:
url – The url
- Returns:
mimetype string
- dgenerate.mediainput.supported_animation_reader_formats()[source]
Supported animation reader formats, file extensions with no period.
- Returns:
list of file extensions.
- dgenerate.mediainput.supported_image_formats()[source]
What file extensions does PIL/Pillow support for reading?
File extensions are returned without a period.
- Returns:
list of file extensions
- dgenerate.mediainput.url_aware_basename(path)[source]
Get the
os.path.basenameof a file path or URL.- Parameters:
path – the path
- Returns:
basename
dgenerate.mediaoutput module
Media output, handles writing videos and animations.
Provides information about supported output formats.
- exception dgenerate.mediaoutput.UnknownAnimationFormatError[source]
Bases:
ExceptionRaised by
create_animation_writer()when an unknown animation format is provided.
- class dgenerate.mediaoutput.AnimatedImageWriter(filename: str, duration: float)[source]
Bases:
AnimationWriterAnimation writer for animated images such as GIFs and webp
- class dgenerate.mediaoutput.MultiAnimationWriter(animation_format: str, filename: str, fps: float, allow_overwrites=False)[source]
Bases:
AnimationWriterSplits writes between N files with generated filename suffixes if necessary depending on how many images were written on the first write.
- __init__(animation_format: str, filename: str, fps: float, allow_overwrites=False)[source]
- Parameters:
animation_format – One of
supported_animation_writer_formats()filename – The desired filename, if multiple images are written a suffix
_animation_Nwill be appended for each filefps – Frames per second
allow_overwrites – Allow overwrites of existing files? or append
_duplicate_N, The overwrite dis-allowance is multiprocess safe between instances of this library.
- class dgenerate.mediaoutput.VideoWriter(filename, fps: float)[source]
Bases:
AnimationWriterAnimation writer for MP4 h264 format video
- dgenerate.mediaoutput.create_animation_writer(animation_format: str, out_filename: str, fps: float)[source]
Create an animation writer of a given format.
- Raises:
UnknownAnimationFormatError – if the provided
animation_formatis unknown.- Parameters:
animation_format – The animation format, see
supported_animation_writer_formats()out_filename – the output file name
fps – FPS
- Returns:
dgenerate.memoize module
Function memoization wrapper and associated hashing tools.
- dgenerate.memoize.args_cache_key(args_dict: dict[str, Any], custom_hashes: dict[str, Callable[[Any], str]] = None)[source]
Generate a cache key for a functions arguments to use for memoization.
- Parameters:
args_dict – The args dictionary of the function
custom_hashes – Custom hash functions for specific argument names if needed
- Returns:
string
- dgenerate.memoize.memoize(cache: dict[str, ~typing.Any], exceptions: set[str] = None, hasher: ~typing.Callable[[dict[str, ~typing.Any]], str] = <function args_cache_key>, on_hit: ~typing.Callable[[str, ~typing.Any], None] = None, on_create: ~typing.Callable[[str, ~typing.Any], None] = None)[source]
Decorator used to Memoize a function using a dictionary as a value cache.
- Parameters:
cache – The dictionary to serve as a cache
exceptions – Function arguments to ignore
hasher – Responsible for hashing arguments and argument values
on_hit – Called on cache hit for the wrapped function
on_create – Called on cache miss for the wrapped function
- Returns:
decorator
- dgenerate.memoize.simple_cache_hit_debug(title: str, cache_key: str, cache_hit: Any)[source]
Basic cache hit debug message for
memoize()decorator on_hit parameter.Messages are printed using
dgenerate.messages.debug_log()- Example:
on_hit=lambda key, hit: simple_cache_hit_debug("My Object", key, hit)- Debug Prints:
Cache Hit, Loaded My Object: (fully qualified name of hit object), Cache Key: (key)
- Parameters:
title – Object Title
cache_key – cache key
cache_hit – cached object
- dgenerate.memoize.simple_cache_miss_debug(title: str, cache_key: str, new: Any)[source]
Basic cache hit debug message for
memoize()decorator on_create parameter.Messages are printed using
dgenerate.messages.debug_log()- Example:
on_create=lambda key, hit: simple_cache_miss_debug("My Object", key, hit)- Debug Prints:
Cache Miss, Created My Object: (fully qualified name of new object), Cache Key: (key)
- Parameters:
title – Object Title
cache_key – cache key
new – newly created object
- dgenerate.memoize.struct_hasher(obj: Any, custom_hashes: dict[str, Callable[[Any], str]] = None, exclude: set[str] | None = None) str[source]
Create a hash string from a simple objects public attributes.
- Parameters:
obj – the object
custom_hashes – Custom hash functions for specific attribute names if needed
exclude – Exclude attributes by name
- Returns:
string
dgenerate.memory module
System memory information and memory constraint expressions.
- exception dgenerate.memory.MemoryConstraintSyntaxError[source]
Bases:
ExceptionThrown by
memory_constraints()on syntax errors or if an expression returns a non-boolean value
- dgenerate.memory.bytes_best_human_unit(byte_count: int, delimiter='') str[source]
Return a string for humans from a byte count using an appropriate unit: IE 1KB, 1MB, 1GB etc.
- Parameters:
delimiter – add this string between the value and the unit
byte_count – the byte count
- Returns:
formatted string
- dgenerate.memory.calculate_chunk_size(file_size)[source]
Calculate the chunk size for downloading / copying a file based on the file size and available memory.
- Parameters:
file_size – The size of the file to be downloaded / copied.
- Returns:
The calculated chunk size.
- dgenerate.memory.cuda_memory_constraints(expressions: ~collections.abc.Iterable[str], extra_vars: dict[str, int | float] | None = None, mode=<built-in function any>, device: str | ~torch.device = 'cuda:0') bool[source]
Evaluate a user boolean expression involving the CUDA device’s memory in bytes, used memory percent, and available CUDA memory in bytes.
If you pass a non cuda device identifier to this method, it will always return
False- Available functions are:
kb(bytes to kilobytes)
mb(bytes to megabytes)
gb(bytes to gigabytes)
kib(bytes to kibibytes)
mib(bytes to mebibytes)
gib(bytes to gibibytes)
- Available values are:
used / u (memory currently used by the CUDA device in bytes)
used_total_percent / utp (memory used by the CUDA device, as percent of total CUDA memory, example: 25.4)
available / a (available memory remaining on the CUDA device in bytes that can be used)
total / t (total memory on the CUDA device in bytes)
- Example expressions:
used > gb(1)(when the device has used more than 1GB of memory)used_total_percent > 25(when the device has used more than 25 percent of CUDA memory)available < gb(2)(when the available memory on the device is less than 2GB)
Expressions may not be longer than 128 characters. However, multiple expressions may be provided.
- Raises:
ValueError – if extra_vars overwrites a reserved variable name, or if
deviceis not astrortorch.deviceobject.MemoryConstraintSyntaxError – on syntax errors or if the return value of an expression is not a boolean value.
- Parameters:
expressions – a list of expressions, if expressions is
Noneor empty this function will returnFalse.extra_vars – extra integer or float variables
mode – the standard library function ‘any’ (equating to OR all expressions) or the standard library function ‘all’ (equating to AND all expressions). The default is ‘any’ which ORs all expressions.
device – CUDA device string or torch.device object, defaults to ‘cuda:0’.
- Returns:
Boolean result of the expression
- dgenerate.memory.get_available_memory(unit='b')[source]
Get the available memory remaining on the system in a selectable unit.
- Parameters:
unit – one of (case insensitive): b (bytes), kb (kilobytes), mb (megabytes), gb (gigabytes), kib (kibibytes), mib (mebibytes), gib (gibibytes)
- Returns:
Requested value.
- dgenerate.memory.get_total_memory(unit='b')[source]
Get the total physical memory on the system.
- Parameters:
unit – one of (case insensitive): b (bytes), kb (kilobytes), mb (megabytes), gb (gigabytes), kib (kibibytes), mib (mebibytes), gib (gibibytes)
- Returns:
Requested value.
- dgenerate.memory.get_used_memory(unit='b', pid: int | None = None) int[source]
Get the memory used by a process in a selectable unit.
- Parameters:
unit – one of (case insensitive): b (bytes), kb (kilobytes), mb (megabytes), gb (gigabytes), kib (kibibytes), mib (mebibytes), gib (gibibytes)
pid – The process PID to retrieve this information from, defaults to the current process.
- Returns:
Requested value.
- dgenerate.memory.get_used_memory_percent(pid: int | None = None) float[source]
Get the percentage of memory used by a process as a percentage of already used memory plus available virtual memory.
- Parameters:
pid – PID of the process, defaults to the current process.
- Returns:
A whole percentage, for example: 25.4
- dgenerate.memory.get_used_total_memory_percent(pid: int | None = None) float[source]
Get the percentage of memory used by a process as a percentage of total system memory.
- Parameters:
pid – PID of the process, defaults to the current process.
- Returns:
A whole percentage, for example: 25.4
- dgenerate.memory.memory_constraint_syntax_check(expression: str)[source]
Syntax check an expression given to
memory_constraints()- Parameters:
expression – the expression string
- Raises:
MemoryConstraintSyntaxError – on syntax errors.
- dgenerate.memory.memory_constraints(expressions: ~collections.abc.Iterable[str], extra_vars: dict[str, int | float] | None = None, mode=<built-in function any>, pid: int | None = None) bool[source]
Evaluate a user boolean expression involving the processes used memory in bytes, used memory percent, and available system memory in bytes.
- Available functions are:
kb(bytes to kilobytes)
mb(bytes to megabytes)
gb(bytes to gigabytes)
kib(bytes to kibibytes)
mib(bytes to mebibytes)
gib(bytes to gibibytes)
- Available values are:
used / u (memory currently used by the process in bytes)
used_total_percent / utp (memory used by the process, as percent of total system memory, example: 25.4)
used_percent / up (memory used by the process, as a percent of used + available memory, example 75.4)
available / a (available memory remaining on the system in bytes that can be used without going to the swap)
total / t (total memory on the system in bytes)
- Example expressions:
used > gb(1)(when the process has used more than 1GB of memory)used_total_percent > 25(when the process has used more than 25 percent of system memory)used_percent > 25(when the process has used more than 25 percent of virtual memory available to it)available < gb(2)(when the available memory on the system is less than 2GB)
Expressions may not be longer than 128 characters. However, multiple expressions may be provided.
- Raises:
ValueError – if extra_vars overwrites a reserved variable name
MemoryConstraintSyntaxError – on syntax errors or if the return value of an expression is not a boolean value.
- Parameters:
expressions – a list of expressions, if expressions is
Noneor empty this function will returnFalse.extra_vars – extra integer or float variables
mode – the standard library function ‘any’ (equating to OR all expressions) or the standard library function ‘all’ (equating to AND all expressions). The default is ‘any’ which ORs all expressions.
pid – PID of the process from which to acquire the ‘used’ and ‘used_percent’ variable values from, defaults to the current process.
- Returns:
Boolean result of the expression
- dgenerate.memory.memory_use_debug_string(pid=None)[source]
Return a debug string using describing the memory consumption of a process and also available system memory.
- Example:
“Used Memory: 465.25MB, Available Memory: 50.94GB, Used Percent: 0.91%, Total Memory: 68.64GB, Used Total Percent: 0.68%”
- Where:
Used Memory =
get_used_memory()Available Memory =
get_available_memory()Used Percent =
get_used_memory_percent()Total Memory =
get_total_memory()Used Percent Total =
get_used_total_memory_percent()
- Parameters:
pid – PID of the process to describe, defaults to the current process.
- Returns:
formatted string
dgenerate.messages module
Library logging / informational output.
- dgenerate.messages.add_logging_handler(callback: Callable[[ParamSpecArgs, int, bool, str], None])[source]
Add your own logging handler callback.
- Parameters:
callback – Callback accepting
(*args, LEVEL, underline (bool), underline_char)
- dgenerate.messages.debug_log(*func_or_str: Callable[[], Any] | Any, underline=False, underline_char='=')[source]
Conditionally log strings or possibly expensive functions if
LEVELis set toDEBUG.- Parameters:
func_or_str – objects to be stringified and printed or callables that return said objects
underline – Underline this message?
underline_char – Underline character.
- dgenerate.messages.get_error_file()[source]
Get the file stream or file like object for dgenerates error output.
- dgenerate.messages.get_message_file()[source]
Get the file stream or file like object for dgenerates normal (non error) output.
- dgenerate.messages.log(*args: Any, level=1, underline=False, underline_char='=')[source]
Write a message to dgenerates log
- Parameters:
args – args, objects that will be stringified and joined with a space
level – Log level, one of:
INFO,WARNING,ERROR,DEBUGunderline – Underline this message?
underline_char – Underline character
- Returns:
- dgenerate.messages.pop_level()[source]
Pop
LEVELvalue last saved bypush_level()and assign it toLEVEL.If no previous level was saved, no-op.
- dgenerate.messages.push_level(level)[source]
Set
LEVELand save the previous value to a stack.- Parameters:
level – one of
INFO,WARNING,ERROR, ,DEBUG
- dgenerate.messages.remove_logging_handler(callback: Callable[[ParamSpecArgs, int, bool, str], None])[source]
Remove a logging handler callback by reference.
- Parameters:
callback – The previously registered callback
- dgenerate.messages.set_error_file(file: TextIO)[source]
Set a file stream or file like object for dgenerates error output.
- Parameters:
file – The file stream
- dgenerate.messages.set_message_file(file: TextIO)[source]
Set a file stream or file like object for dgenerates normal (non error) output.
- Parameters:
file – The file stream
- dgenerate.messages.AUTO_FLUSH_MESSAGES = True
Whether to auto flush the output stream when printing to
stdoutor the output file assigned withset_message_file().Errors are printed to
stderrwhich is unbuffered by default.
- dgenerate.messages.DEBUG = 8
Log Level
DEBUG
- dgenerate.messages.ERROR = 4
Log Level
ERROR
- dgenerate.messages.INFO = 1
Log level
INFO
- dgenerate.messages.LEVEL = 1
Current Log Level (set-able)
Setting to
INFOmeans print all messages exceptDEBUGmessages.Setting to
ERRORmeans only printERRORmessages.Setting to
WARNINGmeans only printWARNINGmessages.Setting to
DEBUGmeans print every message.Levels are a bitfield, so you can set:
LEVEL = WARNING | ERRORetc.
- dgenerate.messages.WARNING = 2
Log Level
WARNING
dgenerate.pipelinewrapper module
huggingface diffusers pipeline wrapper / driver interface.
All functionality needed from the diffusers library is behind this interface.
- dgenerate.pipelinewrapper.CACHE_MEMORY_CONSTRAINTS = ['used_percent > 70']
Cache constraint expressions for when to clear all model caches (DiffusionPipeline, VAE, ControlNet, and Text Encoder), syntax provided via
dgenerate.memory.memory_constraints()If any of these constraints are met, a call to
enforce_cache_constraints()will callclear_model_cache()and force a garbage collection.
- dgenerate.pipelinewrapper.PIPELINE_CACHE_MEMORY_CONSTRAINTS = ['pipeline_size > (available * 0.75)']
Cache constraint expressions for when to clear the DiffusionPipeline cache, syntax provided via
dgenerate.memory.memory_constraints()If any of these constraints are met, a call to
enforce_pipeline_cache_constraints()will callclear_pipeline_cache()and force a garbage collection.Extra variables include:
cache_size(the current estimated cache size in bytes), andpipeline_size(the estimated size of the new pipeline before it is brought into memory, in bytes)
- dgenerate.pipelinewrapper.UNET_CACHE_MEMORY_CONSTRAINTS = ['unet_size > (available * 0.75)']
Cache constraint expressions for when to clear UNet cache, syntax provided via
dgenerate.memory.memory_constraints()If any of these constraints are met, a call to
enforce_unet_cache_constraints()will callclear_unet_cache()and force a garbage collection.Extra variables include:
cache_size(the current estimated cache size in bytes), andunet_size(the estimated size of the new UNet before it is brought into memory, in bytes)
- dgenerate.pipelinewrapper.VAE_CACHE_MEMORY_CONSTRAINTS = ['vae_size > (available * 0.75)']
Cache constraint expressions for when to clear VAE cache, syntax provided via
dgenerate.memory.memory_constraints()If any of these constraints are met, a call to
enforce_vae_cache_constraints()will callclear_vae_cache()and force a garbage collection.Extra variables include:
cache_size(the current estimated cache size in bytes), andvae_size(the estimated size of the new VAE before it is brought into memory, in bytes)
- dgenerate.pipelinewrapper.CONTROL_NET_CACHE_MEMORY_CONSTRAINTS = ['control_net_size > (available * 0.75)']
Cache constraint expressions for when to clear the ControlNet cache, syntax provided via
dgenerate.memory.memory_constraints()If any of these constraints are met, a call to
enforce_control_net_cache_constraints()will callclear_control_net_cache()and force a garbage collection.Extra variables include:
cache_size(the current estimated cache size in bytes), andcontrol_net_size(the estimated size of the new ControlNet before it is brought into memory, in bytes)
- dgenerate.pipelinewrapper.TEXT_ENCODER_CACHE_MEMORY_CONSTRAINTS = ['text_encoder_size > (available * 0.75)']
Cache constraint expressions for when to clear the Text Encoder cache, syntax provided via
dgenerate.memory.memory_constraints()If any of these constraints are met, a call to
enforce_text_encoder_cache_constraints()will callclear_text_encoder_cache()and force a garbage collection.Extra variables include:
cache_size(the current estimated cache size in bytes), andtext_encoder_size(the estimated size of the new Text Encoder before it is brought into memory, in bytes)
- dgenerate.pipelinewrapper.DEFAULT_INFERENCE_STEPS = 30
Default value for inference steps.
- dgenerate.pipelinewrapper.DEFAULT_GUIDANCE_SCALE = 5
Default value for guidance scale.
- dgenerate.pipelinewrapper.DEFAULT_IMAGE_SEED_STRENGTH = 0.8
Default image seed strength for img2img.
- dgenerate.pipelinewrapper.DEFAULT_IMAGE_GUIDANCE_SCALE = 1.5
Default image guidance scale for pix2pix.
- dgenerate.pipelinewrapper.DEFAULT_SDXL_HIGH_NOISE_FRACTION = 0.8
Default SDXL high noise fraction.
- dgenerate.pipelinewrapper.DEFAULT_X4_UPSCALER_NOISE_LEVEL = 20
Default x4 upscaler noise level.
- dgenerate.pipelinewrapper.DEFAULT_FLOYD_SUPERRESOLUTION_NOISE_LEVEL = 250
Default noise level for floyd super resolution upscalers.
- dgenerate.pipelinewrapper.DEFAULT_FLOYD_SUPERRESOLUTION_IMG2IMG_NOISE_LEVEL = 250
Default noise level for floyd super resolution upscalers when preforming img2img.
- dgenerate.pipelinewrapper.DEFAULT_FLOYD_SUPERRESOLUTION_INPAINT_NOISE_LEVEL = 0
Default noise level for floyd super resolution upscalers when inpainting.
- dgenerate.pipelinewrapper.DEFAULT_OUTPUT_WIDTH = 512
Default output width for txt2img.
- dgenerate.pipelinewrapper.DEFAULT_OUTPUT_HEIGHT = 512
Default output height for txt2img.
- dgenerate.pipelinewrapper.DEFAULT_SDXL_OUTPUT_WIDTH = 1024
Default output width for SDXL txt2img.
- dgenerate.pipelinewrapper.DEFAULT_SDXL_OUTPUT_HEIGHT = 1024
Default output height for SDXL txt2img.
- dgenerate.pipelinewrapper.DEFAULT_FLOYD_IF_OUTPUT_WIDTH = 64
Default output width for Deep Floyd IF txt2img first stage.
- dgenerate.pipelinewrapper.DEFAULT_FLOYD_IF_OUTPUT_HEIGHT = 64
Default output height for Deep Floyd IF txt2img first stage.
- dgenerate.pipelinewrapper.DEFAULT_SEED = 0
Default RNG seed.
- dgenerate.pipelinewrapper.DEFAULT_S_CASCADE_DECODER_GUIDANCE_SCALE = 0
Default guidance scale for the Stable Cascade decoder.
- dgenerate.pipelinewrapper.DEFAULT_S_CASCADE_DECODER_INFERENCE_STEPS = 10
Default inference steps for the Stable Cascade decoder.
- dgenerate.pipelinewrapper.DEFAULT_S_CASCADE_OUTPUT_HEIGHT = 1024
Default output height for Stable Cascade.
- dgenerate.pipelinewrapper.DEFAULT_S_CASCADE_OUTPUT_WIDTH = 1024
Default output width for Stable Cascade.
- dgenerate.pipelinewrapper.DEFAULT_SD3_OUTPUT_HEIGHT = 1024
Default output height for Stable Diffusion 3.
- dgenerate.pipelinewrapper.DEFAULT_SD3_OUTPUT_WIDTH = 1024
Default output width for Stable Diffusion 3.
huggingface diffusers pipeline wrapper / driver interface.
All functionality needed from the diffusers library is behind this interface.
- exception dgenerate.pipelinewrapper.ArgumentHelpException[source]
Bases:
ExceptionNot an error, runtime argument help was requested by passing “help” or a special value to an argument of
DiffusionPipelineWrapper.__init__()which supports a help query.
- exception dgenerate.pipelinewrapper.ControlNetUriLoadError[source]
Bases:
ModelUriLoadErrorError while loading model file in
--control-netsuri
- exception dgenerate.pipelinewrapper.InvalidControlNetUriError[source]
Bases:
InvalidModelUriErrorError in
--control-netsuri
- exception dgenerate.pipelinewrapper.InvalidDeviceOrdinalException[source]
Bases:
ExceptionGPU in device specification (cuda:N) does not exist
- exception dgenerate.pipelinewrapper.InvalidLoRAUriError[source]
Bases:
InvalidModelUriErrorError in
--lorasuri
- exception dgenerate.pipelinewrapper.InvalidModelFileError[source]
Bases:
ExceptionRaised when a file is loaded from disk that is an invalid diffusers model format.
This indicates that was a problem loading the primary diffusion model, This could also refer to an SDXL refiner model or Stable Cascade decoder model which are considered primary models.
- exception dgenerate.pipelinewrapper.InvalidModelUriError[source]
Bases:
ExceptionThrown on model path syntax or logical usage error
- exception dgenerate.pipelinewrapper.InvalidSDXLRefinerUriError[source]
Bases:
InvalidModelUriErrorError in
--sdxl-refineruri
- exception dgenerate.pipelinewrapper.InvalidSchedulerNameError[source]
Bases:
ExceptionUnknown scheduler name used
- exception dgenerate.pipelinewrapper.InvalidTextualInversionUriError[source]
Bases:
InvalidModelUriErrorError in
--textual-inversionsuri
- exception dgenerate.pipelinewrapper.InvalidUNetUriError[source]
Bases:
InvalidModelUriErrorError in
--uneturi
- exception dgenerate.pipelinewrapper.InvalidVaeUriError[source]
Bases:
InvalidModelUriErrorError in
--vaeuri
- exception dgenerate.pipelinewrapper.LoRAUriLoadError[source]
Bases:
ModelUriLoadErrorError while loading model file in
--lorasuri
- exception dgenerate.pipelinewrapper.ModelNotFoundError[source]
Bases:
ExceptionRaised when a specified model can not be located either locally or remotely
- exception dgenerate.pipelinewrapper.ModelUriLoadError[source]
Bases:
ExceptionThrown when model fails to load from a URI for a reason other than not being found, such as being unsupported.
This exception refers to loadable sub models such as VAEs, LoRAs, ControlNets, Textual Inversions etc.
- exception dgenerate.pipelinewrapper.SchedulerHelpException[source]
Bases:
ArgumentHelpExceptionNot an error, runtime scheduler help was requested by passing “help” to a scheduler name argument of
DiffusionPipelineWrapper.__init__()such asschedulerorsdxl_refiner_scheduler. Upon callingDiffusionPipelineWrapper.__call__()info was printed usingdgenerate.messages.log(), then this exception raised to get out of the call stack.
- exception dgenerate.pipelinewrapper.TextEncodersHelpException[source]
Bases:
ArgumentHelpExceptionNot an error, runtime text encoder help was requested by passing “help” to a text encoder URI argument of
DiffusionPipelineWrapper.__init__()such astext_encoder_urisorsecond_text_encoder_uris. Upon callingDiffusionPipelineWrapper.__call__()info was printed usingdgenerate.messages.log(), then this exception raised to get out of the call stack.
- exception dgenerate.pipelinewrapper.TextualInversionUriLoadError[source]
Bases:
ModelUriLoadErrorError while loading model file in
--textual-inversionsuri
- exception dgenerate.pipelinewrapper.UNetUriLoadError[source]
Bases:
ModelUriLoadErrorError while loading model file in
--unet / --unet2uri
- exception dgenerate.pipelinewrapper.UnsupportedPipelineConfigError[source]
Bases:
ExceptionOccurs when the a diffusers pipeline is requested to be configured in a way that is unsupported by that pipeline.
- exception dgenerate.pipelinewrapper.VAEUriLoadError[source]
Bases:
ModelUriLoadErrorError while loading model file in
--vaeuri
- class dgenerate.pipelinewrapper.DataType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
EnumRepresents model precision
- AUTO = 0
Auto selection.
- BFLOAT16 = 3
16 bit brain floating point.
- FLOAT16 = 1
16 bit floating point.
- FLOAT32 = 2
32 bit floating point.
- class dgenerate.pipelinewrapper.DiffusionArguments[source]
Bases:
SetFromMixinRepresents all possible arguments for a
DiffusionPipelineWrappercall.- describe_pipeline_wrapper_args() str[source]
Describe the pipeline wrapper arguments in a pretty, human-readable way, with word wrapping depending on console size or a maximum length depending on what stdout currently is.
- Returns:
description string.
- determine_pipeline_type()[source]
Determine the
dgenerate.pipelinewrapper.PipelineTypeneeded to utilize these arguments.
- get_pipeline_wrapper_kwargs()[source]
Get the arguments dictionary needed to call
DiffusionPipelineWrapper- Returns:
dictionary of argument names with values
- batch_size: int | None = None
Number of images to produce in a single generation step on the same GPU.
Invalid to use with FLAX ModeTypes.
- clip_skip: int | None = None
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that the output of the pre-final layer will be used for computing the prompt embeddings. Only supported for
model_typevaluestorchandtorch-sdxl, including withcontrol_net_urisdefined.
- control_images: Sequence[Image] | None = None
ControlNet guidance images to use if
control_net_uriswere given to the constructor ofDiffusionPipelineWrapper.All input images involved in a generation must match in dimension and be aligned by 8 pixels.
- floyd_image: Image | None = None
The output image of the last stage when preforming img2img or inpainting generation with Deep Floyd. When preforming txt2img generation
DiffusionArguments.imageis used.
- guidance_rescale: float | None = None
This value is only supported for certain
dgenerate.pipelinewrapper.DiffusionPipelineWrapperconfigurations, an error will be produced when it is unsupported.Guidance rescale factor proposed by [Common Diffusion Noise Schedules and Sample Steps are Flawed](https://arxiv.org/pdf/2305.08891.pdf) guidance_scale is defined as φ in equation 16. of [Common Diffusion Noise Schedules and Sample Steps are Flawed](https://arxiv.org/pdf/2305.08891.pdf). Guidance rescale factor should fix overexposure when using zero terminal SNR.
- guidance_scale: float | None = None
A higher guidance scale value encourages the model to generate images closely linked to the text
DiffusionArguments.promptat the expense of lower image quality. Guidance scale is enabled whenDiffusionArguments.guidance_scale> 1
- height: int | None = None
Output image height.
Ignored when img2img, inpainting, or controlnet guidance images are involved.
Width will be the width of the input image in those cases.
Output image width, must be aligned by 8
- image: Image | None = None
Image for img2img operations, or the base for inpainting operations.
All input images involved in a generation must match in dimension and be aligned by 8 pixels.
- image_guidance_scale: float | None = None
This value is only relevant for
pix2pixdgenerate.pipelinewrapper.ModelType.Image guidance scale is to push the generated image towards the initial image
DiffusionArguments.image. Image guidance scale is enabled by settingDiffusionArguments.image_guidance_scale> 1. Higher image guidance scale encourages to generate images that are closely linked to the source imageDiffusionArguments.image, usually at the expense of lower image quality.
- image_seed_strength: float | None = None
Image seed strength, which relates to how much an img2img source (image attribute) is used during generation. Between 0.001 (close to zero but not 0) and 1.0, the closer to 1.0 the less the image is used for generation, IE. the more creative freedom the AI has.
- inference_steps: int | None = None
The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
- mask_image: Image | None = None
Mask image for inpainting operations.
All input images involved in a generation must match in dimension and be aligned by 8 pixels.
- s_cascade_decoder_guidance_scale: float | None = None
Guidance scale value for the Stable Cascade decoder, this corresponds to the
--s-cascade-decoder-guidance-scalesargument of the dgenerate command line tool.
- s_cascade_decoder_inference_steps: int | None = None
Inference steps value for the Stable Cascade decoder, this corresponds to the
--s-cascade-decoder-inference-stepsargument of the dgenerate command line tool.
- s_cascade_decoder_prompt: Prompt | None = None
Primary prompt for the Stable Cascade decoder when a decoder URI is specified in the constructor of
DiffusionPipelineWrapper. Usually the prompt attribute of this object is used, unless you override it by giving this attribute a value.
- sd3_max_sequence_length: int | None = None
Max number of prompt tokens that the T5EncoderModel (text encoder 3) of Stable Diffusion can handle.
This defaults to 256 when not specified, and the maximum value is 512 and the minimum value is 1.
High values result in more resource usage and processing time.
- sd3_second_prompt: Prompt | None = None
Secondary prompt for the SD3 main pipeline. Usually the prompt attribute of this object is used, unless you override it by giving this attribute a value.
- sd3_third_prompt: Prompt | None = None
Tertiary (T5) prompt for the SD3 main pipeline. Usually the prompt attribute of this object is used, unless you override it by giving this attribute a value.
- sdxl_aesthetic_score: float | None = None
Optional, defaults to 6.0. This argument is used for img2img and inpainting operations only Used to simulate an aesthetic score of the generated image by influencing the positive text condition. Part of SDXL’s micro-conditioning as explained in section 2.2 of [https://huggingface.co/papers/2307.01952](https://huggingface.co/papers/2307.01952).
- sdxl_crops_coords_top_left: tuple[int, int] | None = None
Optional SDXL conditioning parameter.
DiffusionArguments.sdxl_crops_coords_top_leftcan be used to generate an image that appears to be “cropped” from the positionDiffusionArguments.sdxl_crops_coords_top_leftdownwards. Favorable, well-centered images are usually achieved by settingDiffusionArguments.sdxl_crops_coords_top_leftto (0, 0). Part of SDXL’s micro-conditioning as explained in section 2.2 of [https://huggingface.co/papers/2307.01952](https://huggingface.co/papers/2307.01952).
- sdxl_high_noise_fraction: float | None = None
SDXL high noise fraction. This proportion of timesteps/inference steps are handled by the primary model, while the inverse proportion is handled by the refiner model when an SDXL model_type value.
When the refiner is operating in edit mode the number of total inference steps for the refiner will be calculated in a different manner, currently the refiner operates in edit mode during generations involving ControlNets as well as inpainting.
In edit mode, the refiner uses img2img with an image seed strength to add details to the image instead of cooperative denoising, this image seed strength is calculated as (1.0 -
DiffusionArguments.sdxl_high_noise_fraction), and the number of inference steps for the refiner is then calculated as (image_seed_strength * inference_steps).
- sdxl_negative_aesthetic_score: float | None = None
Negative influence version of
DiffusionArguments.sdxl_aesthetic_score
- sdxl_negative_crops_coords_top_left: tuple[int, int] | None = None
Negative influence version of
DiffusionArguments.sdxl_crops_coords_top_left
- sdxl_negative_original_size: tuple[int, int] | None = None
This value is only supported for certain
dgenerate.pipelinewrapper.DiffusionPipelineWrapperconfigurations, an error will be produced when it is unsupported. It is not known to be supported bypix2pix.Optional SDXL conditioning parameter. To negatively condition the generation process based on a specific image resolution. Part of SDXL’s micro-conditioning as explained in section 2.2 of [https://huggingface.co/papers/2307.01952](https://huggingface.co/papers/2307.01952). For more information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208.
- sdxl_negative_target_size: tuple[int, int] | None = None
This value is only supported for certain
dgenerate.pipelinewrapper.DiffusionPipelineWrapperconfigurations, an error will be produced when it is unsupported. It is not known to be supported bypix2pix.Optional SDXL conditioning parameter. To negatively condition the generation process based on a target image resolution. It should be as same as the
DiffusionArguments.target_sizefor most cases. Part of SDXL’s micro-conditioning as explained in section 2.2 of [https://huggingface.co/papers/2307.01952](https://huggingface.co/papers/2307.01952). For more information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208.
- sdxl_original_size: tuple[int, int] | None = None
Optional SDXL conditioning parameter. If
DiffusionArguments.sdxl_original_sizeis not the same asDiffusionArguments.sdxl_target_sizethe image will appear to be down- or up-sampled.DiffusionArguments.sdxl_original_sizedefaults to(width, height)if not specified or the size of any input images provided. Part of SDXL’s micro-conditioning as explained in section 2.2 of [https://huggingface.co/papers/2307.01952](https://huggingface.co/papers/2307.01952).
- sdxl_refiner_aesthetic_score: float | None = None
Override the refiner value usually taken from
DiffusionArguments.sdxl_aesthetic_score
- sdxl_refiner_clip_skip: int | None = None
Clip skip override value for the SDXL refiner, which normally defaults to that of
DiffusionArguments.clip_skipwhen it is defined.
- sdxl_refiner_crops_coords_top_left: tuple[int, int] | None = None
Override the refiner value usually taken from
DiffusionArguments.sdxl_crops_coords_top_left
- sdxl_refiner_edit: bool | None = None
Force the SDXL refiner to operate in edit mode instead of cooperative denoising mode.
- sdxl_refiner_guidance_rescale: float | None = None
Override the guidance rescale value used by the SDXL refiner, which is normally set to the value of
DiffusionArguments.guidance_rescale.
- sdxl_refiner_guidance_scale: float | None = None
Override the guidance scale used by the SDXL refiner, which is normally set to the value of
DiffusionArguments.guidance_scale.
- sdxl_refiner_inference_steps: int | None = None
Override the default amount of inference steps preformed by the SDXL refiner. Which is normally set to the value for the primary model.
The attribute
DiffusionArguments.sdxl_high_noise_fractionstill factors in to the actual amount of inference steps preformed.
- sdxl_refiner_negative_aesthetic_score: float | None = None
Override the refiner value usually taken from
DiffusionArguments.sdxl_negative_aesthetic_score
- sdxl_refiner_negative_crops_coords_top_left: tuple[int, int] | None = None
Override the refiner value usually taken from
DiffusionArguments.sdxl_negative_crops_coords_top_left
- sdxl_refiner_negative_original_size: tuple[int, int] | None = None
Override the refiner value usually taken from
DiffusionArguments.sdxl_negative_original_size
- sdxl_refiner_negative_target_size: tuple[int, int] | None = None
Override the refiner value usually taken from
DiffusionArguments.sdxl_negative_target_size
- sdxl_refiner_original_size: tuple[int, int] | None = None
Override the refiner value usually taken from
DiffusionArguments.sdxl_original_size
- sdxl_refiner_prompt: Prompt | None = None
Primary prompt for the SDXL refiner when a refiner URI is specified in the constructor of
DiffusionPipelineWrapper. Usually the prompt attribute of this object is used, unless you override it by giving this attribute a value.
- sdxl_refiner_second_prompt: Prompt | None = None
Secondary prompt for the SDXL refiner when a refiner URI is specified in the constructor of
DiffusionPipelineWrapper. Usually the sdxl_refiner_prompt attribute of this object is used, unless you override it by giving this attribute a value.
- sdxl_refiner_target_size: tuple[int, int] | None = None
Override the refiner value usually taken from
DiffusionArguments.sdxl_target_size
- sdxl_second_prompt: Prompt | None = None
Secondary prompt for the SDXL main pipeline when a refiner URI is specified in the constructor of
DiffusionPipelineWrapper. Usually the prompt attribute of this object is used, unless you override it by giving this attribute a value.
- sdxl_target_size: tuple[int, int] | None = None
Optional SDXL conditioning parameter. For most cases,
DiffusionArguments.sdxl_target_sizeshould be set to the desired height and width of the generated image. If not specified it will default to(width, height)or the size of any input images provided. Part of SDXL’s micro-conditioning as explained in section 2.2 of [https://huggingface.co/papers/2307.01952](https://huggingface.co/papers/2307.01952).
- seed: int | None = None
An integer to serve as an RNG seed.
- upscaler_noise_level: int | None = None
Upscaler noise level for the
dgenerate.pipelinewrapper.ModelType.TORCH_UPSCALER_X4model type only.
- width: int | None = None
Output image width.
Ignored when img2img, inpainting, or controlnet guidance images are involved.
Width will be the width of the input image in those cases.
Output image width, must be aligned by 8
- class dgenerate.pipelinewrapper.DiffusionPipelineWrapper(model_path: str, model_type: ModelType | str = ModelType.TORCH, revision: str | None = None, variant: str | None = None, subfolder: str | None = None, dtype: DataType | str = DataType.AUTO, unet_uri: str | None = None, second_unet_uri: str | None = None, vae_uri: str | None = None, vae_tiling: bool = False, vae_slicing: bool = False, lora_uris: Sequence[str] | None = None, textual_inversion_uris: Sequence[str] | None = None, text_encoder_uris: Sequence[str] | None = None, second_text_encoder_uris: Sequence[str] | None = None, control_net_uris: Sequence[str] | None = None, scheduler: str | None = None, sdxl_refiner_uri: str | None = None, sdxl_refiner_scheduler: str | None = None, s_cascade_decoder_uri: str | None = None, s_cascade_decoder_scheduler: str | None = None, device: str = 'cuda', safety_checker: bool = False, auth_token: str | None = None, local_files_only: bool = False, model_extra_modules=None, second_model_extra_modules=None, model_cpu_offload=False, model_sequential_offload: bool = False, sdxl_refiner_cpu_offload: bool = False, sdxl_refiner_sequential_offload: bool = False, s_cascade_decoder_cpu_offload: bool = False, s_cascade_decoder_sequential_offload: bool = False, prompt_weighter_uri: str | None = None, prompt_weighter_loader: PromptWeighterLoader | None = None)[source]
Bases:
objectMonolithic diffusion pipelines wrapper.
- __call__(args: DiffusionArguments | None = None, **kwargs) PipelineWrapperResult[source]
Call the pipeline and generate a result.
- Parameters:
args – Optional
DiffusionArgumentskwargs – See
DiffusionArguments.get_pipeline_wrapper_kwargs(), any keyword arguments given here will override values derived from theDiffusionArgumentsobject given to the args parameter.
- Raises:
- Returns:
- __init__(model_path: str, model_type: ModelType | str = ModelType.TORCH, revision: str | None = None, variant: str | None = None, subfolder: str | None = None, dtype: DataType | str = DataType.AUTO, unet_uri: str | None = None, second_unet_uri: str | None = None, vae_uri: str | None = None, vae_tiling: bool = False, vae_slicing: bool = False, lora_uris: Sequence[str] | None = None, textual_inversion_uris: Sequence[str] | None = None, text_encoder_uris: Sequence[str] | None = None, second_text_encoder_uris: Sequence[str] | None = None, control_net_uris: Sequence[str] | None = None, scheduler: str | None = None, sdxl_refiner_uri: str | None = None, sdxl_refiner_scheduler: str | None = None, s_cascade_decoder_uri: str | None = None, s_cascade_decoder_scheduler: str | None = None, device: str = 'cuda', safety_checker: bool = False, auth_token: str | None = None, local_files_only: bool = False, model_extra_modules=None, second_model_extra_modules=None, model_cpu_offload=False, model_sequential_offload: bool = False, sdxl_refiner_cpu_offload: bool = False, sdxl_refiner_sequential_offload: bool = False, s_cascade_decoder_cpu_offload: bool = False, s_cascade_decoder_sequential_offload: bool = False, prompt_weighter_uri: str | None = None, prompt_weighter_loader: PromptWeighterLoader | None = None)[source]
This is a monolithic wrapper around all supported diffusion pipelines which handles txt2img, img2img, and inpainting on demand. It spins up the correct pipelines as needed in order to handle provided pipeline arguments using lazy initialization.
Pipelines and user specified sub models are memoized and their lifetimes are managed via heuristics based on system memory and available resources.
All arguments to this constructor should be provided as keyword arguments, using this constructor in any other fashion could result in breakage inbetween semver compatible versions.
- Parameters:
model_path – main model path
model_type – main model type
revision – main model revision
variant – main model variant
subfolder – main model subfolder (huggingface or disk)
dtype – main model dtype
unet_uri – main model UNet URI string
second_unet_uri – secondary model unet uri (SDXL Refiner, Stable Cascade decoder)
vae_uri – main model VAE URI string
vae_tiling – use VAE tiling?
vae_slicing – use VAE slicing?
lora_uris – One or more LoRA URI strings
textual_inversion_uris – One or more Textual Inversion URI strings
text_encoder_uris – One or more Text Encoder URIs (“+”, or None for default. Or “null” indicating do not load) for the main model
second_text_encoder_uris – One or more Text Encoder URIs (“+”, or None for default. Or “null” indicating do not load) for the secondary model (SDXL Refiner or Stable Cascade decoder)
control_net_uris – One or more ControlNet URI strings
scheduler – Scheduler URI string for the main model
sdxl_refiner_uri – SDXL Refiner model URI string
sdxl_refiner_scheduler – Scheduler URI string for the SDXL Refiner
s_cascade_decoder_uri – Stable Cascade decoder URI string
s_cascade_decoder_scheduler – Scheduler URI string for the Stable Cascade decoder
device – Rendering device string, example:
cuda:0orcudasafety_checker – Use safety checker model if available? (antiquated, for SD 1/2, Deep Floyd etc.)
auth_token – huggingface authentication token.
local_files_only – Do not attempt to download files from huggingface?
model_extra_modules – Raw extra diffusers modules for the main pipeline
second_model_extra_modules – Raw extra diffusers modules for the secondary pipeline (SDXL Refiner, Stable Cascade decoder)
model_cpu_offload – Use model CPU offloading for the main pipeline via the accelerate module?
model_sequential_offload – Use sequential CPU offloading for the main pipeline via the accelerate module?
sdxl_refiner_cpu_offload – Use CPU offloading for the SDXL Refiner via the accelerate module?
sdxl_refiner_sequential_offload – Use sequential CPU offloading for the SDXL Refiner via the accelerate module?
s_cascade_decoder_cpu_offload – Use CPU offloading for the Stable Cascade decoder via the accelerate module?
s_cascade_decoder_sequential_offload – Use sequential CPU offloading for the Stable Cascade decoder via the accelerate module?
prompt_weighter_uri – Prompt weighter implementation URI, to be loaded from
prompt_weighter_loaderprompt_weighter_loader – Plugin loader for prompt weighter implementations, if you pass
Nonea default instance will be created.
- gen_dgenerate_command(args: DiffusionArguments | None = None, extra_opts: Sequence[tuple[str] | tuple[str, Any]] | None = None, omit_device=False, **kwargs)[source]
Generate a valid dgenerate command line invocation that reproduces this result.
- Parameters:
args –
DiffusionArgumentsobject to take values fromextra_opts – Extra option pairs to be added to the end of reconstructed options of the dgenerate invocation, this should be a sequence of tuples of length 1 (switch only) or length 2 (switch with args)
omit_device – Omit the
--deviceoption? For a shareable configuration it might not make sense to include the device specification. And instead simply fallback to whatever the default device is, which is generallycudakwargs – pipeline wrapper keyword arguments, these will override values derived from any
DiffusionArgumentsobject given to the args argument. See:DiffusionArguments.get_pipeline_wrapper_kwargs
- Returns:
A string containing the dgenerate command line needed to reproduce this result.
- gen_dgenerate_config(args: DiffusionArguments | None = None, extra_opts: Sequence[tuple[str] | tuple[str, Any]] | None = None, extra_comments: Iterable[str] | None = None, omit_device: bool = False, **kwargs)[source]
Generate a valid dgenerate config file with a single invocation that reproduces this result.
- Parameters:
args –
DiffusionArgumentsobject to take values fromextra_opts – Extra option pairs to be added to the end of reconstructed options of the dgenerate invocation, this should be a sequence of tuples of length 1 (switch only) or length 2 (switch with args)
extra_comments – Extra strings to use as comments after the initial version check directive
omit_device – Omit the
--deviceoption? For a shareable configuration it might not make sense to include the device specification. And instead simply fallback to whatever the default device is, which is generallycudakwargs – pipeline wrapper keyword arguments, these will override values derived from any
DiffusionArgumentsobject given to the args argument. See:DiffusionArguments.get_pipeline_wrapper_kwargs
- Returns:
The configuration as a string
- recall_main_pipeline() PipelineCreationResult[source]
Fetch the last used main pipeline creation result, possibly the pipeline will be recreated if no longer in the in memory cache. If there is no pipeline currently created, which will be the case if an image was never generated yet,
RuntimeErrorwill be raised.- Raises:
RuntimeError –
- Returns:
- recall_refiner_pipeline() PipelineCreationResult[source]
Fetch the last used refiner pipeline creation result, possibly the pipeline will be recreated if no longer in the in memory cache. If there is no refiner pipeline currently created, which will be the case if an image was never generated yet or a refiner model was not specified,
RuntimeErrorwill be raised.- Raises:
RuntimeError –
- Returns:
- reconstruct_dgenerate_opts(args: DiffusionArguments | None = None, extra_opts: Sequence[tuple[str] | tuple[str, Any]] | None = None, omit_device=False, shell_quote=True, **kwargs) list[tuple[str] | tuple[str, Any]][source]
Reconstruct dgenerates command line arguments from a particular set of pipeline wrapper call arguments.
- Parameters:
args –
DiffusionArgumentsobject to take values fromextra_opts – Extra option pairs to be added to the end of reconstructed options, this should be a sequence of tuples of length 1 (switch only) or length 2 (switch with args)
omit_device – Omit the
--deviceoption? For a shareable configuration it might not make sense to include the device specification. And instead simply fallback to whatever the default device is, which is generallycudashell_quote – Shell quote and format the argument values? or return them raw.
kwargs – pipeline wrapper keyword arguments, these will override values derived from any
DiffusionArgumentsobject given to the args argument. See:DiffusionArguments.get_pipeline_wrapper_kwargs
- Returns:
List of tuples of length 1 or 2 representing the option
- property auth_token: str | None
Current
--auth-tokenvalue orNone
- property control_net_uris: Sequence[str] | None
List of supplied
--control-netsuri strings or an empty list
- property device: str
Currently set
--devicestring
- property dtype_string: str
Currently set
--dtypestring value for the main model
- property local_files_only: bool
Currently set value for
local_files_only
- property lora_uris: Sequence[str] | None
List of supplied
--lorasuri strings or an empty list
- property model_cpu_offload: bool
Current
--model-cpu-offloadvalue
- property model_path: str
Model path for the main model
- property model_sequential_offload: bool
Current
--model-sequential-offloadvalue
- property model_type_string: str
Currently set
--model-typestring value
- property prompt_weighter_loader: PromptWeighterLoader
Current prompt weighter loader
- property prompt_weighter_uri: str | None
Current prompt weighter implementation uri
- property revision: str | None
Currently set
--revisionfor the main model orNone
- property s_cascade_decoder_cpu_offload: bool
Current
--s-cascade-decoder-cpu-offloadvalue
- property s_cascade_decoder_scheduler: str | None
Selected scheduler URI for the Stable Cascade decoder or
None
- property s_cascade_decoder_sequential_offload: bool
Current
--s-cascade-decoder-sequential-offloadvalue
- property s_cascade_decoder_uri: str | None
Model URI for the Stable Cascade decoder or
None
- property safety_checker: bool
Safety checker enabled status
- property scheduler: str | None
Selected scheduler URI for the main model or
None
- property sdxl_refiner_cpu_offload: bool
Current
--sdxl-refiner-cpu-offloadvalue
- property sdxl_refiner_scheduler: str | None
Selected scheduler URI for the SDXL refiner or
None
- property sdxl_refiner_sequential_offload: bool
Current
--sdxl-refiner-sequential-offloadvalue
- property sdxl_refiner_uri: str | None
Model URI for the SDXL refiner or
None
- property second_text_encoder_uris: Sequence[str] | None
List of supplied
--text-encoders2uri strings or an empty list
- property second_unet_uri: str | None
Selected
--unet2uri for the SDXL refiner or Stable Cascade decoder model orNone
- property subfolder: str | None
Selected model
--subfolderfor the main model, (remote repo subfolder or local) orNone
- property text_encoder_uris: Sequence[str] | None
List of supplied
--text-encodersuri strings or an empty list
- property textual_inversion_uris: Sequence[str] | None
List of supplied
--textual-inversionsuri strings or an empty list
- property unet_uri: str | None
Selected
--uneturi for the main model orNone
- property vae_slicing: bool
Current
--vae-slicingstatus
- property vae_tiling: bool
Current
--vae-tilingstatus
- property vae_uri: str | None
Selected
--vaeuri for the main model orNone
- property variant: str | None
Currently set
--variantfor the main model orNone
- class dgenerate.pipelinewrapper.FlaxControlNetUri(model: str, revision: str | None = None, subfolder: str | None = None, dtype: DataType | str | None = None, scale: float = 1.0, from_torch: bool = False)[source]
Bases:
objectRepresentation of
--control-netsuri when--model-typeflax*- __init__(model: str, revision: str | None = None, subfolder: str | None = None, dtype: DataType | str | None = None, scale: float = 1.0, from_torch: bool = False)[source]
- Parameters:
model – model path
revision – model revision (branch)
subfolder – model subfolder
dtype – data type (precision)
scale – control net scale value
from_torch – load from a model designed for torch?
- Raises:
InvalidControlNetUriError – If the
modelpath represents a singular file (not supported), or ifdtypeis passed an invalid string
- load(dtype_fallback: DataType = DataType.AUTO, use_auth_token: str | None = None, local_files_only: bool = False) tuple[FlaxControlNetModel, Any][source]
Load a
diffusers.FlaxControlNetModelfrom this URI.- Parameters:
dtype_fallback – Fallback datatype if
dtypewas not specified in the URI.use_auth_token – Optional huggingface API auth token, used for downloading restricted repos that your account has access to.
local_files_only – Avoid connecting to huggingface to download models and only use cached models?
- Raises:
ModelNotFoundError – If the model could not be found.
- Returns:
tuple (
diffusers.FlaxControlNetModel, flax_control_net_params)
- static parse(uri: str) FlaxControlNetUri[source]
Parse a
--model-typeflax*--control-netsuri specification and return an object representing its constituents- Parameters:
uri – string with
--control-netsuri syntax- Raises:
- Returns:
FlaxControlNetPath
- property from_torch: bool
Load from a model format meant for torch?
- property model: str
Model path, huggingface slug
- property revision: str | None
Model repo revision
- property scale: float
ControlNet guidance scale
- property subfolder: str | None
Model repo subfolder
- class dgenerate.pipelinewrapper.FlaxPipelineCreationResult(pipeline: FlaxDiffusionPipeline, flax_params: dict[str, Any], parsed_unet_uri: FlaxUNetUri | None, flax_unet_params: dict[str, Any] | None, parsed_vae_uri: FlaxVAEUri | None, flax_vae_params: dict[str, Any] | None, parsed_control_net_uris: Sequence[FlaxControlNetUri], flax_control_net_params: dict[str, Any] | None)[source]
Bases:
PipelineCreationResult- __init__(pipeline: FlaxDiffusionPipeline, flax_params: dict[str, Any], parsed_unet_uri: FlaxUNetUri | None, flax_unet_params: dict[str, Any] | None, parsed_vae_uri: FlaxVAEUri | None, flax_vae_params: dict[str, Any] | None, parsed_control_net_uris: Sequence[FlaxControlNetUri], flax_control_net_params: dict[str, Any] | None)[source]
- call(prompt_weighter: PromptWeighter | None = None, **kwargs) BaseOutput[source]
Call pipeline, see:
call_pipeline()- Parameters:
prompt_weighter – Optional prompt weighter for weighted prompt syntaxes
kwargs – forward kwargs to pipeline
- Returns:
A subclass of
diffusers.utils.BaseOutput
- flax_control_net_params: dict[str, Any] | None
Flax specific ControlNet params object
- flax_params: dict[str, Any]
Flax specific Pipeline params object
- flax_unet_params: dict[str, Any] | None
Flax specific UNet params object
- flax_vae_params: dict[str, Any] | None
Flax specific VAE params object
- parsed_control_net_uris: Sequence[FlaxControlNetUri]
Parsed ControlNet URIs if any were present
- parsed_unet_uri: FlaxUNetUri | None
Parsed UNet URI if one was present
- parsed_vae_uri: FlaxVAEUri | None
Parsed VAE URI if one was present
- property pipeline: FlaxDiffusionPipeline
A created subclass of
diffusers.FlaxDiffusionPipeline
- class dgenerate.pipelinewrapper.FlaxPipelineFactory(pipeline_type: PipelineType, model_path: str, model_type: ModelType = ModelType.FLAX, revision: str | None = None, subfolder: str | None = None, dtype: DataType = DataType.AUTO, unet_uri: str | None = None, vae_uri: str | None = None, control_net_uris: Sequence[str] | None = None, text_encoder_uris: Sequence[str] | None = None, scheduler: str | None = None, safety_checker: bool = False, auth_token: str | None = None, extra_modules: dict[str, Any] | None = None, local_files_only: bool = False)[source]
Bases:
objectTurns
create_flax_diffusion_pipeline()into a factory that can recreate the same Flax pipeline over again, possibly from cache.- __call__() FlaxPipelineCreationResult[source]
- __init__(pipeline_type: PipelineType, model_path: str, model_type: ModelType = ModelType.FLAX, revision: str | None = None, subfolder: str | None = None, dtype: DataType = DataType.AUTO, unet_uri: str | None = None, vae_uri: str | None = None, control_net_uris: Sequence[str] | None = None, text_encoder_uris: Sequence[str] | None = None, scheduler: str | None = None, safety_checker: bool = False, auth_token: str | None = None, extra_modules: dict[str, Any] | None = None, local_files_only: bool = False)[source]
- class dgenerate.pipelinewrapper.FlaxUNetUri(model: str, revision: str | None, subfolder: str | None, dtype: DataType | None)[source]
Bases:
objectRepresentation of
--uneturi when--model-typeflax*- __init__(model: str, revision: str | None, subfolder: str | None, dtype: DataType | None)[source]
- Parameters:
model – model path
revision – model revision (branch name)
subfolder – model subfolder
dtype – model data type (precision)
- Raises:
InvalidUNetUriError – If
modelpoints to a single file, single file loads are not supported, or ifdtypeis passed an invalid string.
- load(dtype_fallback: DataType = DataType.AUTO, use_auth_token: str | None = None, local_files_only: bool = False) tuple[FlaxUNet2DConditionModel, Any][source]
Load a
diffusers.FlaxUNet2DConditionModelUNet and its flax_params from this URI- Parameters:
dtype_fallback – If the URI does not specify a dtype, use this dtype.
use_auth_token – optional huggingface auth token.
local_files_only – avoid downloading files and only look for cached files when the model path is a huggingface slug or blob link
- Raises:
ModelNotFoundError – If the model could not be found.
- Returns:
tuple (
diffusers.FlaxUNet2DConditionModel, flax_unet_params)
- static parse(uri: str) FlaxUNetUri[source]
Parse a
--model-typeflax*--uneturi and return an object representing its constituents- Parameters:
uri – string with
--uneturi syntax- Raises:
- Returns:
- property model: str
Model path, huggingface slug
- property revision: str | None
Model repo revision
- property subfolder: str | None
Model repo subfolder
- class dgenerate.pipelinewrapper.FlaxVAEUri(encoder: str, model: str, revision: str | None, subfolder: str | None, dtype: DataType | None)[source]
Bases:
objectRepresentation of
--vaeuri when--model-typeflax*- __init__(encoder: str, model: str, revision: str | None, subfolder: str | None, dtype: DataType | None)[source]
- Parameters:
encoder – Encoder class name
model – model path
revision – model revision (branch name)
subfolder – model subfolder
dtype – model data type (precision)
- Raises:
InvalidVaeUriError – If
encoder != 'FlaxAutoencoderKL', or if themodelpath is a single file and that is not supported, or ifdtypeis passed an invalid string.
- load(dtype_fallback: DataType = DataType.AUTO, use_auth_token: str | None = None, local_files_only: bool = False) tuple[FlaxAutoencoderKL, Any][source]
Load a
diffusers.FlaxAutoencoderKLVAE and its flax_params from this URI- Parameters:
dtype_fallback – If the URI does not specify a dtype, use this dtype.
use_auth_token – optional huggingface auth token.
local_files_only – avoid downloading files and only look for cached files when the model path is a huggingface slug or blob link
- Raises:
ModelNotFoundError – If the model could not be found.
- Returns:
tuple (
diffusers.FlaxAutoencoderKL, flax_vae_params)
- static parse(uri: str) FlaxVAEUri[source]
Parse a
--model-typeflax*--vaeuri and return an object representing its constituents- Parameters:
uri – string with
--vaeuri syntax- Raises:
- Returns:
- property encoder: str
Encoder class name, “FlaxAutoencoderKL”
- property model: str
Model path, huggingface slug
- property revision: str | None
Model repo revision
- property subfolder: str | None
Model repo subfolder
- class dgenerate.pipelinewrapper.LoRAUri(model: str, revision: str | None = None, subfolder: str | None = None, weight_name: str | None = None, scale: float = 1.0)[source]
Bases:
objectRepresentation of a
--lorasuri- __init__(model: str, revision: str | None = None, subfolder: str | None = None, weight_name: str | None = None, scale: float = 1.0)[source]
- load_on_pipeline(pipeline: DiffusionPipeline, use_auth_token: str | None = None, local_files_only: bool = False)[source]
Load LoRA weights on to a pipeline using this URI
- Parameters:
pipeline –
diffusers.DiffusionPipelineuse_auth_token – optional huggingface auth token.
local_files_only – avoid downloading files and only look for cached files when the model path is a huggingface slug
- Raises:
ModelNotFoundError – If the model could not be found.
- static parse(uri: str) LoRAUri[source]
Parse a
--lorasuri and return an object representing its constituents- Parameters:
uri – string with
--lorasuri syntax- Raises:
- Returns:
LoRAPath
- property model: str
Model path, huggingface slug, file path
- property revision: str | None
Model repo revision
- property scale: float
LoRA scale
- property subfolder: str | None
Model repo subfolder
- property weight_name: str | None
Model weight-name
- class dgenerate.pipelinewrapper.ModelType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
EnumEnum representation of
--model-type- FLAX = 9
Stable Diffusion, such as SD 1.0 - 2.x, with Flax / Jax parallelization.
- TORCH = 0
Stable Diffusion, such as SD 1.0 - 2.x
- TORCH_IF = 3
Deep Floyd IF stage 1
- TORCH_IFS = 4
Deep Floyd IF superscaler (stage 2)
- TORCH_IFS_IMG2IMG = 5
Deep Floyd IF superscaler (stage 2) image to image / variation mode.
- TORCH_PIX2PIX = 1
Stable Diffusion pix2pix prompt guided editing.
- TORCH_SD3 = 12
Stable Diffusion 3
- TORCH_SDXL = 2
Stable Diffusion XL
- TORCH_SDXL_PIX2PIX = 6
Stable Diffusion XL pix2pix prompt guided editing.
- TORCH_S_CASCADE = 10
Stable Cascade prior
- TORCH_S_CASCADE_DECODER = 11
Stable Cascade decoder
- TORCH_UPSCALER_X2 = 7
Stable Diffusion X2 upscaler
- TORCH_UPSCALER_X4 = 8
Stable Diffusion X4 upscaler
- class dgenerate.pipelinewrapper.PipelineCreationResult(pipeline)[source]
Bases:
object- get_pipeline_modules(names=collections.abc.Iterable[str])[source]
Get associated pipeline module such as
vaeetc, in a dictionary mapped from name to module value.Possible Module Names:
unetvaetransformertext_encodertext_encoder_2text_encoder_3tokenizertokenizer_2tokenizer_3safety_checkerfeature_extractorcontrolnetscheduler
If the module is not present or a recognized name, a
ValueErrorwill be thrown describing the module that is not part of the pipeline.- Raises:
ValueError –
- Parameters:
names – module names, such as
vae,text_encoder- Returns:
dictionary
- property pipeline
- class dgenerate.pipelinewrapper.PipelineType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
EnumRepresents possible diffusers pipeline types.
- IMG2IMG = 2
Image to image mode. Generation seeded / controlled with an image in some fashion.
- INPAINT = 3
Inpainting mode. Generation seeded / controlled with an image and a mask in some fashion.
- TXT2IMG = 1
Text to image mode. Prompt only generation.
- class dgenerate.pipelinewrapper.PipelineWrapperResult(images: Sequence[Image] | None)[source]
Bases:
objectThe result of calling
DiffusionPipelineWrapper- image_grid(cols_rows: tuple[int, int])[source]
Render an image grid from the images in this result.
- Raises:
ValueError – if no images are present on this object. This is impossible if this object was produced by
DiffusionPipelineWrapper.- Parameters:
cols_rows – columns and rows (WxH) desired as a tuple
- Returns:
PIL.Image.Image
- property image
The first image in the batch of requested batch size.
- Returns:
PIL.Image.Image
- property image_count
The number of images produced.
- Returns:
int
- images: MutableSequence[Image] | None
- class dgenerate.pipelinewrapper.SDXLRefinerUri(model: str, revision: str | None = None, variant: str | None = None, subfolder: str | None = None, dtype: DataType | str | None = None)[source]
Bases:
objectRepresentation of
--sdxl-refineruri- __init__(model: str, revision: str | None = None, variant: str | None = None, subfolder: str | None = None, dtype: DataType | str | None = None)[source]
- Parameters:
model – model path
revision – model revision (branch name)
variant – model variant, for example
fp16subfolder – model subfolder
dtype – model data type (precision)
- Raises:
InvalidSDXLRefinerUriError – If
dtypeis passed an invalid data type string.
- static parse(uri: str) SDXLRefinerUri[source]
Parse an
--sdxl-refineruri and return an object representing its constituents- Parameters:
uri – string with
--sdxl-refineruri syntax- Raises:
- Returns:
- property model: str
Model path, huggingface slug
- property revision: str | None
Model repo revision
- property subfolder: str | None
Model repo subfolder
- property variant: str | None
Model repo revision
- class dgenerate.pipelinewrapper.TextualInversionUri(model: str, token: str | None = None, revision: str | None = None, subfolder: str | None = None, weight_name: str | None = None)[source]
Bases:
objectRepresentation of
--textual-inversionsuri- __init__(model: str, token: str | None = None, revision: str | None = None, subfolder: str | None = None, weight_name: str | None = None)[source]
- load_on_pipeline(pipeline: DiffusionPipeline, use_auth_token: str | None = None, local_files_only: bool = False)[source]
Load Textual Inversion weights on to a pipeline using this URI
- Parameters:
pipeline –
diffusers.DiffusionPipelineuse_auth_token – optional huggingface auth token.
local_files_only – avoid downloading files and only look for cached files when the model path is a huggingface slug
- Raises:
ModelNotFoundError – If the model could not be found.
- static parse(uri: str) TextualInversionUri[source]
Parse a
--textual-inversionsuri and return an object representing its constituents- Parameters:
uri – string with
--textual-inversionsuri syntax- Raises:
- Returns:
TextualInversionPath
- property model: str
Model path, huggingface slug, file path
- property revision: str | None
Model repo revision
- property subfolder: str | None
Model repo subfolder
- property token: str | None
Prompt keyword
- property weight_name: str | None
Model weight-name
- class dgenerate.pipelinewrapper.TorchControlNetUri(model: str, revision: str | None, variant: str | None, subfolder: str | None, dtype: DataType | str | None = None, scale: float = 1.0, start: float = 0.0, end: float = 1.0)[source]
Bases:
objectRepresentation of
--control-netsuri when--model-typetorch*- __init__(model: str, revision: str | None, variant: str | None, subfolder: str | None, dtype: DataType | str | None = None, scale: float = 1.0, start: float = 0.0, end: float = 1.0)[source]
- Parameters:
model – model path
revision – model revision (branch name)
variant – model variant, for example
fp16subfolder – model subfolder
dtype – model data type (precision)
scale – control net scale
start – control net guidance start value
end – control net guidance end value
- Raises:
InvalidControlNetUriError – If
dtypeis passed an invalid data type string.
- load(dtype_fallback: ~dgenerate.pipelinewrapper.enums.DataType = DataType.AUTO, use_auth_token: str | None = None, local_files_only: bool = False, sequential_cpu_offload_member: bool = False, model_cpu_offload_member: bool = False, model_class: type[~diffusers.models.controlnet.ControlNetModel] | type[~diffusers.models.controlnet_sd3.SD3ControlNetModel] = <class 'diffusers.models.controlnet.ControlNetModel'>) ControlNetModel | SD3ControlNetModel[source]
Load a
diffusers.ControlNetModelfrom this URI.- Parameters:
dtype_fallback – Fallback datatype if
dtypewas not specified in the URI.use_auth_token – Optional huggingface API auth token, used for downloading restricted repos that your account has access to.
local_files_only – Avoid connecting to huggingface to download models and only use cached models?
sequential_cpu_offload_member – This model will be attached to a pipeline which will have sequential cpu offload enabled?
model_cpu_offload_member – This model will be attached to a pipeline which will have model cpu offload enabled?
model_class – What class of control net model should be loaded?
diffusers.ControlNetModelordiffusers.SD3ControlNetModel. The first class is for Stable Diffusion 1 & 2, and the second class is used for Stable Diffusion 3.
- Raises:
ModelNotFoundError – If the model could not be found.
- Returns:
diffusers.ControlNetModelordiffusers.SD3ControlNetModel
- static parse(uri: str) TorchControlNetUri[source]
Parse a
--model-typetorch*--control-netsuri specification and return an object representing its constituents- Parameters:
uri – string with
--control-netsuri syntax- Raises:
- Returns:
TorchControlNetPath
- property end: float
ControlNet guidance end point, fraction of inference / timesteps.
- property model: str
Model path, huggingface slug
- property revision: str | None
Model repo revision
- property scale: float
ControlNet guidance scale
- property start: float
ControlNet guidance start point, fraction of inference / timesteps.
- property subfolder: str | None
Model repo subfolder
- property variant: str | None
Model repo revision
- class dgenerate.pipelinewrapper.TorchPipelineCreationResult(pipeline: DiffusionPipeline, parsed_unet_uri: TorchUNetUri | None, parsed_vae_uri: TorchVAEUri | None, parsed_lora_uris: Sequence[LoRAUri], parsed_textual_inversion_uris: Sequence[TextualInversionUri], parsed_control_net_uris: Sequence[TorchControlNetUri])[source]
Bases:
PipelineCreationResult- __init__(pipeline: DiffusionPipeline, parsed_unet_uri: TorchUNetUri | None, parsed_vae_uri: TorchVAEUri | None, parsed_lora_uris: Sequence[LoRAUri], parsed_textual_inversion_uris: Sequence[TextualInversionUri], parsed_control_net_uris: Sequence[TorchControlNetUri])[source]
- call(device: str | None = 'cuda', prompt_weighter: PromptWeighter | None = None, **kwargs) BaseOutput[source]
Call pipeline, see:
call_pipeline()- Parameters:
device – move the pipeline to this device before calling
prompt_weighter – Optional prompt weighter for weighted prompt syntaxes
kwargs – forward kwargs to pipeline
- Returns:
A subclass of
diffusers.utils.BaseOutput
- parsed_control_net_uris: Sequence[TorchControlNetUri]
Parsed ControlNet URIs if any were present
- parsed_textual_inversion_uris: Sequence[TextualInversionUri]
Parsed Textual Inversion URIs if any were present
- parsed_unet_uri: TorchUNetUri | None
Parsed UNet URI if one was present
- parsed_vae_uri: TorchVAEUri | None
Parsed VAE URI if one was present
- property pipeline: DiffusionPipeline
A created subclass of
diffusers.DiffusionPipeline
- class dgenerate.pipelinewrapper.TorchPipelineFactory(pipeline_type: PipelineType, model_path: str, model_type: ModelType = ModelType.TORCH, revision: str | None = None, variant: str | None = None, subfolder: str | None = None, dtype: DataType = DataType.AUTO, unet_uri: str | None = None, vae_uri: str | None = None, lora_uris: Sequence[str] | None = None, textual_inversion_uris: Sequence[str] | None = None, control_net_uris: Sequence[str] | None = None, text_encoder_uris: Sequence[str] | None = None, scheduler: str | None = None, safety_checker: bool = False, auth_token: str | None = None, device: str = 'cuda', extra_modules: dict[str, Any] | None = None, model_cpu_offload: bool = False, sequential_cpu_offload: bool = False, local_files_only: bool = False, vae_tiling=False, vae_slicing=False)[source]
Bases:
objectCombines
create_torch_diffusion_pipeline()andset_vae_slicing_tiling()into a factory that can recreate the same Torch pipeline over again, possibly from cache.- __call__() TorchPipelineCreationResult[source]
- __init__(pipeline_type: PipelineType, model_path: str, model_type: ModelType = ModelType.TORCH, revision: str | None = None, variant: str | None = None, subfolder: str | None = None, dtype: DataType = DataType.AUTO, unet_uri: str | None = None, vae_uri: str | None = None, lora_uris: Sequence[str] | None = None, textual_inversion_uris: Sequence[str] | None = None, control_net_uris: Sequence[str] | None = None, text_encoder_uris: Sequence[str] | None = None, scheduler: str | None = None, safety_checker: bool = False, auth_token: str | None = None, device: str = 'cuda', extra_modules: dict[str, Any] | None = None, model_cpu_offload: bool = False, sequential_cpu_offload: bool = False, local_files_only: bool = False, vae_tiling=False, vae_slicing=False)[source]
- class dgenerate.pipelinewrapper.TorchUNetUri(model: str, revision: str | None = None, variant: str | None = None, subfolder: str | None = None, dtype: DataType | str | None = None)[source]
Bases:
objectRepresentation of
--uneturi when--model-typetorch*- __init__(model: str, revision: str | None = None, variant: str | None = None, subfolder: str | None = None, dtype: DataType | str | None = None)[source]
- Parameters:
model – model path
revision – model revision (branch name)
variant – model variant, for example
fp16subfolder – model subfolder
dtype – model data type (precision)
- Raises:
InvalidUNetUriError – If
modelpoints to a single file, single file loads are not supported. Or ifdtypeis passed an invalid data type string.
- load(variant_fallback: str | None = None, dtype_fallback: ~dgenerate.pipelinewrapper.enums.DataType = DataType.AUTO, use_auth_token: str | None = None, local_files_only: bool = False, sequential_cpu_offload_member: bool = False, model_cpu_offload_member: bool = False, unet_class=<class 'diffusers.models.unets.unet_2d_condition.UNet2DConditionModel'>)[source]
Load a UNet of type
diffusers.UNet2DConditionModel- Parameters:
variant_fallback – If the URI does not specify a variant, use this variant.
dtype_fallback – If the URI does not specify a dtype, use this dtype.
use_auth_token – optional huggingface auth token.
local_files_only – avoid downloading files and only look for cached files when the model path is a huggingface slug or blob link
sequential_cpu_offload_member – This model will be attached to a pipeline which will have sequential cpu offload enabled?
model_cpu_offload_member – This model will be attached to a pipeline which will have model cpu offload enabled?
unet_class – UNet class
- Raises:
ModelNotFoundError – If the model could not be found.
- Returns:
diffusers.UNet2DConditionModel
- static parse(uri: str) TorchUNetUri[source]
Parse a
--model-typetorch*--uneturi and return an object representing its constituents- Parameters:
uri – string with
--uneturi syntax- Raises:
- Returns:
TorchUNetPath
- property model: str
Model path, huggingface slug, file path, or blob link
- property revision: str | None
Model repo revision
- property subfolder: str | None
Model repo subfolder
- property variant: str | None
Model repo revision
- class dgenerate.pipelinewrapper.TorchVAEUri(encoder: str, model: str, revision: str | None = None, variant: str | None = None, subfolder: str | None = None, dtype: DataType | str | None = None)[source]
Bases:
objectRepresentation of
--vaeuri when--model-typetorch*- __init__(encoder: str, model: str, revision: str | None = None, variant: str | None = None, subfolder: str | None = None, dtype: DataType | str | None = None)[source]
- Parameters:
encoder – encoder class name, for example
AutoencoderKLmodel – model path
revision – model revision (branch name)
variant – model variant, for example
fp16subfolder – model subfolder
dtype – model data type (precision)
- Raises:
InvalidVaeUriError – If
dtypeis passed an invalid data type string, or ifmodelpoints to a single file and the specifiedencoderclass name does not support loading from a single file.
- load(dtype_fallback: DataType = DataType.AUTO, use_auth_token: str | None = None, local_files_only: bool = False, sequential_cpu_offload_member: bool = False, model_cpu_offload_member: bool = False) AutoencoderKL | AsymmetricAutoencoderKL | AutoencoderTiny | ConsistencyDecoderVAE[source]
Load a VAE of type
diffusers.AutoencoderKL,diffusers.AsymmetricAutoencoderKL,diffusers.AutoencoderKLTemporalDecoder, ordiffusers.AutoencoderTinyfrom this URI- Parameters:
dtype_fallback – If the URI does not specify a dtype, use this dtype.
use_auth_token – optional huggingface auth token.
local_files_only – avoid downloading files and only look for cached files when the model path is a huggingface slug or blob link
sequential_cpu_offload_member – This model will be attached to a pipeline which will have sequential cpu offload enabled?
model_cpu_offload_member – This model will be attached to a pipeline which will have model cpu offload enabled?
- Raises:
ModelNotFoundError – If the model could not be found.
- Returns:
diffusers.AutoencoderKL,diffusers.AsymmetricAutoencoderKL,diffusers.AutoencoderKLTemporalDecoder, ordiffusers.AutoencoderTiny
- static parse(uri: str) TorchVAEUri[source]
Parse a
--model-typetorch*--vaeuri and return an object representing its constituents- Parameters:
uri – string with
--vaeuri syntax- Raises:
- Returns:
TorchVAEPath
- property encoder: str
Encoder class name such as “AutoencoderKL”
- property model: str
Model path, huggingface slug
- property revision: str | None
Model repo revision
- property subfolder: str | None
Model repo subfolder
- property variant: str | None
Model repo revision
- dgenerate.pipelinewrapper.call_pipeline(pipeline: DiffusionPipeline | FlaxDiffusionPipeline, device: device | str | None = 'cuda', prompt_weighter: PromptWeighter = None, **kwargs)[source]
Call a diffusers pipeline, offload the last called pipeline to CPU before doing so if the last pipeline is not being called in succession
- Parameters:
pipeline – The pipeline
device – The device to move the pipeline to before calling, it will be moved to this device if it is not already on the device. If the pipeline does not support moving to a device, such as with flax pipelines, or with sequentially offloaded or cpu offloaded models,this argument is ignored.
kwargs – diffusers pipeline keyword arguments
prompt_weighter – Optional prompt weighter for weighted prompt syntaxes
- Raises:
dgenerate.OutOfMemoryError – if there is not enough memory on the specified device
UnsupportedPipelineConfiguration – If the pipeline is missing certain required modules, such as text encoders.
- Returns:
the result of calling the diffusers pipeline
- dgenerate.pipelinewrapper.clear_control_net_cache(collect=True)[source]
Clear ControlNet cache and then garbage collect.
- Parameters:
collect – Call
gc.collect()?
- dgenerate.pipelinewrapper.clear_model_cache(collect=True)[source]
Clear all in memory model caches and garbage collect.
- Parameters:
collect – Call
gc.collect()?
- dgenerate.pipelinewrapper.clear_pipeline_cache(collect=True)[source]
Clear DiffusionPipeline cache and then garbage collect.
- Parameters:
collect – Call
gc.collect()?
- dgenerate.pipelinewrapper.clear_text_encoder_cache(collect=True)[source]
Clear Text Encoder cache and then garbage collect.
- Parameters:
collect – Call
gc.collect()?
- dgenerate.pipelinewrapper.clear_unet_cache(collect=True)[source]
Clear UNet cache and then garbage collect.
- Parameters:
collect – Call
gc.collect()?
- dgenerate.pipelinewrapper.clear_vae_cache(collect=True)[source]
Clear VAE cache and then garbage collect.
- Parameters:
collect – Call
gc.collect()?
- dgenerate.pipelinewrapper.control_net_cache_size() int[source]
Return the estimated memory usage in bytes of all user specified ControlNet models currently cached in memory.
- Returns:
memory usage in bytes.
- dgenerate.pipelinewrapper.controlnet_create_update_cache_info(controlnet, estimated_size: int)[source]
Add additional information about the size of a newly created ControlNet model to the cache.
Tag the object with an internal tag.
- Parameters:
controlnet – the ControlNet object
estimated_size – size bytes
- dgenerate.pipelinewrapper.create_flax_diffusion_pipeline(pipeline_type: PipelineType, model_path: str, model_type: ModelType = ModelType.FLAX, revision: str | None = None, subfolder: str | None = None, dtype: DataType = DataType.AUTO, unet_uri: str | None = None, vae_uri: str | None = None, control_net_uris: Sequence[str] | None = None, text_encoder_uris: Sequence[str] | None = None, scheduler: str | None = None, safety_checker: bool = False, auth_token: str | None = None, extra_modules: dict[str, Any] | None = None, local_files_only: bool = False) FlaxPipelineCreationResult[source]
Create a
diffusers.FlaxDiffusionPipelinein dgenerates in memory cacheing system.- Parameters:
pipeline_type –
dgenerate.pipelinewrapper.PipelineTypeenum valuemodel_path – huggingface slug, huggingface blob link, path to folder on disk, path to file on disk
model_type – Currently only accepts
dgenerate.pipelinewrapper.ModelType.FLAXrevision – huggingface repo revision (branch)
subfolder – huggingface repo subfolder if applicable
dtype – Optional
dgenerate.pipelinewrapper.DataTypeenum valueunet_uri – Optional Flax specific
--unetURI string for specifying a specific UNetvae_uri – Optional Flax specific
--vaeURI string for specifying a specific VAEcontrol_net_uris – Optional
--control-netsURI strings for specifying ControlNet modelstext_encoder_uris – Optional user specified
--text-encodersURIs that will be loaded on to the pipeline in order. A uri value of+orNoneindicates use default, a string value ofnullindicates to explicitly not load any encoder allscheduler – Optional scheduler (sampler) class name, unqualified, or “help” to print supported values to STDOUT and raise
dgenerate.pipelinewrapper.SchedulerHelpExceptionsafety_checker – Safety checker enabled? default is
Falseauth_token – Optional huggingface API token for accessing repositories that are restricted to your account
extra_modules – Extra module arguments to pass directly into
diffusers.FlaxDiffusionPipeline.from_pretrained()local_files_only – Only look in the huggingface cache and do not connect to download models?
- Raises:
- Returns:
- dgenerate.pipelinewrapper.create_torch_diffusion_pipeline(pipeline_type: PipelineType, model_path: str, model_type: ModelType = ModelType.TORCH, revision: str | None = None, variant: str | None = None, subfolder: str | None = None, dtype: DataType = DataType.AUTO, unet_uri: str | None = None, vae_uri: str | None = None, lora_uris: Sequence[str] | None = None, textual_inversion_uris: Sequence[str] | None = None, text_encoder_uris: Sequence[str] | None = None, control_net_uris: Sequence[str] | None = None, scheduler: str | None = None, safety_checker: bool = False, auth_token: str | None = None, device: str = 'cuda', extra_modules: dict[str, Any] | None = None, model_cpu_offload: bool = False, sequential_cpu_offload: bool = False, local_files_only: bool = False) TorchPipelineCreationResult[source]
Create a
diffusers.DiffusionPipelinein dgenerates in memory cacheing system.- Parameters:
pipeline_type –
dgenerate.pipelinewrapper.PipelineTypeenum valuemodel_type –
dgenerate.pipelinewrapper.ModelTypeenum valuemodel_path – huggingface slug, huggingface blob link, path to folder on disk, path to file on disk
revision – huggingface repo revision (branch)
variant – model weights name variant, for example ‘fp16’
subfolder – huggingface repo subfolder if applicable
dtype – Optional
dgenerate.pipelinewrapper.DataTypeenum valueunet_uri – Optional
--unetURI string for specifying a specific UNetvae_uri – Optional
--vaeURI string for specifying a specific VAElora_uris – Optional
--lorasURI strings for specifying LoRA weightstextual_inversion_uris – Optional
--textual-inversionsURI strings for specifying Textual Inversion weightstext_encoder_uris – Optional user specified
--text-encodersURIs that will be loaded on to the pipeline in order. A uri value of+orNoneindicates use default, a string value ofnullindicates to explicitly not load any encoder allcontrol_net_uris – Optional
--control-netsURI strings for specifying ControlNet modelsscheduler – Optional scheduler (sampler) class name, unqualified, or “help” / “helpargs” to print supported values to STDOUT and raise
dgenerate.pipelinewrapper.SchedulerHelpException. Dgenerate URI syntax is supported for overriding the schedulers constructor parameter defaults.safety_checker – Safety checker enabled? default is
Falseauth_token – Optional huggingface API token for accessing repositories that are restricted to your account
device – Optional
--devicestring, defaults to “cuda”extra_modules – Extra module arguments to pass directly into
diffusers.DiffusionPipeline.from_single_file()ordiffusers.DiffusionPipeline.from_pretrained()model_cpu_offload – This pipeline has model_cpu_offloading enabled?
sequential_cpu_offload – This pipeline has sequential_cpu_offloading enabled?
local_files_only – Only look in the huggingface cache and do not connect to download models?
- Raises:
- Returns:
- dgenerate.pipelinewrapper.enable_model_cpu_offload(pipeline: DiffusionPipeline, device: device | str = 'cuda')[source]
Enable sequential model cpu offload on a torch pipeline, in a way dgenerate can keep track of.
- Parameters:
pipeline – the pipeline
device – the device
- dgenerate.pipelinewrapper.enable_sequential_cpu_offload(pipeline: DiffusionPipeline, device: device | str = 'cuda')[source]
Enable sequential offloading on a torch pipeline, in a way dgenerate can keep track of.
- Parameters:
pipeline – the pipeline
device – the device
- dgenerate.pipelinewrapper.enforce_cache_constraints(collect=True)[source]
Enforce
dgenerate.pipelinewrapper.CACHE_MEMORY_CONSTRAINTSand clear caches accordingly- Parameters:
collect – Call
gc.collect()after a cache clear ?- Returns:
Whether any caches were cleared due to constraint expressions.
- dgenerate.pipelinewrapper.enforce_control_net_cache_constraints(new_control_net_size, collect=True)[source]
Enforce
dgenerate.pipelinewrapper.CONTROL_NET_CACHE_MEMORY_CONSTRAINTSand clear the ControlNet cache if needed.- Parameters:
new_control_net_size – estimated size in bytes of any new control net that is about to enter memory
collect – Call
gc.collect()after a cache clear ?
- Returns:
Whether the cache was cleared due to constraint expressions.
- dgenerate.pipelinewrapper.enforce_pipeline_cache_constraints(new_pipeline_size, collect=True)[source]
Enforce
dgenerate.pipelinewrapper.PIPELINE_CACHE_MEMORY_CONSTRAINTSand clear thediffusers.DiffusionPipelinecache if needed.- Parameters:
new_pipeline_size – estimated size in bytes of any new pipeline that is about to enter memory
collect – Call
gc.collect()after a cache clear ?
- Returns:
Whether the cache was cleared due to constraint expressions.
- dgenerate.pipelinewrapper.enforce_text_encoder_cache_constraints(new_text_encoder_size, collect=True)[source]
Enforce
dgenerate.pipelinewrapper.TEXT_ENCODER_CACHE_MEMORY_CONSTRAINTSand clear the Text Encoder cache if needed.- Parameters:
new_text_encoder_size – estimated size in bytes of any new text encoder that is about to enter memory
collect – Call
gc.collect()after a cache clear ?
- Returns:
Whether the cache was cleared due to constraint expressions.
- dgenerate.pipelinewrapper.enforce_unet_cache_constraints(new_unet_size, collect=True)[source]
Enforce
dgenerate.pipelinewrapper.UNET_CACHE_MEMORY_CONSTRAINTSand clear the UNet cache if needed.- Parameters:
new_unet_size – estimated size in bytes of any new unet that is about to enter memory
collect – Call
gc.collect()after a cache clear ?
- Returns:
Whether the cache was cleared due to constraint expressions.
- dgenerate.pipelinewrapper.enforce_vae_cache_constraints(new_vae_size, collect=True)[source]
Enforce
dgenerate.pipelinewrapper.VAE_CACHE_MEMORY_CONSTRAINTSand clear the VAE cache if needed.- Parameters:
new_vae_size – estimated size in bytes of any new vae that is about to enter memory
collect – Call
gc.collect()after a cache clear ?
- Returns:
Whether the cache was cleared due to constraint expressions.
- dgenerate.pipelinewrapper.estimate_pipeline_memory_use(pipeline_type: PipelineType, model_path: str, model_type: ModelType, revision: str = 'main', variant: str | None = None, subfolder: str | None = None, unet_uri: str | None = None, vae_uri: str | None = None, lora_uris: Sequence[str] | None = None, textual_inversion_uris: Sequence[str] | None = None, text_encoder_uris: Sequence[str] | None = None, safety_checker: bool = False, auth_token: str | None = None, extra_args: dict[str, Any] | None = None, local_files_only: bool = False)[source]
Estimate the CPU side memory use of a pipeline.
- Parameters:
pipeline_type –
dgenerate.pipelinewrapper.PipelineTypemodel_path – huggingface slug, blob link, path to folder on disk, path to model file.
model_type –
dgenerate.pipelinewrapper.ModelTyperevision – huggingface repo revision if using a huggingface slug
variant – model file variant desired, for example “fp16”
subfolder – huggingface repo subfolder if using a huggingface slug
unet_uri – optional user specified
--unetURI that will be loaded on to the pipelinevae_uri – optional user specified
--vaeURI that will be loaded on to the pipelinelora_uris – optional user specified
--lorasURIs that will be loaded on to the pipelinetextual_inversion_uris – optional user specified
--textual-inversionURIs that will be loaded on to the pipelinetext_encoder_uris – optional user specified
--text-encodersURIs that will be loaded on to the pipelinesafety_checker – consider the safety checker? dgenerate usually loads the safety checker and then retroactively disables it if needed, so it usually considers the size of the safety checker model.
auth_token – optional huggingface auth token to access restricted repositories that your account has access to.
extra_args –
extra_argsas to be passed tocreate_torch_diffusion_pipeline()orcreate_flax_diffusion_pipeline()local_files_only – Only ever attempt to look in the local huggingface cache? if
Falsethe huggingface API will be contacted when necessary.
- Returns:
size estimate in bytes.
- dgenerate.pipelinewrapper.get_data_type_enum(id_str: DataType | str | None) DataType[source]
Convert a
--dtypestring to itsDataTypeenum value- Parameters:
id_str –
--dtypestring- Raises:
ValueError – if an invalid string value (name) is passed
- Returns:
- dgenerate.pipelinewrapper.get_data_type_string(data_type_enum: DataType) str[source]
Convert a
DataTypeenum value to its--dtypestring- Parameters:
data_type_enum –
DataTypevalue- Returns:
--dtypestring
- dgenerate.pipelinewrapper.get_flax_dtype(dtype: DataType | str | Any | None)[source]
Return a
jax.numpy.dtypedatatype from aDataTypevalue, or a string, or ajax.numpy.dtypedatatype itself.Passing
Noneresults inNonebeing returned.Passing ‘auto’ or
DataType.AUTOresults inNonebeing returned.- Parameters:
dtype –
DataType, string,jax.numpy.dtype,None- Raises:
ValueError – if an invalid string value (name) is passed
- Returns:
jax.numpy.dtype
- dgenerate.pipelinewrapper.get_model_type_enum(id_str: ModelType | str) ModelType[source]
Convert a
--model-typestring to itsModelTypeenum value- Parameters:
id_str –
--model-typestring- Raises:
ValueError – if an invalid string value (name) is passed
- Returns:
- dgenerate.pipelinewrapper.get_model_type_string(model_type_enum: ModelType) str[source]
Convert a
ModelTypeenum value to its--model-typestring- Parameters:
model_type_enum –
ModelTypevalue- Returns:
--model-typestring
- dgenerate.pipelinewrapper.get_pipeline_type_enum(id_str: PipelineType | str | None) PipelineType[source]
Get a
PipelineTypeenum value from a string.- Parameters:
id_str – one of: “txt2img”, “img2img”, or “inpaint”
- Raises:
ValueError – if an invalid string value (name) is passed
- Returns:
- dgenerate.pipelinewrapper.get_pipeline_type_string(pipeline_type_enum: PipelineType)[source]
Convert a
PipelineTypeenum value to a string.- Parameters:
pipeline_type_enum –
PipelineTypevalue- Returns:
one of: “txt2img”, “img2img”, or “inpaint”
- dgenerate.pipelinewrapper.get_torch_device(component: DiffusionPipeline | Module) device[source]
Get the device that a pipeline or pipeline component exists on.
- Parameters:
component – pipeline or pipeline component.
- Returns:
torch.device
- dgenerate.pipelinewrapper.get_torch_device_string(component: DiffusionPipeline | Module) str[source]
Get the device string that a pipeline or pipeline component exists on.
- Parameters:
component – pipeline or pipeline component.
- Returns:
device string
- dgenerate.pipelinewrapper.get_torch_dtype(dtype: DataType | dtype | str | None) dtype | None[source]
Return a
torch.dtypedatatype from aDataTypevalue, or a string, or atorch.dtypedatatype itself.Passing
Noneresults inNonebeing returned.Passing ‘auto’ or
DataType.AUTOresults inNonebeing returned.- Parameters:
dtype –
DataType, string,torch.dtype, None- Raises:
ValueError – if an invalid string value (name) is passed
- Returns:
torch.dtype
- dgenerate.pipelinewrapper.get_torch_pipeline_modules(pipeline: DiffusionPipeline)[source]
Get all component modules of a torch diffusers pipeline.
- Parameters:
pipeline – the pipeline
- Returns:
dictionary of modules by name
- dgenerate.pipelinewrapper.is_model_cpu_offload_enabled(module: DiffusionPipeline | Module)[source]
Test if a pipeline or torch neural net module created by dgenerate has model cpu offload enabled.
- Parameters:
module – the module object
- Returns:
TrueorFalse
- dgenerate.pipelinewrapper.is_sequential_cpu_offload_enabled(module: DiffusionPipeline | Module)[source]
Test if a pipeline or torch neural net module created by dgenerate has sequential offload enabled.
- Parameters:
module – the module object
- Returns:
TrueorFalse
- dgenerate.pipelinewrapper.is_valid_device_string(device, raise_ordinal=True)[source]
Is a device string valid? including the device ordinal specified?
- Parameters:
device – device string, such as
cpu, orcuda, orcuda:Nraise_ordinal – Raise
InvalidDeviceOrdinalExceptionif a specified CUDA device ordinal is found to not exist?
- Raises:
InvalidDeviceOrdinalException – If
raise_ordinal=Trueand a the device ordinal specified in a CUDA device string does not exist.- Returns:
TrueorFalse
- dgenerate.pipelinewrapper.load_scheduler(pipeline: DiffusionPipeline | FlaxDiffusionPipeline, scheduler_name=None, model_path: str | None = None)[source]
Load a specific compatible scheduler class name onto a huggingface diffusers pipeline object.
- Raises:
SchedulerHelpException – if “help” is passed as a scheduler name.
- Parameters:
pipeline – pipeline object
scheduler_name – compatible scheduler class name, pass “help” to receive a print out to STDOUT and raise
SchedulerHelpException, this argument can accept a URI in typical dgenerate format, for overriding the schedulers constructor parameters.model_path – Optional model path to be used in the message to STDOUT produced by passing “help”
- Returns:
- dgenerate.pipelinewrapper.model_type_is_flax(model_type: ModelType | str) bool[source]
Does a
--model-typestring orModelTypeenum value represent an Flax model?- Parameters:
model_type –
--model-typestring orModelTypeenum value- Returns:
bool
- dgenerate.pipelinewrapper.model_type_is_floyd(model_type: ModelType | str) bool[source]
Does a
--model-typestring orModelTypeenum value represent an floyd “if” of “ifs” type model?- Parameters:
model_type –
--model-typestring orModelTypeenum value- Returns:
bool
- dgenerate.pipelinewrapper.model_type_is_floyd_if(model_type: ModelType | str) bool[source]
Does a
--model-typestring orModelTypeenum value represent an floyd “if” type model?- Parameters:
model_type –
--model-typestring orModelTypeenum value- Returns:
bool
- dgenerate.pipelinewrapper.model_type_is_floyd_ifs(model_type: ModelType | str) bool[source]
Does a
--model-typestring orModelTypeenum value represent an floyd “ifs” type model?- Parameters:
model_type –
--model-typestring orModelTypeenum value- Returns:
bool
- dgenerate.pipelinewrapper.model_type_is_pix2pix(model_type: ModelType | str) bool[source]
Does a
--model-typestring orModelTypeenum value represent an pix2pix type model?- Parameters:
model_type –
--model-typestring orModelTypeenum value- Returns:
bool
- dgenerate.pipelinewrapper.model_type_is_s_cascade(model_type: ModelType | str) bool[source]
Does a
--model-typestring orModelTypeenum value represent a Stable Cascade related model?- Parameters:
model_type –
--model-typestring orModelTypeenum value- Returns:
bool
- dgenerate.pipelinewrapper.model_type_is_sd3(model_type: ModelType | str) bool[source]
Does a
--model-typestring orModelTypeenum value represent an SD3 model?- Parameters:
model_type –
--model-typestring orModelTypeenum value- Returns:
bool
- dgenerate.pipelinewrapper.model_type_is_sdxl(model_type: ModelType | str) bool[source]
Does a
--model-typestring orModelTypeenum value represent an SDXL model?- Parameters:
model_type –
--model-typestring orModelTypeenum value- Returns:
bool
- dgenerate.pipelinewrapper.model_type_is_torch(model_type: ModelType | str) bool[source]
Does a
--model-typestring orModelTypeenum value represent an Torch model?- Parameters:
model_type –
--model-typestring orModelTypeenum value- Returns:
bool
- dgenerate.pipelinewrapper.model_type_is_upscaler(model_type: ModelType | str) bool[source]
Does a
--model-typestring orModelTypeenum value represent an upscaler model?- Parameters:
model_type –
--model-typestring orModelTypeenum value- Returns:
bool
- dgenerate.pipelinewrapper.pipeline_cache_size() int[source]
Return the estimated memory usage in bytes of all diffusers pipelines currently cached in memory.
- Returns:
memory usage in bytes.
- dgenerate.pipelinewrapper.pipeline_create_update_cache_info(pipeline, estimated_size: int)[source]
Add additional information about the size of a newly created
diffusers.DiffusionPipelineto the cache.Tag the object with an internal tag.
- Parameters:
pipeline – the
diffusers.DiffusionPipelineobjectestimated_size – size bytes
- dgenerate.pipelinewrapper.pipeline_off_cpu_update_cache_info(pipeline: DiffusionPipeline | FlaxDiffusionPipeline)[source]
Update CPU side cache size information when a diffusers pipeline is moved to a device that is not the CPU
- Parameters:
pipeline – the pipeline
- dgenerate.pipelinewrapper.pipeline_to(pipeline, device: device | str | None)[source]
Move a diffusers pipeline to a device if possible, in a way that dgenerate can keep track of.
This calls methods associated with updating the cache statistics such as
dgenerate.pipelinewrapper.pipeline_off_cpu_update_cache_info()anddgenerate.pipelinewrapper.pipeline_to_cpu_update_cache_info()for you, as well as the associated cache update functions for the pipelines individual components as needed.If the pipeline does not possess the
.to()method (such as with flax pipelines), this is a no-op.If
device==Nonethis is a no-op.Modules which are meta tensors will not be moved (sequentially offloaded modules)
Modules which have model cpu offload enabled will not be moved unless they are moving to “cpu”
- Raises:
dgenerate.OutOfMemoryError – if there is not enough memory on the specified device
- Parameters:
pipeline – the pipeline
device – the device
- Returns:
the moved pipeline
- dgenerate.pipelinewrapper.pipeline_to_cpu_update_cache_info(pipeline: DiffusionPipeline)[source]
Update CPU side cache size information when a diffusers pipeline is moved to the CPU
- Parameters:
pipeline – the pipeline
- dgenerate.pipelinewrapper.scheduler_is_help(name: str | None)[source]
This scheduler name is simply a request for help?, IE: “help”?
- Parameters:
name – string to test
- Returns:
TrueorFalse
- dgenerate.pipelinewrapper.set_vae_slicing_tiling(pipeline: DiffusionPipeline | FlaxDiffusionPipeline, vae_tiling: bool, vae_slicing: bool)[source]
Set the vae_slicing and vae_tiling status on a created huggingface diffusers pipeline.
- Raises:
UnsupportedPipelineConfigError – if the pipeline does not support one or both of the provided values for
vae_tilingandvae_slicing- Parameters:
pipeline – pipeline object
vae_tiling – tiling status
vae_slicing – slicing status
- dgenerate.pipelinewrapper.supported_data_type_enums() list[DataType][source]
Return a list of supported
DataTypeenum values
- dgenerate.pipelinewrapper.supported_data_type_strings()[source]
Return a list of supported
--dtypestrings
- dgenerate.pipelinewrapper.supported_model_type_enums() list[ModelType][source]
Return a list of supported
ModelTypeenum values
- dgenerate.pipelinewrapper.supported_model_type_strings()[source]
Return a list of supported
--model-typestrings
- dgenerate.pipelinewrapper.text_encoder_cache_size() int[source]
Return the estimated memory usage in bytes of all user specified Text Encoder models currently cached in memory.
- Returns:
memory usage in bytes.
- dgenerate.pipelinewrapper.text_encoder_create_update_cache_info(text_encoder, estimated_size: int)[source]
Add additional information about the size of a newly created Text Encoder model to the cache.
Tag the object with an internal tag.
- Parameters:
text_encoder – the Text Encoder object
estimated_size – size bytes
- dgenerate.pipelinewrapper.text_encoder_is_help(text_encoder_uris: Sequence[str] | None)[source]
Text encoder uris specification is simply a request for help?, IE: “help”?
- Parameters:
text_encoder_uris – list of text encoder URIs to test
- Returns:
TrueorFalse
- dgenerate.pipelinewrapper.unet_cache_size() int[source]
Return the estimated memory usage in bytes of all user specified UNets currently cached in memory.
- Returns:
memory usage in bytes.
- dgenerate.pipelinewrapper.unet_create_update_cache_info(unet, estimated_size: int)[source]
Add additional information about the size of a newly created UNet model to the cache.
Tag the object with an internal tag.
- Parameters:
unet – the UNet object
estimated_size – size bytes
- dgenerate.pipelinewrapper.uri_hash_with_parser(parser)[source]
Create a hash function from a particular URI parser function that hashes a URI string.
The URI is parsed and then the object that results from parsing is hashed with
dgenerate.memoize.struct_hasher().If the parser returns a string, it is regarded as the hash value instead of being passed to
dgenerate.memoize.struct_hasher().- Parameters:
parser – The URI parser function
- Returns:
a hash function compatible with
dgenerate.memoize.memoize()
- dgenerate.pipelinewrapper.uri_list_hash_with_parser(parser)[source]
Create a hash function from a particular URI parser function that hashes a list of URIs.
- Parameters:
parser – The URI parser function
- Returns:
a hash function compatible with
dgenerate.memoize.memoize()
dgenerate.plugin module
URI based plugin loading system base implementations.
- exception dgenerate.plugin.ModuleFileNotFoundError[source]
Bases:
FileNotFoundErrorRaised by
load_modules()if a module could not be found on disk.
- exception dgenerate.plugin.PluginArgumentError[source]
Bases:
ExceptionRaised when a plugin encounters an error in the arguments it is loaded by.
Or errors in arguments used for execution.
- exception dgenerate.plugin.PluginNotFoundError[source]
Bases:
ExceptionRaised when a plugin could not be located by a name.
- class dgenerate.plugin.Plugin(loaded_by_name: str, argument_error_type: type[~dgenerate.plugin.PluginArgumentError] = <class 'dgenerate.plugin.PluginArgumentError'>, **kwargs)[source]
Bases:
object- __init__(loaded_by_name: str, argument_error_type: type[~dgenerate.plugin.PluginArgumentError] = <class 'dgenerate.plugin.PluginArgumentError'>, **kwargs)[source]
- Parameters:
loaded_by_name – The name the plugin was loaded by, will be passed by the loader.
argument_error_type – This exception type will be raised upon argument errors (invalid arguments) when loading a plugin using a
PluginLoaderimplementation. It should match theargument_error_typegiven to thePluginLoaderimplementation being used to load the inheritor of this class.kwargs – Additional arguments that may arise when using an
ARGSstatic signature definition with multipleNAMESin your implementation.
- argument_error(msg: str)[source]
Return an constructed exception that is suitable for raising as an argument error for this plugin.
Example:
raise self.argument_error('oops!')- Parameters:
msg – exception message
- Returns:
the exception object, you must
raiseit.
- classmethod get_accepted_args(loaded_by_name: str, include_bases: bool = False)[source]
Retrieve the argument signature of a plugin implementation.
- classmethod get_accepted_args_schema(loaded_by_name: str, include_bases: bool = False)[source]
Reduce the accepted arguments to a schema dict.
Keyed by argument
name, content keys include:defaultcontains any default value, this key may not exist if the argument has no default value.typescontains all accepted types for the argument in string form.optionalcan the argument accept the valueNone?- Parameters:
loaded_by_name – Plugin loaded by name
include_bases – Include all base classes except
Plugin?
- Returns:
dict
- classmethod get_bases() list[Type[Plugin]][source]
Return a list of base classes, except for
Plugin- Returns:
list of class type objects
- classmethod get_default_args(loaded_by_name: str) list[PluginArg][source]
Get the names and values of arguments for this plugin that possess default values.
- Parameters:
loaded_by_name – The name used to load the plugin. Default arguments may vary by name used to load.
- Returns:
list of arguments with default value: (name, value)
- classmethod get_help(loaded_by_name: str, wrap_width: int | None = None, include_bases: bool = False) str[source]
Get formatted help information about the plugin.
This includes any implemented help strings and an auto formatted description of the plugins accepted arguments.
- Parameters:
loaded_by_name – The name used to load the plugin. Help may vary depending on how many names the plugin implementation handles and what loading it by a certain name does.
wrap_width – wrap paragraphs to this width.
include_bases – include argument names and inherited help from base classes?
- Returns:
Formatted help string
Get argument names that have been explicitly hidden from use or disabled by the plugin for URI use.
These may be unsupported arguments inherited from a base class, or just arguments the plugin does not want you to use via a URI.
These arguments can still be passed manually from code in the interest of maintaining a generic interface, but they may be ignored by the plugin implementation.
- Parameters:
loaded_by_name – The name used to load the plugin. Argument signature may vary by name used to load.
- Returns:
hidden argument names
- classmethod get_required_args(loaded_by_name: str) list[PluginArg][source]
Get a list of required arguments for this plugin class.
- Parameters:
loaded_by_name – The name used to load the plugin. Required arguments may vary by name used to load.
- Returns:
list of argument names
- property loaded_by_name: str
The name the plugin was loaded by.
- Returns:
name
- class dgenerate.plugin.PluginArg(name: str, type: type = typing.Any, **kwargs)[source]
Bases:
object- property base_type
- property hinted_optional_type
- property is_hinted_optional
- class dgenerate.plugin.PluginLoader(base_class=<class 'dgenerate.plugin.Plugin'>, description: str = 'plugin', reserved_args: list[~dgenerate.plugin.PluginArg] | None = None, argument_error_type: type[~dgenerate.plugin.PluginArgumentError] = <class 'dgenerate.plugin.PluginArgumentError'>, not_found_error_type: type[~dgenerate.plugin.PluginNotFoundError] = <class 'dgenerate.plugin.PluginNotFoundError'>)[source]
Bases:
object- __init__(base_class=<class 'dgenerate.plugin.Plugin'>, description: str = 'plugin', reserved_args: list[~dgenerate.plugin.PluginArg] | None = None, argument_error_type: type[~dgenerate.plugin.PluginArgumentError] = <class 'dgenerate.plugin.PluginArgumentError'>, not_found_error_type: type[~dgenerate.plugin.PluginNotFoundError] = <class 'dgenerate.plugin.PluginNotFoundError'>)[source]
- Parameters:
base_class – Base class of plugins, will be used for searching modules.
description – Short plugin description / name, used in exception messages.
reserved_args – Constructor arguments that are used by the plugin class which cannot be redefined by implementors of the plugin class. This should be a list of plugin argument descriptors,
PluginArgargument_error_type – This exception type will be raised when the plugin is loaded with invalid URI arguments.
not_found_error_type – This exception type will be raised when a plugin could not be located by a name specified in a loading URI.
- add_class(cls: type[Plugin])[source]
Add an implementation class to this loader.
- Raises:
RuntimeError – If the added class specifies a name that already exists in this loader.
- Parameters:
cls – the class
- add_search_module(module: module) list[type[Plugin]][source]
Directly add a module object that will be searched for implementations.
- Parameters:
module – the module object
- Raises:
ValueError – If
moduleis not a python module object.- Returns:
list of classes that were newly discovered
- add_search_module_string(string: str) list[type[Plugin]][source]
Add a module string (in sys.modules) that will be searched for implementations.
- Parameters:
string – the module string
- Returns:
list of classes that were newly discovered
- get_accepted_args_schema(include_bases: bool = False) dict[str, dict[str, Any]][source]
Get a
Plugin.get_accepted_args_schema()for every plugin class, keyed by callable plugin name.- Parameters:
include_bases – Include base class arguments? This excludes the base
Plugin- Returns:
dict
- get_all_names() Sequence[str][source]
Get all plugin names that this loader can see.
- Returns:
list of names (strings)
- get_available_classes() list[type[Plugin]][source]
Get classes seen by this plugin loader.
- Returns:
list of classes (types)
- get_class_by_name(plugin_name: str) type[Plugin][source]
Get a plugin class by one of its names.
IE: one of the names listed in its
NAMESstatic attribute.- Parameters:
plugin_name – a name associated with a plugin class
- Raises:
PluginNotFoundError – If the plugin name could not be found.
- Returns:
class (type)
- get_help(plugin_name: str, wrap_width: int | None = None, include_bases: bool = False) str[source]
Get a formatted help string for a plugin by one of its loadable names.
- Parameters:
plugin_name – a name associated with the plugin class
wrap_width – wrap paragraphs to this width.
include_bases – include argument names and inherited help from base classes?
- Raises:
PluginNotFoundError – If the plugin name could not be found.
- Returns:
formatted string
- load(uri: str, **kwargs) Plugin[source]
Load an plugin using a URI string containing its name and arguments.
- Parameters:
uri – The URI string
kwargs – default argument values, will be override by arguments specified in the URI
- Raises:
ValueError – If uri is
NoneRuntimeError – If a plugin is discovered to be using a reserved argument name upon loading it.
PluginArgumentError – If there is an error in the loading arguments for the plugin.
PluginNotFoundError – If the plugin name mentioned in the URI could not be found.
- Returns:
plugin instance
- load_plugin_modules(paths: Iterable[str]) list[type[Plugin]][source]
Modules that will be loaded from disk, or the python environment, and searched for implementations.
Either python files, or module directories containing __init__.py, or names of python modules installed in the environment.
It can be a mix of these.
- Raises:
ModuleFileNotFoundError – If a module path could not be found on disk, or when a module could not be loaded from the python environment.
- Parameters:
paths – list of folder/file paths, or references to python modules installed in the environment
- Returns:
list of classes that were newly discovered
- loader_help(names: Sequence[str], plugin_module_paths: Sequence[str] | None = None, title='plugin', title_plural='plugins', throw=False, log_error=True, include_bases: bool = False)[source]
Implements
--sub-command-helpand--image-processor-helpcommand line options for example.- Parameters:
names – arguments (sub-command names, or empty list)
plugin_module_paths – extra plugin module paths to search
title – plugin title, used in messages
title_plural – plural plugin title, used in messages
throw – throw on error?
log_error – log errors to stderr?
include_bases – include argument names from base classes?
- Raises:
PluginNotFoundError –
namescontained an unknown plugin nameModuleFileNotFoundError –
plugin_module_pathscontained a missing module
- Returns:
return-code, anything other than 0 is failure
- property plugin_module_paths: frozenset[str]
Every module path ever seen by
PluginLoader.load_plugin_modules().- Returns:
frozen set
- dgenerate.plugin.import_plugins(paths: Iterable[str])[source]
Set plugin paths that will be considered by all plugin loader instances.
- Parameters:
paths – environment modules, python script paths, directory paths
- dgenerate.plugin.load_modules(paths: Iterable[str]) list[module][source]
Load python modules from a folder, directly from a .py file, or from a python module installed in the environment. Cache them so that repeat requests for loading return an already loaded module.
- Raises:
ModuleFileNotFoundError – If a module path could not be found on disk, or when a module could not be loaded from the python environment.
- Parameters:
paths – list of folder/file paths, or references to python modules installed in the environment
- Returns:
list of
types.ModuleType
- dgenerate.plugin.LOADED_PLUGIN_MODULES: dict[str, module] = {}
Plugin module in memory cache
- dgenerate.plugin.PLUGIN_PATHS = {}
Plugin paths that are considered by all
PluginLoaderinstances.This should be updated with
import_plugins()
dgenerate.prompt module
Prompt representation object / prompt parsing.
- class dgenerate.prompt.Prompt(positive: str | None = None, negative: str | None = None, delimiter: str = ';')[source]
Bases:
objectRepresents a combined positive and optional negative prompt split by a delimiter character.
dgenerate.promptweighters module
- exception dgenerate.promptweighters.PromptWeighterArgumentError[source]
Bases:
PluginArgumentErrorThrown when a
dgenerate.promptweighters.PromptWeighterimplementation is loaded with an invalid argument.
- exception dgenerate.promptweighters.PromptWeighterHelpUsageError[source]
Bases:
ExceptionRaised on argument parse errors in
prompt_weighters_help()
- exception dgenerate.promptweighters.PromptWeighterNotFoundError[source]
Bases:
PluginNotFoundErrorThrown when a
dgenerate.promptweighters.PromptWeighterimplementation could not be found for a given name.
- exception dgenerate.promptweighters.PromptWeightingUnsupported[source]
Bases:
ExceptionThrown when a
dgenerate.promptweighters.PromptWeighterimplementation cannot handle a specific pipeline or combination of pipeline arguments.
- class dgenerate.promptweighters.CompelPromptWeighter(syntax: str = 'compel', **kwargs)[source]
Bases:
PromptWeighterImplements prompt weighting syntax for Stable Diffusion 1/2 and Stable Diffusion XL using compel. The default syntax is “compel” which is analogous to the syntax used by InvokeAI.
Specifying the syntax “sdwui” will translate your prompt from Stable Diffusion Web UI syntax into compel / InvokeAI syntax before generating the prompt embeddings.
If you wish to use prompt syntax for weighting tokens that is similar to ComfyUI, Automatic1111, or CivitAI for example, use: ‘compel;syntax=sdwui’
The underlying weighting behavior for tokens is not exactly the same as other software that uses the more common “sdwui” syntax, so your prompt may need adjusting if you are reusing a prompt from those other pieces of software.
You can read about compel here: https://github.com/damian0815/compel
And InvokeAI here: https://github.com/invoke-ai/InvokeAI
This prompt weighter supports the model types:
NOWRAP! –model-type torch –model-type torch-pix2pix –model-type torch-upscaler-x4 –model-type torch-sdxl –model-type torch-sdxl-pix2pix
The secondary prompt option for SDXL –sdxl-second-prompts is supported by this prompt weighter implementation. However, –sdxl-refiner-second-prompts is not supported and will be ignored with a warning message.
- __init__(syntax: str = 'compel', **kwargs)[source]
- Parameters:
loaded_by_name – The name the prompt weighter was loaded by
model_type – Model type enum
dgenerate.ModelTypepipeline_type – Pipeline type enum
dgenerate.PipelineTypedtype – Data type enum
dgenerate.DataTypekwargs – child class forwarded arguments
- translate_to_embeds(pipeline, device: str, args: dict[str, any])[source]
Translate the pipeline prompt arguments to
prompt_embedsandpooled_prompt_embedsas needed.- Parameters:
pipeline – The pipeline object
device – The device the pipeline modules are on
args – Call arguments to the pipeline
- Returns:
args, supplemented with prompt embedding arguments
- NAMES = ['compel']
- class dgenerate.promptweighters.PromptWeighter(loaded_by_name: str, model_type: ModelType, pipeline_type: PipelineType, dtype: DataType, **kwargs)[source]
Bases:
PluginAbstract base class for prompt weighter implementations.
- __init__(loaded_by_name: str, model_type: ModelType, pipeline_type: PipelineType, dtype: DataType, **kwargs)[source]
- Parameters:
loaded_by_name – The name the prompt weighter was loaded by
model_type – Model type enum
dgenerate.ModelTypepipeline_type – Pipeline type enum
dgenerate.PipelineTypedtype – Data type enum
dgenerate.DataTypekwargs – child class forwarded arguments
- translate_to_embeds(pipeline, device: str, args: dict[str, any])[source]
Translate the pipeline prompt arguments to
prompt_embedsandpooled_prompt_embedsas needed.- Parameters:
pipeline – The pipeline object
device – The device the pipeline modules are on
args – Call arguments to the pipeline
- Returns:
args, supplemented with prompt embedding arguments
- HIDE_ARGS = ['model-type', 'pipeline-type', 'dtype']
- property pipeline_type: PipelineType
- class dgenerate.promptweighters.PromptWeighterLoader[source]
Bases:
PluginLoaderLoads
dgenerate.promptweighters.PromptWeighterplugins.- load(uri: str, **kwargs) PromptWeighter[source]
Load an plugin using a URI string containing its name and arguments.
- Parameters:
uri – The URI string
kwargs – default argument values, will be override by arguments specified in the URI
- Raises:
ValueError – If uri is
NoneRuntimeError – If a plugin is discovered to be using a reserved argument name upon loading it.
PluginArgumentError – If there is an error in the loading arguments for the plugin.
PluginNotFoundError – If the plugin name mentioned in the URI could not be found.
- Returns:
plugin instance
- class dgenerate.promptweighters.SdEmbedPromptWeighter(**kwargs)[source]
Bases:
PromptWeighterImplements prompt weighting syntax for Stable Diffusion 1/2, Stable Diffusion XL, and Stable Diffusion 3 using sd_embed.
sd_embed uses a Stable Diffusion Web UI compatible prompt syntax.
See: https://github.com/xhinker/sd_embed
NOWRAP! @misc{sd_embed_2024,
author = {Shudong Zhu(Andrew Zhu)}, title = {Long Prompt Weighted Stable Diffusion Embedding}, howpublished = {url{https://github.com/xhinker/sd_embed}}, year = {2024},
}
NOWRAP! –model-type torch –model-type torch-pix2pix –model-type torch-upscaler-x4 –model-type torch-sdxl –model-type torch-sdxl-pix2pix –model-type torch-sd3
The secondary prompt option for SDXL –sdxl-second-prompts is supported by this prompt weighter implementation. However, –sdxl-refiner-second-prompts is not supported and will be ignored with a warning message.
The secondary prompt option for SD3 –sd3-second-prompts is not supported by this prompt weighter implementation. Neither is –sd3-third-prompts. The prompts from these arguments will be ignored.
- __init__(**kwargs)[source]
- Parameters:
loaded_by_name – The name the prompt weighter was loaded by
model_type – Model type enum
dgenerate.ModelTypepipeline_type – Pipeline type enum
dgenerate.PipelineTypedtype – Data type enum
dgenerate.DataTypekwargs – child class forwarded arguments
- translate_to_embeds(pipeline, device: str, args: dict[str, any])[source]
Translate the pipeline prompt arguments to
prompt_embedsandpooled_prompt_embedsas needed.- Parameters:
pipeline – The pipeline object
device – The device the pipeline modules are on
args – Call arguments to the pipeline
- Returns:
args, supplemented with prompt embedding arguments
- NAMES = ['sd-embed']
- dgenerate.promptweighters.create_prompt_weighter(uri, model_type: ModelType, pipeline_type: PipelineType, dtype: DataType) PromptWeighter[source]
Create a prompt weighter implementation using the default
PromptWeighterLoaderinstance.- Parameters:
uri – The prompt weighter URI
model_type – Model type the prompt weighter is expected to handle
pipeline_type – Pipeline type the prompt weighter is expected to handle
dtype – The dtype of the pipeline
- Returns:
A
PromptWeighterimplementation
- dgenerate.promptweighters.prompt_weighter_exists(uri)[source]
Check if a prompt weighter implementation exists for a given URI.
This uses the default
PromptWeighterLoaderinstance.- Parameters:
uri – The prompt weighter URI
- Returns:
TrueorFalse
- dgenerate.promptweighters.prompt_weighter_help(names: Sequence[str], plugin_module_paths: Sequence[str] | None = None, throw=False, log_error=True)[source]
Implements
--prompt-weighter-helpcommand line option- Parameters:
names – arguments (prompt weighter names, or empty list)
plugin_module_paths – extra plugin module paths to search
throw – throw on error? or simply print to stderr and return a return code.
log_error – log errors to stderr?
- Raises:
- Returns:
return-code, anything other than 0 is failure
- dgenerate.promptweighters.prompt_weighter_name_from_uri(uri)[source]
Extract just the implementation name from a prompt weighter URI.
- Parameters:
uri – the URI
- Returns:
the implementation name.
- dgenerate.promptweighters.prompt_weighter_names()[source]
Implementation names for all prompt weighters implemented by dgenerate, which are visible to the default
PromptWeighterLoaderinstance.- Returns:
a list of prompt weighter implementation names.
dgenerate.pygments module
This module provides a pygments lexer for the dgenerate config / shell language.
This can be used for syntax highlighting.
- class dgenerate.pygments.DgenerateLexer(*args, **kwds)[source]
Bases:
RegexLexerpygments lexer for dgenerate configuration / script
- get_tokens_unprocessed(text, stack=('root',))
Split
textinto (tokentype, text) pairs.stackis the initial stack (default:['root'])
- aliases = ['dgenerate']
A list of short, unique identifiers that can be used to look up the lexer from a list, e.g., using get_lexer_by_name().
- filenames = ['*.dgen']
A list of fnmatch patterns that match filenames which contain content for this lexer. The patterns in this list should be unique among all lexers.
- name = 'DgenerateLexer'
Full name of the lexer, in human-readable form
dgenerate.renderloop module
The main dgenerate render loop, which implements the primary functionality of dgenerate.
- exception dgenerate.renderloop.RenderLoopConfigError[source]
Bases:
ExceptionRaised by
RenderLoopConfig.check()on configuration errors.
- class dgenerate.renderloop.AnimationETAEvent(origin, frame_index: int, total_frames: int, eta: timedelta)[source]
Bases:
EventCommon event stream object produced by the
events()event stream of a render loop.Occurs when there is an update about the estimated finish time of an animation being generated.
- eta: timedelta
Current estimated time to complete the animation.
- frame_index: int
Frame index at which the ETA was calculated.
- total_frames: int
Total frames needed for the animation to complete.
- class dgenerate.renderloop.AnimationFileFinishedEvent(origin: RenderLoop, path: str, config_filename: str, starting_event: StartingAnimationFileEvent)[source]
Bases:
EventGenerated in the event stream of
RenderLoop.events()Occurs when an animation (video or animated image) has finished being written to disk.
- __init__(origin: RenderLoop, path: str, config_filename: str, starting_event: StartingAnimationFileEvent)[source]
- config_filename: str | None
Path to a dgenerate config file if
output_configsis enabled.
- path: str
Path on disk where the video/animated image was saved.
- starting_event: StartingAnimationFileEvent
Animation
StartingAnimationFileEventrelated to this file finished event.
- class dgenerate.renderloop.AnimationFinishedEvent(origin, starting_event: StartingAnimationEvent)[source]
Bases:
EventCommon event stream object produced by the
events()event stream of a render loop.Occurs when a sequence of images that belong to an animation are done generating.
This occurs whether an animation was written to disk or not.
- __init__(origin, starting_event: StartingAnimationEvent)[source]
- starting_event: StartingAnimationEvent
Animation
StartingAnimationEventrelated to this file finished event.
- class dgenerate.renderloop.ImageFileSavedEvent(origin: RenderLoop, generated_event: ImageGeneratedEvent, path: str, config_filename: str | None = None)[source]
Bases:
EventGenerated in the event stream of
RenderLoop.events()Occurs when an image file is written to disk.
- __init__(origin: RenderLoop, generated_event: ImageGeneratedEvent, path: str, config_filename: str | None = None)[source]
- config_filename: str | None = None
Path to a dgenerate config file if
output_configsis enabled.
- generated_event: ImageGeneratedEvent
The
ImageGeneratedEventfor the image that was saved.
- path: str
Path to the saved image.
- class dgenerate.renderloop.ImageGeneratedEvent(origin: RenderLoop, image: Image | None, generation_step: int, batch_index: int, suggested_directory: str, suggested_filename: str, diffusion_args: DiffusionArguments, image_seed: ImageSeed, command_string: str, config_string: str)[source]
Bases:
EventGenerated in the event stream of
RenderLoop.events()Occurs when an image is generated (but not saved yet).
- __init__(origin: RenderLoop, image: Image | None, generation_step: int, batch_index: int, suggested_directory: str, suggested_filename: str, diffusion_args: DiffusionArguments, image_seed: ImageSeed, command_string: str, config_string: str)[source]
- batch_index: int
The index in the image batch for this image. Will only every be greater than zero if
RenderLoopConfig.batch_size> 1 andRenderLoopConfig.batch_grid_sizeisNone.
- command_string: str
Reproduction of a command line that can be used to reproduce this image.
This does not include the
--deviceargument.
- config_string: str
Reproduction of a dgenerate config file that can be used to reproduce this image.
This does not include the
--deviceargument.
- diffusion_args: DiffusionArguments
Diffusion argument object, contains
dgenerate.pipelinewrapper.DiffusionPipelineWrapperarguments used to produce this image.
- property frame_index: int | None
The frame index if this is an animation frame. Also available through image_seed.frame_index, though here for convenience.
- generation_step: int
The current generation step. (zero indexed)
- image: Image
The generated image.
- image_seed: ImageSeed | None
If an
--image-seedsspecification was used in the generation of this image, this object represents that image seed and contains the images that contributed to the generation of this image.
- property is_animation_frame: bool
Is this image a frame in an animation?
- suggested_directory: str
A suggested directory path for saving this image in.
A value of
'.'may be present, this indicates the current working directory.
- suggested_filename: str
A suggested filename for saving this image as. This filename will be unique to the render loop run / configuration. This filename will not contain
RenderLoopConfig.output_path, it is the suggested filename by itself.
- class dgenerate.renderloop.RenderLoop(config: RenderLoopConfig | None = None, image_processor_loader: ImageProcessorLoader | None = None, prompt_weighter_loader: PromptWeighterLoader | None = None)[source]
Bases:
objectRender loop which implements the bulk of dgenerates rendering capability.
This object handles the scatter gun iteration over requested diffusion parameters, the generation of animations, and writing images and media to disk or providing those to library users through callbacks.
- __init__(config: RenderLoopConfig | None = None, image_processor_loader: ImageProcessorLoader | None = None, prompt_weighter_loader: PromptWeighterLoader | None = None)[source]
- Parameters:
config –
RenderLoopConfigordgenerate.arguments.DgenerateArguments. IfNoneis provided, aRenderLoopConfiginstance will be created and assigned toRenderLoop.config.image_processor_loader –
dgenerate.imageprocessors.ImageProcessorLoader. IfNoneis provided, an instance will be created and assigned toRenderLoop.image_processor_loader.prompt_weighter_loader –
dgenerate.promptweighters.PromptWeighterLoader. IfNoneis provided, an instance will be created and assigned toRenderLoop.prompt_weighter_loader.
- events() Generator[ImageGeneratedEvent | StartingAnimationEvent | StartingAnimationFileEvent | AnimationFileFinishedEvent | ImageFileSavedEvent | AnimationFinishedEvent | StartingGenerationStepEvent | AnimationETAEvent, None, None][source]
Run the render loop, and iterate over a stream of event objects produced by the render loop.
Event objects are of the union type
RenderLoopEventThe exceptions mentioned here are those you may encounter upon iterating, they will not occur upon simple acquisition of the event stream iterator.
- Raises:
- Returns:
- run()[source]
Run the diffusion loop, this calls
RenderLoopConfig.check()prior to running.
- config: RenderLoopConfig
Render loop configuration.
- disable_writes: bool = False
Disable or enable all writes to disk, if you intend to only ever use the event stream of the render loop when using dgenerate as a library, this is a useful option.
RenderLoop.written_imagesandRenderLoop.written_animationswill not be available if writes to disk are disabled.
- property generation_step
Returns the current generation step, (zero indexed)
- image_processor_loader: ImageProcessorLoader
Responsible for loading any image processors referenced in the render loop configuration.
- model_extra_modules: dict[str, Any] = None
Extra raw diffusers modules to use in the creation of the main model pipeline.
- property pipeline_wrapper: DiffusionPipelineWrapper
Get the last used
dgenerate.pipelinewrapper.DiffusionPipelineWrapperinstance.Will be
NoneifRenderLoop.run()has never been called.- Returns:
- prompt_weighter_loader: PromptWeighterLoader
Responsible for loading any prompt weighters referenced in the render loop configuration.
- second_model_extra_modules: dict[str, Any] = None
Extra raw diffusers modules to use in the creation of any refiner or stable cascade decoder model pipeline.
- property written_animations: Iterable[str]
Iterable over animation filenames written by the last run
- property written_images: Iterable[str]
Iterable over image filenames written by the last run
- class dgenerate.renderloop.RenderLoopConfig[source]
Bases:
SetFromMixinThis object represents configuration for
RenderLoop.It nearly directly maps to dgenerates command line arguments.
See subclass
dgenerate.arguments.DgenerateArguments- calculate_generation_steps()[source]
Calculate the number of generation steps that this configuration results in.
- Returns:
int
- check(attribute_namer: Callable[[str], str] = None)[source]
Check the configuration for type and logical usage errors, set defaults for certain values when needed and not specified.
- Raises:
RenderLoopConfigError – on errors
- Parameters:
attribute_namer – Callable for naming attributes mentioned in exception messages
- iterate_diffusion_args(**overrides) Iterator[DiffusionArguments][source]
Iterate over
dgenerate.pipelinewrapper.DiffusionArgumentsargument objects using every combination of argument values provided for that object by this configuration.- Parameters:
overrides – use key word arguments to override specific attributes of this object with a new list value.
- Returns:
an iterator over
dgenerate.pipelinewrapper.DiffusionArguments
- animation_format: str = 'mp4'
Format for any rendered animations, see:
dgenerate.mediaoutput.supported_animation_writer_formats(). This value may also be set to ‘frames’ to indicate that only individual frames should be output and no animation file coalesced. This corresponds to the--animation-formatargument of the dgenerate command line tool.
- auth_token: str | None = None
Optional huggingface API token which will allow the download of restricted repositories that your huggingface account has been granted access to. This corresponds to the
--auth-tokenargument of the dgenerate command line tool.
- batch_grid_size: tuple[int, int] | None = None
Optional image grid size specification for when batch_size is greater than one. This is the
--batch-grid-sizeargument of the dgenerate command line tool.
- batch_size: int | None = None
Image generation batch size,
--batch-sizeargument of dgenerate command line tool.
- clip_skips: Sequence[int] | None = None
List of clip skip values. Clip skip is the number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that the output of the pre-final layer will be used for computing the prompt embeddings. Only supported for
model_typevaluestorchandtorch-sdxl, including withcontrol_net_urisdefined.
- control_image_processors: Sequence[str] | None = None
Corresponds to the
--control-image-processorsargument of the dgenerate command line tool verbatim, including the grouping syntax using the “+” symbol, the plus symbol should be used as its own list element, IE: it is a token.
- control_net_uris: Sequence[str] | None = None
Optional user specified ControlNet URIs, this corresponds to the
--control-netsargument of the dgenerate command line tool.
- device: str = 'cuda'
Processing device specification, for example “cuda” or “cuda:N” where N is an alternate GPU id as reported by nvidia-smi if you want to specify a specific GPU. This corresponds to the
--deviceargument of the dgenerate command line tool.
- dtype: DataType = 0
Primary model data type specification, IE: integer precision. Default is auto selection. Lower precision datatypes result in less GPU memory usage. This corresponds to the
--dtypeargument of the dgenerate command line tool.
- frame_end: int | None = None
Optional end frame inclusive frame slice for any rendered animations. This corresponds to the
--frame-endargument of the dgenerate command line tool.
- frame_start: int = 0
Start frame inclusive frame slice for any rendered animations. This corresponds to the
--frame-startargument of the dgenerate command line tool.
- guidance_rescales: Sequence[float] | None = None
List of floating point guidance rescale values which are supported by some pipelines, (there will be an error if it is unsupported upon running), this corresponds to the
--guidance-rescalesargument of the dgenerate command line tool.
- guidance_scales: Sequence[float]
List of floating point guidance scales, this corresponds to the
--guidance-scalesargument of the dgenerate command line tool.
- image_format: str = 'png'
Format for any images that are written including animation frames.
Anything other than “png” is not compatible with
output_metadata=Trueand aRenderLoopConfigErrorwill be raised upon running the render loop ifoutput_metadata=Trueand this value is not “png”
- image_guidance_scales: Sequence[float] | None = None
Optional list of floating point image guidance scales, used for pix2pix model types, this corresponds to the
--image-guidance-scalesargument of the dgenerate command line tool.
- image_seed_strengths: Sequence[float] | None = None
Optional list of floating point image seed strengths, this corresponds to the
--image-seed-strengthsargument of the dgenerate command line tool.
- image_seeds: Sequence[str] | None = None
List of
--image-seedsURI strings.
- inference_steps: Sequence[int]
List of inference steps values, this corresponds to the
--inference-stepsargument of the dgenerate command line tool.
- lora_uris: Sequence[str] | None = None
Optional user specified LoRA URIs, this corresponds to the
--lorasargument of the dgenerate command line tool.
- mask_image_processors: Sequence[str] | None = None
Corresponds to the
--mask-image-processorsargument of the dgenerate command line tool verbatim.
- model_cpu_offload: bool = False
Force model cpu offloading for the main pipeline, this may reduce memory consumption and allow large models to run when they would otherwise not fit in your GPUs VRAM. Inference will be slower. Mutually exclusive with
RenderLoopConfig.model_sequential_offload
- model_path: str | None = None
Primary diffusion model path,
model_pathargument of dgenerate command line tool.
- model_sequential_offload: bool = False
Force sequential model offloading for the main pipeline, this may drastically reduce memory consumption and allow large models to run when they would otherwise not fit in your GPUs VRAM. Inference will be much slower. Mutually exclusive with
RenderLoopConfig.model_cpu_offload
- model_type: ModelType = 0
Corresponds to the
--model-typeargument of the dgenerate command line tool.
- no_aspect: bool = False
Should Seed, Mask, and Control guidance images specified in
RenderLoopConfig.image_seedsdefinitions (--image-seeds) have their aspect ratio ignored when being resized toRenderLoopConfig.output_size(--output-size) ?
- no_frames: bool = False
Should individual frames not be output when rendering an animation? defaults to False. This corresponds to the
--no-framesargument of the dgenerate command line tool. Using this option whenRenderLoopConfig.animation_formatis equal to"frames"will cause aRenderLoopConfigErrorbe raised.
- offline_mode: bool = False
Avoid ever connecting to the huggingface API? this can be used if all your models are cached or if you are only ever using models that exist on disk. Corresponds to the
--offline-modeargument of the dgenerate command line tool.
- output_configs: bool = False
Output a config text file next to every generated image or animation? this file will contain configuration that is pipeable to dgenerate stdin, which will reproduce said file. This corresponds to the
--output-configsargument of the dgenerate command line tool.
- output_metadata: bool = False
Write config text to the metadata of all written images? this data is not written to animated files, only PNGs. This corresponds to the
--output-metadataargument of the dgenerate command line tool.
- output_overwrite: bool = False
Allow overwrites of files? or avoid this with a file suffix in a multiprocess safe manner? This corresponds to the
--output-overwriteargument of the dgenerate command line tool.
- output_path: str = 'output'
Render loop write folder, where images and animations will be written. This corresponds to the
--output-pathargument of the dgenerate command line tool.
- output_prefix: str | None = None
Output filename prefix, add an optional prefix string to all written files. This corresponds to the
--output-prefixargument of the dgenerate command line tool.
- output_size: tuple[int, int] | None = None
Desired output size, sizes not aligned by 8 pixels will result in an error message. This corresponds to the
--output-sizeargument of the dgenerate command line tool.
- parsed_image_seeds: Sequence[ImageSeedParseResult] | None = None
The results of parsing URIs mentioned in
RenderLoopConfig.image_seeds, will only be available ifRenderLoopConfig.check()has been called.
- post_processors: Sequence[str] | None = None
Corresponds to the
--post-processorsargument of the dgenerate command line tool verbatim.
- prompt_weighter_uri: str | None = None
The URI of a prompt weighter implementation supported by dgenerate.
This corresponds to the
--prompt-weighterargument of the dgenerate command line tool.
- prompts: Sequence[Prompt]
List of prompt objects, this corresponds to the
--promptsargument of the dgenerate command line tool.
- revision: str = 'main'
Repo revision selector for the main model when loading from a huggingface repository. This corresponds to the
--revisionargument of the dgenerate command line tool.
- s_cascade_decoder_cpu_offload: bool | None = None
Force model cpu offloading for the Stable Cascade decoder pipeline, this may reduce memory consumption and allow large models to run when they would otherwise not fit in your GPUs VRAM. Inference will be slower. Mutually exclusive with
RenderLoopConfig.s_cascade_decoder_sequential_offload
- s_cascade_decoder_guidance_scales: Sequence[float] | None = None
List of guidance scale values for the Stable Cascade decoder, this corresponds to the
--s-cascade-decoder-guidance-scalesargument of the dgenerate command line tool.
- s_cascade_decoder_inference_steps: Sequence[int] | None = None
List of inference steps values for the Stable Cascade decoder, this corresponds to the
--s-cascade-decoder-inference-stepsargument of the dgenerate command line tool.
- s_cascade_decoder_prompts: Sequence[Prompt] | None = None
Optional list of Stable Cascade decoder prompt overrides, this corresponds to the
--s-cascade-decoder-promptsargument of the dgenerate command line tool.
- s_cascade_decoder_scheduler: str | None = None
Optional Stable Cascade decoder model scheduler/sampler class name specification, this corresponds to the
--s-cascade-decoder-schedulerargument of the dgenerate command line tool. Setting this to ‘help’ will yield a help message to stdout describing scheduler names compatible with the current configuration upon running. Passing ‘helpargs’ will yield a help message with a list of overridable arguments for each scheduler and their typical defaults.
- s_cascade_decoder_sequential_offload: bool | None = None
Force sequential model offloading for the Stable Cascade decoder pipeline, this may drastically reduce memory consumption and allow large models to run when they would otherwise not fit in your GPUs VRAM. Inference will be much slower. Mutually exclusive with
RenderLoopConfig.s_cascade_decoder_cpu_offload
- s_cascade_decoder_uri: str | None = None
Stable Cascade model URI,
--s-cascade-decoderargument of dgenerate command line tool.
- safety_checker: bool = False
Enable safety checker?
--safety-checker
- scheduler: str | None = None
Optional primary model scheduler/sampler class name specification, this corresponds to the
--schedulerargument of the dgenerate command line tool. Setting this to ‘help’ will yield a help message to stdout describing scheduler names compatible with the current configuration upon running. Passing ‘helpargs’ will yield a help message with a list of overridable arguments for each scheduler and their typical defaults.
- sd3_max_sequence_length: int | None = None
Max number of prompt tokens that the T5EncoderModel (text encoder 3) of Stable Diffusion can handle.
This defaults to 256 when not specified, and the maximum value is 512 and the minimum value is 1.
High values result in more resource usage and processing time.
- sd3_second_prompts: Sequence[Prompt] | None = None
Optional list of SD3 secondary prompts, this corresponds to the
--sd3-second-promptsargument of the dgenerate command line tool.
- sd3_third_prompts: Sequence[Prompt] | None = None
Optional list of SD3 tertiary prompts, this corresponds to the
--sd3-third-promptsargument of the dgenerate command line tool.
- sdxl_aesthetic_scores: Sequence[float] | None = None
Optional list of SDXL aesthetic-score conditioning values, this corresponds to the
--sdxl-aesthetic-scoresargument of the dgenerate command line tool.
- sdxl_crops_coords_top_left: Sequence[tuple[int, int]] | None = None
Optional list of SDXL top-left-crop-coords micro-conditioning parameters, this corresponds to the
--sdxl-crops-coords-top-leftargument of the dgenerate command line tool.
- sdxl_high_noise_fractions: Sequence[float] | None = None
Optional list of SDXL refiner high noise fractions (floats), this value is the fraction of inference steps that the base model handles, the inverse proportion of the provided fraction is handled by the refiner model. This corresponds to the
--sdxl-high-noise-fractionsargument of the dgenerate command line tool.
- sdxl_negative_aesthetic_scores: Sequence[float] | None = None
Optional list of negative influence SDXL aesthetic-score conditioning values, this corresponds to the
--sdxl-negative-aesthetic-scoresargument of the dgenerate command line tool.
- sdxl_negative_crops_coords_top_left: Sequence[tuple[int, int]] | None = None
Optional list of negative influence SDXL top-left crop coords micro-conditioning parameters, this corresponds to the
--sdxl-negative-crops-coords-top-leftargument of the dgenerate command line tool.
- sdxl_negative_original_sizes: Sequence[tuple[int, int]] | None = None
Optional list of negative influence SDXL original-size micro-conditioning parameters, this corresponds to the
--sdxl-negative-original-sizesargument of the dgenerate command line tool.
- sdxl_negative_target_sizes: Sequence[tuple[int, int]] | None = None
Optional list of negative influence SDXL target-size micro-conditioning parameters, this corresponds to the
--sdxl-negative-target-sizesargument of the dgenerate command line tool.
- sdxl_original_sizes: Sequence[tuple[int, int]] | None = None
Optional list of SDXL original-size micro-conditioning parameters, this corresponds to the
--sdxl-original-sizesargument of the dgenerate command line tool.
- sdxl_refiner_aesthetic_scores: Sequence[float] | None = None
Optional list of SDXL-refiner override aesthetic-score conditioning values, this corresponds to the
--sdxl-refiner-aesthetic-scoresargument of the dgenerate command line tool.
- sdxl_refiner_clip_skips: Sequence[int] | None = None
Clip skip override values for the SDXL refiner, which normally defaults to the clip skip value for the main model when it is defined.
- sdxl_refiner_cpu_offload: bool | None = None
Force model cpu offloading for the SDXL refiner pipeline, this may reduce memory consumption and allow large models to run when they would otherwise not fit in your GPUs VRAM. Inference will be slower. Mutually exclusive with
RenderLoopConfig.sdxl_refiner_sequential_offload
- sdxl_refiner_crops_coords_top_left: Sequence[tuple[int, int]] | None = None
Optional list of SDXL-refiner override top-left-crop-coords micro-conditioning parameters, this corresponds to the
--sdxl-refiner-crops-coords-top-leftargument of the dgenerate command line tool.
- sdxl_refiner_edit: bool | None = None
Force the SDXL refiner to operate in edit mode instead of cooperative denoising mode.
- sdxl_refiner_guidance_rescales: Sequence[float] | None = None
Optional list of guidance rescale value overrides for the SDXL refiner, this corresponds to the
--sdxl-refiner-guidance-rescalesargument of the dgenerate command line tool.
- sdxl_refiner_guidance_scales: Sequence[float] | None = None
Optional list of guidance scale value overrides for the SDXL refiner, this corresponds to the
--sdxl-refiner-guidance-scalesargument of the dgenerate command line tool.
- sdxl_refiner_inference_steps: Sequence[int] | None = None
Optional list of inference steps value overrides for the SDXL refiner, this corresponds to the
--sdxl-refiner-inference-stepsargument of the dgenerate command line tool.
- sdxl_refiner_negative_aesthetic_scores: Sequence[float] | None = None
Optional list of negative influence SDXL-refiner override aesthetic-score conditioning values, this corresponds to the
--sdxl-refiner-negative-aesthetic-scoresargument of the dgenerate command line tool.
- sdxl_refiner_negative_crops_coords_top_left: Sequence[tuple[int, int]] | None = None
Optional list of negative influence SDXL-refiner top-left crop coords micro-conditioning parameters, this corresponds to the
--sdxl-refiner-negative-crops-coords-top-leftargument of the dgenerate command line tool.
- sdxl_refiner_negative_original_sizes: Sequence[tuple[int, int]] | None = None
Optional list of negative influence SDXL-refiner override original-size micro-conditioning parameters, this corresponds to the
--sdxl-refiner-negative-original-sizesargument of the dgenerate command line tool.
- sdxl_refiner_negative_target_sizes: Sequence[tuple[int, int]] | None = None
Optional list of negative influence SDXL-refiner override target-size micro-conditioning parameters, this corresponds to the
--sdxl-refiner-negative-target-sizesargument of the dgenerate command line tool.
- sdxl_refiner_original_sizes: Sequence[tuple[int, int]] | None = None
Optional list of SDXL-refiner override original-size micro-conditioning parameters, this corresponds to the
--sdxl-refiner-original-sizesargument of the dgenerate command line tool.
- sdxl_refiner_prompts: Sequence[Prompt] | None = None
Optional list of SDXL refiner prompt overrides, this corresponds to the
--sdxl-refiner-promptsargument of the dgenerate command line tool.
- sdxl_refiner_scheduler: str | None = None
Optional SDXL refiner model scheduler/sampler class name specification, this corresponds to the
--sdxl-refiner-schedulerargument of the dgenerate command line tool. Setting this to ‘help’ will yield a help message to stdout describing scheduler names compatible with the current configuration upon running. Passing ‘helpargs’ will yield a help message with a list of overridable arguments for each scheduler and their typical defaults.
- sdxl_refiner_second_prompts: Sequence[Prompt] | None = None
Optional list of SDXL refiner secondary prompt overrides, this corresponds to the
--sdxl-refiner-second-promptsargument of the dgenerate command line tool.
- sdxl_refiner_sequential_offload: bool | None = None
Force sequential model offloading for the SDXL refiner pipeline, this may drastically reduce memory consumption and allow large models to run when they would otherwise not fit in your GPUs VRAM. Inference will be much slower. Mutually exclusive with
RenderLoopConfig.sdxl_refiner_cpu_offload
- sdxl_refiner_target_sizes: Sequence[tuple[int, int]] | None = None
Optional list of SDXL-refiner override target-size micro-conditioning parameters, this corresponds to the
--sdxl-refiner-target-sizesargument of the dgenerate command line tool.
- sdxl_refiner_uri: str | None = None
SDXL Refiner model URI,
--sdxl-refinerargument of dgenerate command line tool.
- sdxl_second_prompts: Sequence[Prompt] | None = None
Optional list of SDXL secondary prompts, this corresponds to the
--sdxl-second-promptsargument of the dgenerate command line tool.
- sdxl_target_sizes: Sequence[tuple[int, int]] | None = None
Optional list of SDXL target-size micro-conditioning parameters, this corresponds to the
--sdxl-target-sizesargument of the dgenerate command line tool.
- second_text_encoder_uris: Sequence[str] | None = None
Optional user specified Text Encoder URIs, this corresponds to the
--text-encoders2argument of the dgenerate command line tool. This specifies text encoders for the SDXL refiner or Stable Cascade decoder.
- second_unet_uri: str | None = None
Optional user specified second UNet URI, this corresponds to the
--unet2argument of the dgenerate command line tool. This UNet uri will be used for the SDXL refiner or Stable Cascade decoder model.
- seed_image_processors: Sequence[str] | None = None
Corresponds to the
--seed-image-processorsargument of the dgenerate command line tool verbatim.
- seeds: Sequence[int]
List of integer seeds, this corresponds to the
--seedsargument of the dgenerate command line tool.
- seeds_to_images: bool = False
Should
RenderLoopConfig.seedsbe interpreted as seeds for each image input instead of combinatorial? this includes control images.
- subfolder: str | None = None
Primary model subfolder argument,
--subfolderargument of dgenerate command line tool.
- text_encoder_uris: Sequence[str] | None = None
Optional user specified Text Encoder URIs, this corresponds to the
--text-encodersargument of the dgenerate command line tool.
- textual_inversion_uris: Sequence[str] | None = None
Optional user specified Textual Inversion URIs, this corresponds to the
--textual-inversionsargument of the dgenerate command line tool.
- unet_uri: str | None = None
Optional user specified UNet URI, this corresponds to the
--unetargument of the dgenerate command line tool.
- upscaler_noise_levels: Sequence[int] | None = None
Optional list of integer upscaler noise levels, this corresponds to the
--upscaler-noise-levelsargument of the dgenerate command line tool that is used for thedgenerate.pipelinewrapper.ModelType.TORCH_UPSCALER_X4model type only.
- vae_slicing: bool = False
Enable VAE slicing?
--vae-slicing
- vae_tiling: bool = False
Enable VAE tiling?
--vae-tiling
- vae_uri: str | None = None
Optional user specified VAE URI, this corresponds to the
--vaeargument of the dgenerate command line tool.
- variant: str | None = None
Primary model weights variant string. This corresponds to the
--variantargument of the dgenerate command line tool.
- class dgenerate.renderloop.StartingAnimationEvent(origin, total_frames: int, fps: float, frame_duration: float)[source]
Bases:
EventCommon event stream object produced by the
events()event stream of a render loop.Occurs when a sequence of images that belong to an animation are about to start being generated.
This occurs whether an animation is going to be written to disk or not.
- fps: float
FPS of the generated file.
- frame_duration: float
Frame duration of the generated file, (the time a frame is visible in milliseconds)
- total_frames: int
Number of frames written.
- class dgenerate.renderloop.StartingAnimationFileEvent(origin, path: str, total_frames: int, fps: float, frame_duration: float)[source]
Bases:
EventCommon event stream object produced by the
events()event stream of a render loop.Occurs when a sequence of images that belong to an animation are about to start being written to a file.
- fps: float
FPS of the generated file.
- frame_duration: float
Frame duration of the generated file, (the time a frame is visible in milliseconds)
- path: str
File path where the animation will reside.
- total_frames: int
Number of frames written.
- class dgenerate.renderloop.StartingGenerationStepEvent(origin, generation_step: int, total_steps: int)[source]
Bases:
EventCommon event stream object produced by the
events()event stream of a render loop.Occurs when a generation step is starting, a generation step may produce multiple images and or an animation.
- generation_step: int
The generation step number.
- total_steps: int
The total number of steps that are needed to complete the render loop.
- dgenerate.renderloop.gen_seeds(n) list[int][source]
Generate a list of N random seed integers
- Parameters:
n – number of seeds to generate
- Returns:
list of integer seeds
- dgenerate.renderloop.RenderLoopEvent
Possible events from the event stream created by
RenderLoop.events()alias of
Union[ImageGeneratedEvent,StartingAnimationEvent,StartingAnimationFileEvent,AnimationFileFinishedEvent,ImageFileSavedEvent,AnimationFinishedEvent,StartingGenerationStepEvent,AnimationETAEvent]
- dgenerate.renderloop.RenderLoopEventStream
Event stream created by
RenderLoop.events()alias of
Generator[Union[ImageGeneratedEvent,StartingAnimationEvent,StartingAnimationFileEvent,AnimationFileFinishedEvent,ImageFileSavedEvent,AnimationFinishedEvent,StartingGenerationStepEvent,AnimationETAEvent],None,None]
dgenerate.textprocessing module
Text processing, console text rendering, and parsing utilities. URI parser, and reusable tokenization.
- exception dgenerate.textprocessing.ConceptUriParseError[source]
Bases:
ExceptionRaised by
ConceptUriParser.parse()on parsing errors.
- exception dgenerate.textprocessing.ShellParseSyntaxError[source]
Bases:
ExceptionRaised by
shell_parse()on syntax errors.
- exception dgenerate.textprocessing.TimeDeltaParseError[source]
Bases:
ExceptionRaised by
parse_timedelta()on parse errors.
- exception dgenerate.textprocessing.TokenizedSplitSyntaxError[source]
Bases:
ExceptionRaised by
tokenized_split()on syntax errors.
- exception dgenerate.textprocessing.UnquoteSyntaxError[source]
Bases:
ExceptionRaised by
unquote()on parsing errors.
- class dgenerate.textprocessing.ConceptUri(concept: str, args: dict[str, str])[source]
Bases:
objectRepresents a parsed concept URI.
- args: dict[str, str | list[str]]
Provided keyword arguments with their (string) values.
- concept: str
The primary concept mentioned in the URI.
- class dgenerate.textprocessing.ConceptUriParser(concept_name: str, known_args: Iterable[str], args_lists: bool | Iterable[str] | None = None, args_raw: bool | Iterable[str] | None = None)[source]
Bases:
objectParser for dgenerate concept paths with arguments, IE:
concept;arg1="a";arg2="b"Used for
--vae,--lorasetc. as well as image processor plugin module arguments.- __init__(concept_name: str, known_args: Iterable[str], args_lists: bool | Iterable[str] | None = None, args_raw: bool | Iterable[str] | None = None)[source]
- Raises:
ValueError – if duplicate argument names are specified.
- Parameters:
concept_name – Concept name, used in error messages
known_args – valid arguments for the parser, must be unique
- parse(uri: str) ConceptUri[source]
Parse a string.
- Parameters:
uri – the string
- Raises:
ConceptUriParseError – on parsing errors
ValueError – if uri is
None
- Returns:
ConceptPath
- args_lists: bool | set[str] | None
Trueindicates all arguments can accept a comma separated list.NoneorFalseindicates no arguments can accept a comma separated list.Assigning a set containing argument names indicates only the specified arguments can accept a comma separated list.
When an argument is parsed as a comma separated list, its value/type in
ConceptUri.argswill be that of a list.
- args_raw: bool | set[str] | None
Trueindicates all argument values are returned without any unquoting or processing into lists.NoneorFalseindicates no argument values skip extended processing.Assigning a set containing argument names indicates only the specified arguments skip extended processing (unquoting or splitting).
- concept_name: str
Name / title string for this concept. Used in parse error exceptions.
- known_args: set[str]
Unique recognized keyword arguments
- dgenerate.textprocessing.contains_space(string: str) bool[source]
Check if a string contains any whitespace characters including newlines
- Parameters:
string – the string
- Returns:
bool
- dgenerate.textprocessing.dashdown(string: str) str[source]
Replace ‘-’ with ‘_’
- Parameters:
string – the string
- Returns:
modified string
- dgenerate.textprocessing.dashup(string: str) str[source]
Replace ‘_’ with ‘-’
- Parameters:
string – the string
- Returns:
modified string
- dgenerate.textprocessing.debug_format_args(args_dict: dict[str, Any], value_transformer: Callable[[str, Any], str] | None = None, max_value_len: int = 256, as_kwargs: bool = False)[source]
Format function arguments in a way that can be printed for debug messages.
- Parameters:
args_dict – argument dictionary
value_transformer – transform values in the argument dictionary
max_value_len – Max length of a formatted value before it is turned into a class and id string only
as_kwargs – Format the string as python keyword arguments instead of a dictionary
- Returns:
formatted string
- dgenerate.textprocessing.format_dgenerate_config(lines: Iterator[str], indentation=' ') Iterator[str][source]
A very rudimentary code formatter for dgenerate configuration / script.
Does not handle breaking jinja control blocks on to a new line if multiple start blocks exist on the same line.
- Parameters:
lines – iterator over lines
indentation – level of indentation for top level jinja control blocks
- Returns:
formatted code
- dgenerate.textprocessing.format_image_seed_uri(seed_image: str | None, inpaint_image: str | None = None, control_images: str | None = None, resize: str | tuple[int | str, int | str] | None = None, aspect: bool = True, frame_start: int | None = None, frame_end: int | None = None) str[source]
Formats a dgenerate
--image-seedsURI to its shortest possible string form.A control image / images specifier can be specified alone.
An inpaint image must have an accompanying seed image.
- Raises:
ValueError – if
inpaint_imageis specified withoutseed_image. if keyword arguments are present withoutseed_imageorcontrol_images. ifframe_startorframe_endare negative values. ifframe_startis greater thanframe_end.- Parameters:
seed_image – Seed image path
inpaint_image – Inpaint image path
control_images – Single control image path, or a paths string with multiple paths separated by the
,character.resize – Optional resize dimension (WxH string)
aspect – Preserve aspect ratio?
frame_start – Optional frame start index
frame_end – Optional frame end index
- Returns:
The generated
--image-seedsURI string
- dgenerate.textprocessing.format_size(size: Iterable[int])[source]
Join together an iterable of integers with the character x
- Parameters:
size – the iterable
- Returns:
formatted string
- dgenerate.textprocessing.indent_text(text, initial_indent: str | None = None, subsequent_indent: str | None = None)[source]
Indent consecutive lines of text.
- Parameters:
text – Text to be indented
initial_indent – String of characters to be used for the initial indentation
subsequent_indent – String of characters to be used for the subsequent indentation
- Returns:
Indented text
- dgenerate.textprocessing.is_quoted(string: str) bool[source]
Return
Trueif a string is quoted with an identical starting and end quote.- Parameters:
string – the string
- Returns:
TrueorFalse
- dgenerate.textprocessing.justify_left(string: str)[source]
Justify text to the left.
- Parameters:
string – string with text
- Returns:
left justified text
- dgenerate.textprocessing.long_text_wrap_width() int[source]
Return the current terminal width or the default value of 150 characters for text-wrapping purposes.
This can be affected by the environmental variable
COLUMNS.- Raises:
ValueError – if the environmental variable
COLUMNSis not an integer value or is less than 0.- Returns:
int
- dgenerate.textprocessing.oxford_comma(elements: Collection[str], conjunction: str) str[source]
Join a sequence of strings with commas, end with an oxford comma and conjunction if needed.
- Parameters:
elements – strings
conjunction – “and”, “or”
- Returns:
a joined string
- dgenerate.textprocessing.parse_dimensions(string)[source]
Parse a dimensions tuple from a string, integers seperated by the character ‘x’
- Parameters:
string – the string
- Raises:
ValueError – On non integer dimension values.
- Returns:
a tuple representing the dimensions
- dgenerate.textprocessing.parse_image_size(string)[source]
Parse an image size tuple from a string, 2 integers seperated by the character ‘x’, or a single integer specifying both dimensions.
- Parameters:
string – the string
- Raises:
ValueError – On non integer dimension values, or if more than 2 dimensions are provided, or if the product of the dimensions is 0.
- Returns:
a tuple representing the dimensions
- dgenerate.textprocessing.parse_timedelta(string: str | None) timedelta[source]
Parse a
datetime.timedeltaobject from an arguments string.Passing ‘forever’, an empty string, or
Nonewill result in this function returningdatetime.timedelta.maxAccepts all named arguments of
datetime.timedeltaparse_time_delta('days=1; seconds=30')
- Raises:
TimeDeltaParseError – on parse errors
- Parameters:
string – the arguments string
- Returns:
datetime.timedelta
- dgenerate.textprocessing.parse_version(string: str) tuple[int, int, int][source]
Parse a SemVer version string into a tuple of 3 integers
- Parameters:
string – the version string
- Returns:
tuple of three ints
- dgenerate.textprocessing.quote(string: str, char='"') str[source]
Wrap a string with a quote character.
Double quotes by default.
This is not equivalent to shell quoting.
- Parameters:
string – the string
char – the quote character to use
- Returns:
the quoted string
- dgenerate.textprocessing.quote_spaces(value_or_struct: Any | Iterable[Any | list | tuple]) list | tuple | Any[source]
Quote any
strtype values containing spaces, orstrtype values containing spaces within a list, or list of lists/tuples.The entire content of the data structure is stringified by this process.
This is not equivalent to shell quoting.
- Parameters:
value_or_struct – value or (list of values, and or lists/tuples containing values)
- Returns:
input data structure with strings quoted if needed
- dgenerate.textprocessing.remove_tail_comments(string) tuple[bool, str][source]
Remove trailing comments from a dgenerate config line
Will not remove a comment if it is the only thing on the line.
Considers strings and comment escape sequences.
- Parameters:
string – the string
- Returns:
(removed anything?, stripped string)
- dgenerate.textprocessing.remove_terminal_escape_sequences(string)[source]
Remove any terminal escape sequences from a string.
- Parameters:
string – the string
- Returns:
the clean string
- dgenerate.textprocessing.shell_expandvars(string: str)[source]
Expand shell variables of form
$var,${var},%var%in a string.Unknown variables expand to nothing.
- Parameters:
string – input string
- dgenerate.textprocessing.shell_parse(string, expand_home: bool = True, expand_vars: bool = True, expand_glob: bool = True, expand_vars_func: ~typing.Callable[[str], str] = <function shell_expandvars>) list[str][source]
Shell command line parsing, implements basic home directory expansion, globbing, and environmental variable expansion.
Globbing and home directory expansion do not occur inside strings.
This can be used in place of
shlex.split# basic glob shell_parse('command *.png') # recursive glob shell_parse('command dir/**/*.png') # home directory shell_parse('command ~') # home directory of user test shell_parse('command ~test') # everything under home directory shell_parse('command ~/*') # append text to every glob result shell_parse('command *".png"') # append text to every glob result shell_parse("command *'.png'") # environmental variable syntax 1 shell_parse('command $ENVVAR') # environmental variable syntax 2 shell_parse('command %ENVVAR%')
- Parameters:
string – String to parse
expand_home – Expand
~?expand_vars – Expand unix style
$and windows style%environmental variables?expand_glob – Expand
*glob expressions including recursive globs?expand_vars_func – This function is used to expand shell variables in a string, analogous to os.path.expandvars
- Returns:
shell arguments
- dgenerate.textprocessing.tokenized_split(string: str, separator: str | None, remove_quotes: bool = False, strict: bool = False, escapes_in_unquoted: bool = False, escapes_in_quoted: bool = False, single_quotes_raw: bool = False, double_quotes_raw: bool = False, string_expander: Callable[[str, str], str] = None, text_expander: Callable[[str], list[str]] = None, remove_stray_separators: bool = False, escapable_separator: bool = False, allow_unterminated_strings: bool = False, first_string_halts: bool = False) list[str][source]
Split a string by a separator and discard whitespace around tokens, avoid splitting within single or double-quoted strings. Empty fields may be used.
Quotes can be always be escaped with a backslash to avoid the creation of a string type token. The backslash will remain in the output if
escapes_in_unquotedorescapes_in_quotedareFalseand the escape occurs in the relevant context.- Raises:
TokenizedSplitSyntaxError – on syntax errors.
- Parameters:
string – the string
separator – separator
remove_quotes – remove quotes from quoted string tokens?
strict – Text tokens cannot be intermixed with quoted strings? disallow IE:
"text'string'text"escapes_in_unquoted – evaluate escape sequences in text tokens (unquoted strings)? The slash is retained by default when escaping quotes, this disables that, and also enables handling of the escapes
n, r, t, b, f, and \. IE: givenseparator =";"parse\"token\"; "a b"->['"token"', 'a b'], instead of\"token\"; "a b"->['\"token\"', 'a b']escapes_in_quoted – evaluate escape sequences in quoted string tokens? The slash is retained by default when escaping quotes, this disables that, and also enables handling of the escapes
n, r, t, b, f, and \. IE givenseparator = ";"parsetoken; "a \" b"->['token', 'a " b'], instead oftoken; "a \" b"->['token', 'a \" b']single_quotes_raw – Never evaluate escape sequences in single-quoted strings?
double_quotes_raw – Never evaluate escape sequences in double-quoted strings?
string_expander – User post process string expansion hook
string_expander(quote_char, string) -> strtext_expander – User post process text token expansion hook
text_expander(text_token) -> list[str]. should return a list of new tokens.remove_stray_separators – Remove consecutive seperator characters with no inner content at the end of the string? In effect, do not create entrys for empty seperators at the end of a string.
escapable_separator – The seperator character may be escaped with a backslash where it would otherwise cause a split?
allow_unterminated_strings – Allows the lex to end on an unterminated string without a syntax error being produced. It is necessary to preform lookahead N to determine if a seperator is quoted by a string or not, this allows your input to end with an unterminated string and still split correctly, complete strings proceeding the unterminated string which contain the seperator character will not be split on the seperator because the seperator is considered quoted in a string token.
first_string_halts – The first completed string token halts lexing immediately, this is mainly used by the lexer internally for recursion in cases where a lookahead for string termination is required, but may be useful for some external parsing tasks.
- Returns:
parsed fields
- dgenerate.textprocessing.underline(string: str, underline_char: str = '=') str[source]
Underline a string with the selected character.
- Parameters:
string – the string
underline_char – the character to underline with
- Returns:
the underlined string
- dgenerate.textprocessing.unquote(string: str, escapes_in_quoted=True, escapes_in_unquoted=False) str[source]
Remove quotes from a string, including single quotes.
Unquoted strings will have leading an trailing whitespace stripped.
Quoted strings will have leading and trailing whitespace stripped up to where the quotes were.
- Parameters:
escapes_in_unquoted – Render escape sequences in strings that are unquoted?
escapes_in_quoted – Render escape sequences in strings that are quoted?
string – the string
- Returns:
The un-quoted string
- dgenerate.textprocessing.wrap(text: str, width: int, initial_indent='', subsequent_indent='', break_long_words=False, break_on_hyphens=False, **fill_args)[source]
Wrap text.
- Parameters:
text – The prompt text
width – The wrap width
initial_indent – initial indent string
subsequent_indent – subsequent indent string
break_long_words – Break on long words?
break_on_hyphens – Break on hyphens?
fill_args – extra keyword arguments to
textwrap.fill()if desired
- Returns:
text wrapped string
- dgenerate.textprocessing.wrap_paragraphs(text: str, width: int, break_long_words=False, break_on_hyphens=False, **fill_args)[source]
Wrap text that may contain paragraphs without removing separating whitespace.
The directive
NOWRAP!can be used to start a paragraph block with no word wrapping, which is useful for manually formatting small blocks of text.NOWRAP!should exist on its own line, immediately followed by the block of text which will have wrapping disabled. TheNOWRAP!directive line will not exist in the output text.- Parameters:
text – Text containing paragraphs
width – Wrap with in characters
break_long_words – break on long words? default
Falsebreak_on_hyphens – break on hyphens? default
Falsefill_args – extra keyword arguments to
textwrap.fill()if desired
- Returns:
text wrapped string
dgenerate.types module
Commonly used static type definitions and utilities for introspecting on objects, functions, types, etc.
- class dgenerate.types.SetFromMixin[source]
Bases:
objectAllows an obj ot have its attributes set from a dictionary or attributes taken from another obj with an overlapping set of attribute names.
- set_from(obj: Any | dict, missing_value_throws: bool = True)[source]
Set the attributes in this configuration obj from a dictionary or another obj possessing keys / attributes of the same name.
- Parameters:
obj – The obj, or dictionary
missing_value_throws – whether to throw
ValueErrorif obj is missing an attribute that exist in this obj
- Returns:
self
- dgenerate.types.class_and_id_string(obj) str[source]
Return a string formatted with an objects class name next to its memory ID.
IE: <ClassName: id_integer>
- Parameters:
obj – the obj
- Returns:
formatted string
- dgenerate.types.default(value, default_value)[source]
Return value if value is not None, otherwise default
- Parameters:
value –
default_value –
- Returns:
valueordefault_value
- dgenerate.types.format_function_signature(func: Callable, alternate_name: str | None = None, omit_params: Container | None = None) str[source]
Formats the signature of a given function to a string, including default values for arguments.
- Parameters:
func – The function
alternate_name – Alternate function display name
omit_params – Omit parameters by name, this should be a container filled with parameter names, such as a set or list.
- dgenerate.types.fullname(obj)[source]
Get the fully qualified name of an obj or function or type/class
- Parameters:
obj – The obj
- Returns:
Fully qualified name
- dgenerate.types.get_accepted_args_with_defaults(func) Iterator[tuple[str] | tuple[str, Any]][source]
Get the argument signature of a simple function with any default values present.
- Parameters:
func – the function
- Returns:
an iterator over tuples of length 1 or 2, length 2 indicates a default argument value is present. (name,) or (name, value)
- dgenerate.types.get_all_base_classes(cls) list[Type][source]
Get all base classes of a given class recursively.
- Returns:
A list of all base classes.
- dgenerate.types.get_default_args(func) Iterator[tuple[str, Any]][source]
Get a list of default arguments from a simple function with their default values.
- Parameters:
func – the function
- Returns:
iterator over tuples of length 2 (name, value)
- dgenerate.types.get_null_attr_name(e)[source]
If an
AttributeErroroccurred due to accessing an attribute of aNoneTypevalue, get the exact name of the value (variable ID) that wasNone.For the most accurate result, sources must be present. If sources are not present the line of code where the issue occurred is returned instead.
- Parameters:
e – The exception
- Returns:
Name or
Noneif not found.
- dgenerate.types.get_null_call_name(e)[source]
If a
TypeErroroccurred due to calling aNoneTypevalue, get the exact name of the value (variable ID) that wasNoneFor the most accurate result, sources must be present. If sources are not present the line of code where the issue occurred is returned instead.
- Parameters:
e – The exception
- Returns:
Name or
Noneif not found.
- dgenerate.types.get_public_attributes(obj) dict[str, Any][source]
Get the public attributes (excluding functions) and their values from an obj.
- Parameters:
obj – the obj
- Returns:
dict of attribute names to values
- dgenerate.types.get_public_members(obj) dict[str, Any][source]
Get the public members (including functions) and their values from an obj.
- Parameters:
obj – the obj
- Returns:
dict of attribute names to values
- dgenerate.types.get_type(hinted_type)[source]
Get the basic type of hinted type
- Parameters:
hinted_type – the type hint
- Returns:
bool
- dgenerate.types.get_type_of_optional(hinted_type: type, get_origin=True)[source]
Get the first possible type for an
typing.Optional[]type hint- Parameters:
get_origin – Should the returned type be the origin type?
hinted_type – The hinted type to extract from
- Returns:
the type, or
None
- dgenerate.types.is_optional(hinted_type)[source]
Check if a hinted type is
typing.Optional[]- Parameters:
hinted_type – The hinted type
- Returns:
bool
- dgenerate.types.is_type(hinted_type, comparison_type)[source]
Check if a hinted type is equal to a comparison type.
- Parameters:
hinted_type – The hinted type
comparison_type – The type to check for
- Returns:
bool
- dgenerate.types.is_type_or_optional(hinted_type, comparison_type)[source]
Check if a hinted type is equal to a comparison type, even if the type hint is
typing.Optional[](compare the inside if necessary).- Parameters:
hinted_type – The hinted type
comparison_type – The type to check for
- Returns:
bool
- dgenerate.types.is_typing_hint(obj)[source]
Does a type hint object originate from the the builtin
typingmodule?- Parameters:
obj – type hint
- Returns:
TrueorFalse
- dgenerate.types.iterate_attribute_combinations(attribute_defs: Iterable[tuple[str, Iterable]], return_type: type) Iterator[source]
Iterate over every combination of attributes in a given class using a list of tuples mapping attribute names to a list of possible values.
- Parameters:
attribute_defs – sequence of tuple (attribute_name, [sequence of values])
return_type – Construct this type and assign attribute values to it
- Returns:
an iterator over instances of the type mentioned in the my_class argument
- dgenerate.types.module_all()[source]
Return the name of all public non-module type global objects inside the current module.
Can be used for __all__
- Returns:
list of names
- dgenerate.types.parse_bool(string_or_bool: str | bool)[source]
Parse a case insensitive boolean value from a string, for example “true” or “false”
Additionally, values that are already bool are passed through.
- Raises:
ValueError – on parse failure.
- Parameters:
string_or_bool – the string, or a bool value
- Returns:
python boolean type equivalent
- dgenerate.types.partial_deep_copy_container(container: list | dict | set)[source]
Partially copy nested containers, handles lists, dicts, and sets.
- Parameters:
container – top level container
- Returns:
structure with all containers copied
- dgenerate.types.type_check_struct(obj, attribute_namer: Callable[[str], str] | None = None)[source]
Preform some basic type checks on a struct like objects attributes using their type hints.
- Raises:
ValueError – on type checking failure
- Parameters:
obj – the object
attribute_namer – function which names attributes an alternate name
dgenerate.webcache module
Single point of access to the global dgenerate web cache.
- dgenerate.webcache.create_web_cache_file(url, mime_acceptable_desc: str | None = None, mimetype_is_supported: ~typing.Callable[[str], bool] | None = None, unknown_mimetype_exception=<class 'ValueError'>, overwrite: bool = False, tqdm_pbar=<class 'tqdm.std.tqdm'>) tuple[str, str][source]
Download a file from a url and add it to dgenerates temporary web cache that is available to all concurrent dgenerate processes.
If the file exists in the cache already, return information for the existing file.
- Raises:
requests.RequestException – Can raise any exception raised by
requests.getfor request related errors.- Parameters:
url – The url
mime_acceptable_desc – A description of acceptable mimetypes for use in exceptions.
mimetype_is_supported – A function that determines if a mimetype is supported for downloading.
unknown_mimetype_exception – The exception type to raise when an unknown mimetype is encountered.
overwrite – Always overwrite any previously cached file?
tqdm_pbar – tqdm progress bar type, if set to None no progress bar will be used. Defaults to tqdm.tqdm
- Returns:
tuple(mimetype_str, filepath)
- dgenerate.webcache.get_web_cache_directory() str[source]
Get the default web cache directory or the value of the environmental variable
DGENERATE_WEB_CACHE- Returns:
string (directory path)
- dgenerate.webcache.is_downloadable_url(string) bool[source]
Does a string represent a URL that can be downloaded into the web cache?
- Parameters:
string – the string
- Returns:
TrueorFalse
- dgenerate.webcache.request_mimetype(url) str[source]
Request the mimetype of a file at a URL, if the file exists in the cache, a known mimetype is returned without connecting to the internet. Otherwise, connect to the internet to retrieve the mimetype, this action does not update the cache.
- Parameters:
url – The url
- Returns:
mimetype string