赞
踩
目录
类似这种彩色图片就是多通道图片,如图:有三层(通常是红绿蓝)然后在这个基础上去除一小块像素块,开始遍历,得到的值包含所有像素的信息
例如上图的单通道的图片 卷积操作:是相应的区域做数乘,即对应位置相乘并相加。
对于多通道,多通道有几层,那么卷积核就要有几个。多通道的计算方式就如图上。
这样就把多通道变为了单通道,假设需要m通道输出,就需要m个(3*w*h)卷积核来计算。
对于特定的输出和输入,需要的卷积核的大小:
n:通道数 k_sie:可以随意选择相应的大小 m:卷积核的数量
还可以使用填充,增加步长。
这里:存在数学关系
若图像为正方形:设输入图像尺寸为WxW,卷积核尺寸为FxF,步幅为S,Padding使用P(图片被填充了几圈),经过该卷积层后输出的图像尺寸为NxN:
池化or下采样--减少数据
然后利用卷积和池化对minist处理。
- import torch
-
- in_c,out_c=5,10
- width,height=100,100
- k_size=3
- batch_size=1
-
- input=torch.randn(batch_size,in_c,width,height)
- #当我去掉batch_size时他从思维变成了三维的张量,说明batch_size对他进行了打包,当我把batch_size改为2时并没有发生变化,我不明白
- juanji=torch.nn.Conv2d(in_c,out_c,kernel_size=k_size)
-
- output=juanji(input)
-
- print(input.shape)
- print(output.shape)
- print(juanji.weight.shape)#卷积层权重的形状
另外两个就不写了,只是加了步长和扩充。
- import torch
- input=[3,4,6,5,2,4,6,8,1,6,7,8,9,7,4,6]
-
- input=torch.Tensor(input).view(1,1,4,4)
-
- maxpooling_layer=torch.nn.MaxPool2d(kernel_size=2)#相当于步长为2
-
- output=maxpooling_layer(input)
-
- print(output)
- from ast import Return
- from operator import itemgetter
- from pickletools import optimize
- from unittest import result
- from winreg import REG_FULL_RESOURCE_DESCRIPTOR
- from torchvision import transforms
- from torchvision import datasets
- from torch.utils.data import Dataset #抽象类,不能有实例化对象,只能被继承
- from torch.utils.data import DataLoader
- import numpy.matlib
- import torch
- import numpy as np
- import torch.optim as optim
- import torch.nn.functional as F
- import matplotlib.pyplot as plt
-
- batch_size=64
- transform=transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.1307,),(0.3081))])
-
-
- train_dataset = datasets.MNIST(root='../data/mnist',train=True, download=True, transform=transform)
- train_loader=DataLoader(train_dataset,shuffle=True,batch_size=batch_size)
- test_dataset=datasets.MNIST(root='../data/mnist',train=False, download=True, transform=transform)
- test_loader=DataLoader(test_dataset,shuffle=False,batch_size=batch_size)
-
- class Model(torch.nn.Module):
- def __init__(self) :
- super(Model,self).__init__()
- self.Conv1=torch.nn.Conv2d(1,10,kernel_size=5)
- self.Conv2=torch.nn.Conv2d(10,20,kernel_size=5)
- self.pooling=torch.nn.MaxPool2d(2)
- self.linear=torch.nn.Linear(320,10)
-
-
- def forward(self,x):
- batch_size=x.size(0)
- x=F.relu(self.pooling(self.Conv1(x)))
- x=F.relu(self.pooling(self.Conv2(x)))
- x=x.view(batch_size,-1)
- x=self.linear(x)
- return x
-
- model=Model()
- device=torch.device("cuda:0"if torch.cuda.is_available()else"cpu")#使用GPU进行计算
- model.to(device)#把model模型放进去
-
- sunshi=torch.nn.CrossEntropyLoss()
- youhua=optim.SGD(model.parameters(),lr=0.01,momentum=0.5)
-
-
- def train(epoch):
- running_loss=0.0
- for batch_idx, data in enumerate(train_loader,0):
- input,target=data
- input,target=input.to(device),target.to(device)#这里的数据(原数据)也要迁移过去
- output=model(input)
- loss=sunshi(output,target)
- loss.backward()
-
- youhua.step()
- youhua.zero_grad()
- running_loss+=loss.item()
-
- if batch_idx%300==299 :
- print('[%d,%5d]loss:%.3f'%(epoch+1,batch_idx+1,running_loss/300))
- running_loss=0.0
-
-
- corect=[]
- def test():
- correct=0
- total=0
- with torch.no_grad():
- for data in test_loader:
- image,labels=data
- image,labels=image.to(device),labels.to(device)
- output=model(image)
- _,predicted= torch.max(output.data,dim=1)
- total+=labels.size(0)
- correct+=(predicted==labels).sum().item()
- print("正确率:%d %%"%(100*correct/total))
- corect.append(100*correct/total)
-
- ep=[]
-
- if __name__=='__main__':
- for epoch in range(10):
- ep.append(epoch)
- train(epoch)
- test()
-
- plt.plot(ep,corect)
- plt.xlabel("epoch")
- plt.ylabel("正确率")
- plt.show()
作业:
- class Model(torch.nn.Module):
- def __init__(self) :
- super(Model,self).__init__()
- self.Conv1=torch.nn.Conv2d(1,10,kernel_size=3)#经过变换,会输出10*26*26((28-3+0)/1+1)
- self.Conv2=torch.nn.Conv2d(10,20,kernel_size=2)
- self.Conv3=torch.nn.Conv2d(20,10,kernel_size=3)
- self.pooling=torch.nn.MaxPool2d(2)
- self.linear1=torch.nn.Linear(40,30)
- self.linear2=torch.nn.Linear(30,20)
- self.linear3=torch.nn.Linear(20,10)
-
- def forward(self,x):
- batch_size=x.size(0)
- x=F.relu(self.pooling(self.Conv1(x)))
- x=F.relu(self.pooling(self.Conv2(x)))
- x=F.relu(self.pooling(self.Conv3(x)))
- x=x.view(batch_size,-1)
- x=self.linear1(x)
- x=self.linear2(x)
- x=self.linear3(x)
- return x
1.关于卷积池化
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。