• pytorch 图像的卷积操作


    目录

    1.卷积核基本参数说明

     2.卷积相关操作说明

    3.卷积操作示例


     

         1.卷积核基本参数说明

             pytorch进行图像卷积操作之前,需要把图像素格式进行分离,比如一个图像为rgb格式,把R,G,B取出来作为一个ndarray,前文讲过,在pytorch中进行图像转Tensor,大小变换,相关处理的库,基本都放在 from torchvision import transforms里面,对于把正常的图像转换为单独的RGB的ndarray,并且归一化,使用 transforms.ToTensor即可一次性完成转换。在训练图像相关模型的时候,主要是训练卷积核的参数,一般的3*3的卷积核结构如代码所示:

    1. import cv2
    2. import os
    3. import numpy as np
    4. import torch
    5. import torchvision
    6. from torchvision import transforms
    7. from PIL import Image
    8. from torch import nn
    9. from matplotlib import pyplot as plt
    10. from torchvision import transforms
    11. #定义卷积核心,bias为False则不要偏置参数
    12. #输入通道为3,输出通道为1,卷积核大小为3*3,偏置为真
    13. cov = nn.Conv2d(3,1,3,bias=True)
    14. print(cov.state_dict())
    15. '''
    16. OrderedDict([('weight', tensor([[[[ 0.1062, 0.0600, -0.0675],
    17. [-0.0303, 0.0045, -0.0276],
    18. [ 0.0114, 0.1434, -0.1323]],
    19. [[-0.0622, -0.0029, -0.0695],
    20. [-0.0282, -0.0664, -0.0157],
    21. [ 0.0037, -0.0900, -0.0588]],
    22. [[-0.1231, -0.1717, 0.1089],
    23. [ 0.0051, 0.1269, -0.0846],
    24. [-0.0662, 0.0817, 0.1689]]]])), ('bias', tensor([0.0631]))])
    25. 进程已结束,退出代码为 0
    26. '''
     2.卷积相关操作说明

           用transforms.ToTensor把图像分为RGB单独通道且归一化后,就可以对图像进行卷积操作,示例代码如图:

    1. import cv2
    2. import os
    3. import numpy as np
    4. import torch
    5. import torchvision
    6. from torchvision import transforms
    7. from PIL import Image
    8. from torch import nn
    9. from matplotlib import pyplot as plt
    10. from torchvision import transforms
    11. cov = nn.Conv2d(3,1,3,bias=True)
    12. # print(cov.state_dict())
    13. #初始化卷积核所以参数为0.5
    14. for x in cov.parameters():
    15. nn.init.constant_(x,0.5)
    16. print(cov.state_dict())
    17. d = torch.ones(3,6,6)
    18. d = torch.unsqueeze(d,0)
    19. print(d)
    20. c = cov(d)
    21. print(c)
    22. '''
    23. OrderedDict([('weight', tensor([[[[0.5000, 0.5000, 0.5000],
    24. [0.5000, 0.5000, 0.5000],
    25. [0.5000, 0.5000, 0.5000]],
    26. [[0.5000, 0.5000, 0.5000],
    27. [0.5000, 0.5000, 0.5000],
    28. [0.5000, 0.5000, 0.5000]],
    29. [[0.5000, 0.5000, 0.5000],
    30. [0.5000, 0.5000, 0.5000],
    31. [0.5000, 0.5000, 0.5000]]]])), ('bias', tensor([0.5000]))])
    32. tensor([[[[1., 1., 1., 1., 1., 1.],
    33. [1., 1., 1., 1., 1., 1.],
    34. [1., 1., 1., 1., 1., 1.],
    35. [1., 1., 1., 1., 1., 1.],
    36. [1., 1., 1., 1., 1., 1.],
    37. [1., 1., 1., 1., 1., 1.]],
    38. [[1., 1., 1., 1., 1., 1.],
    39. [1., 1., 1., 1., 1., 1.],
    40. [1., 1., 1., 1., 1., 1.],
    41. [1., 1., 1., 1., 1., 1.],
    42. [1., 1., 1., 1., 1., 1.],
    43. [1., 1., 1., 1., 1., 1.]],
    44. [[1., 1., 1., 1., 1., 1.],
    45. [1., 1., 1., 1., 1., 1.],
    46. [1., 1., 1., 1., 1., 1.],
    47. [1., 1., 1., 1., 1., 1.],
    48. [1., 1., 1., 1., 1., 1.],
    49. [1., 1., 1., 1., 1., 1.]]]])
    50. tensor([[[[14., 14., 14., 14.],
    51. [14., 14., 14., 14.],
    52. [14., 14., 14., 14.],
    53. [14., 14., 14., 14.]]]], grad_fn=)
    54. '''

            从示例代码可以看出,因为我们定义的3通道输入的3*3卷积核心,就生成了3个3*3的核心,3个核心分比对3个通道进行卷积((对应位置直接相乘)然后求和加偏置),得出输出,同理如果定义卷积核输出为三,那么就会定义3*3=9个卷积核每三个卷积核分别对图像进行卷积操作,得出三个输出通道。

    3.卷积操作示例

            以一张图像为例打开图像,定义卷积核进行卷积操作:

    1. import cv2
    2. import os
    3. import numpy as np
    4. import torch
    5. import torchvision
    6. from torchvision import transforms
    7. from PIL import Image
    8. from torch import nn
    9. from matplotlib import pyplot as plt
    10. from torchvision import transforms
    11. cov = nn.Conv2d(3,3,3,bias=True)
    12. for x in cov.parameters():
    13. nn.init.constant_(x,0.05)
    14. print(cov.state_dict())
    15. img = cv2.imread("E:/test/pythonProject/test.jpg")
    16. img = cv2.resize(img,dsize=(320,240))
    17. print('img.shape',img.shape)
    18. trans = transforms.ToTensor()
    19. timg = trans(img)
    20. print('timg.shape',timg.shape)
    21. cimg = cov(timg)
    22. print('cimg.shape',cimg.shape)
    23. timg = timg.permute(1,2,0)
    24. ta = timg.numpy()
    25. cimg = cimg.permute(1,2,0)
    26. ca = cimg.data.numpy()
    27. cv2.imshow("test",img)
    28. cv2.imshow("ta",ta)
    29. cv2.imshow("cimg",ca)
    30. cv2.waitKey()
    31. '''
    32. OrderedDict([('weight', tensor([[[[0.0500, 0.0500, 0.0500],
    33. [0.0500, 0.0500, 0.0500],
    34. [0.0500, 0.0500, 0.0500]],
    35. [[0.0500, 0.0500, 0.0500],
    36. [0.0500, 0.0500, 0.0500],
    37. [0.0500, 0.0500, 0.0500]],
    38. [[0.0500, 0.0500, 0.0500],
    39. [0.0500, 0.0500, 0.0500],
    40. [0.0500, 0.0500, 0.0500]]],
    41. [[[0.0500, 0.0500, 0.0500],
    42. [0.0500, 0.0500, 0.0500],
    43. [0.0500, 0.0500, 0.0500]],
    44. [[0.0500, 0.0500, 0.0500],
    45. [0.0500, 0.0500, 0.0500],
    46. [0.0500, 0.0500, 0.0500]],
    47. [[0.0500, 0.0500, 0.0500],
    48. [0.0500, 0.0500, 0.0500],
    49. [0.0500, 0.0500, 0.0500]]],
    50. [[[0.0500, 0.0500, 0.0500],
    51. [0.0500, 0.0500, 0.0500],
    52. [0.0500, 0.0500, 0.0500]],
    53. [[0.0500, 0.0500, 0.0500],
    54. [0.0500, 0.0500, 0.0500],
    55. [0.0500, 0.0500, 0.0500]],
    56. [[0.0500, 0.0500, 0.0500],
    57. [0.0500, 0.0500, 0.0500],
    58. [0.0500, 0.0500, 0.0500]]]])), ('bias', tensor([0.0500, 0.0500, 0.0500]))])
    59. img.shape (240, 320, 3)
    60. timg.shape torch.Size([3, 240, 320])
    61. cimg.shape torch.Size([3, 238, 318])
    62. 进程已结束,退出代码为 0
    63. '''

            这里定义的卷积核输入为3通道,输出为3通道,这里三组卷积核,每组卷积核包含三个卷积核,三个卷积核分别对三个通道进行卷积,最后每组输出一个通道,三组输出三个通道图像,因为卷积核参数一样,所以最后卷积输出的RGB值相等,输出灰色图像。

    这里注意:

    cimg = cimg.permute(1,2,0)

    这个函数是进行维度调换,理解不了,可以先把他转为numpy,再用cv2.merge((r,g,b))函数进行融合,cv2.split(imgt) 可以把图像重新分为 r g b 的numpy.ndarray结构,如代码所示:

    1. t = cimg.data.numpy()
    2. r = t[0]
    3. g = t[1]
    4. b = t[2]
    5. imgt = cv2.merge((r,g,b))
    6. r,g,b = cv2.split(imgt)
    7. print(r.shape,g.shape,b.shape)
    8. cv2.imshow("imgt",imgt)
    9. cv2.waitKey()
    10. '''
    11. (238, 318) (238, 318) (238, 318)
    12. '''

  • 相关阅读:
    互联网Java工程师面试题·Redis 篇·第二弹
    PaddleClas学习1——使用PPLCNet模型对车辆属性进行识别(python)
    html 常见兼容性问题
    「笔试刷题」:腐烂的苹果
    SkyWalking 告警规则配置说明
    看服装数字化工厂是如何运作的
    【SAP Abap】SAP增强开发总结
    【最优化方法】实验三 无约束最优化方法的MATLAB实现
    map-reduce执行过程
    c基础知识-数组(详解)
  • 原文地址:https://blog.csdn.net/klp1358484518/article/details/136344576