Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<load_from_csv>
import numpy as np
import pandas as pd
import zipfile
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import log_loss, roc_auc_score
from sklearn.linear_model import LogisticRegression
import os
test = NumpyImageList.from_csv('.. /input/', 'test.csv')
 test<load_from_csv>
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()
tfms = get_transforms(do_flip=False)
 data =(NumpyImageList.from_csv('.. /input/', 'train.csv')
.split_by_rand_pct (.1)
.label_from_df(cols='label')
.add_test(test, label=0)
.transform(tfms)
.databunch(bs=128, num_workers=0)
.normalize(imagenet_stats))
data<define_variables>
TRAIN_FOLDER = '.. /input/cats-and-dogs-embedded-data/train/train/'
TEST_FOLDER = '.. /input/cats-and-dogs-embedded-data/test/test/'
IMG_SIZE = 224
data.show_batch(rows=5, figsize=(10,10))<choose_model_class>
train_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/train_image_list.npy')
 test_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/test_image_list.npy' )
dropout = 0.25
model = nn.Sequential(
 nn.Conv2d(in_channels=3,
out_channels=32,
kernel_size=5),
nn.ReLU() ,
nn.BatchNorm2d(32),
nn.MaxPool2d(kernel_size=2,
stride=2),
nn.Dropout(dropout),
nn.Conv2d(in_channels=32,
out_channels=64,
kernel_size=3),
nn.ReLU() ,
nn.BatchNorm2d(64),
nn.MaxPool2d(kernel_size=2,
stride=2),
nn.Dropout(dropout),
nn.Conv2d(in_channels=64,
out_channels=64,
kernel_size=3),
nn.ReLU() ,
Flatten() ,
nn.Linear(64*3*3, 256),
nn.ReLU() ,
nn.Dropout(dropout*1.5),
nn.BatchNorm1d(256),
nn.Linear(256, 10), 
)
 if torch.cuda.is_available() :
model = model.cuda()
learn = Learner(data, model, metrics=accuracy, model_dir='/kaggle/working/models')
 learn.summary()<train_model>
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]
learn.fit_one_cycle(20, max_lr=slice(1e-1))<save_model>
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )
learn.save('stage1' )<train_model>
train_EB5_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB5_ns.npy')
 test_EB5_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB5_ns.npy' )
learn.fit_one_cycle(5, max_lr=slice(1e-2))<save_model>
X_train, X_val, y_train, y_val = train_test_split(train_EB5_ns, target, test_size=0.1, random_state=42 )
learn.save('stage2' )<save_to_csv>
%%time
lr = LogisticRegression(C=0.02, max_iter=10000)
 lr.fit(X_train, y_train )
predictions, *_ = learn.get_preds(DatasetType.Test)
 labels = np.argmax(predictions, 1)
 submission_df = pd.DataFrame({'ImageId': list(range(1,len(labels)+1)) , 'Label': labels})
 submission_df.to_csv(f'submission.csv', index=False )<set_options>
val_preds = lr.predict_proba(X_val)[:,1]
%matplotlib inline
np.random.seed(2)
 sns.set(style='white', context='notebook', palette='deep' )<load_from_csv>
roc_auc_score(y_val, val_preds )
train=pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test=pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )<load_pretrained>
log_loss(y_val, val_preds )
def load_data() :

 path='/kaggle/input/mnist-numpy/mnist.npz'
f = np.load(path)
 x_train, y_train = f['x_train'], f['y_train']
x_test, y_test = f['x_test'], f['y_test']
f.close()
return(x_train, y_train),(x_test, y_test )<prepare_x_and_y>
test_preds = lr.predict_proba(test_EB5_ns)[:,1]
<categorify><EOS> 
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()