Focus的作用及pytorch实现

Focus的作用及pytorch实现_第1张图片

用于切片操作,比如4*4*3变成了2*2*12的特征图

class Focus(nn.Module):
    # Focus wh information into c-space
    def __init__(self, c1, c2, k=1):
        super(Focus, self).__init__()
        self.conv = Conv(c1 * 4, c2, k, 1)

    def forward(self, x):  # x(b,c,w,h) -> y(b,4c,w/2,h/2)
        return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))

import torch
x = torch.rand((1,3,640,640))
f = Focus(3,12)
print(f(x).shape)

 

你可能感兴趣的:(计算机视觉,深度学习)