对单通道数据进行卷积node
若是咱们训练的数据是单通道照片,那么一个样本其数据结构是二维矩阵。shell
由于训练模型时,通常都是使用小批量(n个样本)屡次对模型进行训练。那么这一个批次的数据就是三维结构(多个二维数据组成三维数据)。markdown
那么整个数据集由于分红了不少个小批量数据。最终整个数据集应该组织成一个四维数据结构(多个三维数据组成思惟数据)。数据结构
如今咱们有一个照片数据集,这个数据集只有一张照片。ide
假设该照片是单通道照片,那么咱们按照上面的推理,应该把这一张照片组织成四维数据函数
from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = 'all' #显示多个变量结果 import numpy as np import torch.nn as nn import torch as t #单通道是二维数据 img1 = np.array([[3,3,2,1,0], [0,0,1,3,1], [3,1,2,2,3], [2,0,0,2,2], [2,0,0,0,1]]) #5*5,说明是二维的 img1.shape img1_tensor = t.Tensor(one_channel_data) img1_tensor
运行结果动画
(5, 5) tensor([[3., 3., 2., 1., 0.], [0., 0., 1., 3., 1.], [3., 1., 2., 2., 3.], [2., 0., 0., 2., 2.], [2., 0., 0., 0., 1.]])
该照片img1是单通道照片,也就是二维数据。(5,5)表示图片长4,宽5。很简单,(5,5)一看就是二维。code
以前咱们也讨论了,多个二维数据组成三维,怎么把二维变成三维数据。方法有两种:blog
将二维数据放到列表中,而后在将其转化为torch.Tensorthree
列表法
img2 = np.array([[3,3,2,1,0], [0,0,1,3,1], [3,1,2,2,3], [2,0,0,2,2], [2,0,0,0,1]]) three_dims = [img2] three_dims = t.Tensor(three_dims) three_dims1.shape three_dims1
运行结果
torch.Size([1, 5, 5]) tensor([[[3., 3., 2., 1., 0.], [0., 0., 1., 3., 1.], [3., 1., 2., 2., 3.], [2., 0., 0., 2., 2.], [2., 0., 0., 0., 1.]]])
(1,5,5)元组中有三个数,因此咱们成功将二维变成三维数据
unsqueeze(0)方法
img3 = np.array([[3,3,2,1,0], [0,0,1,3,1], [3,1,2,2,3], [2,0,0,2,2], [2,0,0,0,1]]) img3_tensor = t.Tensor(img3) three_dims2 = img3_tensor.unsqueeze(0) three_dims2.shape three_dims2
运行结果
torch.Size([1, 5, 5]) tensor([[[3., 3., 2., 1., 0.], [0., 0., 1., 3., 1.], [3., 1., 2., 2., 3.], [2., 0., 0., 2., 2.], [2., 0., 0., 0., 1.]]])
使用squeeze(0)也成功的将二维数据转化为三维数据。同理,多个三维数据组成四维数据,这里咱们能够依然使用squeeze(0)将三维转换为四维
img3 = np.array([[3,3,2,1,0], [0,0,1,3,1], [3,1,2,2,3], [2,0,0,2,2], [2,0,0,0,1]]) img3_tensor = t.Tensor(img3) four_dims = img3_tensor.unsqueeze(0).unsqueeze(0) four_dims.shape four_dims
运行结果
torch.Size([1, 1, 5, 5]) tensor([[[[3., 3., 2., 1., 0.], [0., 0., 1., 3., 1.], [3., 1., 2., 2., 3.], [2., 0., 0., 2., 2.], [2., 0., 0., 0., 1.]]]])
(1, 1, 5, 5)元组中有四个数,因此咱们成功将二维变成 四维数据
接下来,咱们使用卷积方法 对这 四维数据 ,即 只有一张照片的数据集进行训练
同时如图,咱们能够知道 卷积核尺寸kernel_size = 3 , 卷积先后均为单通道数据,
所以 in_channels=1, out_channels=1。用pytorch实现动画中的卷积操做,代码以下:
one_img = np.array([[3,3,2,1,0], [0,0,1,3,1], [3,1,2,2,3], [2,0,0,2,2], [2,0,0,0,1]]) #将二维数据转化为四维数据 one_data = t.Tensor(one_img).unsqueeze(0).unsqueeze(0) #(1, 1, 5, 5) 成功转化为四维数据 one_data.shape #卷积conv2d须要输入四维数据,其中输入和输出均为单通道,卷积核为3 conv = nn.Conv2d(in_channels=1, out_channels=1, kernel_size=3) conv(one_data)
成功使用了con2d函数,说明咱们组织的数据形式是正确的。运行结果以下
torch.Size([1, 1, 5, 5]) tensor([[[[-0.4410, -0.7628, -0.6724], [-0.1663, 0.0914, -0.7396], [ 0.5701, -0.1886, -0.2795]]]], grad_fn=<ThnnConv2DBackward>)
对多通道数据进行卷积
这里咱们假设照片是三通道,
from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = 'all' #显示多个变量结果 import numpy as np import torch.nn as nn import torch as t chanel_1 = [[3,3,2,1,0], [0,0,1,3,1], [3,1,2,2,3], [2,0,0,2,2], [2,0,0,0,1]] chanel_2 = [[3,3,2,1,0], [0,0,3,3,1], [0,1,2,2,3], [2,2,0,2,2], [2,0,1,0,1]] chanel_3 = [[3,3,2,1,0], [0,0,3,3,1], [3,3,2,2,3], [2,2,0,2,2], [2,5,0,0,1]] #伪造一张照片,该照片是三通道数据 img_data2 = t.Tensor([chanel_1, chanel_2, chanel_3]) #三通道数据若是输入到conv2d中应该是四维数据。 img_data2 = img_data2.unsqueeze(0) #(1, 1, 5, 5) 成功转化为四维数据 img_data2.shape #卷积conv2d须要输入四维数据,其中输入为三通道in_channels=3,假设咱们想输出单通道,则out_channels=1 conv = nn.Conv2d(in_channels=3, out_channels=1, kernel_size=3) conv(img_data2)
成功使用了con2d函数,说明咱们组织的数据形式是正确的。运行结果以下
torch.Size([1, 3, 5, 5]) tensor([[[[ 1.0329, 0.9904, -0.1885], [ 0.0870, 0.2805, 0.3501], [ 0.4509, -0.1977, 0.9282]]]], grad_fn=<ThnnConv2DBackward>)