对image进行1x1卷积,输入为
(
b
a
t
c
h
,
C
,
H
,
W
)
(batch, C, H, W)
(batch,C,H,W),如下例子,输入为
(
4
,
3
,
225
,
225
)
(4, 3, 225, 225)
(4,3,225,225)的图片,输出为
(
4
,
256
,
225
,
225
)
(4, 256, 225, 225)
(4,256,225,225)的特征图
import torch
import torch.nn as nn
batch = 4
C = 3
H = W = 225
x = torch.rand((batch, C, H, W))
conv = nn.Conv2d(in_channels=C, out_channels=256, kernel_size=1, stride=1, padding=0)
out = conv(x)
print(out.shape)
对3维张量进行1x1卷积,由于pytorch进行2D卷积要求输入是4维张量,因此要对形如
(
b
a
t
c
h
,
C
,
N
)
(batch,C,N)
(batch,C,N)的张量进行1x1卷积时要先将其扩展一个维度,可以扩展为
(
b
a
t
c
h
,
C
,
1
,
N
)
(batch,C,1,N)
(batch,C,1,N)或
(
b
a
t
c
h
,
C
,
N
,
1
)
(batch,C,N,1)
(batch,C,N,1)再进行1x1卷积,结果都是一样的,例如
import torch
import torch.nn as nn
torch.manual_seed(28)
x = torch.rand((4, 128, 2048))
conv = nn.Conv2d(128, 256, kernel_size=1, stride=1)
out1 = conv(x.unsqueeze(-1))
res1 = out1.squeeze(-1)
print(res1.shape)
print(torch.sum(torch.sum(torch.sum(res1, dim=-1), dim=-1), dim=-1))
out2 = conv(x.unsqueeze(-2))
res2 = out2.squeeze(-2)
print(res2.shape)
print(torch.sum(torch.sum(torch.sum(res2, dim=-1), dim=-1), dim=-1))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)