f infer_params(state_dict): # this code is copied from https://github.com/victorca25/iNNfer scale2x = 0 scalemin = 6 n_uplayer = 0 plus = False for block in list(state_dict): parts = block.split(".") n_parts = len(parts) if n_parts == 5 and parts[2] == "sub": nb = int(parts[3]) elif n_parts == 3: part_num = int(parts[1]) if (part_num > scalemin and parts[0] == "model" and parts[2] == "weight"): scale2x += 1 if part_num > n_uplayer: n_uplayer = part_num out_nc = state_dict[block].shape[0] if not plus and "conv1x1" in block: plus = True nf = state_dict["model.0.weight"].shape[0] in_nc = state_dict["model.0.weight"].shape[1] out_nc = out_nc scale = 2 ** scale2x return in_nc, out_nc, nf, nb, plus, scale class UpscalerESRGAN(Upscaler): def __init__(self, dirname): self.name = "ESRGAN" self.model_url = "https://github.com/cszn/KAIR/releases/download/v1.0/ESRGAN.pth" self.model_name = "ESRGAN_4x" self.scalers = [] self.user_path = dirname super().__init__() model_paths = self.find_models(ext_filter=[".pt", ".pth"]) scalers = [] if len(model_paths) == 0: scaler_data = UpscalerData(self.model_name, self.model_url, self, 4) scalers.append(scaler_data) for file in model_paths: if file.startswith("http"): name = self.model_name else: name = modelloader.friendly_name(file) scaler_data = UpscalerData(name, file, self, 4) self.scalers.append(scaler_data) def do_upscale(self, img, selected_model): try: model = self.load_model(selected_model) except Exception as e: print(f"Unable to load ESRGAN model {selected_model}: {e}", file=sys.stderr) return img model.to(devices.device_esrgan) img = esrgan_upscale(model, img) return img def load_model(self, path: str): if path.startswith("http"): # TODO: this doesn't use `path` at all? filename = modelloader.load_file_from_url( url=self.model_url, model_dir=self.model_download_path, file_name=f"{self.model_name}.pth", ) else: filename = path state_