Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<train_model>
warnings.filterwarnings('ignore')
min_loss = float('inf')
 NUM_EPOCHS = 10
for epoch in range(NUM_EPOCHS):
train_loss, train_acc, train_f1, min_loss = train_one_epoch(model, train_iter, val_iter, optimizer, loss_func, min_loss, scheduler, eval_every)
 print('train_loss', train_loss, 'train_acc', train_acc, 'train_f1', train_f1)
 val_loss, val_acc, val_f1 = get_val_score(model, val_iter, loss_func)
 print('val_loss', val_loss, 'val_acc', val_acc, 'val_f1', val_f1)
 if val_loss < min_loss:
save(m=model, info={'step': 'none', 'epoch': epoch, 'val_loss': val_loss, 'val_f1': val_f1})
 val_loss = min_loss<load_pretrained>
MAIN_DIR = ".. /input/Kannada-MNIST/"
DATA_TRAIN = MAIN_DIR + "train.csv"
DATA_TEST = MAIN_DIR + "test.csv"
IMG_HEIGHT = 28
IMG_WIDTH = 28
CHANNELS = 1
IMG_SHAPE =(IMG_WIDTH, IMG_HEIGHT, CHANNELS)
 sns.set(font_scale=1.3 )
model, m_info = load()
m_info<categorify>
def load_data() :
df_train = pd.read_csv(DATA_TRAIN)
 df_test = pd.read_csv(DATA_TEST)
 return df_train, df_test
val_preds = []
val_labels = []
for batch in val_iter:
question, x_len = batch.text
x = question.cuda()
y = batch.target.type(torch.Tensor ).cuda()
outs = model.forward(x, x_len ).view(-1)
 outs = sigmoid(outs.cpu().data.numpy() ).tolist()
y = y.cpu().data.numpy().tolist()
val_preds += outs
val_labels += y
val_preds = np.array(val_preds)
 val_labels = np.array(val_labels )<prepare_output>
data_train, data_test = load_data()
test_pred, test_id = [], []
for batch in test_iter:
question, x_len = batch.text
x = question.cuda()
outs = model.forward(x, x_len ).view(-1)
 outs = sigmoid(outs.cpu().data.numpy() ).tolist()
test_pred += outs
test_id += batch.qid.view(-1 ).data.numpy().tolist()
sub_df = pd.DataFrame()
sub_df['qid'] = [qid.vocab.itos[i] for i in test_id]
sub_df['prediction'] =(np.array(test_pred)>= best_threshold ).astype(int )<save_to_csv>
print(f"Data train has {data_train.isna().sum().sum(axis=0)} Nan values")
 print(f"Data test has {data_test.isna().sum().sum(axis=0)} Nan values" )
sub_df.to_csv("submission.csv", index=False )<set_options>
class DataGenerator(keras.utils.Sequence):
'Generates data for Keras'
def __init__(self,
data,
list_IDs,
labels=None,
batch_size=32,
dim=IMG_SHAPE,
n_channels=1,
augment=False,
n_classes=10,
mode='fit',
shuffle=True,
random_state=42):
'Initialization'
self.data = data
self.dim = dim
self.batch_size = batch_size
self.labels = labels
self.list_IDs = list_IDs
self.n_channels = n_channels
self.n_classes = n_classes
self.shuffle = shuffle
self.augment = augment
self.mode = mode
self.random_state = random_state
self.on_epoch_end()
def __len__(self):
'Denotes the number of batches per epoch'
if(self.mode == 'fit'):
return int(np.floor(len(self.list_IDs)/ self.batch_size))
else:
return int(np.ceil(len(self.list_IDs)/ self.batch_size))
def __getitem__(self, index):
'Generate one batch of data'
augment_batch = random.choice([True, False])
 indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
list_IDs_batch = [self.list_IDs[k] for k in indexes]
X = self.__generate_X(list_IDs_batch)
 if(self.mode == 'fit'):
y = self.__generate_y(list_IDs_batch)
 if(self.augment and augment_batch):
X = self.__augment_batch(X)
 return X, y
elif self.mode == 'predict':
return X
else:
raise AttributeError('The mode parameter should be set to "fit" or "predict".')
 def on_epoch_end(self):
'Updates indexes after each epoch'
self.indexes = np.arange(len(self.list_IDs))
if self.shuffle == True:
np.random.seed(self.random_state)
 np.random.shuffle(self.indexes)
 def __generate_X(self, list_IDs_batch):
'Generates data containing batch_size samples'
X = np.empty(( len(list_IDs_batch), *self.dim))
for i, ID in enumerate(list_IDs_batch):
X[i,] = np.reshape(self.data[ID], self.dim)
 return X
def __generate_y(self, list_IDs_batch):
y = np.empty(( self.batch_size), dtype=int)
 for i, ID in enumerate(list_IDs_batch):
y[i, ] = self.labels[ID]
return keras.utils.to_categorical(y, num_classes=self.n_classes)
 def __random_transform(self, img):
composition = albu.Compose([
albu.ShiftScaleRotate(rotate_limit=10, shift_limit=0.15, scale_limit=0.1)
 ])
 composed = composition(image=img)
 aug_img = composed['image']
return aug_img
def __augment_batch(self, img_batch):
for i in range(img_batch.shape[0]):
img_batch[i, ] = self.__random_transform(img_batch[i, ])
 return img_batch
np.random.seed(0)
 tqdm.pandas()
np.random.seed(1 )<load_from_csv>
def build_model() :
input_layer = Input(shape=IMG_SHAPE)
 x = Conv2D(32,(3,3), strides=1, padding="same", name="conv1" )(input_layer)
 x = BatchNormalization(name="batch1" )(x)
 x = Activation('relu', name='relu1' )(x)
 x = MaxPooling2D(pool_size=2, strides=2, padding="valid", name="max2" )(x)
 x = Conv2D(32,(3, 3), padding='same', name="conv2" )(x)
 x = BatchNormalization(name="batch2" )(x)
 x = Activation('relu', name='relu2' )(x)
 x = Dropout(0.4, name='dropout1' )(x)
 x = Conv2D(64,(3,3), strides=1, padding="same", name="conv3" )(x)
 x = BatchNormalization(name="batch3" )(x)
 x = Activation('relu', name='relu3' )(x)
 x = MaxPooling2D(pool_size=2, strides=2, padding="valid", name="max3" )(x)
 x = Conv2D(64,(3, 3), padding='same', name="conv4" )(x)
 x = BatchNormalization(name="batch4" )(x)
 x = Activation('relu', name='relu4' )(x)
 x = Dropout(0.4, name='dropout2' )(x)
 x = Conv2D(128,(3,3), strides=1, padding="same", name="conv5" )(x)
 x = BatchNormalization(name="batch5" )(x)
 x = Activation('relu', name='relu5' )(x)
 x = MaxPooling2D(pool_size=2, strides=2, padding="valid", name="max4" )(x)
 x = Conv2D(128,(3, 3), padding='same', name="conv6" )(x)
 x = BatchNormalization(name="batch6" )(x)
 x = Activation('relu', name='relu6' )(x)
 x = Dropout(0.4, name='dropout3' )(x)
 x = Flatten(name='flatten' )(x)
 x = Dense(128, name='dense1' )(x)
 x = BatchNormalization(name="batch7" )(x)
 x = Activation('relu', name="relu7" )(x)
 x = Dropout(0.45, name="dropout4" )(x)
 x = Dense(10, activation='softmax', name="dense2" )(x)
 model = Model(inputs=input_layer, outputs=x)
 model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=1e-3), metrics=['accuracy'])
 return model
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv" )<categorify>
def get_generators(data_train, data_test):
df_train = data_train.copy()
df_test = data_test.copy()
train_idx, val_idx = train_test_split(
 df_train.index, random_state=42, test_size=0.1, shuffle=True 
)
 params = {'dim': IMG_SHAPE,
'batch_size': 32,
'n_classes': 10,
'n_channels': 1}
X = df_train.drop(['label'], axis=1 ).values
y = df_train['label'].values
training_generator = DataGenerator(data=X,
labels=y,
list_IDs=train_idx,
**params,
mode='fit',
shuffle=True, augment=False)
 validation_generator = DataGenerator(data=X,
labels=y,
list_IDs=val_idx,
**params,
mode='fit',
shuffle=True, augment=False)
 X_test = df_test.drop(['id'], axis=1 ).values
test_generator = DataGenerator(data=X_test,
list_IDs=df_test.index.values,
**params,
mode='predict',
shuffle=False)
 return training_generator, validation_generator, test_generator
def clean_text(x):
x = str(x)
 for punct in "/-'":
x = x.replace(punct, ' ')
 for punct in '&':
x = x.replace(punct, f' {punct} ')
 for punct in '?!.,"
x = x.replace(punct, '')
 return x<feature_engineering>
def train_model(gen_train, gen_val):
model = build_model()
cbs = [ReduceLROnPlateau(monitor='loss',
factor=0.5,
patience=1,
min_lr=1e-5,
verbose=0,
skip_mismatch=True),
EarlyStopping(monitor='val_loss',
min_delta=1e-6,
patience=10,
verbose=1,
mode='auto',
restore_best_weights=True)]
model.summary()
history = model.fit_generator(gen_train,
epochs=50,
validation_data=gen_val,
validation_steps=len(gen_val),
shuffle=True,
callbacks=cbs,
verbose=1)
 return model, history
train_df["question_text"] = train_df["question_text"].progress_apply(lambda x: clean_text(x))
test_df["question_text"] = test_df["question_text"].progress_apply(lambda x: clean_text(x))<split>
train_gen, val_gen, test_gen = get_generators(data_train, data_test)
 model, history = train_model(train_gen, val_gen)
 y_pred = model.predict_generator(test_gen, use_multiprocessing=True )
train_df, val_df = train_test_split(train_df, test_size=0.1, random_state=2018 )<string_transform>
def print_results(history):
print("ACCURACY :")
 print(f"Training accuracy : {history.history['accuracy'][-1]}")
 print(f"Validation accuracy : {history.history['val_accuracy'][-1]}")
 print("
LOSS :")
 print(f"Training categorical crossentropy loss : {history.history['loss'][-1]}")
 print(f"Validation categorical crossentropy loss : {history.history['val_loss'][-1]}" )
def get_synonyms(word, pos=None):
wordnet_pos = {
"NN": wordnet.NOUN,
"VB": wordnet.VERB,
"VBD": wordnet.VERB,
"VBG": wordnet.VERB,
"VBN": wordnet.VERB,
"VBP": wordnet.VERB,
"JJ": wordnet.ADJ,
"RB": wordnet.ADV,
"RBR": wordnet.ADV,
"RBS": wordnet.ADV,
}
if pos:
if pos in list(wordnet_pos.keys()):
synsets = wordnet.synsets(word, pos=wordnet_pos[pos])
 synonyms = []
for synset in synsets:
synonyms += [str(lemma.name())for lemma in synset.lemmas() ]
synonyms = [synonym.replace("_", " ")for synonym in synonyms]
synonyms = list(set(synonyms))
synonyms = [synonym for synonym in synonyms if synonym != word]
if synonyms:
return synonyms[0]
return ''
def get_syn_sentence(text):
words = text.split()
words_with_pos_tag = pos_tag(words)
 words_with_pos_tag
new_sentence_words = []
for word, pos in words_with_pos_tag:
synonym = get_synonyms(word, pos)
 if synonym:
new_sentence_words.append(synonym)
 else:
new_sentence_words.append(word)
 synonym_sentence = ' '.join(new_sentence_words)
 return synonym_sentence<filter>
data_test_label = data_test.copy()
y_pred_label = np.argmax(y_pred, axis=1)
 data_test_label['label'] = y_pred_label
df_obscene = train_df[train_df['target'] == 1]
df_obscene = df_obscene.reset_index()<feature_engineering>
def create_submission(data):
df = pd.DataFrame()
df['id'] = data['id']
df['label'] = data['label']
return df
sentence_with_synonyms = []
for idx, row in df_obscene.iterrows() :
sentence_with_synonyms.append(get_syn_sentence(row['question_text']))
df_obscene['question_text'] = sentence_with_synonyms 
<concatenate>
submission = create_submission(data_test_label)
 submission.to_csv('submission.csv', index=False )