Kaggle competition: Kannada MNIST
if debug:
magic_max=2
magic_min=0
NFOLDS=2
else:
magic_max=train['wheezy-copper-turtle-magic'].max()
magic_min=train['wheezy-copper-turtle-magic'].min()<prepare_x_and_y>
data = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 print(data.shape )
def preprocess(train=train, test=test):
prepr = {}
for i in range(magic_min, magic_max+1):
X = train[train['wheezy-copper-turtle-magic']==i].copy()
Y = X.pop('target' ).values
X_test = test[test['wheezy-copper-turtle-magic']==i].copy()
idx_train = X.index
idx_test = X_test.index
X.reset_index(drop=True,inplace=True)
 cols = np.array([c for c in X.columns if c not in ['id', 'wheezy-copper-turtle-magic']])
 l=len(X)
 X_all = pd.concat([X[cols], X_test[cols]], ignore_index=True)
 sel = VarianceThreshold(threshold=2)
 X_vt = sel.fit_transform(X_all)
 prepr['vt_' + str(i)] = X_vt
prepr['n_vt' + str(i)] = X_vt.shape[1]
prepr['feats_vt' + str(i)] = cols[sel.get_support(indices=True)]
prepr['train_size_' + str(i)] = l
prepr['idx_train_' + str(i)] = idx_train
prepr['idx_test_' + str(i)] = idx_test
prepr['target_' + str(i)] = Y
return prepr<categorify>
test_data = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')
 print(test_data.shape )
%%time
data = preprocess()<prepare_x_and_y>
datagen = ImageDataGenerator(
 rotation_range=10,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range = 10,
horizontal_flip = False,
zoom_range = 0.15 )
def get_data(i, data):
l = data['train_size_' + str(i)]
X_all = data['vt_' + str(i)]
X = X_all[:l, :]
X_test = X_all[l:, :]
Y = data['target_' + str(i)]
idx_train = data['idx_train_' + str(i)]
idx_test = data['idx_test_' + str(i)]
return X, X_test, Y, idx_train, idx_test<find_best_params>
encoder = OneHotEncoder(sparse=False,categories='auto')
 yy = [[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]]
encoder.fit(yy)
 train_label = train_label.reshape(-1,1)
 val_label = val_label.reshape(-1,1)
 train_label = encoder.transform(train_label)
 val_label = encoder.transform(val_label)
 print('train_label shape: %s'%str(train_label.shape))
print('val_label shape: %s'%str(val_label.shape))
def initialize_cv() :
auc = np.array([])
 oof = np.zeros(len(train))
preds = np.zeros(len(test))
return preds, oof, auc<compute_test_metric>
model = Sequential()
model.add(Conv2D(64, kernel_size=3, activation='relu', input_shape=(28, 28, 1),padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(64, kernel_size=3, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(64, kernel_size=5, padding='same', activation='relu'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.3))
model.add(Conv2D(128, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(128, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(128, kernel_size=5, padding='same', activation='relu'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.3))
model.add(Conv2D(256, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.3))
model.add(Flatten())
 model.add(Dense(256,kernel_regularizer=regularizers.l2(0.02)))
 model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(Dropout(0.3))
model.add(Dense(256,kernel_regularizer=regularizers.l2(0.02)))
 model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(Dropout(0.3))
model.add(Dense(512,kernel_regularizer=regularizers.l2(0.02)))
 model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(Dropout(0.3))
model.add(Dense(10, activation='softmax'))
model.summary()
def report_results(oof, auc_all, clf_name='GMM'):
print(f'The result summary for the {clf_name} classifier:')
 auc_combo = roc_auc_score(train['target'].values, oof)
 auc_av = np.mean(auc_all)
 std = np.std(auc_all)/(np.sqrt(NFOLDS)*np.sqrt(magic_max+1))
print(f'The combined CV score is {round(auc_combo, 5)}.')
 print(f'The folds average CV score is {round(auc_av, 5)}.')
 print(f'The standard deviation is {round(std, 5)}.
 ' )<train_on_grid>
BATCH_SIZE = 512
EPOCHS = 60
def clusters_stats(X_train, Y_train, params=params_2):
X_train_0 = X_train[Y_train==0]
Y_train_0 = Y_train[Y_train==0].reshape(-1, 1)
 X_train_1 = X_train[Y_train==1]
Y_train_1 = Y_train[Y_train==1].reshape(-1, 1)
 clf_0 = GaussianMixture(**params)
 clf_0.fit(X_train_0)
 means_0 = clf_0.means_
covs_0 = clf_0.covariances_
ps_0 = [np.linalg.inv(m)for m in covs_0]
clf_1 = GaussianMixture(**params)
 clf_1.fit(X_train_1)
 means_1 = clf_1.means_
covs_1 = clf_1.covariances_
ps_1 = [np.linalg.inv(m)for m in covs_1]
ms = np.stack(( means_0[0], means_0[1], means_1[0], means_1[1]))
ps = np.stack(( ps_0[0], ps_0[1], ps_1[0], ps_1[1]))
return ms, ps<train_model>
model.compile(loss='categorical_crossentropy',optimizer=Adadelta() ,metrics=['accuracy'])
 datagen.fit(train_image)
 history = model.fit_generator(datagen.flow(train_image,train_label, batch_size=BATCH_SIZE),
epochs = EPOCHS,
shuffle=True,
validation_data =(val_image,val_label),
verbose = 1,
steps_per_epoch=train_image.shape[0] // BATCH_SIZE )
def pseudolabeling(X_train, X_test, Y_train, Y_pseudo,
idx_test, test=test, low=low, high=high):
assert len(test)== len(Y_pseudo), "The length of test does not match that of Y_pseudo!"
Y_aug = np.copy(Y_pseudo[idx_test])
 assert len(Y_aug)== len(X_test), "The length of Y_aug does not match that of X_test!"
Y_aug[Y_aug > high] = 1
Y_aug[Y_aug < low] = 0
mask =(Y_aug == 1)|(Y_aug == 0)
 Y_useful = Y_aug[mask]
X_test_useful = X_test[mask]
X_train_aug = np.vstack(( X_train, X_test_useful))
Y_train_aug = np.vstack(( Y_train.reshape(-1, 1), Y_useful.reshape(-1, 1)))
 return X_train_aug, Y_train_aug<split>
test_label = model.predict(test_image)
def train_classifier(Y_pseudo, params=params_4):
preds, oof, auc_all = initialize_cv()
print(f"Computing centroids and covariances for the four clusters(two per class ).")
 for i in tqdm(range(magic_min, magic_max+1)) :
X, X_test, Y, idx_train, idx_test = get_data(i=i, data=data)
 auc_folds=np.array([])
 folds = StratifiedKFold(n_splits=NFOLDS, random_state=RS)
 for train_index, val_index in folds.split(X, Y):
X_train, Y_train = X[train_index, :], Y[train_index]
X_val, Y_val = X[val_index, :], Y[val_index]
if Y_pseudo is None:
params['means_init'], params['precisions_init'] = clusters_stats(X_train, Y_train)
 else:
X_aug, Y_aug = pseudolabeling(X_train, X_test, Y_train, Y_pseudo, idx_test)
 params['means_init'], params['precisions_init'] = clusters_stats(X_aug, Y_aug.ravel())
 clf = GaussianMixture(**params)
 clf.fit(np.concatenate([X_train, X_test], axis = 0))
oof[idx_train[val_index]] = np.sum(clf.predict_proba(X_val)[:, 2:], axis=1)
 preds[idx_test] += np.sum(clf.predict_proba(X_test)[:,2: ], axis=1)/NFOLDS
auc = roc_auc_score(Y_val, oof[idx_train[val_index]])
 auc_folds = np.append(auc_folds, auc)
 auc_all = np.append(auc_all, np.mean(auc_folds))
report_results(oof, auc_all)
 return preds, oof, auc_all<load_pretrained>
label_hot = model.predict(test_image)
 label = np.argmax(label_hot,1)
 id_ = np.arange(0,label.shape[0] )
Y_pseudo, oof, auc_all = train_classifier(Y_pseudo=None )<save_to_csv>
sim = pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')
 print(sim.head(10))
<predict_on_test><EOS> 
save = pd.DataFrame({'id':id_,'label':label})
 print(save.head(10))
save.to_csv('submission.csv',index=False )