当前位置:网站首页>【pytorch】微调技术
【pytorch】微调技术
2022-07-26 06:11:00 【李峻枫】
前言
训练神经网络是一件非常耗费时间的事情,其需要大量的算力以及大量的数据。显然从头开始训练并不是明智之选,利用好已有的资源才是明智之选。
微调技术
图像识别笼统地可以分为两步:
- 提取图片的特征,此部分往往通过CNN卷积神经网络实现。
- 根据提取的特征,进行分类,此部分往往通过全连接神经网络来实现。
- 识别一只猫和识别一只狗有没有类似的地方呢?
答案是有的,它们在提取图片特征都是非常相似的。
考虑CNN中卷积的作用,它就是在识别不同的边缘,因此无论是猫,还是狗,图片特征都是类似的,但是如何根据这些特征来学习才是关键。
预训练模型
在图像识别中,有许多经典的神经网络,例如vgg,resnet等,对于这些经典的网络,pytorch都是提供了训练模型好的模型的。这些某些都是在ImageNet上训练好的,有较高的精确度。利用训练好的某些进行图片特征的提取,就能够大大减少训练的耗时。
代码实现
import torch
from torch import nn
from torch.nn import functional as F
from torchsummary import summary
net = torchvision.models.resnet18(pretrained=True)
net.fc = nn.Linear(net.fc.in_features, 5)
nn.init.xavier_uniform_(net.fc.weight)
summary(net , input_size=(3,224,224) , device="cpu")
lr = 0.0005
loss = nn.CrossEntropyLoss(reduction="mean")
params_1x = [param for name, param in net.named_parameters()
if name not in ["fc.weight", "fc.bias"]]
trainer = torch.optim.SGD([{
'params': params_1x},{
'params': net.fc.parameters(),'lr': lr * 80}],lr=lr, weight_decay=0.001)
epochs = 15
其实非常简单,甚至比自己完全手动定义神经网络都简单,因为它完全不需要自己定义网络结构。
但是这些与训练模型并不是能够直接拿过来就能使用的,还需要一些修改:
- 修改最后的类别数
在ImageNet中,其最后的全连接层是一个输出为 1000 1000 1000的向量,也就是代表着 1000 1000 1000个类别,在实际中,需要根据当前识别认为的类别数进行修改。 - 学习率
一般来说,预训练好的参数无需修改,可以将其设为无需学习的参量,也可以将其的学习率设置的非常小。而对于最后的全连接层,也就是对提取出来的图片信息进行分类的网络,其学习率就要比较大了。
作用与意义
微调技术可谓是没有足够算力人的福音了,其大大减少了训练的成本。
- 提取图像特征的CNN网络往往更加靠近输入,通过梯度反向传播进行训练,往往需要比靠近输出的全连接网络更耗费时间。
- 利用微雕技术,可以在极短的时间内得到一个非常好的结果,在5轮迭代之后就已经可以达到91%的正确率了;15轮迭代就能达到97%的正确率。

不足与局限性
并不是所有情况都能使用微调技术的,在图片特征出现显著差异的时候,使用微调技术往往不能得到满意的结果。
比如,在ImageNet上训练的模型都是基于正常图片的,但是如果将它用于识别医学影像(X光片等)就会导致失败。同样用这个模型去识别卡通图片也往往会识别。
边栏推荐
- 2022年下半年系统集成项目管理工程师(软考中级)报名条件
- Jincang database kingbasees SQL language reference manual (5. Operators)
- VS中使用动态库
- Traversal of the first, middle, and last order of a binary tree -- Essence (each node is a "root" node)
- 【Day03_0420】C语言选择题
- ament_cmake生成ROS2库并链接
- 【Day_06 0423】把字符串转换成整数
- Understanding the mathematical essence of machine learning
- Embedded sharing collection 15
- Xiao He shows his sharp corners and says hello to flutter app
猜你喜欢

Leetcode:934. The shortest Bridge

Knowledge precipitation I: what does an architect do? What problems have been solved

Niuke network: TOPK problem of additive sum between two ordinal groups

Redis sentinel cluster setup

递归处理——子问题
![[(SV & UVM) knowledge points encountered in written interview] ~ phase mechanism](/img/19/32206eb6490c2a5a7a8e746b5003c1.png)
[(SV & UVM) knowledge points encountered in written interview] ~ phase mechanism

Workflow activiti5.13 learning notes (I)

YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors

Qu Weihai, chairman and CEO of Xinyi interactive, adheres to mutual benefit and win-win results, and Qu Weihai promotes enterprise development

Acquisition of bidding information
随机推荐
Acquisition of bidding information
漫谈软件缺陷管理的实践
Balanced binary tree (AVL)~
【Day_06 0423】不要二
Kingbasees SQL language reference manual of Jincang database (8. Functions (XI))
Leetcode:940. How many subsequences have different literal values
Servlet filter details
Solutions to the failure of copy and paste shortcut keys
英语句式参考纯享版 - 定语从句
实习运维知识积累
redis 哨兵集群搭建
Qu Weihai, chairman and CEO of Xinyi interactive, adheres to mutual benefit and win-win results, and Qu Weihai promotes enterprise development
语法泛化三种可行方案介绍
【Day03_0420】C语言选择题
字节面试题——判断一棵树是否为平衡二叉树
[highly available MySQL solution] centos7 configures MySQL master-slave replication
Full binary tree / true binary tree / complete binary tree~
Flex layout
Huawei cloud koomessage is a new marketing weapon in the hot public beta
Latex同时合并表格的多行多列