图像&视频编辑工具箱MMEditing使用示例:图像超分辨率(super-resolution)

该博客详细介绍了如何利用MMEditing库进行图像超分辨率处理。首先,通过下载预训练模型checkpoint,然后根据配置文件加载模型。接着,准备测试图像并进行推理生成超分辨率图像。最后,展示超分结果并与源图对比,并保存图像。整个过程清晰地展示了图像超分的实践操作步骤。
摘要由CSDN通过智能技术生成

      MMEditing的介绍及安装参考:https://blog.csdn.net/fengbingchun/article/details/126331541,这里给出图像超分的测试代码,论文:《Learning Continuous Image Representation with Local Implicit Image Function》:

      (1).下载模型(checkpoint):

def download_checkpoint(path, name, url):
	if os.path.isfile(path+name) == False:
		print("checkpoint(model) file does not exist, now download ...")
		subprocess.run(["wget", "-P", path, url])

path = "../../data/model/"
checkpoint = "liif_edsr_norm_c64b16_g1_1000k_div2k_20210715-ab7ce3fc.pth"
url = "https://download.openmmlab.com/mmediting/restorers/liif/liif_edsr_norm_c64b16_g1_1000k_div2k_20210715-ab7ce3fc.pth"
download_checkpoint(path, checkpoint, url)

      (2).根据配置文件和checkpoint文件构建模型:

config = "../../src/mmediting/configs/restorers/liif/liif_edsr_norm_c64b16_g1_1000k_div2k.py"
model = init_model(config, path+checkpoint, device)

      (3).准备测试图像:源图来自于MMEditing

image_path = "../../src/mmediting/tests/data/gt/"
image_name = "baboon.png"

     

      (4).进行推理产生超分图:

result = restoration_inference(model, image)
print(f"result shape: {result.shape}; max value: {torch.max(result)}") # result shape: torch.Size([1, 3, 1920, 2000]); max value: 1.0

      (5).显示执行结果及保存图像:

def crop_save_image(srcimage, dstimage, name):
	crop_height, crop_width = int(srcimage.shape[1]/2), int(srcimage.shape[0]/2)
	print(f"crop height: {crop_height}; crop width: {crop_width}, data type: {type(crop_height)}")

	mat = cv2.resize(srcimage, (dstimage.shape[1], dstimage.shape[0]))
	srccrop = mat[0:crop_height, 0:crop_width]
	dstcrop = dstimage[0:crop_height, 0:crop_width]

	path = "../../data/"
	cv2.imwrite(path+"src_"+name, srccrop)
	cv2.imwrite(path+"result_"+name, dstcrop)

	cv2.imshow("show_src", srccrop)
	cv2.waitKey(0)
	cv2.imshow("show_result", dstcrop)
	cv2.waitKey(0)

result = tensor2img(result)
srcimage = cv2.imread(image)
crop_save_image(srcimage, result, "restoration_liif.png")

      结果如下图所示:左图为源图,右图为结果图,仅显示图像的部分结果

                       

       GitHubhttps://github.com/fengbingchun/PyTorch_Test

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值