Commit
·
16b5241
1
Parent(s):
1819a06
upload model to huggingface
Browse files- model.py +97 -0
- requirements.txt +5 -0
model.py
CHANGED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torchvision
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torch.optim as optim
|
| 5 |
+
import math
|
| 6 |
+
|
| 7 |
+
class ImprovedEfficientBackbone(nn.Module):
|
| 8 |
+
def __init__(self):
|
| 9 |
+
super().__init__()
|
| 10 |
+
self.efficientnet = torchvision.models.efficientnet_b0(weights=torchvision.models.EfficientNet_B0_Weights.IMAGENET1K_V1)
|
| 11 |
+
self.features = self.efficientnet.features
|
| 12 |
+
|
| 13 |
+
def forward(self, x):
|
| 14 |
+
return self.features(x)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class ImprovedPatchEmbedding(nn.Module):
|
| 18 |
+
def __init__(self, in_channels=1280, embed_dim=384):
|
| 19 |
+
super().__init__()
|
| 20 |
+
self.proj = nn.Linear(in_channels, embed_dim)
|
| 21 |
+
|
| 22 |
+
def forward(self, x):
|
| 23 |
+
"""
|
| 24 |
+
Input: [B, 1280, 7, 7]
|
| 25 |
+
Output: [B, 49, 384]
|
| 26 |
+
"""
|
| 27 |
+
B, C, H, W = x.shape
|
| 28 |
+
x = x.flatten(2).transpose(1, 2)
|
| 29 |
+
x = self.proj(x)
|
| 30 |
+
return x
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class ImprovedViTBlock(nn.Module):
|
| 34 |
+
def __init__(self, embed_dim=384, num_heads=4, mlp_ratio=4):
|
| 35 |
+
super().__init__()
|
| 36 |
+
self.norm1 = nn.LayerNorm(embed_dim)
|
| 37 |
+
self.attn = nn.MultiheadAttention(embed_dim, num_heads, batch_first=True)
|
| 38 |
+
self.norm2 = nn.LayerNorm(embed_dim)
|
| 39 |
+
self.mlp = nn.Sequential(
|
| 40 |
+
nn.Linear(embed_dim, embed_dim * mlp_ratio),
|
| 41 |
+
nn.GELU(),
|
| 42 |
+
nn.Linear(embed_dim * mlp_ratio, embed_dim)
|
| 43 |
+
)
|
| 44 |
+
self.dropout = nn.Dropout(0.1)
|
| 45 |
+
|
| 46 |
+
def forward(self, x):
|
| 47 |
+
x = x + self.dropout(self.attn(self.norm1(x), self.norm1(x), self.norm1(x))[0])
|
| 48 |
+
x = x + self.dropout(self.mlp(self.norm2(x)))
|
| 49 |
+
return x
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class ImprovedEfficientViT(nn.Module):
|
| 54 |
+
def __init__(self, embed_dim=384, depth=8, num_heads=4):
|
| 55 |
+
super().__init__()
|
| 56 |
+
self.backbone = ImprovedEfficientBackbone()
|
| 57 |
+
self.patch_embed = ImprovedPatchEmbedding(embed_dim=embed_dim)
|
| 58 |
+
|
| 59 |
+
self.cls_token = nn.Parameter(torch.randn(1, 1, embed_dim))
|
| 60 |
+
self.register_buffer("pos_embed", self._get_sinusoidal_encoding(50, embed_dim)) # Use sin/cos
|
| 61 |
+
|
| 62 |
+
self.blocks = nn.ModuleList([ImprovedViTBlock(embed_dim, num_heads) for _ in range(depth)])
|
| 63 |
+
|
| 64 |
+
self.head = nn.Sequential(
|
| 65 |
+
nn.LayerNorm(embed_dim),
|
| 66 |
+
nn.Linear(embed_dim, 128),
|
| 67 |
+
nn.GELU(),
|
| 68 |
+
nn.Linear(128, 1)
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
self._init_weights()
|
| 72 |
+
|
| 73 |
+
def _init_weights(self):
|
| 74 |
+
nn.init.trunc_normal_(self.cls_token, std=0.02)
|
| 75 |
+
|
| 76 |
+
def _get_sinusoidal_encoding(self, seq_len, dim):
|
| 77 |
+
pe = torch.zeros(seq_len, dim)
|
| 78 |
+
position = torch.arange(0, seq_len, dtype=torch.float).unsqueeze(1)
|
| 79 |
+
div_term = torch.exp(torch.arange(0, dim, 2).float() * (-math.log(10000.0) / dim))
|
| 80 |
+
pe[:, 0::2] = torch.sin(position * div_term)
|
| 81 |
+
pe[:, 1::2] = torch.cos(position * div_term)
|
| 82 |
+
return pe.unsqueeze(0)
|
| 83 |
+
|
| 84 |
+
def forward(self, x):
|
| 85 |
+
features = self.backbone(x)
|
| 86 |
+
tokens = self.patch_embed(features)
|
| 87 |
+
|
| 88 |
+
B = tokens.shape[0]
|
| 89 |
+
cls_tokens = self.cls_token.expand(B, -1, -1)
|
| 90 |
+
x = torch.cat((cls_tokens, tokens), dim=1)
|
| 91 |
+
x = x + self.pos_embed[:, :x.size(1), :]
|
| 92 |
+
|
| 93 |
+
for block in self.blocks:
|
| 94 |
+
x = block(x)
|
| 95 |
+
|
| 96 |
+
cls_final = x[:, 0]
|
| 97 |
+
return self.head(cls_final)
|
requirements.txt
CHANGED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
opencv-python
|
| 4 |
+
mtcnn
|
| 5 |
+
Pillow
|