Kaggle competition: Titanic - Machine Learning from Disaster
epochs = 85
Costs = []
Accuracy = []
deepNet = deepNet.train()
for epoch in range(epochs):
acc = 0
count = 0
for i, batch in enumerate(train_loader):
count += 1
images, label = batch
images = images.to(device)
 label['Vowel'] = label['Vowel'].to(device ).long()
label['Consonant'] = label['Consonant'].to(device ).long()
cnnet_optim.zero_grad()
out_1, out_2 = deepNet(images)
 loss_1 = cnn_ce_loss(out_1, label['Vowel'])
 loss_2 = cnn_ce_loss(out_2, label['Consonant'])
 loss = loss_1 + loss_2
loss.backward()
cnnet_optim.step()
cost = loss.item()
acc1 = get_accuracy(out_1, label['Vowel'], images.shape[0])
 acc2 = get_accuracy(out_2, label['Consonant'], images.shape[0])
 acc = acc +(acc1 + acc2)/2
Accuracy.append(acc/(10*count))
Costs.append(cost)
 print("Epoch [{}/{}], Loss : {}, Accuracy : {}, acc_1 : {}, acc_2 : {}".format(( epoch+1), epochs,
round(float(cost), 2),
round(float(Accuracy[-1].cpu()), 2),
round(float(acc1.cpu()), 2),
round(float(acc2.cpu()), 2)))
 plt.title("Loss and Accuracy with iterations")
 plt.plot(Costs, label = 'Cost')
 plt.plot(Accuracy, label = 'Accuracy')
 plt.xlabel("Iterations")
 plt.ylabel("Loss and Accuracy")
 plt.legend()
plt.show()
count = 0
acc = 0
deepNet = deepNet.eval()
for i, batch in enumerate(train_loader):
count += 1
images, label = batch
images = images.to(device)
 label['Vowel'] = label['Vowel'].to(device ).long()
label['Consonant'] = label['Consonant'].to(device ).long()
out_1, out_2 = deepNet(images)
 out_1, out_2 = F.log_softmax(out_1, dim = 1), F.log_softmax(out_2, dim = 1)
 acc1 = get_accuracy(out_1, label['Vowel'], images.shape[0])
 acc2 = get_accuracy(out_2, label['Consonant'], images.shape[0])
 acc = acc +(acc1 + acc2)/2
print("Train Accuracy : {}".format(acc/count))<compute_train_metric>
%matplotlib inline
deepNet = deepNet.eval()
count = 0
acc = 0
for i, batch in enumerate(validation_loader):
count += 1
images, label = batch
images = images.to(device)
 label['Vowel'] = label['Vowel'].to(device ).long()
label['Consonant'] = label['Consonant'].to(device ).long()
out_1, out_2 = deepNet(images)
 out_1, out_2 = F.log_softmax(out_1, dim = 1), F.log_softmax(out_2, dim = 1)
 acc1 = get_accuracy(out_1, label['Vowel'], images.shape[0])
 acc2 = get_accuracy(out_2, label['Consonant'], images.shape[0])
 acc = acc +(acc1 + acc2)/2
print("Test Accuracy : {}".format(acc/count)) 
<create_dataframe>
train= pd.read_csv('/kaggle/input/titanic/train.csv' )
test_data = HindiDataset(train_path, test_path, transform = transforms.Compose([transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]),
train = False)
 testset = DataLoader(test_data, batch_size = 32, shuffle = False )<find_best_params>
def impute_age(cols):
Age = cols[0]
Pclass = cols[1]
if pd.isnull(Age):
if Pclass==1:
return 37
elif Pclass == 2:
return 29
else :
return 24
else:
return Age
preds = {}
img_ids = []
F_results = []
deepNet = deepNet.eval()
for i, batch in enumerate(testset):
images, img_names = batch
images = images.to(device)
 out_1, out_2 = deepNet(images)
 out_1, out_2 = F.log_softmax(out_1, dim = 1), F.log_softmax(out_2, dim = 1)
 out_1, out_2 = torch.max(out_1, dim=1)[1].cpu() , torch.max(out_2, dim=1)[1].cpu()
for names,V,C in zip(img_names, out_1, out_2):
F_results.append('V' + str(int(V)) + '_C' + str(int(C)))
 img_ids.append(names)
 if i%50 == 0:
print("{} images tested.... ".format(( i + 1)* images.shape[0]))<save_to_csv>
train['Age']=train[['Age','Pclass']].apply(impute_age,axis = 1 )
submission = pd.DataFrame({'ImageId':img_ids, 'Class':F_results})
 submission = submission.sort_values(['ImageId'])
 submission.to_csv("submission.csv", index=False)
 submission.tail()<categorify>
train.drop('Cabin',inplace = True,axis =1 )
class VowelConsonantDataset(Dataset):
def __init__(self, file_path,train=True,transform=None):
self.transform = transform
self.file_path=file_path
self.train=train
self.file_names=[file for _,_,files in os.walk(self.file_path)for file in files]
self.len = len(self.file_names)
 if self.train:
self.classes_mapping=self.get_classes()
def __len__(self):
return len(self.file_names)
 def __getitem__(self, index):
file_name=self.file_names[index]
image_data=self.pil_loader(self.file_path+"/"+file_name)
 if self.transform:
image_data = self.transform(image_data)
 if self.train:
file_name_splitted=file_name.split("_")
 Y1 = self.classes_mapping[file_name_splitted[0]]
Y2 = self.classes_mapping[file_name_splitted[1]]
z1,z2=torch.zeros(10),torch.zeros(10)
 z1[Y1-10],z2[Y2]=1,1
label=torch.stack([z1,z2])
 return image_data, label
else:
return image_data, file_name
def pil_loader(self,path):
with open(path, 'rb')as f:
img = Image.open(f)
 return img.convert('RGB')
 def get_classes(self):
classes=[]
for name in self.file_names:
name_splitted=name.split("_")
 classes.extend([name_splitted[0],name_splitted[1]])
 classes=list(set(classes))
classes_mapping={}
for i,cl in enumerate(sorted(classes)) :
classes_mapping[cl]=i
return classes_mapping 
<set_options>
sex = pd.get_dummies(train['Sex'],drop_first=True)
 embark = pd.get_dummies(train['Embarked'],drop_first=True )
train_on_gpu = torch.cuda.is_available()<categorify>
train = pd.concat([train,sex,embark],axis=1 )
transform_train = transforms.Compose([
transforms.ColorJitter() ,
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor() ,
] )<load_pretrained>
train.drop(['Sex','Embarked','PassengerId','Name','Ticket'],axis=1,inplace=True )
full_data = VowelConsonantDataset(".. /input/train/train",train=True,transform=transform_train)
 train_size = int(0.9 * len(full_data))
test_size = len(full_data)- train_size
train_data, validation_data = random_split(full_data, [train_size, test_size])
 train_loader = torch.utils.data.DataLoader(train_data, batch_size=50, shuffle=True)
 validation_loader = torch.utils.data.DataLoader(validation_data, batch_size=50, shuffle=True )<create_dataframe>
st = StandardScaler()
test_data = VowelConsonantDataset(".. /input/test/test",train=False,transform=transform_train)
 test_loader = torch.utils.data.DataLoader(test_data, batch_size=20,shuffle=False )<set_options>
feature_scale = ['Age','Fare']
train[feature_scale] = st.fit_transform(train[feature_scale] )
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 print(device )<choose_model_class>
x = train.drop(['Survived'],axis=1)
 y = train['Survived']
class MyModel(nn.Module):
def __init__(self, num_classes1, num_classes2):
super(MyModel, self ).__init__()
self.model_snet = models.mobilenet_v2(pretrained=True)
 self.model_snet.classifier = nn.Sequential(nn.Dropout(p=0.2))
self.fc1 = nn.Linear(1280, num_classes1,bias=True)
 torch.nn.init.xavier_uniform_(self.fc1.weight)
 torch.nn.init.zeros_(self.fc1.bias)
 self.fc2 = nn.Linear(1280, num_classes2,bias=True)
 torch.nn.init.xavier_uniform_(self.fc2.weight)
 torch.nn.init.zeros_(self.fc2.bias)
 def forward(self, x):
x = self.model_snet(x)
 out1 = self.fc1(x)
 out2 = self.fc2(x)
 return out1, out2<choose_model_class>
from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
my_model = MyModel(10,10 )<train_model>
tree = DecisionTreeClassifier()
tree.fit(x,y)
 tree.score(x,y )
my_model = my_model.to(device )<categorify>
test = pd.read_csv('/kaggle/input/titanic/test.csv' )
def evaluation(dataloader,model):
total,correct=0,0
for data in dataloader:
inputs,labels=data
inputs,labels=inputs.to(device),labels.to(device)
 out1,out2=my_model(inputs)
 _,pred1=torch.max(out1.data,1)
 _,pred2=torch.max(out2.data,1)
 _,labels1=torch.max(labels[:,0,:].data,1)
 _,labels2=torch.max(labels[:,1,:].data,1)
 total+=labels.size(0)
 fin1=(pred1==labels1)
 fin2=(pred2==labels2)
 correct+=(fin1==fin2 ).sum().item()
return 100*correct/total<choose_model_class>
test2 = test.copy()
loss_fn = nn.CrossEntropyLoss()
plist = [
{'params': my_model.fc1.parameters() , 'lr': 5e-3},
{'params': my_model.fc2.parameters() , 'lr': 5e-3}
]
lr=0.01
opt = optim.SGD(my_model.parameters() ,lr=0.01,momentum=0.9,nesterov=True)
<train_model>
test['Age']=test[['Age','Pclass']].apply(impute_age,axis = 1 )
loss_epoch_arr = []
loss_arr = []
max_epochs = 10
min_loss = 1000
batch_size = 50
n_iters = np.ceil(9000/batch_size)
 for epoch in range(max_epochs):
for i, data in enumerate(train_loader, 0):
my_model.train()
images, labels = data
images = images.to(device)
 targetnp=labels[:,0,:].cpu().numpy()
targetnp1 = labels[:,1,:].cpu().numpy()
new_targets1 = np.argmax(targetnp,axis=1)
 new_targets2 = np.argmax(targetnp1,axis=1)
 new_targets1=torch.LongTensor(new_targets1)
 new_targets2=torch.LongTensor(new_targets2)
 new_targets1 = new_targets1.to(device)
 new_targets2 = new_targets2.to(device)
 opt.zero_grad()
out = my_model.forward(images)
 loss_fc1 = loss_fn(out[0], new_targets1)
 loss_fc2 = loss_fn(out[1],new_targets2)
 loss = torch.add(loss_fc1,loss_fc2)
 loss.backward()
opt.step()
if min_loss > loss.item() :
min_loss = loss.item()
best_model = copy.deepcopy(my_model.state_dict())
 print('Min loss %0.2f' % min_loss)
 if i % 100 == 0:
print('Iteration: %d/%d, Loss: %0.2f' %(i, n_iters, loss.item()))
del images, labels, out
torch.cuda.empty_cache()
loss_arr.append(loss.item())
 print("Epoch number :",epoch)
 print("Train Accuracy :",evaluation(train_loader,my_model))
print("Test Accuracy :" ,evaluation(validation_loader,my_model))
print('Learning Rate :',lr)
 loss_epoch_arr.append(loss.item())
 plt.plot(loss_arr)
 plt.show()<compute_test_metric>
test.drop('Cabin',inplace = True,axis =1 )
evaluation(validation_loader,my_model )<find_best_params>
sex = pd.get_dummies(test['Sex'],drop_first=True)
 embark = pd.get_dummies(test['Embarked'],drop_first=True )
my_model.eval()
plist=[]
fn_list=[]
for inputs_test, fn in test_loader:
inputs_test=inputs_test.to(device)
 out1,out2=my_model.forward(inputs_test)
 _,pred1=torch.max(out1,1)
 pred1=pred1.tolist()
_,pred2=torch.max(out2,1)
 pred2=pred2.tolist()
for x,y,z in zip(pred1,pred2,fn):
p="V"+str(x)+"_"+"C"+str(y)
 plist.append(p)
 fn_list.append(z )<save_to_csv>
test = pd.concat([test,sex,embark],axis=1 )
submission = pd.DataFrame({"ImageId":fn_list, "Class":plist})
 submission.to_csv('submission.csv', index=False )<categorify>
test.drop(['Sex','Embarked','PassengerId','Name','Ticket'],axis=1,inplace=True )
class VowelConsonantDataset(Dataset):
def __init__(self, file_path,train=True,transform=None):
self.transform = transform
self.file_path=file_path
self.train=train
self.file_names=[file for _,_,files in os.walk(self.file_path)for file in files]
self.len = len(self.file_names)
 if self.train:
self.classes_mapping=self.get_classes()
def __len__(self):
return len(self.file_names)
 def __getitem__(self, index):
file_name=self.file_names[index]
image_data=self.pil_loader(self.file_path+"/"+file_name)
 if self.transform:
image_data = self.transform(image_data)
 if self.train:
file_name_splitted=file_name.split("_")
 Y1 = self.classes_mapping[file_name_splitted[0]]
Y2 = self.classes_mapping[file_name_splitted[1]]
z1,z2=torch.zeros(10),torch.zeros(10)
 z1[Y1-10],z2[Y2]=1,1
label=torch.stack([z1,z2])
 return image_data, label
else:
return image_data, file_name
def pil_loader(self,path):
with open(path, 'rb')as f:
img = Image.open(f)
 return img.convert('RGB')
 def get_classes(self):
classes=[]
for name in self.file_names:
name_splitted=name.split("_")
 classes.extend([name_splitted[0],name_splitted[1]])
 classes=list(set(classes))
classes_mapping={}
for i,cl in enumerate(sorted(classes)) :
classes_mapping[cl]=i
return classes_mapping 
<set_options>
test['Fare'].fillna(test['Fare'].mean() ,inplace=True )
train_on_gpu = torch.cuda.is_available()<categorify>
feature_scale = ['Age','Fare']
test[feature_scale] = st.fit_transform(test[feature_scale] )
transform_train = transforms.Compose([
transforms.ColorJitter() ,
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor() ,
] )<split>
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
full_data = VowelConsonantDataset(".. /input/padhai-hindivowelconsonantdataset/train/train",train=True,transform=transform_train)
 train_size = int(0.9 * len(full_data))
test_size = len(full_data)- train_size
train_data, validation_data = random_split(full_data, [train_size, test_size])
 train_loader = torch.utils.data.DataLoader(train_data, batch_size=50, shuffle=True)
 validation_loader = torch.utils.data.DataLoader(validation_data, batch_size=50, shuffle=True)
<create_dataframe>
level1 = LogisticRegression()
model = StackingClassifier(estimators=level0,final_estimator=level1,cv=5 )
test_data = VowelConsonantDataset(".. /input/padhai-hindivowelconsonantdataset/test/test",train=False,transform=transform_train)
 test_loader = torch.utils.data.DataLoader(test_data, batch_size=20,shuffle=False)
 print(len(test_data))<set_options>
level1 = LogisticRegression()
model = StackingClassifier(estimators=level0,final_estimator=level1,cv=5 )
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 print(device )<choose_model_class>
model.fit(x,y )
class MyModel(nn.Module):
def __init__(self, num_classes1, num_classes2):
super(MyModel, self ).__init__()
self.model_snet = models.mobilenet_v2(pretrained=True)
 self.model_snet.classifier = nn.Sequential(nn.Dropout(p=0.2))
self.fc1 = nn.Linear(1280, num_classes1,bias=True)
 torch.nn.init.xavier_uniform_(self.fc1.weight)
 torch.nn.init.zeros_(self.fc1.bias)
 self.fc2 = nn.Linear(1280, num_classes2,bias=True)
 torch.nn.init.xavier_uniform_(self.fc2.weight)
 torch.nn.init.zeros_(self.fc2.bias)
 def forward(self, x):
x = self.model_snet(x)
 out1 = self.fc1(x)
 out2 = self.fc2(x)
 return out1, out2<choose_model_class>
y_predicted = model.predict(test )
my_model = MyModel(10,10 )<train_model>
submission = pd.DataFrame({
"PassengerId":test2['PassengerId'],
"Survived":y_predicted
} )
my_model = my_model.to(device )<categorify>
submission.to_csv('first_kaggale_titanic_submission.csv',index=False )