Kaggle competition: Titanic - Machine Learning from Disaster
class enetv2(nn.Module):
def __init__(self, backbone, out_dim):
super(enetv2, self ).__init__()
self.enet = enet.EfficientNet.from_name(backbone)
 self.myfc = nn.Linear(self.enet._fc.in_features, out_dim)
 self.enet._fc = nn.Identity()
def extract(self, x):
return self.enet(x)
 def forward(self, x):
x = self.extract(x)
 x = self.myfc(x)
 return x
def load_models(model_files):
models = []
for model_f in model_files:
model_f = os.path.join(model_dir, model_f)
 backbone = 'efficientnet-b0'
model = enetv2(backbone, out_dim=5)
 model.load_state_dict(torch.load(model_f, map_location=lambda storage, loc: storage), strict=True)
 model.eval()
model.to(device)
 models.append(model)
 print(f'{model_f} loaded!')
 return models
model_files = [
'cls_effnet_b0_Rand36r36tiles256_big_bce_lr0.3_augx2_30epo_model_fold0.pth'
]
models = load_models(model_files )<categorify>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv", index_col="PassengerId")
 train_data.head()
def get_tiles(img, mode=0):
result = []
h, w, c = img.shape
pad_h =(tile_size - h % tile_size)% tile_size +(( tile_size * mode)// 2)
 pad_w =(tile_size - w % tile_size)% tile_size +(( tile_size * mode)// 2)
 img2 = np.pad(img,[[pad_h // 2, pad_h - pad_h // 2], [pad_w // 2,pad_w - pad_w//2], [0,0]], constant_values=255)
 img3 = img2.reshape(
 img2.shape[0] // tile_size,
tile_size,
img2.shape[1] // tile_size,
tile_size,
3 
)
 img3 = img3.transpose(0,2,1,3,4 ).reshape(-1, tile_size, tile_size,3)
 n_tiles_with_info =(img3.reshape(img3.shape[0],-1 ).sum(1)< tile_size ** 2 * 3 * 255 ).sum()
if len(img)< n_tiles:
img3 = np.pad(img3,[[0,N-len(img3)],[0,0],[0,0],[0,0]], constant_values=255)
 idxs = np.argsort(img3.reshape(img3.shape[0],-1 ).sum(-1)) [:n_tiles]
img3 = img3[idxs]
for i in range(len(img3)) :
result.append({'img':img3[i], 'idx':i})
 return result, n_tiles_with_info >= n_tiles
class PANDADataset(Dataset):
def __init__(self,
df,
image_size,
n_tiles=n_tiles,
tile_mode=0,
rand=False,
sub_imgs=False 
):
self.df = df.reset_index(drop=True)
 self.image_size = image_size
self.n_tiles = n_tiles
self.tile_mode = tile_mode
self.rand = rand
self.sub_imgs = sub_imgs
def __len__(self):
return self.df.shape[0]
def __getitem__(self, index):
row = self.df.iloc[index]
img_id = row.image_id
tiff_file = os.path.join(image_folder, f'{img_id}.tiff')
 image = skimage.io.MultiImage(tiff_file)[1]
tiles, OK = get_tiles(image, self.tile_mode)
 if self.rand:
idxes = np.random.choice(list(range(self.n_tiles)) , self.n_tiles, replace=False)
 else:
idxes = list(range(self.n_tiles))
idxes = np.asarray(idxes)+ self.n_tiles if self.sub_imgs else idxes
n_row_tiles = int(np.sqrt(self.n_tiles))
images = np.zeros(( image_size * n_row_tiles, image_size * n_row_tiles, 3))
for h in range(n_row_tiles):
for w in range(n_row_tiles):
i = h * n_row_tiles + w
if len(tiles)> idxes[i]:
this_img = tiles[idxes[i]]['img']
else:
this_img = np.ones(( self.image_size, self.image_size, 3)).astype(np.uint8)* 255
this_img = 255 - this_img
h1 = h * image_size
w1 = w * image_size
images[h1:h1+image_size, w1:w1+image_size] = this_img
images = images.astype(np.float32)
 images /= 255
images = images.transpose(2, 0, 1)
 return torch.tensor(images)
<load_pretrained>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv", index_col="PassengerId")
 test_data.head()
dataset = PANDADataset(df, image_size, n_tiles, 0)
 loader = DataLoader(dataset, batch_size=batch_size, num_workers=num_workers, shuffle=False)
 dataset2 = PANDADataset(df, image_size, n_tiles, 2)
 loader2 = DataLoader(dataset2, batch_size=batch_size, num_workers=num_workers, shuffle=False )<save_to_csv>
combined = pd.concat([train_data, test_data])
 combined.head()
LOGITS = []
LOGITS2 = []
with torch.no_grad() :
for data in tqdm(loader):
data = data.to(device)
 logits = models[0](data)
 LOGITS.append(logits)
 for data in tqdm(loader2):
data = data.to(device)
 logits = models[0](data)
 LOGITS2.append(logits)
 LOGITS =(torch.cat(LOGITS ).sigmoid().cpu() + torch.cat(LOGITS2 ).sigmoid().cpu())/ 2
PREDS = LOGITS.sum(1 ).round().numpy()
df['isup_grade'] = PREDS.astype(int)
 df[['image_id', 'isup_grade']].to_csv('submission.csv', index=False)
 print(df.head())
 print()
print(df.isup_grade.value_counts() )<define_variables>
combined.iloc[891:]
DEBUG = False<define_variables>
combined.Ticket = combined.Ticket.str.replace(".","" ).str.replace("/", "" )
sys.path = [
'.. /input/efficientnet-pytorch/EfficientNet-PyTorch/EfficientNet-PyTorch-master',
] + sys.path<define_variables>
combined.Ticket[combined.Ticket.str.contains("LINE")]
sys.path<import_modules>
combined.Ticket = combined.Ticket.str.replace("SOTON","STON" )
import skimage.io
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, Dataset
from efficientnet_pytorch import model as enet
import albumentations
from albumentations.pytorch import ToTensor
import matplotlib.pyplot as plt
from tqdm import tqdm_notebook as tqdm 
<load_from_csv>
def ticket_type(row):
t = row.Ticket.split(" ")[0].lower()
return t[:2] if not t.isdigit() else "U"
combined["ticket_type"] = combined.apply(ticket_type, axis=1)
 combined["ticket_type"].head()
data_dir = '.. /input/prostate-cancer-grade-assessment'
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df_test = pd.read_csv(os.path.join(data_dir, 'test.csv'))
df_sub = pd.read_csv(os.path.join(data_dir, 'sample_submission.csv'))
model_dir = '.. /input/effnet-model-assemble'
image_folder = os.path.join(data_dir, 'test_images')
 is_test = os.path.exists(image_folder)
 image_folder = image_folder if is_test else os.path.join(data_dir, 'train_images')
 df = df_test if is_test else df_train.loc[:100]
tile_size = 256
image_size = 256
n_tiles = 36
batch_size = 8
num_workers = 4
device = torch.device('cuda')
 print(image_folder )<define_search_model>
class enetv2(nn.Module):
def __init__(self, backbone, out_dim):
super(enetv2, self ).__init__()
self.enet = enet.EfficientNet.from_name(backbone)
 self.myfc = nn.Linear(self.enet._fc.in_features, out_dim)
 self.enet._fc = nn.Identity()
def extract(self, x):
return self.enet(x)
 def forward(self, x):
x = self.extract(x)
 x = self.myfc(x)
 return x
def load_models(model_files):
models = []
for model_f in model_files:
model_f = os.path.join(model_dir, model_f)
 backbone = 'efficientnet-b0'
model = enetv2(backbone, out_dim=5)
 model.load_state_dict(torch.load(model_f, map_location=lambda storage, loc: storage), strict=True)
 model.eval()
model.to(device)
 models.append(model)
 print(f'{model_f} loaded!')
 return models
model_files = [
'cls_effnet_b0_Rand36r36tiles256_big_bce_lr0.3_augx2_30epo_model_fold0.pth'
]
models = load_models(model_files )<categorify>
combined.isnull().sum()
model_dir_1 = '.. /input/effnet-model-assemble/'
backbone_1 = 'efficientnet-b1'
class enetv2_1(nn.Module):
def __init__(self, backbone_1, out_dim):
super(enetv2_1, self ).__init__()
self.enet = enet.EfficientNet.from_name(backbone_1)
 self.myfc = nn.Linear(self.enet._fc.in_features, out_dim)
 self.enet._fc = nn.Identity()
def extract(self, x):
return self.enet(x)
 def forward(self, x):
x = self.extract(x)
 x = self.myfc(x)
 return x
class WrappedModel(nn.Module):
def __init__(self):
super(WrappedModel, self ).__init__()
self.module = enetv2_1(backbone_1, out_dim=5)
 def forward(self, x):
return self.module(x)
 def load_models_1(model_files):
models = []
for model_f in model_files:
model_f = os.path.join(model_dir, model_f)
 model = WrappedModel()
model.load_state_dict(torch.load(model_f, map_location=lambda storage, loc: storage.cuda(0)) , strict=True)
 model.eval()
model.to(device)
 models.append(model)
 print(f'{model_f} loaded!')
 return models
model_files = [
'efficientnet_b1_36x256x256_final_best_fold0.pth',
'efficientnet_b1_36x256x256_final_best_fold1.pth'
]
models_1 = load_models_1(model_files)
<concatenate>
combined[combined.Embarked.isnull() ]
test_transform = albumentations.Compose([
albumentations.Transpose(p=0.5),
albumentations.VerticalFlip(p=0.5),
albumentations.HorizontalFlip(p=0.5),
])
 transforms_val = albumentations.Compose([] )<categorify>
combined.Embarked.mode()
def get_tiles(img, mode=0):
result = []
h, w, c = img.shape
pad_h =(tile_size - h % tile_size)% tile_size +(( tile_size * mode)// 2)
 pad_w =(tile_size - w % tile_size)% tile_size +(( tile_size * mode)// 2)
 img2 = np.pad(img,[[pad_h // 2, pad_h - pad_h // 2], [pad_w // 2,pad_w - pad_w//2], [0,0]], constant_values=255)
 img3 = img2.reshape(
 img2.shape[0] // tile_size,
tile_size,
img2.shape[1] // tile_size,
tile_size,
3 
)
 img3 = img3.transpose(0,2,1,3,4 ).reshape(-1, tile_size, tile_size,3)
 n_tiles_with_info =(img3.reshape(img3.shape[0],-1 ).sum(1)< tile_size ** 2 * 3 * 255 ).sum()
if len(img)< n_tiles:
img3 = np.pad(img3,[[0,N-len(img3)],[0,0],[0,0],[0,0]], constant_values=255)
 idxs = np.argsort(img3.reshape(img3.shape[0],-1 ).sum(-1)) [:n_tiles]
img3 = img3[idxs]
for i in range(len(img3)) :
result.append({'img':img3[i], 'idx':i})
 return result, n_tiles_with_info >= n_tiles
class PANDADataset(Dataset):
def __init__(self,
df,
image_size,
n_tiles=n_tiles,
tile_mode=0,
rand=False,
sub_imgs=False,
transform=None 
):
self.df = df.reset_index(drop=True)
 self.image_size = image_size
self.n_tiles = n_tiles
self.tile_mode = tile_mode
self.rand = rand
self.sub_imgs = sub_imgs
self.transform = transform
def __len__(self):
return self.df.shape[0]
def __getitem__(self, index):
row = self.df.iloc[index]
img_id = row.image_id
tiff_file = os.path.join(image_folder, f'{img_id}.tiff')
 image = skimage.io.MultiImage(tiff_file)[1]
tiles, OK = get_tiles(image, self.tile_mode)
 if self.rand:
idxes = np.random.choice(list(range(self.n_tiles)) , self.n_tiles, replace=False)
 else:
idxes = list(range(self.n_tiles))
idxes = np.asarray(idxes)+ self.n_tiles if self.sub_imgs else idxes
n_row_tiles = int(np.sqrt(self.n_tiles))
images = np.zeros(( image_size * n_row_tiles, image_size * n_row_tiles, 3))
for h in range(n_row_tiles):
for w in range(n_row_tiles):
i = h * n_row_tiles + w
if len(tiles)> idxes[i]:
this_img = tiles[idxes[i]]['img']
else:
this_img = np.ones(( self.image_size, self.image_size, 3)).astype(np.uint8)* 255
this_img = 255 - this_img
h1 = h * image_size
w1 = w * image_size
images[h1:h1+image_size, w1:w1+image_size] = this_img
if self.transform is not None:
images = self.transform(image=images)['image']
images = images.astype(np.float32)
 images /= 255
images = images.transpose(2, 0, 1)
 return torch.tensor(images)
<define_variables>
combined["Embarked"].fillna("S", inplace=True )
TTA_num = 16
LOGITS_bags_0= []
LOGITS_bags_1= []
LOGITS_bags_b0=[]<define_variables>
def process_age_type(row):
age = row.Age
if age <=5:
return "infant"
elif age <=18:
return "adolescent"
elif age <= 24:
return "adult"
elif age <= 44:
return "middle_aged"
elif age <= 64:
return "old"
else:
return "senior"
combined["Age_type"] = combined.apply(process_age_type, axis=1 )
for i in range(2*TTA_num):
LOGITS_bags_0.append([])
 LOGITS_bags_1.append([])
 LOGITS_bags_b0.append([] )<create_dataframe>
combined.isnull().sum()
LOGITS_total=[]
LOGITS_total_b0=[]
with torch.no_grad() :
for i in range(2*TTA_num):
if i%2 == 0:
dataset=(PANDADataset(df, image_size, n_tiles, 0, transform=test_transform))
loader=(DataLoader(dataset, batch_size=batch_size, num_workers=num_workers, shuffle=False))
else:
dataset=(PANDADataset(df, image_size, n_tiles, 2, transform=test_transform))
loader=(DataLoader(dataset, batch_size=batch_size, num_workers=num_workers, shuffle=False))
for data in tqdm(loader):
data = data.to(device)
 logits_0 = models_1[0](data)
 logits_1 = models_1[1](data)
 logits_b0 = models[0](data)
 LOGITS_bags_0[i].append(logits_0)
 LOGITS_bags_1[i].append(logits_1)
 LOGITS_bags_b0[i].append(logits_b0)
 LOGITS_total.append(torch.cat(LOGITS_bags_0[i] ).sigmoid().cpu())
 LOGITS_total.append(torch.cat(LOGITS_bags_1[i] ).sigmoid().cpu())
 LOGITS_total_b0.append(torch.cat(LOGITS_bags_b0[i] ).sigmoid().cpu())
<feature_engineering>
combined.loc[889]
LOGITS=sum(LOGITS_total)/(2*TTA_num*2)
 LOGITS_b0=sum(LOGITS_total_b0)/(2*TTA_num )<save_to_csv>
test_data[test_data.Fare.isnull() ]
LOGITS_final =(LOGITS + LOGITS_b0)/2
PREDS = LOGITS_final.sum(1 ).round().numpy()
df['isup_grade'] = PREDS.astype(int)
 df[['image_id', 'isup_grade']].to_csv('submission.csv', index=False)
 print(df.head())
 print()
print(df.isup_grade.value_counts() )<define_variables>
combined.Fare.fillna(combined[combined.Pclass == 3]["Fare"].mode() [0], inplace=True )
sys.path = [
'.. /input/efficientnet-pytorch/EfficientNet-PyTorch/EfficientNet-PyTorch-master',
] + sys.path 
<load_from_csv>
def process_deck(row):
cabin = row.Cabin
if pd.isnull(cabin):
return "U"
else:
return cabin[0]
combined["Deck"] = combined.apply(process_deck, axis=1)
 combined["Deck"].head()
data_dir = '.. /input/prostate-cancer-grade-assessment'
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df_test = pd.read_csv(os.path.join(data_dir, 'test.csv'))
df_sub = pd.read_csv(os.path.join(data_dir, 'sample_submission.csv'))
model_dir = '.. /input/panda-base-model'
image_folder = os.path.join(data_dir, 'test_images')
 is_test = os.path.exists(image_folder)
 image_folder = image_folder if is_test else os.path.join(data_dir, 'train_images')
 df = df_test if is_test else df_train.loc[:3]
print(df.shape)
 tile_size = 256
image_size = 256
n_tiles = 36
batch_size = 4
num_workers = 4
device = torch.device('cuda')
 print(image_folder )<define_search_model>
combined.groupby(["Pclass", "Deck"] ).mean() ["Fare"]
class enetv2(nn.Module):
def __init__(self, backbone, out_dim):
super(enetv2, self ).__init__()
self.enet = enet.EfficientNet.from_name(backbone)
 self.myfc = nn.Linear(self.enet._fc.in_features, out_dim)
 self.enet._fc = nn.Identity()
def extract(self, x):
return self.enet(x)
 def forward(self, x):
x = self.extract(x)
 x = self.myfc(x)
 return x
def load_models(model_files):
models = []
for model_dict in model_files:
model_f = model_dict['name']
model_f = os.path.join(model_dir, model_f)
 backbone = 'efficientnet-b0'
model = enetv2(backbone, out_dim=model_dict['out_dim'])
 if model_dict['parallel']:
model = nn.DataParallel(model)
 model.load_state_dict(torch.load(model_f, map_location=lambda storage, loc: storage), strict=True)
 model.eval()
model.to(device)
 models.append(model)
 print(f'{model_f} loaded!')
 return models
model_files = [
{
'name': 'model_2_bce_0.881.pth',
'parallel': True,
'out_dim': 6,
'n_tiles': 36
},
{
'name': 'model_0_bce_4x4_0.884.pth',
'parallel': False,
'out_dim': 5,
'n_tiles': 16
},
{
'name': 'model_2_bce_5x5_total_0.883.pth',
'parallel': False,
'out_dim': 5,
'n_tiles': 25
},
{
'name': 'model_3_bce_6x6_total_0.883.pth',
'parallel': False,
'out_dim': 5,
'n_tiles': 36
}
]
models = load_models(model_files )<normalization>
def process_unknown_decks(row):
deck = row.Deck
if deck == "U":
fare = row.Fare
if fare == 0:
return "U"
if row.Pclass == 1:
if fare <= 35.075:
return "A"
elif fare <= 53.1:
return "D"
elif fare <= 55.5:
return "E"
elif fare <= 82.27:
return "B"
else:
return "C"
elif row.Pclass == 2:
if fare <= 11.5:
return "E"
elif fare <= 13.6:
return "D"
else:
return "F"
elif row.Pclass == 3:
if fare <= 7.65:
return "F"
elif fare <= 12.5:
return "E"
else:
return "G"
else:
return deck
combined["Deck"] = combined.apply(process_unknown_decks, axis=1)
 combined["Deck"].head()
def get_tiles(img, n_tiles, mode=0):
result = []
h, w, c = img.shape
pad_h =(tile_size - h % tile_size)% tile_size
pad_w =(tile_size - w % tile_size)% tile_size
mode_pad_h =(tile_size * mode)// 4
mode_pad_w =(tile_size * mode)// 4
img2 = np.pad(img,[[pad_h // 2 + mode_pad_h, pad_h - pad_h // 2 + tile_size - mode_pad_h],
[pad_w // 2 + mode_pad_w, pad_w - pad_w // 2 + tile_size - mode_pad_w], [0,0]], constant_values=255)
 img3 = img2.reshape(
 img2.shape[0] // tile_size,
tile_size,
img2.shape[1] // tile_size,
tile_size,
3 
)
 img3 = img3.transpose(0,2,1,3,4 ).reshape(-1, tile_size, tile_size,3)
 n_tiles_with_info =(img3.reshape(img3.shape[0],-1 ).sum(1)< tile_size ** 2 * 3 * 255 ).sum()
if len(img3)< n_tiles:
img3 = np.pad(img3, [[0, n_tiles - len(img3)], [0, 0] , [0, 0] , [0, 0]], constant_values=255)
 idxs = np.argsort(img3.reshape(img3.shape[0],-1 ).sum(-1)) [:n_tiles]
img3 = img3[idxs]
for i in range(len(img3)) :
result.append({'img':img3[i], 'idx':i})
 return result, n_tiles_with_info >= n_tiles
class PANDADataset(Dataset):
def __init__(self,
df,
image_size,
n_tiles=n_tiles,
tile_mode=0,
rotate=False,
rand=False,
sub_imgs=False 
):
self.df = df.reset_index(drop=True)
 self.image_size = image_size
self.n_tiles = n_tiles
self.tile_mode = tile_mode
self.rotate = rotate
self.rand = rand
self.sub_imgs = sub_imgs
def __len__(self):
return self.df.shape[0]
def __getitem__(self, index):
row = self.df.iloc[index]
img_id = row.image_id
tiff_file = os.path.join(image_folder, f'{img_id}.tiff')
 image = skimage.io.MultiImage(tiff_file)[1]
tiles, OK = get_tiles(image, n_tiles=self.n_tiles, mode=self.tile_mode)
 if self.rand:
idxes = np.random.choice(list(range(self.n_tiles)) , self.n_tiles, replace=False)
 else:
idxes = list(range(self.n_tiles))
idxes = np.asarray(idxes)+ self.n_tiles if self.sub_imgs else idxes
n_row_tiles = int(np.sqrt(self.n_tiles))
images = np.zeros(( image_size * n_row_tiles, image_size * n_row_tiles, 3))
for h in range(n_row_tiles):
for w in range(n_row_tiles):
i = h * n_row_tiles + w
if len(tiles)> idxes[i]:
this_img = tiles[idxes[i]]['img']
else:
this_img = np.ones(( self.image_size, self.image_size, 3)).astype(np.uint8)* 255
this_img = 255 - this_img
h1 = h * image_size
w1 = w * image_size
if self.rotate:
this_img = this_img.transpose(1, 0, 2)[::-1]
images[h1:h1+image_size, w1:w1+image_size] = this_img
images = images.astype(np.float32)
 images /= 255
images = images.transpose(2, 0, 1)
 return torch.tensor(images )<create_dataframe>
def get_title(row):
return row.Name.split(",")[1].strip().split(".")[0]
combined["Title"] = combined.apply(get_title, axis=1)
 combined.Title.unique()
model_loaders = []
for model_dict, model in zip(model_files, models):
loaders = []
for mode in [0, 1, 2, 3]:
for rotate in [False, True]:
dataset = PANDADataset(df, image_size, n_tiles=model_dict['n_tiles'], tile_mode=mode, rotate=rotate)
 loader = DataLoader(dataset, batch_size=batch_size, num_workers=num_workers, shuffle=False)
 loaders.append(loader)
 model_loaders.append(( model, loaders))
print(f'{len(model_loaders)* len(model_loaders[0][1])} predictions in total' )<categorify>
title_mappings = {
"Capt": "Officer",
"Col": "Officer",
"Major": "Officer",
"Jonkheer": "Royalty",
"Don": "Royalty",
"Sir" : "Royalty",
"Dr": "Officer",
"Rev": "Officer",
"the Countess":"Royalty",
"Mme": "Mrs",
"Mlle": "Miss",
"Ms": "Mrs",
"Mr" : "Mr",
"Mrs" : "Mrs",
"Miss" : "Miss",
"Master" : "Royalty",
"Lady" : "Royalty",
"Dona": "Royalty"
}
combined["Title"] = combined["Title"].map(title_mappings)
 combined["Title"].unique()
LOGITSS = []
with torch.no_grad() :
for model, loaders in model_loaders:
for loader in loaders:
LOGITS = []
for data in tqdm(loader):
data = data.to(device)
 logits = model(data)
 LOGITS.append(logits)
 LOGITS = torch.cat(LOGITS ).sigmoid().cpu().numpy().sum(1)
 LOGITSS.append(LOGITS)
 LOGITS = np.array(LOGITSS ).mean(0)
 PREDS = LOGITS.round()
df['isup_grade'] = PREDS.astype(int)
 df[['image_id', 'isup_grade']].to_csv('submission.csv', index=False)
 print(df.head())
 print()
print(df.isup_grade.value_counts() )<define_variables>
grouped_train = combined.groupby(['Sex','Pclass','Title'])
 grouped_median_train = grouped_train.median()
grouped_median_train = grouped_median_train.reset_index() [['Sex', 'Pclass', 'Title', 'Age']]
grouped_median_train.head()
DEBUG = False<define_variables>
def fill_age(row):
condition =(
(grouped_median_train['Sex'] == row['Sex'])& 
(grouped_median_train['Title'] == row['Title'])& 
(grouped_median_train['Pclass'] == row['Pclass'])
)
 return grouped_median_train[condition]['Age'].values[0]
combined["Age"] = combined.apply(lambda row: fill_age(row)if np.isnan(row['Age'])else row['Age'], axis=1 )
sys.path = [
'.. /input/efficientnet-pytorch/EfficientNet-PyTorch/EfficientNet-PyTorch-master',
] + sys.path<import_modules>
combined.Sex = combined.Sex.map({"male":0, "female":1} )
import skimage.io
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, Dataset
from efficientnet_pytorch import model as enet
import matplotlib.pyplot as plt
from tqdm import tqdm_notebook as tqdm 
<load_from_csv>
combined["Family_size"] = combined["Parch"] + combined["SibSp"] + 1
combined["Family_size"].head()
data_dir = '.. /input/prostate-cancer-grade-assessment'
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df_test = pd.read_csv(os.path.join(data_dir, 'test.csv'))
df_sub = pd.read_csv(os.path.join(data_dir, 'sample_submission.csv'))
model_dir = '.. /input/panda-public-models'
image_folder = os.path.join(data_dir, 'test_images')
 is_test = os.path.exists(image_folder)
 image_folder = image_folder if is_test else os.path.join(data_dir, 'train_images')
 df = df_test if is_test else df_train.loc[:10]
tile_size = 256
image_size = 256
n_tiles = 36
batch_size = 8
num_workers = 4
device = torch.device('cuda')
 print(image_folder)
<define_search_model>
oh_features = ["Embarked", "Deck", "Title"]
combined = pd.get_dummies(combined, columns=oh_features, prefix=oh_features)
 combined.head()
class enetv2(nn.Module):
def __init__(self, backbone, out_dim):
super(enetv2, self ).__init__()
self.enet = enet.EfficientNet.from_name(backbone)
 self.myfc = nn.Linear(self.enet._fc.in_features, out_dim)
 self.enet._fc = nn.Identity()
def extract(self, x):
return self.enet(x)
 def forward(self, x):
x = self.extract(x)
 x = self.myfc(x)
 return x
def load_models(model_files):
models = []
for model_f in model_files:
model_f = os.path.join(model_dir, model_f)
 backbone = 'efficientnet-b0'
model = enetv2(backbone, out_dim=5)
 model.load_state_dict(torch.load(model_f, map_location=lambda storage, loc: storage), strict=True)
 model.eval()
model.to(device)
 models.append(model)
 print(f'{model_f} loaded!')
 return models
model_files = [
'cls_effnet_b0_Rand36r36tiles256_big_bce_lr0.3_augx2_30epo_model_fold0.pth'
]
models = load_models(model_files)
 model_dir2 = '.. /input/pandaenetb042x256x256x3'
def load_models2(model_files):
models = []
for model_f in model_files:
model_f = os.path.join(model_dir2, model_f)
 backbone = 'efficientnet-b1'
model = enetv2(backbone, out_dim=5)
 model.load_state_dict(torch.load(model_f, map_location=lambda storage, loc: storage), strict=True)
 model.eval()
model.to(device)
 models.append(model)
 print(f'{model_f} loaded!')
 return models
model_files2 = [
'enet_b1_8ep_fold0.pth'
]
models2 = load_models2(model_files2 )<categorify>
label_features = ["ticket_type", "Age_type"]
le = LabelEncoder()
for feature in label_features:
combined[feature] = le.fit_transform(combined[feature] )
def get_tiles(img, mode=0):
result = []
h, w, c = img.shape
pad_h =(tile_size - h % tile_size)% tile_size +(( tile_size * mode)// 2)
 pad_w =(tile_size - w % tile_size)% tile_size +(( tile_size * mode)// 2)
 img2 = np.pad(img,[[pad_h // 2, pad_h - pad_h // 2], [pad_w // 2,pad_w - pad_w//2], [0,0]], constant_values=255)
 img3 = img2.reshape(
 img2.shape[0] // tile_size,
tile_size,
img2.shape[1] // tile_size,
tile_size,
3 
)
 img3 = img3.transpose(0,2,1,3,4 ).reshape(-1, tile_size, tile_size,3)
 n_tiles_with_info =(img3.reshape(img3.shape[0],-1 ).sum(1)< tile_size ** 2 * 3 * 255 ).sum()
if len(img)< n_tiles:
img3 = np.pad(img3,[[0,n_tiles-len(img3)],[0,0],[0,0],[0,0]], constant_values=255)
 idxs = np.argsort(img3.reshape(img3.shape[0],-1 ).sum(-1)) [:n_tiles]
img3 = img3[idxs]
for i in range(len(img3)) :
result.append({'img':img3[i], 'idx':i})
 return result, n_tiles_with_info >= n_tiles
class PANDADataset(Dataset):
def __init__(self,
df,
image_size,
n_tiles=n_tiles,
tile_mode=0,
rand=False,
sub_imgs=False,
transform=None 
):
self.df = df.reset_index(drop=True)
 self.image_size = image_size
self.n_tiles = n_tiles
self.tile_mode = tile_mode
self.rand = rand
self.sub_imgs = sub_imgs
self.transform = transform
def __len__(self):
return self.df.shape[0]
def __getitem__(self, index):
row = self.df.iloc[index]
img_id = row.image_id
tiff_file = os.path.join(image_folder, f'{img_id}.tiff')
 image = skimage.io.MultiImage(tiff_file)[1]
tiles, OK = get_tiles(image, self.tile_mode)
 if self.rand:
idxes = np.random.choice(list(range(self.n_tiles)) , self.n_tiles, replace=False)
 else:
idxes = list(range(self.n_tiles))
idxes = np.asarray(idxes)+ self.n_tiles if self.sub_imgs else idxes
n_row_tiles = int(np.sqrt(self.n_tiles))
images = np.zeros(( image_size * n_row_tiles, image_size * n_row_tiles, 3))
for h in range(n_row_tiles):
for w in range(n_row_tiles):
i = h * n_row_tiles + w
if len(tiles)> idxes[i]:
this_img = tiles[idxes[i]]['img']
else:
this_img = np.ones(( self.image_size, self.image_size, 3)).astype(np.uint8)* 255
this_img = 255 - this_img
h1 = h * image_size
w1 = w * image_size
images[h1:h1+image_size, w1:w1+image_size] = this_img
if self.transform is not None:
images = self.transform(image=images)['image']
images = images.astype(np.float32)
 images /= 255
images = images.transpose(2, 0, 1)
 return torch.tensor(images)
<concatenate>
from numpy import mean
from numpy import std
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
from xgboost import XGBClassifier
transforms_train = albumentations.Compose([
albumentations.Transpose(p=0.5),
albumentations.VerticalFlip(p=0.5),
albumentations.HorizontalFlip(p=0.5),
])
 transforms_val = albumentations.Compose([])
 transforms_val1 = albumentations.Compose([
albumentations.Transpose(p=1)
 ])
 transforms_val2 = albumentations.Compose([
albumentations.VerticalFlip(p=1)
 ])
 transforms_val3= albumentations.Compose([
albumentations.HorizontalFlip(p=1),
])
 transforms_val4= albumentations.Compose([
albumentations.Transpose(p=1),
albumentations.VerticalFlip(p=1),
albumentations.HorizontalFlip(p=1),
] )<create_dataframe>
combined.drop(["Name", "Ticket","Cabin"], axis=1, inplace=True )
dataset = PANDADataset(df, image_size, n_tiles, 0, False, False, transforms_val)
 loader = DataLoader(dataset, batch_size=batch_size, num_workers=num_workers, shuffle=False)
 dataset2 = PANDADataset(df, image_size, n_tiles, 2, False, False, transforms_val)
 loader2 = DataLoader(dataset2, batch_size=batch_size, num_workers=num_workers, shuffle=False)
 LOGITS = []
LOGITS2 = []
LOGITS3 = []
LOGITS4 = []
with torch.no_grad() :
for data in tqdm(loader):
data = data.to(device)
 logits = models[0](data)
 LOGITS.append(logits)
 logits = models2[0](data)
 LOGITS3.append(logits)
 for data in tqdm(loader2):
data = data.to(device)
 logits = models[0](data)
 LOGITS2.append(logits)
 logits = models2[0](data)
 LOGITS4.append(logits)
 LOGITS =(torch.cat(LOGITS ).sigmoid().cpu() +torch.cat(LOGITS2 ).sigmoid().cpu() +torch.cat(LOGITS3 ).sigmoid().cpu() +torch.cat(LOGITS4 ).sigmoid().cpu())/ 4
PREDS = LOGITS.sum(1 ).numpy() 
<create_dataframe>
y = combined.iloc[:891].Survived
dataset = PANDADataset(df, image_size, n_tiles, 0, False, False, transforms_val1)
 loader = DataLoader(dataset, batch_size=batch_size, num_workers=num_workers, shuffle=False)
 dataset2 = PANDADataset(df, image_size, n_tiles, 2, False, False, transforms_val1)
 loader2 = DataLoader(dataset2, batch_size=batch_size, num_workers=num_workers, shuffle=False)
 LOGITS = []
LOGITS2 = []
LOGITS3 = []
LOGITS4 = []
with torch.no_grad() :
for data in tqdm(loader):
data = data.to(device)
 logits = models[0](data)
 LOGITS.append(logits)
 logits = models2[0](data)
 LOGITS3.append(logits)
 for data in tqdm(loader2):
data = data.to(device)
 logits = models[0](data)
 LOGITS2.append(logits)
 logits = models2[0](data)
 LOGITS4.append(logits)
 LOGITS =(torch.cat(LOGITS ).sigmoid().cpu() +torch.cat(LOGITS2 ).sigmoid().cpu() +torch.cat(LOGITS3 ).sigmoid().cpu() +torch.cat(LOGITS4 ).sigmoid().cpu())/ 4
PREDS1 = LOGITS.sum(1 ).numpy() 
<load_pretrained>
combined.drop(["Survived"], axis=1,inplace=True)
 X = combined.iloc[:891]
test_data = combined.iloc[891:]
dataset = PANDADataset(df, image_size, n_tiles, 0, False, False, transforms_val2)
 loader = DataLoader(dataset, batch_size=batch_size, num_workers=num_workers, shuffle=False)
 dataset2 = PANDADataset(df, image_size, n_tiles, 2, False, False, transforms_val2)
 loader2 = DataLoader(dataset2, batch_size=batch_size, num_workers=num_workers, shuffle=False)
 LOGITS = []
LOGITS2 = []
LOGITS3 = []
LOGITS4 = []
with torch.no_grad() :
for data in tqdm(loader):
data = data.to(device)
 logits = models[0](data)
 LOGITS.append(logits)
 logits = models2[0](data)
 LOGITS3.append(logits)
 for data in tqdm(loader2):
data = data.to(device)
 logits = models[0](data)
 LOGITS2.append(logits)
 logits = models2[0](data)
 LOGITS4.append(logits)
 LOGITS =(torch.cat(LOGITS ).sigmoid().cpu() +torch.cat(LOGITS2 ).sigmoid().cpu() +torch.cat(LOGITS3 ).sigmoid().cpu() +torch.cat(LOGITS4 ).sigmoid().cpu())/ 4
PREDS2 = LOGITS.sum(1 ).numpy() 
<load_pretrained>
y = y.astype("uint8" )
dataset = PANDADataset(df, image_size, n_tiles, 0, False, False, transforms_val3)
 loader = DataLoader(dataset, batch_size=batch_size, num_workers=num_workers, shuffle=False)
 dataset2 = PANDADataset(df, image_size, n_tiles, 2, False, False, transforms_val3)
 loader2 = DataLoader(dataset2, batch_size=batch_size, num_workers=num_workers, shuffle=False)
 LOGITS = []
LOGITS2 = []
LOGITS3 = []
LOGITS4 = []
with torch.no_grad() :
for data in tqdm(loader):
data = data.to(device)
 logits = models[0](data)
 LOGITS.append(logits)
 logits = models2[0](data)
 LOGITS3.append(logits)
 for data in tqdm(loader2):
data = data.to(device)
 logits = models[0](data)
 LOGITS2.append(logits)
 logits = models2[0](data)
 LOGITS4.append(logits)
 LOGITS =(torch.cat(LOGITS ).sigmoid().cpu() +torch.cat(LOGITS2 ).sigmoid().cpu() +torch.cat(LOGITS3 ).sigmoid().cpu() +torch.cat(LOGITS4 ).sigmoid().cpu())/ 4
PREDS3 = LOGITS.sum(1 ).numpy()<load_pretrained>
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=1)
 model = XGBClassifier(learning_rate=0.01,
n_estimators=1150,
max_depth=5,
objective= 'binary:logistic', 
)
 scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv)
 mean(scores)*100
dataset = PANDADataset(df, image_size, n_tiles, 0, False, False, transforms_val4)
 loader = DataLoader(dataset, batch_size=batch_size, num_workers=num_workers, shuffle=False)
 dataset2 = PANDADataset(df, image_size, n_tiles, 2, False, False, transforms_val4)
 loader2 = DataLoader(dataset2, batch_size=batch_size, num_workers=num_workers, shuffle=False)
 LOGITS = []
LOGITS2 = []
LOGITS3 = []
LOGITS4 = []
with torch.no_grad() :
for data in tqdm(loader):
data = data.to(device)
 logits = models[0](data)
 LOGITS.append(logits)
 logits = models2[0](data)
 LOGITS3.append(logits)
 for data in tqdm(loader2):
data = data.to(device)
 logits = models[0](data)
 LOGITS2.append(logits)
 logits = models2[0](data)
 LOGITS4.append(logits)
 LOGITS =(torch.cat(LOGITS ).sigmoid().cpu() +torch.cat(LOGITS2 ).sigmoid().cpu() +torch.cat(LOGITS3 ).sigmoid().cpu() +torch.cat(LOGITS4 ).sigmoid().cpu())/ 4
PREDS4 = LOGITS.sum(1 ).numpy() 
<set_options>
model.fit(X,y)
 predictions = model.predict(test_data )
!pip install.. /input/kaggle-efficientnet-repo/efficientnet-1.0.0-py3-none-any.whl
gc.enable()
sz = 256
N = 48
def tile(img):
result = []
shape = img.shape
pad0,pad1 =(sz - shape[0]%sz)%sz,(sz - shape[1]%sz)%sz
img = np.pad(img,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],constant_values=255)
 img = img.reshape(img.shape[0]//sz,sz,img.shape[1]//sz,sz,3)
 img = img.transpose(0,2,1,3,4 ).reshape(-1,sz,sz,3)
 if len(img)< N:
img = np.pad(img,[[0,N-len(img)],[0,0],[0,0],[0,0]],constant_values=255)
 idxs = np.argsort(img.reshape(img.shape[0],-1 ).sum(-1)) [:N]
img = img[idxs]
return img
def tile2(img):
result = []
shape = img.shape
pad0,pad1 =(sz - shape[0]%sz)%sz +(( sz * 2)// 2),(sz - shape[1]%sz)%sz +(( sz * 2)// 2)
 img = np.pad(img,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],constant_values=255)
 img = img.reshape(img.shape[0]//sz,sz,img.shape[1]//sz,sz,3)
 img = img.transpose(0,2,1,3,4 ).reshape(-1,sz,sz,3)
 if len(img)< N:
img = np.pad(img,[[0,N-len(img)],[0,0],[0,0],[0,0]],constant_values=255)
 idxs = np.argsort(img.reshape(img.shape[0],-1 ).sum(-1)) [:N]
img = img[idxs]
return img
class ConvNet(tf.keras.Model):
def __init__(self, engine, input_shape, weights):
super(ConvNet, self ).__init__()
self.engine = engine(
 include_top=False, input_shape=input_shape, weights=weights)
 self.avg_pool2d = tf.keras.layers.GlobalAveragePooling2D()
self.dropout = tf.keras.layers.Dropout(0.5)
 self.dense_1 = tf.keras.layers.Dense(1024)
 self.dense_2 = tf.keras.layers.Dense(5,activation='sigmoid')
 @tf.function
def call(self, inputs, **kwargs):
x = tf.reshape(inputs,(-1, IMG_SIZE, IMG_SIZE, 3))
x = self.engine(x)
 shape = x.shape
x = tf.reshape(x,(-1, N_TILES, shape[1], shape[2], shape[3]))
x = tf.transpose(x, perm=[0, 2, 1, 3, 4])
 x = tf.reshape(x,(-1, shape[1], N_TILES*shape[2], shape[3]))
x = self.avg_pool2d(x)
 x = self.dropout(x, training=False)
 x = self.dense_1(x)
 x = tf.nn.relu(x)
 return self.dense_2(x)
 is_ef = True
backbone_name = 'efficientnet-b0'
N_TILES = 48
IMG_SIZE = 256
if backbone_name.startswith('efficientnet'):
model_fn = getattr(efn, f'EfficientNetB{backbone_name[-1]}')
 model = ConvNet(engine=model_fn, input_shape=(IMG_SIZE, IMG_SIZE, 3), weights=None)
 TRAIN = '.. /input/prostate-cancer-grade-assessment/train_images/'
MASKS = '.. /input/prostate-cancer-grade-assessment/train_label_masks/'
BASE_PATH = '.. /input/prostate-cancer-grade-assessment/'
train = pd.read_csv(BASE_PATH + "train.csv")
 train.head()
sub = pd.read_csv(".. /input/prostate-cancer-grade-assessment/sample_submission.csv")
 sub.head()
test = pd.read_csv(".. /input/prostate-cancer-grade-assessment/test.csv")
 test.head()
TEST = '.. /input/prostate-cancer-grade-assessment/test_images/'
PRED_PATH = TEST
df = sub
t_df = test
transforms_val0 = albumentations.Compose([])
 transforms_val1 = albumentations.Compose([
albumentations.VerticalFlip(p=1)
 ])
 transforms_val2= albumentations.Compose([
albumentations.HorizontalFlip(p=0.5),
albumentations.VerticalFlip(p=0.5),
])
 transforms_val3= albumentations.Compose([
albumentations.HorizontalFlip(p=0.5),
albumentations.VerticalFlip(p=0.5),
])
 n_TTA = 2
dummy_data = tf.zeros(( n_TTA * N_TILES, IMG_SIZE, IMG_SIZE, 3), dtype=tf.float32)
 _ = model(dummy_data)
<choose_model_class>
import lightgbm as lgb
from lightgbm import LGBMClassifier
model.compile(optimizer = tf.keras.optimizers.Adam(lr= 1e-05), loss= tf.nn.sigmoid_cross_entropy_with_logits)
 model.load_weights('.. /input/pandaenetb042x256x256x3/efficientnet-b0-48-full-epochs60.h5')
 if os.path.exists(PRED_PATH):
predictions10 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = 0.5*np.sum(pred)
 predictions10.append(isup)
 del patches, img
gc.collect()
else:
PRED_PATH = TRAIN
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df = df_train.loc[:10]
df = df[['image_id','isup_grade']].copy()
predictions10 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = 0.5*np.sum(pred)
 predictions10.append(isup)
 del patches, img
gc.collect()
if os.path.exists(PRED_PATH):
predictions12 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile2(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = 0.5*np.sum(pred)
 predictions12.append(isup)
 del patches, img
gc.collect()
else:
PRED_PATH = TRAIN
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df = df_train.loc[:10]
df = df[['image_id','isup_grade']].copy()
predictions12 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile2(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = 0.5*np.sum(pred)
 predictions12.append(isup)
 del patches, img
gc.collect()<choose_model_class>
params= {
"learning_rate":0.03,
"num_leaves" : 63,
"boosting_type":'gbdt',
"objective":'binary',
"metric": 'binary_logloss,auc',
"feature_fraction": 0.85,
"bagging_freq" : 10,
"bagging_fraction" : 0.85,
"n_estimators":750,
"max_bin":300,
"subsample_for_bin":50000,
"min_data_in_leaf": 55,
"min_sum_hessian_in_leaf" :5.0,
}
params1= {
"learning_rate":0.01,
"num_leaves" : 120,
"boosting_type":'gbdt',
"objective":'binary',
"metric": 'binary_logloss,auc',
"max_depth": 8,
"n_estimators":500,
"max_bin":75,
"subsample_for_bin":10000,
"min_data_in_leaf": 60,
"min_sum_hessian_in_leaf" :7.5,
"min_split_gain": 1,
"min_child_weight":2,
"min_child_samples": 9,
"subsample": 0.995,
"alpha":0.001
}
dftrainLGB = lgb.Dataset(data = X, label = y)
 history = lgb.cv(train_set=dftrainLGB, params=params)
 for metric in history:
print(metric,history[metric][-1]*100 )
class ConvNet(tf.keras.Model):
def __init__(self, engine, input_shape, weights):
super(ConvNet, self ).__init__()
self.engine = engine(
 include_top=False, input_shape=input_shape, weights=weights)
 self.avg_pool2d = tf.keras.layers.GlobalAveragePooling2D()
self.dropout = tf.keras.layers.Dropout(0.5)
 self.dense_1 = tf.keras.layers.Dense(1024)
 self.dense_2 = tf.keras.layers.Dense(5,activation='sigmoid')
 @tf.function
def call(self, inputs, **kwargs):
x = tf.reshape(inputs,(-1, IMG_SIZE, IMG_SIZE, 3))
x = self.engine(x)
 shape = x.shape
x = tf.reshape(x,(-1, N_TILES, shape[1], shape[2], shape[3]))
x = tf.transpose(x, perm=[0, 2, 1, 3, 4])
 x = tf.reshape(x,(-1, shape[1], N_TILES*shape[2], shape[3]))
x = self.avg_pool2d(x)
 x = self.dropout(x, training=False)
 x = self.dense_1(x)
 x = tf.nn.relu(x)
 return self.dense_2(x)
 is_ef = True
backbone_name = 'efficientnet-b1'
N_TILES = 48
IMG_SIZE = 256
if backbone_name.startswith('efficientnet'):
model_fn = getattr(efn, f'EfficientNetB{backbone_name[-1]}')
 model = ConvNet(engine=model_fn, input_shape=(IMG_SIZE, IMG_SIZE, 3), weights=None)
 n_TTA = 2
dummy_data = tf.zeros(( n_TTA * N_TILES, IMG_SIZE, IMG_SIZE, 3), dtype=tf.float32)
 _ = model(dummy_data)
 TRAIN = '.. /input/prostate-cancer-grade-assessment/train_images/'
MASKS = '.. /input/prostate-cancer-grade-assessment/train_label_masks/'
BASE_PATH = '.. /input/prostate-cancer-grade-assessment/'
train = pd.read_csv(BASE_PATH + "train.csv")
 train.head()
sub = pd.read_csv(".. /input/prostate-cancer-grade-assessment/sample_submission.csv")
 sub.head()
test = pd.read_csv(".. /input/prostate-cancer-grade-assessment/test.csv")
 test.head()
TEST = '.. /input/prostate-cancer-grade-assessment/test_images/'
PRED_PATH = TEST
df = sub
t_df = test<choose_model_class>
model = LGBMClassifier(learning_rate = 0.03,
num_leaves=63,
boosting_type='gbdt',
objective = 'binary',
metric= 'binary_logloss,auc',
n_estimators= 750,
max_bin = 300,
subsample_for_bin = 50000,
min_data_in_leaf= 55,
min_sum_hessian_in_leaf = 7.5,
feature_fraction = 0.85,
bagging_freq = 10,
bagging_fraction = 0.85,
)
model.compile(optimizer = tf.keras.optimizers.Adam(lr= 1e-05), loss= tf.nn.sigmoid_cross_entropy_with_logits)
 model.load_weights('.. /input/pandaenetb042x256x256x3/efficientnet-b1-48-full-epochs60.h5')
 if os.path.exists(PRED_PATH):
predictions20 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = 0.5*np.sum(pred)
 predictions20.append(isup)
 del patches, img
gc.collect()
else:
PRED_PATH = TRAIN
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df = df_train.loc[:10]
df = df[['image_id','isup_grade']].copy()
predictions20 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = 0.5*np.sum(pred)
 predictions20.append(isup)
 del patches, img
gc.collect()
if os.path.exists(PRED_PATH):
predictions22 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile2(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = 0.5*np.sum(pred)
 predictions22.append(isup)
 del patches, img
gc.collect()
else:
PRED_PATH = TRAIN
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df = df_train.loc[:10]
df = df[['image_id','isup_grade']].copy()
predictions22 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile2(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = 0.5*np.sum(pred)
 predictions22.append(isup)
 del patches, img
gc.collect()<choose_model_class>
model.fit(X,y)
 y_pred = model.predict(X)
 print(roc_auc_score(y_pred,y)*100 )
class ConvNet(tf.keras.Model):
def __init__(self, engine, input_shape, weights):
super(ConvNet, self ).__init__()
self.engine = engine(
 include_top=False, input_shape=input_shape, weights=weights)
 self.avg_pool2d = tf.keras.layers.GlobalAveragePooling2D()
self.dropout = tf.keras.layers.Dropout(0.5)
 self.dense_1 = tf.keras.layers.Dense(1024)
 self.dense_2 = tf.keras.layers.Dense(5,activation='sigmoid')
 @tf.function
def call(self, inputs, **kwargs):
x = tf.reshape(inputs,(-1, IMG_SIZE, IMG_SIZE, 3))
x = self.engine(x)
 shape = x.shape
x = tf.reshape(x,(-1, N_TILES, shape[1], shape[2], shape[3]))
x = tf.transpose(x, perm=[0, 2, 1, 3, 4])
 x = tf.reshape(x,(-1, shape[1], N_TILES*shape[2], shape[3]))
x = self.avg_pool2d(x)
 x = self.dropout(x, training=False)
 x = self.dense_1(x)
 x = tf.nn.relu(x)
 return self.dense_2(x)
 is_ef = True
backbone_name = 'efficientnet-b2'
N_TILES = 48
IMG_SIZE = 256
if backbone_name.startswith('efficientnet'):
model_fn = getattr(efn, f'EfficientNetB{backbone_name[-1]}')
 model = ConvNet(engine=model_fn, input_shape=(IMG_SIZE, IMG_SIZE, 3), weights=None)
 n_TTA = 2
dummy_data = tf.zeros(( n_TTA * N_TILES, IMG_SIZE, IMG_SIZE, 3), dtype=tf.float32)
 _ = model(dummy_data)
 TRAIN = '.. /input/prostate-cancer-grade-assessment/train_images/'
MASKS = '.. /input/prostate-cancer-grade-assessment/train_label_masks/'
BASE_PATH = '.. /input/prostate-cancer-grade-assessment/'
train = pd.read_csv(BASE_PATH + "train.csv")
 train.head()
sub = pd.read_csv(".. /input/prostate-cancer-grade-assessment/sample_submission.csv")
 sub.head()
test = pd.read_csv(".. /input/prostate-cancer-grade-assessment/test.csv")
 test.head()
TEST = '.. /input/prostate-cancer-grade-assessment/test_images/'
PRED_PATH = TEST
df = sub
t_df = test<choose_model_class>
predictions = model.predict(test_data )
model.compile(optimizer = tf.keras.optimizers.Adam(lr= 1e-05), loss= tf.nn.sigmoid_cross_entropy_with_logits)
 model.load_weights('.. /input/pandaenetb042x256x256x3/efficientnet-b2-48-full-epochs60.h5')
 if os.path.exists(PRED_PATH):
predictions30 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = 0.5*np.sum(pred)
 predictions30.append(isup)
 del patches, img
gc.collect()
else:
PRED_PATH = TRAIN
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df = df_train.loc[:10]
df = df[['image_id','isup_grade']].copy()
predictions30 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = 0.5*np.sum(pred)
 predictions30.append(isup)
 del patches, img
gc.collect()
if os.path.exists(PRED_PATH):
predictions32 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile2(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = 0.5*np.sum(pred)
 predictions32.append(isup)
 del patches, img
gc.collect()
else:
PRED_PATH = TRAIN
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df = df_train.loc[:10]
df = df[['image_id','isup_grade']].copy()
predictions32 = []
for index, row in tqdm(df.iterrows() , total = df.shape[0]):
image_id = row['image_id']
img_path = PRED_PATH + image_id + '.tiff'
img = skimage.io.MultiImage(img_path)[1]
patches = tile2(img)
 patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = 0.5*np.sum(pred)
 predictions32.append(isup)
 del patches, img
gc.collect() 
<choose_model_class>
output = pd.DataFrame({'PassengerId': test_data.index, 'Survived': predictions})
 output.to_csv('my_submission34.csv', index=False )
<choose_model_class><EOS>