# PyTorch中的模型创建（二）

### 卷积层

• in_channels: 输入通道数
• out_channels: 输出通道数（卷积核数量）
• kernel_size: 卷积核大小
• stride: 卷积步长
• dilation: 扩散卷积
• group: 分组卷积
• bias: 是否带有偏置

import torch
import torch.nn as nn
#使用方形卷积核，以及相同的步长
m = nn.conv2d(16，33，3, stride=2)
#使用非方形的卷积核，以及非对称的步长和补零
#使用非方形的卷积核，以及非对称的步长，补零和膨胀系数
input = torch.randn(20，16，50，100)
output = m( input)
print(output.shape)

• 输入：(𝑁,𝐶𝑖𝑛,𝐻𝑖𝑛,𝑊𝑖𝑛)或者(𝐶𝑖𝑛,𝐻𝑖𝑛,𝑊𝑖𝑛)
• 输出：(𝑁,𝐶𝑜𝑢𝑡,𝐻𝑜𝑢𝑡,𝑊𝑜𝑢𝑡)或者(𝐶𝑜𝑢𝑡,𝐻𝑜𝑢𝑡,𝑊𝑜𝑢𝑡)

import torch
import torch.nn as nn

# 定义一个转置卷积层
transposed_conv = nn.ConvTranspose2d(in_channels=3, out_channels=64, kernel_size=4, stride=2, padding=1)

# 创建一个输入张量，形状为 (batch_size, in_channels, height, width)
input_tensor = torch.randn(1, 3, 32, 32)

# 使用转置卷积层处理输入张量
output_tensor = transposed_conv(input_tensor)

print("输入张量的形状：", input_tensor.shape)
print("输出张量的形状：", output_tensor.shape)

### 搭建全卷积网络结构案例

import torch.nn as nn
import torch.nn.functional as F
import torch
from torchsummary import summary

class FCN(nn.Module):
def __init__(self,num_classes):
super(FCN,self).__init__()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3).cuda()  # kernel_size=3, 卷积核大小
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3).cuda()
self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3).cuda()

self.upsample1 = nn.ConvTranspose2d(in_channels=128, out_channels=64, kernel_size=3).cuda()
self.upsample2 = nn.ConvTranspose2d(in_channels=64, out_channels=32, kernel_size=3).cuda()
self.upsample3 = nn.ConvTranspose2d(in_channels=32, out_channels=num_classes, kernel_size=3).cuda()
# 最后的upsample3 输出通道数和标签类别一致

def forward(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
x = F.relu(self.upsample1(x))
x = F.relu(self.upsample2(x))
x = F.relu(self.upsample3(x))
return  x

# 10个类别的图像分割
num_classes = 10
# 每个像素都会得到一个10维的特征向量，表示它属于每个类别的概率
fcn_model = FCN(num_classes)

print(fcn_model)
summary(fcn_model, (3, 224, 224))

FCN(
(conv1): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1))
(conv2): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1))
(conv3): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1))
(upsample1): ConvTranspose2d(128, 64, kernel_size=(3, 3), stride=(1, 1))
(upsample2): ConvTranspose2d(64, 32, kernel_size=(3, 3), stride=(1, 1))
(upsample3): ConvTranspose2d(32, 10, kernel_size=(3, 3), stride=(1, 1))
)
----------------------------------------------------------------
Layer (type)               Output Shape         Param #
================================================================
Conv2d-1         [-1, 32, 222, 222]             896
Conv2d-2         [-1, 64, 220, 220]          18,496
Conv2d-3        [-1, 128, 218, 218]          73,856
ConvTranspose2d-4         [-1, 64, 220, 220]          73,792
ConvTranspose2d-5         [-1, 32, 222, 222]          18,464
ConvTranspose2d-6         [-1, 10, 224, 224]           2,890
================================================================
Total params: 188,394
Trainable params: 188,394
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 121.57
Params size (MB): 0.72
Estimated Total Size (MB): 122.86
----------------------------------------------------------------

### 搭建卷积+全连接的网络结构

import torch.nn as nn
import torch.nn.functional as F
import torch
from torchsummary import summary

class ConvNet(nn.Module):
def __init__(self,num_classes=10):
super(ConvNet,self).__init__()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3).cuda()  # kernel_size=3, 卷积核大小
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3).cuda()

# 全连接层
self.flatten = nn.Flatten(start_dim=1).cuda()
# 将输入张量从第1个维度开始展平
self.fc1 = nn.Linear(64*28*28, 50).cuda()
# 输入图像的大小为64x28x28,输出特征数为50
self.fc2 = nn.Linear(50, num_classes).cuda()
# 输入特征数为512，输出特征数为num_classes

def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.flatten(x)
x = self.fc1(x)
x = self.fc2(x)
return  x

# 10个类别的图像分割
num_classes = 10
# 每个像素都会得到一个10维的特征向量，表示它属于每个类别的概率
conv_net = ConvNet(num_classes)

bacth_size = 4
input_tensor = torch.randn(bacth_size, 3, 32, 32).cuda()    # 输入是4张32x32的RGB图像
output = conv_net(input_tensor)

print(output.shape)
summary(conv_net, (3, 32, 32))

torch.Size([4, 10])
----------------------------------------------------------------
Layer (type)               Output Shape         Param #
================================================================
Conv2d-1           [-1, 32, 30, 30]             896
Conv2d-2           [-1, 64, 28, 28]          18,496
Flatten-3                [-1, 50176]               0
Linear-4                   [-1, 50]       2,508,850
Linear-5                   [-1, 10]             510
================================================================
Total params: 2,528,752
Trainable params: 2,528,752
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.01
Forward/backward pass size (MB): 0.99
Params size (MB): 9.65
Estimated Total Size (MB): 10.64
----------------------------------------------------------------

Process finished with exit code 0

#### 池化层

torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)

• 一个 int ：代表长宽使用同样的参数
• 两个int组成的元组：第一个int用在H维度，第二个int用在W维度

#长宽一致的池化，核尺寸为3x3，池化步长为2
m1 = nn.MaxPool2d( 3，stride=2)
#长宽不一致的池化
m2 = nn.MaxPool2d(( 3，2), stride=(2，1))
input = torch.randn(4，3，24，24)
output1 = m1( input)
output2 = m2(input)
print( "input.shape = " ,input.shape)
print( "output1.shape = ", output1.shape)
print( "output2.shape = " , output2.shape)

input.shape = torch.size( [4，3，24，24])

output1.shape = torch.size([4，3，11，11])

output2.shape = torch.size([4，3，11，23])

#### 平均池化

import torch
import torch.nn as nn
#长宽一致的池化,核尺寸为3x3，池化步长为2
m1 = nn.AvgPool2d( 3, stride=2)
#长宽不一致的池化
m2 = nn.AvgPool2d((3，2), stride=(2，1))
input = torch.randn( 4，3，24，24)
output1 = m1( input)
output2 = m2(input)
print( "input.shape = " , input. shape)
print( "output1.shape = ", output1.shape)
print( "output2.shape = " , output2.shape)

input.shape = torch.size([4，3，24，24])

output1.shape = torch.size([4，3，11，11])

output2.shape = torch.size([4，3，11，23])

#### BN层

BN，即Batch Normalization，是对每一个batch的数据进行归一化操作，可以使得网络训练更稳定，加速网络的收敛。

#批量归一化层（具有可学习参数)
m_learnable = nn.BatchNorm2d( 100)
#批量归一化层（不具有可学习参数>
m_non_learnable = nn.BatchNorm2d(100，affine=False)
#随机生成输入数据
input = torch.randn(20，100，35，45)
#应用具有可学习参数的批量归一化层
output_learnable = m_learnable( input)
# 应用不具有可学习参数的批量归一化层
output_non_learnable = m_non_learnable(input)
print( "input.shape = ", input.shape)
print( "output_learnable.shape = ", output_learnable.shape)
print( "output_non_learnable.shape = ", output_non_learnable.shape)

input.shape = torch.size( [20，100，35，45])

output_learnable.shape = torch.size( [20，100，35，45])

output_non_learnable.shape = torch.size([20，100，35，45])

|
2月前
|

【从零开始学习深度学习】26.卷积神经网络之AlexNet模型介绍及其Pytorch实现【含完整代码】
【从零开始学习深度学习】26.卷积神经网络之AlexNet模型介绍及其Pytorch实现【含完整代码】
158 3
|
2月前
|

【从零开始学习深度学习】28.卷积神经网络之NiN模型介绍及其Pytorch实现【含完整代码】
【从零开始学习深度学习】28.卷积神经网络之NiN模型介绍及其Pytorch实现【含完整代码】
86 2
|
1天前
|

AI智能体研发之路-模型篇（五）：pytorch vs tensorflow框架DNN网络结构源码级对比
AI智能体研发之路-模型篇（五）：pytorch vs tensorflow框架DNN网络结构源码级对比
9 1
|
26天前
|

8615 3
|
16天前
|

29 0
|
21天前
|

C++多态崩溃问题之在PyTorch中，如何定义一个简单的线性回归模型
C++多态崩溃问题之在PyTorch中，如何定义一个简单的线性回归模型
13 0
|
1月前
|

36 0
|
2月前
|

Pytorch实现线性回归模型

50 11
|
2月前
|

PyTorch中的模型创建（一）

70 4
|
1月前
|

43 0