Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<concatenate>
%matplotlib inline
train['WHERE'] = 'train'
test['WHERE'] = 'val'
sub['WHERE'] = 'test'
data = train.append([test, sub] )<train_model>
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.compat.v1.Session(config=config )
print('Training Data:',train.info() , end = "
")
 print('Testing Data:',test.info() )<feature_engineering>
test = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv")
 train = pd.read_csv("/kaggle/input/Kannada-MNIST/train.csv")
 dig_mnist = pd.read_csv("/kaggle/input/Kannada-MNIST/Dig-MNIST.csv")
 sample_submission = pd.read_csv("/kaggle/input/Kannada-MNIST/sample_submission.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 = train.loc[:, train.columns!='label'].values.astype('uint8')
 print("X_train.shape", X_train.shape)
 y_train = train['label'].values
X_train = X_train.reshape(( X_train.shape[0],28,28))
print("X_train.shape", X_train.shape)
 print("y_train.shape",X_train.shape )
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>
X_train = X_train[:,:,:,None]
X_test = X_test[:,:,:,None]
data = data.merge(base, on='Patient', how='left')
 data['base_week'] = data['Weeks'] - data['min_week']
del base<data_type_conversions>
batch_size = 32
num_epochs = 50
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>
num_samples = X_train.shape[0]
num_classes = np.unique(y_train ).shape[0]
img_rows, img_cols = X_train[0,:,:,0].shape
classes = np.unique(y_train )
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>
print("num_samples",num_samples)
 print("num_classes",num_classes)
 print("img_rows",img_rows)
 print("img_cols",img_cols)
 print("classes",classes )
train = data.loc[data.WHERE=='train']
test = data.loc[data.WHERE=='val']
sub = data.loc[data.WHERE=='test']
del data<compute_test_metric>
y_train = np_utils.to_categorical(y_train, num_classes)
 y_train.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
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="CNN")
 model.compile(loss=mloss(0.8), optimizer=tf.keras.optimizers.Adam(lr=0.1, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.01, amsgrad=False), metrics=[score])
 return model<prepare_x_and_y>
X_train_norm = X_train.astype('float32')
 X_test_norm = X_test.astype('float32')
 X_train_norm /= 255
X_test_norm /= 255
y = train['FVC'].values
z = train[FE].values
ze = sub[FE].values
pe = np.zeros(( ze.shape[0], 3))
pred = np.zeros(( z.shape[0], 3))<choose_model_class>
learning_rate_reduction=ReduceLROnPlateau(monitor='val_loss',
patience=5,
verbose=1,
factor=0.2
)
NFOLD = 5
kf = KFold(n_splits=NFOLD )<find_best_model_class>
early_stopping = EarlyStopping(monitor='val_loss',
mode='min',
verbose=1,
patience=10
)
%%time
cnt = 0
for tr_idx, val_idx in kf.split(z):
cnt += 1
print(f"FOLD {cnt}")
 net = make_model()
net.fit(z[tr_idx], y[tr_idx], batch_size=BATCH_SIZE, epochs=2000,
validation_data=(z[val_idx], y[val_idx]), verbose=0)
 print("train", net.evaluate(z[tr_idx], y[tr_idx], verbose=0, batch_size=BATCH_SIZE))
print("val", net.evaluate(z[val_idx], y[val_idx], verbose=0, batch_size=BATCH_SIZE))
print("predict val...")
 pred[val_idx] = net.predict(z[val_idx], batch_size=BATCH_SIZE, verbose=0)
 print("predict test...")
 pe += net.predict(ze, batch_size=BATCH_SIZE, verbose=0)/ NFOLD<compute_test_metric>
model_check_point = ModelCheckpoint('model.h5', save_best_only=True )
sigma_opt = mean_absolute_error(y, pred[:, 1])
 unc = pred[:,2] - pred[:, 0]
sigma_mean = np.mean(unc)
 print(sigma_opt, sigma_mean )<feature_engineering>
class DenseNet:
def __init__(self, input_shape=None, dense_blocks=3, dense_layers=-1, growth_rate=12, nb_classes=None,
dropout_rate=None, bottleneck=False, compression=1.0, weight_decay=1e-4, depth=40):
if nb_classes == None:
raise Exception(
 'Please define number of classes(e.g.num_classes=10 ).This is required for final softmax.')
 if compression <= 0.0 or compression > 1.0:
raise Exception('Compression have to be a value between 0.0 and 1.0.')
 if type(dense_layers)is list:
if len(dense_layers)!= dense_blocks:
raise AssertionError('Number of dense blocks have to be same length to specified layers')
 elif dense_layers == -1:
dense_layers = int(( depth - 4)/ 3)
 if bottleneck:
dense_layers = int(dense_layers / 2)
 dense_layers = [dense_layers for _ in range(dense_blocks)]
else:
dense_layers = [dense_layers for _ in range(dense_blocks)]
self.dense_blocks = dense_blocks
self.dense_layers = dense_layers
self.input_shape = input_shape
self.growth_rate = growth_rate
self.weight_decay = weight_decay
self.dropout_rate = dropout_rate
self.bottleneck = bottleneck
self.compression = compression
self.nb_classes = nb_classes
def build_model(self):
img_input = Input(shape=self.input_shape, name='img_input')
 nb_channels = self.growth_rate
x = Conv2D(2*self.growth_rate,(3,3),
padding='same', strides =(1,1),
kernel_regularizer=keras.regularizers.l2(self.weight_decay))(img_input)
 for block in range(self.dense_blocks-1):
x, nb_channels = self.dense_block(x, self.dense_layers[block], nb_channels, self.growth_rate,
self.dropout_rate, self.bottleneck, self.weight_decay)
 x = self.transition_layer(x, nb_channels, self.dropout_rate, self.compression, self.weight_decay)
 nb_channels = int(nb_channels*self.compression)
 x, nb_channels = self.dense_block(x, self.dense_layers[-1], nb_channels, self.growth_rate, self.dropout_rate, self.weight_decay)
 x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = GlobalAveragePooling2D()(x)
 prediction = Dense(self.nb_classes, activation='softmax' )(x)
 return Model(inputs=img_input, outputs=prediction, name='densenet')
 def dense_block(self, x, nb_layers, nb_channels, growth_rate, dropout_rate=None, bottleneck=False, weight_decay=1e-4):
for i in range(nb_layers):
cb = self.convolution_block(x, growth_rate, dropout_rate, bottleneck)
 nb_channels += growth_rate
x = concatenate([cb,x])
 return x, nb_channels
def convolution_block(self, x, nb_channels, dropout_rate=None, bottleneck=False, weight_decay=1e-4):
if bottleneck:
bottleneckWidth = 4
x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = Conv2D(nb_channels * bottleneckWidth,(1, 1),
kernel_regularizer=keras.regularizers.l2(weight_decay))(x)
 if dropout_rate:
x = Dropout(dropout_rate )(x)
 x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = Conv2D(nb_channels,(3, 3), padding='same' )(x)
 if dropout_rate:
x = Dropout(dropout_rate )(x)
 return x
def transition_layer(self, x, nb_channels, dropout_rate=None, compression=1.0, weight_decay=1e-4):
x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = Conv2D(int(nb_channels * compression),(1, 1), padding='same',
kernel_regularizer=keras.regularizers.l2(weight_decay))(x)
 if dropout_rate:
x = Dropout(dropout_rate )(x)
 x = AveragePooling2D(( 2, 2), strides=(2, 2))(x)
 return x
sub['FVC1'] = 0.996 * pe[:, 1]
sub['Confidence1'] = pe[:, 2] - pe[:, 0]<prepare_output>
densenet = DenseNet(( 28,28,1), nb_classes=10, depth=21 )
subm = sub[['Patient_Week','FVC','Confidence','FVC1','Confidence1']].copy()<feature_engineering>
model = densenet.build_model()
model_optimizer = Adam(lr=1E-3, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
 model.compile(loss='categorical_crossentropy', optimizer=model_optimizer, metrics=['accuracy'])
 model.summary()
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>
history = model.fit(X_train_norm,
y_train,
batch_size = batch_size,
epochs = num_epochs,
validation_split = 0.1,
shuffle = True,
callbacks = [learning_rate_reduction, early_stopping]
)
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.save('model.h5' )
subm[["Patient_Week","FVC","Confidence"]].to_csv("submission.csv", index=False )<import_modules>
pred = model.predict(X_test_norm )
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>
pred=np.argmax(pred, axis=1 )
import tensorflow as tf
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M<define_variables>
sample_submission['label'] = pred
<define_variables><EOS> 
sample_submission.to_csv("submission.csv", index=False )