pytorch 更改预训练模型网络结构的方法
作者:wayne980 时间:2022-04-02 04:41:19
一个继承nn.module的model它包含一个叫做children()的函数,这个函数可以用来提取出model每一层的网络结构,在此基础上进行修改即可,修改方法如下(去除后两层):
resnet_layer = nn.Sequential(*list(model.children())[:-2])
那么,接下来就可以构建我们的网络了:
class Net(nn.Module):
def __init__(self , model):
super(Net, self).__init__()
#取掉model的后两层
self.resnet_layer = nn.Sequential(*list(model.children())[:-2])
self.transion_layer = nn.ConvTranspose2d(2048, 2048, kernel_size=14, stride=3)
self.pool_layer = nn.MaxPool2d(32)
self.Linear_layer = nn.Linear(2048, 8)
def forward(self, x):
x = self.resnet_layer(x)
x = self.transion_layer(x)
x = self.pool_layer(x)
x = x.view(x.size(0), -1)
x = self.Linear_layer(x)
return x
最后,构建一个对象,并加载resnet预训练的参数就可以啦~
resnet = models.resnet50(pretrained=True)
model = Net(resnet)
来源:https://blog.csdn.net/wayne980/article/details/84026939
标签:pytorch,预训练,模型,网络结构
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python网络编程 使用UDP、TCP协议收发信息详解
2021-02-15 14:37:41
[翻译]标记语言和样式手册 Chapter 7 锚点
2008-01-28 14:01:00
![](https://img.aspxhome.com/file/UploadPic/20081/28/2008128141614907s.jpg)
PHP get_html_translation_table()函数用法讲解
2023-06-05 18:36:00
![](https://img.aspxhome.com/file/2023/7/55387_0s.png)
如何在windows下安装配置python工具Ulipad
2023-07-25 20:38:59
![](https://img.aspxhome.com/file/2023/7/97497_0s.jpg)
tensorflow -gpu安装方法(不用自己装cuda,cdnn)
2021-01-26 09:06:46
Python获取浏览器窗口句柄过程解析
2022-05-25 10:37:33
python输入中文的实例方法
2023-08-03 03:25:43
![](https://img.aspxhome.com/file/2023/0/62000_0s.png)
在Python中使用poplib模块收取邮件的教程
2023-02-05 20:48:25
![](https://img.aspxhome.com/file/2023/0/107850_0s.png)
python奇偶行分开存储实现代码
2022-01-30 09:30:24
![](https://img.aspxhome.com/file/2023/2/90282_0s.png)
Webform 内置对象 Session对象、Application全局对象,ViewState详细介绍
2023-06-30 02:14:07
python中hashlib模块用法示例
2023-03-20 12:20:13
Pandas 缺失数据处理的实现
2023-07-14 05:57:38
解决django服务器重启端口被占用的问题
2023-11-10 14:14:12
![](https://img.aspxhome.com/file/2023/1/105441_0s.jpg)
基于XML语言的来实现购物车的详细代码
2008-05-29 13:57:00
python beautifulsoup4 模块详情
2021-12-30 07:50:03
![](https://img.aspxhome.com/file/2023/3/107593_0s.webp)
[译]“我心中的ebay”
2008-06-04 12:09:00
![](https://img.aspxhome.com/file/UploadPic/20086/4/200864121043106s.jpg)
解析python高级异常和运算符重载
2021-06-17 00:56:41
JSQL SQLProxy 的 php 版本代码
2023-11-15 01:05:54
如何在MySQL数据库中使用XML数据
2009-12-29 10:48:00
Python Pygame实战之打砖块小游戏
2021-04-10 01:41:11
![](https://img.aspxhome.com/file/2023/6/66886_0s.png)