pytorch——tensor.expand_as()函数和AdaptiveAvgPool2d函数

1.tensor.expand.as()函数

import torch
x = torch.tensor([[1], [2], [3]])  # 生成tensor x,3行1列
print('xsize:',x.size())
print('x:',x)

xsize: torch.Size([3, 1])
x: tensor([[1],
           [2],
           [3]])


x_expand=x.expand(3,4)  # 将x扩展为3行4列
print('x_expand:',x_expand)

x_expand: tensor([[1, 1, 1, 1],
                  [2, 2, 2, 2],
                  [3, 3, 3, 3]]) 


x_expand=x.expand(-1,4)  # -1 means not changing the size of that dimension
print('x_expand:',x_expand)

x_expand: tensor([[1, 1, 1, 1],
                  [2, 2, 2, 2],
                  [3, 3, 3, 3]])
 
 
x_expand_as=x.expand_as(x_expand) # 将x的形状变为和x_expand一样
print('x_expand_as:',x_expand_as)

x_expand_as: tensor([[1, 1, 1, 1],
                     [2, 2, 2, 2],
                     [3, 3, 3, 3]])
————————————————
版权声明:本文为CSDN博主「机器不学习我学习」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/AugustMe/article/details/121473996

就是会将维度更小的tensor在同一行用相同的数来填充,用于se模块中,求出各个通道之间的权重之后与原数据相乘进行加权操作。

2.AdaptiveAvgPool2d函数

nn.AdaptiveAvgPool2d((1,1)),首先这句话的含义是使得池化后的每个通道上的大小是一个1x1的,也就是每个通道上只有一个像素点。(1,1)表示的outputsize。

原型如下:

如题:只需要给定输出特征图的大小就好,其中通道数前后不发生变化。具体如下:

AdaptiveAvgPool2d

CLASStorch.nn.AdaptiveAvgPool2d(output_size)[SOURCE]

Applies a 2D adaptive average pooling over an input signal composed of several input planes.

The output is of size H x W, for any input size. The number of output features is equal to the number of input planes.

Parameters

output_size – the target output size of the image of the form H x W. Can be a tuple (H, W) or a single H for a square image H x H. H and W can be either a int, or None which means the size will be the same as that of the input.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import torch import torch.nn as nn import torch.nn.functional as F from torch.autograd import Variable 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(3, 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], 10) def _make_layer(self, in_planes, out_planes, num_blocks, dense_depth, stride): strides = [stride] + 1 layers = [] for i,stride in (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) return out 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)基于这个程序改成对摄像头采集的图像检测与分类输出坐标、大小和种类
05-05

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值