Kaggle competition: Bengali.AI Handwritten Grapheme Classification
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt<split>
!pip install /kaggle/input/pretrained/pretrainedmodels-0.7.4/pretrainedmodels-0.7.4/
def kfold_splits(n_splits, train_df):

 all_folds = list(range(0, n_splits))
kf_splits = []
for fold in range(0, n_splits):
train_folds = [x for x in all_folds if x != fold]
trn_idx = train_df[train_df.kfold!=fold].index
val_idx = train_df[train_df.kfold==fold].index
kf_splits.append(( fold, trn_idx, val_idx))
return kf_splits<init_hyperparams>
if not os.path.exists('/tmp/.cache/torch/checkpoints/'):
os.makedirs('/tmp/.cache/torch/checkpoints/')
 shutil.copy('/kaggle/input/pretrained/densenet121-fbdb23505.pth','/tmp/.cache/torch/checkpoints/' )
params = {'objective': 'regression',
'metric': 'rmse',
'verbosity': -1,
'boosting_type': 'gbdt',
'feature_pre_filter': False,
'learning_rate': 0.0035,
'lambda_l1': 18.42,
'lambda_l2': 4.02,
'num_leaves': 128,
'min_data_in_leaf': 81,
'sub_feature': 0.5,
'sub_row': 0.8,
'subsample_freq': 10}<train_model>
model_name = 'densenet121'
channels = 1024
loss_weight1 = 0.8
loss_weight2 = 0.1
loss_weight3 = 0.1
y = train_df['target']
oof = np.zeros(len(train_df))
predictions = np.zeros(len(test_df))
random_state = 42
num_round = 15000
for fold, trn_idx, val_idx in kfold_splits(n_splits, train_df):
print(f"fold: {fold}, train len: {len(trn_idx)}, val len: {len(val_idx)}")
 trn_data = lgb.Dataset(train_df.iloc[trn_idx][features], label=y.iloc[trn_idx])
 val_data = lgb.Dataset(train_df.iloc[val_idx][features], label=y.iloc[val_idx])
 clf = lgb.train(params, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=1000, early_stopping_rounds = 500)
 oof[val_idx] = clf.predict(train_df.iloc[val_idx][features], num_iteration=clf.best_iteration)
 predictions += clf.predict(test_df[features], num_iteration=clf.best_iteration)/ n_splits
print(f'CV score: {np.round(mean_squared_error(y, oof, squared=False),5)}' )<save_to_csv>
IMAGE_RGB_MEAN = [0.485, 0.456, 0.406]
IMAGE_RGB_STD = [0.229, 0.224, 0.225]
class LinearBlock(nn.Module):
def __init__(self, in_features, out_features, bias=True,
use_bn=True, activation=F.relu, dropout_ratio=-1, residual=False,):
super(LinearBlock, self ).__init__()
if in_features is None:
self.linear = LazyLinear(in_features, out_features, bias=bias)
 else:
self.linear = nn.Linear(in_features, out_features, bias=bias)
 if use_bn:
self.bn = nn.BatchNorm1d(out_features)
 if dropout_ratio > 0.:
self.dropout = nn.Dropout(p=dropout_ratio)
 else:
self.dropout = None
self.activation = activation
self.use_bn = use_bn
self.dropout_ratio = dropout_ratio
self.residual = residual
def __call__(self, x):
h = self.linear(x)
 if self.use_bn:
h = self.bn(h)
 if self.activation is not None:
h = self.activation(h)
 if self.residual:
h = residual_add(h, x)
 if self.dropout_ratio > 0:
h = self.dropout(h)
 return h
class RGB(nn.Module):
def __init__(self,):
super(RGB, self ).__init__()
self.register_buffer('mean', torch.zeros(1,3,1,1))
self.register_buffer('std', torch.ones(1,3,1,1))
self.mean.data = torch.FloatTensor(IMAGE_RGB_MEAN ).view(self.mean.shape)
 self.std.data = torch.FloatTensor(IMAGE_RGB_STD ).view(self.std.shape)
 def forward(self, x):
print(self.mean,self.std)
 x =(x-self.mean)/self.std
return x
class classifier(nn.Module):
def __init__(self,n_classes=[168,11,7],use_bn=True):
super(classifier, self ).__init__()
self.base_model = pretrainedmodels.__dict__[model_name](pretrained='imagenet')
 self.backbone = nn.Sequential(*list(self.base_model.children())[:-1])
 inch = channels
self.lin1 = LinearBlock(inch, 512, use_bn=use_bn, activation=F.leaky_relu, dropout_ratio = 0.5, residual=False)
 self.head1 = LinearBlock(512, 168, use_bn=False, activation=None, residual=False)
 self.head2 = LinearBlock(512, 11, use_bn=False, activation=None, residual=False)
 self.head3 = LinearBlock(512, 7, use_bn=False, activation=None, residual=False)
 def forward(self,x):
x_ = x
feat = self.backbone(x_)
 h = torch.mean(feat, dim=(-1,-2))
h = self.lin1(h)
 x1 = self.head1(h)
 x2 = self.head2(h)
 x3 = self.head3(h)
 return x1,x2,x3
submission = pd.DataFrame({"id":test_df.id, "target":predictions})
 submission.to_csv('submission.csv', index=False )<import_modules>
model = classifier().cuda()
if os.path.exists('/kaggle/input/densenet/densenet121_86.pth'):
model.load_state_dict(torch.load('/kaggle/input/densenet/densenet121_86.pth'))
print("load model success")
 else:
print('load model fail')
 model.eval()
import lightgbm as lgb
import optuna.integration.lightgbm as oplgb
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
from tqdm.notebook import tqdm
import matplotlib.pyplot as plt
import category_encoders as ce
import seaborn as sns<load_from_csv>
H=137
W=236
class GraphemeDataset(Dataset):
def __init__(self, fname):
print(fname)
 self.df = pd.read_parquet(fname)
 self.data = 255 - self.df.iloc[:, 1:].values.reshape(-1, H, W ).astype(np.uint8)
 def __len__(self):
return len(self.data)
 def __getitem__(self, idx):
name = self.df.iloc[idx,0]
img = np.reshape(self.data[idx],(H,W))
img = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
 img = img.astype(np.float32)/255.0
img = np.transpose(img,(2,0,1))
mean = np.array([0.485, 0.456, 0.406] ).reshape(3,1,1)
 std = np.array([0.229, 0.224, 0.225] ).reshape(3,1,1)
 img =(img-mean)/std
img = torch.FloatTensor(img)
 return img, name
test_data = ['/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']
<drop_column><EOS> 
row_id,target = [],[]
for fname in test_data:
test_image = GraphemeDataset(fname)
 dl = torch.utils.data.DataLoader(test_image,batch_size=128,num_workers=12,shuffle=False)
 with torch.no_grad() :
for x, y in tqdm(dl):
x = x.float().cuda()
p1,p2,p3 = model(x)
 p1 = p1.argmax(-1 ).view(-1 ).cpu()
p2 = p2.argmax(-1 ).view(-1 ).cpu()
p3 = p3.argmax(-1 ).view(-1 ).cpu()
for idx,name in enumerate(y):
row_id += [f'{name}_grapheme_root',f'{name}_vowel_diacritic',
f'{name}_consonant_diacritic']
target += [p1[idx].item() ,p2[idx].item() ,p3[idx].item() ]
sub_df = pd.DataFrame({'row_id': row_id, 'target': target})
 sub_df.to_csv('submission.csv', index=False)
 sub_df.head(20 )