2022-10-07 14:22:22 -06:00
|
|
|
import html
|
|
|
|
import os
|
2022-10-19 09:28:42 -06:00
|
|
|
import re
|
2022-10-07 14:22:22 -06:00
|
|
|
|
|
|
|
import gradio as gr
|
|
|
|
import modules.textual_inversion.preprocess
|
2022-10-22 05:07:00 -06:00
|
|
|
import modules.textual_inversion.textual_inversion
|
|
|
|
from modules import devices, sd_hijack, shared
|
2022-10-11 06:51:22 -06:00
|
|
|
from modules.hypernetworks import hypernetwork
|
2022-10-07 14:22:22 -06:00
|
|
|
|
2022-10-24 23:48:49 -06:00
|
|
|
keys = list(hypernetwork.HypernetworkModule.activation_dict.keys())
|
2022-10-07 14:22:22 -06:00
|
|
|
|
2022-10-24 23:48:49 -06:00
|
|
|
def create_hypernetwork(name, enable_sizes, overwrite_old, layer_structure=None, activation_func=None, weight_init=None, add_layer_norm=False, use_dropout=False):
|
2022-10-21 03:11:12 -06:00
|
|
|
# Remove illegal characters from name.
|
|
|
|
name = "".join( x for x in name if (x.isalnum() or x in "._- "))
|
|
|
|
|
2022-10-07 14:22:22 -06:00
|
|
|
fn = os.path.join(shared.cmd_opts.hypernetwork_dir, f"{name}.pt")
|
2022-10-19 17:09:40 -06:00
|
|
|
if not overwrite_old:
|
|
|
|
assert not os.path.exists(fn), f"file {fn} already exists"
|
2022-10-07 14:22:22 -06:00
|
|
|
|
2022-10-19 09:28:42 -06:00
|
|
|
if type(layer_structure) == str:
|
2022-10-19 23:18:02 -06:00
|
|
|
layer_structure = [float(x.strip()) for x in layer_structure.split(",")]
|
2022-10-19 09:28:42 -06:00
|
|
|
|
2022-10-19 08:30:33 -06:00
|
|
|
hypernet = modules.hypernetworks.hypernetwork.Hypernetwork(
|
|
|
|
name=name,
|
|
|
|
enable_sizes=[int(x) for x in enable_sizes],
|
|
|
|
layer_structure=layer_structure,
|
2022-10-19 18:10:45 -06:00
|
|
|
activation_func=activation_func,
|
2022-10-24 23:48:49 -06:00
|
|
|
weight_init=weight_init,
|
2022-10-22 05:07:00 -06:00
|
|
|
add_layer_norm=add_layer_norm,
|
|
|
|
use_dropout=use_dropout,
|
2022-10-19 08:30:33 -06:00
|
|
|
)
|
2022-10-11 05:53:02 -06:00
|
|
|
hypernet.save(fn)
|
2022-10-07 14:22:22 -06:00
|
|
|
|
|
|
|
shared.reload_hypernetworks()
|
|
|
|
|
|
|
|
return gr.Dropdown.update(choices=sorted([x for x in shared.hypernetworks.keys()])), f"Created: {fn}", ""
|
|
|
|
|
|
|
|
|
|
|
|
def train_hypernetwork(*args):
|
|
|
|
|
2022-10-11 05:53:02 -06:00
|
|
|
initial_hypernetwork = shared.loaded_hypernetwork
|
2022-10-07 14:22:22 -06:00
|
|
|
|
2022-10-11 14:07:09 -06:00
|
|
|
assert not shared.cmd_opts.lowvram, 'Training models with lowvram is not possible'
|
2022-10-11 09:33:57 -06:00
|
|
|
|
2022-10-07 14:22:22 -06:00
|
|
|
try:
|
|
|
|
sd_hijack.undo_optimizations()
|
|
|
|
|
2022-10-11 06:54:34 -06:00
|
|
|
hypernetwork, filename = modules.hypernetworks.hypernetwork.train_hypernetwork(*args)
|
2022-10-07 14:22:22 -06:00
|
|
|
|
|
|
|
res = f"""
|
|
|
|
Training {'interrupted' if shared.state.interrupted else 'finished'} at {hypernetwork.step} steps.
|
|
|
|
Hypernetwork saved to {html.escape(filename)}
|
|
|
|
"""
|
|
|
|
return res, ""
|
|
|
|
except Exception:
|
|
|
|
raise
|
|
|
|
finally:
|
2022-10-11 05:53:02 -06:00
|
|
|
shared.loaded_hypernetwork = initial_hypernetwork
|
2022-10-11 10:03:08 -06:00
|
|
|
shared.sd_model.cond_stage_model.to(devices.device)
|
|
|
|
shared.sd_model.first_stage_model.to(devices.device)
|
2022-10-07 14:22:22 -06:00
|
|
|
sd_hijack.apply_optimizations()
|
|
|
|
|