侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

pytorch构建多模型实例

Python  /  管理员 发布于 6年前   460

pytorch构建双模型

第一部分:构建"se_resnet152","DPN92()"双模型

import numpy as npfrom functools import partialimport torchfrom torch import nnimport torch.nn.functional as Ffrom torch.optim import SGD,Adamfrom torch.autograd import Variablefrom torch.utils.data import Dataset, DataLoaderfrom torch.optim.optimizer import Optimizerimport torchvisionfrom torchvision import modelsimport pretrainedmodelsfrom pretrainedmodels.models import *from torch import nnfrom torchvision import transforms as Timport randomrandom.seed(2050)np.random.seed(2050)torch.manual_seed(2050)torch.cuda.manual_seed_all(2050)class FCViewer(nn.Module):  def forward(self, x):    return x.view(x.size(0), -1)  '''Dual Path Networks in PyTorch.'''class Bottleneck(nn.Module):  def __init__(self, last_planes, in_planes, out_planes, dense_depth, stride, first_layer):    super(Bottleneck, self).__init__()    self.out_planes = out_planes    self.dense_depth = dense_depth    self.conv1 = nn.Conv2d(last_planes, in_planes, kernel_size=1, bias=False)    self.bn1 = nn.BatchNorm2d(in_planes)    self.conv2 = nn.Conv2d(in_planes, in_planes, kernel_size=3, stride=stride, padding=1, groups=32, bias=False)    self.bn2 = nn.BatchNorm2d(in_planes)    self.conv3 = nn.Conv2d(in_planes, out_planes+dense_depth, kernel_size=1, bias=False)    self.bn3 = nn.BatchNorm2d(out_planes+dense_depth)    self.shortcut = nn.Sequential()    if first_layer:      self.shortcut = nn.Sequential(        nn.Conv2d(last_planes, out_planes+dense_depth, kernel_size=1, stride=stride, bias=False),        nn.BatchNorm2d(out_planes+dense_depth)      )  def forward(self, x):    out = F.relu(self.bn1(self.conv1(x)))    out = F.relu(self.bn2(self.conv2(out)))    out = self.bn3(self.conv3(out))    x = self.shortcut(x)    d = self.out_planes    out = torch.cat([x[:,:d,:,:]+out[:,:d,:,:], x[:,d:,:,:], out[:,d:,:,:]], 1)    out = F.relu(out)    return outclass DPN(nn.Module):  def __init__(self, cfg):    super(DPN, self).__init__()    in_planes, out_planes = cfg['in_planes'], cfg['out_planes']    num_blocks, dense_depth = cfg['num_blocks'], cfg['dense_depth']    self.conv1 = nn.Conv2d(7, 64, kernel_size=3, stride=1, padding=1, bias=False)    self.bn1 = nn.BatchNorm2d(64)    self.last_planes = 64    self.layer1 = self._make_layer(in_planes[0], out_planes[0], num_blocks[0], dense_depth[0], stride=1)    self.layer2 = self._make_layer(in_planes[1], out_planes[1], num_blocks[1], dense_depth[1], stride=2)    self.layer3 = self._make_layer(in_planes[2], out_planes[2], num_blocks[2], dense_depth[2], stride=2)    self.layer4 = self._make_layer(in_planes[3], out_planes[3], num_blocks[3], dense_depth[3], stride=2)    self.linear = nn.Linear(out_planes[3]+(num_blocks[3]+1)*dense_depth[3], 64)     self.bn2 = nn.BatchNorm1d(64)  def _make_layer(self, in_planes, out_planes, num_blocks, dense_depth, stride):    strides = [stride] + [1]*(num_blocks-1)    layers = []    for i,stride in enumerate(strides):      layers.append(Bottleneck(self.last_planes, in_planes, out_planes, dense_depth, stride, i==0))      self.last_planes = out_planes + (i+2) * dense_depth    return nn.Sequential(*layers)  def forward(self, x):    out = F.relu(self.bn1(self.conv1(x)))    out = self.layer1(out)    out = self.layer2(out)    out = self.layer3(out)    out = self.layer4(out)    out = F.avg_pool2d(out, 4)    out = out.view(out.size(0), -1)    out = self.linear(out)    out= F.relu(self.bn2(out))    return outdef DPN26():  cfg = {    'in_planes': (96,192,384,768),    'out_planes': (256,512,1024,2048),    'num_blocks': (2,2,2,2),    'dense_depth': (16,32,24,128)  }  return DPN(cfg)def DPN92():  cfg = {    'in_planes': (96,192,384,768),    'out_planes': (256,512,1024,2048),    'num_blocks': (3,4,20,3),    'dense_depth': (16,32,24,128)  }  return DPN(cfg)class MultiModalNet(nn.Module):  def __init__(self, backbone1, backbone2, drop, pretrained=True):    super().__init__()    if pretrained:      img_model = pretrainedmodels.__dict__[backbone1](num_classes=1000, pretrained='imagenet') #seresnext101    else:      img_model = pretrainedmodels.__dict__[backbone1](num_classes=1000, pretrained=None)        self.visit_model=DPN26()        self.img_encoder = list(img_model.children())[:-2]    self.img_encoder.append(nn.AdaptiveAvgPool2d(1))        self.img_encoder = nn.Sequential(*self.img_encoder)    if drop > 0:      self.img_fc = nn.Sequential(FCViewer(),      nn.Dropout(drop),      nn.Linear(img_model.last_linear.in_features, 512),      nn.BatchNorm1d(512))          else:      self.img_fc = nn.Sequential(        FCViewer(),        nn.BatchNorm1d(img_model.last_linear.in_features),        nn.Linear(img_model.last_linear.in_features, 512))    self.bn=nn.BatchNorm1d(576)    self.cls = nn.Linear(576,9)   def forward(self, x_img,x_vis):    x_img = self.img_encoder(x_img)    x_img = self.img_fc(x_img)    x_vis=self.visit_model(x_vis)    x_cat = torch.cat((x_img,x_vis),1)    x_cat = F.relu(self.bn(x_cat))    x_cat = self.cls(x_cat)    return x_cattest_x = Variable(torch.zeros(64, 7,26,24))test_x1 = Variable(torch.zeros(64, 3,224,224))model=MultiModalNet("se_resnet152","DPN92()",0.1)out=model(test_x1,test_x)print(model._modules.keys())print(model)print(out.shape)

第二部分构建densenet201单模型

#encoding:utf-8import torchvision.models as modelsimport torchimport pretrainedmodelsfrom torch import nnfrom torch.autograd import Variable#model = models.resnet18(pretrained=True)#print(model)#print(model._modules.keys())#feature = torch.nn.Sequential(*list(model.children())[:-2])#模型的结构#print(feature)'''class FCViewer(nn.Module):  def forward(self, x):    return x.view(x.size(0), -1)class M(nn.Module):  def __init__(self, backbone1, drop, pretrained=True):    super(M,self).__init__()    if pretrained:      img_model = pretrainedmodels.__dict__[backbone1](num_classes=1000, pretrained='imagenet')     else:      img_model = pretrainedmodels.__dict__[backbone1](num_classes=1000, pretrained=None)        self.img_encoder = list(img_model.children())[:-1]    self.img_encoder.append(nn.AdaptiveAvgPool2d(1))    self.img_encoder = nn.Sequential(*self.img_encoder)    if drop > 0:      self.img_fc = nn.Sequential(FCViewer(),      nn.Dropout(drop),      nn.Linear(img_model.last_linear.in_features, 236))          else:      self.img_fc = nn.Sequential(        FCViewer(),        nn.Linear(img_model.last_linear.in_features, 236)      )    self.cls = nn.Linear(236,9)   def forward(self, x_img):    x_img = self.img_encoder(x_img)    x_img = self.img_fc(x_img)    return x_img model1=M('densenet201',0,pretrained=True)print(model1)print(model1._modules.keys())feature = torch.nn.Sequential(*list(model1.children())[:-2])#模型的结构feature1 = torch.nn.Sequential(*list(model1.children())[:])#print(feature)#print(feature1)test_x = Variable(torch.zeros(1, 3, 100, 100))out=feature(test_x)print(out.shape)''''''import torch.nn.functional as Fclass LenetNet(nn.Module):  def __init__(self):    super(LenetNet, self).__init__()    self.conv1 = nn.Conv2d(7, 6, 5)     self.conv2 = nn.Conv2d(6, 16, 5)     self.fc1  = nn.Linear(144, 120)    self.fc2  = nn.Linear(120, 84)    self.fc3  = nn.Linear(84, 10)  def forward(self, x):     x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))     x = F.max_pool2d(F.relu(self.conv2(x)), 2)    x = x.view(x.size()[0], -1)     x = F.relu(self.fc1(x))    x = F.relu(self.fc2(x))    x = self.fc3(x)        return xmodel1=LenetNet()#print(model1)#print(model1._modules.keys())feature = torch.nn.Sequential(*list(model1.children())[:-3])#模型的结构#feature1 = torch.nn.Sequential(*list(model1.children())[:])print(feature)#print(feature1)test_x = Variable(torch.zeros(1, 7, 27, 24))out=model1(test_x)print(out.shape)class FCViewer(nn.Module):  def forward(self, x):    return x.view(x.size(0), -1)class M(nn.Module):  def __init__(self):    super(M,self).__init__()    img_model =model1     self.img_encoder = list(img_model.children())[:-3]    self.img_encoder.append(nn.AdaptiveAvgPool2d(1))    self.img_encoder = nn.Sequential(*self.img_encoder)    self.img_fc = nn.Sequential(FCViewer(),      nn.Linear(16, 236))    self.cls = nn.Linear(236,9)   def forward(self, x_img):    x_img = self.img_encoder(x_img)    x_img = self.img_fc(x_img)    return x_img model2=M()test_x = Variable(torch.zeros(1, 7, 27, 24))out=model2(test_x)print(out.shape)'''

以上这篇pytorch构建多模型实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。


  • 上一条:
    Pytorch 定义MyDatasets实现多通道分别输入不同数据方式
    下一条:
    利用Pytorch实现简单的线性回归算法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • AI Agent
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • opencode + Oh-my-openagent,我的第一个免费的ai编程智能体管家:Sisyphus(0个评论)
    • Google AI Studio升级全栈 vibe coding体验,可直接构建带登录和数据库的应用(0个评论)
    • Apifox桌面端被曝遭供应链投毒:CDN 脚本被篡改,窃取 SSH 密钥与 Git 凭证(0个评论)
    • 在go语言中实现字符串可逆性压缩及解压缩功能(0个评论)
    • 使用go + gin + jwt + qrcode实现网站生成登录二维码在app中扫码登录功能(0个评论)
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 近期评论
    • test1 在

      opencode + Oh-my-openagent,我的第一个免费的ai编程智能体管家:Sisyphus中评论 test..
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • Zita 在

      Google AI Studio升级全栈 vibe coding体验,可直接构建带登录和数据库的应用中评论 111222..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客