Kaggle competition: Bengali.AI Handwritten Grapheme Classification
<SOS> metric: WeightedCategorizationAccuracy Kaggle data source: bengali.ai-handwritten-grapheme-classification<merge>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
warnings.filterwarnings("ignore" )
merged_df = df_for_plot.join(df_for_pca, on='word_id',lsuffix='_l', rsuffix='_r')
 df_cleaned = merged_df[merged_df['word_x'] < 0.6 ]
df_cleaned.columns<normalization>
HEIGHT = 137
WIDTH = 236
SIZE = 128
bs = 128
stats =(0.0692, 0.2051)
 arch = models.resnet18
MODEL = '/kaggle/input/grapheme-fast-ai-starter-using-resnet18/resnet18_model_fold_0.pth'
nworkers = 2
TEST = ['/kaggle/input/bengaliai-cv19/test_image_data_0.parquet',
'/kaggle/input/bengaliai-cv19/test_image_data_1.parquet',
'/kaggle/input/bengaliai-cv19/test_image_data_2.parquet',
'/kaggle/input/bengaliai-cv19/test_image_data_3.parquet']
LABELS = '.. /input/bengaliai-cv19/train.csv'
df = pd.read_csv(LABELS)
 nunique = list(df.nunique())[1:-1]
stats.normaltest(train['word_count'] )<normalization>
class Head(nn.Module):
def __init__(self, nc, n, ps=0.5):
super().__init__()
layers = [AdaptiveConcatPool2d() , Mish() , Flatten() ] + \
bn_drop_lin(nc*2, 512, True, ps, Mish())+ \
bn_drop_lin(512, n, True, ps)
 self.fc = nn.Sequential(*layers)
 self._init_weight()
def _init_weight(self):
for m in self.modules() :
if isinstance(m, nn.Conv2d):
torch.nn.init.kaiming_normal_(m.weight)
 elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1.0)
 m.bias.data.zero_()
def forward(self, x):
return self.fc(x)
 class Rnet_1ch(nn.Module):
def __init__(self, arch=arch, n=nunique, pre=True, ps=0.5):
super().__init__()
m = arch(True)if pre else arch()
conv = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)
 w =(m.conv1.weight.sum(1)).unsqueeze(1)
 conv.weight = nn.Parameter(w)
 self.layer0 = nn.Sequential(conv, m.bn1, nn.ReLU(inplace=True))
self.layer1 = nn.Sequential(
 nn.MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False),
m.layer1)
 self.layer2 = nn.Sequential(m.layer2)
 self.layer3 = nn.Sequential(m.layer3)
 self.layer4 = nn.Sequential(m.layer4)
 self.avgpool = nn.AdaptiveAvgPool2d(( 1,1))
nc = 512
self.head1 = Head(nc,n[0])
 self.head2 = Head(nc,n[1])
 self.head3 = Head(nc,n[2])
 to_Mish(self.layer0), to_Mish(self.layer1), to_Mish(self.layer2)
 to_Mish(self.layer3), to_Mish(self.layer4)
 def forward(self, x):
x = self.layer0(x)
 x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x)
 x = self.layer4(x)
 x = self.avgpool(x)
 x1 = self.head1(x)
 x2 = self.head2(x)
 x3 = self.head3(x)
 return x1,x2,x3
stats.normaltest(train['stopword_count'] )<statistical_test>
model = Rnet_1ch(pre=False ).cuda()
model.load_state_dict(torch.load(MODEL, map_location=torch.device('cpu')));
model.eval() ;
<compute_test_metric><EOS> 
class GraphemeDataset(Dataset):
def __init__(self, fname):
self.df = pd.read_parquet(fname)
 self.data = 255 - self.df.iloc[:, 1:].values.reshape(-1, HEIGHT, WIDTH ).astype(np.uint8)
 def __len__(self):
return len(self.data)
 def __getitem__(self, idx):
name = self.df.iloc[idx,0]
img =(self.data[idx]*(255.0/self.data[idx].max())).astype(np.uint8)
 img = crop_resize(img)
 img =(img.astype(np.float32)/255.0 - stats[0])/stats[1]
return img, name