Kaggle competition: Kannada MNIST
gc.collect()
del train_df,test_df,train_df_out_rem<import_modules>
import tensorflow as tf
from sklearn.model_selection import train_test_split
import keras
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from keras.layers import LeakyReLU
from keras.layers.normalization import BatchNormalization
from tensorflow.keras.callbacks import LearningRateScheduler
from tensorflow.keras.callbacks import EarlyStopping
import matplotlib.pyplot as plt
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import KFold, StratifiedKFold
import lightgbm as lgb<train_on_grid>
train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv")
 Dig_MNIST = pd.read_csv("/kaggle/input/Kannada-MNIST/Dig-MNIST.csv" )
def kfold_lightgbm(train_df,target,test_df, num_folds, stratified = True, debug= False):
print("Starting LightGBM.Train shape: {}, test shape: {}".format(train_df.shape, test_df.shape))
if stratified:
folds = StratifiedKFold(n_splits= num_folds, shuffle=True, random_state=326)
 else:
folds = KFold(n_splits= num_folds, shuffle=True, random_state=326)
 oof_preds = np.zeros(train_df.shape[0])
 sub_preds = np.zeros(test_df.shape[0])
 feature_importance_df = pd.DataFrame()
for n_fold,(train_idx, valid_idx)in enumerate(folds.split(train_df,target)) :
train_x, train_y = train_df.iloc[train_idx], target[train_idx]
valid_x, valid_y = train_df.iloc[valid_idx], target[valid_idx]
lgb_train = lgb.Dataset(train_x,
label=train_y,
free_raw_data=False)
 lgb_test = lgb.Dataset(valid_x,
label=valid_y,
free_raw_data=False)
 params = {
'num_leaves': 20,
'max_bin': 60,
'min_data_in_leaf': 17,
'learning_rate': 0.07,
'min_sum_hessian_in_leaf': 0.000446,
'bagging_fraction': 1.0,
'bagging_freq': 5,
'lambda_l1': 4,
'lambda_l2': 1,
'min_gain_to_split': 0.15,
'max_depth': 18,
'save_binary': True,
'objective': 'binary',
'boosting_type': 'gbdt',
'verbose': 1,
'metric': 'auc',
'is_unbalance': True,
}
reg = lgb.train(
 params,
lgb_train,
valid_sets=[lgb_test],
valid_names=[ 'test'],
num_boost_round=10000,
early_stopping_rounds= 200,
verbose_eval=100 
)
 oof_preds[valid_idx] = reg.predict(valid_x, num_iteration=reg.best_iteration)
 sub_preds += reg.predict(test_df, num_iteration=reg.best_iteration)/ folds.n_splits
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = list(test_df.columns)
 fold_importance_df["importance"] = np.log1p(reg.feature_importance(importance_type='gain', iteration=reg.best_iteration))
fold_importance_df["fold"] = n_fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 print('Fold %2d AUC : %.6f' %(n_fold + 1, roc_auc_score(valid_y, oof_preds[valid_idx])))
 del reg, train_x, train_y, valid_x, valid_y
gc.collect()
return sub_preds,oof_preds<concatenate>
sample_sub = pd.read_csv("/kaggle/input/Kannada-MNIST/sample_submission.csv" )
test_preds,train_preds=kfold_lightgbm(train_x,train_y.values,final_test_df, 10 )<compute_test_metric>
X=train.iloc[:,1:].values
Y=train.iloc[:,0].values
Y[:10]
roc_auc_score(train_y.values,train_preds )<load_from_csv>
X_train, X_valid, Y_train, Y_valid = train_test_split(X, Y, test_size = 0.10, random_state=42 )
subm=pd.read_csv('.. /input/sample_submission.csv' )<save_to_csv>
train_datagen = ImageDataGenerator(rescale = 1./255.,
rotation_range = 10,
width_shift_range = 0.25,
height_shift_range = 0.25,
shear_range = 0.1,
zoom_range = 0.25,
horizontal_flip = False )
subm.target=test_preds
subm.to_csv('submission.csv',index=False )<set_options>
valid_datagen = ImageDataGenerator(rescale=1./255 )
%matplotlib inline<define_variables>
def lr_decay(epoch):
return initial_learningrate * 0.99 ** epoch
all_files = glob.glob(".. /input/rsna-outputs/*.csv")
 all_files<define_variables>
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(filters=64, kernel_size=(3,3), padding='same', input_shape=(28, 28, 1)) ,
tf.keras.layers.BatchNormalization(momentum=0.5, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Conv2D(filters=64, kernel_size=(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Conv2D(filters=128, kernel_size=(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.2, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Conv2D(filters=128, kernel_size=(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Conv2D(filters=256, kernel_size=(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.2, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Conv2D(filters=256, kernel_size=(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(256),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(10, activation='softmax')
 ])
 model.summary()
all_files_addit = glob.glob(".. /input/rsna-public-not-checked0/*.csv")
 all_files_addit<define_variables>
initial_learningrate=2e-3
batch_size = 1024
epochs = 30
input_shape =(28, 28, 1)
 model.compile(loss="categorical_crossentropy",
optimizer=RMSprop(lr=initial_learningrate),
metrics=['accuracy'])
 learning_rate_reduction = ReduceLROnPlateau(monitor='loss',
patience=2,
verbose=1,
factor=0.2,
min_lr=0.00001)
 history = model.fit_generator(
 train_datagen.flow(X_train,Y_train, batch_size=batch_size),
steps_per_epoch=100,
epochs=epochs,
callbacks=[learning_rate_reduction],
validation_data=valid_datagen.flow(X_valid,Y_valid),
validation_steps=50
)
all_files_addit_manoj = glob.glob(".. /input/inceptionresnetv2manoj/*.csv")
 all_files_addit_manoj<load_from_csv>
preds_dig=model.predict_classes(x_dig/255)
 metrics.accuracy_score(preds_dig, y_dig )
!cp.. /input/rsna-public-not-checked0/*.csv.. /input/alltogether/
!cp.. /input/inceptionresnetv2manoj/*.csv.. /input/alltogether/
!cp.. /input/rsna-outputs/*.csv.. /input/alltogether/<load_from_csv>
predictions = model.predict_classes(x_test/255.)
<merge><EOS> 
submission = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 submission['label'] = predictions
submission.to_csv("submission.csv",index=False )