Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<choose_model_class>
PATH = "data/dogscats/"
vec = CountVectorizer(ngram_range=(1,2), tokenizer=re_tokenizer, min_df=4, max_df=0.8, strip_accents='unicode', lowercase=False )<normalization>
for r in random.sample(range(12499), 1000):
os.rename(f'{PATH}train/cats/cat.{r}.jpg', f'{PATH}valid/cats/cat.{r}.jpg')
 for r in random.sample(range(12499), 1000):
os.rename(f'{PATH}train/dogs/dog.{r}.jpg', f'{PATH}valid/dogs/dog.{r}.jpg' )
train_term_doc = vec.fit_transform(X_train)
 valid_term_doc = vec.transform(X_valid)
 train_term_doc.shape, valid_term_doc.shape<prepare_x_and_y>
sz=224
bs=20
arch=resnet50
x_train = train_term_doc
y_train = y_train.values
x_valid = valid_term_doc
y_valid = y_valid.values<normalization>
tfms = tfms_from_model(arch, sz, aug_tfms=transforms_side_on, max_zoom=1.1)
 data = ImageClassifierData.from_paths(PATH, tfms=tfms, bs=bs,
trn_name='train',
test_name='test1',
val_name='valid')
 learn = ConvLearner.pretrained(arch, data, precompute=False )
p1 = np.squeeze(np.asarray(x_train[y_train==1].sum(0)))
 p0 = np.squeeze(np.asarray(x_train[y_train==0].sum(0)) )<define_variables>
learn.fit(0.01, 3, cycle_len=1 )
pr1 =(p1+1)/(( y_train==1 ).sum() +1)
 pr0 =(p0+1)/(( y_train==0 ).sum() +1 )<feature_engineering>
learn.unfreeze()
learn.bn_freeze(True)
 learn.fit(lrs=[1e-5,1e-4,1e-2], n_cycle=1,
cycle_len=1 )
pr1[2164] / pr0[2164]<feature_engineering>
log_preds,y = learn.TTA()
b = np.log(( y_train==1 ).mean() /(y_train==0 ).mean()); b<define_variables>
probs = np.mean(np.exp(log_preds),0)
 accuracy_np(probs, y), metrics.log_loss(y, probs )
preds =(x_valid @ r + b)> 0<compute_test_metric>
prediction = learn.predict(is_test=True)
 pred_test = np.argmax(prediction, axis=1)
 label_probs = np.exp(prediction[:,1] )
print(f'Validation accuracy: {(preds == y_valid ).mean() }')
 print(f'Validation F1 score: {f1_score(y_valid, preds)}' )<choose_model_class>
submission = pd.DataFrame({'id':os.listdir(f'{PATH}test1'), 'label':label_probs})
 submission['id'] = submission['id'].map(lambda x: x.split('.')[0])
 submission['id'] = submission['id'].astype(int)
 submission = submission.sort_values('id')
 submission.to_csv('.. /working/submission.csv', index=False )
vec_tfidf = TfidfVectorizer(ngram_range=(1,2), tokenizer=re_tokenizer, lowercase=False,
min_df=4, max_df=0.8, strip_accents='unicode', sublinear_tf=True )<categorify>
! rm -rf data