Kaggle competition: Titanic - Machine Learning from Disaster
generator = train_datagen.flow_from_dataframe(train_labels.iloc[20:21],
directory = os.path.join(WORK_DIR, "train_images"),
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,
class_mode = "sparse")
 aug_images = [generator[0][0][0]/255 for i in range(10)]
fig, axes = plt.subplots(2, 5, figsize =(20, 10))
axes = axes.flatten()
for img, ax in zip(aug_images, axes):
ax.imshow(img)
 ax.axis('off')
 plt.tight_layout()
plt.show()<choose_model_class>
train_df = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_df = pd.read_csv('/kaggle/input/titanic/test.csv')
 passenger_ids = test_df['PassengerId']
<choose_model_class>
train_df['Survived'].value_counts()
def create_model() :
conv_base = EfficientNetB0(include_top = False, weights = None,
input_shape =(TARGET_SIZE, TARGET_SIZE, 3))
model = conv_base.output
model = layers.GlobalAveragePooling2D()(model)
 model = layers.Dense(5, activation = "softmax" )(model)
 model = models.Model(conv_base.input, model)
 model.compile(optimizer = Adam(lr = 0.001),
loss = "sparse_categorical_crossentropy",
metrics = ["acc"])
 return model<choose_model_class>
hist_survived_vs_feature('SibSp' )
print('Our EfficientNet CNN has %d layers' %len(model.layers))<load_pretrained>
dataset = [train_df, test_df]
for df in dataset:
df['Title'] = df['Name'].map(lambda x: re.search(r'([A-Za-z]+)\.', x ).group().strip().replace('.', ''))
train_df['Title'].value_counts().index
model.load_weights('.. /input/cassava-leaf-disease-models/basic_EfNetB0_imagenet_512.h5' )<choose_model_class>
test_df['Title'].value_counts().index
model_save = ModelCheckpoint('./EffNetB0_512_8_best_weights.h5',
save_best_only = True,
save_weights_only = True,
monitor = 'val_loss',
mode = 'min', verbose = 1)
 early_stop = EarlyStopping(monitor = 'val_loss', min_delta = 0.001,
patience = 5, mode = 'min', verbose = 1,
restore_best_weights = True)
 reduce_lr = ReduceLROnPlateau(monitor = 'val_loss', factor = 0.3,
patience = 2, min_delta = 0.001,
mode = 'min', verbose = 1)
 history = model.fit(
 train_generator,
steps_per_epoch = STEPS_PER_EPOCH,
epochs = EPOCHS,
validation_data = validation_generator,
validation_steps = VALIDATION_STEPS,
callbacks = [model_save, early_stop, reduce_lr]
)<save_model>
title_mapping = {'Mr': 1, 'Miss': 2, 'Mrs': 3, 'Master': 4, 'Dr': 5, 'Rev': 5, 'Col': 5,
'Major': 5, 'Mlle': 5, 'Ms': 5, 'Countess': 5, 'Lady': 5, 'Capt': 5,
'Jonkheer': 5, 'Don': 5, 'Sir': 5, 'Mme': 5}
for df in dataset:
df['Title'] = df['Title'].map(title_mapping)
 train_df.head()
model.save('./EffNetB0_512_8.h5' )<predict_on_test>
def concat_dummies(feature_name):
global dataset
new_train_df = pd.concat([train_df, pd.get_dummies(train_df[feature_name], prefix=feature_name)], axis=1)
 new_test_df = pd.concat([test_df, pd.get_dummies(test_df[feature_name], prefix=feature_name)], axis=1)
 dataset = [new_train_df, new_test_df]
return new_train_df, new_test_df
def all_activations_vis(img, layers = 10):
layer_outputs = [layer.output for layer in model.layers[:layers]]
activation_model = models.Model(inputs = model.input, outputs = layer_outputs)
 activations = activation_model.predict(img)
 layer_names = []
for layer in model.layers[:layers]:
layer_names.append(layer.name)
 images_per_row = 3
for layer_name, layer_activation in zip(layer_names, activations):
n_features = layer_activation.shape[-1]
size = layer_activation.shape[1]
n_cols = n_features // images_per_row
display_grid = np.zeros(( size * n_cols, images_per_row * size))
for col in range(n_cols):
for row in range(images_per_row):
channel_image = layer_activation[0, :, :, col * images_per_row + row]
channel_image -= channel_image.mean()
channel_image /= channel_image.std()
channel_image *= 64
channel_image += 128
channel_image = np.clip(channel_image, 0, 255 ).astype('uint8')
 display_grid[col * size :(col + 1)* size,
row * size :(row + 1)* size] = channel_image
scale = 1./ size
plt.figure(figsize=(scale * 5 * display_grid.shape[1],
scale * 5 * display_grid.shape[0]))
plt.title(layer_name)
 plt.grid(False)
 plt.axis('off')
 plt.imshow(display_grid, aspect = 'auto', cmap = 'viridis' )<load_from_csv>
train_df, test_df = concat_dummies('Title')
 train_df.head()
ss = pd.read_csv(os.path.join(WORK_DIR, "sample_submission.csv"))
ss<predict_on_test>
sex_mapping = {'male': 0, 'female': 1}
for df in dataset:
df['Sex'] = df['Sex'].map(sex_mapping)
 train_df.head()
preds = []
for image_id in ss.image_id:
image = Image.open(os.path.join(WORK_DIR, "test_images", image_id))
image = image.resize(( TARGET_SIZE, TARGET_SIZE))
image = np.expand_dims(image, axis = 0)
 preds.append(np.argmax(model.predict(image)))
 ss['label'] = preds
ss<save_to_csv>
train_df, test_df = concat_dummies('AgeGroup')
 train_df.head()
ss.to_csv('submission.csv', index = False )<define_variables>
def scale_feature(feature):
result = []
for df in dataset:
feature_val = df[feature]
max_val = feature_val.max()
min_val = feature_val.min()
scaled_feature =(feature_val - min_val)/(max_val - min_val)
 result.append(scaled_feature)
 return result
train_df['SibSp'], test_df['SibSp'] = scale_feature('SibSp')
 train_df['Parch'], test_df['Parch'] = scale_feature('Parch')
 train_df.head()
package_path = '.. /input/vision-transformer-pytorch/VisionTransformer-Pytorch'
sys.path.append(package_path)
<define_variables>
test_df['Fare'] = test_df['Fare'].fillna(test_df.groupby('Pclass')['Fare'].transform('median'))
train_df['Fare'], test_df['Fare'] = scale_feature('Fare')
 train_df.head()
package_path = '.. /input/pytorch-image-models/pytorch-image-models-master'
sys.path.append(package_path )<import_modules>
train_df['Embarked'] = train_df['Embarked'].fillna('S')
 train_df, test_df = concat_dummies('Embarked')
 train_df.head()
from glob import glob
from sklearn.model_selection import GroupKFold, StratifiedKFold
import cv2
from skimage import io
import torch
from torch import nn
import os
from datetime import datetime
import time
import random
import cv2
import torchvision
from torchvision import transforms
import pandas as pd
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
from torch.utils.data import Dataset,DataLoader
from torch.utils.data.sampler import SequentialSampler, RandomSampler
from torch.cuda.amp import autocast, GradScaler
import sklearn
import warnings
import joblib
from sklearn.metrics import roc_auc_score, log_loss
from sklearn import metrics
import warnings
import cv2
import pydicom
import timm
from scipy.ndimage.interpolation import zoom
from sklearn.metrics import log_loss<init_hyperparams>
train_df = train_df.drop(['PassengerId', 'Name', 'Age', 'Ticket', 'Cabin', 'Embarked',
'Title', 'AgeGroup'], axis=1)
 test_df = test_df.drop(['PassengerId', 'Name', 'Age', 'Ticket', 'Cabin', 'Embarked',
'Title', 'AgeGroup'], axis=1)
 train_df.head()
CFG = {
'fold_num': 10,
'seed': 719,
'model_arch': 'tf_efficientnet_b3_ns',
'img_size': 384,
'epochs': 32,
'train_bs': 28,
'valid_bs': 32,
'lr': 1e-4,
'num_workers': 4,
'accum_iter': 1,
'verbose_step': 1,
'device': 'cuda:0',
'tta': 3,
'used_epochs': [6,7,8,9],
'weights': [1,1,1,1]
}<load_from_csv>
X = train_df.iloc[:, 1:].values
y = train_df['Survived'].values
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=1)
 X_test = test_df.values
train = pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv')
 train.head()<count_values>
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dropout(0.25, input_shape=[20]))
model.add(tf.keras.layers.Dense(25, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
 model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
 model.summary()
train.label.value_counts()<load_from_csv>
history = model.fit(
 x=X_train,
y=y_train,
epochs=10,
batch_size=1,
validation_data=(X_val, y_val)
 )
submission = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 submission.head()<categorify>
prediction = model.predict(X_test)
 rounded_prediction = np.where(prediction >= 0.5, 1, 0 ).flatten()
submission_df = pd.DataFrame({
'PassengerId': passenger_ids,
'Survived': rounded_prediction
})
 submission_df.head()
class CassavaDataset(Dataset):
def __init__(
 self, df, data_root, transforms=None, output_label=True 
):
super().__init__()
self.df = df.reset_index(drop=True ).copy()
self.transforms = transforms
self.data_root = data_root
self.output_label = output_label
def __len__(self):
return self.df.shape[0]
def __getitem__(self, index: int):
if self.output_label:
target = self.df.iloc[index]['label']
path = "{}/{}".format(self.data_root, self.df.iloc[index]['image_id'])
 img = get_img(path)
 if self.transforms:
img = self.transforms(image=img)['image']
if self.output_label == True:
return img, target
else:
return img<import_modules>
submission_df.to_csv('submission.csv', index=False )