Kaggle competition: Kannada MNIST
Submission['RefId']=test_data['RefId']
Submission.set_index('RefId',inplace=True)
 Submission.head()
Submission.to_csv('Submission.csv' )<import_modules>
train_data = pd.read_csv(".. /input/Kannada-MNIST/train.csv")
 train_data.shape
import numpy as np
import pandas as pd
import os
import random
import matplotlib.pyplot as plt
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import KFold
import tensorflow as tf
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M
from tqdm import tqdm
from PIL import Image
import pydicom<define_variables>
X_train_test = train_data.values[:, 1:]
y_train_test = train_data.label.values
X_train, X_test, y_train, y_test = train_test_split(X_train_test, y_train_test, test_size = 0.02, random_state=42)
 print('Train shapes: ', X_train.shape, y_train.shape)
 print('Test shapes: ', X_test.shape, y_test.shape )
def seed_everything(seed=2020):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 seed_everything(42 )<define_variables>
print(np.min(X_train), np.max(X_train))
X_train_max = np.max(X_train)
 X_train = X_train /(0.5 * X_train_max)- 1
print(np.min(X_train), np.max(X_train))
print(np.min(X_test), np.max(X_test))
X_test = X_test /(0.5 * X_train_max)- 1
print(np.min(X_test), np.max(X_test))
ROOT = ".. /input/osic-pulmonary-fibrosis-progression"
BATCH_SIZE = 128<feature_engineering>
from keras.layers import *
from keras.models import Sequential
from keras.optimizers import *
from keras import regularizers
from keras.utils import plot_model, model_to_dot
from IPython.display import SVG
tr = pd.read_csv(f"{ROOT}/train.csv")
 tr.drop_duplicates(keep=False, inplace=True, subset=['Patient','Weeks'])
 chunk = pd.read_csv(f"{ROOT}/test.csv")
 sub = pd.read_csv(f"{ROOT}/sample_submission.csv")
 sub['Patient'] = sub['Patient_Week'].apply(lambda x:x.split('_')[0])
 sub['Weeks'] = sub['Patient_Week'].apply(lambda x: int(x.split('_')[-1]))
sub = sub[['Patient','Weeks','Confidence','Patient_Week']]
sub = sub.merge(chunk.drop('Weeks', axis=1), on="Patient" )<concatenate>
model = Sequential()
l2_reg_conv2d = 0
l2_reg_dense = 0.01
activation_type = 'relu'
model.add(Conv2D(64, kernel_size=3, activation=activation_type, input_shape=(28, 28, 1), padding='same', kernel_regularizer=regularizers.l2(l2_reg_conv2d)))
 model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size=3, activation=activation_type, padding='same', kernel_regularizer=regularizers.l2(l2_reg_conv2d)))
 model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size=5, activation=activation_type, padding='same', kernel_regularizer=regularizers.l2(l2_reg_conv2d)))
 model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(3, 3)))
 model.add(Dropout(0.4))
model.add(Conv2D(128, kernel_size=3, activation=activation_type, padding='same', kernel_regularizer=regularizers.l2(l2_reg_conv2d)))
 model.add(BatchNormalization())
 model.add(Conv2D(128, kernel_size=3, activation=activation_type, padding='same', kernel_regularizer=regularizers.l2(l2_reg_conv2d)))
 model.add(BatchNormalization())
 model.add(Conv2D(128, kernel_size=5, activation=activation_type, padding='same', kernel_regularizer=regularizers.l2(l2_reg_conv2d)))
 model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(3, 3)))
 model.add(Dropout(0.3))
model.add(Conv2D(256, kernel_size=3, activation=activation_type, padding='same', kernel_regularizer=regularizers.l2(l2_reg_conv2d)))
 model.add(BatchNormalization())
 model.add(Conv2D(256, kernel_size=3, activation=activation_type, padding='same', kernel_regularizer=regularizers.l2(l2_reg_conv2d)))
 model.add(BatchNormalization())
 model.add(Conv2D(256, kernel_size=5, activation=activation_type, padding='same', kernel_regularizer=regularizers.l2(l2_reg_conv2d)))
 model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(3, 3)))
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(256, activation=activation_type, kernel_regularizer=regularizers.l2(l2_reg_dense)))
 model.add(BatchNormalization())
 model.add(Dense(128, activation=activation_type, kernel_regularizer=regularizers.l2(l2_reg_dense)))
 model.add(BatchNormalization())
 model.add(Dense(10, activation='softmax'))
model.compile(optimizer = SGD(lr=0.01),
loss = 'sparse_categorical_crossentropy',
metrics=['accuracy'])
 model.summary()
tr['WHERE'] = 'train'
chunk['WHERE'] = 'val'
sub['WHERE'] = 'test'
data = tr.append([chunk, sub] )<feature_engineering>
datagen = ImageDataGenerator(
 rotation_range = 20,
width_shift_range = 0.3,
height_shift_range = 0.3,
shear_range = 0.2,
zoom_range = 0.3,
horizontal_flip = False )
data['min_week'] = data['Weeks']
data.loc[data.WHERE=='test','min_week'] = np.nan
data['min_week'] = data.groupby('Patient')['min_week'].transform('min' )<categorify>
epochs = 75
batch_size = 128
X_train = X_train.reshape(X_train.shape[0],28,28,1)
 X_test = X_test.reshape(X_test.shape[0],28,28,1)
 train_story = model.fit_generator(datagen.flow(X_train, y_train, batch_size=batch_size),
epochs = epochs,
steps_per_epoch = 100,
validation_data =(X_test, y_test),
callbacks=[
ModelCheckpoint('/kaggle/working/best_kannada_model.h5', save_best_only=True),
CSVLogger('/kaggle/working/learning_log_RMSprop_without_BN.csv'),
],
verbose=1 )
base = data.loc[data.Weeks == data.min_week]
base = base[['Patient','FVC']].copy()
base.columns = ['Patient','min_FVC']
base['nb'] = 1
base['nb'] = base.groupby('Patient')['nb'].transform('cumsum')
 base = base[base.nb==1]
base.drop('nb', axis=1, inplace=True )<merge>
log_batch_norm = np.array(pd.read_csv("/kaggle/working/learning_log_RMSprop_with_BN.csv")['val_accuracy'])
 log_no_batch_norm = np.array(pd.read_csv("/kaggle/working/learning_log_RMSprop_without_BN.csv")['val_accuracy'])
 plt.figure(figsize=(20,10))
plt.plot(range(1, 11), log_batch_norm, label='with BatchNorm')
 plt.plot(range(1, 11), log_no_batch_norm, label='without BatchNorm')
 plt.legend()
plt.show()
data = data.merge(base, on='Patient', how='left')
 data['base_week'] = data['Weeks'] - data['min_week']
del base<data_type_conversions>
log_softmax = np.array(pd.read_csv("/kaggle/working/learning_log_RMSprop_softmax.csv")['val_accuracy'])
 log_elu = np.array(pd.read_csv("/kaggle/working/learning_log_RMSprop_elu.csv")['val_accuracy'])
 log_relu = np.array(pd.read_csv("/kaggle/working/learning_log_RMSprop_relu.csv")['val_accuracy'])
 log_tanh = np.array(pd.read_csv("/kaggle/working/learning_log_RMSprop_tanh.csv")['val_accuracy'])
 log_sigmoid = np.array(pd.read_csv("/kaggle/working/learning_log_RMSprop_sigmoid.csv")['val_accuracy'])
 log_exponential = np.array(pd.read_csv("/kaggle/working/learning_log_RMSprop_exponential.csv")['val_accuracy'])
 plt.figure(figsize=(20,10))
plt.plot(range(1, 11), log_softmax, label='softmax')
 plt.plot(range(1, 11), log_elu, label='elu')
 plt.plot(range(1, 11), log_relu, label='relu')
 plt.plot(range(1, 11), log_tanh, label='tanh')
 plt.plot(range(1, 11), log_sigmoid, label='sigmoid')
 plt.plot(range(1, 11), log_exponential, label='exponential')
 plt.legend()
plt.show()
COLS = ['Sex','SmokingStatus']
FE = []
for col in COLS:
for mod in data[col].unique() :
FE.append(mod)
 data[mod] =(data[col] == mod ).astype(int )<feature_engineering>
data['age'] =(data['Age'] - data['Age'].min())/(data['Age'].max() - data['Age'].min())
 data['BASE'] =(data['min_FVC'] - data['min_FVC'].min())/(data['min_FVC'].max() - data['min_FVC'].min())
 data['week'] =(data['base_week'] - data['base_week'].min())/(data['base_week'].max() - data['base_week'].min())
 data['percent'] =(data['Percent'] - data['Percent'].min())/(data['Percent'].max() - data['Percent'].min())
 FE += ['age','percent','week','BASE']<filter>
log_SGD = np.array(pd.read_csv("/kaggle/working/learning_log_SGD.csv")['val_accuracy'])
 log_SGD_mom = np.array(pd.read_csv("/kaggle/working/learning_log_SGD_mom.csv")['val_accuracy'])
 log_Adam = np.array(pd.read_csv("/kaggle/working/learning_log_Adam.csv")['val_accuracy'])
 log_Adadelta = np.array(pd.read_csv("/kaggle/working/learning_log_Adadelta.csv")['val_accuracy'])
 log_RMSprop = np.array(pd.read_csv("/kaggle/working/learning_log_RMSprop.csv")['val_accuracy'] )
tr = data.loc[data.WHERE=='train']
chunk = data.loc[data.WHERE=='val']
sub = data.loc[data.WHERE=='test']
del data<compute_test_metric>
test_csv = pd.read_csv(".. /input/Kannada-MNIST/test.csv")
 X_val = np.array(test_csv.drop("id",axis=1), dtype=np.float32)
 X_val.shape
C1, C2 = tf.constant(70, dtype='float32'), tf.constant(1000, dtype="float32")
 def score(y_true, y_pred):
tf.dtypes.cast(y_true, tf.float32)
 tf.dtypes.cast(y_pred, tf.float32)
 sigma = y_pred[:, 2] - y_pred[:, 0]
fvc_pred = y_pred[:, 1]
sigma_clip = tf.maximum(sigma, C1)
 delta = tf.abs(y_true[:, 0] - fvc_pred)
 delta = tf.minimum(delta, C2)
 sq2 = tf.sqrt(tf.dtypes.cast(2, dtype=tf.float32))
metric =(delta / sigma_clip)*sq2 + tf.math.log(sigma_clip* sq2)
 return K.mean(metric)
 def qloss(y_true, y_pred):
qs = [0.2, 0.50, 0.8]
q = tf.constant(np.array([qs]), dtype=tf.float32)
 e = y_true - y_pred
v = tf.maximum(q*e,(q-1)*e)
 return K.mean(v)
 def mloss(_lambda):
def loss(y_true, y_pred):
return _lambda * qloss(y_true, y_pred)+(1 - _lambda)*score(y_true, y_pred)
 return loss<init_hyperparams>
best_model = load_model('/kaggle/working/best_kannada_model.h5')
 Y_val = best_model.predict(X_val)
 Y_val = np.argmax(Y_val, axis = 1 )
LR = 0.1
BETA_1 = 0.9
BETA_2 = 0.999
DECAY = 0.01
AMSGRAD = False<choose_model_class>
submission = pd.read_csv(".. /input/Kannada-MNIST/sample_submission.csv")
 submission['label'] = Y_val
submission.to_csv("submission.csv",index=False )