Conv2D基本原理与相关函数
常见的图像卷积是二维卷积,而深度学习中Conv2D卷积是三维卷积,图示如下:
Pytroch中的Conv2D是构建卷积神经网络常用的函数,支持的输入数据是四维的tensor对象,格式为NCHW,其中N表示样本数目、C表示通道数目彩色图像为3,灰度图像为1、H跟W分别表示图像高与宽。它们的计算方法可以图示如下:
Conv2D在pytorch中有两个相关的API函数,分别如下:
torch.nn.Conv2d(
in_channels, // 输入通道数
out_channels, // 输出通道数
kernel_size, // 卷积核大小
stride=1, // 步长
padding=0, // 填充
dilation=1, // 空洞卷积支持
groups=1, // 分组卷积支持
bias=True, // 偏置
padding_mode='zeros' // 填0
)
torch.nn.functional.conv2d(
input, // 输入数据
weight, // 卷积核
bias=None, // 偏置
stride=1, // 步长
padding=0, // 填充
dilation=1, // 空洞
groups=1 // 分组
)
其中torch.nn.Conv2d主要是在各种组合的t.nn.Sequential中使用,构建CNN模型。torch.nn.functional.conv2d更多是在各种自定义中使用,需要明确指出输入与权重filters参数。
Pytorch图像卷积处理
下面的代码演示如何使用torch.nn.functional.conv2d实现图像的模糊、梯度、拉普拉斯等常见的图像卷积处理,代码实现与运行演示分别如下:
图像模糊(左侧为原图):
图像梯度(左侧为原图):
图像拉普拉斯(左侧为原图):
边缘提取(左侧为原图):
Pytoch也可以像OpenCV一样随意完成各种常规的图像卷积功能了!上面几个演示的源码如下所示:
import torch
import torch.nn.functional as F
import cv2 as cv
import numpy as np
def image_blur():
image = cv.imread("D:/images/1024.png", cv.IMREAD_GRAYSCALE)
h, w = image.shape
print(h, w)
cv.imshow("input", image)
img = np.reshape(image, (1, 1, h, w))
img = np.float32(img)
k = torch.ones((1, 1, 7, 7), dtype=torch.float) / 49.0
z = F.conv2d(torch.from_numpy(img), k, padding=3)
result = z.numpy()
print(result.shape)
result = np.reshape(result, (h, w))
cv.imshow("blur", np.uint8(result))
cv.waitKey(0)
cv.destroyAllWindows()
def image_gradient():
image = cv.imread("D:/images/1024.png", cv.IMREAD_GRAYSCALE)
h, w = image.shape
print(h, w)
cv.imshow("input", image)
img = np.reshape(image, (1, 1, h, w))
img = np.float32(img)
k = torch.tensor([-1, -2, -1, 0, 0, 0, 1, 2, 2], dtype=torch.float)
k = k.view(1, 1, 3, 3)
print(k.size(), k)
z = F.conv2d(torch.from_numpy(img), k, padding=1)
result = z.numpy()
print(result.shape)
result = np.reshape(result, (h, w))
cv.normalize(result, result, 0, 1.0, cv.NORM_MINMAX)
cv.imshow("gradint", np.uint8(result*255))
cv.waitKey(0)
cv.destroyAllWindows()
def image_laplian():
image = cv.imread("D:/images/1024.png", cv.IMREAD_GRAYSCALE)
h, w = image.shape
print(h, w)
cv.imshow("input", image)
img = np.reshape(image, (1, 1, h, w))
img = np.float32(img)
k = torch.tensor([-1, -1, -1, -1, 8, -1, -1, -1, -1], dtype=torch.float)
k = k.view(1, 1, 3, 3)
print(k.size(), k)
z = F.conv2d(torch.from_numpy(img), k, padding=1)
result = z.numpy()
print(result.shape)
result = np.reshape(result, (h, w))
cv.normalize(result, result, 0, 1.0, cv.NORM_MINMAX)
cv.imshow("reshape", np.uint8(result*255))
cv.waitKey(0)
cv.destroyAllWindows()
def image_edge():
image = cv.imread("D:/images/1024.png", cv.IMREAD_GRAYSCALE)
h, w = image.shape
print(h, w)
cv.imshow("input", image)
img = np.reshape(image, (1, 1, h, w))
img = np.float32(img)
k = torch.tensor([-1, 0, 0, 1], dtype=torch.float)
k = k.view(1, 1, 2, 2)
print(k.size(), k)
z = F.conv2d(torch.from_numpy(img), k, padding=0)
result = z.numpy()
print(result.shape)
result = np.reshape(result, (h-1, w-1))
cv.imshow("reshape", np.uint8(abs(result)))
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == "__main__":
image_edge()
本文暂时没有评论,来添加一个吧(●'◡'●)