对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) # (4, 256, 225, 225)
对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) # (4,256,2048)
print(torch.sum(torch.sum(torch.sum(res1, dim=-1), dim=-1), dim=-1)) # 17060.7676
out2 = conv(x.unsqueeze(-2))
res2 = out2.squeeze(-2)
print(res2.shape) # (4,256,2048)
print(torch.sum(torch.sum(torch.sum(res2, dim=-1), dim=-1), dim=-1)) # 17060.7676