Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<define_variables>
plt.style.use('ggplot')
 %matplotlib inline
display_batch_of_images(next(test_batch))<choose_model_class>
test = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 train = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 dig_df = pd.read_csv('.. /input/Kannada-MNIST/Dig-MNIST.csv')
 sample_df = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv' )
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>
X = train.drop('label', axis=1 ).to_numpy()
y = train['label'].to_numpy()
X_test = test.drop('id', axis=1 ).to_numpy()
X_dig = dig_df.drop('label', axis=1 ).to_numpy()
y_dig = dig_df['label'].to_numpy()
X = X.reshape(-1, 28, 28, 1)
 y = to_categorical(y)
 X_test = X_test.reshape(-1, 28, 28, 1)
 X_dig = X_dig.reshape(-1, 28, 28, 1)
 X = X / 255
X_dig = X_dig / 255
X_test = X_test / 255
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.15, random_state=42 )
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>
train_datagen = ImageDataGenerator(rescale=1.0,
rotation_range=10,
width_shift_range=0.25,
height_shift_range=0.25,
shear_range=0.1,
zoom_range=0.25,
horizontal_flip=False )
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>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience=7,
verbose=1,
factor=0.1,
min_lr=1e-8 )
es_callback = tf.keras.callbacks.EarlyStopping(min_delta=0, patience=10, verbose=1, mode='auto', restore_best_weights=True )<train_model>
def build_norm_model() :
inputs = layers.Input(shape=(28, 28, 1))
x = layers.Conv2D(filters=64, kernel_size=(5, 5), strides=(1, 1), padding='same', input_shape=(28, 28, 1))(inputs)
 x = layers.LeakyReLU(alpha=0.3 )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1))(x)
 x = layers.LeakyReLU(alpha=0.3 )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1))(x)
 x = layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)
 x = layers.LeakyReLU(alpha=0.3 )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Conv2D(filters=256, kernel_size=(3, 3), strides=(1, 1))(x)
 x = layers.LeakyReLU(alpha=0.3 )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Dropout(0.5 )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Conv2D(filters=256, kernel_size=(3, 3), strides=(1, 1))(x)
 x = layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)
 x = layers.LeakyReLU(alpha=0.3 )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Flatten()(x)
 x = layers.Dense(units=256 )(x)
 x = layers.LeakyReLU(alpha=0.3 )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Dropout(0.5 )(x)
 output = layers.Dense(10, activation='softmax' )(x)
 return Model(inputs=inputs, outputs=output )
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>
def make_prediction(model, x):
y_pred = model.predict(x)
 return np.argmax(y_pred, axis=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])
<predict_on_test>
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)+model2.predict(images_ds)) /2
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>
final_model = build_norm_model()
final_model.compile(optimizer=optimizers.RMSprop(lr=0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])
 history_final = final_model.fit_generator(train_datagen.flow(X_train, y_train, batch_size=1024),
steps_per_epoch=100,
epochs=120,
validation_data=(X_valid, y_valid),
callbacks=[learning_rate_reduction],
verbose=1 )
<predict_on_test><EOS> 
y_result = make_prediction(final_model, X_test)
 sample_df['label'] = y_result
sample_df.to_csv('submission.csv',index=False )