Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<install_modules>
numpy.set_printoptions(threshold=sys.maxsize)
!pip install -q efficientnet<install_modules>
train= pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 test= pd.read_csv('.. /input/Kannada-MNIST/test.csv', index_col='id')
 print('Train shape: ', train.shape)
 print('Train missing vals: ', train.isna().sum().sum())
 print('Test shape: ', test.shape)
 print('Test missing vals: ', test.isna().sum().sum() )
!pip install tensorflow_addons<import_modules>
x_train= train.iloc[:, 1:].values
y_train= train.iloc[:, 0].values
x_test= test.iloc[:, :].values
print("Tensorflow version " + tf.__version__)
<set_options>
x_train= x_train.reshape(train.shape[0], 28, 28, 1)
 x_test= x_test.reshape(test.shape[0], 28, 28, 1)
 y_train= to_categorical(y_train, 10)
 x_train, x_valid, y_train, y_valid= train_test_split(x_train, y_train, test_size= 0.2, random_state= 42 )
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 )<define_variables>
GCS_DS_PATH = KaggleDatasets().get_gcs_path('flower-classification-with-tpus')
 IMAGE_SIZE = [512, 512]
img_size=512
EPOCHS = 12
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
GCS_PATH_SELECT = {
192: GCS_DS_PATH + '/tfrecords-jpeg-192x192',
224: GCS_DS_PATH + '/tfrecords-jpeg-224x224',
331: GCS_DS_PATH + '/tfrecords-jpeg-331x331',
512: GCS_DS_PATH + '/tfrecords-jpeg-512x512'
}
GCS_PATH = GCS_PATH_SELECT[IMAGE_SIZE[0]]
TRAINING_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/train/*.tfrec')
 VALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec' )<randomize_order>
num_classes= 10
lr= 0.001
batch_size= 1024
model= Sequential()
model.add(Conv2D(32, kernel_size=3, padding='same', activation='relu', input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size=3, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size=5, padding='same', strides= 2, activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.4))
model.add(Conv2D(64, kernel_size= 3, padding='same', activation= 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size= 3, padding='same', activation= 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size= 5, padding= 'same', strides= 2, activation= 'relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.4))
model.add(Flatten())
 model.add(Dense(128))
model.add(BatchNormalization())
 model.add(Dense(10, activation='softmax'))
model.summary()
optimizer = RMSprop(lr=lr)
 model.compile(loss='categorical_crossentropy', optimizer= optimizer, metrics= ['accuracy'] )
SKIP_VALIDATION = True
if SKIP_VALIDATION:
TRAINING_FILENAMES = TRAINING_FILENAMES + VALIDATION_FILENAMES
else:
random.shuffle(VALIDATION_FILENAMES)
 TRAINING_FILENAMES = TRAINING_FILENAMES + VALIDATION_FILENAMES[:len(VALIDATION_FILENAMES)//2]
VALIDATION_FILENAMES = VALIDATION_FILENAMES[len(VALIDATION_FILENAMES)//2:]<define_variables>
img_gen= ImageDataGenerator(rotation_range= 5, zoom_range= 0.5, horizontal_flip= 0, vertical_flip= 0,
width_shift_range= 2, height_shift_range= 2, rescale= 1/255)
 valid_img_gen = ImageDataGenerator(rescale=1./255.)
learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss', patience=200, verbose=1, factor=0.2)
 es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=200)
 model.fit_generator(img_gen.flow(x_train, y_train, batch_size= batch_size), steps_per_epoch= 100, epochs= 100,
validation_data= valid_img_gen.flow(x_valid, y_valid, batch_size= batch_size), validation_steps= 50,
callbacks=[learning_rate_reduction, es], verbose= 1 )
<categorify><EOS> 
pred= model.predict_classes(x_test/255)
 sub= pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 sub['label']= pred
sub.to_csv('submission.csv', index= False )