Kaggle competition: Kannada MNIST
def make_model() :
z = L.Input(( 9,), name="Patient")
 x = L.Dense(100, activation="relu", name="d1" )(z)
 x = L.Dense(100, activation="relu", name="d2" )(x)
 p1 = L.Dense(3, activation="linear", name="p1" )(x)
 p2 = L.Dense(3, activation="relu", name="p2" )(x)
 preds = L.Lambda(lambda x: x[0] + tf.cumsum(x[1], axis=1),
name="preds" )([p1, p2])
 model = M.Model(z, preds, name="NN-Optimized")
 model.compile(loss=mloss(0.8), optimizer = tf.keras.optimizers.Adam(lr = LR, beta_1 = BETA_1, beta_2 = BETA_2, epsilon = None, decay = DECAY, amsgrad = AMSGRAD), metrics=[score])
 return model<prepare_x_and_y>
import pandas as pd
import numpy as np
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn import metrics
import time
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Dense, Conv2D, Flatten, MaxPool2D, Dropout, BatchNormalization, LeakyReLU
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.callbacks import LearningRateScheduler
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, LearningRateScheduler
import matplotlib.pyplot as plt
y = tr['FVC'].values
z = tr[FE].values
ze = sub[FE].values
pe = np.zeros(( ze.shape[0], 3))
pred = np.zeros(( z.shape[0], 3))<choose_model_class>
train_data = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test_data = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv")
 dig_data = pd.read_csv("/kaggle/input/Kannada-MNIST/Dig-MNIST.csv" )
NFOLD = 10
kf = KFold(n_splits=NFOLD )<train_model>
data_train = train_data.iloc[:,1:].values
x_train = data_train.reshape(data_train.shape[0], 28, 28, 1)
 train_label = train_data.iloc[:,0].values
y_train = tf.keras.utils.to_categorical(train_label, 10)
 print(x_train.shape, y_train.shape )
%%time
count = 0
for train_idx, val_idx in kf.split(z):
count += 1
print(f"FOLD {count}:")
 net = make_model()
net.fit(z[train_idx], y[train_idx], batch_size=BATCH_SIZE, epochs=850,
validation_data=(z[val_idx], y[val_idx]), verbose=0)
 print("Train:", net.evaluate(z[train_idx], y[train_idx], verbose=0, batch_size=BATCH_SIZE))
print("Val:", net.evaluate(z[val_idx], y[val_idx], verbose=0, batch_size=BATCH_SIZE))
pred[val_idx] = net.predict(z[val_idx], batch_size=BATCH_SIZE, verbose=0)
 print("Predicting Test...")
 pe += net.predict(ze, batch_size=BATCH_SIZE, verbose=0)/ NFOLD<compute_test_metric>
data_val=dig_data.drop('label',axis=1 ).iloc[:,:].values
x_val = data_val.reshape(data_val.shape[0], 28, 28,1)
 val_label=dig_data.label
y_val = tf.keras.utils.to_categorical(val_label, 10)
 print(x_val.shape, y_val.shape )
sigma_opt = mean_absolute_error(y, pred[:, 1])
 unc = pred[:,2] - pred[:, 0]
sigma_mean = np.mean(unc)
 print(sigma_opt, sigma_mean )<prepare_output>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(3,3), padding = 'Same', activation = 'relu', input_shape =(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(3,3), padding = 'Same', activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
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(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.2))
model.add(Conv2D(filters = 128, kernel_size =(3,3), padding = 'Same', activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 128, kernel_size =(3,3), padding = 'Same', activation = 'relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(64, activation = 'relu'))
model.add(Dense(32, activation = 'relu'))
model.add(Dropout(0.3))
model.add(Dense(10, activation = 'softmax'))
sub['FVC1'] = pe[:, 1]
sub['Confidence1'] = pe[:, 2] - pe[:, 0]<prepare_output>
input_shape =(28, 28, 1)
 learning_rate=0.001
batch_size = 512
epochs = 280
optimizer = Adam(learning_rate=learning_rate, beta_1=0.9, beta_2=0.999 )
subm = sub[['Patient_Week','FVC','Confidence','FVC1','Confidence1']].copy()<feature_engineering>
def lr_decay(epoch):
return learning_rate * 0.99 ** epoch
subm.loc[~subm.FVC1.isnull() ,'FVC'] = subm.loc[~subm.FVC1.isnull() ,'FVC1']
if sigma_mean<70:
subm['Confidence'] = sigma_opt
else:
subm.loc[~subm.FVC1.isnull() ,'Confidence'] = subm.loc[~subm.FVC1.isnull() ,'Confidence1']<load_from_csv>
train_data_generator = ImageDataGenerator(rescale = 1./255.,
rotation_range = 20,
width_shift_range = 0.1,
height_shift_range = 0.1,
shear_range = 0.1,
zoom_range = [0.2, 1.2],
horizontal_flip = False)
 val_data_generator = ImageDataGenerator(rescale=1./255 )
otest = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/test.csv')
 for i in range(len(otest)) :
subm.loc[subm['Patient_Week']==otest.Patient[i]+'_'+str(otest.Weeks[i]), 'FVC'] = otest.FVC[i]
subm.loc[subm['Patient_Week']==otest.Patient[i]+'_'+str(otest.Weeks[i]), 'Confidence'] = 0.1<save_to_csv>
model.compile(optimizer=optimizer, loss=['categorical_crossentropy'], metrics=['accuracy'] )
subm[["Patient_Week","FVC","Confidence"]].to_csv("submission.csv", index=False )<import_modules>
start = time.time()
time_spent = 0
accuracy = []
val_accuracy = []
for epoch in range(220):
time_spent = time.time() -start
if time_spent >= 7150:
break
else:
epoch += 1
print('epoch:', epoch)
 history = model.fit_generator(
 train_data_generator.flow(x_train,y_train, batch_size=batch_size),
steps_per_epoch=100,
epochs=1,
callbacks=[LearningRateScheduler(lr_decay)],
validation_data=val_data_generator.flow(x_val,y_val),
validation_steps=50,
verbose=1, shuffle = True)
 accuracy.append(history.history['accuracy'])
 val_accuracy.append(history.history['val_accuracy'])
import numpy as np
import pandas as pd
import pydicom
import os
import random
import matplotlib.pyplot as plt
from tqdm import tqdm
from PIL import Image
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import KFold<import_modules>
x_test = x_test/255
predictions = model.predict_classes(x_test)
 submission = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 submission['label'] = predictions
submission.head()
import tensorflow as tf
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M<define_variables>
submission.to_csv("submission.csv",index=False )
<define_variables><EOS> 
print('end...' )