图像&视频编辑工具箱MMEditing使用示例:图像生成(generation)

      MMEditing的介绍及安装参考:https://blog.csdn.net/fengbingchun/article/details/126331541,这里给出图像生成的测试代码,论文:《Image-to-Image Translation with Conditional Adversarial Networks》:

      (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 = "pix2pix_vanilla_unet_bn_1x1_80k_facades_20200524-6206de67.pth"
url = "https://download.openmmlab.com/mmediting/synthesizers/pix2pix/pix2pix_facades/pix2pix_vanilla_unet_bn_1x1_80k_facades_20200524-6206de67.pth"
download_checkpoint(path, checkpoint, url)

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

config = "../../src/mmediting/configs/synthesizers/pix2pix/pix2pix_vanilla_unet_bn_1x1_80k_facades.py"
model = init_model(config, path+checkpoint, device)

      (3).准备测试图像:源图来自于网络,这里进行了拼接,两幅图宽、高一致,左图为鞋子的源图,右图为草图

image_path = "../../data/image/"
image_name = "11.png"

     

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

result = generation_inference(model, image)
print(f"result shape: {result.shape}; max value: {np.max(result)}") # result shape: (256, 768, 3); max value: 255

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

cv2.imwrite("../../data/result_generation_pix2pix.jpg", result)
cv2.imshow("show", result)
cv2.waitKey(0)

      结果如下图所示:

图像&视频编辑工具箱MMEditing使用示例:图像生成(generation)_第1张图片

      GitHub:https://github.com/fengbingchun/PyTorch_Test 

你可能感兴趣的:(PyTorch,MMEditing)