Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<set_options>
import matplotlib.pyplot as plt,seaborn as sns,pandas as pd,numpy as np
from keras.models import Sequential, load_model
from keras.layers.core import Dense, Dropout, Activation
from keras.layers import Conv2D, MaxPooling2D,MaxPool2D,Flatten,BatchNormalization
from keras.utils import np_utils
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
from keras.optimizers import Adam
import tensorflow as tf
from sklearn.model_selection import train_test_split
spark.stop()<set_options>
train_data = pd.read_csv('.. /input/Kannada-MNIST/train.csv' )
for name in dir() :
if not name.startswith('_'):
del globals() [name]
gc.collect()<load_from_csv>
x_train = train_data.drop(labels = ["label"],axis = 1)
 y_train = train_data["label"]
train = pd.read_csv(".. /input/real-or-not-data-cleaned/train.csv")
 test = pd.read_csv(".. /input/real-or-not-data-cleaned/test.csv" )<set_options>
x_train = x_train.values.astype('float32')/ 255
if torch.cuda.is_available() :
device = torch.device("cuda")
 print('There are %d GPU(s)available.' % torch.cuda.device_count())
 print('We will use the GPU:', torch.cuda.get_device_name(0))
else:
print('No GPU available, using the CPU instead.')
 device = torch.device("cpu" )<load_pretrained>
x_train, x_test, y_train, y_test = train_test_split(x_train, y_train, test_size = 0.2, random_state=42 )
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True )<categorify>
X_train = x_train.reshape(-1, 28, 28,1)
 X_test = x_test.reshape(-1, 28, 28,1)
 print("Train matrix shape", X_train.shape)
 print("Test matrix shape", X_test.shape )
def tokenize_map(sentence, labs='None'):

 global labels
input_ids = []
attention_masks = []
for text in sentence:
encoded_dict = tokenizer.encode_plus(
 text,
add_special_tokens = True,
truncation='longest_first',
max_length = 64,
pad_to_max_length = True,
return_attention_mask = True,
return_tensors = 'pt', 
)
 input_ids.append(encoded_dict['input_ids'])
 attention_masks.append(encoded_dict['attention_mask'])
 input_ids = torch.cat(input_ids, dim=0)
 attention_masks = torch.cat(attention_masks, dim=0)
 if labs != 'None':
labels = torch.tensor(labels)
 return input_ids, attention_masks, labels
else:
return input_ids, attention_masks<define_variables>
n_classes = 10
print("Shape before one-hot encoding: ", y_train.shape)
 Y_train = np_utils.to_categorical(y_train, n_classes)
 Y_test = np_utils.to_categorical(y_test, n_classes)
 print("Shape after one-hot encoding: ", Y_train.shape )
train_sentences = train.clean_text.values
labels = train.target.values
test_sentences = test.clean_text.values
input_ids, attention_masks, labels = tokenize_map(train_sentences, labels)
 test_input_ids, test_attention_masks= tokenize_map(test_sentences )<split>
model = Sequential()
model.add(Conv2D(32, kernel_size = 3, activation='relu', input_shape =(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(128, kernel_size = 4, activation='relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
 adam = Adam(lr=5e-4)
 model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer=adam )
dataset = TensorDataset(input_ids, attention_masks, labels)
 train_size = int(0.75 * len(dataset))
val_size = len(dataset)- train_size
train_dataset, val_dataset = random_split(dataset, [train_size, val_size] )<load_pretrained>
reduce_lr = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.2,
min_lr=1e-6 )
train_dataloader = DataLoader(
 train_dataset,
sampler = RandomSampler(train_dataset),
batch_size = 32 
)
 validation_dataloader = DataLoader(
 val_dataset,
sampler = SequentialSampler(val_dataset),
batch_size = 32
)<load_pretrained>
datagen = ImageDataGenerator(
 rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1)
 history = datagen.fit(X_train )
model = BertForSequenceClassification.from_pretrained(
 'bert-large-uncased',
num_labels = 2,
output_attentions = False,
output_hidden_states = False 
)
 model.to(device )<set_options>
history = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=100), steps_per_epoch=len(X_train)/100,
epochs=20, validation_data=(X_test, Y_test), callbacks=[reduce_lr] )
gc.collect()<init_hyperparams>
score = model.evaluate(X_test, Y_test, verbose=0)
 print('Test loss:', score[0])
 print('Test accuracy:', score[1] )
EPOCHS = 4
optimizer = AdamW(model.parameters() ,
lr = 6e-6,
eps = 1e-8 
)
 total_steps = len(train_dataloader)* EPOCHS
scheduler = get_linear_schedule_with_warmup(optimizer,
num_warmup_steps = 0,
num_training_steps = total_steps )<create_dataframe>
test_data = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 id_ = test_data.id
test_data = test_data.drop("id",axis="columns")
 test_data = test_data.values.reshape(-1, 28, 28,1)
 test_data = test_data.astype('float32')
 test_data /= 255
print("Test data matrix shape", test_data.shape )
prediction_data = TensorDataset(test_input_ids, test_attention_masks)
 prediction_dataloader = DataLoader(prediction_data,
sampler=SequentialSampler(prediction_data),
batch_size=64 )<predict_on_test>
y_pred = model.predict_classes(test_data, verbose=0)
 print(y_pred )
model.eval()
predictions = []
for batch in prediction_dataloader:
batch = tuple(t.to(device)for t in batch)
 b_input_ids, b_input_mask, = batch
with torch.no_grad() :
outputs = model(b_input_ids, token_type_ids=None,
attention_mask=b_input_mask)
 logits = outputs[0]
logits = logits.detach().cpu().numpy()
predictions.append(logits )<load_from_csv>
i = 7
predicted_value = np.argmax(model.predict(X_test[i].reshape(1,28, 28,1)))
 print('predicted value:',predicted_value)
 plt.imshow(X_test[i].reshape([28, 28]), cmap='Greys_r' )
<save_to_csv><EOS> 
sample_sub=pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 sample_sub['label']=y_pred
sample_sub.to_csv('submission.csv',index=False )