2022-10-08 04:22:22 +08:00
|
|
|
import html
|
|
|
|
import os
|
2022-10-19 23:28:42 +08:00
|
|
|
import re
|
2022-10-08 04:22:22 +08:00
|
|
|
|
|
|
|
import gradio as gr
|
2022-12-25 07:02:22 +08:00
|
|
|
import modules.hypernetworks.hypernetwork
|
2022-10-22 19:07:00 +08:00
|
|
|
from modules import devices, sd_hijack, shared
|
2022-10-08 04:22:22 +08:00
|
|
|
|
2022-10-27 14:40:24 +08:00
|
|
|
not_available = ["hardswish", "multiheadattention"]
|
2022-12-25 07:02:22 +08:00
|
|
|
keys = list(x for x in modules.hypernetworks.hypernetwork.HypernetworkModule.activation_dict.keys() if x not in not_available)
|
2022-10-08 04:22:22 +08:00
|
|
|
|
2023-01-21 13:36:07 +08:00
|
|
|
|
2023-01-10 13:56:57 +08: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, dropout_structure=None):
|
|
|
|
filename = modules.hypernetworks.hypernetwork.create_hypernetwork(name, enable_sizes, overwrite_old, layer_structure, activation_func, weight_init, add_layer_norm, use_dropout, dropout_structure)
|
2022-10-21 17:11:12 +08:00
|
|
|
|
2022-12-25 07:02:22 +08:00
|
|
|
return gr.Dropdown.update(choices=sorted([x for x in shared.hypernetworks.keys()])), f"Created: {filename}", ""
|
2022-10-08 04:22:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
def train_hypernetwork(*args):
|
2023-01-21 13:36:07 +08:00
|
|
|
shared.loaded_hypernetworks = []
|
2022-10-08 04:22:22 +08:00
|
|
|
|
2022-10-12 04:07:09 +08:00
|
|
|
assert not shared.cmd_opts.lowvram, 'Training models with lowvram is not possible'
|
2022-10-11 23:33:57 +08:00
|
|
|
|
2022-10-08 04:22:22 +08:00
|
|
|
try:
|
|
|
|
sd_hijack.undo_optimizations()
|
|
|
|
|
2022-10-11 20:54:34 +08:00
|
|
|
hypernetwork, filename = modules.hypernetworks.hypernetwork.train_hypernetwork(*args)
|
2022-10-08 04:22:22 +08: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-12 00:03:08 +08:00
|
|
|
shared.sd_model.cond_stage_model.to(devices.device)
|
|
|
|
shared.sd_model.first_stage_model.to(devices.device)
|
2022-10-08 04:22:22 +08:00
|
|
|
sd_hijack.apply_optimizations()
|
|
|
|
|