Kaggle competition: Kannada MNIST
test_ds = get_test_dataset(ordered=True)
 print('Computing predictions...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 enet_probabilities = enet_model.predict(test_images_ds)
 enet_predictions = np.argmax(enet_probabilities, axis=-1)
 print(enet_predictions)
<save_to_csv>
train = pd.read_csv(".. /input/Kannada-MNIST/train.csv")
 test = pd.read_csv(".. /input/Kannada-MNIST/test.csv")
 submission = pd.read_csv(".. /input/Kannada-MNIST/sample_submission.csv" )
<choose_model_class>
X = train.drop(['label'], axis = 1)
 X_valid = test.drop(['id'], axis = 1 )
def call_model_rn152v2() :
rnet = ResNet152V2(
 input_shape=(512,512,3),
weights = 'imagenet',
include_top=False 
)
 rnet.trainable = True
model = tf.keras.Sequential([
rnet,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ])
 model.compile(
 optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
 return model 
<train_model>
def haar(block):
a = pywt.dwt2(block, 'db1')
 return a
with strategy.scope() :
rnet_model = call_model_rn152v2()
rnet_model.summary()
history_resnet = rnet_model.fit(
 get_training_dataset(load_dataset(TRAINING_FILENAMES),do_aug=False),
steps_per_epoch=STEPS_PER_EPOCH,
epochs=EPOCHS,
callbacks = [lr_callback],
validation_data = get_validation_dataset() ,
verbose=2 
)
<compute_train_metric>
Y = train['label'].values
X_exp = []
for i in tnrange(train.shape[0]):
im = train.iloc[i][train.columns[1:]].values.reshape(( 28,28))
a,(b,c,d)= haar(im)
 newim = np.zeros(( 14,14,4))
newim[:,:,0] = a
newim[:,:,1] = b
newim[:,:,2] = c
newim[:,:,3] = d
X_exp.append(newim)
 X = np.array(X_exp)
 X_exp = []
for i in tnrange(test.shape[0]):
im = test.iloc[i][test.columns[1:]].values.reshape(( 28,28))
a,(b,c,d)= haar(im)
 newim = np.zeros(( 14,14,4))
newim[:,:,0] = a
newim[:,:,1] = b
newim[:,:,2] = c
newim[:,:,3] = d
X_exp.append(newim)
 X_valid = np.array(X_exp)
cmdataset = get_validation_dataset(ordered=True)
 images_ds = cmdataset.map(lambda image, label: image)
 labels_ds = cmdataset.map(lambda image, label: label ).unbatch()
rm_correct_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES)) ).numpy()
rm_probabilities = rnet_model.predict(images_ds)
 rm_predictions = np.argmax(rm_probabilities, axis=-1)
 print("Correct labels: ", rm_correct_labels.shape, cm_correct_labels)
 print("Predicted labels: ", rm_predictions.shape, cm_predictions)
 rmat = confusion_matrix(rm_correct_labels, rm_predictions, labels=range(len(CLASSES)))
 score = f1_score(rm_correct_labels, rm_predictions, labels=range(len(CLASSES)) , average='macro')
 precision = precision_score(rm_correct_labels, rm_predictions, labels=range(len(CLASSES)) , average='macro')
 recall = recall_score(rm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 rmat =(rmat.T / rmat.sum(axis=1)).T
display_confusion_matrix(rmat, score, precision, recall)
 print('f1 score: {:.3f}, precision: {:.3f}, recall: {:.3f}'.format(score, precision, recall)) 
<predict_on_test>
X_train, X_dev, Y_train, Y_dev = train_test_split(X, Y, test_size = 0.2 )
test_ds = get_test_dataset(ordered=True)
 print('Computing predictions...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 rnet_probabilities = rnet_model.predict(test_images_ds)
 rnet_predictions = np.argmax(rnet_probabilities, axis=-1)
 print(rnet_predictions)
<choose_model_class>
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(64,(3,3), padding='same', input_shape=(14, 14, 4)) ,
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Conv2D(64,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Conv2D(64,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(128,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Conv2D(128,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Conv2D(128,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(256,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Conv2D(256,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(256),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(10, activation='softmax')
 ])
 print(model.summary())
def call_model_IRv2() :
irnet = InceptionResNetV2(
 input_shape=(512,512,3),
weights = 'imagenet',
include_top=False 
)
 irnet.trainable = True
model = tf.keras.Sequential([
irnet,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ])
 model.compile(
 optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
 return model 
<train_model>
initial_learningrate=0.001
model.compile(optimizer=
RMSprop(lr=initial_learningrate),
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'] )
with strategy.scope() :
irnet_model = call_model_IRv2()
irnet_model.summary()
history_incepresnet = irnet_model.fit(
 get_training_dataset(load_dataset(TRAINING_FILENAMES),do_aug=False),
steps_per_epoch=STEPS_PER_EPOCH,
epochs=EPOCHS,
callbacks = [lr_callback],
validation_data = get_validation_dataset() ,
verbose=2 
)
<compute_train_metric>
lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='accuracy',
patience=300,
verbose=1,
factor=0.5,
min_lr=0.00001 )
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 = irnet_model.predict(images_ds)
 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)
 cmat = confusion_matrix(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)))
 rscore = f1_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 rprecision = precision_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 rrecall = recall_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 cmat =(cmat.T / cmat.sum(axis=1)).T
display_confusion_matrix(cmat, rscore, rprecision, rrecall)
 print('f1 score: {:.3f}, precision: {:.3f}, recall: {:.3f}'.format(rscore, rprecision, rrecall)) 
<predict_on_test>
def lr_decay(epoch, initial_learningrate = 0.001):
return initial_learningrate * 0.99 ** epoch
test_ds = get_test_dataset(ordered=True)
 print('Computing predictions...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 irnet_probabilities = irnet_model.predict(test_images_ds)
 irnet_predictions = np.argmax(irnet_probabilities, axis=-1)
 print(irnet_predictions)
<import_modules>
batchsize = 200
epoch = 45
train_datagen = ImageDataGenerator(
 rotation_range=10,
width_shift_range=0.25,
height_shift_range=0.25,
shear_range=0.1,
zoom_range=0.25,
horizontal_flip=False)
 valid_datagen = ImageDataGenerator(
 horizontal_flip=False,
rotation_range=15,
width_shift_range=0.25,
height_shift_range=0.25,
shear_range=0.15,
zoom_range=0.25, 
)
 callback = tf.keras.callbacks.EarlyStopping(monitor='accuracy', patience=5)
 history = model.fit_generator(train_datagen.flow(X_train, Y_train, batch_size = batchsize),
steps_per_epoch = 100,
epochs = epoch,
callbacks=[callback,
LearningRateScheduler(lr_decay),
lr
],
validation_data=valid_datagen.flow(X_dev, Y_dev),
validation_steps=50,
)
from tensorflow.keras.applications import DenseNet201<choose_model_class>
yhat = model.predict(X_valid ).argmax(axis=1)
 submission['label']=pd.Series(yhat)
 submission.to_csv('submission.csv',index=False )
<train_model><EOS> 
model.save('my_model.h5' )