作者:diuhunpo_813 | 来源:互联网 | 2023-08-31 07:27
importnumpyasnpimporttorchfromtorchimportnnfromtorch.autogradimportVariablefromPILimpo
import numpy as npimport torchfrom torch import nnfrom torch. autograd import Variablefrom PIL import Imageimport matplotlib. pylab as plt% matplotlib inline
卷积层 导入图片并且转化为灰度图 kaggle环境中所有本地数据都有上传才能加载! im = Image. open ( '../input/cat.jpg' ) . convert( 'L' )
将图片转化为矩阵 im = np. array( im, dtype= 'float32' )
查看图片,将图片矩阵转化为uint8类型,uint8是无符号八位整型,表示范围是[0, 255]的整数,colormap为gray plt. imshow( im. astype( 'uint8' ) , cmap= 'gray' )
im. shape
(121, 121)
将图片矩阵转化为Tensor,并将图片大小转化为卷积输入的要求大小 print ( im. shape) im = torch. from_numpy( im. reshape( 1 , 1 , im. shape[ 0 ] , im. shape[ 1 ] ) ) print ( im. shape)
(121, 121) torch.Size([1, 1, 121, 121])
使用nn.Conv2d定义卷积层。参数:输入通道数,输出通道数,kernel大小,偏置为0 conv1 = nn. Conv2d( 1 , 1 , 3 , bias= False )
定义kernel矩阵内容,为3X3矩阵,中间为8,其余为-1 sobel_kernel = np. array( [ [ - 1 , - 1 , - 1 ] , [ - 1 , 8 , - 1 ] , [ - 1 , - 1 , - 1 ] ] , dtype= 'float32' ) print ( sobel_kernel. shape)
(3, 3)
将卷积核大小设置与卷积层通道相一致 sobel_kernel = sobel_kernel. reshape( ( 1 , 1 , 3 , 3 ) ) print ( sobel_kernel. shape)
(1, 1, 3, 3)
将定义好得卷积核转化为Tensor,并赋值给卷积层权重 conv1. weight. data = torch. from_numpy( sobel_kernel)
使用卷积层对图片进行卷积 edge1 = conv1( Variable( im) ) print ( edge1. shape)
torch.Size([1, 1, 119, 119])
用函数squeeze去掉卷积操作后的其他为1的维数,并且将Tensor转化为numpy print ( edge1. data. squeeze( ) . shape) edge1 = edge1. data. squeeze( ) . numpy( )
torch.Size([119, 119])
将经过卷积后的图片显示出来 plt. imshow( edge1, cmap= 'gray' )
池化层 导入最大池化层,并定义池化大小 pool1 = nn. MaxPool2d( 2 , 2 )
打印出张量格式的图片大小 print ( 'before max pool, image shape: {} x {}' . format ( im. shape[ 2 ] , im. shape[ 3 ] ) )
before max pool, image shape: 121 x 121
将Tensor转化为Variable,进行最大池化操作。并将结果转化为numpy small_im1 = pool1( Variable( im) ) small_im1 = small_im1. data. squeeze( ) . numpy( )
打印出经过池化层后的图片大小 print ( 'after max pool, image shape: {} X {}' . format ( small_im1. shape[ 0 ] , small_im1. shape[ 1 ] ) )
after max pool, image shape: 60 X 60
打印出图片 plt. imshow( small_im1, cmap= 'gray' )