| import torch.nn as nn | |
| class ScriptClassifier(nn.Module): | |
| """Lightweight Bengali/English script classifier. 23K params, ~0.1MB.""" | |
| def __init__(self): | |
| super().__init__() | |
| self.features = nn.Sequential( | |
| nn.Conv2d(1,16,3,padding=1), nn.BatchNorm2d(16), nn.ReLU(True), | |
| nn.MaxPool2d(2), | |
| nn.Conv2d(16,32,3,padding=1), nn.BatchNorm2d(32), nn.ReLU(True), | |
| nn.MaxPool2d(2), | |
| nn.Conv2d(32,64,3,padding=1), nn.BatchNorm2d(64), nn.ReLU(True), | |
| nn.MaxPool2d(2), | |
| nn.Conv2d(64,64,3,padding=1), nn.BatchNorm2d(64), nn.ReLU(True), | |
| nn.AdaptiveAvgPool2d((1,1)), | |
| ) | |
| self.classifier = nn.Sequential( | |
| nn.Flatten(), nn.Dropout(0.3), nn.Linear(64,2) | |
| ) | |
| def forward(self, x): | |
| return self.classifier(self.features(x)) | |
| def predict(self, x): | |
| """x: (1,1,64,256) tensor. Returns 'bengali' or 'english'.""" | |
| with __import__('torch').no_grad(): | |
| return ['bengali','english'][self.forward(x).argmax(1).item()] | |