from torch import nn
import torch
import torch.nn.functional as F
#dilation空洞卷积 groups组卷机
conv=nn.Conv2d(in_channels=1,out_channels=1,kernel_size=3,stride=1,padding=1,dilation=1,groups=1,bias=True)
input=torch.randn(1,1,2,2)
output=conv(input)
#激活函数
print(input)
sigmoid=nn.Sigmoid()
print(sigmoid(input))
input=torch.randn(1,1,2,2)
print(input)
relu=nn.ReLU(inplace=True)
print(relu(input))
input=torch.randn(1,1,3,3)
print(input)
lucky_relu=nn.LeakyReLU(0.04,True)
print(F.softmax(input,3))
#池化层
max_pooling=nn.MaxPool2d(2,stride=2)
aver_pooling=nn.AvgPool2d(2,stride=2)
input=torch.randn(1,1,4,4)
print(input)
print(max_pooling(input))
print(aver_pooling(input))
#droput层
dropout=nn.Dropout(0.5)
input1=torch.ones(2,2)
print(input1)
print(dropout(input1))
#dropout 代码
X = torch.arange(4).view(2,2)
print(X)
drop_prob=0.5
keep_prob = 1 - drop_prob
print(torch.rand(X.shape))
mask = (torch.rand(X.shape) < keep_prob).float()
print(mask * X / keep_prob)
#BN
bn=nn.BatchNorm2d(2)
print(bn)
input=torch.randn(2,2,2,2)
output=bn(input)
print(output)
#GN
#全连接层FC
input=torch.randn(4,1024)
linear=nn.Linear(1024,10)
print(linear(input))
#全局平均池化层GAP
感受野是指特征图上的某个点能看到输入图像的区域,即特征图上的点由输入图像中感受野大小区域的计算得到。
卷积和池化会影响感受野,激活函数不会
输入层中边缘点的使用次数明显少于中间点,经过多层的卷积堆叠之后,输入层对于特征图点做出的贡献分布呈高斯分布形状。
图像分割通常使用池化层来增大感受野,同时也缩小了特征图尺寸,然后利用上采样还原图像尺寸。特征图缩小在放大的过程造成了精度上的损失,因此需要增加感受野,保持特征图尺寸不变。空洞卷积。
#空洞卷积
conv1=nn.Conv2d(3,256,3,stride=1,padding=1,dilation=2)
# -*- coding: utf-8 -*-
# Created by WIN10 on 2020/8/4
# Copyright (c) 2020 WIN10. All rights reserved.
from torch import nn
import torch
class VGG(nn.Module):
def __init__(self,num_classes=1000):
super(VGG,self).__init__()
layers=[]
in_dim=3
out_dim=64
#循环构造卷积层,一共13个
for i in range(13):
layers+=[nn.Conv2d(in_dim,out_dim,3,1,1),nn.ReLU(inplace=True)]
in_dim=out_dim
if i==1 or i ==3 or i==6 or i==9 or i==12:
layers+=[nn.MaxPool2d(2,2)]
if i!=9:
out_dim*=2
self.features=nn.Sequential(*layers)
self.classifier=nn.Sequential(
nn.Linear(512*7*7,4096),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096,4096),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096,num_classes),
)
def forward(self,x):
x=self.features(x)
#[1,512,7,7]>[1,512*7*7]
x=x.view(x.size(0),-1)
y=self.classifier(x)
return y
vgg=VGG(2)
input=torch.randn(1,3,224,224)
scores=vgg(input)
print(scores)