Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<feature_engineering>
combine.loc[(combine.Fare.notna()),"Fare_PP"] = combine.Fare/combine.Ticket_Counts
combine[["Fare","Fare_PP","Ticket_Counts"]].sample(5 )<filter>
seed = 50
os.environ['PYTHONHASHSEED'] = str(seed)
 random.seed(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.cuda.manual_seed_all(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.enabled = False
combine[combine.Fare.isna() ]<feature_engineering>
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu' )
combine.loc[(combine.Fare_PP.isna()),"Fare_PP"] = combine[(combine.Embarked=="S")&(combine.Pclass==3)]["Fare_PP"].median()
combine.loc[(combine.Fare.isna()),"Fare"] = combine[(combine.Embarked=="S")&(combine.Pclass==3)]["Fare_PP"].median()<count_unique_values>
data_path = '/kaggle/input/aerial-cactus-identification/'
labels = pd.read_csv(data_path + 'train.csv')
 submission = pd.read_csv(data_path + 'sample_submission.csv' )
multi_ticket = list(combine[(combine.Ticket_Counts>=2)&(combine.Cabin_Title.notna())]["Ticket"].values)
 len(set(multi_ticket))<groupby>
with ZipFile(data_path + 'train.zip')as zipper:
zipper.extractall()
with ZipFile(data_path + 'test.zip')as zipper:
zipper.extractall()
multi_ticket_cabin = combine[(combine.Cabin_Title.notna())&(combine.Ticket_Counts>=2)][["Ticket","Cabin_Title"]]
same_ticket_differ_cabin = []
for i,j in multi_ticket_cabin.groupby(["Ticket"]):
differ_cabin = set(list(j.Cabin_Title.values))
if len(differ_cabin)>=2:
same_ticket_differ_cabin = same_ticket_differ_cabin+[i]
print(same_ticket_differ_cabin )<feature_engineering>
_, valid = train_test_split(labels,
test_size=0.1,
stratify=labels['has_cactus'],
random_state=50 )
need_filling = list(combine[(combine.Ticket.isin(multi_ticket)) &(combine.Cabin.isna())].PassengerId.values)
 for passid in need_filling:
ticket = list(combine[combine.PassengerId==passid]["Ticket"].values)
 cabin_title = combine[combine.Ticket.isin(ticket)]["Cabin_Title"].describe() ["top"]
combine.loc[combine.PassengerId==passid,"Cabin_Title"] = cabin_title
combine[combine.PassengerId.isin(need_filling)][["Cabin_Title"]][:3]<drop_column>
class ImageDataset(Dataset):
def __init__(self, df, img_dir='./', transform=None):
super().__init__()
self.df = df
self.img_dir = img_dir
self.transform = transform
def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
img_id = self.df.iloc[idx, 0]
img_path = self.img_dir + img_id
image = cv2.imread(img_path)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 label = self.df.iloc[idx, 1]
if self.transform is not None:
image = self.transform(image)
 return image, label
combine.drop(["Cabin","Fare"],axis = 1,inplace = True )<filter>
transform_train = transforms.Compose([transforms.ToPILImage() ,
transforms.Pad(32, padding_mode='symmetric'),
transforms.RandomHorizontalFlip() ,
transforms.RandomVerticalFlip() ,
transforms.RandomRotation(10),
transforms.ToTensor() ,
transforms.Normalize(( 0.485, 0.456, 0.406), 
(0.229, 0.224, 0.225)) ])
 transform_test= transforms.Compose([transforms.ToPILImage() ,
transforms.Pad(32, padding_mode='symmetric'),
transforms.ToTensor() ,
transforms.Normalize(( 0.485, 0.456, 0.406), 
(0.229, 0.224, 0.225)) ] )
need_check = combine[(combine.PassengerId.isin(maybe_wrong)) &(combine.Ticket_Counts==1)]["PassengerId"].values
need_check<categorify>
dataset_train = ImageDataset(df=labels, img_dir='train/', transform=transform_train)
 dataset_valid = ImageDataset(df=valid, img_dir='train/', transform=transform_test )
def get_from_passid(dataset,passid_list):
return dataset[dataset.PassengerId.isin(passid_list)][['Age', 'Cabin_Title', 'Embarked', 'Name', 'Parch', 'SibSp','Ticket',
'Ticket2', 'Surname','Maiden',"Ticket_Counts", 'Pclass_Gender',
'Family_size',"Survived"]]
def get_from_name(dataset,name_list):
return dataset[(dataset.Surname.isin(name_list))
|(dataset.Maiden.isin(name_list)) ][['Age', 'Cabin_Title', 'Embarked', 'Name', 'Parch', 'SibSp','Ticket',
'Ticket2', 'Surname','Maiden',"Ticket_Counts", 'Pclass_Gender',
'Family_size',"Survived"]]<filter>
loader_train = DataLoader(dataset=dataset_train, batch_size=32, shuffle=True)
 loader_valid = DataLoader(dataset=dataset_valid, batch_size=32, shuffle=False )
combine[(combine.Ticket2=="175xx")&(combine.Cabin_Title=="C")&(combine.Embarked=="C")&(combine.Family_size>=2)&(combine.Pclass==1)]<filter>
class Model(nn.Module):
def __init__(self):
super().__init__()
self.layer1 = nn.Sequential(nn.Conv2d(in_channels=3, out_channels=32,
kernel_size=3, padding=2),
nn.BatchNorm2d(32),
nn.LeakyReLU() ,
nn.MaxPool2d(kernel_size=2))
self.layer2 = nn.Sequential(nn.Conv2d(in_channels=32, out_channels=64,
kernel_size=3, padding=2),
nn.BatchNorm2d(64),
nn.LeakyReLU() ,
nn.MaxPool2d(kernel_size=2))
self.layer3 = nn.Sequential(nn.Conv2d(in_channels=64, out_channels=128,
kernel_size=3, padding=2),
nn.BatchNorm2d(128),
nn.LeakyReLU() ,
nn.MaxPool2d(kernel_size=2))
self.layer4 = nn.Sequential(nn.Conv2d(in_channels=128, out_channels=256,
kernel_size=3, padding=2),
nn.BatchNorm2d(256),
nn.LeakyReLU() ,
nn.MaxPool2d(kernel_size=2))
self.layer5 = nn.Sequential(nn.Conv2d(in_channels=256, out_channels=512,
kernel_size=3, padding=2),
nn.BatchNorm2d(512),
nn.LeakyReLU() ,
nn.MaxPool2d(kernel_size=2))
self.avg_pool = nn.AvgPool2d(kernel_size=4)
 self.fc1 = nn.Linear(in_features=512 * 1 * 1, out_features=64)
 self.fc2 = nn.Linear(in_features=64, out_features=2)
 def forward(self, x):
x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x)
 x = self.layer4(x)
 x = self.layer5(x)
 x = self.avg_pool(x)
 x = x.view(-1, 512 * 1 * 1)
 x = self.fc1(x)
 x = self.fc2(x)
 return x
combine[(( combine.Age<=3)|(combine.Age>=32))
&(combine.Embarked=="S")&(combine.Family_size>=2)
 &(combine.Ticket2=="2506xx")&(combine.Pclass==2)][['Age', 'Cabin_Title', 'Embarked', 'Name', 'Parch', 'SibSp','Ticket',
'Ticket2', 'Surname','Maiden',"Ticket_Counts", 'Pclass_Gender', 'Family_size']]<filter>
model = Model().to(device )
combine[(combine.Embarked=="S")&(combine.Ticket2=="3470xx")&(combine.Pclass==3)&(combine.Family_size==2)][['Age', 'Cabin_Title', 'Embarked', 'Name', 'Parch', 'SibSp','Ticket', 'Surname', 'Maiden',
'Pclass_Gender', 'Family_size', 'Surname_Counts', 'Ticket_Counts',"Maiden_Counts","Survived"]]<feature_engineering>
criterion = nn.CrossEntropyLoss()
combine.loc[combine.PassengerId.isin([137]),"Maiden"] = "Monypeny"
combine.loc[combine.Maiden.isin(["Monypeny"]),"Maiden_Counts"] = 2
combine.loc[combine.PassengerId.isin([274]),"Parch"] = 0
combine.loc[combine.PassengerId.isin([274]),"Family_size"] = 1
combine.loc[combine.PassengerId.isin([418]),"Parch"] = 0
combine.loc[combine.PassengerId.isin([418]),"Family_size"] = 1
combine.loc[combine.PassengerId.isin([1130]),"Parch"] = 0
combine.loc[combine.PassengerId.isin([1130]),"SibSp"] = 0
combine.loc[combine.PassengerId.isin([1130]),"Family_size"] = 1<define_variables>
optimizer = torch.optim.Adamax(model.parameters() , lr=0.00006 )
test_error = pd.DataFrame(columns=["Category","Train_data","Test_data","Error_Counts","Error_Probability"])
 def get_error_counts(pg,index):
PG_train_size = combine[(combine.Pclass_Gender.isin(pg)) &(combine.Survived.notna())].shape[0]
PG_test_size = combine[(combine.Pclass_Gender.isin(pg)) &(combine.Survived.isna())].shape[0]
PG_survival = combine[(combine.Pclass_Gender.isin(pg)) ]["Survived"].mean()
if PG_survival>=0.5:
error_probability = 1-PG_survival
try:
test_error_counts = round(error_probability*PG_test_size)
 except:
test_error_counts=0
else:
error_probability = PG_survival
test_error_counts = round(error_probability*PG_test_size)
 test_error.loc[index]=[pg,PG_train_size,PG_test_size,test_error_counts,"%.2f%%"%(round(PG_survival*100,2)) ]
index = 0
PG_list = [["P1-Woman","P2-Woman"],["P3-Woman"],["P1-Man"],["P2-Man"],["P3-Man"],["P1-Boy","P2-Boy"],["P3-Boy"]]
for pg in PG_list:
get_error_counts(pg,index)
 index +=1
test_error.sort_values(by = "Error_Counts",ascending = False )<feature_engineering>
epochs = 70
for epoch in range(epochs):
epoch_loss = 0
for images, labels in loader_train:
images = images.to(device)
 labels = labels.to(device)
 optimizer.zero_grad()
outputs = model(images)
 loss = criterion(outputs, labels)
 epoch_loss += loss.item()
loss.backward()
optimizer.step()
print(f'에폭: [{epoch+1}/{epochs}], 손실값: {epoch_loss/len(loader_train):.4f}' )
combine.loc[(combine.Pclass_Gender.isin(["P1-Boy","P2-Boy","P1-Woman","P2-Woman"])) ,"Predict"] = 1
combine.loc[(combine.Pclass_Gender=="P2-Man"),"Predict"] = 0<filter>
dataset_test = ImageDataset(df=submission, img_dir='test/', transform=transform_test)
 loader_test = DataLoader(dataset=dataset_test, batch_size=32, shuffle=False)
 model.eval()
preds = []
with torch.no_grad() :
for images, _ in loader_test:
images = images.to(device)
 outputs = model(images)
 preds_part = torch.softmax(outputs.cpu() , dim=1)[:, 1].tolist()
preds.extend(preds_part )
have_family = combine[combine.Family_size>=2]<filter>
submission['has_cactus'] = preds
submission.to_csv('submission.csv', index=False )
<filter><EOS> 
shutil.rmtree('./train')
 shutil.rmtree('./test' )