from upcunet_v3 import RealWaifuUpScaler import gradio as gr import time import logging import os from PIL import ImageOps import numpy as np import math def greet(input_img, input_model_name, input_tile_mode): # if input_img.size[0] * input_img.size[1] > 256 * 256: # y = int(math.sqrt(256*256/input_img.size[0]*input_img.size[1])) # x = int(input_img.size[0]/input_img.size[1]*y) # input_img = ImageOps.fit(input_img, (x, y)) input_img = np.array(input_img) if input_model_name not in model_cache: t1 = time.time() upscaler = RealWaifuUpScaler(input_model_name[2], ModelPath + input_model_name, half=False, device="cpu") t2 = time.time() logger.info(f'load model time, {t2 - t1}') model_cache[input_model_name] = upscaler else: upscaler = model_cache[input_model_name] logger.info(f'load model from cache') start = time.time() result = upscaler(input_img, tile_mode=input_tile_mode) end = time.time() logger.info(f'input_model_name, {input_model_name}') logger.info(f'input_tile_mode, {input_tile_mode}') logger.info(f'input shape, {input_img.shape}') logger.info(f'output shape, {result.shape}') logger.info(f'speed time, {end - start}') return result if __name__ == '__main__': logging.basicConfig(level=logging.INFO, format="[%(asctime)s] [%(process)d] [%(levelname)s] %(message)s") logger = logging.getLogger() ModelPath = "weights_v3/" model_cache = {} input_model_name = gr.inputs.Dropdown(os.listdir(ModelPath), default="up2x-latest-denoise2x.pth", label='选择model') input_tile_mode = gr.inputs.Dropdown([0, 1, 2, 3, 4], default=2, label='选择tile_mode') input_img = gr.inputs.Image(label='image', type='pil') inputs = [input_img, input_model_name, input_tile_mode] outputs = "image" iface = gr.Interface(fn=greet, inputs=inputs, outputs=outputs, allow_screenshot=False, allow_flagging='never', examples=[['test-img.jpg', "up2x-latest-denoise2x.pth", 2]], article='[https://github.com/bilibili/ailab/tree/main/Real-CUGAN](https://github.com/bilibili/ailab/tree/main/Real-CUGAN)
' '感谢b站开源的项目,图片过大会导致内存不足,所有我将图片裁剪小,想体验大图片的效果请自行前往上面的链接。
' '参数路径,可更换,最前面是超分倍率,过大会内存不足
' '降噪版(denoise):如果原片噪声多,压得烂,推荐使用;目前2倍模型支持了3个降噪等级
' '无降噪版(no-denoise):如果原片噪声不多,压得还行,但是想提高分辨率/清晰度/做通用性的增强、修复处理,推荐使用
' '保守版(conservative):如果你担心丢失纹理,担心画风被改变,担心颜色被增强,总之就是各种担心AI会留下浓重的处理痕迹,推荐使用该版本。
' 'tile越大,越省显存,速度越慢') iface.launch()