Kaggle competition: Kannada MNIST
class AvgStats() :
def __init__(self, metrics, in_train): self.metrics, self.in_train = listify(metrics), in_train
def reset(self):
self.tot_loss, self.count = 0., 0
self.tot_mets = [0.]*len(self.metrics)
 @property
def all_stats(self): return [self.tot_loss.item() ] + self.tot_mets
@property
def avg_stats(self): return [o/self.count for o in self.all_stats]
def __repr__(self):
if not self.count: return ''
return f"{'train' if self.in_train else 'valid'}: {self.avg_stats}"
def accumulate(self, run):
bn = run.xb.shape[0]
self.tot_loss+=run.loss*bn
self.count+=bn
for i, m in enumerate(self.metrics):
self.tot_mets[i]+=m(run.pred, run.yb)*bn<train_model>
from IPython.display import display, Image
import os
import PIL.Image
import cv2
import matplotlib.pyplot as plt
class AvgStatsCallBack(Callback):
def __init__(self, metrics):
self.train_stats, self.valid_stats = AvgStats(metrics, True), AvgStats(metrics, False)
 def begin_epoch(self):
self.train_stats.reset()
self.valid_stats.reset()
def after_loss(self):
stats = self.train_stats if self.in_train else self.valid_stats
with torch.no_grad() : stats.accumulate(self.run)
 def after_epoch(self):
print(self.train_stats)
 print(self.valid_stats )<set_options>
dig_mnist = pd.read_csv("/kaggle/input/Kannada-MNIST/Dig-MNIST.csv")
 print(dig_mnist.shape)
 dig_mnist.head()
%matplotlib inline<import_modules>
test = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv")
 print(test.shape)
 test.head()
from sklearn.metrics import roc_auc_score<compute_test_metric>
train = pd.read_csv("/kaggle/input/Kannada-MNIST/train.csv")
 print(train.shape)
 train.head()
def roc(out, y):
score = roc_auc_score(y.cpu().detach().numpy() , out.cpu().detach().numpy())
 return score<train_model>
ss = pd.read_csv("/kaggle/input/Kannada-MNIST/sample_submission.csv")
 print(ss.shape)
 ss.head()
class ParamScheduler(Callback):
_order = 1
def __init__(self, pname, sched_funcs): self.pname, self.sched_funcs = pname, sched_funcs
def begin_fit(self):
if not isinstance(self.sched_funcs,(list, tuple)) :
self.sched_funcs = [self.sched_funcs]*len(self.opt.param_groups)
 def set_param(self):
assert len(self.opt.param_groups)==len(self.sched_funcs)
 for pg, f in zip(self.opt.param_groups, self.sched_funcs):
pg[self.pname] = f(self.n_epochs/self.epochs)
 def begin_batch(self):
if self.in_train: self.set_param()<init_hyperparams>
train.drop(['label'],axis=1,inplace=True)
 tr = train.as_matrix(columns=None)
 print(tr.shape )
class LR_Find(Callback):
_order = 1
def __init__(self, max_iter=100, min_lr=1e-6, max_lr=10):
self.max_iter, self.min_lr, self.max_lr = max_iter, min_lr, max_lr
self.best_loss = 1e9
def begin_batch(self):
if not self.in_train: return
pos = self.n_iter/self.max_iter
lr = self.min_lr*(self.max_lr/self.min_lr)**pos
for pg in self.opt.param_groups: pg['lr'] = lr
def after_step(self):
if self.n_iter>=self.max_iter or self.loss>self.best_loss*10:
raise CancelTrainException()
if self.loss<self.best_loss: self.best_loss = self.loss<choose_model_class>
X_train, X_test, y_train, y_test = train_test_split(tr, lbl, test_size=0.20, random_state=42 )
run = Runner(cb_funcs=[LR_Find, Recorder] )<set_options>
clf = DecisionTreeClassifier()
clf = clf.fit(X_train,y_train)
 prediction = clf.predict(X_test)
 accscore = accuracy_score(y_test,prediction)
 print(accscore )
torch.cuda.empty_cache()<set_options>
from __future__ import print_function
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import np_utils
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.optimizers import RMSprop
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
gc.collect()<categorify>
nb_classes=10
y = np_utils.to_categorical(lbl, nb_classes ).astype(np.float32)
 class_totals = y.sum(axis=0)
 class_weight = class_totals.max() / class_totals
y.shape
def annealer(f):
def _inner(start, end): return partial(f, start, end)
 return _inner<import_modules>
datagen_try = ImageDataGenerator(rotation_range=15,
width_shift_range = 0.25,
height_shift_range = 0.25,
shear_range = 25,
zoom_range = 0.4,)
 datagen_try.fit(X)
 for x_batch, y_batch in datagen_try.flow(X, y, batch_size=9):
for i in range(0, 9):
plt.subplot(330 + 1 + i)
 plt.imshow(x_batch[i].reshape(28, 28), cmap=plt.get_cmap('gray'))
plt.show()
break
import math<define_variables>
print("[INFO] generating images...")
 total = X.shape[0]
print(total,"is total")
 finalX,finalY = [],[]
i = 0
for x_batch, y_batch in datagen_try.flow(X, y, batch_size=1):
finalX.append(x_batch)
 finalY.append(y_batch)
 i+=1
if i==total:
break
if i%10000==0 and i>999:
print(i,"/60000 done" )
def pg_dicts(pgs): return [{'params': o} for o in pgs]<compute_test_metric>
finalX = np.array(finalX)
 finalY = np.array(finalY )
def cos_1cycle_anneal(start, high, end):
return [sched_cos(start, high), sched_cos(high, end)]<import_modules>
finalY = finalY.reshape(( 60000, 10))
from functools import partial<define_variables>
phases = [0.2, 0.8]<concatenate>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape = X.shape[1:]))
model.add(Conv2D(filters = 32, kernel_size =(4,4),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.3))
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(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.3))
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(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.3))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
 model.summary()
scheds = combine_scheds(phases, [sched_cos(1e-4, 5e-3), sched_cos(5e-3, 1e-3)] )<import_modules>
validation_split = 0.01
model.fit(finalX, finalY, batch_size=1000, class_weight=class_weight, epochs=80, verbose=1, validation_split=validation_split )
from sklearn.metrics import roc_auc_score<compute_test_metric>
open('model.json', 'w' ).write(model.to_json())
 model.save_weights('weights.h5')
 print("Model Saved" )
def accuracy(out, yb): return(torch.argmax(out, dim=1)==yb.long() ).float().mean()<choose_model_class>
test.drop(['id'],axis=1,inplace=True)
 Y = test.as_matrix(columns=None)
 Y.shape
run = Runner(cb_funcs=cbfs )<train_model>
predict = model.predict(Y )
def train_and_eval() :
test_preds = np.zeros(len(test_x))
for fold,(train_idx, valid_idx)in enumerate(splits):
print('Fold:', fold)
 torch.cuda.empty_cache()
learn = get_learner(train_idx, valid_idx)
 gc.collect()
run = Runner(cb_funcs=cbfs)
 learn.model.train()
run.fit(4, learn)
 learn.model.eval()
test_preds_fold = np.zeros(len(test_dl.dataset))
for i,(x_batch,)in enumerate(test_dl):
with torch.no_grad() :
y_pred = learn.model(x_batch ).detach()
test_preds_fold[i*batch_size:(i+1)*batch_size] = sigmoid(y_pred.cpu().numpy())[:, 0]
test_preds+=test_preds_fold/len(splits)
 del(learn)
 gc.collect()
print(f'Test {fold} added')
 print('Training Completed')
 return test_preds<compute_test_metric>
results = np.argmax(predict,axis = 1)
 results
def sigmoid(x): return 1/(1+np.exp(-x))<prepare_output>
Y=Y.reshape(test.shape[0],28,28 )
preds = train_and_eval()<load_from_csv>
ss.drop(n,axis=1,inplace=True )
sub = pd.read_csv('.. /input/jigsaw-unintended-bias-in-toxicity-classification/sample_submission.csv', index_col='id')
 sub['prediction'] = preds
sub.reset_index(drop=False, inplace=True)
 sub.head()<save_to_csv>
ss[n] = results
<save_to_csv><EOS> 
ss.to_csv('submission.csv',index=False )