Kaggle competition: Bengali.AI Handwritten Grapheme Classification
<SOS> metric: WeightedCategorizationAccuracy Kaggle data source: bengali.ai-handwritten-grapheme-classification<categorify>
pt_models = ".. /input/pretrained-models/pretrained-models.pytorch-master/"
sys.path.insert(0,pt_models)
class_names = ['Fish', 'Flower', 'Sugar', 'Gravel']
def rle_decode(mask_rle: str = '', shape =(1400, 2100)) :

 s = mask_rle.split()
starts, lengths = [np.asarray(x, dtype=int)for x in(s[0:][::2], s[1:][::2])]
starts -= 1
ends = starts + lengths
img = np.zeros(shape[0] * shape[1], dtype=np.uint8)
 for lo, hi in zip(starts, ends):
img[lo:hi] = 1
return img.reshape(shape, order='F')
 def mask2rle(img):

 pixels= img.T.flatten()
pixels = np.concatenate([[0], pixels, [0]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs)
 def make_mask(df, image_label, shape =(1400, 2100), cv_shape =(525, 350),debug=False):

 if debug:
print(shape,cv_shape)
 df = df.set_index('Image_Label')
 encoded_mask = df.loc[image_label, 'EncodedPixels']
mask = np.zeros(( shape[0], shape[1]), dtype=np.float32)
 if encoded_mask is not np.nan:
mask = rle_decode(encoded_mask,shape=shape)
 return cv2.resize(mask, cv_shape)
 min_size = [10000 ,10000, 10000, 10000]
def post_process_minsize(mask, min_size):

 num_component, component = cv2.connectedComponents(mask.astype(np.uint8))
predictions = np.zeros(mask.shape)
 num = 0
for c in range(1, num_component):
p =(component == c)
 if p.sum() > min_size:
predictions[p] = 1
num += 1
return predictions<define_variables>
TEST_BATCH_SIZE = 32
MODEL_MEAN=(0.485,0.465,0.406)
 MODEL_STD=(0.229,0.224,0.225)
 IMG_HEIGHT=137
IMG_WIDTH=236
DEVICE="cuda"
mode='convex'
test_imgs_folder = '.. /input/understanding-clouds-resized/test_images_525/test_images_525'
NN=5
folder_images=test_imgs_folder
images_list = ['e0cacb5.jpg', '05fcce5.jpg', 'e949e4b.jpg', 'ce8df6b.jpg', '41be432.jpg']
current_batch = images_list[0: NN]
print(current_batch)
 for i, image_name in enumerate(current_batch):
path = os.path.join(folder_images, image_name)
 print(path)
 img = cv2.imread(path ).astype(np.float32)
 print(img.max() , img.min())
 img = img/255.
 img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 img2 = np.stack([img2,img2,img2],axis=-1)
 img_mask=[]
img_mask_convex=[]
img_mask_convex_minsize=[]
for class_id in range(4):
img_mask.append(make_mask(sub, image_name + '_' + class_names[class_id],shape=(350,525)))
 print(img_mask[class_id].min() , img_mask[class_id].max())
 img_mask_convex.append(draw_convex_hull(img_mask[class_id].astype(np.uint8), mode=mode))
img_mask_convex[class_id][img2[:,:,0]<=2/255.] = 0
img_mask_convex_minsize.append(post_process_minsize(img_mask_convex[class_id], min_size[class_id]))
img3 = draw_masks(img2,img_mask)
 img4 = draw_masks(img2,img_mask_convex_minsize)
 show_Nimages([img2,img3,img4])
 show_Nimages(img_mask)
 show_Nimages(img_mask_convex_minsize )<define_variables>
class ResNet34(nn.Module):
def __init__(self ,pretrained):
super(ResNet34,self ).__init__()
if pretrained is True:
self.model = pretrainedmodels.__dict__["resnet34"](pretrained="imagenet")
 else:
self.model = pretrainedmodels.__dict__["resnet34"](pretrained=None)
 self.l0 = nn.Linear(512,168)
 self.l1 = nn.Linear(512,11)
 self.l2 = nn.Linear(512,7)
 def forward(self,x):
batch_size ,_,_,_ = x.shape
x = self.model.features(x)
 x = F.adaptive_avg_pool2d(x,1 ).reshape(batch_size, -1)
 l0 = self.l0(x)
 l1 = self.l1(x)
 l2 = self.l2(x)
 return l0, l1, l2
mode='approx'
test_imgs_folder = '.. /input/understanding-clouds-resized/test_images_525/test_images_525'
NN=5
folder_images=test_imgs_folder
images_list = ['e0cacb5.jpg', '05fcce5.jpg', 'e949e4b.jpg', 'ce8df6b.jpg', '41be432.jpg']
current_batch = images_list[0: NN]
print(current_batch)
 for i, image_name in enumerate(current_batch):
path = os.path.join(folder_images, image_name)
 print(path)
 img = cv2.imread(path ).astype(np.float32)
 print(img.max() , img.min())
 img = img/255.
 img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 img2 = np.stack([img2,img2,img2],axis=-1)
 img_mask=[]
img_mask_convex=[]
img_mask_convex_minsize=[]
for class_id in range(4):
img_mask.append(make_mask(sub, image_name + '_' + class_names[class_id],shape=(350,525)))
 print(img_mask[class_id].min() , img_mask[class_id].max())
 img_mask_convex.append(draw_convex_hull(img_mask[class_id].astype(np.uint8), mode=mode))
img_mask_convex[class_id][img2[:,:,0]<=2/255.] = 0
img_mask_convex_minsize.append(post_process_minsize(img_mask_convex[class_id], min_size[class_id]))
img3 = draw_masks(img2,img_mask)
 img4 = draw_masks(img2,img_mask_convex_minsize)
 show_Nimages([img2,img3,img4])
 show_Nimages(img_mask)
 show_Nimages(img_mask_convex_minsize )<define_variables>
class BengaliDatasetTest:
def __init__(self, df, img_height, img_width, mean, std):
self.image_ids = df.image_id.values
self.img_arr = df.iloc[:, 1:].values
self.aug = albumentations.Compose([
albumentations.Resize(img_height, img_width, always_apply=True),
albumentations.Normalize(mean, std, always_apply=True)
 ])
 def __len__(self):
return len(self.image_ids)
 def __getitem__(self, item):
image = self.img_arr[item, :]
img_id = self.image_ids[item]
image = image.reshape(137, 236 ).astype(float)
 image = Image.fromarray(image ).convert("RGB")
 image = self.aug(image=np.array(image)) ["image"]
image = np.transpose(image,(2, 0, 1)).astype(np.float32)
 return {
"image": torch.tensor(image, dtype=torch.float),
"image_id": img_id
}
model_class_names=['Fish', 'Flower', 'Gravel', 'Sugar']<load_from_csv>
def model_predict() :
g_pred, v_pred, c_pred = [], [], []
img_ids_list = []
for file_idx in range(4):
df = pd.read_parquet(f".. /input/bengaliai-cv19/test_image_data_{file_idx}.parquet")
 dataset = BengaliDatasetTest(df=df,
img_height=IMG_HEIGHT,
img_width=IMG_WIDTH,
mean=MODEL_MEAN,
std=MODEL_STD)
 data_loader = torch.utils.data.DataLoader(
 dataset=dataset,
batch_size= TEST_BATCH_SIZE,
shuffle=False,
num_workers=4 
)
 for bi, d in enumerate(data_loader):
image = d["image"]
img_id = d["image_id"]
image = image.to(DEVICE, dtype=torch.float)
 g, v, c = model(image)
 for ii, imid in enumerate(img_id):
g_pred.append(g[ii].cpu().detach().numpy())
 v_pred.append(v[ii].cpu().detach().numpy())
 c_pred.append(c[ii].cpu().detach().numpy())
 img_ids_list.append(imid)
 return g_pred, v_pred, c_pred, img_ids_list
sub_test=pd.read_csv('./sub_convex.csv')
 sub_test.head(10 )<install_modules>
model = ResNet34(pretrained=False)
 TEST_BATCH_SIZE = 32
Start_fold=3
final_g_pred = []
final_v_pred = []
final_c_pred = []
final_img_ids = []
for i in range(Start_fold,4):
model.load_state_dict(torch.load(f".. /input/resnet3441epochs/resnet34_fold4(1 ).bin"))
model.to(DEVICE)
 model.eval()
g_pred, v_pred, c_pred, img_ids_list = model_predict()
final_g_pred.append(g_pred)
 final_v_pred.append(v_pred)
 final_c_pred.append(c_pred)
 if i == Start_fold:
final_img_ids.extend(img_ids_list )
!pip install -U keras-applications<import_modules>
final_g = np.argmax(np.mean(np.array(final_g_pred), axis=0), axis=1)
 final_v = np.argmax(np.mean(np.array(final_v_pred), axis=0), axis=1)
 final_c = np.argmax(np.mean(np.array(final_c_pred), axis=0), axis=1)
 predictions = []
for ii, imid in enumerate(final_img_ids):
predictions.append(( f"{imid}_grapheme_root", final_g[ii]))
predictions.append(( f"{imid}_vowel_diacritic", final_v[ii]))
predictions.append(( f"{imid}_consonant_diacritic", final_c[ii]))
import keras_applications
import keras
import tensorflow as tf<import_modules>
sub = pd.DataFrame(predictions,columns=["row_id","target"])
 print(sub )
<import_modules><EOS> 
sub.to_csv("submission.csv",index=False )