• 图像&视频编辑工具箱MMEditing安装及使用示例(Inpainting)


          MMEditing是基于PyTorch的图像&视频编辑开源工具箱,支持图像和视频超分辨率(super-resolution)、图像修复(inpainting)、图像抠图(matting)、图像生成(generation)、视频插帧(video interpolation),是OpenMMLab项目的成员之一。源码在:https://github.com/open-mmlab/mmediting,最新发布版本为v0.15.1,License为Apache-2.0。它支持在Windows、Linux和Mac上执行。

          1.安装:使用conda安装

          (1).创建openmmlab虚拟环境:

    1. conda create -n openmmlab python=3.8
    2. conda activate openmmlab

          (2).安装PyTorch:这里PyTorch使用1.11.0版本,CUDA使用10.2版本,此CUDA版本对PyTorch各版本都支持

    conda install pytorch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0 cudatoolkit=10.2 -c pytorch

          (3).安装MMCV:MMCV有两个版本,这里安装带CUDA的mmcv-full

          1).mmcv-full: 完整版,包含所有的特性以及丰富的开箱即用的CUDA算子,安装此版本需要较长时间。

          2).mmcv:精简版,不包含CUDA算子但包含其余所有特性和功能,类似MMCV 1.0之前的版本。

          不要在同一个环境中安装两个版本,否则可能会遇到类似ModuleNotFound的错误。在安装一个版本之前,需要先卸载另一个:

    1. pip uninstall mmcv-full
    2. pip uninstall mmcv

          注意:mmcv-full版本与mmdetection版本存在兼容对应关系,mmcv不能使用最新版,MMDetection 2.25.1要求MMCV版本(mmcv-full)为[1.3.17, 1.6.0),这里使用1.5.3版本。CUDA版本和PyTorch版本与安装PyTorch时保持一致

    pip install mmcv-full==1.5.3 -f https://download.openmmlab.com/mmcv/dist/cu102/torch1.11.0/index.html

          (4).安装mmediting:没有通过源码安装

    pip install mmedit==0.15.1

          2.测试:inpainting,论文《Globally and Locally Consistent Image Completion》

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

    1. def download_checkpoint(path, name, url):
    2. if os.path.isfile(path+name) == False:
    3. print("checkpoint(model) file does not exist, now download ...")
    4. subprocess.run(["wget", "-P", path, url])
    5. path = "../../data/model/"
    6. checkpoint = "gl_256x256_8x12_celeba_20200619-5af0493f.pth"
    7. url="https://download.openmmlab.com/mmediting/inpainting/global_local/gl_256x256_8x12_celeba_20200619-5af0493f.pth"
    8. download_checkpoint(path, checkpoint, url)

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

    1. config = "../../src/mmediting/configs/inpainting/global_local/gl_256x256_8x12_celeba.py"
    2. model = init_model(config, path+checkpoint, device)

          (3).准备测试图像:

    1. image_path = "../../src/mmediting/tests/data/image/"
    2. image_name = "celeba_test.png"
    3. image_mask_name = "bbox_mask.png"

          每组需要2张,一张是待修复的彩色图像;一张是二值掩码图像,背景色为0,前景色为255,指定待修复的彩色图像中待修复的区域,如下图所示:源图来自于MMEditing

          (4).进行推理修复:

    result = inpainting_inference(model, image, mask)

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

    1. print(f"result shape: {result.shape}; max value: {torch.max(result)}") # result shape: torch.Size([1, 3, 256, 256]); max value: 1.0
    2. result = tensor2img(result, min_max=(-1, 1))[..., ::-1]
    3. print("shape:", np.shape(result)) # shape: (256, 256, 3)
    4. cv2.imwrite("../../data/result_inpainting_global_local_celeba.jpg", result)
    5. cv2.imshow("show", result)
    6. cv2.waitKey(0)

           修复后的结果图如下所示:

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

  • 相关阅读:
    百分点科技|怎样做数据运营,才能让数据中台真正用起来?
    ROS从入门到精通系列(二十七)-- ros Parameter Server
    CDN许可证申请
    python 编写简单的程序解释器(十一)
    对Spring AOP的进一步深入理解
    [爬虫]4.数据解析及应用 之 bs4【爬取一部小说的文本】
    C++初阶 | [二] 类和对象(上)
    使用企业微信移动办公效果如何?
    python+vue+elementui知识共享网站
    【云原生 | 17】容器的四种网络模式
  • 原文地址:https://blog.csdn.net/fengbingchun/article/details/126331541