轻松学Pytorch-详解Conv2D卷积处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | 轻松学Pytorch - 详解Conv2D卷积处理 原创 gloomyfish OpenCV学堂 4 月 25 日 收录于话题 #轻松学Pytorch系列 30 个 图片 点击上方蓝字关注我们 微信公众号:OpenCV学堂 关注获取更多计算机视觉与深度学习知识 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() 如果你对Pytorch中的YOLOv5对象检测感兴趣,想训练自定义对象检测,扫码查看下面的视频教程,一键获取该技能即可: 图片 |
分类:
工具箱--torch
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
2019-05-10 图像分割方法()