计算机视觉大作业:EdgeConnect论文阅读

在这里插入图片描述

复现

一、环境配置
运行环境:
Python 3.6.6
PyTorch 1.10.0
NVIDIA GPU + CUDA 10.2 + cuDNN 8.2
Python包:
numpy = 1.14.3+mkl
scipy = 1.0.1
future = 0.18.2
matplotlib = 2.2.2
pillow = 8.3.1
opencv-python = 4.5.3.56
scikit-image = 0.14.5
Pyaml = 21.10.1
二、下载预训练模型
在这里插入图片描述
三、运行模型
(若要自己训练模型,要提供原图的边缘检测结果、掩膜和原图灰度图,再加上训练时间可能较长,我们就直接采用作者训练好的模型去跑结果了。。。)
Windows 10下,在命令行输入:
python test.py --model 3 --input ./examples/places2/images --mask ./examples/places2/masks --output ./checkpoints/results

在这里插入图片描述
原始图像:
在这里插入图片描述在这里插入图片描述
图像修复结果:
在这里插入图片描述
在这里插入图片描述
可以发现修复效果还是比较好的。
四、验证模型准确性

  1. PSNR, SSIM and Mean Absolute Error测试:
    python ./scripts/metrics.py --data-path ./examples/places2/images --output-path ./checkpoints/results
    Edge-Connect模型图像修复结果的各项指标如下图所示:
    在这里插入图片描述
    PSNR = 10.0142 SSIM = 0.4224 MAE = 0.1742
    和论文里的数据对比一下:
    在这里插入图片描述
    可以发现相差的比较远(?!),不知道作者论文里的表格数据是怎么达到那么好的,估计是后期对模型做了优化然后选取了修复结果最好的验证结果贴到论文上了?但总体来说修复程度就人眼看上去已经非常好了。

注:EdgeConnect项目源码请前往GitHub自行搜索并Clone。

以下是 InpaintingModel 的上下文编解码的网络结构原版代码: ```python class ContextualAttention(nn.Module): def __init__(self, kernel_size=3): super(ContextualAttention, self).__init__() self.kernel_size = kernel_size self.softmax = nn.Softmax(dim=3) def forward(self, f, b, mask=None): # f: foreground, b: background # f: (n, c, h, w), b: (n, c, h, w), mask: (n, 1, h, w) n, c, h, w = f.size() kh, kw = self.kernel_size, self.kernel_size # padding the feature maps pad_h = int((kh - 1) / 2) pad_w = int((kw - 1) / 2) f = F.pad(f, (pad_w, pad_w, pad_h, pad_h)) b = F.pad(b, (pad_w, pad_w, pad_h, pad_h)) mask = F.pad(mask, (pad_w, pad_w, pad_h, pad_h)) # convolve the padded foreground with a kernel to get the key feature map kernel = torch.ones(c, 1, kh, kw).to(f.device) key = F.conv2d(f * mask, kernel, stride=1, padding=0) key = key.view(n, c, -1) key = key.permute(0, 2, 1) # convolve the padded background with a kernel to get the query feature map query = F.conv2d(b * mask, kernel, stride=1, padding=0) query = query.view(n, c, -1) # obtain the spatial attention map attn = torch.bmm(key, query) attn = self.softmax(attn) # obtain the context feature map value = F.conv2d(b, kernel, stride=1, padding=0) value = value.view(n, c, -1) context = torch.bmm(value, attn.permute(0, 2, 1)) context = context.view(n, c, kh, kw) return context ``` 使用该原版实现载入 InpaintingModel 的预训练模型进行不规则掩膜修复的功能,可以按以下步骤进行: 1. 安装所需的 Python 库: ``` pip install numpy opencv-python torch torchvision ``` 2. 下载预训练模型: ``` wget https://github.com/knazeri/edge-connect/releases/download/v1.0/pytorch_edge_connect.tar.gz tar -zxvf pytorch_edge_connect.tar.gz ``` 3. 加载预训练模型,进行不规则掩膜修复: ```python import cv2 import numpy as np import torch import torch.nn.functional as F from models import EdgeGenerator, InpaintingModel from utils import get_edges, tensor2im, mask_image from PIL import Image # Load the EdgeGenerator edge_generator = EdgeGenerator() edge_generator.load_state_dict(torch.load('pytorch_edge_connect/checkpoints/latest_net_G.pth')) edge_generator.eval() # Load the InpaintingModel inpainting_model = InpaintingModel() inpainting_model.load_state_dict(torch.load('pytorch_edge_connect/checkpoints/latest_net_E.pth')) inpainting_model.eval() # Read the input image and the mask img = cv2.imread('input.png') mask = cv2.imread('mask.png', 0) # Convert the input image to a tensor img_tensor = torch.from_numpy(np.transpose(img, (2, 0, 1))).float().unsqueeze(0) / 255.0 # Convert the mask to a tensor mask_tensor = torch.from_numpy(mask).unsqueeze(0).unsqueeze(0).float() / 255.0 # Generate the edges edges_tensor = get_edges(img_tensor) # Generate the inpainted image with torch.no_grad(): _, _, _, _, gen_mask = edge_generator(img_tensor, edges_tensor, mask_tensor) inpainted_img_tensor = inpainting_model(img_tensor, gen_mask) # Convert the inpainted image tensor to a numpy array inpainted_img = tensor2im(inpainted_img_tensor) # Save the inpainted image cv2.imwrite('output.png', inpainted_img) ```
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Polaris_T

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值