Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
automatic1111
GitHub Repository: automatic1111/stable-diffusion-webui
Path: blob/master/modules/dat_model.py
3055 views
1
import os
2
3
from modules import modelloader, errors
4
from modules.shared import cmd_opts, opts
5
from modules.upscaler import Upscaler, UpscalerData
6
from modules.upscaler_utils import upscale_with_model
7
8
9
class UpscalerDAT(Upscaler):
10
def __init__(self, user_path):
11
self.name = "DAT"
12
self.user_path = user_path
13
self.scalers = []
14
super().__init__()
15
16
for file in self.find_models(ext_filter=[".pt", ".pth"]):
17
name = modelloader.friendly_name(file)
18
scaler_data = UpscalerData(name, file, upscaler=self, scale=None)
19
self.scalers.append(scaler_data)
20
21
for model in get_dat_models(self):
22
if model.name in opts.dat_enabled_models:
23
self.scalers.append(model)
24
25
def do_upscale(self, img, path):
26
try:
27
info = self.load_model(path)
28
except Exception:
29
errors.report(f"Unable to load DAT model {path}", exc_info=True)
30
return img
31
32
model_descriptor = modelloader.load_spandrel_model(
33
info.local_data_path,
34
device=self.device,
35
prefer_half=(not cmd_opts.no_half and not cmd_opts.upcast_sampling),
36
expected_architecture="DAT",
37
)
38
return upscale_with_model(
39
model_descriptor,
40
img,
41
tile_size=opts.DAT_tile,
42
tile_overlap=opts.DAT_tile_overlap,
43
)
44
45
def load_model(self, path):
46
for scaler in self.scalers:
47
if scaler.data_path == path:
48
if scaler.local_data_path.startswith("http"):
49
scaler.local_data_path = modelloader.load_file_from_url(
50
scaler.data_path,
51
model_dir=self.model_download_path,
52
)
53
if not os.path.exists(scaler.local_data_path):
54
raise FileNotFoundError(f"DAT data missing: {scaler.local_data_path}")
55
return scaler
56
raise ValueError(f"Unable to find model info: {path}")
57
58
59
def get_dat_models(scaler):
60
return [
61
UpscalerData(
62
name="DAT x2",
63
path="https://github.com/n0kovo/dat_upscaler_models/raw/main/DAT/DAT_x2.pth",
64
scale=2,
65
upscaler=scaler,
66
),
67
UpscalerData(
68
name="DAT x3",
69
path="https://github.com/n0kovo/dat_upscaler_models/raw/main/DAT/DAT_x3.pth",
70
scale=3,
71
upscaler=scaler,
72
),
73
UpscalerData(
74
name="DAT x4",
75
path="https://github.com/n0kovo/dat_upscaler_models/raw/main/DAT/DAT_x4.pth",
76
scale=4,
77
upscaler=scaler,
78
),
79
]
80
81