Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<string_transform>
import numpy as np
import pandas as pd
import os
from fastai.vision import *
def clean_contractions(text, mapping):
specials = ["’", "‘", "´", "`"]
for s in specials:
text = text.replace(s, "'")
 text = ' '.join([mapping[t] if t in mapping else t for t in text.split(" ")])
 return text<feature_engineering>
path = Path('.. /input' )
train['text_clean']=train['text'].progress_apply(lambda x:clean_contractions(x,contraction_mapping))
sentence=train['text_clean'].apply(lambda x:x.split())
 vocab=build_vocab(sentence )<compute_test_metric>
path.ls()
oov=check_coverage(vocab,embed_glove )<categorify>
path_img = path/'train'
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>
get_image_files(path_img)[:5]
train['text_clean']=train['text_clean'].progress_apply(lambda x:clean_text(x))
sentence=train['text_clean'].apply(lambda x:x.split())
 vocab=build_vocab(sentence )<compute_test_metric>
np.random.seed(42)
 size = 224
bs = 64
num_workers = 0
pat = r'/([^/.]+ ).\d+.jpg$'
oov=check_coverage(vocab,embed_glove )<categorify>
tfms = get_transforms()
data =(ImageItemList.from_folder(path_img)
.random_split_by_pct() 
.label_from_re(pat)
.add_test_folder('.. /test')
.transform(tfms, size=size)
.databunch(bs=bs, num_workers=num_workers)
.normalize(imagenet_stats))
def clean_numbers(x):
x = re.sub('[0-9]{5,}', '
x = re.sub('[0-9]{4}', '
x = re.sub('[0-9]{3}', '
x = re.sub('[0-9]{2}', '
x = re.sub('[0-9]','
return x<feature_engineering>
data.show_batch(rows=3, figsize=(7,6))
train['text_clean']=train['text_clean'].progress_apply(lambda x: clean_numbers(x))
train['text_clean']=train['text_clean'].apply(lambda x:x.lower() )<string_transform>
learn = create_cnn(data, models.resnet50, metrics=accuracy, model_dir='/tmp/models' )
sentence=train['text_clean'].apply(lambda x:x.split())
 vocab=build_vocab(sentence)
 oov=check_coverage(vocab,embed_glove )<define_variables>
learn.fit_one_cycle(4 )
'disease' in embed_glove<define_variables>
learn.save('stage-1' )
mispell_dict={'mh
'
're
'
'diseasese':'disease',
'don
'
'
'mediterraneanean':'mediterranean',
'it
'
'you
'chinaªs':'china',
'be
'ices
's
'by
'iªm':'I am',
'
'read
'enchanged':'exchanged',
'let
'can
'americaªs':'america',
'of
'here
'grief
'
'idfire':'id fire',
'karymsky':'sky',
'rexyy':'rex',
'japìn':'japan',
'
'carryi':'carrying',
'offensive
'
'lulgzimbestpicts':'pics',
'ramag':'ram',
'diamondkesawn':'diamond',
'raynbowaffair':'rainbow affair',
'viwxy
'lvlh
'yazidis':'yazid',
'
'waimate':'mate',
'otrametlife':'
'
'
'
'rqwuoy
'ks
'
'iªve':'I have',
'unsuckdcmetro':'metro',
'realdonaldtrump':'donald trump',
'abbswinston':'winston',
'moll
'oppressions':'oppression',
'slanglucci':'slang',
'fettilootch':'
'worstsummerjob':'worst summer job',
'
'i
'åê':'
'åè':'
'
'warfighting':'war fighting',
'mitt
'
'michael
'kurtschlichter':'
'beforeitsnews':'before it is news',
'zujwuiomb':'
'lonewolffur':'lone wolf',
'm
'localarsonist':'local arson',
'wvj
'thoyhrhkfj':'
'o
'nnmqlzo
'temporary
'pbban':'
'm
'votejkt
'rockyfire':'rock fire',
'throwingknifes':'throwing knife',
'dannyonpc':'danny on picture',
'godslove':'god love',
'ptsdchat':'chat',
'cbcca':'
'usnwsgov':'us government',
'metrofmtalk':'metro fm talk',
'
'bookboost':'book boost',
'ibooklove':'book love',
'aoms':'
'foxysiren':'fox',
'blowmandyup':'blow',
'dehmym
'itunesmusic':'i tunes music',
'\x89û÷politics':'politics','viralspell':'viral spell'}<categorify>
learn.fit_one_cycle(2, max_lr=slice(1e-6,1e-4))
def correct_spelling(x, dic):
for word in dic.keys() :
x = x.replace(word, dic[word])
 return x 
<feature_engineering>
learn.save('stage-2' )
train['text_clean']=train['text_clean'].progress_apply(lambda x: correct_spelling(x,mispell_dict))
sentence=train['text_clean'].apply(lambda x:x.split())
 vocab=build_vocab(sentence)
 oov=check_coverage(vocab,embed_glove)
<feature_engineering>
interp = ClassificationInterpretation.from_learner(learn)
 losses,idxs = interp.top_losses()
len(data.valid_ds)==len(losses)==len(idxs )
test['text_clean']=test['text'].progress_apply(lambda x:clean_contractions(x,contraction_mapping))
test['text_clean']=test['text_clean'].progress_apply(lambda x:clean_text(x))
test['text_clean']=test['text_clean'].progress_apply(lambda x: clean_numbers(x))
test['text_clean']=test['text_clean'].apply(lambda x:x.lower())
 test['text_clean']=test['text_clean'].progress_apply(lambda x: correct_spelling(x,mispell_dict))
sentence=test['text_clean'].apply(lambda x:x.split())
 vocab=build_vocab(sentence)
 oov=check_coverage(vocab,embed_glove)
<split>
preds, y = learn.get_preds(ds_type=DatasetType.Test )
train_df,val_df=train_test_split(train,test_size=0.1,random_state=100 )<define_variables>
dog_preds = preds[:,1]
embed_size=300
max_features=20000
maxlen=300 
<prepare_x_and_y>
submission = pd.DataFrame({'id':os.listdir('.. /input/test'), 'label':dog_preds} )
train_X=train_df['text_clean'].values
valid_X=val_df['text_clean'].values
test_X=test['text_clean'].values 
<prepare_x_and_y>
submission['id'] = submission['id'].map(lambda x: x.split('.')[0] )
tokenizer=Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(list(train_X))
train_X=tokenizer.texts_to_sequences(train_X)
 valid_X=tokenizer.texts_to_sequences(valid_X)
 test_X=tokenizer.texts_to_sequences(test_X)
 train_X=pad_sequences(train_X,maxlen=maxlen)
 valid_X=pad_sequences(valid_X,maxlen=maxlen)
 test_X=pad_sequences(test_X,maxlen=maxlen)
 train_Y=train_df['target'].values
valid_Y=val_df['target'].values 
<feature_engineering>
submission['id'] = submission['id'].astype(int )
all_embs = np.stack(embed_glove.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
word_index = tokenizer.word_index 
<feature_engineering>
submission = submission.sort_values('id' )
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in tqdm(word_index.items()):
if i >= max_features: continue
embedding_vector = embed_glove.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector<choose_model_class>
submission.to_csv('submission.csv', index=False )
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix],trainable=False )(inp)
 x=SpatialDropout1D(0.2 )(x)
 x = Bidirectional(LSTM(128,return_sequences=True))(x)
 x=GlobalMaxPool1D()(x)
 x = Dense(16, activation="relu" )(x)
 x = Dropout(0.1 )(x)
 x = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
 print(model.summary() )<train_model>
submission.to_csv('submission.csv', index=False )