File size: 1,484 Bytes
3773ad2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
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
|