书接上回(实现第一个目标检测网络模型)

书接上回(实现第一个目标检测网络模型) #前景提要一、代码1.首先去Models and pre-trained weights — Torchvision 0.28 documentation 这个torchvision.models官网搜素VGG1.VGG16参数2.torchvision.models.vgg16(*,weights: Optional[VGG16_Weights] None,progress: bool True,**kwargs: Any)其中weights(VGG16_Weights, optional) 这个预训练权重能够加快我们训练自己模型的速度2.回顾moduleModule — PyTorch 2.13 documentation初始化方法就是定义模型有哪些层但是forward就是告诉你图像是按照什么顺序流动的import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) - None: super().__init__() self.conv1 nn.Conv2d(1, 20, 5) self.conv2 nn.Conv2d(20, 20, 5) def forward(self, x): x F.relu(self.conv1(x)) return F.relu(self.conv2(x))3.开始写我的VGG16运行结果发现VGG16包含了三部分因为我们只要VGG16的特征提取部分所以self.feature_extractvgg16().features这里加一个.features即可4.backbone主干部分代码输入一张size为448*448的图片输出任务接下来补充输入和输出部分代码inputtorch.rand(1,3,448,448) outputmodel(input) print(output) print(output.shape)1输入部分这个torch.randbatch,channel,height,width)2结果vgg6.feature特征提取之后得到512*14*14的特征图 feature map512个通道因为下载了pixelens这个插件 所以右键view as image即可输入是3个channel输出是512个channel3代码import torch import torch.nn as nn import torch.nn.functional as F from torchvision.models import vgg16 class VGG16new(nn.Module): def __init__(self) : super().__init__() self.feature_extractvgg16().features def forward(self, x): return self.feature_extract(x) if __name__ __main__: modelVGG16new() print(model) inputtorch.rand(1,3,448,448) outputmodel(input) print(output) print(output.shape)5.head的FCN全连接层linear pytorch network层至此 已完成如下部分的backbone头部网络还差head部分FCNfully connected network更正一下之前的nn.Linear线性——PyTorch 2.13 文档 --- Linear — PyTorch 2.13 documentationclasstorch.nn.Linear(in_features,out_features,biasTrue,deviceNone,dtypeNone)下边就是linear比如前两个就是linear89接下来从512*14*14的特征图变成8个输出需要用到的方法就是展平flatten何为flatten比如这个左边是2通道2*2的flatten之后变成右边的一行数字所以接下来操作接下来我们就是要把vgg16.feature特征提取层得到的512*14*14的特征图数字转化为一行共512*14*14100352个数字电脑win那块儿搜计算器再就是把1*100352通过linear全连接层转化为1*8代码实现1.自定义一个属性fc_layer里边是nn.Flatten()代码import torch import torch.nn as nn import torch.nn.functional as F from torchvision.models import vgg16 class VGG16new(nn.Module): def __init__(self) : super().__init__() self.feature_extractvgg16().features#self.xxx 中的 xxx 就是当前实例对象的一个属性名字是人为自己随意定义的。所以feature_extract自己起的名字 self.fc_layernn.Sequential( nn.Flatten() ) def forward(self, x): xself.feature_extract(x) return self.fc_layer(x) if __name__ __main__: modelVGG16new() print(model) inputtorch.rand(1,3,448,448) outputmodel(input) print(output) print(output.shape)运行结果2.nn.Linear中间层的话linear之后加一个非线性激活nn.ReLU()但是最后一个linear之后不要加relu代码import torch import torch.nn as nn import torch.nn.functional as F from torchvision.models import vgg16 class VGG16new(nn.Module): def __init__(self) : super().__init__() self.feature_extractvgg16().features#self.xxx 中的 xxx 就是当前实例对象的一个属性名字是人为自己随意定义的。所以feature_extract自己起的名字 self.fc_layernn.Sequential( nn.Flatten(), nn.Linear(512*14*14,4096), nn.ReLU(), nn.Linear(4096,1024), nn.ReLU(), nn.Linear(1024,8) ) def forward(self, x): xself.feature_extract(x) return self.fc_layer(x) if __name__ __main__: modelVGG16new() print(model) inputtorch.rand(1,3,448,448) outputmodel(input) print(output) print(output.shape)