pytorch怎么使用定义好的模型的一部分
Encoder代码为:
class Encoder(nn.Module): #输入图片的大小isize、噪声的维度nz=、输入图片的通道nc=、ndf=、
def __init__(self,isize,nz,nc,ndf,ngpu,n_exter_layers=,add_final_conv=True):
super(Encoder,self).__init__()
self.ngpu=ngpu
# 必须为16倍数
assert isize % ==,"isize has to be a multiple of 16" main=nn.Sequential()
# 图片的高宽缩小一倍
main.add_module('initial-conv-{0}-{1}'.format(nc,ndf),nn.Conv2d(nc,ndf,,,,bias=False))
main.add_module('initial-relu-{0}'.format(ndf),nn.LeakyReLU(0.2,inplace=True))
csize,cndf=isize/,ndf for t in range(n_exter_layers): #在这里面特征宽高不变,通道数也不变
main.add_module('extra-layers-{0}-{1}-conv'.format(t,cndf),nn.Conv2d(cndf,cndf,,,,bias=False))
main.add_module('extra-layers-{0}-{1}-batchnorm'.format(t,cndf),nn.BatchNorm2d(cndf))
main.add_module('extra-layers-{0}-{1}-relu'.format(t,cndf),nn.LeakyReLU(0.2,inplace=True)) # 在特征高宽仍大于4时,就添加缩小一倍高宽,通道增加一倍的卷积块
while csize>:
in_feat = cndf out_feat = cndf * main.add_module('pyramid-{0}-{1}-conv'.format(in_feat, out_feat),nn.Conv2d(in_feat, out_feat, , , , bias=False)) main.add_module('pyramid-{0}-batchnorm'.format(out_feat),nn.BatchNorm2d(out_feat)) main.add_module('pyramid-{0}-relu'.format(out_feat),nn.LeakyReLU(0.2, inplace=True)) cndf = cndf * csize = csize / # 最后一层卷积,将4*4变为1*,得到nz = 100的噪声
if add_final_conv: main.add_module('final-{0}-{1}-conv'.format(cndf, ),nn.Conv2d(cndf, nz, , , , bias=False))
self.main=main def forward(self,input):
if self.ngpu>:
output=nn.parallel.data_parallel(self.main,input,range(self.ngpu)) #在多个gpu上运行模型,并行计算
else:
output=self.main(input) return output #如果输入的大小是3××,最后的输出是100××.
判别器为:
#定义判别器D (编码器)
class NetD(nn.Module):
def __init__(self, opt):
super(NetD, self).__init__() # 第二个参数是1 因为判别器最后输出一个数
# 不过编码器在生成器里的时候
# 这个参数是100 因为它要把图片下采样成100××1的向量
model = Encoder(opt.isize, , opt.nc, opt.ndf, opt.ngpu, opt.extralayers) layers = list(model.main.children()) '''layers的输出如下:
[
Conv2d(, , kernel_size=(, ), stride=(, ), padding=(, ), bias=False), LeakyReLU(negative_slope=0.2, inplace), Conv2d(, , kernel_size=(, ), stride=(, ), padding=(, ), bias=False), BatchNorm2d(, eps=1e-, momentum=0.1, affine=True, track_running_stats=True), LeakyReLU(negative_slope=0.2, inplace), Conv2d(, , kernel_size=(, ), stride=(, ), padding=(, ), bias=False), BatchNorm2d(, eps=1e-, momentum=0.1, affine=True, track_running_stats=True), LeakyReLU(negative_slope=0.2, inplace), Conv2d(, , kernel_size=(, ), stride=(, ), bias=False)] 因为132行定义的nz参数是1,所以经过这层之后输出的大小是1××
''' self.features = nn.Sequential(*layers[:-])
# self.features的内容为除了最后一层的前8层
# nn.Sequential函数里面的参数一定是Module的子类,而list不是一个模块子类,所以不能当做参数
# 当然model.children()也一样不是一个模块子类,只有他们里面的值才是
# 这里的*就起了作用,将list或者children的内容迭代地一个一个的传进去。 #生成一个分类器模块
self.classifier = nn.Sequential(layers[-])
#self.classifier的内容为Conv2d(, , kernel_size=(, ), stride=(, ), bias=False)最后一层
#并在后面添加子模块sigmoid
self.classifier.add_module('Sigmoid', nn.Sigmoid()) def forward(self, x): features = self.features(x) #图片通过前8层之后的结果256××,前提是输入的图片的大小是32 features = features classifier = self.classifier(features)#此时的结果是1××,值在[,],因为经过了sigmoid classifier = classifier.view(-, ).squeeze()
#a=torch.ones([,,]) 即a=tensor([[[ .]]]) 再a.view(-,) 变成tensor([[ .]]) 再加一个squeeze就是
# a.view(-,).squeeze() 结果是tensor([ .]),squeeze里的值是0是1随意,只要去掉一个维度上的就可以 # 因此返回得到一个判别值classifier 和一个大小为256××4的特征值
return classifier, features
重点在:
- layers = list(model.main.children())
- self.features = nn.Sequential(*layers[:-1]) :使用除了最后一层的前面所有层
- self.classifier = nn.Sequential(layers[-1]):仅使用最后一层
这里可见同样的模型结构我们在Encoder.py中已经定义过一遍了,在判别其中实在不想再定义一遍,那我们就能够使用model.main.children()来获得模块中的子模块,然后将其转成列表形式,然后就能够根据想要的部分来进行处理了
pytorch怎么使用定义好的模型的一部分的更多相关文章
- 详解Pytorch中的网络构造,模型save和load,.pth权重文件解析
转载:https://zhuanlan.zhihu.com/p/53927068 https://blog.csdn.net/wangdongwei0/article/details/88956527 ...
- pytorch入门2.2构建回归模型初体验(开始训练)
pytorch入门2.x构建回归模型系列: pytorch入门2.0构建回归模型初体验(数据生成) pytorch入门2.1构建回归模型初体验(模型构建) pytorch入门2.2构建回归模型初体验( ...
- pytorch入门2.1构建回归模型初体验(模型构建)
pytorch入门2.x构建回归模型系列: pytorch入门2.0构建回归模型初体验(数据生成) pytorch入门2.1构建回归模型初体验(模型构建) pytorch入门2.2构建回归模型初体验( ...
- pytorch入门2.0构建回归模型初体验(数据生成)
pytorch入门2.x构建回归模型系列: pytorch入门2.0构建回归模型初体验(数据生成) pytorch入门2.1构建回归模型初体验(模型构建) pytorch入门2.2构建回归模型初体验( ...
- 【小白学PyTorch】18 TF2构建自定义模型
[机器学习炼丹术]的炼丹总群已经快满了,要加入的快联系炼丹兄WX:cyx645016617 参考目录: 目录 1 创建自定义网络层 2 创建一个完整的CNN 2.1 keras.Model vs ke ...
- 1、pytorch写的第一个Linear模型(原始版,不调用nn.Modules模块)
参考: https://github.com/Iallen520/lhy_DL_Hw/blob/master/PyTorch_Introduction.ipynb 模拟一个回归模型,y = X * w ...
- 重磅发布 | 全球首个云原生应用标准定义与架构模型 OAM 正式开源
作者: OAM 项目负责人 导读:2019 年 10 月 17 日,阿里巴巴合伙人.阿里云智能基础产品事业部总经理蒋江伟(花名:小邪)在 Qcon 上海重磅宣布,阿里云与微软联合推出开放应用模型 Op ...
- 开放应用模型(OAM):全球首个云原生应用标准定义与架构模型
Kubernetes 项目作为容器编排领域的事实标准, 成功推动了诸如阿里云 Kubernetes (ACK)等云原生服务的迅速增长.但同时我们也关注到,Kubernetes 的核心 API 资源比如 ...
- [Pytorch]基于混和精度的模型加速
这篇博客是在pytorch中基于apex使用混合精度加速的一个偏工程的描述,原理层面的解释并不是这篇博客的目的,不过在参考部分提供了非常有价值的资料,可以进一步研究. 一个关键原则:“仅仅在权重更新的 ...
随机推荐
- 《BUG创造队》作业9:【Beta】冲刺 Scrum meeting 3
项目 内容 这个作业属于哪个课程 2016级软件工程 这个作业的要求在哪里 实验十三 团队作业9:Beta冲刺与团队项目验收 团队名称 BUG创造队 作业学习目标 (1)掌握软件黑盒测试技术:(2)学 ...
- 洛谷 P1955 [NOI2015]程序自动分析 题解
每日一题 day22 打卡 Analysis 离散化+并查集 先离散化所有的约束条件,再处理所有e=1的条件,将i的祖先和j的祖先合并到一个集合中:e=0时,如果i的祖先与j的祖先在同一个集合中,说明 ...
- Linux rpm安装指定安装路径
可以使用prefix参数. rpm -i –prefix=/home/gpadmin greenplum-db-6.0.0-rhel6-x86_64.rpm 将greenplum-db-6.0. ...
- PHP命令行执行文件或代码
Linux环境 1.执行代码 php -r "echo 'hello';" (注意加分号,与PHP文件一样) 2.执行文件 php -f 文件所在路径(/var/www/xxx. ...
- BZOJ 2159: Crash 的文明世界 第二类斯特林数+树形dp
这个题非常巧妙啊~ #include <bits/stdc++.h> #define M 170 #define N 50003 #define mod 10007 #define LL ...
- am335x system upgrade kernel ec20 simcom7600ce(十一)
1 Scope of Document This document describes 4G hardware design, support quectel ec20 4G module/ ...
- Ubuntu安装php7.0环境
1.下载必须组件 sudo apt-get install libxml2-dev sudo apt-get install curl 参考文献:http://php.net/manual/zh/in ...
- Angular动态组件
一.主页面: app.component.html: <button (click)="load();">动态</button> 2<div #dom ...
- 【洛谷】P3188 [HNOI2007]梦幻岛宝珠
题目描述 给你N颗宝石,每颗宝石都有重量和价值.要你从这些宝石中选取一些宝石,保证总重量不超过W,且总价值最大为,并输出最大的总价值. 数据范围:N<=100;W<=2^30,并且保证每 ...
- Tuxedo 介绍
快速阅读 介绍Tuxedo,以及webLogic两个中间件,都是oracle旗下的产品 ,现在各银行系统用的最多.因为有部分项目涉及,所以有必须弄清楚,明白 . 什么是Tuxedo 官方介绍:http ...