Upload model.py
Browse files
model.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch.nn as nn
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
# -----------------------------
|
| 5 |
+
# 3️⃣ Model definition
|
| 6 |
+
# -----------------------------
|
| 7 |
+
def GN(c, groups=16): return nn.GroupNorm(min(groups, c), c)
|
| 8 |
+
|
| 9 |
+
class LightResNetCNN(nn.Module):
|
| 10 |
+
def __init__(self, in_channels=1, adaptive_height=8):
|
| 11 |
+
super().__init__()
|
| 12 |
+
self.adaptive_height = adaptive_height
|
| 13 |
+
self.layer1 = nn.Sequential(nn.Conv2d(in_channels, 32, 3, 1, 1), GN(32), nn.ReLU(), nn.MaxPool2d(2, 2))
|
| 14 |
+
self.layer2 = nn.Sequential(nn.Conv2d(32, 64, 3, 1, 1), GN(64), nn.ReLU(), nn.MaxPool2d(2, 2))
|
| 15 |
+
self.layer3 = nn.Sequential(nn.Conv2d(64, 128, 3, 1, 1), GN(128), nn.ReLU(), nn.MaxPool2d(2, 2))
|
| 16 |
+
self.layer4 = nn.Sequential(nn.Conv2d(128, 256, 3, 1, 1), GN(256), nn.ReLU())
|
| 17 |
+
self.layer5 = nn.Sequential(nn.Conv2d(256, 256, 3, 1, 1), GN(256), nn.ReLU())
|
| 18 |
+
self.layer6 = nn.Sequential(nn.Conv2d(256, 128, 3, 1, 1), GN(128), nn.ReLU())
|
| 19 |
+
self.adaptive_pool = nn.AdaptiveAvgPool2d((self.adaptive_height, None))
|
| 20 |
+
def forward(self, x):
|
| 21 |
+
for i in range(1, 7):
|
| 22 |
+
x = getattr(self, f"layer{i}")(x)
|
| 23 |
+
x = self.adaptive_pool(x)
|
| 24 |
+
return x
|
| 25 |
+
|
| 26 |
+
class PositionalEncoding(nn.Module):
|
| 27 |
+
def __init__(self, d_model, max_len=2000):
|
| 28 |
+
super().__init__()
|
| 29 |
+
pe = torch.zeros(max_len, d_model)
|
| 30 |
+
position = torch.arange(0, max_len).unsqueeze(1)
|
| 31 |
+
div_term = torch.exp(torch.arange(0, d_model, 2) * (-torch.log(torch.tensor(10000.0)) / d_model))
|
| 32 |
+
pe[:, 0::2] = torch.sin(position * div_term)
|
| 33 |
+
pe[:, 1::2] = torch.cos(position * div_term)
|
| 34 |
+
self.register_buffer("pe", pe.unsqueeze(0))
|
| 35 |
+
def forward(self, x):
|
| 36 |
+
return x + self.pe[:, :x.size(1), :]
|
| 37 |
+
|
| 38 |
+
class CNN_Transformer_OCR(nn.Module):
|
| 39 |
+
def __init__(self, num_classes, d_model=1280, nhead=16, num_layers=8, dropout=0.2):
|
| 40 |
+
super().__init__()
|
| 41 |
+
self.cnn = LightResNetCNN(in_channels=1, adaptive_height=8)
|
| 42 |
+
self.proj = nn.Linear(128 * 8, d_model)
|
| 43 |
+
self.posenc = PositionalEncoding(d_model)
|
| 44 |
+
encoder_layer = nn.TransformerEncoderLayer(d_model, nhead, batch_first=True, dropout=dropout)
|
| 45 |
+
self.transformer = nn.TransformerEncoder(encoder_layer, num_layers=num_layers)
|
| 46 |
+
self.fc = nn.Linear(d_model, num_classes)
|
| 47 |
+
def forward(self, x):
|
| 48 |
+
f = self.cnn(x)
|
| 49 |
+
B, C, H, W = f.size()
|
| 50 |
+
f = f.permute(0, 3, 1, 2).reshape(B, W, C * H)
|
| 51 |
+
f = self.posenc(self.proj(f))
|
| 52 |
+
out = self.transformer(f)
|
| 53 |
+
out = self.fc(out)
|
| 54 |
+
return out.log_softmax(2)
|