Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<prepare_output>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization
from tensorflow.keras.callbacks import LearningRateScheduler, EarlyStopping, ReduceLROnPlateau
from sklearn.metrics import accuracy_score
subm = sub[['Patient_Week','FVC1','Confidence1']].copy()<rename_columns>
train_dataset = pd.read_csv("/kaggle/input/Kannada-MNIST/train.csv")
 test_dataset = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv" )
subm.rename(columns={'FVC1':'FVC','Confidence1':'Confidence'},inplace=True )<save_to_csv>
X_train = train_dataset.iloc[:, 1:].values.astype("float32")
 Y_train = train_dataset.iloc[:, 0].values.astype("int32")
 X_test = test_dataset.iloc[:, 1:].values.astype("float32")
 NumberID = test_dataset.iloc[:, 0].values.astype("float32" )
subm[["Patient_Week","FVC","Confidence"]].to_csv("submission.csv", index=False )<install_modules>
X_train /= 255.0
X_test /= 255.0
!pip install.. /input/kerasapplications/keras-team-keras-applications-3b180cb -f./ --no-index
!pip install.. /input/efficientnet/efficientnet-1.1.0/ -f./ --no-index<set_options>
Y_train = to_categorical(Y_train, num_classes = 10 )
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 )<set_options>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state = 0 )
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.compat.v1.Session(config=config )<load_from_csv>
epochs = 50
batch_size = 64
input_dim =(28, 28, 1 )
train = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/train.csv' )<concatenate>
model = Sequential([
Conv2D(32,(3, 3), padding = 'same', activation = 'relu', input_shape = input_dim),
BatchNormalization() ,
Conv2D(32,(3, 3), padding = 'same', activation = 'relu'),
BatchNormalization() ,
MaxPool2D(2, 2),
Dropout(0.2),
Conv2D(64,(3, 3), padding = 'same', activation = 'relu'),
BatchNormalization() ,
Conv2D(64,(3, 3), padding = 'same', activation = 'relu'),
BatchNormalization() ,
MaxPool2D(2, 2),
Dropout(0.3),
Conv2D(128,(3, 3), padding = 'same', activation = 'relu'),
BatchNormalization() ,
Conv2D(128,(3, 3), padding = 'same', activation = 'relu'),
BatchNormalization() ,
MaxPool2D(2, 2),
Dropout(0.4),
Flatten() ,
Dense(10, activation = 'softmax')
 ] )
def get_tab(df):
vector = [(df.Age.values[0] - 30)/ 30]
if df.Sex.values[0] == 'male':
vector.append(0)
 else:
vector.append(1)
 if df.SmokingStatus.values[0] == 'Never smoked':
vector.extend([0,0])
 elif df.SmokingStatus.values[0] == 'Ex-smoker':
vector.extend([1,1])
 elif df.SmokingStatus.values[0] == 'Currently smokes':
vector.extend([0,1])
 else:
vector.extend([1,0])
 return np.array(vector )<feature_engineering>
datagen = ImageDataGenerator(rotation_range = 10,
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range = 0.1,
horizontal_flip = False,
vertical_flip = False)
 datagen.fit(X_train )
A = {}
TAB = {}
P = []
for i, p in tqdm(enumerate(train.Patient.unique())) :
sub = train.loc[train.Patient == p, :]
fvc = sub.FVC.values
weeks = sub.Weeks.values
c = np.vstack([weeks, np.ones(len(weeks)) ] ).T
a, b = np.linalg.lstsq(c, fvc)[0]
A[p] = a
TAB[p] = get_tab(sub)
 P.append(p )<feature_engineering>
early_stopping = EarlyStopping(monitor = 'val_acc',
patience = 10,
verbose = 1,
mode = 'auto',
restore_best_weights = True )
class IGenerator(Sequence):
BAD_ID = ['ID00011637202177653955184', 'ID00052637202186188008618']
def __init__(self, keys, a, tab, batch_size=32):
self.keys = [k for k in keys if k not in self.BAD_ID]
self.a = a
self.tab = tab
self.batch_size = batch_size
self.train_data = {}
for p in train.Patient.values:
self.train_data[p] = os.listdir(f'.. /input/osic-pulmonary-fibrosis-progression/train/{p}/')
 def __len__(self):
return 1000
def __getitem__(self, idx):
x = []
a, tab = [], []
keys = np.random.choice(self.keys, size = self.batch_size)
 for k in keys:
try:
i = np.random.choice(self.train_data[k], size=1)[0]
img = get_img(f'.. /input/osic-pulmonary-fibrosis-progression/train/{k}/{i}')
 x.append(img)
 a.append(self.a[k])
 tab.append(self.tab[k])
 except:
print(k, i)
 x,a,tab = np.array(x), np.array(a), np.array(tab)
 x = np.expand_dims(x, axis=-1)
 return [x, tab] , a<choose_model_class>
learning_rate_reduction = ReduceLROnPlateau(monitor = 'val_acc',
patience = 3,
verbose = 1,
factor = 0.5,
min_lr = 0.00001 )
Dense, Dropout, Activation, Flatten, Input, BatchNormalization, GlobalAveragePooling2D, Add, Conv2D, AveragePooling2D,
LeakyReLU, Concatenate 
)
 def get_efficientnet(model, shape):
models_dict = {
'b0': efn.EfficientNetB0(input_shape=shape,weights=None,include_top=False),
'b1': efn.EfficientNetB1(input_shape=shape,weights=None,include_top=False),
'b2': efn.EfficientNetB2(input_shape=shape,weights=None,include_top=False),
'b3': efn.EfficientNetB3(input_shape=shape,weights=None,include_top=False),
'b4': efn.EfficientNetB4(input_shape=shape,weights=None,include_top=False),
'b5': efn.EfficientNetB5(input_shape=shape,weights=None,include_top=False),
'b6': efn.EfficientNetB6(input_shape=shape,weights=None,include_top=False),
'b7': efn.EfficientNetB7(input_shape=shape,weights=None,include_top=False)
 }
return models_dict[model]
def build_model(shape=(512, 512, 1), model_class=None):
inp = Input(shape=shape)
 base = get_efficientnet(model_class, shape)
 x = base(inp)
 x = GlobalAveragePooling2D()(x)
 inp2 = Input(shape=(4,))
x2 = tf.keras.layers.GaussianNoise(0.2 )(inp2)
 x = Concatenate()([x, x2])
 x = Dropout(0.5 )(x)
 x = Dense(1 )(x)
 model = Model([inp, inp2] , x)
 weights = [w for w in os.listdir('.. /input/osic-model-weights')if model_class in w][0]
model.load_weights('.. /input/osic-model-weights/' + weights)
 return model
model_classes = ['b5']
models = [build_model(shape=(512, 512, 1), model_class=m)for m in model_classes]
print('Number of models: ' + str(len(models)) )<split>
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['acc'] )
tr_p, vl_p = train_test_split(P,
shuffle=True,
train_size= 0.8 )<compute_test_metric>
history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size = batch_size),
epochs = epochs,
steps_per_epoch = X_train.shape[0] // batch_size,
validation_data =(X_val, Y_val),
callbacks = [learning_rate_reduction, early_stopping] )
<define_variables><EOS> 
preds = model.predict_classes(X_test)
 def write_preds(preds, fname):
pd.DataFrame({"id": np.arange(preds.shape[0]), "label": preds} ).to_csv(fname, index=False)
 write_preds(preds, "submission.csv" )