学习总结:
- Conv2d是torch.nn模块封装好的2维卷积模型。
- 相关图层锐化操作等操作不是直接修改卷积核,而是选择通过重新赋值修改Conv2d.weight.data的参数,但需要注意的是修改参数的前提是赋值的维度必须和原Conv2d.weight.data的维度保持一模一样。
- 输入通道数取决于数据当前的通道数,比如一开始输入的图像数据是一张彩色图,那么输入通道数就是3。
- 关于卷积核和滤波器的关系(重要):卷积核看作是三维的(NI,A,A),输入通道数为 NI (视为卷积核的高),卷积核的长和宽都为 A,输出通道数 NO 可以看作是滤波器(过滤器)的数目,一个滤波器可以看作一个卷积核,那么就有 NO 个卷积核(NI,A,A)进行数据特征提取。注意:输入通道数一定在数据要在执行卷积模型(毕竟构造卷积模型后参数还可以再修改如conv.weight.data)前 和输入数据的通道数保持相同,比如彩色图输入通道数是3。
- 池化就是在卷积核的基础上进一步提取原始图像数据特征,一般有最大化池化和平均池化。
- 顺手推荐一波深度学习卷积网络画图神器:ConvNetDraw
一、彩色图像输入,灰度输出(代码和结果如下)
1、卷积未池化
1.1 代码一
from PIL import Image import torch as t from torch import nn from torch.autograd import Variable as V from torchvision.transforms import ToTensor,ToPILImage to_tensor = ToTensor() # 创建Totensor对象,可以将图片转换为tensor to_pil = ToPILImage() # 创建ToPILImage对象,可以将tensor转换成图像 image = Image.open('E:/pythonProjecttest/mg.png') # 图片提取 # if image.mode != 'L': # image = image.convert('L') input = to_tensor(image).unsqueeze(0) # 将图片转换为tensor # 锐化卷积核 # 为修改conv.weight.data重新赋值做准备 kernel = t.ones(3,3,3)/9 # 准备 kernel[1][1][1] = 1 # 准备 conv = nn.Conv2d(3,1,3,1,bias=False) # 设置输入通道数、输出通道数、卷积核大小、步长、...偏置 # print(conv.weight.data.size()) conv.weight.data = kernel.view(1,3,3,3) # 必须和封装的Conv2d函数中的conv.weight.data维度一致 滤波器数目(输出通道);输入通道(卷积的高);卷积长宽 out = conv(V(input)) to_pil(out.data.squeeze(0)).show() # 将tensor转换成图像并显示图片
1.2 代码二
from PIL import Image import torch as t from torch import nn from torch.autograd import Variable as V from torchvision.transforms import ToTensor,ToPILImage to_tensor = ToTensor() # 创建Totensor对象,可以将图片转换为tensor to_pil = ToPILImage() # 创建ToPILImage对象,可以将tensor转换成图像 image = Image.open('E:/pythonProjecttest/mg.png') # 图片提取 if image.mode != 'L': # 彩色图转换为黑白图 image = image.convert('L') input = to_tensor(image).unsqueeze(0) # 锐化卷积核 # 为修改conv.weight.data重新赋值做准备 kernel = t.ones(3,3)/9 kernel[1][1] = 1 conv = nn.Conv2d(1,1,3,1,bias=False) # 设置输入通道数、输出通道数、卷积核大小、步长、...偏置 # print(conv.weight.data.size()) conv.weight.data = kernel.view(1,1,3,3) # 必须和封装的Conv2d函数中的conv.weight.data维度一致 out = conv(V(input)) to_pil(out.data.squeeze(0)).show() # 将tensor转换成图像并显示图片
2、池化未卷积
from PIL import Image import torch as t from torch import nn from torch.autograd import Variable as V from torchvision.transforms import ToTensor,ToPILImage to_tensor = ToTensor() # 创建Totensor对象,可以将图片转换为tensor to_pil = ToPILImage() # 创建ToPILImage对象,可以将tensor转换成图像 image = Image.open('E:/pythonProjecttest/mg.png') # 图片提取 if image.mode != 'L': # 彩色图转换为黑白图 image = image.convert('L') input = to_tensor(image).unsqueeze(0) # 锐化卷积核 # kernel = t.ones(3,3)/9 # kernel[1][1] = 1 # conv = nn.Conv2d(1,1,(3,3),1,bias=False) # 设置输入通道数、输出通道数、卷积核大小、步长、...偏置 # conv.weight.data = kernel.view(1,1,3,3) # out = conv(V(input)) # 池化层 pool = nn.AvgPool2d(2,2) out = pool(V(input)) to_pil(out.data.squeeze(0)).show() # 显示图片
3、卷积及池化
from PIL import Image import torch as t from torch import nn from torch.autograd import Variable as V from torchvision.transforms import ToTensor,ToPILImage to_tensor = ToTensor() # 创建Totensor对象,可以将图片转换为tensor to_pil = ToPILImage() # 创建ToPILImage对象,可以将tensor转换成图像 image = Image.open('E:/pythonProjecttest/mg.png') # 图片提取 if image.mode != 'L': # 彩色图转换为黑白图 image = image.convert('L') input = to_tensor(image).unsqueeze(0) # 锐化卷积核 kernel = t.ones(3,3)/9 kernel[1][1] = 1 conv = nn.Conv2d(1,1,3,1,bias=False) # 设置输入通道数、输出通道数、卷积核大小、步长、...偏置 conv.weight.data = kernel.view(1,1,3,3) out = conv(V(input)) # 池化层 pool = nn.AvgPool2d(2,2) out = pool(out) to_pil(out.data.squeeze(0)).show() # 显示图片
二、彩色图像输入,彩色输出(代码和结果如下)
1、卷积未池化
from PIL import Image import torch as t from torch import nn from torch.autograd import Variable as V from torchvision.transforms import ToTensor,ToPILImage to_tensor = ToTensor() # 创建Totensor对象,可以将图片转换为tensor to_pil = ToPILImage() # 创建ToPILImage对象,可以将tensor转换成图像 image = Image.open('E:/pythonProjecttest/mg.png') # 图片提取 # if image.mode != 'L': # image = image.convert('L') input = to_tensor(image).unsqueeze(0) # 锐化卷积核 kernel = t.ones(3,3,3,3)/9 kernel[1][1][1][1] = 1 conv = nn.Conv2d(3,3,3,1,bias=False) # 设置输入通道数、输出通道数、卷积核大小、步长、...偏置 conv.weight.data = kernel.view(3,3,3,3) out = conv(V(input)) to_pil(out.data.squeeze(0)).show() # 显示图片
2、池化未卷积
from PIL import Image import torch as t from torch import nn from torch.autograd import Variable as V from torchvision.transforms import ToTensor,ToPILImage to_tensor = ToTensor() # 创建Totensor对象,可以将图片转换为tensor to_pil = ToPILImage() # 创建ToPILImage对象,可以将tensor转换成图像 image = Image.open('E:/pythonProjecttest/mg.png') # 图片提取 # if image.mode != 'L': # 彩色图转换为黑白图 # image = image.convert('L') input = to_tensor(image).unsqueeze(0) # 锐化卷积核 # kernel = t.ones(3,3)/9 # kernel[1][1] = 1 # conv = nn.Conv2d(1,1,3,1,bias=False) # 设置输入通道数、输出通道数、卷积核大小、步长、...偏置 # conv.weight.data = kernel.view(1,1,3,3) # out = conv(V(input)) # 池化层 pool = nn.AvgPool2d(2,2) out = pool(V(input)) to_pil(out.data.squeeze(0)).show() # 显示图片
3、卷积及池化
from PIL import Image import torch as t from torch import nn from torch.autograd import Variable as V from torchvision.transforms import ToTensor,ToPILImage to_tensor = ToTensor() # 创建Totensor对象,可以将图片转换为tensor to_pil = ToPILImage() # 创建ToPILImage对象,可以将tensor转换成图像 image = Image.open('E:/pythonProjecttest/mg.png') # 图片提取 # if image.mode != 'L': # 彩色图转换为黑白图 # image = image.convert('L') input = to_tensor(image).unsqueeze(0) # 锐化卷积核 kernel = t.ones(3,3,3,3)/9 kernel[1][1][1][1] = 1 conv = nn.Conv2d(3,3,3,1,bias=False) # 设置输入通道数、输出通道数、卷积核大小、步长、...偏置 conv.weight.data = kernel.view(3,3,3,3) # 卷积核必须数量一致 out = conv(V(input)) # 池化层 pool = nn.AvgPool2d(2,2) out = pool(out) to_pil(out.data.squeeze(0)).show() # 显示图片