Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<load_from_csv>
import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping
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")
 print("add infos")
 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>
train_set = ".. /input/Kannada-MNIST/train.csv"
valid_set = ".. /input/Kannada-MNIST/Dig-MNIST.csv"
test_set = ".. /input/Kannada-MNIST/test.csv"
train_csv = pd.read_csv(train_set)
 valid_csv = pd.read_csv(valid_set)
 test_csv = pd.read_csv(test_set )
tr['WHERE'] = 'train'
chunk['WHERE'] = 'val'
sub['WHERE'] = 'test'
data = tr.append([chunk, sub] )<count_unique_values>
def image_generator(images_csv, lbl=True):
labels = 0
if(lbl==True):
labels = images_csv[images_csv.columns[0]].to_numpy(dtype=np.float64, copy=True)
 labels = tf.keras.utils.to_categorical(labels, num_classes=10)
 images = images_csv.loc[:,'pixel0':'pixel783'].to_numpy(dtype=np.float64, copy=True)
 images = images.reshape(( len(images),28,28,1))
images = images / 255.
 return images, labels
print(tr.shape, chunk.shape, sub.shape, data.shape)
 print(tr.Patient.nunique() , chunk.Patient.nunique() , sub.Patient.nunique() ,
data.Patient.nunique())
<feature_engineering>
train_images, train_labels = image_generator(train_csv)
 valid_images, valid_labels = image_generator(valid_csv )
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>
X_train, y_train, X_test, y_test = train_test_split(np.concatenate(( train_images, valid_images)) ,
np.concatenate(( train_labels, valid_labels)) ,
test_size=0.1,
shuffle=True )
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>
def cnn_model() :
inp = tf.keras.Input(shape=(28,28,1))
x1 = tf.keras.layers.Conv2D(128,(1,1), strides=(1,1), activation='relu' )(inp)
 x1 = tf.keras.layers.BatchNormalization()(x1)
 x3 = tf.keras.layers.Conv2D(128,(3,3), padding='same', strides=(1,1), activation='relu' )(inp)
 x3 = tf.keras.layers.BatchNormalization()(x3)
 x5 = tf.keras.layers.Conv2D(128,(5,5), padding='same', strides=(1,1), activation='relu' )(inp)
 x5 = tf.keras.layers.BatchNormalization()(x5)
 averaged = tf.keras.layers.Average()([x1,x3,x5])
 averaged = tf.keras.layers.Activation('relu' )(averaged)
 averaged = tf.keras.layers.BatchNormalization()(averaged)
 x = tf.keras.layers.Conv2D(128,(5,5), strides=(1,1), activation='relu' )(averaged)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.MaxPooling2D()(x)
 x = tf.keras.layers.Dropout(rate=0.25 )(x)
 x1 = tf.keras.layers.Conv2D(128,(1,1), strides=(1,1), activation='relu' )(x)
 x1 = tf.keras.layers.BatchNormalization()(x1)
 x3 = tf.keras.layers.Conv2D(128,(3,3), padding='same', strides=(1,1), activation='relu' )(x)
 x3 = tf.keras.layers.BatchNormalization()(x3)
 x5 = tf.keras.layers.Conv2D(128,(5,5), padding='same', strides=(1,1), activation='relu' )(x)
 x5 = tf.keras.layers.BatchNormalization()(x5)
 averaged = tf.keras.layers.Average()([x1,x3,x5])
 averaged = tf.keras.layers.Activation('relu' )(averaged)
 averaged = tf.keras.layers.BatchNormalization()(averaged)
 x = tf.keras.layers.Conv2D(128,(3,3), strides=(1,1), activation='relu' )(averaged)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.MaxPooling2D()(x)
 x = tf.keras.layers.Dropout(rate=0.25 )(x)
 x1 = tf.keras.layers.Conv2D(256,(1,1), strides=(1,1), activation='relu' )(x)
 x1 = tf.keras.layers.BatchNormalization()(x1)
 x3 = tf.keras.layers.Conv2D(256,(3,3), padding='same', strides=(1,1), activation='relu' )(x)
 x3 = tf.keras.layers.BatchNormalization()(x3)
 x5 = tf.keras.layers.Conv2D(256,(5,5), padding='same', strides=(1,1), activation='relu' )(x)
 x5 = tf.keras.layers.BatchNormalization()(x5)
 averaged = tf.keras.layers.Average()([x1,x3,x5])
 averaged = tf.keras.layers.Activation('relu' )(averaged)
 averaged = tf.keras.layers.BatchNormalization()(averaged)
 x = tf.keras.layers.Conv2D(256,(1,1), strides=(1,1), activation='relu' )(averaged)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.MaxPooling2D()(x)
 x = tf.keras.layers.Dropout(rate=0.25 )(x)
 x1 = tf.keras.layers.Conv2D(256,(1,1), strides=(1,1), activation='relu' )(x)
 x1 = tf.keras.layers.BatchNormalization()(x1)
 x3 = tf.keras.layers.Conv2D(256,(3,3), padding='same', strides=(1,1), activation='relu' )(x)
 x3 = tf.keras.layers.BatchNormalization()(x3)
 x5 = tf.keras.layers.Conv2D(256,(5,5), padding='same', strides=(1,1), activation='relu' )(x)
 x5 = tf.keras.layers.BatchNormalization()(x5)
 averaged = tf.keras.layers.Average()([x1,x3,x5])
 averaged = tf.keras.layers.Activation('relu' )(averaged)
 averaged = tf.keras.layers.BatchNormalization()(averaged)
 x = tf.keras.layers.Conv2D(256,(1,1), strides=(1,1), activation='relu' )(averaged)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.MaxPooling2D()(x)
 x = tf.keras.layers.Dropout(rate=0.25 )(x)
 x = tf.keras.layers.Flatten()(x)
 x = tf.keras.layers.Dense(256, activation=tf.nn.relu )(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Dense(128, activation=tf.nn.relu )(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Dense(64, activation=tf.nn.relu )(x)
 x = tf.keras.layers.BatchNormalization()(x)
 output = tf.keras.layers.Dense(10, activation=tf.nn.softmax )(x)
 model = tf.keras.Model(inputs=inp, outputs=output)
 return model
data = data.merge(base, on='Patient', how='left')
 data['base_week'] = data['Weeks'] - data['min_week']
del base<data_type_conversions>
model.load_weights(".. /input/pretrained/model2.h5" )
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>
EPOCHS = 1
BATCH_SIZE = 128
lr = 0.00000001
opt = tf.keras.optimizers.Adam(learning_rate=lr)
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss',
patience=10,
verbose=1,
factor=0.75)
 es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=25 )
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>
train_aug = tf.keras.preprocessing.image.ImageDataGenerator(rotation_range=10,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.1,
zoom_range=0.2,
horizontal_flip=False)
 valid_aug = tf.keras.preprocessing.image.ImageDataGenerator()
mcp = tf.keras.callbacks.ModelCheckpoint(".. /input/pretrained-models/model.h5", monitor='val_loss',
save_best_only=True, save_weights_only=True)
 model.compile(optimizer=opt, loss=tf.keras.losses.CategoricalCrossentropy() ,metrics=['accuracy'])
 model.fit_generator(train_aug.flow(train_images, train_labels,batch_size=BATCH_SIZE),
steps_per_epoch=10,
validation_data=valid_aug.flow(valid_images, valid_labels),
validation_steps=50,
epochs=EPOCHS, verbose=1,
callbacks=[learning_rate_reduction, es] )
tr = data.loc[data.WHERE=='train']
chunk = data.loc[data.WHERE=='val']
sub = data.loc[data.WHERE=='test']
del data<compute_test_metric>
test_images, _ = image_generator(test_csv, lbl=False)
 preds = model.predict(test_images)
 preds = preds.argmax(axis=1)
 print(preds)
 preds = preds.astype(int ).flatten()
preds =(LabelEncoder().fit_transform(( preds)))
 preds = pd.DataFrame({'label': preds} )
<prepare_x_and_y><EOS> 
sub = pd.DataFrame(data=test_csv.id)
 sub = sub.join(preds)
 sub.to_csv('submission.csv', index=False )