Sarjinkhan2003 commited on
Commit
34450ee
·
verified ·
1 Parent(s): d09b86e

Script classifier architecture conf_threshold=0.70

Browse files
Files changed (1) hide show
  1. 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
- """Lightweight Bengali/English script classifier. 23K params, ~0.1MB."""
 
 
 
 
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(16,32,3,padding=1), nn.BatchNorm2d(32), nn.ReLU(True),
12
- nn.MaxPool2d(2),
13
- nn.Conv2d(32,64,3,padding=1), nn.BatchNorm2d(64), nn.ReLU(True),
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
- """x: (1,1,64,256) tensor. Returns 'bengali' or 'english'."""
25
- with __import__('torch').no_grad():
26
- return ['bengali','english'][self.forward(x).argmax(1).item()]
 
 
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()