Path: blob/master/modules/dat_model.py
3055 views
import os12from modules import modelloader, errors3from modules.shared import cmd_opts, opts4from modules.upscaler import Upscaler, UpscalerData5from modules.upscaler_utils import upscale_with_model678class UpscalerDAT(Upscaler):9def __init__(self, user_path):10self.name = "DAT"11self.user_path = user_path12self.scalers = []13super().__init__()1415for file in self.find_models(ext_filter=[".pt", ".pth"]):16name = modelloader.friendly_name(file)17scaler_data = UpscalerData(name, file, upscaler=self, scale=None)18self.scalers.append(scaler_data)1920for model in get_dat_models(self):21if model.name in opts.dat_enabled_models:22self.scalers.append(model)2324def do_upscale(self, img, path):25try:26info = self.load_model(path)27except Exception:28errors.report(f"Unable to load DAT model {path}", exc_info=True)29return img3031model_descriptor = modelloader.load_spandrel_model(32info.local_data_path,33device=self.device,34prefer_half=(not cmd_opts.no_half and not cmd_opts.upcast_sampling),35expected_architecture="DAT",36)37return upscale_with_model(38model_descriptor,39img,40tile_size=opts.DAT_tile,41tile_overlap=opts.DAT_tile_overlap,42)4344def load_model(self, path):45for scaler in self.scalers:46if scaler.data_path == path:47if scaler.local_data_path.startswith("http"):48scaler.local_data_path = modelloader.load_file_from_url(49scaler.data_path,50model_dir=self.model_download_path,51)52if not os.path.exists(scaler.local_data_path):53raise FileNotFoundError(f"DAT data missing: {scaler.local_data_path}")54return scaler55raise ValueError(f"Unable to find model info: {path}")565758def get_dat_models(scaler):59return [60UpscalerData(61name="DAT x2",62path="https://github.com/n0kovo/dat_upscaler_models/raw/main/DAT/DAT_x2.pth",63scale=2,64upscaler=scaler,65),66UpscalerData(67name="DAT x3",68path="https://github.com/n0kovo/dat_upscaler_models/raw/main/DAT/DAT_x3.pth",69scale=3,70upscaler=scaler,71),72UpscalerData(73name="DAT x4",74path="https://github.com/n0kovo/dat_upscaler_models/raw/main/DAT/DAT_x4.pth",75scale=4,76upscaler=scaler,77),78]798081