Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<define_variables>
import os
import pandas as pd
import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Conv2D, Dense, Dropout, Flatten, MaxPool2D, BatchNormalization, Activation
from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau, TensorBoard
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import plot_model
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import itertools
SEED = 1234
EPOCHS = 28
BATCH_SIZE = 128
FOLDS = 5
REPEATS = 5
LR = 0.0005
N_TARGETS = len(target_cols )<define_variables>
path_main = '/kaggle/input/Kannada-MNIST'
path_train = os.path.join(path_main, 'train.csv')
 path_test = os.path.join(path_main, 'test.csv')
 path_sample_sub = os.path.join(path_main, 'sample_submission.csv')
 path_dig_mnist = os.path.join(path_main, 'Dig-MNIST.csv' )
def seed_everything(seed):
np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 tf.random.set_seed(seed )<compute_test_metric>
train_dane = pd.read_csv(path_train)
 test_dane = pd.read_csv(path_test)
 sample_dane = pd.read_csv(path_sample_sub)
 dig_mnist_dane = pd.read_csv(path_dig_mnist )
def multi_log_loss(y_true, y_pred):
losses = []
for col in y_true.columns:
losses.append(log_loss(y_true.loc[:, col], y_pred.loc[:, col]))
return np.mean(losses )<data_type_conversions>
X_train = train_dane.iloc[:,1:].values.astype('float32')
 y_train = train_dane.iloc[:,0].values.astype('int32')
 X_test = test_dane.iloc[:,1:].values.astype('float32')
 Dig_mnist_im = dig_mnist_dane.iloc[:,1:].values.astype('float32')
 X_train = X_train.reshape(X_train.shape[0], 28, 28)
 X_test = X_test.reshape(X_test.shape[0], 28, 28)
 Dig_mnist_im = Dig_mnist_im.reshape(Dig_mnist_im.shape[0], 28, 28 )
def preprocess_df(data):
data['cp_type'] =(data['cp_type'] == 'trt_cp' ).astype(int)
 data['cp_dose'] =(data['cp_dose'] == 'D2' ).astype(int)
 return data<prepare_x_and_y>
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
 X_test = X_test.reshape(X_test.shape[0], 28, 28,1)
 mean_px = X_train.mean().astype(np.float32)
 std_px = X_train.std().astype(np.float32)
 mean_px = X_train.mean().astype(np.float32)
 std_px = X_train.std().astype(np.float32)
 def standardize(x):
return(x-mean_px)/std_px
y_train= to_categorical(y_train)
 num_classes = y_train.shape
x_train = preprocess_df(train_df.drop(columns="sig_id"))
x_test =preprocess_df(test_df.drop(columns="sig_id"))
y_train = train_target_df.drop(columns="sig_id")
 N_FEATURES = x_train.shape[1]<choose_model_class>
X = X_train
y = y_train
X_traine, X_val, y_traine, y_val = train_test_split(X_train, y_train, test_size=0.20, random_state=42 )
def create_model() :
model = tf.keras.Sequential([
tf.keras.layers.Input(N_FEATURES),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.2),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(2048, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(2048, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(N_TARGETS, activation="sigmoid"))
])
 model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate = LR), loss='binary_crossentropy', metrics=["accuracy"])
 return model<train_model>
input_shape=(28,28,1 )
def build_train(resume_models = None, repeat_number = 0, folds = 5, skip_folds = 0):
models = []
oof_preds = y_train.copy()
kfold = KFold(folds, shuffle = True)
 for fold,(train_ind, val_ind)in enumerate(kfold.split(x_train)) :
print('
')
 print('-'*50)
 print(f'Training fold {fold + 1}')
 cb_lr_schedule = tf.keras.callbacks.ReduceLROnPlateau(monitor = 'binary_crossentropy', factor = 0.4, patience = 2, verbose = 1, min_delta = 0.0001, mode = 'auto')
 checkpoint_path = f'repeat:{repeat_number}_Fold:{fold}.hdf5'
cb_checkpt = tf.keras.callbacks.ModelCheckpoint(checkpoint_path, monitor = 'val_loss', verbose = 0, save_best_only = True, save_weights_only = True, mode = 'min')
 model = create_model()
model.fit(x_train.values[train_ind],
y_train.values[train_ind],
validation_data=(x_train.values[val_ind], y_train.values[val_ind]),
callbacks = [cb_lr_schedule, cb_checkpt],
epochs=EPOCHS, batch_size=BATCH_SIZE, verbose=2 
)
 model.load_weights(checkpoint_path)
 oof_preds.loc[val_ind, :] = model.predict(x_train.values[val_ind])
 models.append(model)
 return models, oof_preds<train_model>
model = Sequential([
Conv2D(32,(3,3), input_shape=input_shape, padding='same', activation='relu'),
Conv2D(32,(3,3), padding='same', activation='relu'),
BatchNormalization(momentum=0.15),
MaxPool2D(( 2,2)) ,
BatchNormalization(momentum=0.15),
Conv2D(64,(5,5), padding='same', activation='relu'),
Dropout(0.3),
Conv2D(32,(3,3), padding='same', activation='relu'),
Conv2D(32,(3,3), padding='same', activation='relu'),
BatchNormalization(momentum=0.15),
MaxPool2D(( 2,2)) ,
BatchNormalization(momentum=0.15),
Conv2D(64,(5,5), padding='same', activation='relu'),
Dropout(0.3),
Flatten() ,
Dense(128, activation='relu'),
Dropout(0.4),
Dense(10, activation='softmax')
 ] )
models = []
oof_preds = []
seed_everything(SEED)
 for i in range(REPEATS):
m, oof = build_train(repeat_number = i, folds=FOLDS)
 models = models + m
oof_preds.append(oof)
<compute_test_metric>
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
mean_oof_preds = y_train.copy()
mean_oof_preds.loc[:, target_cols] = 0
for i, p in enumerate(oof_preds):
print(f"Repeat {i + 1} OOF Log Loss: {multi_log_loss(y_train, p)}")
 mean_oof_preds.loc[:, target_cols] += p[target_cols]
mean_oof_preds.loc[:, target_cols] /= len(oof_preds)
 print(f"Mean OOF Log Loss: {multi_log_loss(y_train, mean_oof_preds)}")
 mean_oof_preds.loc[x_train['cp_type'] == 0, target_cols] = 0
print(f"Mean OOF Log Loss(ctl adjusted): {multi_log_loss(y_train, mean_oof_preds)}" )<save_to_csv>
earlystopper = EarlyStopping(monitor='val_accuracy', min_delta=0.0001, patience=5, verbose=0, mode='max', baseline=0.995, restore_best_weights=False)
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
 callback = [learning_rate_reduction]
test_preds = sub.copy()
test_preds[target_cols] = 0
for model in models:
test_preds.loc[:,target_cols] += model.predict(x_test)
 test_preds.loc[:,target_cols] /= len(models)
 test_preds.loc[x_test['cp_type'] == 0, target_cols] = 0
test_preds.to_csv('submission.csv', index=False )<import_modules>
history = model.fit(
 X_traine, y_traine,
batch_size=512,
epochs=50,
validation_data=(X_val, y_val),
callbacks=callback
)
print("Tensorflow version " + tf.__version__)
 AUTO = tf.data.experimental.AUTOTUNE<set_options>
layer_outputs = [layer.output for layer in model.layers[:len(model.layers)]]
activation_model = Model(input=model.input, output=layer_outputs)
 activations = activation_model.predict(test_im.reshape(1,28,28,1))
first_layer_activation = activations[0]
plt.matshow(first_layer_activation[0, :, :, 4], cmap='viridis' )
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )<set_options>
model.layers[:-1]
MIXED_PRECISION = False
XLA_ACCELERATE = True
if MIXED_PRECISION:
if tpu: policy = tf.keras.mixed_precision.experimental.Policy('mixed_bfloat16')
 else: policy = tf.keras.mixed_precision.experimental.Policy('mixed_float16')
 mixed_precision.set_policy(policy)
 print('Mixed precision enabled')
 if XLA_ACCELERATE:
tf.config.optimizer.set_jit(True)
 print('Accelerated Linear Algebra enabled' )<load_from_csv>
layer_names = []
for layer in model.layers[:-1]:
layer_names.append(layer.name)
 images_per_row = 16
zipped_layers = zip(layer_names, activations)
 for layer_name, layer_activation in zipped_layers:
if layer_name.startswith('conv'):
n_features = layer_activation.shape[-1]
size = layer_activation.shape[1]
n_cols = n_features // images_per_row
display_grid = np.zeros(( size * n_cols, images_per_row * size))
for col in range(n_cols):
for row in range(images_per_row):
channel_image = layer_activation[0,:, :, col * images_per_row + row]
channel_image -= channel_image.mean()
channel_image /= channel_image.std()
channel_image *= 64
channel_image += 128
channel_image = np.clip(channel_image, 0, 255 ).astype('uint8')
 display_grid[col * size :(col + 1)* size,
row * size :(row + 1)* size] = channel_image
scale = 1./ size
plt.figure(figsize=(scale * display_grid.shape[1],
scale * display_grid.shape[0]))
plt.title(layer_name)
 plt.grid(False)
 plt.imshow(display_grid, aspect='auto', cmap='viridis' )
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 ss = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )<categorify>
layer_names = []
for layer in model.layers[:-1]:
layer_names.append(layer.name)
 images_per_row = 16
for layer_name, layer_activation in zip(layer_names, activations):
if layer_name.startswith('max'):
n_features = layer_activation.shape[-1]
size = layer_activation.shape[1]
n_cols = n_features // images_per_row
display_grid = np.zeros(( size * n_cols, images_per_row * size))
for col in range(n_cols):
for row in range(images_per_row):
channel_image = layer_activation[0,:, :, col * images_per_row + row]
channel_image -= channel_image.mean()
channel_image /= channel_image.std()
channel_image *= 64
channel_image += 128
channel_image = np.clip(channel_image, 0, 255 ).astype('uint8')
 display_grid[col * size :(col + 1)* size,
row * size :(row + 1)* size] = channel_image
scale = 1./ size
plt.figure(figsize=(scale * display_grid.shape[1],
scale * display_grid.shape[0]))
plt.title(layer_name)
 plt.grid(False)
 plt.imshow(display_grid, aspect='auto', cmap='viridis' )
def preprocess(df):
df.loc[:, 'cp_type'] = df.loc[:, 'cp_type'].map({'trt_cp': 0, 'ctl_vehicle': 1})
 df.loc[:, 'cp_dose'] = df.loc[:, 'cp_dose'].map({'D1': 0, 'D2': 1})
 del df['sig_id']
return df
train = preprocess(train_features)
 test = preprocess(test_features)
 del train_targets['sig_id']<choose_model_class>
final_loss, final_acc = model.evaluate(X_val, y_val, verbose=0)
 print("Final loss: {0:.6f}, final accuracy: {1:.6f}".format(final_loss, final_acc))
<define_variables><EOS> 
predictions = model.predict_classes(X_test, verbose=0)
 predictions = model.predict_classes(X_test, verbose=0)
 submission = pd.read_csv(path_sample_sub)
 submission['label'] = predictions
submission.to_csv('submission.csv', index=False )