当前位置:网站首页>Pytorch -- use and modification of existing network model
Pytorch -- use and modification of existing network model
2022-06-29 00:55:00 【Herding cattle】

With VGG Take the Internet for example , stay Docs in , Select image related torchvision, Left side torchvision.models Are some ready-made network models , On the right is a classification of network models , The first is the model related to classification .
vgg The most common is vgg16 and vgg19:

Parameters pretrained If True, Then the parameters in the model use the existing parameters that are already in ImageNet Data set training is completed , if False, Then the parameter has not been trained ,process if True, A download progress bar will be displayed .

need scipy The package can be installed ,root Is the path ,split Whether to choose training set or test set ,transform Yes PIL Form transformation ,target_transform Transform your goals ,loader Use... When loading datasets .
train_data = torchvision.datasets.ImageNet("./data_ImageNet", split="train", download=True, transform=torchvision.transforms.ToTensor())If you use this method to download , Will report a mistake :
RuntimeError: The dataset is no longer publicly accessible. You need to download the archives externally and place them in the root directory.
Data sets are no longer publicly accessible , To download the data set and put it in the directory . Just search the Internet , The size of the training set is 100 Multiple G.
We load the model directly :
import torchvision
vgg16_False = torchvision.models.vgg16(pretrained=False)#progress The default is True
vgg16_True = torchvision.models.vgg16(pretrained=True)View the parameters of the two models :
vgg16_False:

vgg16_True:

Set to False when , The parameter is the initial default value , by True when , The parameters have been trained , You can get good results . Print out the trained network model :
VGG(
(features): Sequential(
(0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): ReLU(inplace=True)
(2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(3): ReLU(inplace=True)
(4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(6): ReLU(inplace=True)
(7):...
Look at the last layer :
Linear(in_features=4096, out_features=1000, bias=True)
The output is 1000 individual , The network is a classification model , We know that in the end 1000 Classes . As mentioned in the previous blog CIFAR10 Data sets divide images into 10 Classes , Can be vgg16 Last linear layer 1000 Change it to 10, Or add a new linear layer , Input 1000, Output 10.
vgg16_True.add_module('add_linear', nn.Linear(1000, 100))see add_module Source code :
def add_module(self, name: str, module: Optional['Module']) -> None:
r"""Adds a child module to the current module.
The module can be accessed as an attribute using the given name.
Args:
name (string): name of the child module. The child module can be
accessed from this module using the given name
module (Module): child module to be added to the module.
"""name It's the name of the new layer ,module Is the newly added layer . New network is :
...
(classifier): Sequential(
(0): Linear(in_features=25088, out_features=4096, bias=True)
(1): ReLU(inplace=True)
(2): Dropout(p=0.5, inplace=False)
(3): Linear(in_features=4096, out_features=4096, bias=True)
(4): ReLU(inplace=True)
(5): Dropout(p=0.5, inplace=False)
(6): Linear(in_features=4096, out_features=1000, bias=True)
)
(add_linear): Linear(in_features=1000, out_features=100, bias=True)
If you want to add to classifier in , It is amended as follows :
vgg16_True.classifier.add_module('add_linear', nn.Linear(1000, 100))If you want to put the last one Linear4096 Input ,1000 The output is modified to 4096 Input 10 Output :
vgg16_True.classifier[6] = nn.Linear(4096, 10)( Because the last floor is classifier in , And the serial number is 6)
边栏推荐
- [leetcode] 522. 最长特殊序列 II 暴力 + 双指针
- 用户登录(记住用户)&用户注册(验证码) [运用Cookie Session技术]
- It is safer for individuals to choose a securities company to open an account when buying interbank certificates of deposit
- Is it safe to open an account on the flush
- Misunderstanding of innovation by enterprise and it leaders
- cocoscreator动态切换SkeletonData实现骨骼更新
- Seven mistakes in IT Governance and how to avoid them
- Getting started with SQL
- Is it safe and reliable for qiniu school to help open a securities account? How to drive
- Redis常用命令手册
猜你喜欢

Reasons for high price of optical fiber slip ring

【火灾检测】基于matlab GUI森林火灾检测系统(带面板)【含Matlab源码 1921期】

流媒体集群应用与配置:如何在一台服务器部署多个EasyCVR?

Depth first search to realize the problem of catching cattle
![[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter](/img/7b/f9cea5dfe6831f5f226b907e2095eb.jpg)
[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter

Xuetong denies that the theft of QQ number is related to it: it has been reported; IPhone 14 is ready for mass production: four models are launched simultaneously; Simple and elegant software has long

Daily practice: delete duplicates in the ordered array

运营级智慧校园系统源码 智慧校园小程序源码+电子班牌+人脸识别系统

674. longest continuous increasing sequence
![[MCU club] design of GSM version of range hood based on MCU [physical design]](/img/cf/65c1bdbb45afcc6db265a79c25a3ae.jpg)
[MCU club] design of GSM version of range hood based on MCU [physical design]
随机推荐
[MCU club] design of GSM version of range hood based on MCU [physical design]
戴口罩人脸数据集和戴口罩人脸生成方法
盘点 6 月 yyds 的开源项目!
如果你会玩这4个自媒体运营工具,副业收入6000+很轻松
EasyCVR新建用户后,视频调阅页面不能点击的问题修复
【RRT三维路径规划】基于matlab快速扩展随机树无人机三维路径规划【含Matlab源码 1914期】
深度优先搜索实现抓牛问题
Different subsequence problems I
UI highly adaptive modification scheme
Leetcode daily question: implementing strstr()
Realization of beauty system with MATLAB
Seven mistakes in IT Governance and how to avoid them
个人买同业存单基金选择什么证券公司开户好,更安全
Report on the convenient bee Lantern Festival: the first peak sales of pasta products this year; prefabricated wine dumplings became the winners
大智慧上开户是安全的吗
Introduction to four MySQL engines
Xuetong denies that the theft of QQ number is related to it: it has been reported; IPhone 14 is ready for mass production: four models are launched simultaneously; Simple and elegant software has long
Daily English articles, reading accumulation
Daily question 1: the number of numbers in the array 2
[staff] accent mark, gradually stronger mark and gradually weaker mark