首页 > 编程知识 正文

神经网络算法入门,神经网络编程是什么

时间:2023-05-03 07:11:54 阅读:18889 作者:2431

目录

一.论文

二.型号介绍

三.模特预训

一、论文先看看VGG这篇论文《Very Deep Convolutional Networks for Large-Scale Image Recognition》论文的下载地址

论文中的几种模式主要是几种方案a、b、c、d、e。 现在主要采用VGG16和VGG19,也就是下图分别为红框和绿框的部分。

二、模型介绍其实通过上表已经大致知道了模型框架的组成部分。 其实VGG16和VGG19的区别在于前者在三、四、五部分卷积减少了一级。 这里提供了几个基于pytorch的预培训模型的预培训模型下载地址。

从上图中可以看出有无与VGG相应的BatchNormalization。 这里首先介绍VGG16_bn的一些内部层结构。

VGG16_bn编号层结构层数权重0 con v1-1164 x 3x 31 batch norm2relu1- 13 con v1-2264 x 3x 34 batch norm5relu1- 26 pool 17 conv2- 13128 x 38 batch norm9relu 15 batch nor M16 relu3- 117 con v3-26256 x 3x 318 batch norm 19 relu3- 220 con v3-37256 x 321 batch 25 batch norm 26 relu4- 127 con v4-29512 x 312 x 3x 331 batch norm 32 relu4- 333 pool 434 conv5- 111555 512 x 3x 338 batch norm 39 relu5- 240 con V5-313512 x 3x 341 batch norm 42 relu5- 3512 x 3x 4096 ) 15596上的表是VGG16_bn的几个详细的层结构,crdxyz (层是指卷积层和所有连接层。 VGG16仅去除红色部分的Batch_normalization部分。 其中,可以看到VGG16_bn的modules共有44 (此处不是所有连接层),如果是VGG16则为31 )。

在调试过程中导入VGG16_bn模型的结果显示,下图与上述内容相匹配。

三、模型预训3.1模型整体加载基于pytorch模型预训,首先引入加载模型。 有两种方式。 以下逐一介绍。

1 .采用网上下载。 这种常见的互联网原因很慢,所以不推荐。

2 .自己下载预培训模式,从本地加载。 在这里,加载预训练模型后,自己提供图像进行分类识别。

importtorchimportnumpyimporttorch.nnasnnimporttorch.nn.functionalasffrompilimportimagefromtorchvisionimportttransformsimpimppororch g16 _ bn (pre=torch.load ('./vgg 16 _ bn-6c64b 313.PTH ' ) vgg.load_state_ )。 0.456,0.406 ],#这是imagenet数据集的平均值STD=[ 0.229,0.224,0.225 ] ([ tran=transforms.com pose.resize ]

])])im='./1.jpg'im=Image.open(im)im=tran(im)im.unsqueeze_(dim=0)print(im.shape)# input()out=vgg(im)outnp=out.data[0]ind=int(numpy.argmax(outnp))print(ind)from cls import dprint(d[ind])print(out.shape)# im.show()

3 主要有几个注意的地方。由于是加载VGG模型的,并提供自己一张图像进行预测,输入就必须符合VGG的格式。

VGG模型的图像读入方式采用PIL库所以就得使用PIL库进行读入图片输入图像的尺寸得必须和VGG保持一致224x224的三通道。(因为全连接层用的是VGG的)上面采用的normalization归一化方式 几个固定的参数是因为VGG数据的分布,其均值和方差VGG的最终分类的类别是1000类,最终out=vgg(img)是一个1000元素的张量。

 4 查看加載的參數

pre = torch.load('./pretrain/vgg16_bn-6c64b313.pth') for key, v in pre.items(): print(key, v.size())

加載得到的是VGG網絡參數,可以將其輸出查看,這裏只顯示其size

features.0.weight torch.Size([64, 3, 3, 3])features.0.bias torch.Size([64])features.1.weight torch.Size([64])features.1.bias torch.Size([64])features.1.running_mean torch.Size([64])features.1.running_var torch.Size([64])features.3.weight torch.Size([64, 64, 3, 3])features.3.bias torch.Size([64])features.4.weight torch.Size([64])features.4.bias torch.Size([64])features.4.running_mean torch.Size([64])features.4.running_var torch.Size([64])features.7.weight torch.Size([128, 64, 3, 3])features.7.bias torch.Size([128])features.8.weight torch.Size([128])features.8.bias torch.Size([128])features.8.running_mean torch.Size([128])features.8.running_var torch.Size([128])features.10.weight torch.Size([128, 128, 3, 3])features.10.bias torch.Size([128])features.11.weight torch.Size([128])features.11.bias torch.Size([128])features.11.running_mean torch.Size([128])features.11.running_var torch.Size([128])features.14.weight torch.Size([256, 128, 3, 3])features.14.bias torch.Size([256])features.15.weight torch.Size([256])features.15.bias torch.Size([256])features.15.running_mean torch.Size([256])features.15.running_var torch.Size([256])features.17.weight torch.Size([256, 256, 3, 3])features.17.bias torch.Size([256])features.18.weight torch.Size([256])features.18.bias torch.Size([256])features.18.running_mean torch.Size([256])features.18.running_var torch.Size([256])features.20.weight torch.Size([256, 256, 3, 3])features.20.bias torch.Size([256])features.21.weight torch.Size([256])features.21.bias torch.Size([256])features.21.running_mean torch.Size([256])features.21.running_var torch.Size([256])features.24.weight torch.Size([512, 256, 3, 3])features.24.bias torch.Size([512])features.25.weight torch.Size([512])features.25.bias torch.Size([512])features.25.running_mean torch.Size([512])features.25.running_var torch.Size([512])features.27.weight torch.Size([512, 512, 3, 3])features.27.bias torch.Size([512])features.28.weight torch.Size([512])features.28.bias torch.Size([512])features.28.running_mean torch.Size([512])features.28.running_var torch.Size([512])features.30.weight torch.Size([512, 512, 3, 3])features.30.bias torch.Size([512])features.31.weight torch.Size([512])features.31.bias torch.Size([512])features.31.running_mean torch.Size([512])features.31.running_var torch.Size([512])features.34.weight torch.Size([512, 512, 3, 3])features.34.bias torch.Size([512])features.35.weight torch.Size([512])features.35.bias torch.Size([512])features.35.running_mean torch.Size([512])features.35.running_var torch.Size([512])features.37.weight torch.Size([512, 512, 3, 3])features.37.bias torch.Size([512])features.38.weight torch.Size([512])features.38.bias torch.Size([512])features.38.running_mean torch.Size([512])features.38.running_var torch.Size([512])features.40.weight torch.Size([512, 512, 3, 3])features.40.bias torch.Size([512])features.41.weight torch.Size([512])features.41.bias torch.Size([512])features.41.running_mean torch.Size([512])features.41.running_var torch.Size([512])classifier.0.weight torch.Size([4096, 25088])classifier.0.bias torch.Size([4096])classifier.3.weight torch.Size([4096, 4096])classifier.3.bias torch.Size([4096])classifier.6.weight torch.Size([1000, 4096])classifier.6.bias torch.Size([1000])

上面的feature最多是42個,不是44個,因爲relu和pool沒有顯示出來,其分別是features.42  feature.43.因爲加載的參數pre裏面包含的內容是參數,而relu操作和池化操作是不需要參數的,也就是模型保存時並沒有保存下來。

3.2加载部分模型 class VGG(nn.Module): def __init__(self, weights=False): super(VGG, self).__init__() if weights is False: model = models.vgg19_bn(pretrained=True) model = models.vgg19_bn(pretrained=False) pre = torch.load(weights) model.load_state_dict(pre) self.vgg19 = model.features for param in self.vgg19.parameters(): param.requires_grad = False

初始化有个参数权重,当为false时,默认网上下载VGG模型,通常网上下载的比较慢不建议,所以直接本地下载好之后再load即可。这里选择了vgg的features部分,全连接部分没有选择,当然也可以索引或者切片选择任何层的 features。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。