import torch import PIL import numpy as np # from post_process.upscale.RealESRGAN import RealESRGAN from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline class Upscaler(): def __init__(self, text2img: StableDiffusionPipeline, realesrganer): self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # self.scale = scale # self.model = RealESRGAN(self.device, scale=self.scale) # self.model.load_weights(f'CodeFormer/CodeFormer/weights/realesrgan/RealESRGAN_x{self.scale}.pth') self.model = realesrganer self.img2img = StableDiffusionImg2ImgPipeline(**text2img.components).to(self.device) def upscale(self, imgs: list[PIL.Image]) -> list[PIL.Image]: # torch.cuda.empty_cache() upscaled_imgs = [] for img in imgs: upscaled_img = self.model.predict(img) upscaled_imgs.append(upscaled_img) return upscaled_imgs def hires_fix(self, imgs: list[PIL.Image], prompt: str, negative_prompt: str) -> list[PIL.Image]: upscaled_images = self.upscale(imgs) results = self.img2img( prompt=[prompt]*len(imgs), negative_prompt=[negative_prompt]*len(imgs), image=upscaled_images, strength=0.2, guidance_scale=7.5, num_inference_steps=20, schduler="EulerAncestralDiscreteScheduler", ).images return results