from torch import nn import torch import torch.nn.functional as F from torch.autograd import Variable import math from torch.nn import Parameter class Bottleneck(nn.Module): def __init__(self, inp, oup, stride, expansion): super(Bottleneck, self).__init__() self.connect = stride == 1 and inp == oup self.conv = nn.Sequential( # pw nn.Conv2d(inp, inp * expansion, 1, 1, 0, bias=False), nn.BatchNorm2d(inp * expansion), nn.ReLU(inplace=True), # dw nn.Conv2d(inp * expansion, inp * expansion, 3, stride, 1, groups=inp * expansion, bias=False), nn.BatchNorm2d(inp * expansion), nn.ReLU(inplace=True), # pw-linear nn.Conv2d(inp * expansion, oup, 1, 1, 0, bias=False), nn.BatchNorm2d(oup), ) def forward(self, x): if self.connect: return x + self.conv(x) else: return self.conv(x) class ConvBlock(nn.Module): def __init__(self, inp, oup, k, s, p, dw=False, linear=False): super(ConvBlock, self).__init__() self.linear = linear if dw: self.conv = nn.Conv2d(inp, oup, k, s, p, groups=inp, bias=False) else: self.conv = nn.Conv2d(inp, oup, k, s, p, bias=False) self.bn = nn.BatchNorm2d(oup) if not linear: # [중요] 보드 호환성을 위해 PReLU 대신 ReLU 사용 self.relu = nn.ReLU(inplace=True) def forward(self, x): x = self.conv(x) x = self.bn(x) if self.linear: return x else: return self.relu(x) Mobilefacenet_bottleneck_setting = [ # t, c , n ,s [2, 64, 5, 2], [4, 128, 1, 2], [2, 128, 6, 1], [4, 128, 1, 2], [2, 128, 2, 1] ] class MobileFacenet(nn.Module): def __init__(self, bottleneck_setting=Mobilefacenet_bottleneck_setting): super(MobileFacenet, self).__init__() self.conv1 = ConvBlock(3, 64, 3, 2, 1) self.dw_conv1 = ConvBlock(64, 64, 3, 1, 1, dw=True) self.inplanes = 64 block = Bottleneck self.blocks = self._make_layer(block, bottleneck_setting) # 1. 기존 1x1 Expansion (128 -> 512) self.conv2 = ConvBlock(128, 512, 1, 1, 0) # 2. [추가] Feature Mixing Layer (512 -> 512) # AvgPool 전에 채널 정보를 섞어주고 비선형성(ReLU)을 추가하여 표현력 보강 # Kernel=1 이라 NPU 호환성 좋음 & Static Shape self.conv3 = ConvBlock(512, 512, 1, 1, 0) # 3. [수정] Global Average Pooling (8x8 -> 1x1) # Kernel=8인 ConvBlock 대신 사용. Static Shape 유지. self.gap = nn.Sequential(nn.AvgPool2d(kernel=8)) # 4. Final Embedding Layer (512 -> 128) self.linear1 = ConvBlock(512, 128, 1, 1, 0, linear=True) for m in self.modules(): if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() def _make_layer(self, block, setting): layers = [] for t, c, n, s in setting: for i in range(n): if i == 0: layers.append(block(self.inplanes, c, s, t)) else: layers.append(block(self.inplanes, c, 1, t)) self.inplanes = c return nn.Sequential(*layers) def forward(self, x): x = self.conv1(x) x = self.dw_conv1(x) x = self.blocks(x) x = self.conv2(x) # (Batch, 512, 8, 8) # [신규 아키텍처 적용] x = self.conv3(x) # (Batch, 512, 8, 8) -> 추가된 Mixing Layer x = self.gap(x) # (Batch, 512, 1, 1) -> 8x8 영역 평균 x = self.linear1(x) # (Batch, 128, 1, 1) x = x.view(x.size(0), -1) # (Batch, 128) return x class ArcMarginProduct(nn.Module): def __init__(self, in_features=128, out_features=200, s=32.0, m=0.50, easy_margin=False): super(ArcMarginProduct, self).__init__() self.in_features = in_features self.out_features = out_features self.s = s self.m = m self.weight = Parameter(torch.Tensor(out_features, in_features)) nn.init.xavier_uniform_(self.weight) self.easy_margin = easy_margin self.cos_m = math.cos(m) self.sin_m = math.sin(m) self.th = math.cos(math.pi - m) self.mm = math.sin(math.pi - m) * m def forward(self, x, label): cosine = F.linear(F.normalize(x), F.normalize(self.weight)) sine = torch.sqrt(1.0 - torch.pow(cosine, 2)) phi = cosine * self.cos_m - sine * self.sin_m if self.easy_margin: phi = torch.where(cosine > 0, phi, cosine) else: phi = torch.where((cosine - self.th) > 0, phi, cosine - self.mm) one_hot = torch.zeros(cosine.size(), device='cuda') one_hot.scatter_(1, label.view(-1, 1).long(), 1) output = (one_hot * phi) + ((1.0 - one_hot) * cosine) output *= self.s return output if __name__ == "__main__": # 해상도 128x128 테스트 (Static Shape 확인) input = Variable(torch.FloatTensor(2, 3, 128, 128)) net = MobileFacenet() print("Network Created") x = net(input) print("Output Shape:", x.shape) # Expected: [2, 128]