用代码调用RealESRGAN_x2plus

本文介绍了如何使用RRDBNet架构和RealESRGAN工具进行图像超分辨率(如2x和4x)处理,通过实例展示了如何加载模型并进行增强,同时提到了在RTX3090GPU上遇到内存问题时的解决建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import time

import cv2
from basicsr.archs.rrdbnet_arch import RRDBNet
from realesrgan import RealESRGANer

if __name__ == '__main__':
    outscale = 1.5
    # RealESRGAN_x2plus
    model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
    netscale = 2
    # RealESRNet_x4plus
    # model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
    # netscale = 4
    model_path = 'weights/RealESRGAN_x2plus.pth'

    # restorer
    upsampler = RealESRGANer(
        scale=netscale,
        model_path=model_path,
        dni_weight=1,  # RealESRGAN_x2plus不需要这个参数
        model=model,
        tile=0,
        tile_pad=10,
        pre_pad=0,
        half=True,  # 半精度计算
        gpu_id=0)

    img = cv2.imread("./demo.jpg", cv2.IMREAD_COLOR)
    print(img.shape)

    start_time = time.time()
    try:
        output, _ = upsampler.enhance(img, outscale=outscale)
    except RuntimeError as error:
        print('Error', error)
        print('If you encounter CUDA out of memory, try to set --tile with a smaller number.')
    print("time:", time.time() - start_time)
    print(output.shape)
    cv2.imwrite("./demo2.jpg", output)

经测试RTX3090 512->2048 0.5s

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ai君臣

学会的就要教给人

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值