Kaggle competition: Titanic - Machine Learning from Disaster
submission = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 submission.head()<categorify>
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
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>
train_data = pd.read_csv('.. /input/titanic/train.csv')
 test_data = pd.read_csv('.. /input/titanic/test.csv')
 train = train_data.copy()
test = test_data.copy()
HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize 
)
 HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize 
)
 def get_train_transforms() :
return Compose([
RandomResizedCrop(CFG['img_size'], CFG['img_size']),
Transpose(p=0.5),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
ShiftScaleRotate(p=0.5),
HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),
RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
CoarseDropout(p=0.5),
Cutout(p=0.5),
ToTensorV2(p=1.0),
], p=1.)
def get_valid_transforms() :
return Compose([
CenterCrop(CFG['img_size'], CFG['img_size'], p=1.) ,
Resize(CFG['img_size'], CFG['img_size']),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
], p=1.)
def get_inference_transforms() :
return Compose([
RandomResizedCrop(CFG['img_size'], CFG['img_size']),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
], p=1.)<choose_model_class>
train.drop('PassengerId', axis = 1, inplace = True )
class CassvaImgClassifier(nn.Module):
def __init__(self, model_arch, n_class, pretrained=False):
super().__init__()
self.model = timm.create_model(model_arch, pretrained=pretrained)
 n_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(n_features, n_class)
 def forward(self, x):
x = self.model(x)
 return x<create_dataframe>
test.drop('PassengerId', axis = 1, inplace = True)
 pred = train['Survived']
if __name__ == '__main__':
seed_everything(CFG['seed'])
 folds = StratifiedKFold(n_splits=CFG['fold_num'] ).split(np.arange(train.shape[0]), train.label.values)
 for fold,(trn_idx, val_idx)in enumerate(folds):
if fold > 0:
break
print('Inference fold {} started'.format(fold))
valid_ = train.loc[val_idx,:].reset_index(drop=True)
 valid_ds = CassavaDataset(valid_, '.. /input/cassava-leaf-disease-classification/train_images/', transforms=get_inference_transforms() , output_label=False)
 test = pd.DataFrame()
test['image_id'] = list(os.listdir('.. /input/cassava-leaf-disease-classification/test_images/'))
test_ds = CassavaDataset(test, '.. /input/cassava-leaf-disease-classification/test_images/', transforms=get_inference_transforms() , output_label=False)
 val_loader = torch.utils.data.DataLoader(
 valid_ds,
batch_size=CFG['valid_bs'],
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=False, 
)
 tst_loader = torch.utils.data.DataLoader(
 test_ds,
batch_size=CFG['valid_bs'],
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=False, 
)
 device = torch.device(CFG['device'])
 model = CassvaImgClassifier(CFG['model_arch'], train.label.nunique() ).to(device)
 val_preds = []
tst_preds = []
for i, epoch in enumerate(CFG['used_epochs']):
model.load_state_dict(torch.load('.. /input/fork-pytorch-efficientnet-baseline-train-amp-a/{}_fold_{}_{}'.format(CFG['model_arch'], fold, epoch)))
 with torch.no_grad() :
for _ in range(CFG['tta']):
tst_preds += [CFG['weights'][i]/sum(CFG['weights'])/CFG['tta']*inference_one_epoch(model, tst_loader, device)]
tst_preds = np.mean(tst_preds, axis=0)
 tst_preds = softmax(tst_preds)
 test['image_id'] = glob.glob('.. /input/cassava-leaf-disease-classification/test_images/*.jpg')
 del model
torch.cuda.empty_cache()<feature_engineering>
train.isnull().sum()
test['label'] = np.argmax(tst_preds, axis=1)
<feature_engineering>
train.isnull().sum()
def rect_path(path):
return '.. /input/cassava-leaf-disease-classification/train_images/'+path
train['image_id'] = train['image_id'].apply(rect_path )<concatenate>
test.isnull().sum()
train = pd.concat([train,test] ).reset_index()
train<create_dataframe>
test.isnull().sum()
class CassavaPseDataset(Dataset):
def __init__(self, df, data_root,
transforms=None,
output_label=True,
one_hot_label=False,
do_fmix=False,
fmix_params={
'alpha': 1.,
'decay_power': 3.,
'shape':(CFG['img_size'], CFG['img_size']),
'max_soft': True,
'reformulate': False
},
do_cutmix=False,
cutmix_params={
'alpha': 1,
} 
):
super().__init__()
self.df = df.reset_index(drop=True ).copy()
self.transforms = transforms
self.data_root = data_root
self.do_fmix = do_fmix
self.fmix_params = fmix_params
self.do_cutmix = do_cutmix
self.cutmix_params = cutmix_params
self.output_label = output_label
self.one_hot_label = one_hot_label
if output_label == True:
self.labels = self.df['label'].values
if one_hot_label is True:
self.labels = np.eye(self.df['label'].max() +1)[self.labels]
def __len__(self):
return self.df.shape[0]
def __getitem__(self, index: int):
if self.output_label:
target = self.labels[index]
img = get_img(self.df.loc[index]['image_id'])
 if self.transforms:
img = self.transforms(image=img)['image']
if self.do_fmix and np.random.uniform(0., 1., size=1)[0] > 0.5:
with torch.no_grad() :
lam = np.clip(np.random.beta(self.fmix_params['alpha'], self.fmix_params['alpha']),0.6,0.7)
 mask = make_low_freq_image(self.fmix_params['decay_power'], self.fmix_params['shape'])
 mask = binarise_mask(mask, lam, self.fmix_params['shape'], self.fmix_params['max_soft'])
 fmix_ix = np.random.choice(self.df.index, size=1)[0]
fmix_img = get_img("{}/{}".format(self.data_root, self.df.iloc[fmix_ix]['image_id']))
if self.transforms:
fmix_img = self.transforms(image=fmix_img)['image']
mask_torch = torch.from_numpy(mask)
 img = mask_torch*img+(1.-mask_torch)*fmix_img
rate = mask.sum() /CFG['img_size']/CFG['img_size']
target = rate*target +(1.-rate)*self.labels[fmix_ix]
if self.do_cutmix and np.random.uniform(0., 1., size=1)[0] > 0.5:
with torch.no_grad() :
cmix_ix = np.random.choice(self.df.index, size=1)[0]
cmix_img = get_img("{}/{}".format(self.data_root, self.df.iloc[cmix_ix]['image_id']))
if self.transforms:
cmix_img = self.transforms(image=cmix_img)['image']
lam = np.clip(np.random.beta(self.cutmix_params['alpha'], self.cutmix_params['alpha']),0.3,0.4)
 bbx1, bby1, bbx2, bby2 = rand_bbox(( CFG['img_size'], CFG['img_size']), lam)
 img[:, bbx1:bbx2, bby1:bby2] = cmix_img[:, bbx1:bbx2, bby1:bby2]
rate = 1 -(( bbx2 - bbx1)*(bby2 - bby1)/(CFG['img_size'] * CFG['img_size']))
target = rate*target +(1.-rate)*self.labels[cmix_ix]
if self.output_label == True:
return img, target
else:
return img<create_dataframe>
train['Age'].fillna(train['Age'].quantile(0.5), inplace = True)
 test['Age'].fillna(test['Age'].quantile(0.5), inplace = True )
def prepare_dataloader(df, trn_idx, val_idx, data_root='.. /input/cassava-leaf-disease-classification/train_images/'):
train_ = df.loc[trn_idx,:].reset_index(drop=True)
 valid_ = df.loc[val_idx,:].reset_index(drop=True)
 train_ds = CassavaPseDataset(train_, data_root, transforms=get_train_transforms() , output_label=True, one_hot_label=False, do_fmix=False, do_cutmix=False)
 valid_ds = CassavaPseDataset(valid_, data_root, transforms=get_valid_transforms() , output_label=True)
 train_loader = torch.utils.data.DataLoader(
 train_ds,
batch_size=CFG['train_bs'],
pin_memory=False,
drop_last=False,
shuffle=True,
num_workers=CFG['num_workers'], 
)
 val_loader = torch.utils.data.DataLoader(
 valid_ds,
batch_size=CFG['valid_bs'],
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=False, 
)
 return train_loader, val_loader
def train_one_epoch(epoch, model, loss_fn, optimizer, train_loader, device, scheduler=None, schd_batch_update=False):
model.train()
t = time.time()
running_loss = None
pbar = tqdm(enumerate(train_loader), total=len(train_loader))
for step,(imgs, image_labels)in pbar:
imgs = imgs.to(device ).float()
target_ = image_labels.numpy()
image_labels = image_labels.to(device ).long()
with autocast() :
image_preds = model(imgs)
 loss = loss_fn(image_preds, image_labels)
 scaler.scale(loss ).backward()
if running_loss is None:
running_loss = loss.item()
else:
running_loss = running_loss *.99 + loss.item() *.01
if(( step + 1)% CFG['accum_iter'] == 0)or(( step + 1)== len(train_loader)) :
scaler.step(optimizer)
 scaler.update()
optimizer.zero_grad()
if scheduler is not None and schd_batch_update:
scheduler.step()
if(( step + 1)% CFG['verbose_step'] == 0)or(( step + 1)== len(train_loader)) :
description = f'epoch {epoch} loss: {running_loss:.4f}'
pbar.set_description(description)
 if scheduler is not None and not schd_batch_update:
scheduler.step()
def valid_one_epoch(epoch, model, loss_fn, val_loader, device, scheduler=None, schd_loss_update=False):
model.eval()
t = time.time()
loss_sum = 0
sample_num = 0
image_preds_all = []
image_targets_all = []
pbar = tqdm(enumerate(val_loader), total=len(val_loader))
for step,(imgs, image_labels)in pbar:
imgs = imgs.to(device ).float()
image_labels = image_labels.to(device ).long()
image_preds = model(imgs)
 image_preds_all += [torch.argmax(image_preds, 1 ).detach().cpu().numpy() ]
image_targets_all += [image_labels.detach().cpu().numpy() ]
loss = loss_fn(image_preds, image_labels)
 loss_sum += loss.item() *image_labels.shape[0]
sample_num += image_labels.shape[0]
if(( step + 1)% CFG['verbose_step'] == 0)or(( step + 1)== len(val_loader)) :
description = f'epoch {epoch} loss: {loss_sum/sample_num:.4f}'
pbar.set_description(description)
 image_preds_all = np.concatenate(image_preds_all)
 image_targets_all = np.concatenate(image_targets_all)
 print('validation multi-class accuracy = {:.4f}'.format(( image_preds_all==image_targets_all ).mean()))
if scheduler is not None:
if schd_loss_update:
scheduler.step(loss_sum/sample_num)
 else:
scheduler.step()<train_model>
train['Embarked'].fillna('S', inplace = True)
 test['Embarked'].fillna('S', inplace = True )
if __name__ == '__main__':
seed_everything(CFG['seed'])
 folds = StratifiedKFold(n_splits=CFG['fold_num'], shuffle=True, random_state=CFG['seed'] ).split(np.arange(train.shape[0]), train.label.values)
 for fold,(trn_idx, val_idx)in enumerate(folds):
if fold>0:
break
test = pd.DataFrame()
test['image_id'] = list(os.listdir('.. /input/cassava-leaf-disease-classification/test_images/'))
print('Training with {} started'.format(fold))
print(len(trn_idx), len(val_idx))
train_loader, val_loader = prepare_dataloader(train, trn_idx, val_idx, data_root='.. /input/cassava-leaf-disease-classification/train_images/')
 device = torch.device(CFG['device'])
 tst_preds = []
loss_tr = nn.CrossEntropyLoss().to(device)
 loss_fn = nn.CrossEntropyLoss().to(device)
 for i, epoch in enumerate(CFG['used_epochs']):
model.load_state_dict(torch.load('.. /input/fork-pytorch-efficientnet-baseline-train-amp-a/{}_fold_{}_{}'.format(CFG['model_arch'], fold, epoch)))
 model = CassvaImgClassifier(CFG['model_arch'], train.label.nunique() , pretrained=False ).to(device)
 scaler = GradScaler()
optimizer = torch.optim.Adam(model.parameters() , lr=CFG['lr'], weight_decay=CFG['weight_decay'])
 scheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizer, T_0=CFG['T_0'], T_mult=1, eta_min=CFG['min_lr'], last_epoch=-1)
 for _ in range(CFG['PseEpochs']):
if len(test)>2:
train_one_epoch(epoch, model, loss_tr, optimizer, train_loader, device, scheduler=scheduler, schd_batch_update=False)
 with torch.no_grad() :
for _ in range(CFG['tta']):
tst_preds += [CFG['weights'][i]/sum(CFG['weights'])/CFG['tta']*inference_one_epoch(model, tst_loader, device)]
tst_preds = np.mean(tst_preds, axis=0)
 del model, optimizer, train_loader, val_loader, scaler, scheduler
torch.cuda.empty_cache()<feature_engineering>
train.isnull().sum()
test['label'] = np.argmax(tst_preds, axis=1)
 test.head()<save_to_csv>
train.isnull().sum()
test.to_csv('submission.csv', index=False )<install_modules>
test.isnull().sum()
!pip install --quiet /kaggle/input/kerasapplications
!pip install --quiet /kaggle/input/efficientnet-git<import_modules>
test.isnull().sum()
print("Tensorflow version " + tf.__version__ )<define_variables>
test['Fare'].fillna(test['Fare'].quantile(0.5), inplace = True )
strategy = tf.distribute.get_strategy()
AUTOTUNE = tf.data.experimental.AUTOTUNE
GCS_PATH = ".. /input/cassava-leaf-disease-classification"
IMAGE_SIZE = [512, 512]
RESIZE_IMAGE_SIZE = [512, 512]
CLASSES = ['0', '1', '2', '3', '4']
WEIGHTS_PATH = ".. /input/cassava-leaf-disease-resnet-weights/EfficientNetB4-best-08-0.8890.h5"
BATCH_SIZE = 16<set_options>
test.isnull().sum()
def seed_everything(seed=0):
random.seed(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 os.environ['TF_DETERMINISTIC_OPS'] = '1'
seed = 0
seed_everything(seed)
 warnings.filterwarnings('ignore' )<categorify>
test.isnull().sum()
def decode_image(image):
image = tf.image.decode_jpeg(image, channels=3)
 image = tf.cast(image, tf.float32)
 image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image<create_dataframe>
sex1 = pd.get_dummies(train['Sex'])
 sex2 = pd.get_dummies(test['Sex'] )
def load_dataset(filenames, labeled=True, ordered=False):
ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTOTUNE)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(partial(read_tfrecord, labeled=labeled), num_parallel_calls=AUTOTUNE)
 return dataset
def data_val_augment(image, label):
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 if not IMAGE_SIZE == RESIZE_IMAGE_SIZE:
image = tf.image.resize(image, RESIZE_IMAGE_SIZE)
 return image, label
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.map(data_val_augment, num_parallel_calls=AUTOTUNE)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTOTUNE)
 return dataset<groupby>
train.drop('Sex', axis = 1, inplace = True)
 test.drop('Sex', axis = 1, inplace = True)
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n )<categorify>
train = pd.concat([train, sex1], axis=1)
 test = pd.concat([test, sex2], axis=1 )
def read_tfrecord(example, labeled):
tfrecord_format = {
"image": tf.io.FixedLenFeature([], tf.string),
"target": tf.io.FixedLenFeature([], tf.int64)
 } if labeled else {
"image": tf.io.FixedLenFeature([], tf.string),
"image_name": tf.io.FixedLenFeature([], tf.string)
 }
example = tf.io.parse_single_example(example, tfrecord_format)
 image = decode_image(example['image'])
 if labeled:
label = tf.cast(example['target'], tf.int32)
 return image, label
idnum = example['image_name']
return image, idnum<predict_on_test>
train.drop('female', axis = 1, inplace = True)
 test.drop('female', axis = 1, inplace = True )
test_ds = get_test_dataset(ordered=True)
 print('Computing predictions...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities = trained_model.predict(test_images_ds)
 predictions = np.argmax(probabilities, axis=-1)
 print(predictions )<save_to_csv>
Embarked1 = pd.get_dummies(train['Embarked'])
 Embarked2 = pd.get_dummies(test['Embarked'])
 train.drop(['Embarked'], axis = 1, inplace = True)
 test.drop(['Embarked'], axis = 1, inplace = True)
 train = pd.concat([train, Embarked1], axis=1)
 test = pd.concat([test, Embarked2], axis=1 )
print('Generating submission.csv file...')
 NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions]), fmt=['%s', '%d'], delimiter=',', header='image_id,label', comments='')
 !head submission.csv<set_options>
def family(x):
if x['SibSp'] + x['Parch'] > 1:
return 1
else:
return 0
train['Family'] = train.apply(family, axis=1)
 test['Family'] =test.apply(family, axis = 1 )
warnings.simplefilter(action = 'ignore', category = FutureWarning)
 print("Tensorflow version " + tf.__version__ )<define_variables>
train.drop(['SibSp','Parch'], axis=1, inplace=True)
 test.drop(['SibSp','Parch'], axis=1, inplace=True)
def seed_everything(seed=0):
random.seed(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 os.environ['TF_DETERMINISTIC_OPS'] = '1'
SEED = 414
seed_everything(SEED )<set_options>
train['Cabin'] = pd.Series(i[0] if not pd.isnull(i)else 'X' for i in train['Cabin'])
 test['Cabin'] = pd.Series(i[0] if not pd.isnull(i)else 'X' for i in test['Cabin'] )
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
REPLICAS = strategy.num_replicas_in_sync
print("REPLICAS: ", REPLICAS )<define_variables>
train['Cabin'] = train['Cabin'].map({
'X': 0,
'A': 1,
'B': 2,
'C': 3,
'D': 4,
'E': 5,
'F': 6,
'G': 7,
'T': 0
})
 train['Cabin'] = train['Cabin'].astype(int)
 test['Cabin'] = test['Cabin'].map({
'X': 0,
'A': 1,
'B': 2,
'C': 3,
'D': 4,
'E': 5,
'F': 6,
'G': 7,
'T': 0
})
 test['Cabin'] = test['Cabin'].astype(int )
GCS_DS_PATH = '.. /input/cassava-leaf-disease-classification'
print(GCS_DS_PATH )<define_variables>
train_title = [i.split(",")[1].split(".")[0].strip() for i in train["Name"]]
train["Title"] = pd.Series(train_title)
 test_title = [i.split(",")[1].split(".")[0].strip() for i in test["Name"]]
test["Title"] = pd.Series(test_title )
BATCH_SIZE = 16 * REPLICAS
WARMUP_EPOCHS = 3
WARMUP_LEARNING_RATE = 1e-4 * REPLICAS
EPOCHS = 20
LEARNING_RATE = 5e-5 * REPLICAS
ES_PATIENCE = 5
CHANNELS = 3
N_CLASSES = 5
DIM = 512
HEIGHT = 512
WIDTH = 512
CLASSES = ['0', '1', '2', '3', '4']
AUTO = tf.data.experimental.AUTOTUNE<define_variables>
train = train.drop(['Name'], axis = 1)
 test = test.drop(['Name'], axis = 1 )
ROT_ = 180.0
SHR_ = 2.0
HZOOM_ = 8.0
WZOOM_ = 8.0
HSHIFT_ = 8.0
WSHIFT_ = 8.0<normalization>
train["Title"] = train["Title"].replace(['Lady', 'the Countess','Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 train["Title"] = train["Title"].map({"Master":0, "Miss":1, "Ms" : 1 , "Mme":1, "Mlle":1, "Mrs":1, "Mr":2, "Rare":3})
 train["Title"] = train["Title"].astype(int)
 test["Title"] = test["Title"].replace(['Lady', 'the Countess','Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 test["Title"] = test["Title"].map({"Master":0, "Miss":1, "Ms" : 1 , "Mme":1, "Mlle":1, "Mrs":1, "Mr":2, "Rare":3})
 test["Title"] = test["Title"].astype(int )
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = tf.reshape(tf.concat([c1,s1,zero, -s1,c1,zero, zero,zero,one],axis=0),[3,3])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = tf.reshape(tf.concat([one,s2,zero, zero,c2,zero, zero,zero,one],axis=0),[3,3])
 zoom_matrix = tf.reshape(tf.concat([one/height_zoom,zero,zero, zero,one/width_zoom,zero, zero,zero,one],axis=0),[3,3])
 shift_matrix = tf.reshape(tf.concat([one,zero,height_shift, zero,one,width_shift, zero,zero,one],axis=0),[3,3])
 return K.dot(K.dot(rotation_matrix, shear_matrix), K.dot(zoom_matrix, shift_matrix))<categorify>
Ticket1 = []
for i in list(train.Ticket):
if not i.isdigit() :
Ticket1.append(i.replace(".","" ).replace("/","" ).strip().split(' ')[0])
 else:
Ticket1.append("X")
 train["Ticket"] = Ticket1
Ticket2 = []
for j in list(test.Ticket):
if not j.isdigit() :
Ticket2.append(j.replace(".","" ).replace("/","" ).strip().split(' ')[0])
 else:
Ticket2.append("X")
 test["Ticket"] = Ticket2
def transform(image, DIM=512):
XDIM = DIM%2
rot = ROT_ * tf.random.normal([1], dtype='float32')
 shr = SHR_ * tf.random.normal([1], dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ HZOOM_
w_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ WZOOM_
h_shift = HSHIFT_ * tf.random.normal([1], dtype='float32')
 w_shift = WSHIFT_ * tf.random.normal([1], dtype='float32')
 m = get_mat(rot,shr,h_zoom,w_zoom,h_shift,w_shift)
 x = tf.repeat(tf.range(DIM//2, -DIM//2,-1), DIM)
 y = tf.tile(tf.range(-DIM//2, DIM//2), [DIM])
 z = tf.ones([DIM*DIM], dtype='int32')
 idx = tf.stack([x,y,z])
 idx2 = K.dot(m, tf.cast(idx, dtype='float32'))
idx2 = K.cast(idx2, dtype='int32')
 idx2 = K.clip(idx2, -DIM//2+XDIM+1, DIM//2)
 idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])
 d = tf.gather_nd(image, tf.transpose(idx3))
return tf.reshape(d,[DIM, DIM,3] )<prepare_x_and_y>
train= pd.get_dummies(train, columns = ["Ticket"], prefix="T")
 test = pd.get_dummies(test, columns = ["Ticket"], prefix="T" )
def read_labeled_tfrecord(example):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'target' : tf.io.FixedLenFeature([], tf.int64)
 }
example = tf.io.parse_single_example(example, tfrec_format)
 return example['image'], example['target']
def read_unlabeled_tfrecord(example, return_image_name):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'image_name' : tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, tfrec_format)
 return example['image'], example['image_name'] if return_image_name else 0
def prepare_image(img, augment=True, dim=512):
img = tf.image.decode_jpeg(img, channels=3)
 img = tf.cast(img, tf.float32)/ 255.0
if augment:
img = transform(img,DIM=dim)
 img = tf.image.random_flip_left_right(img)
 img = tf.image.random_brightness(img, 0.1)
 img = tf.reshape(img, [dim,dim, 3])
 return img
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1))
for filename in filenames]
return np.sum(n )<create_dataframe>
train = train.drop(['T_SP','T_SOP','T_Fa','T_LINE','T_SWPP','T_SCOW','T_PPP','T_AS','T_CASOTON'],axis = 1)
 test = test.drop(['T_SCA3','T_STONOQ','T_AQ4','T_A','T_LP','T_AQ3'],axis = 1 )
def get_dataset(files, augment = False, shuffle = False, repeat = False,
labeled=True, return_image_names=True, batch_size=BATCH_SIZE, dim=512):
ds = tf.data.TFRecordDataset(files, num_parallel_reads=AUTO)
 ds = ds.cache()
if repeat:
ds = ds.repeat()
if shuffle:
ds = ds.shuffle(1024*8)
 opt = tf.data.Options()
opt.experimental_deterministic = False
ds = ds.with_options(opt)
 if labeled:
ds = ds.map(read_labeled_tfrecord, num_parallel_calls=AUTO)
 else:
ds = ds.map(lambda example: read_unlabeled_tfrecord(example, return_image_names),
num_parallel_calls=AUTO)
 ds = ds.map(lambda img, imgname_or_label:(prepare_image(img, augment=augment, dim=dim),
imgname_or_label),
num_parallel_calls=AUTO)
 ds = ds.batch(batch_size * REPLICAS)
 ds = ds.prefetch(AUTO)
 return ds<define_variables>
train.drop(['Survived'],axis=1,inplace=True )
TEST_FILENAMES = tf.io.gfile.glob(GCS_DS_PATH + '/test_tfrecords/*.tfrec')
<define_variables>
train.isnull().sum()
NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 print('Dataset: {} unlabeled test images'.format(NUM_TEST_IMAGES))<install_modules>
test.isnull().sum()
sys.path.append('/kaggle/input/efficientnet-keras-dataset/efficientnet_kaggle')
 ! pip install /kaggle/input/efficientnet-keras-dataset/efficientnet_kaggle<import_modules>
test.isnull().sum()
import efficientnet.keras as efn 
<choose_model_class>
scaler = StandardScaler()
train2 = scaler.fit_transform(train)
 test2 = scaler.fit_transform(test )
def create_model_efnB6() :
base_model = efn.EfficientNetB6(weights=None,
include_top=False,
input_shape=[HEIGHT, WIDTH, 3])
 model = tf.keras.Sequential([
base_model,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ])
 return model<choose_model_class>
KFold_Score = pd.DataFrame()
classifiers = ['Linear SVM', 'Radial SVM', 'LogisticRegression',
'RandomForestClassifier', 'AdaBoostClassifier',
'XGBoostClassifier', 'KNeighborsClassifier','GradientBoostingClassifier']
models = [svm.SVC(kernel='linear'),
svm.SVC(kernel='rbf'),
LogisticRegression(max_iter = 1000),
RandomForestClassifier(n_estimators=200, random_state=0),
AdaBoostClassifier(random_state = 0),
xgb.XGBClassifier(n_estimators=100),
KNeighborsClassifier() ,
GradientBoostingClassifier(random_state=0)
 ]
j = 0
for i in models:
model = i
cv = KFold(n_splits=5, random_state=0, shuffle=True)
 KFold_Score[classifiers[j]] =(cross_val_score(model, train2, np.ravel(pred), scoring = 'accuracy', cv=cv))
j = j+1
with strategy.scope() :
model_efnB6 = create_model_efnB6()<load_from_csv>
mean = pd.DataFrame(KFold_Score.mean() , index= classifiers)
 KFold_Score = pd.concat([KFold_Score,mean.T])
 KFold_Score.index=['Fold 1','Fold 2','Fold 3','Fold 4','Fold 5','Mean']
KFold_Score.T.sort_values(by=['Mean'], ascending = False )
TTA = 1
print('Predicting Test with TTA...')
 test_ds = get_dataset(TEST_FILENAMES,labeled=False,return_image_names=False,augment=False,
repeat=False,shuffle=False)
 test_ct = count_data_items(TEST_FILENAMES);
STEPS = TTA * test_ct/BATCH_SIZE/REPLICAS
if STEPS < 1:
STEPS = 1
test_df = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 probabilities = np.zeros(( test_df.shape[0],5))
for f in range(5):
model_efnB6.load_weights(".. /input/cassava-leaf-disease-classification-kfold/model_efnB6_train_fold_%i.h5"%f, by_name=True)
 prob = model_efnB6.predict(test_ds, verbose=2)
 probabilities += prob/5
predictions = np.argmax(probabilities, axis=-1)
 print(predictions)
 print(probabilities )<save_to_csv>
col_name1[0],col_name1[2] = col_name1[2],col_name1[0]
col_name2[0],col_name2[2] = col_name2[2],col_name2[0]
print('Generating submission.csv file...')
 ds = get_dataset(TEST_FILENAMES,labeled=False,return_image_names=True,augment=False,
repeat=False,shuffle=False)
 test_ids = np.array([img_name.numpy().decode("utf-8")
 for img, img_name in iter(ds.unbatch())])
 np.savetxt(
 'submission.csv',
np.rec.fromarrays([test_ids, predictions]),
fmt=['%s', '%d'],
delimiter=',',
header='image_id,label',
comments='', 
)
 !head submission.csv<install_modules>
train_new = train[col_name1]
test_new = test[col_name2]
!pip install --no-deps.. /input/pretrined-models/timm-0.3.3-py3-none-any.whl<import_modules>
train_new = train_new.drop(['Cabin'],axis = 1)
 test_new = test_new.drop(['Cabin'],axis = 1 )
import os
import pandas as pd
import timm
from PIL import Image, ImageDraw, ImageChops
import matplotlib.pyplot as plt
from torchvision.utils import make_grid
from tqdm import tqdm<load_from_csv>
sc = StandardScaler()
train3 = sc.fit_transform(train_new)
 test3 = sc.transform(test_new )
df = pd.read_csv(path + "/train.csv" )<drop_column>
clf = RandomForestClassifier(random_state=0)
 param_grid={
'n_estimators': [200,300],
'max_features': ['auto', 'sqrt'],
'max_depth': [6,7,8],
'criterion':['gini','entropy']
}
df["path"] = df["image_id"].map(lambda x: path + "/train_images/" + x)
 df = df.drop(columns=["image_id"])
 df = df.sample(frac=1 ).reset_index(drop=True )<split>
CV_clf = GridSearchCV(estimator=clf, param_grid=param_grid, cv=5)
 CV_clf.fit(train3, pred)
 CV_clf.best_params_
train_df, valid_df = model_selection.train_test_split(
 df, test_size=0.2, random_state=42, stratify=df.label.values
)<drop_column>
clf1 = RandomForestClassifier(random_state=0, n_estimators=200, criterion='gini', max_features='auto', max_depth=8)
 clf1.fit(train3, pred )
train_df = train_df.reset_index().drop(columns=["index"])
 train_df.head()<drop_column>
pred3 = clf1.predict(test3 )
valid_df = valid_df.reset_index().drop(columns=["index"])
 valid_df.head()<load_pretrained>
pred_test = pred3
output = pd.DataFrame({
'PassengerId': test_data.PassengerId,
'Survived': pred_test
})
 output.to_csv('submission1.csv', index=False )