Kaggle competition: Prostate cANcer graDe Assessment (PANDA) Challenge
<SOS> metric: QuadraticWeightedKappa Kaggle data source: prostate-cancer-grade-assessment-(panda)-challenge<train_model>
DEBUG = False
model2 = train_model(model2, train_X, train_y, val_X, val_y, epochs=5 )<predict_on_test>
sys.path = [
'.. /input/efficientnet-pytorch/EfficientNet-PyTorch/EfficientNet-PyTorch-master',
] + sys.path
alt_pred_val_y = model2.predict(val_X, batch_size=1024, verbose=1 )<compute_test_metric>
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
f1, threshold = f1_smart(val_y, pred_val_y)
 print('Optimal F1: {} at threshold: {}'.format(f1, threshold))<compute_test_metric>
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)
f1, alt_threshold = f1_smart(val_y, alt_pred_val_y)
 print('Optimal F1: {} at threshold: {}'.format(f1, alt_threshold))<predict_on_test>
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 )
long_pred_val_y = model.predict(long_val_X, batch_size=1024, verbose=1 )<predict_on_test>
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)
def update_preds(pred_val_y, long_pred_val_y, long_id_list):
global threshold
threshold_range = 0.10
copy_pred_val_y = pred_val_y
for i, pred in enumerate(list(pred_val_y)) :
if(pred <(threshold + threshold_range)) and(pred >(threshold - threshold_range)) :
count = 1
sum_pred = pred
for long_id, long_pred in zip(long_id_list, long_pred_val_y.tolist()):
if long_id == i:
if(long_pred >(threshold + threshold_range)) or(long_pred <(threshold - threshold_range)) :
if long_pred[0] > pred:
copy_pred_val_y[i] = long_pred[0]
return copy_pred_val_y<predict_on_test>
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),
] )
pred_val_y = update_preds(pred_val_y, long_pred_val_y, long_id_list )<compute_test_metric>
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()
f1, threshold = f1_smart(val_y, pred_val_y)
 print('Optimal F1: {} at threshold: {}'.format(f1, threshold))<train_model>
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()
all_train_X = np.concatenate(( train_X,val_X),axis=0)
 all_train_y = np.concatenate(( train_y,val_y),axis=0)
 all_train_X = np.concatenate(( train_X,generalized_train_X),axis=0)
 all_train_y = np.concatenate(( train_y,generalized_train_y),axis=0)
 model = train_model(model, train_X, train_y, val_X, val_y, epochs=5 )<statistical_test>
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()
def seq_ensemble(pred_y, alt_pred_y):
global threshold, alt_threshold
threshold_range_list = [0.15,0.12,0.10,0.08,0.05,0.03,0.02,0.01]
copy_pred_val_y = pred_val_y
for threshold_range in threshold_range_list:
for i, pred_pair in enumerate(zip(list(pred_val_y),list(alt_pred_y))):
if(pred_pair[0] <(threshold + threshold_range)) and \ 
(pred_pair[0] >(threshold - threshold_range)) :
if(pred_pair[1] >(alt_threshold + threshold_range)) or \ 
(pred_pair[1] <(alt_threshold - threshold_range)) :
copy_pred_val_y[i] = pred_pair[1]
return copy_pred_val_y<compute_test_metric>
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()
pred_val_y = seq_ensemble(pred_val_y, alt_pred_val_y )<compute_test_metric>
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()
f1, threshold = f1_smart(val_y, pred_val_y)
 print('Optimal F1: {} at threshold: {}'.format(f1, threshold))<predict_on_test>
!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)
pred_test_y = model.predict(test_X, batch_size=1024, verbose=1 )<predict_on_test>
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()
long_pred_test_y = model.predict(long_test_X, batch_size=1024, verbose=1 )<compute_test_metric>
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
pred_test_y = update_preds(pred_test_y, long_pred_test_y, long_test_id )<save_to_csv>
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()
pred_test_y =(pred_test_y>threshold ).astype(int)
 out_df = pd.DataFrame({"qid":test_df["qid"].values})
 out_df['prediction'] = pred_test_y
out_df.to_csv("submission.csv", index=False )<predict_on_test>
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
pred_goofy_y = model.predict(goofy_test_X, batch_size=10, verbose=1)
 f1, threshold = f1_smart(goofy_test_y, pred_goofy_y)
 print('Optimal F1: {} at threshold: {}'.format(f1, threshold))
for question,pred in zip(goofy_test_questions, pred_goofy_y):
print(pred, " ", question, "
" )<set_options>
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()
%matplotlib inline<define_variables>
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)
 N_TILES = 48
IMG_SIZE = 256
model_fn = getattr(den, 'DenseNet121')
 model = ConvNet(engine=model_fn, input_shape=(IMG_SIZE, IMG_SIZE, 3), weights=None)
 n_TTA = 4
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
datagen = ImageDataGenerator(rescale=1./255)
 train_generator = datagen.flow_from_directory(
 '.. /input/train/',
batch_size=1,
class_mode='categorical' )<data_type_conversions>
model.compile(optimizer = tf.keras.optimizers.Adam(lr= 1e-05), loss= tf.nn.sigmoid_cross_entropy_with_logits)
 model.load_weights('.. /input/pandaenetb042x256x256x3/DenseNet121-48-full-epochs60.h5')
 if os.path.exists(PRED_PATH):
predictions40 = []
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)
 patches1 = patches.copy()
patches2 = patches.copy()
patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches1[k, ] = transforms_val0(image=patches1[k, ])['image']
patches2[k, ] = transforms_val1(image=patches2[k, ])['image']
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches1, patches2, patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = 0.25*np.sum(pred)
 predictions40.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()
predictions40 = []
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)
 patches1 = patches.copy()
patches2 = patches.copy()
patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches1[k, ] = transforms_val0(image=patches1[k, ])['image']
patches2[k, ] = transforms_val1(image=patches2[k, ])['image']
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches1, patches2, patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = 0.25*np.sum(pred)
 predictions40.append(isup)
 del patches, img
gc.collect()
if os.path.exists(PRED_PATH):
predictions42 = []
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)
 patches1 = patches.copy()
patches2 = patches.copy()
patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches1[k, ] = transforms_val0(image=patches1[k, ])['image']
patches2[k, ] = transforms_val1(image=patches2[k, ])['image']
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches1, patches2, patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = 0.25*np.sum(pred)
 predictions42.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()
predictions42 = []
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)
 patches1 = patches.copy()
patches2 = patches.copy()
patches3 = patches.copy()
patches4 = patches.copy()
k = 0
while k < N_TILES:
patches1[k, ] = transforms_val0(image=patches1[k, ])['image']
patches2[k, ] = transforms_val1(image=patches2[k, ])['image']
patches3[k, ] = transforms_val2(image=patches3[k, ])['image']
patches4[k, ] = transforms_val3(image=patches4[k, ])['image']
k += 1
image = np.stack([patches1, patches2, patches3, patches4])
 image = image / 255.0
pred = model.predict(image)
 isup = 0.25*np.sum(pred)
 predictions42.append(isup)
 del patches, img
gc.collect()
del model, dummy_data, sub, pred, train, isup, image
del patches1,patches2,patches3,patches4
gc.collect()
x, y = train_generator.next()
plt.imshow(( x[0]*255 ).astype('uint8')) ;
print(list(train_generator.class_indices.keys())[np.argmax(y)] )<concatenate>
sz = 256
N = 42
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(1)
 @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 = 42
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
n_TTA = 2
dummy_data = tf.zeros(( n_TTA * N_TILES, IMG_SIZE, IMG_SIZE, 3), dtype=tf.float32)
 _ = model(dummy_data)
X_data, Y_data = [], []
for _ in tqdm(range(2750)) :
x, y = train_generator.next()
X_data.append(x[0])
 Y_data.append(y[0])
 X_data = np.asarray(X_data)
 Y_data = np.asarray(Y_data )<define_search_model>
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-fold0-epochs40.h5')
 if os.path.exists(PRED_PATH):
predictions50 = []
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 = np.mean(pred)
 predictions50.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()
predictions50 = []
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 = np.mean(pred)
 predictions50.append(isup)
 del patches, img
gc.collect()
if os.path.exists(PRED_PATH):
predictions52 = []
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 = np.mean(pred)
 predictions52.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()
predictions52 = []
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 = np.mean(pred)
 predictions52.append(isup)
 del patches, img
gc.collect()
def get_model() :
input_img = Input(( 256, 256, 3))
X = BatchNormalization()(input_img)
 X = Convolution2D(16,(3, 3), activation='relu' )(X)
 X = BatchNormalization()(X)
 X = Convolution2D(16,(3, 3), activation='relu' )(X)
 X = MaxPooling2D()(X)
 X = Convolution2D(32,(3, 3), activation='relu' )(X)
 X = BatchNormalization()(X)
 X = Convolution2D(32,(3, 3), activation='relu' )(X)
 X = GlobalMaxPooling2D()(X)
 X = BatchNormalization()(X)
 X = Dense(512, activation='relu' )(X)
 X = Dropout(0.2 )(X)
 X = Dense(10, activation='softmax' )(X)
 model = Model(inputs=input_img, outputs=X)
 model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['acc'])
 model.summary()
return model<find_best_params>
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-fold4-epochs60.h5')
 if os.path.exists(PRED_PATH):
predictions60 = []
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 = np.mean(pred)
 predictions60.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()
predictions60 = []
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 = np.mean(pred)
 predictions60.append(isup)
 del patches, img
gc.collect()
if os.path.exists(PRED_PATH):
predictions62 = []
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 = np.mean(pred)
 predictions62.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()
predictions62 = []
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 = np.mean(pred)
 predictions62.append(isup)
 del patches, img
gc.collect()
model = get_model()<train_model>
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-fold2-epochs40.h5')
 if os.path.exists(PRED_PATH):
predictions70 = []
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 = np.mean(pred)
 predictions70.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()
predictions70 = []
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 = np.mean(pred)
 predictions70.append(isup)
 del patches, img
gc.collect()
if os.path.exists(PRED_PATH):
predictions72 = []
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 = np.mean(pred)
 predictions72.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()
predictions72 = []
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 = np.mean(pred)
 predictions72.append(isup)
 del patches, img
gc.collect()
del model, dummy_data, sub, pred, train, isup, image
del patches3,patches4
gc.collect()
<load_from_csv><EOS> 
PREDS =(1/5)*PREDS +(1/5)*PREDS1 +(1/5)*PREDS2 +(1/5)*PREDS3 +(1/5)*PREDS4
FINAL = np.round(( 6/10)*PREDS + 
(2/60)*np.array(predictions10)+(2/60)*np.array(predictions12)+ 
(2/60)*np.array(predictions20)+(2/60)*np.array(predictions22)+ 
(2/60)*np.array(predictions30)+(2/60)*np.array(predictions32)+ 
(0.5/10)*np.array(predictions40)+(0.5/10)*np.array(predictions42)+ 
(1/60)*np.array(predictions50)+(1/60)*np.array(predictions52)+ 
(1/60)*np.array(predictions60)+(1/60)*np.array(predictions62)+ 
(1/60)*np.array(predictions70)+(1/60)*np.array(predictions72))
df['isup_grade'] = FINAL.astype(int)
 df[['image_id', 'isup_grade']].to_csv('submission.csv', index=False)
 print(df.head())
 print()
print(df.isup_grade.value_counts() )