|
import torch |
|
import PIL |
|
import numpy as np |
|
|
|
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.model = realesrganer |
|
self.img2img = StableDiffusionImg2ImgPipeline(**text2img.components).to(self.device) |
|
|
|
def upscale(self, imgs: list[PIL.Image]) -> list[PIL.Image]: |
|
|
|
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 |
|
|