Kaggle competition: Aerial Cactus Identification
dataset['IsAlone'] = dataset['FamilySize'].map(lambda s: 1 if s == 1 else 0 )<feature_engineering>
import numpy as np
import pandas as pd
import os
import cv2
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import TensorDataset, DataLoader, Dataset
import torchvision
import torchvision.transforms as transforms
from sklearn.model_selection import train_test_split
dataset['LastName'] = dataset.Name.str.split(',' ).str[0]<categorify>
train_path = '.. /input/train/train/'
test_path = '.. /input/test/test/'
labels = pd.read_csv('.. /input/train.csv')
 sub = pd.read_csv('.. /input/sample_submission.csv' )
le = LabelEncoder()
dataset['LastName'] = le.fit_transform(dataset['LastName'])
 dataset['LastName'] = dataset['LastName'].astype(int )<drop_column>
num_epochs = 25
num_classes = 2
batch_size = 128
learning_rate = 0.002
dataset.drop(labels=['Fare', 'Cabin', 'FamilySize', 'PassengerId', 'Ticket', 'Age', 'Name'],
axis=1, inplace=True )<categorify>
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu' )
dataset = pd.get_dummies(dataset, columns = ["Embarked"])
 dataset = pd.get_dummies(dataset, columns = ["Parch"])
 dataset = pd.get_dummies(dataset, columns = ["Pclass"])
 dataset = pd.get_dummies(dataset, columns = ["CabinType"])
 dataset = pd.get_dummies(dataset, columns = ["Title"])
 dataset = pd.get_dummies(dataset, columns = ["FamilySizeBin"])
 dataset = pd.get_dummies(dataset, columns = ["FareBin"])
 dataset = pd.get_dummies(dataset, columns = ["AgeGroup"] )<split>
train, val = train_test_split(labels,
stratify=labels.has_cactus,
test_size=0.3)
 train.shape, val.shape
train = dataset[:len(train)]
test = dataset[len(train):]
test.drop(labels=["Survived"],axis = 1,inplace=True )<prepare_x_and_y>
class Cactus(Dataset):
def __init__(self, df_data, data_dir = './', transform=None):
super().__init__()
self.df = df_data.values
self.data_dir = data_dir
self.transform = transform
def __len__(self):
return len(self.df)
 def __getitem__(self, index):
img_name,label = self.df[index]
img_path = os.path.join(self.data_dir, img_name)
 image = cv2.imread(img_path)
 if self.transform is not None:
image = self.transform(image)
 return image, label
X_train = train.drop(labels = ["Survived"],axis = 1)
 y_train = train["Survived"].astype(int )<import_modules>
trans_train = transforms.Compose([transforms.ToPILImage() ,
transforms.Pad(32, padding_mode='reflect'),
transforms.ToTensor() ,
transforms.Normalize(mean=[0.5, 0.5, 0.5],std=[0.5, 0.5, 0.5])])
 trans_valid = transforms.Compose([transforms.ToPILImage() ,
transforms.Pad(32, padding_mode='reflect'),
transforms.ToTensor() ,
transforms.Normalize(mean=[0.5, 0.5, 0.5],std=[0.5, 0.5, 0.5])] )
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import accuracy_score<compute_train_metric>
dataset_train = Cactus(df_data=train,
data_dir=train_path,
transform=trans_train)
 dataset_valid = Cactus(df_data=val,
data_dir=train_path,
transform=trans_valid)
 loader_train = DataLoader(dataset = dataset_train,
batch_size=batch_size,
shuffle=True,
num_workers=0)
 loader_valid = DataLoader(dataset = dataset_valid,
batch_size=batch_size//2,
shuffle=False,
num_workers=0 )
gbdt = GradientBoostingClassifier(learning_rate=0.02, min_samples_split=6, min_samples_leaf=4)
 cross_val_score(gbdt, X_train, y_train, cv=10 ).mean()<choose_model_class>
class SimpleCNN(nn.Module):
def __init__(self):
super(SimpleCNN, self ).__init__()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, padding=2)
 self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=2)
 self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=2)
 self.conv4 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=2)
 self.conv5 = nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, padding=2)
 self.bn1 = nn.BatchNorm2d(32)
 self.bn2 = nn.BatchNorm2d(64)
 self.bn3 = nn.BatchNorm2d(128)
 self.bn4 = nn.BatchNorm2d(256)
 self.bn5 = nn.BatchNorm2d(512)
 self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
 self.avg = nn.AvgPool2d(4)
 self.fc = nn.Linear(512 * 1 * 1, 2)
 def forward(self, x):
x = self.pool(F.leaky_relu(self.bn1(self.conv1(x))))
x = self.pool(F.leaky_relu(self.bn2(self.conv2(x))))
x = self.pool(F.leaky_relu(self.bn3(self.conv3(x))))
x = self.pool(F.leaky_relu(self.bn4(self.conv4(x))))
x = self.pool(F.leaky_relu(self.bn5(self.conv5(x))))
x = self.avg(x)
 x = x.view(-1, 512 * 1 * 1)
 x = self.fc(x)
 return x
model = GradientBoostingClassifier(learning_rate=0.02, min_samples_split=6, min_samples_leaf=4 )<train_model>
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adamax(model.parameters() , lr=learning_rate )
model.fit(X_train, y_train )<train_model>
total_step = len(loader_train)
 for epoch in range(num_epochs):
for i,(images, labels)in enumerate(loader_train):
images = images.to(device)
 labels = labels.to(device)
 outputs = model(images)
 loss = criterion(outputs, labels)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
if(i+1)% 100 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}' 
.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
model.fit(X_train, y_train )<save_to_csv>
model.eval()
with torch.no_grad() :
correct = 0
total = 0
for images, labels in loader_valid:
images = images.to(device)
 labels = labels.to(device)
 outputs = model(images)
 _, predicted = torch.max(outputs.data, 1)
 total += labels.size(0)
 correct +=(predicted == labels ).sum().item()
print('Test Accuracy of the model on the 1750 validation images: {} %'.format(100 * correct / total))
torch.save(model.state_dict() , 'model.ckpt' )
test_Survived = pd.Series(model.predict(test), name="Survived")
 results = pd.concat([IDtest, test_Survived], axis=1)
 results.to_csv("titanic_with_ensemble.csv",index=False )<filter>
dataset_valid = Cactus(df_data=sub, data_dir=test_path, transform=trans_valid)
 loader_test = DataLoader(dataset = dataset_valid, batch_size=32, shuffle=False, num_workers=0 )
dataset.iloc[old_man_nan_fare_idx]<predict_on_test>
model.eval()
preds = []
for batch_i,(data, target)in enumerate(loader_test):
data, target = data.cuda() , target.cuda()
output = model(data)
 pr = output[:,1].detach().cpu().numpy()
for i in pr:
preds.append(i)
 sub['has_cactus'] = preds
sub.to_csv('sub.csv', index=False )