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")
结果如下图所示:左图为源图,右图为结果图,仅显示图像的部分结果
GitHub:https://github.com/fengbingchun/PyTorch_Test