Kaggle competition: Kannada MNIST
def load_embeddings(path):
with open(path)as f:
return dict(get_coefs(*line.strip().split(' ')) for line in f )<categorify>
import shutil
import cv2
import re
from fastai import *
from fastai.vision import *
from fastai.metrics import error_rate
def build_matrix(word_index, path):
embedding_index = load_embeddings(path)
 embedding_matrix = np.zeros(( len(word_index)+ 1, 300))
for word, i in word_index.items() :
try:
embedding_matrix[i] = embedding_index[word]
except KeyError:
pass
return embedding_matrix<concatenate>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
embedding_matrix = np.concatenate(
 [build_matrix(tokenizer.word_index, f)for f in EMBEDDING_FILES], axis=-1 )<set_options>
bs = 64
del tokenizer
gc.collect()<define_variables>
path = Path('/kaggle/input/Kannada-MNIST')
 train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test =pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')
 sub = pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')
 dig = pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv' )
checkpoint_predictions = []
weights = []<choose_model_class>
print(train.isna().sum().sum());print(test.isna().sum().sum());print(dig.isna().sum().sum() )
def build_model(embedding_matrix, num_aux_targets):
words = Input(shape=(MAX_LEN,))
x = Embedding(*embedding_matrix.shape, weights=[embedding_matrix], trainable=False )(words)
 x = SpatialDropout1D(0.3 )(x)
 x = Bidirectional(CuDNNLSTM(LSTM_UNITS, return_sequences=True))(x)
 x = Bidirectional(CuDNNLSTM(LSTM_UNITS, return_sequences=True))(x)
 hidden = concatenate([
GlobalMaxPooling1D()(x),
GlobalAveragePooling1D()(x),
])
 hidden = add([hidden, Dense(DENSE_HIDDEN_UNITS, activation='relu' )(hidden)])
 hidden = add([hidden, Dense(DENSE_HIDDEN_UNITS, activation='relu' )(hidden)])
 result = Dense(1, activation='sigmoid' )(hidden)
 aux_result = Dense(num_aux_targets, activation='sigmoid' )(hidden)
 model = Model(inputs=words, outputs=[result, aux_result])
 model.compile(loss='binary_crossentropy', optimizer='adam')
 return model<train_model>
def save_images(outpath,df):
for index, row in df.iterrows() :
pixels=np.asarray(row['pixel0':'pixel783'])
 img=pixels.reshape(( 28,28))
pathname=os.path.join(outpath,'image_'+str(row['label'])+'_'+str(index)+'.jpg')
 cv2.imwrite(pathname,img )
for model_idx in range(NUM_MODELS):
model = build_model(embedding_matrix, y_aux_train.shape[-1])
 for global_epoch in range(EPOCHS):
model.fit(
 x_train,
[y_train, y_aux_train],
batch_size=BATCH_SIZE,
epochs=1,
verbose=2,
callbacks=[
LearningRateScheduler(lambda epoch: 1e-3 *(0.6 ** global_epoch))
] 
)
 checkpoint_predictions.append(model.predict(x_test, batch_size=2048)[0].flatten())
 weights.append(2 ** global_epoch)
 predictions = np.average(checkpoint_predictions, weights=weights, axis=0 )<save_to_csv>
outpath = '/kaggle/output/train'
save_images(outpath,train )
submission = pd.DataFrame.from_dict({
'id': test['id'],
'prediction': predictions
})
 submission.to_csv('submission.csv', index=False )<set_options>
path_img = Path(outpath)
 fnames = get_image_files(path_img)
 fnames[:5]
tqdm.pandas() 
<define_variables>
pat = r'/image_(\d+)_'
data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms() , size=224, bs=bs, num_workers=4 ).normalize(imagenet_stats )
TEXT_COL = 'comment_text'
VECS_PATH = Path('.. /input/fasttext-crawl-300d-2m/crawl-300d-2M.vec')
 TRAIN_DATA = '.. /input/jigsaw-unintended-bias-in-toxicity-classification/train.csv'
TEST_DATA = '.. /input/jigsaw-unintended-bias-in-toxicity-classification/test.csv'<set_options>
data.show_batch(rows=5, figsize=(15,8))
def seed_torch(seed=1029):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True<define_variables>
learn1 = cnn_learner(data, models.resnet34, metrics=[error_rate, accuracy], model_dir = '/kaggle/output/model')
 learn2 = cnn_learner(data, models.resnet50, metrics=[error_rate, accuracy], model_dir = '/kaggle/output/model' )
misspell_dict = {"aren't": "are not", "can't": "cannot", "couldn't": "could not",
"didn't": "did not", "doesn't": "does not", "don't": "do not",
"hadn't": "had not", "hasn't": "has not", "haven't": "have not",
"he'd": "he would", "he'll": "he will", "he's": "he is",
"i'd": "I had", "i'll": "I will", "i'm": "I am", "isn't": "is not",
"it's": "it is", "it'll": "it will", "i've": "I have", "let's": "let us",
"mightn't": "might not", "mustn't": "must not", "shan't": "shall not",
"she'd": "she would", "she'll": "she will", "she's": "she is",
"shouldn't": "should not", "that's": "that is", "there's": "there is",
"they'd": "they would", "they'll": "they will", "they're": "they are",
"they've": "they have", "we'd": "we would", "we're": "we are",
"weren't": "were not", "we've": "we have", "what'll": "what will",
"what're": "what are", "what's": "what is", "what've": "what have",
"where's": "where is", "who'd": "who would", "who'll": "who will",
"who're": "who are", "who's": "who is", "who've": "who have",
"won't": "will not", "wouldn't": "would not", "you'd": "you would",
"you'll": "you will", "you're": "you are", "you've": "you have",
"'re": " are", "wasn't": "was not", "we'll": " will", "tryin'": "trying"}<string_transform>
learn1.unfreeze()
def _get_misspell(misspell_dict):
misspell_re = re.compile('(%s)' % '|'.join(misspell_dict.keys()))
return misspell_dict, misspell_re<categorify>
learn1.fit_one_cycle(6,3e-5)
def replace_typical_misspell(text):
misspellings, misspellings_re = _get_misspell(misspell_dict)
 def replace(match):
return misspellings[match.group(0)]
return misspellings_re.sub(replace, text )<define_variables>
learn2.unfreeze()
puncts = [',', '.', '"', ':', ')', '(', '-', '!', '?', '|', ';', "'", '$', '&', '/', '[', ']',
'>', '%', '=', '
'®', '`', '<', '→', '°', '€', '™', '›', '♥', '←', '×', '§', '″', '′', 'Â', '█',
'½', 'à', '…', '“', '★', '”', '–', '●', 'â', '►', '−', '¢', '²', '¬', '░', '¶',
'↑', '±', '¿', '▾', '═', '¦', '║', '―', '¥', '▓', '—', '‹', '─', '▒', '：', '¼',
'⊕', '▼', '▪', '†', '■', '’', '▀', '¨', '▄', '♫', '☆', 'é', '¯', '♦', '¤', '▲',
'è', '¸', '¾', 'Ã', '⋅', '‘', '∞', '∙', '）', '↓', '、', '│', '（', '»', '，', '♪',
'╩', '╚', '³', '・', '╦', '╣', '╔', '╗', '▬', '❤', 'ï', 'Ø', '¹', '≤', '‡', '√', "*"]<string_transform>
def clean_text(x):
x = str(x)
 for punct in puncts + list(string.punctuation):
if punct in x:
x = x.replace(punct, f' {punct} ')
 return x<define_variables>
learn2.fit_one_cycle(6,3e-5)
def clean_numbers(x):
return re.sub('\d+', ' ', x )<compute_test_metric>
submit = pd.DataFrame(columns=['id','label'])
 submit['id'] = test['id']
def get_coefs(word, *arr):
return word, np.asarray(arr, dtype='float32' )<feature_engineering>
for index, row in test.iterrows() :
pixels=np.asarray(row['pixel0':'pixel783'])
 arr=pixels.reshape(( 28,28)) /255
arr = np.stack([arr]*3,axis=0)
 img = Image(FloatTensor(arr))
submit.at[index, 'label'] = int(( learn1.predict(img)[1] + learn2.predict(img)[1])/2 )
<define_variables><EOS> 
submit.to_csv('submission.csv',index=False )