Kaggle competition: Titanic - Machine Learning from Disaster
labels.label.value_counts()<split>
train = pd.read_csv('/kaggle/input/titanic/train.csv',index_col=None)
 print(train.shape)
 test = pd.read_csv('/kaggle/input/titanic/test.csv',index_col=None)
 print(test.shape)
 subm = pd.read_csv('/kaggle/input/titanic/gender_submission.csv',index_col=None)
 print(subm.shape )
tr, val = train_test_split(labels.label, stratify=labels.label, test_size=0.15, random_state=SEED )<define_variables>
!pip install autoviml
img_class_dict = {k:v for k, v in zip(labels.id, labels.label)}<categorify>
from autoviml.Auto_ViML import Auto_ViML
class CancerDataset(Dataset):
def __init__(self, datafolder, datatype='train', transform = transforms.Compose([transforms.CenterCrop(64),transforms.ToTensor() ]), labels_dict={}):
self.datafolder = datafolder
self.datatype = datatype
self.image_files_list = [s for s in os.listdir(datafolder)]
self.transform = transform
self.labels_dict = labels_dict
if self.datatype == 'train':
self.labels = [labels_dict[i.split('.')[0]] for i in self.image_files_list]
else:
self.labels = [0 for _ in range(len(self.image_files_list)) ]
def __len__(self):
return len(self.image_files_list)
 def __getitem__(self, idx):
img_name = os.path.join(self.datafolder, self.image_files_list[idx])
 img = cv2.imread(img_name)
 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 image = self.transform(image=img)
 image = image['image']
img_name_short = self.image_files_list[idx].split('.')[0]
if self.datatype == 'train':
label = self.labels_dict[img_name_short]
else:
label = 0
return image, label<define_search_model>
target = 'Survived'
data_transforms = albumentations.Compose([
albumentations.RandomRotate90(p=0.5),
albumentations.Transpose(p=0.5),
albumentations.Flip(p=0.5),
albumentations.OneOf([
albumentations.CLAHE(clip_limit=2), albumentations.IAASharpen() , albumentations.IAAEmboss() ,
albumentations.RandomBrightness() , albumentations.RandomContrast() ,
albumentations.JpegCompression() , albumentations.Blur() , albumentations.GaussNoise() ], p=0.5),
albumentations.HueSaturationValue(p=0.5),
albumentations.ShiftScaleRotate(shift_limit=0.15, scale_limit=0.15, rotate_limit=45, p=0.5),
albumentations.Normalize() ,
AT.ToTensor()
])
 data_transforms_test = albumentations.Compose([
albumentations.Normalize() ,
AT.ToTensor()
])
 data_transforms_tta0 = albumentations.Compose([
albumentations.RandomRotate90(p=0.5),
albumentations.Transpose(p=0.5),
albumentations.Flip(p=0.5),
albumentations.Normalize() ,
AT.ToTensor()
])
 data_transforms_tta1 = albumentations.Compose([
albumentations.RandomRotate90(p=1),
albumentations.Normalize() ,
AT.ToTensor()
])
 data_transforms_tta2 = albumentations.Compose([
albumentations.Transpose(p=1),
albumentations.Normalize() ,
AT.ToTensor()
])
 data_transforms_tta3 = albumentations.Compose([
albumentations.Flip(p=1),
albumentations.Normalize() ,
AT.ToTensor()
])
 dataset = CancerDataset(datafolder=base_dir+'train/', datatype='train', transform=data_transforms, labels_dict=img_class_dict)
 val_set = CancerDataset(datafolder=base_dir+'train/', datatype='train', transform=data_transforms_test, labels_dict=img_class_dict)
 test_set = CancerDataset(datafolder=base_dir+'test/', datatype='test', transform=data_transforms_test)
 train_sampler = SubsetRandomSampler(list(tr.index))
valid_sampler = SubsetRandomSampler(list(val.index))
batch_size = 256
num_workers = 0
train_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, sampler=train_sampler, num_workers=num_workers)
 valid_loader = torch.utils.data.DataLoader(val_set, batch_size=batch_size, sampler=valid_sampler, num_workers=num_workers)
 test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, num_workers=num_workers )<choose_model_class>
m, feats, trainm, testm = Auto_ViML(train, target, test,
sample_submission=subm,
scoring_parameter='balanced-accuracy', KMeans_Featurizer=True,
hyper_param='GS',feature_reduction=True,
Boosting_Flag=None,Binning_Flag=True,
Add_Poly=0, Stacking_Flag=True,Imbalanced_Flag=False,
verbose=0 )
model_conv = pretrainedmodels.se_resnext50_32x4d()
model_conv.avg_pool = nn.AdaptiveAvgPool2d(( 1, 1))
model_conv.last_linear = nn.Linear(in_features=2048, out_features=1, bias=True )<choose_model_class>
subm[target] = testm[target+'_predictions'].astype(int ).values
model_conv.cuda()
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model_conv.parameters() , lr=0.00005)
 scheduler = StepLR(optimizer, 1, gamma=0.5 )<init_hyperparams>
filename='sample_submission_log.csv'
savefile = '/kaggle/working/Survived/sample_submission_log.csv'
savefile
valid_loss_min = np.Inf
patience = 5
p = 0
stop = False
n_epochs = 6
for epoch in range(1, n_epochs+1):
print(time.ctime() , 'Epoch:', epoch)
 if stop:
print("Training stop.")
 break
train_loss = []
scheduler.step(epoch)
 train_auc = []
for tr_batch_i,(data, target)in enumerate(train_loader):
model_conv.train()
data, target = data.cuda() , target.cuda()
optimizer.zero_grad()
output = model_conv(data)
 loss = criterion(output[:,0], target.float())
 train_loss.append(loss.item())
 a = target.data.cpu().numpy()
b = output[:,0].detach().cpu().numpy()
train_auc.append(roc_auc_score(a, b))
loss.backward()
optimizer.step()
if(tr_batch_i+1)%100 == 0:
model_conv.eval()
val_loss = []
val_auc = []
for val_batch_i,(data, target)in enumerate(valid_loader):
data, target = data.cuda() , target.cuda()
output = model_conv(data)
 loss = criterion(output[:,0], target.float())
 val_loss.append(loss.item())
 a = target.data.cpu().numpy()
b = output[:,0].detach().cpu().numpy()
val_auc.append(roc_auc_score(a, b))
print('Epoch %d, batches:%d, train loss: %.4f, valid loss: %.4f.'%(epoch, tr_batch_i, np.mean(train_loss), np.mean(val_loss)) +
' train auc: %.4f, valid auc: %.4f'%(np.mean(train_auc),np.mean(val_auc)))
 train_loss = []
train_auc = []
valid_loss = np.mean(val_loss)
 if valid_loss <= valid_loss_min:
print('Validation loss decreased({:.6f} --> {:.6f} ).Saving model...'.format(
 valid_loss_min,
valid_loss))
torch.save(model_conv.state_dict() , 'model.pt')
 valid_loss_min = valid_loss
p = 0
else:
p += 1
if p > patience:
print('Early stop training')
 stop = True
break<set_options>
subm.to_csv(savefile,index=False )
torch.cuda.empty_cache()<find_best_params>
from autoviml.Auto_NLP import Auto_NLP
model_conv.eval()<categorify>
score_type = 'accuracy'
modeltype = 'Classification'
nlp_column = 'Name'
saved_dict = torch.load('model.pt')
 model_conv.load_state_dict(saved_dict)
 preds = []
for batch_i,(data, target)in enumerate(test_loader):
data, target = data.cuda() , target.cuda()
output = model_conv(data ).detach()
pr = output[:,0].cpu().numpy()
for i in pr:
preds.append(i )<prepare_output>
train_nlp, test_nlp, best_nlp_transformer, _ = Auto_NLP(
 nlp_column, train, test, target, score_type,
modeltype,top_num_features=50, verbose=0 )
test_preds = pd.DataFrame({'imgs': test_set.image_files_list, 'preds': preds})
 test_preds['imgs'] = test_preds['imgs'].apply(lambda x: x.split('.')[0])
 sub = pd.read_csv('.. /input/sample_submission.csv')
 sub = pd.merge(sub, test_preds, left_on='id', right_on='imgs')
 sub = sub[['id', 'preds']]
sub.columns = ['id', 'label']
sub.head()<save_to_csv>
m4, feats4, trainm4, testm4 = Auto_ViML(train_nlp, target, test_nlp,
sample_submission=subm,
scoring_parameter='balanced-accuracy',
hyper_param='GS',feature_reduction=True,
Boosting_Flag="CatBoost",Binning_Flag=False,
Add_Poly=0, Stacking_Flag=False,Imbalanced_Flag=False,
verbose=2 )
sub.to_csv('sub.csv', index=False )<define_variables>
subm[target] = testm4['Survived_CatBoost_predictions'].values.astype(int)
 subm.head()
NUM_TTA = 8<feature_engineering>
subm.to_csv('sample_submission4.csv',index=False )
sigmoid = lambda x: scipy.special.expit(x )<create_dataframe>
m2, feats2, trainm2, testm2 = Auto_ViML(train_nlp, target, test_nlp,
sample_submission=subm,
scoring_parameter='balanced-accuracy',
hyper_param='GS',feature_reduction=True,
Boosting_Flag=None,Binning_Flag=False,
Add_Poly=0, Stacking_Flag=False,
Imbalanced_Flag=False,
verbose=2 )
for num_tta in range(NUM_TTA):
if num_tta==0:
test_set = CancerDataset(datafolder=base_dir+'test/', datatype='test', transform=data_transforms_test)
 test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, num_workers=num_workers)
 elif num_tta==1:
test_set = CancerDataset(datafolder=base_dir+'test/', datatype='test', transform=data_transforms_tta1)
 test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, num_workers=num_workers)
 elif num_tta==2:
test_set = CancerDataset(datafolder=base_dir+'test/', datatype='test', transform=data_transforms_tta2)
 test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, num_workers=num_workers)
 elif num_tta==3:
test_set = CancerDataset(datafolder=base_dir+'test/', datatype='test', transform=data_transforms_tta3)
 test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, num_workers=num_workers)
 else:
test_set = CancerDataset(datafolder=base_dir+'test/', datatype='test', transform=data_transforms_tta0)
 test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, num_workers=num_workers)
 preds = []
for batch_i,(data, target)in enumerate(test_loader):
data, target = data.cuda() , target.cuda()
output = model_conv(data ).detach()
pr = output[:,0].cpu().numpy()
for i in pr:
preds.append(sigmoid(i)/NUM_TTA)
 if num_tta==0:
test_preds = pd.DataFrame({'imgs': test_set.image_files_list, 'preds': preds})
 test_preds['imgs'] = test_preds['imgs'].apply(lambda x: x.split('.')[0])
 else:
test_preds['preds']+=np.array(preds)
 sub = pd.read_csv('.. /input/sample_submission.csv')
 sub = pd.merge(sub, test_preds, left_on='id', right_on='imgs')
 sub = sub[['id', 'preds']]
sub.columns = ['id', 'label']
sub.to_csv('sub_tta.csv', index=False )<train_model>
subm[target] =(testm2['Survived_proba_1']>0.5 ).astype(int ).values
subm.head()
print("Finished" )<set_options>
subm.to_csv('sample_submission2.csv',index=False )
%matplotlib inline 
<init_hyperparams>
m3, feats3, trainm3, testm3 = Auto_ViML(train_nlp, target, test_nlp,
sample_submission=subm,
scoring_parameter='balanced-accuracy',
hyper_param='RS',feature_reduction=True,
Boosting_Flag=True,Binning_Flag=False,
Add_Poly=0, Stacking_Flag=False,
Imbalanced_Flag=False,
verbose=2 )
num_epochs = 8
num_classes = 2
batch_size = 128
learning_rate = 0.002
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu' )<load_from_csv>
subm[target] =(testm3['Survived_proba_1']>0.5 ).astype(int ).values
subm.head()
<split><EOS> 
subm.to_csv('sample_submission3.csv',index=False )