File size: 1,071 Bytes
3bbb1c3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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()]
|