pytorch构建多模型实例
作者:朴素.无恙 发布时间:2021-05-14 13:30:00
标签:pytorch,构建,模型
pytorch构建双模型
第一部分:构建"se_resnet152","DPN92()"双模型
import numpy as np
from functools import partial
import torch
from torch import nn
import torch.nn.functional as F
from torch.optim import SGD,Adam
from torch.autograd import Variable
from torch.utils.data import Dataset, DataLoader
from torch.optim.optimizer import Optimizer
import torchvision
from torchvision import models
import pretrainedmodels
from pretrainedmodels.models import *
from torch import nn
from torchvision import transforms as T
import random
random.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 out
class 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 out
def 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_cat
test_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-8
import torchvision.models as models
import torch
import pretrainedmodels
from torch import nn
from 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 F
class 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 x
model1=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)
'''
来源:https://blog.csdn.net/weixin_40123108/article/details/90670584
0
投稿
猜你喜欢
- 程序设计中会经常碰到一种情况,就是事先无法得知用户会需要哪些数据,必须根据用户选择后再从服务器重新提取数据后反馈给用户。比如一简单的情况,用
- 形参可以设置参数默认值,设置遵循从右至左原则例如:fun(x=0,y=1),fun(x,y=1),但不可以是fun(x=1,y)形参设置可以
- 进行深度学习时,对图像进行预处理的过程是非常重要的,使用pytorch或者TensorFlow时需要对图像进行预处理以及展示来观看处理效果,
- 而标准的事件触发可以使用dispatchEvent方法。但现在FF5无法触发了A的默认行为了。如下 <!doctype html>
- 在Linux下C连接MySQL出现问题如下:编译成功后,运行程序,出现./connect: error while loading shar
- get_template()中使用子目录把所有的模板都存放在一个目录下可能会让事情变得难以掌控。 你可能会考虑把模板存放在你模板目录的子目录
- 最近学到了一个有趣的装饰器写法,就记录一下。装饰器是一个返回函数的函数。写一个装饰器,除了最常见的在函数中定义函数以外,Python还允许使
- PHP chr() 函数实例从不同 ASCII 值返回字符:<?php echo chr(52) . "<br>
- ORM 查询管理器对于 ORM 定义: 对象关系映射, Object Relational Mapping, ORM, 是一种程序设计技术,
- 数据API数据集方法不会修改数据集,而是创建新数据集。可通过调用 map() 方法将转换应用于每个元素:dataset = dataset.
- 如下所示:# the basic ways = 0for x in range(10): s += x# the right ways =
- JMeter可以通过os命令调用Python脚本,Python同样可以通过系统命令调用JMeter执行压测Python调用JMeter首先要
- 前言删除列表中的元素十分简单,有很多方法。使用最多的是remove方法,remove() 方法从集合中删除指定的元素。此方法与discard
- python的库一般都用pip安装。但是有时候也会出现在线安装失败的情况,如下图安装PIL模块时报错:这时候可以采取离线安装的方式;一、首先
- 一、求算术平方根a=x=int(raw_input('Enter a number:'))if x >= :while
- 当需要制作转动鼠标滚轮放大页面字体这样的交互效果时,会用到 Mousewheel 事件。其实在大多数浏览器(IE6, IE7, IE8, O
- create or replace PROCEDURE proceudre_name AS BEGIN DECLARE sPara VARC
- 在大多数语音识别任务中,我们都缺少文本和音频特征的alignment,Connectionist Temporal Classificati
- 1. 引言在日常工作中,大家都需要进行字典的相关操作,对于某些初学者,经常会写一堆繁琐的代码来实现某项简单的功能。本篇文章重点介绍一些在Py
- 本文实例讲述了PHP基于phpqrcode类生成二维码的方法。分享给大家供大家参考,具体如下:使用PHP语言生成二维码,还是挺有难度的,当然