Kaggle competition: Bengali.AI Handwritten Grapheme Classification
<SOS> metric: WeightedCategorizationAccuracy Kaggle data source: bengali.ai-handwritten-grapheme-classification<choose_model_class>
IMG_SIZE=86
N_CHANNELS=1
def resize(df, size=86, need_progress_bar=True):
resized = {}
resize_size=86
if need_progress_bar:
for i in tqdm(range(df.shape[0])) :
image=df.loc[df.index[i]].values.reshape(137,236)
 _, thresh = cv2.threshold(image, 30, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
 contours, _ = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)[-2:]
idx = 0
ls_xmin = []
ls_ymin = []
ls_xmax = []
ls_ymax = []
for cnt in contours:
idx += 1
x,y,w,h = cv2.boundingRect(cnt)
 ls_xmin.append(x)
 ls_ymin.append(y)
 ls_xmax.append(x + w)
 ls_ymax.append(y + h)
 xmin = min(ls_xmin)
 ymin = min(ls_ymin)
 xmax = max(ls_xmax)
 ymax = max(ls_ymax)
 roi = image[ymin:ymax,xmin:xmax]
resized_roi = cv2.resize(roi,(resize_size, resize_size),interpolation=cv2.INTER_AREA)
 resized[df.index[i]] = resized_roi.reshape(-1)
 else:
for i in range(df.shape[0]):
image=df.loc[df.index[i]].values.reshape(137,236)
 _, thresh = cv2.threshold(image, 30, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
 contours, _ = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)[-2:]
idx = 0
ls_xmin = []
ls_ymin = []
ls_xmax = []
ls_ymax = []
for cnt in contours:
idx += 1
x,y,w,h = cv2.boundingRect(cnt)
 ls_xmin.append(x)
 ls_ymin.append(y)
 ls_xmax.append(x + w)
 ls_ymax.append(y + h)
 xmin = min(ls_xmin)
 ymin = min(ls_ymin)
 xmax = max(ls_xmax)
 ymax = max(ls_ymax)
 roi = image[ymin:ymax,xmin:xmax]
resized_roi = cv2.resize(roi,(resize_size, resize_size),interpolation=cv2.INTER_AREA)
 resized[df.index[i]] = resized_roi.reshape(-1)
 resized = pd.DataFrame(resized ).T
return resized
runner = SupervisedRunner(device='cuda', input_key=['stop', 'delta'],)
 loaders = {'train': dataloader_train, 'valid': dataloader_val}
model = ReverseModel()
optimizer = Lookahead(RAdam(params=model.parameters() , lr=1e-3))
criterion = {"bce": nn.BCEWithLogitsLoss() }
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor=0.25, patience=2)
 callbacks = [
CriterionCallback(input_key='start', prefix="loss", criterion_key="bce"),
EarlyStoppingCallback(patience=5),
]
runner.train(
 model=model,
criterion=criterion,
optimizer=optimizer,
scheduler=scheduler,
loaders=loaders,
callbacks=callbacks,
logdir="./logs",
num_epochs=999,
main_metric="loss",
minimize_metric=True,
verbose=True,
)<load_pretrained>
model1 = load_model('.. /input/bengali-alternative/densenet_169_rooty.h5')
 model2 = load_model('.. /input/bengali-alternative/densenet_CNN(3 ).h5')
 model3 = load_model('.. /input/bengali-alternative/densenet_rooty_tooty.h5')
 preds_dict = {
'grapheme_root': [],
'vowel_diacritic': [],
'consonant_diacritic': []
}
components = ['consonant_diacritic', 'grapheme_root', 'vowel_diacritic']
target=[]
row_id=[]
for i in range(4):
df_test_img = pd.read_parquet('/kaggle/input/bengaliai-cv19/test_image_data_{}.parquet'.format(i))
df_test_img.set_index('image_id', inplace=True)
 X_test = resize(df_test_img, need_progress_bar=False)/255
X_test = X_test.values.reshape(-1, IMG_SIZE, IMG_SIZE, N_CHANNELS)
 preds1 = model1.predict(X_test)
 preds2 = model2.predict(X_test)
 preds3 = model3.predict(X_test)
 preds = [np.mean([preds1[0],preds2[0],preds3[0]],axis=0), np.mean([preds1[1],preds2[1],preds3[1]],axis=0), np.mean([preds1[2],preds2[2],preds3[2]],axis=0)]
for i, p in enumerate(preds_dict):
preds_dict[p] = np.argmax(preds[i], axis=1)
 for k,id in enumerate(df_test_img.index.values):
for i,comp in enumerate(components):
id_sample=id+'_'+comp
row_id.append(id_sample)
 target.append(preds_dict[comp][k])
 del df_test_img
del X_test
gc.collect()
df_sample = pd.DataFrame(
 {
'row_id': row_id,
'target':target
},
columns = ['row_id','target'] 
)
 df_sample.to_csv('submission.csv',index=False)
 df_sample.head()