当前位置:网站首页>Use and modification of prior network model
Use and modification of prior network model
2022-07-01 04:46:00 【booze-J】
article
First there is the use of the network model
Use sample code :
import torchvision
from torch import nn
# Load network
# This sentence ( When pretrained Set to False when ) It is equivalent to replacing the network architecture here , The parameters of the network model are initialized , Are the default parameters
vgg16_false = torchvision.models.vgg16(pretrained=False)
# This sentence ( When pretrained Set to True when ) The parameters of the network model are all in ImageNet Trained on the dataset , Is in the ImageNet A good result can be achieved on the data set
vgg16_true = torchvision.models.vgg16(pretrained=True)
vgg16 There are two common parameters for the use of , Namely pretrained and process.
- pretrained - by True Words , It shows that the network has been trained and has a good effect on the training data set if False It means that this network is not trained
- process - by True The progress bar for downloading neural network parameters will be displayed. If it is False The progress bar for downloading neural network parameters will not be displayed
To understand pretrained, It's equivalent to something ? For example, when building the convolution layer of neural network , You gave one kernel_size But it didn't kernel_size Parameters in ,pretrained=True When you get a convolution kernel with parameters ,pretrained=False When you only know the size of this convolution kernel .
First there is the modification of the network model ( How to use the existing network to change its structure )
1. Add a network layer
The sample code is as follows :
import torchvision
from torch import nn
# Load network
vgg16_false = torchvision.models.vgg16(pretrained=False)
print("vgg16_false:\n",vgg16_false)
vgg16_true = torchvision.models.vgg16(pretrained=True)
print("vgg16_true:\n",vgg16_true)
# How to use the existing network to change one of its structures
# 1. Add a network layer
# load CIFAR10 Data sets
train_data = torchvision.datasets.CIFAR10("./CIFAR10",train=True,transform=torchvision.transforms.ToTensor())
# take vgg16_true Model applied to CIFAR10 On dataset , Why add a in_feature=1000,out_feature=10 The linear layer of ? because vgg16_true Network training ImageNet The dataset has 1000 A classification , and CIFAR10 Only 10 classification , So will vgg16_true Internet applications are in CIFAR10 What I said , Need to add a in_feature=1000,out_feature=10 The linear layer of .
# The way 1: Add... Directly across the network
# vgg16_true.add_module("add_linear",nn.Linear(1000,10))
# The way 2: Add
vgg16_true.classifier.add_module("add_linear",nn.Linear(1000,10))
print("vgg16_true:\n",vgg16_true)
Running results :

Explain : take vgg16_true Model applied to CIFAR10 On dataset , Why add a in_feature=1000,out_feature=10 The linear layer of ? because vgg16_true Network training ImageNet The dataset has 1000 A classification , and CIFAR10 Only 10 classification , So will vgg16_true Internet applications are in CIFAR10 What I said , Need to add a in_feature=1000,out_feature=10 The linear layer of .
2. Modify network directly
The sample code is as follows :
import torchvision
from torch import nn
# Load network model
vgg16_false = torchvision.models.vgg16(pretrained=False)
print("vgg16_false:\n",vgg16_false)
vgg16_true = torchvision.models.vgg16(pretrained=True)
print("vgg16_true:\n",vgg16_true)
# How to use the existing network to change one of its structures
# 2. Modify network directly
# load CIFAR10 Data sets
train_data = torchvision.datasets.CIFAR10("./CIFAR10",train=True,transform=torchvision.transforms.ToTensor())
# take vgg16_true Model applied to CIFAR10 On dataset , Why modify the last linear layer out_feature=10 Well ? because vgg16_true Network training ImageNet The dataset has 1000 A classification , and CIFAR10 Only 10 classification , So will vgg16_true Internet applications are in CIFAR10 What I said , Need to modify the last linear layer out_feature=10.
# Index the network sequentially , Modify the last linear layer
vgg16_false.classifier[6] = nn.Linear(4096,10)
print("vgg16_false",vgg16_false)
Running results :

Explain : take vgg16_true Model applied to CIFAR10 On dataset , Why modify the last linear layer out_feature=10 Well ? because vgg16_true Network training ImageNet The dataset has 1000 A classification , and CIFAR10 Only 10 classification , So will vgg16_true Internet applications are in CIFAR10 What I said , Need to modify the last linear layer out_feature=10.
边栏推荐
- STM32扩展板 数码管显示
- Registration for R2 mobile pressure vessel filling test in 2022 and R2 mobile pressure vessel filling free test questions
- The index is invalid
- 2022 gas examination question bank and online simulation examination
- Basic skeleton of neural network nn Use of moudle
- 神经网络-最大池化的使用
- 扩展-Fragment
- 先有网络模型的使用及修改
- AssertionError assert I.ndim == 4 and I.shape[1] == 3
- [2020 overview] overview of link prediction based on knowledge map embedding
猜你喜欢

How to use maixll dock

分布式锁的实现
![[ue4] event distribution mechanism of reflective event distributor and active call event mechanism](/img/44/6a26ad24d56ddd5156f3a31fa7e0b9.jpg)
[ue4] event distribution mechanism of reflective event distributor and active call event mechanism

Dataloader的使用

pytorch 卷积操作

LM small programmable controller software (based on CoDeSys) note 20: PLC controls stepping motor through driver

先有网络模型的使用及修改
![AssertionError assert I.ndim == 4 and I.shape[1] == 3](/img/b1/0109bb0f893eb4c8915df36c100907.png)
AssertionError assert I.ndim == 4 and I.shape[1] == 3

Cmake selecting compilers and setting compiler options

Pytest automated testing - compare robotframework framework
随机推荐
神经网络的基本骨架-nn.Moudle的使用
Dede collection plug-in does not need to write rules
LeetCode_35(搜索插入位置)
The junior college students were angry for 32 days, four rounds of interviews, five hours of soul torture, and won Ali's offer with tears
Thoughts on the construction of Meizhou cell room
2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers
Seven crimes of counting software R & D Efficiency
Summary of testing experience - Testing Theory
Construction of Meizhou nursing laboratory: equipment configuration
Kodori tree board
JS rotation chart
2022 gas examination question bank and online simulation examination
How to use maixll dock
2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
分布式-总结列表
分布式数据库数据一致性的原理、与技术实现方案
神经网络-最大池化的使用
Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom
Caijing 365 stock internal reference | the first IPO of Beijing stock exchange; the subsidiary of the recommended securities firm for gambling and gambling, with a 40% discount
2022 hoisting machinery command registration examination and hoisting machinery command examination registration