当前位置:网站首页>Vggnet neural network based on pytorch for flower recognition
Vggnet neural network based on pytorch for flower recognition
2022-04-21 12:03:00 【Bald Sue】
Author's brief introduction : Bald Sue , Committed to describing problems in the most popular language
Looking back : Kalman filter series 1—— Kalman filtering be based on pytorch build AlexNet Neural network is used for flower recognition
Near term goals : Have 5000 fans
Support Xiao Su : give the thumbs-up 、 Collection 、 Leaving a message.
List of articles
be based on pytorch build VGGNet Neural network is used for flower recognition
Write it at the front
It was written in the last article be based on pytorch build AlexNet Neural network is used for flower recognition Project practice , It is recommended to understand the above before reading this article. In addition, the network model built in this section is VGG, I need you. VGG Have a deeper understanding of the network structure of , It's not clear to stamp this icon *** Learn more .
This article also identifies the types of flowers , And the previous article used AlexNet The overall steps of identification are exactly the same , The main difference is that the structure of the network is different , therefore , This section will only address VGG Explain in detail the construction of network structure , The rest is basically the same as the previous one , I won't repeat , You can download it yourself Code Further study .
VGGNet Network model building
This part of the code may really make you feel the beauty of the code , It's really beautiful. First of all, we know VGG There are four structures , Respectively VGG11、VGG13、VGG16、VGG19. I think if we build one alone VGG The Internet is not difficult to do ,VGG This straight tube structure is easy to implement in code . Official demo Through a dictionary 4 Medium structure VGG The network is put together , We just need to pass in relevant parameters when calling , It's really wonderful !!! Now let's learn
First, we define a dictionary cfgs, There are four key value pairs in the dictionary , Each key corresponds to VGG A structure of , Each value is a parameter in the corresponding structure .
cfgs = {
'vgg11': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'vgg13': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'vgg16': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
'vgg19': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
}
Concrete , We take vgg16 To explain :

With this dictionary , We can construct the feature extraction layer by passing in relevant parameters :
# stay cfgs Pass in "vgg16", Get a list cfg
#cfg = [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M']
cfg = cfgs["vgg16"]
# Define the feature extraction layer function make_features, take cfg Pass in as a parameter
def make_features(cfg: list):
layers = []
in_channels = 3
# Traverse the whole cfg list
for v in cfg:
# if v The value of is "M", Then in the layer structure layers Add a maximum pooling layer , Its kernel_size=2, stride=2
if v == "M":
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
# if v Values are numbers , Then in the layer structure layers Add a convolution core and Relu Activation function
else:
conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
layers += [conv2d, nn.ReLU(True)]
in_channels = v
return nn.Sequential(*layers)
Through the above procedure , We can take a look at the results layers Part of the internal structure 【 Only the first few layers are selected 】, As shown in the figure below :

Next, we can build our classification layer , That is, the part of the whole connection layer :
self.classifier = nn.Sequential(
nn.Linear(512*7*7, 4096),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(4096, 4096),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(4096, num_classes)
)
When all this is ready , Then we can define our network model , As shown below :
class VGG(nn.Module):
def __init__(self, features, num_classes=1000):
super(VGG, self).__init__()
self.features = features
self.classifier = nn.Sequential(
nn.Linear(512*7*7, 4096),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(4096, 4096),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(4096, num_classes)
)
if init_weights:
self._initialize_weights()
def forward(self, x):
# N x 3 x 224 x 224
x = self.features(x)
# N x 512 x 7 x 7
x = torch.flatten(x, start_dim=1)
# N x 512*7*7
x = self.classifier(x)
return x
thus , Our model will be created , Finally, let's take a look at what we just created VGG Model structure :

Display of training results
This article will not explain the training steps in detail , and be based on pytorch build AlexNet Neural network is used for flower recognition Almost the same . Here are the training results , As shown in the figure below :

Its accuracy has reached 0.761, We can take another look at what we keep VGG Model , Here's the picture , It can be seen that VGG There are still many parameters used , Yes 500+M, This is also consistent with our theory .

Summary
For this part, I strongly recommend that you use Pycharm Debugging function of , Look at the results of each run step by step , In this way, you will find that the code structure is particularly clear .
Reference video :https://www.bilibili.com/video/BV1i7411T7ZN/?spm_id_from=333.788
If the article is helpful to you , It would be
Whew, whew, whew ~~duang~~ A great bai
版权声明
本文为[Bald Sue]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211156247577.html
边栏推荐
- How to find the source code, inverse code and complement code
- DR-AP6018-A-wifi6-Access-Point-Qualcomm-IPQ6010-2T2R-2.5G-ETH-port-supporting-5G-celluar-Modem-aluminum-body.
- [software test series 12] stress test report template
- China Resources Yibao is rumored to have an IPO, and nongnongshanquan may usher in an early "old enemy"
- A small game of guessing numbers
- Offline RL Series 4: analysis of the influence of sample complexity on model convergence
- 世界读书日|数据库经典书籍推荐书单(文末留言免费送)
- hiredis和rapidjson库的使用小结
- 【黑马早报】陶虹从张庭传销公司3年分红2.6亿;以岭药业回复连花清瘟争议:已报案;腾讯视频会员再次涨价;华为消费者业务更名...
- How does PHP determine whether the specified date is the previous day
猜你喜欢

Data security: billions of markets, billions of potential capacity

直播江湖生变:李佳琦被困豪宅,罗永浩淡出,薇娅雪梨助播上位

【软件测试系列一】《软件测试基础知识》

基于pytorch搭建VGGNet神经网络用于花类识别

Offline RL Series 4: analysis of the influence of sample complexity on model convergence

What's the use of the enterprise's official website? Do you have to build an official website?

The simpler the Oracle database machine is, the more reliable it is. Oracle buried the "colored egg" 11 years ago

笑死!程序员延寿指南开源了

JD cloud distributed database stardb successfully completed multi-party localization adaptation certification

你与架构师相比差距在哪儿?如何提升
随机推荐
IIR second order filter in flight control
笑死!程序员延寿指南开源了
DR-AP6018-A-wifi6-Access-Point-Qualcomm-IPQ6010-2T2R-2.5G-ETH-port-supporting-5G-celluar-Modem-aluminum-body.
S parameter introduction
Extract name from given string (string, list, re "zero width assertion")
Scala安装和开发环境配置教程
基于SSM开发的医院住院管理信息系统(HIMS)-毕业设计-附源码
《深度学习》学习笔记(六)
Study notes of deep learning (6)
What's the use of the enterprise's official website? Do you have to build an official website?
Zhongshanghui ⺠ evolution of trading platform architecture: response to Apache shardingsphere
Dynamics CRM 365 reports an error when calling the external interface in plugins: Request 'system Net. Permission of type 'webpermission, system, version = 4.0.0.0, culture = neutral, publickey = b77a
jsapi 支付缺少appid
S参数简介
STL 函数用法持续更新
【Flutter 专题】89 图解基本 Overlay 悬浮新手引导 #yyds干货盘点#
Hongshan MOFs distributed storage system won the "2022 Gold Award for distributed storage products"
国网上海直流充电桩专利公布:保证安全运行,全流程检测,分级运维
二进制文件版本控制工具选择难?看完这篇你会找到答案
马斯克活在旧互联网时代?