Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<define_search_space>
import os
import shutil
import numpy as np
import pandas as pd
from PIL import Image
from cv2 import imread, resize, IMREAD_GRAYSCALE
from tensorflow.keras.utils import Sequence
from tensorflow import convert_to_tensor as to_T
from tensorflow.keras.models import load_model
learner.unfreeze()
lrs=np.array([lr/10,lr/3,lr] )<train_model>
def bbox(image):

 HEIGHT = image.shape[0]
WIDTH = image.shape[1]
for i in range(image.shape[1]):
if(image[:, i] > 0 ).sum() >= 1:
x_min = i - 1 if(i > 1)else 0
break
for i in reversed(range(image.shape[1])) :
if(image[:, i] > 0 ).sum() >= 1:
x_max = i + 2 if(i < WIDTH - 2)else WIDTH
break
for i in range(image.shape[0]):
if(image[i] > 0 ).sum() >= 1:
y_min = i - 1 if(i > 1)else 0
break
for i in reversed(range(image.shape[0])) :
if(image[i] > 0 ).sum() >= 1:
y_max = i + 2 if(i < HEIGHT - 2)else HEIGHT
break
return x_min, y_min, x_max, y_max
return x_min, y_min, x_max, y_max
with warnings.catch_warnings() :
warnings.simplefilter("ignore")
 learner.fit(lrs/4,4,cycle_len=2,use_clr=(10,20),callbacks=[f1_callback] )<train_model>
class Dataset(Sequence):
def __init__(self
, image_list
, batch_size
, dimensions 
):

 self.image_list = image_list
self.batch_size = batch_size
self.dimensions = dimensions
self.on_epoch_end()
def __len__(self):

 return int(np.ceil(len(self.image_list)/ self.batch_size))
def __getitem__(self, index):

 indices = self.indices[index * self.batch_size:(index + 1)* self.batch_size]
images = [self.image_list[k] for k in indices]
X = self.__data_generation(images)
 return X
def __data_generation(self, images):

 X = np.empty(( len(images), self.dimensions["height"], self.dimensions["width"], self.dimensions["channels"]))
for idx, image in enumerate(images):
image = imread(image, IMREAD_GRAYSCALE)
 image = resize(image
,(self.dimensions["width"], self.dimensions["height"])
)
 image = np.expand_dims(image, axis=2)
 X[idx,] = image
X = X / 255.0
return to_T(X)
 def on_epoch_end(self):

 self.indices = np.arange(len(self.image_list))
with warnings.catch_warnings() :
warnings.simplefilter("ignore")
 learner.fit(lrs/16,2,cycle_len=4,use_clr=(10,20),callbacks=[f1_callback] )<train_model>
df = pd.read_csv(".. /input/Kannada-MNIST/test.csv")
 ids = []
image_list = []
os.makedirs("images")
 for i,row in df.iterrows() :
id = "{}".format(i)
 img = np.reshape(row[1:].values, newshape=(28,28)).astype(np.uint8)
 x_min, y_min, x_max, y_max = bbox(img)
 img = img[y_min:y_max, x_min:x_max]
x_min, y_min, x_max, y_max = bbox(img)
 img = img[y_min:y_max, x_min:x_max]
img = Image.fromarray(img)
 img = img.resize(( 28,28), Image.ANTIALIAS)
 img.save("images/{}.png".format(id))
ids.append(id)
 image_list.append("images/{}.png".format(id))
test_df = pd.DataFrame({"id": ids, "label": 0} )
with warnings.catch_warnings() :
warnings.simplefilter("ignore")
 learner.fit(lrs/32,1,cycle_len=8,use_clr=(10,20),callbacks=[f1_callback] )<save_model>
ds = Dataset(image_list=image_list
, batch_size=512
, dimensions={"height":28, "width":28, "channels":1}
)
learner.save('ResNet34_256_1' )<feature_engineering>
net = load_model(".. /input/kannada-mnist-tensorflow-2/CustomNetV2.hdf5" )
md = get_data(sz,bs,is_test=True)
 learner.set_data(md)
 preds,y = learner.TTA(n_aug=8)
 preds = np.stack(preds, axis=-1)
 pred = preds.mean(axis=-1 )<train_model>
predictions = net.predict(ds ).argmax(-1 )
def sigmoid_np(x):
return 1.0/(1.0 + np.exp(-x))
def F1_soft(preds,targs,th=0.0,d=25.0):
preds = sigmoid_np(d*(preds - th))
targs = targs.astype(np.float)
 score = 2.0*(preds*targs ).sum(axis=0)/(( preds+targs ).sum(axis=0)+ 1e-6)
 return score
def fit_val(x,y):
params = np.zeros(len(name_label_dict))
wd = 1e-5
error = lambda p: np.concatenate(( F1_soft(x,y,p)- 1.0,
wd*p), axis=None)
 p, success = opt.leastsq(error, params)
 return p<compute_test_metric>
test_df["label"] = predictions
th = fit_val(pred,y)
 print('Thresholds: ',th)
 print('F1 macro: ',f1_score(y, pred>th, average='macro'))
print('F1 macro(th = 0.0): ',f1_score(y, pred>0.0, average='macro'))
print('F1 micro: ',f1_score(y, pred>th, average='micro'))<split>
test_df.to_csv("submission.csv",index=False )
th, score, cv = 0,0,10
for i in range(cv):
xt,xv,yt,yv = train_test_split(pred,y,test_size=0.5,random_state=i)
 th_i = fit_val(xt,yt)
 th += th_i
score += f1_score(yv, xv>th_i, average='macro')
 th/=cv
score/=cv
print('Thresholds: ',th)
 print('F1 macro avr:',score)
 print('F1 macro: ',f1_score(y, pred>th, average='macro'))
print('F1 micro: ',f1_score(y, pred>th, average='micro'))<feature_engineering>
shutil.rmtree("images" )