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

发布时间:2026/10/27 22:20:11
书接上回(实现第一个目标检测网络模型) #前景提要一、代码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)

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询