Script classifier architecture conf_threshold=0.70
Browse files- classifier/shobdo_classifier.py +16 -14
classifier/shobdo_classifier.py
CHANGED
|
@@ -1,26 +1,28 @@
|
|
| 1 |
-
|
| 2 |
import torch.nn as nn
|
| 3 |
|
| 4 |
class ScriptClassifier(nn.Module):
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def __init__(self):
|
| 7 |
super().__init__()
|
| 8 |
self.features = nn.Sequential(
|
| 9 |
-
nn.Conv2d(1,16,3,padding=1), nn.BatchNorm2d(16), nn.ReLU(True),
|
| 10 |
-
nn.MaxPool2d(2),
|
| 11 |
-
nn.Conv2d(
|
| 12 |
-
nn.
|
| 13 |
-
nn.
|
| 14 |
-
nn.MaxPool2d(2),
|
| 15 |
-
nn.Conv2d(64,64,3,padding=1), nn.BatchNorm2d(64), nn.ReLU(True),
|
| 16 |
-
nn.AdaptiveAvgPool2d((1,1)),
|
| 17 |
)
|
| 18 |
self.classifier = nn.Sequential(
|
| 19 |
-
nn.Flatten(), nn.Dropout(0.3), nn.Linear(64,2)
|
| 20 |
)
|
| 21 |
def forward(self, x):
|
| 22 |
return self.classifier(self.features(x))
|
| 23 |
def predict(self, x):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
|
| 4 |
class ScriptClassifier(nn.Module):
|
| 5 |
+
# ShobdoOCR Script Classifier
|
| 6 |
+
# 23K parameters, <0.3MB
|
| 7 |
+
# Input: (B, 1, 64, 256) normalized grayscale
|
| 8 |
+
# Output: (B, 2) logits [0=Bengali, 1=English]
|
| 9 |
+
# Routing threshold: conf >= 0.70
|
| 10 |
def __init__(self):
|
| 11 |
super().__init__()
|
| 12 |
self.features = nn.Sequential(
|
| 13 |
+
nn.Conv2d(1, 16, 3, padding=1), nn.BatchNorm2d(16), nn.ReLU(True), nn.MaxPool2d(2),
|
| 14 |
+
nn.Conv2d(16, 32, 3, padding=1), nn.BatchNorm2d(32), nn.ReLU(True), nn.MaxPool2d(2),
|
| 15 |
+
nn.Conv2d(32, 64, 3, padding=1), nn.BatchNorm2d(64), nn.ReLU(True), nn.MaxPool2d(2),
|
| 16 |
+
nn.Conv2d(64, 64, 3, padding=1), nn.BatchNorm2d(64), nn.ReLU(True),
|
| 17 |
+
nn.AdaptiveAvgPool2d((1, 1)),
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
self.classifier = nn.Sequential(
|
| 20 |
+
nn.Flatten(), nn.Dropout(0.3), nn.Linear(64, 2)
|
| 21 |
)
|
| 22 |
def forward(self, x):
|
| 23 |
return self.classifier(self.features(x))
|
| 24 |
def predict(self, x):
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
probs = torch.softmax(self.forward(x), dim=1)
|
| 27 |
+
conf, pred = probs.max(dim=1)
|
| 28 |
+
return ['bengali', 'english'][pred.item()], conf.item()
|