当前位置:网站首页>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)
边栏推荐
- Redis常用命令手册
- 养老年金险是理财产品吗?预期收益在哪看?
- Pinhole camera with added lens
- cocoscreator动态切换SkeletonData实现骨骼更新
- [image registration] improved SAR image registration based on sar-sift with matlab code
- 最大路径和问题(摘樱桃问题)
- 架构实战营|模块5
- Count the number of different palindrome subsequences in the string
- Nodejs installation and download
- Is it safe to open an account on the flush
猜你喜欢
![[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]

戴口罩人臉數據集和戴口罩人臉生成方法

Bmfont make bitmap font and use it in cocoscreator

PR 2021 quick start tutorial, how to use audio editing in PR?

Cross domain problem of canvas drawing caused by background image cache

Comparison between winding process and lamination process

接雨水系列问题

pinhole camera model

使用.Net驱动Jetson Nano的OLED显示屏

Easycvr service private What should I do if the PEM file is emptied and cannot be started normally?
随机推荐
Précautions d'installation et d'utilisation des joints rotatifs
Mask wearing face data set and mask wearing face generation method
User login (remember the user) & user registration (verification code) [using cookie session technology]
Click hijack: X-FRAME-OPTIONS is not configured
不同的子序列问题I
【图像增强】基于matlab人工多重曝光融合AMEF图像去雾【含Matlab源码 1916期】
Easycvr service private What should I do if the PEM file is emptied and cannot be started normally?
【RRT三维路径规划】基于matlab快速扩展随机树无人机三维路径规划【含Matlab源码 1914期】
Pinhole camera with added lens
sql入门
Browser cache library design summary (localstorage/indexeddb)
Bmfont make bitmap font and use it in cocoscreator
手下两个应届生:一个踏实喜欢加班,一个技术强挑活,怎么选??
Large-scale case applications to developing post-click conversion rate estimation with MTL
Daily question 1: the number of numbers in the array 2
Analysis Framework -- establishment of user experience measurement data system
Comparison between winding process and lamination process
[MCU club] design of classroom number detection based on MCU [simulation design]
卷绕工艺与叠片工艺的对比
[Gym 102423]-Elven Efficiency | 思维