这篇还是common.py
class Focus(nn.Module):
"""Focuses spatial information into channel space using slicing and convolution for efficient feature extraction."""
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True):
"""Initializes Focus module to concentrate width-height info into channel space with configurable convolution
parameters.
"""
super().__init__()
self.conv = Conv(c1 * 4, c2, k, s, p, g, act=act)
# self.contract = Contract(gain=2)
def forward(self, x):
"""Processes input through Focus mechanism, reshaping (b,c,w,h) to (b,4c,w/2,h/2) then applies convolution."""
return self.conv(torch.cat((x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]), 1))
# return self.conv(self.contract(x))
x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]
以6*6举例
A = np.arange(36).reshape(6, 6)
A = torch.from_numpy(A)
C1 = A[..., ::2, ::2]
C2 = A[..., 1::2, ::2]
C3 = A[..., ::2, 1::2]
C4 = A[..., 1::2, 1::2]
C5 = torch.cat((C1, C2, C3, C4), 1)
A
C1——[…, ::2, ::2]
显然是从0开始每行每列间隔1位提取
C2——[…, 1::2, ::2]
从1行开始每行每列间隔1位提取
C3——[…, ::2, 1::2]
从1列开始每行每列间隔1位提取
C4——[…, 1::2, 1::2]