Kaggle competition: Kannada MNIST
display_batch_of_images(next(test_batch))<choose_model_class>
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
with strategy.scope() :
dn201 = tf.keras.applications.DenseNet201(weights='imagenet',
include_top=False,
input_shape=[*IMAGE_SIZE, 3])
 dn201.trainable = True
model1 = tf.keras.Sequential([
dn201,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ])
 model1.compile(
 optimizer = tf.keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False),
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
 model1.summary()<choose_model_class>
train = pd.read_csv("/kaggle/input/Kannada-MNIST/train.csv")
 train.head()
with strategy.scope() :
enb7 = efn.EfficientNetB7(weights='noisy-student', include_top=False, input_shape=[*IMAGE_SIZE, 3])
 enb7.trainable = True
model2 = tf.keras.Sequential([
enb7,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ])
 model2.compile(
 optimizer = tf.keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False),
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
 model2.summary()<randomize_order>
test = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv")
 test.head()
LR_START = 0.0001
LR_MAX = 0.00005 * strategy.num_replicas_in_sync
LR_MIN = 0.00001
LR_RAMPUP_EPOCHS = 4
LR_SUSTAIN_EPOCHS = 6
LR_EXP_DECAY =.8
def lrfn(epoch):
if epoch < LR_RAMPUP_EPOCHS:
lr = np.random.random_sample() * LR_START
elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:
lr = LR_MAX
else:
lr =(LR_MAX - LR_MIN)* LR_EXP_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS)+ LR_MIN
return lr
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True )<choose_model_class>
sample_submission = pd.read_csv("/kaggle/input/Kannada-MNIST/sample_submission.csv")
 sample_submission.head()
es_callback = tf.keras.callbacks.EarlyStopping(min_delta=0, patience=10, verbose=1, mode='auto', restore_best_weights=True )<train_model>
images = train.iloc[:, 1:].values.reshape(train.shape[0],28, 28 ).astype('float32')/255.0
images=images.reshape(train.shape[0],28, 28,1)
 images_test = test.iloc[:, 1:].values.reshape(test.shape[0],28, 28 ).astype('float32')/255.0
images_test=images_test.reshape(test.shape[0],28, 28,1)
 label = train.iloc[:,0].astype('int' ).values
ids_test = test.iloc[:,0].values
plt.imshow(images[0].reshape(28,28),cmap='gray')
 plt.title(label[0] )
if VALIDATION:
history1 = model1.fit(get_training_dataset() , steps_per_epoch=STEPS_PER_EPOCH, epochs=40, validation_data=get_validation_dataset() , callbacks = [lr_callback, es_callback])
 else:
history1 = model1.fit(get_training_dataset() , steps_per_epoch=STEPS_PER_EPOCH, epochs=30, callbacks = [lr_callback])
<train_model>
images_train, images_validation,label_train,label_validation = train_test_split(images,label,test_size=0.1 )
if VALIDATION:
history2 = model2.fit(get_training_dataset() , steps_per_epoch=STEPS_PER_EPOCH, epochs=40, validation_data=get_validation_dataset() , callbacks = [lr_callback, es_callback])
 else:
history2 = model2.fit(get_training_dataset() , steps_per_epoch=STEPS_PER_EPOCH, epochs=30, callbacks = [lr_callback] )<load_pretrained>
label_train = to_categorical(label_train )
model1.save_weights('densenet-201-ver8.h5')
 model2.save_weights('efficentnetb7-ver8.h5' )<predict_on_test>
label_validation = to_categorical(label_validation )
if VALIDATION:
cmdataset = get_validation_dataset(ordered=True)
 images_ds = cmdataset.map(lambda image, label: image)
 labels_ds = cmdataset.map(lambda image, label: label ).unbatch()
cm_correct_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES)) ).numpy()
cm_probabilities =(model1.predict(images_ds)*0.3)+model2.predict(images_ds)*0.7
cm_predictions = np.argmax(cm_probabilities, axis=-1)
 print("Correct labels: ", cm_correct_labels.shape, cm_correct_labels)
 print("Predicted labels: ", cm_predictions.shape, cm_predictions )<compute_test_metric>
from tensorflow.python.keras.applications.vgg16 import VGG16
from tensorflow.keras.layers import Dense,Conv2D, MaxPooling2D, Flatten,BatchNormalization,Dropout
from tensorflow.keras.models import Sequential
if VALIDATION:
cmat = confusion_matrix(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)))
 score = f1_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 precision = precision_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 recall = recall_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 display_confusion_matrix(cmat, score, precision, recall)
 print('f1 score: {:.3f}, precision: {:.3f}, recall: {:.3f}'.format(score, precision, recall))<predict_on_test>
model = Sequential()
model.add(Conv2D(32,(3, 3), input_shape=(28, 28, 1), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D())
 model.add(Dropout(0.4))
model.add(Conv2D(64,(3, 3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D())
 model.add(Dropout(0.4))
model.add(Conv2D(64,(3, 3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D())
 model.add(Dropout(0.4))
model.add(Conv2D(64,(3, 3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D())
 model.add(Dropout(0.4))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='sigmoid'))
model.summary()
test_ds = get_test_dataset(ordered=True)
 print('Computing predictions...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities =(model1.predict(test_images_ds)+model2.predict(test_images_ds)) /2
predictions = np.argmax(probabilities, axis=-1)
 print(predictions)
 print('Generating submission.csv file...')
 test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions]), fmt=['%s', '%d'], delimiter=',', header='id,label', comments='')
 !head submission.csv<define_variables>
earlystop = EarlyStopping(patience=10)
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=2,
verbose=1,
factor=0.5,
min_lr=0.00001)
 callbacks = [earlystop, learning_rate_reduction,ModelCheckpoint(filepath='best_model.h5', monitor='val_loss', save_best_only=True)]
if VALIDATION:
dataset = get_validation_dataset()
dataset = dataset.unbatch().batch(20)
 batch = iter(dataset )<predict_on_test>
model.compile(
 loss="categorical_crossentropy",
optimizer=Adam(lr=0.001),
metrics=['accuracy'] )
if VALIDATION:
images, labels = next(batch)
 probabilities =(model1.predict(images)*0.3)+model2.predict(images)*0.7
predictions = np.argmax(probabilities, axis=-1)
 display_batch_of_images(( images, labels), predictions )<import_modules>
model.fit(images_train,label_train, validation_data=(images_validation,label_validation),batch_size=64, epochs=50,callbacks=callbacks )
import re
import numpy as np
import math
import pandas as pd
import random
import tensorflow.keras.backend as K
from kaggle_datasets import KaggleDatasets
from matplotlib import pyplot as plt
import gc
from sklearn.metrics import f1_score, precision_score, recall_score, confusion_matrix<import_modules>
test_prediction = model.predict(images_test )
print("Tensorflow version " + tf.__version__ )<normalization>
test_labels = []
for i in range(len(test_prediction)) :
test_labels.append(np.argmax(test_prediction[i]))
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = tf.reshape(tf.concat([c1,s1,zero, -s1,c1,zero, zero,zero,one],axis=0),[3,3])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = tf.reshape(tf.concat([one,s2,zero, zero,c2,zero, zero,zero,one],axis=0),[3,3])
 zoom_matrix = tf.reshape(tf.concat([one/height_zoom,zero,zero, zero,one/width_zoom,zero, zero,zero,one],axis=0),[3,3])
 shift_matrix = tf.reshape(tf.concat([one,zero,height_shift, zero,one,width_shift, zero,zero,one],axis=0),[3,3])
 return K.dot(K.dot(rotation_matrix, shear_matrix), K.dot(zoom_matrix, shift_matrix))<define_search_space>
for i in range(len(sample_submission)) :
sample_submission.iloc[i,1]=test_labels[i]
def transform(image,label):
DIM = IMAGE_SIZE[0]
XDIM = DIM%2
rot = 15.* tf.random.normal([1],dtype='float32')
 shr = 5.* tf.random.normal([1],dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1],dtype='float32')/10.
 w_zoom = 1.0 + tf.random.normal([1],dtype='float32')/10.
 h_shift = 16.* tf.random.normal([1],dtype='float32')
 w_shift = 16.* tf.random.normal([1],dtype='float32')
 m = get_mat(rot,shr,h_zoom,w_zoom,h_shift,w_shift)
 x = tf.repeat(tf.range(DIM//2,-DIM//2,-1), DIM)
 y = tf.tile(tf.range(-DIM//2,DIM//2),[DIM])
 z = tf.ones([DIM*DIM],dtype='int32')
 idx = tf.stack([x,y,z])
 idx2 = K.dot(m,tf.cast(idx,dtype='float32'))
idx2 = K.cast(idx2,dtype='int32')
 idx2 = K.clip(idx2,-DIM//2+XDIM+1,DIM//2)
 idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])
 d = tf.gather_nd(image,tf.transpose(idx3))
return tf.reshape(d,[DIM,DIM,3]),label<set_options>
np.argmax(model.predict(images_test[2].reshape(1,28,28,1)) )
<choose_model_class><EOS> 
sample_submission.to_csv("submission.csv",index=False )