Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<define_variables>
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
errors =(Y_pred_classes - y_true != 0)
 Y_pred_classes_errors = Y_pred_classes[errors]
Y_pred_errors = Y_pred[errors]
Y_true_errors = y_true[errors] 
<predict_on_test>
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()
predictions = model.predict_classes(X_test, verbose=2 )<save_to_csv>
TRAIN_FOLDER = '.. /input/cats-and-dogs-embedded-data/train/train/'
TEST_FOLDER = '.. /input/cats-and-dogs-embedded-data/test/test/'
IMG_SIZE = 224
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub['Label'] = predictions
sub.to_csv('submission.csv',index=False )<set_options>
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' )
%matplotlib inline 
<load_from_csv>
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]
train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 submit_df = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv' )<define_variables>
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )
num_pixel = len(train_df.columns)- 1
num_pixel<categorify>
train_EB0 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_3/cats_and_dogs_3/train_EB0.npy')
 test_EB0 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_3/cats_and_dogs_3/test_EB0.npy' )
transform_0 = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize([0.5], [0.5])
 ])
 transform_1 = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomRotation(30),
transforms.ToTensor() ,
transforms.Normalize([0.5], [0.5])
 ])
 transform_2 = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomAffine(degrees=15, translate=(0.1,0.1), scale=(0.8,0.8)) ,
transforms.ToTensor() ,
transforms.Normalize([0.5], [0.5])
 ])
 transform_3 = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomAffine(degrees=30, scale=(1.1,1.1)) ,
transforms.ToTensor() ,
transforms.Normalize([0.5], [0.5])
 ])
 transform_4 = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomAffine(degrees=30, translate=(0.1,0.1)) ,
transforms.ToTensor() ,
transforms.Normalize([0.5], [0.5])
 ])
 transform_5 = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomAffine(degrees=10, shear=45),
transforms.ToTensor() ,
transforms.Normalize([0.5], [0.5])
 ] )<data_type_conversions>
X_train, X_val, y_train, y_val = train_test_split(train_EB0, target, test_size=0.1, random_state=42 )
class DataFrame_to_Dataset(Dataset):
def __init__(self, df, transform=transform_0):
if len(df.columns)== num_pixel:
self.features = df.values.reshape(( -1,28,28)).astype(np.uint8)
 self.labels = None
else:
self.features = df.iloc[:,1:].values.reshape(( -1,28,28)).astype(np.uint8)
 self.labels = torch.from_numpy(df.label.values)
 self.transform = transform
def __len__(self):
return len(self.features)
 def __getitem__(self, index):
if self.labels is not None:
return self.transform(self.features[index]), self.labels[index]
else:
return self.transform(self.features[index] )<split>
%%time
lr = LogisticRegression(C=0.005, max_iter=10000)
 lr.fit(X_train, y_train )
def create_dataloaders(seed, test_size=0.1, df=train_df, batch_size=32):
train_data, valid_data = train_test_split(df,
test_size=test_size,
random_state=seed)
 train_dataset_0 = DataFrame_to_Dataset(train_data)
 train_dataset_1 = DataFrame_to_Dataset(train_data, transform_1)
 train_dataset_2 = DataFrame_to_Dataset(train_data, transform_2)
 train_dataset_3 = DataFrame_to_Dataset(train_data, transform_3)
 train_dataset_4 = DataFrame_to_Dataset(train_data, transform_4)
 train_dataset_5 = DataFrame_to_Dataset(train_data, transform_5)
 train_dataset = ConcatDataset([train_dataset_0, train_dataset_1, train_dataset_2, train_dataset_3, train_dataset_4, train_dataset_5])
 valid_dataset = DataFrame_to_Dataset(valid_data)
 train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
 valid_loader = torch.utils.data.DataLoader(valid_dataset, batch_size=batch_size, shuffle=False)
 return train_loader, valid_loader<choose_model_class>
val_preds = lr.predict_proba(X_val)[:,1]
class Net(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Sequential(
 nn.Conv2d(1, 32, kernel_size=3),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.Conv2d(32, 32, kernel_size=3),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.Conv2d(32, 32, kernel_size=5, stride=2, padding=14),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, 2),
nn.Dropout2d(0.4)
)
 self.conv2 = nn.Sequential(
 nn.Conv2d(32, 64, kernel_size=3),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=3),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=5, stride=2, padding=6),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, 2),
nn.Dropout2d(0.3)
)
 self.conv3 = nn.Sequential(
 nn.Conv2d(64, 128, kernel_size=4),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.Dropout2d(0.2)
)
 self.fc = nn.Sequential(
 nn.Linear(128*1*1, 10)
)
 def forward(self, x):
x = self.conv1(x)
 x = self.conv2(x)
 x = self.conv3(x)
 x = x.view(-1, 128*1*1)
 x = self.fc(x)
 return x<set_options>
roc_auc_score(y_val, val_preds )
use_cuda = torch.cuda.is_available()
print(use_cuda )<choose_model_class>
log_loss(y_val, val_preds )
def train(seed, num_epochs):
print('Creating new dataloaders...')
 train_loader, valid_loader = create_dataloaders(seed=seed)
 print('Creating a new model...')
 net = Net()
criterion = nn.CrossEntropyLoss()
if use_cuda:
net.cuda()
criterion.cuda()
optimizer = optim.Adam(net.parameters() ,
lr=0.003, betas=(0.9, 0.999),
eps=1e-08, weight_decay=0,
amsgrad=False)
 scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.1)
 print('Training the model...')
 for epoch in range(num_epochs):
net.train()
t0 = time.time()
training_loss = 0.0
num_samples = 0
for features, labels in train_loader:
if use_cuda:
features = features.cuda()
labels = labels.cuda()
optimizer.zero_grad()
outputs = net(features)
 loss = criterion(outputs, labels)
 loss.backward()
optimizer.step()
training_loss += loss.item()
num_samples += len(features)
 net.eval()
correct = 0
total = 0
with torch.no_grad() :
for valid_features, valid_labels in valid_loader:
if use_cuda:
valid_features = valid_features.cuda()
valid_labels = valid_labels.cuda()
outputs = net(valid_features)
 _, predicted = torch.max(outputs, 1)
 total += valid_labels.size(0)
 correct +=(predicted == valid_labels ).sum().item()
scheduler.step()
print('[model %d, epoch %d, time: %.3f seconds] train_loss: %.5f, val_acc: %4f %%' % 
(seed + 1, epoch + 1, time.time() - t0, training_loss/num_samples, 100 * correct / total))
net.eval()
test_pred = torch.LongTensor()
if use_cuda:
test_pred = test_pred.cuda()
with torch.no_grad() :
for features in test_loader:
if use_cuda:
features = features.cuda()
outputs = net(features)
 _, predicted = torch.max(outputs, 1)
 test_pred = torch.cat(( test_pred, predicted), dim=0)
 model_name = 'model_' + str(seed + 1)
 ensemble_df[model_name] = test_pred.cpu().numpy()
print('Prediction Saved!
' )<create_dataframe>
test_preds = lr.predict_proba(test_EB0)[:,1]
<data_type_conversions><EOS> 
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()