Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<train_model>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from keras.utils.np_utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense,Conv2D,Flatten,MaxPool2D,Dropout,BatchNormalization
from keras.optimizers import RMSprop,Adam
from keras.callbacks import ReduceLROnPlateau
class FocalLoss(nn.Module):
def __init__(self, gamma=2):
super().__init__()
self.gamma = gamma
def forward(self, input, target):
if not(target.size() == input.size()):
raise ValueError("Target size({})must be the same as input size({})" 
.format(target.size() , input.size()))
max_val =(-input ).clamp(min=0)
 loss = input - input * target + max_val + \ 
(( -max_val ).exp() +(-input - max_val ).exp() ).log()
invprobs = F.logsigmoid(-input *(target * 2.0 - 1.0))
loss =(invprobs * self.gamma ).exp() * loss
return loss.sum(dim=1 ).mean()<compute_train_metric>
train=pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 test=pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 sample_sub=pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv' )
def f1_(preds, targs, start=0.17, end=0.24, step=0.01):
with warnings.catch_warnings() :
warnings.simplefilter("ignore")
 return max([fbeta_score(targs,(preds>th), 1, average='samples')
 for th in np.arange(start,end,step)] )<define_search_space>
test.head(3)
 test=test.drop('id',axis=1)
m = Darknet([1, 2, 4, 4, 3], 28 ).cuda()<define_search_space>
X_train=train.drop('label',axis=1)
 Y_train=train.label
md = get_data(512,8 )<choose_model_class>
X_train=X_train/255
test=test/255
learn = Learner.from_model_data(m, md, tmp_name=TMP, models_name=MODEL)
 learn.crit = FocalLoss()
learn.opt_fn = optim.Adam
learn.metrics = [f1_]<load_pretrained>
Y_train=to_categorical(Y_train )
learn.load(PRETRAINED )<define_search_space>
X_train,X_test,y_train,y_test=train_test_split(X_train,Y_train,random_state=42,test_size=0.15 )
lr = 1E-2<train_model>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train)
learn.fit(lr/10,1,cycle_len=1,use_clr_beta=(10,10, 0.85, 0.9))<save_model>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(BatchNormalization(momentum=.15))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(BatchNormalization(momentum=0.15))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(BatchNormalization(momentum=.15))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.4))
model.add(Dense(10, activation = "softmax"))
learn.save('512' )<load_pretrained>
optimizer=Adam(learning_rate=0.001,beta_1=0.9,beta_2=0.999 )
learn.load('512' )<predict_on_test>
model.compile(optimizer=optimizer,loss=['categorical_crossentropy'],metrics=['accuracy'] )
p_v, t_v = learn.predict_with_targs()<compute_test_metric>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
def sigmoid(a):
return 1/(1+np.exp(-a))<compute_test_metric>
epochs=5
batch_size=64
sp_v = sigmoid(p_v )<compute_test_metric>
history = model.fit_generator(datagen.flow(X_train,y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_test,y_test),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction] )
def f1_np(y_pred, y_true, threshold=0.5):

 y_pred =(y_pred>threshold ).astype(int)
 TP =(y_pred*y_true ).sum(1)
 prec = TP/(y_pred.sum(1)+1e-7)
 rec = TP/(y_true.sum(1)+1e-7)
 res = 2*prec*rec/(prec+rec+1e-7)
 return res.mean()
def f1_n(y_pred, y_true, thresh, n, default=0.5):

 threshold = default * np.ones(y_pred.shape[1])
 threshold[n]=thresh
return f1_np(y_pred, y_true, threshold)
 def find_thresh(y_pred, y_true):

 ths = []
for i in range(y_pred.shape[1]):
aux = []
for th in np.linspace(0,1,100):
aux += [f1_n(y_pred, y_true, th, i)]
ths += [np.array(aux ).argmax() /100]
return np.array(ths )<compute_test_metric>
y_pre_test=model.predict(X_test)
 y_pre_test=np.argmax(y_pre_test,axis=1)
 y_test=np.argmax(y_test,axis=1)
ths = find_thresh(sp_v, t_v); ths<compute_test_metric>
conf=confusion_matrix(y_test,y_pre_test)
 conf=pd.DataFrame(conf,index=range(0,10),columns=range(0,10))
f1_np(sp_v, t_v, 0.5), f1_np(sp_v, t_v, ths )<predict_on_test>
x=(y_pre_test-y_test!=0 ).tolist()
x=[i for i,l in enumerate(x)if l!=False]
preds = learn.predict(is_test=True )<define_variables>
test=pd.read_csv('.. /input/Kannada-MNIST/test.csv' )
preds = sigmoid(preds)
 threshold = ths
print(preds.shape)
 classes = np.array([str(n)for n in range(28)])
 res = np.array([" ".join(classes[(np.where(pp>threshold)) ])for pp in preds] )<define_variables>
test_id=test.id
test=test.drop('id',axis=1)
 test=test/255
test=test.values.reshape(-1,28,28,1)
filenames = np.array([os.path.basename(fn ).split('.')[0] for fn in test_names] )<create_dataframe>
y_pre=model.predict(test)
 y_pre=np.argmax(y_pre,axis=1 )
frame = pd.DataFrame(np.array([filenames, res] ).T, columns = ['Id','Predicted'] )<save_to_csv>
sample_sub['label']=y_pre
sample_sub.to_csv('submission.csv',index=False)