Kaggle competition: OSIC Pulmonary Fibrosis Progression
<SOS> metric: LaplaceLogLikelihood Kaggle data source: osic-pulmonary-fibrosis-progression<load_pretrained>
!pip install.. /input/kerasapplications/keras-team-keras-applications-3b180cb -f./ --no-index
!pip install.. /input/efficientnet/efficientnet-1.1.0/ -f./ --no-index
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>
import os
import cv2
import pydicom
import pandas as pd
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import random
from tqdm.notebook import tqdm
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import mean_absolute_error
from tensorflow_addons.optimizers import RectifiedAdam
from tensorflow.keras import Model
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M
from tensorflow.keras.optimizers import Nadam
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go
from PIL import Image
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>
def seed_everything(seed=2020):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed )
!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>
seed_everything(42 )
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>
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.compat.v1.Session(config=config )
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>
Dropout_model = 0.3865
FVC_weight = 0.2
Confidence_weight = 0.2
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>
commits_df = pd.DataFrame(columns = ['commit_num', 'FVC_weight', 'Dropout_model', 'LB_score', 'seed'] )
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>
n=0
commits_df.loc[n,'commit_num'] = 1
commits_df.loc[n,'Dropout_model'] = 0.4
commits_df.loc[n,'FVC_weight'] = 0.25
commits_df.loc[n,'LB_score'] = -6.8111
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>
n=1
commits_df.loc[n,'commit_num'] = 4
commits_df.loc[n,'Dropout_model'] = 0.36
commits_df.loc[n,'FVC_weight'] = 0.25
commits_df.loc[n,'LB_score'] = -6.8105
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<choose_model_class>
n=2
commits_df.loc[n,'commit_num'] = 6
commits_df.loc[n,'Dropout_model'] = 0.36
commits_df.loc[n,'FVC_weight'] = 0.35
commits_df.loc[n,'LB_score'] = -6.8158
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()<categorify>
n=3
commits_df.loc[n,'commit_num'] = 8
commits_df.loc[n,'Dropout_model'] = 0.35
commits_df.loc[n,'FVC_weight'] = 0.25
commits_df.loc[n,'LB_score'] = -6.8107
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)
<choose_model_class>
n=4
commits_df.loc[n,'commit_num'] = 9
commits_df.loc[n,'Dropout_model'] = 0.35
commits_df.loc[n,'FVC_weight'] = 0.3
commits_df.loc[n,'LB_score'] = -6.8125
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() 
<load_pretrained>
n=5
commits_df.loc[n,'commit_num'] = 10
commits_df.loc[n,'Dropout_model'] = 0.36
commits_df.loc[n,'FVC_weight'] = 0.2
commits_df.loc[n,'LB_score'] = -6.8089
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() 
<choose_model_class>
n=6
commits_df.loc[n,'commit_num'] = 11
commits_df.loc[n,'Dropout_model'] = 0.36
commits_df.loc[n,'FVC_weight'] = 0.15
commits_df.loc[n,'LB_score'] = -6.8100
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()<define_variables>
n=7
commits_df.loc[n,'commit_num'] = 13
commits_df.loc[n,'Dropout_model'] = 0.36
commits_df.loc[n,'FVC_weight'] = 0.175
commits_df.loc[n,'LB_score'] = -6.8096
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() )<import_modules>
n=8
commits_df.loc[n,'commit_num'] = 14
commits_df.loc[n,'Dropout_model'] = 0.36
commits_df.loc[n,'FVC_weight'] = 0.225
commits_df.loc[n,'LB_score'] = -6.8100
!echo "class EfficientNet: pass" > efficientnet_pytorch.py<set_options>
n=9
commits_df.loc[n,'commit_num'] = 15
commits_df.loc[n,'Dropout_model'] = 0.32
commits_df.loc[n,'FVC_weight'] = 0.2
commits_df.loc[n,'LB_score'] = -6.8092
try:
USE_APEX = True
except:
USE_APEX = False 
<init_hyperparams>
n=10
commits_df.loc[n,'commit_num'] = 16
commits_df.loc[n,'Dropout_model'] = 0.25
commits_df.loc[n,'FVC_weight'] = 0.2
commits_df.loc[n,'LB_score'] = -6.8093
USE_PRETRAINED = False
GlobalParams = collections.namedtuple('GlobalParams', [
'batch_norm_momentum', 'batch_norm_epsilon', 'dropout_rate',
'num_classes', 'width_coefficient', 'depth_coefficient',
'depth_divisor', 'min_depth', 'drop_connect_rate', 'image_size'])
 BlockArgs = collections.namedtuple('BlockArgs', [
'kernel_size', 'num_repeat', 'input_filters', 'output_filters',
'expand_ratio', 'id_skip', 'stride', 'se_ratio'])
 GlobalParams.__new__.__defaults__ =(None,)* len(GlobalParams._fields)
 BlockArgs.__new__.__defaults__ =(None,)* len(BlockArgs._fields)
 class SwishImplementation(torch.autograd.Function):
@staticmethod
def forward(ctx, i):
result = i * torch.sigmoid(i)
 ctx.save_for_backward(i)
 return result
@staticmethod
def backward(ctx, grad_output):
i = ctx.saved_variables[0]
sigmoid_i = torch.sigmoid(i)
 return grad_output *(sigmoid_i *(1 + i *(1 - sigmoid_i)))
 class MemoryEfficientSwish(nn.Module):
def forward(self, x):
return SwishImplementation.apply(x)
 class Swish(nn.Module):
def forward(self, x):
return x * torch.sigmoid(x)
 def round_filters(filters, global_params):

 multiplier = global_params.width_coefficient
if not multiplier:
return filters
divisor = global_params.depth_divisor
min_depth = global_params.min_depth
filters *= multiplier
min_depth = min_depth or divisor
new_filters = max(min_depth, int(filters + divisor / 2)// divisor * divisor)
 if new_filters < 0.9 * filters:
new_filters += divisor
return int(new_filters)
 def round_repeats(repeats, global_params):

 multiplier = global_params.depth_coefficient
if not multiplier:
return repeats
return int(math.ceil(multiplier * repeats))
def drop_connect(inputs, p, training):

 if not training: return inputs
batch_size = inputs.shape[0]
keep_prob = 1 - p
random_tensor = keep_prob
random_tensor += torch.rand([batch_size, 1, 1, 1], dtype=inputs.dtype, device=inputs.device)
 binary_tensor = torch.floor(random_tensor)
 output = inputs / keep_prob * binary_tensor
return output
def get_same_padding_conv2d(image_size=None):

 if image_size is None:
return Conv2dDynamicSamePadding
else:
return partial(Conv2dStaticSamePadding, image_size=image_size)
 def get_width_and_height_from_size(x):

 if isinstance(x, int): return x, x
if isinstance(x, list)or isinstance(x, tuple): return x
else: raise TypeError()
def calculate_output_image_size(input_image_size, stride):

 if input_image_size is None: return None
image_height, image_width = get_width_and_height_from_size(input_image_size)
 stride = stride if isinstance(stride, int)else stride[0]
image_height = int(math.ceil(image_height / stride))
image_width = int(math.ceil(image_width / stride))
return [image_height, image_width]
class Conv2dDynamicSamePadding(nn.Conv2d):

 def __init__(self, in_channels, out_channels, kernel_size, stride=1, dilation=1, groups=1, bias=True):
super().__init__(in_channels, out_channels, kernel_size, stride, 0, dilation, groups, bias)
 self.stride = self.stride if len(self.stride)== 2 else [self.stride[0]] * 2
def forward(self, x):
ih, iw = x.size() [-2:]
kh, kw = self.weight.size() [-2:]
sh, sw = self.stride
oh, ow = math.ceil(ih / sh), math.ceil(iw / sw)
 pad_h = max(( oh - 1)* self.stride[0] +(kh - 1)* self.dilation[0] + 1 - ih, 0)
 pad_w = max(( ow - 1)* self.stride[1] +(kw - 1)* self.dilation[1] + 1 - iw, 0)
 if pad_h > 0 or pad_w > 0:
x = F.pad(x, [pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2])
 return F.conv2d(x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
 class Conv2dStaticSamePadding(nn.Conv2d):

 def __init__(self, in_channels, out_channels, kernel_size, image_size=None, **kwargs):
super().__init__(in_channels, out_channels, kernel_size, **kwargs)
 self.stride = self.stride if len(self.stride)== 2 else [self.stride[0]] * 2
assert image_size is not None
ih, iw =(image_size, image_size)if isinstance(image_size, int)else image_size
kh, kw = self.weight.size() [-2:]
sh, sw = self.stride
oh, ow = math.ceil(ih / sh), math.ceil(iw / sw)
 pad_h = max(( oh - 1)* self.stride[0] +(kh - 1)* self.dilation[0] + 1 - ih, 0)
 pad_w = max(( ow - 1)* self.stride[1] +(kw - 1)* self.dilation[1] + 1 - iw, 0)
 if pad_h > 0 or pad_w > 0:
self.static_padding = nn.ZeroPad2d(( pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2))
else:
self.static_padding = Identity()
def forward(self, x):
x = self.static_padding(x)
 x = F.conv2d(x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
 return x
class Identity(nn.Module):
def __init__(self,):
super(Identity, self ).__init__()
def forward(self, input):
return input
def efficientnet_params(model_name):

 params_dict = {
'efficientnet-b0':(1.0, 1.0, 224, 0.2),
'efficientnet-b1':(1.0, 1.1, 240, 0.2),
'efficientnet-b2':(1.1, 1.2, 260, 0.3),
'efficientnet-b3':(1.2, 1.4, 300, 0.3),
'efficientnet-b4':(1.4, 1.8, 380, 0.4),
'efficientnet-b5':(1.6, 2.2, 456, 0.4),
'efficientnet-b6':(1.8, 2.6, 528, 0.5),
'efficientnet-b7':(2.0, 3.1, 600, 0.5),
'efficientnet-b8':(2.2, 3.6, 672, 0.5),
'efficientnet-l2':(4.3, 5.3, 800, 0.5),
}
return params_dict[model_name]
class BlockDecoder(object):

 @staticmethod
def _decode_block_string(block_string):

 assert isinstance(block_string, str)
 ops = block_string.split('_')
 options = {}
for op in ops:
splits = re.split(r'(\d.*)', op)
 if len(splits)>= 2:
key, value = splits[:2]
options[key] = value
assert(( 's' in options and len(options['s'])== 1)or 
(len(options['s'])== 2 and options['s'][0] == options['s'][1]))
return BlockArgs(
 kernel_size=int(options['k']),
num_repeat=int(options['r']),
input_filters=int(options['i']),
output_filters=int(options['o']),
expand_ratio=int(options['e']),
id_skip=('noskip' not in block_string),
se_ratio=float(options['se'])if 'se' in options else None,
stride=[int(options['s'][0])])
 @staticmethod
def _encode_block_string(block):

 args = [
'r%d' % block.num_repeat,
'k%d' % block.kernel_size,
's%d%d' %(block.strides[0], block.strides[1]),
'e%s' % block.expand_ratio,
'i%d' % block.input_filters,
'o%d' % block.output_filters
]
if 0 < block.se_ratio <= 1:
args.append('se%s' % block.se_ratio)
 if block.id_skip is False:
args.append('noskip')
 return '_'.join(args)
 @staticmethod
def decode(string_list):

 assert isinstance(string_list, list)
 blocks_args = []
for block_string in string_list:
blocks_args.append(BlockDecoder._decode_block_string(block_string))
return blocks_args
@staticmethod
def encode(blocks_args):

 block_strings = []
for block in blocks_args:
block_strings.append(BlockDecoder._encode_block_string(block))
return block_strings
def efficientnet(width_coefficient=None, depth_coefficient=None, dropout_rate=0.2,
drop_connect_rate=0.2, image_size=None, num_classes=1000):

 blocks_args = [
'r1_k3_s11_e1_i32_o16_se0.25', 'r2_k3_s22_e6_i16_o24_se0.25',
'r2_k5_s22_e6_i24_o40_se0.25', 'r3_k3_s22_e6_i40_o80_se0.25',
'r3_k5_s11_e6_i80_o112_se0.25', 'r4_k5_s22_e6_i112_o192_se0.25',
'r1_k3_s11_e6_i192_o320_se0.25',
]
blocks_args = BlockDecoder.decode(blocks_args)
 global_params = GlobalParams(
 batch_norm_momentum=0.99,
batch_norm_epsilon=1e-3,
dropout_rate=dropout_rate,
drop_connect_rate=drop_connect_rate,
num_classes=num_classes,
width_coefficient=width_coefficient,
depth_coefficient=depth_coefficient,
depth_divisor=8,
min_depth=None,
image_size=image_size, 
)
 return blocks_args, global_params
def get_model_params(model_name, override_params):

 if model_name.startswith('efficientnet'):
w, d, s, p = efficientnet_params(model_name)
 blocks_args, global_params = efficientnet(
 width_coefficient=w, depth_coefficient=d, dropout_rate=p, image_size=s)
 else:
raise NotImplementedError('model name is not pre-defined: %s' % model_name)
 if override_params:
global_params = global_params._replace(**override_params)
 return blocks_args, global_params
url_map = {
'efficientnet-b0': 'https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b0-355c32eb.pth',
'efficientnet-b1': 'https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b1-f1951068.pth',
'efficientnet-b2': 'https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b2-8bb594d6.pth',
'efficientnet-b3': 'https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b3-5fb5a3c3.pth',
'efficientnet-b4': 'https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b4-6ed6700e.pth',
'efficientnet-b5': 'https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b5-b6417697.pth',
'efficientnet-b6': 'https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b6-c76e70fd.pth',
'efficientnet-b7': 'https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b7-dcc49843.pth',
}
url_map_advprop = {
'efficientnet-b0': 'https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/adv-efficientnet-b0-b64d5a18.pth',
'efficientnet-b1': 'https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/adv-efficientnet-b1-0f3ce85a.pth',
'efficientnet-b2': 'https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/adv-efficientnet-b2-6e9d97e5.pth',
'efficientnet-b3': 'https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/adv-efficientnet-b3-cdd7c0f4.pth',
'efficientnet-b4': 'https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/adv-efficientnet-b4-44fb3a87.pth',
'efficientnet-b5': 'https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/adv-efficientnet-b5-86493f6b.pth',
'efficientnet-b6': 'https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/adv-efficientnet-b6-ac80338e.pth',
'efficientnet-b7': 'https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/adv-efficientnet-b7-4652b6dd.pth',
'efficientnet-b8': 'https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/adv-efficientnet-b8-22a8fe65.pth',
}
def load_pretrained_weights(model, model_name, load_fc=True, advprop=False):

 url_map_ = url_map_advprop if advprop else url_map
state_dict = model_zoo.load_url(url_map_[model_name])
 if load_fc:
model.load_state_dict(state_dict)
 else:
state_dict.pop('_fc.weight')
 state_dict.pop('_fc.bias')
 res = model.load_state_dict(state_dict, strict=False)
 assert set(res.missing_keys)== set(['_fc.weight', '_fc.bias']), 'issue loading pretrained weights'
print('Loaded pretrained weights for {}'.format(model_name))
class MBConvBlock(nn.Module):

 def __init__(self, block_args, global_params, image_size=None):
super().__init__()
self._block_args = block_args
self._bn_mom = 1 - global_params.batch_norm_momentum
self._bn_eps = global_params.batch_norm_epsilon
self.has_se =(self._block_args.se_ratio is not None)and(0 < self._block_args.se_ratio <= 1)
 self.id_skip = block_args.id_skip
inp = self._block_args.input_filters
oup = self._block_args.input_filters * self._block_args.expand_ratio
if self._block_args.expand_ratio != 1:
Conv2d = get_same_padding_conv2d(image_size=image_size)
 self._expand_conv = Conv2d(in_channels=inp, out_channels=oup, kernel_size=1, bias=False)
 self._bn0 = nn.BatchNorm2d(num_features=oup, momentum=self._bn_mom, eps=self._bn_eps)
 k = self._block_args.kernel_size
s = self._block_args.stride
Conv2d = get_same_padding_conv2d(image_size=image_size)
 self._depthwise_conv = Conv2d(
 in_channels=oup, out_channels=oup, groups=oup,
kernel_size=k, stride=s, bias=False)
 self._bn1 = nn.BatchNorm2d(num_features=oup, momentum=self._bn_mom, eps=self._bn_eps)
 image_size = calculate_output_image_size(image_size, s)
 if self.has_se:
Conv2d = get_same_padding_conv2d(image_size=(1,1))
num_squeezed_channels = max(1, int(self._block_args.input_filters * self._block_args.se_ratio))
self._se_reduce = Conv2d(in_channels=oup, out_channels=num_squeezed_channels, kernel_size=1)
 self._se_expand = Conv2d(in_channels=num_squeezed_channels, out_channels=oup, kernel_size=1)
 final_oup = self._block_args.output_filters
Conv2d = get_same_padding_conv2d(image_size=image_size)
 self._project_conv = Conv2d(in_channels=oup, out_channels=final_oup, kernel_size=1, bias=False)
 self._bn2 = nn.BatchNorm2d(num_features=final_oup, momentum=self._bn_mom, eps=self._bn_eps)
 self._swish = MemoryEfficientSwish()
def forward(self, inputs, drop_connect_rate=None):

 x = inputs
if self._block_args.expand_ratio != 1:
x = self._swish(self._bn0(self._expand_conv(inputs)))
 x = self._swish(self._bn1(self._depthwise_conv(x)))
 if self.has_se:
x_squeezed = F.adaptive_avg_pool2d(x, 1)
 x_squeezed = self._se_expand(self._swish(self._se_reduce(x_squeezed)))
 x = torch.sigmoid(x_squeezed)* x
x = self._bn2(self._project_conv(x))
input_filters, output_filters = self._block_args.input_filters, self._block_args.output_filters
if self.id_skip and self._block_args.stride == 1 and input_filters == output_filters:
if drop_connect_rate:
x = drop_connect(x, p=drop_connect_rate, training=self.training)
 x = x + inputs
return x
def set_swish(self, memory_efficient=True):

 self._swish = MemoryEfficientSwish() if memory_efficient else Swish()
class EfficientNet(nn.Module):

 def __init__(self, blocks_args=None, global_params=None):
super().__init__()
assert isinstance(blocks_args, list), 'blocks_args should be a list'
assert len(blocks_args)> 0, 'block args must be greater than 0'
self._global_params = global_params
self._blocks_args = blocks_args
bn_mom = 1 - self._global_params.batch_norm_momentum
bn_eps = self._global_params.batch_norm_epsilon
image_size = global_params.image_size
Conv2d = get_same_padding_conv2d(image_size=global_params.image_size)
 in_channels = 3
out_channels = round_filters(32, self._global_params)
 self._conv_stem = Conv2d(in_channels, out_channels, kernel_size=3, stride=2, bias=False)
 self._bn0 = nn.BatchNorm2d(num_features=out_channels, momentum=bn_mom, eps=bn_eps)
 image_size = calculate_output_image_size(image_size, 2)
 self._blocks = nn.ModuleList([])
 for block_args in self._blocks_args:
block_args = block_args._replace(
 input_filters=round_filters(block_args.input_filters, self._global_params),
output_filters=round_filters(block_args.output_filters, self._global_params),
num_repeat=round_repeats(block_args.num_repeat, self._global_params)
)
 self._blocks.append(MBConvBlock(block_args, self._global_params, image_size=image_size))
image_size = calculate_output_image_size(image_size, block_args.stride)
 if block_args.num_repeat > 1:
block_args = block_args._replace(input_filters=block_args.output_filters, stride=1)
 for _ in range(block_args.num_repeat - 1):
self._blocks.append(MBConvBlock(block_args, self._global_params, image_size=image_size))
in_channels = block_args.output_filters
out_channels = round_filters(1280, self._global_params)
 Conv2d = get_same_padding_conv2d(image_size=image_size)
 self._conv_head = Conv2d(in_channels, out_channels, kernel_size=1, bias=False)
 self._bn1 = nn.BatchNorm2d(num_features=out_channels, momentum=bn_mom, eps=bn_eps)
 self._avg_pooling = nn.AdaptiveAvgPool2d(1)
 self._dropout = nn.Dropout(self._global_params.dropout_rate)
 self._fc = nn.Linear(out_channels, self._global_params.num_classes)
 self._swish = MemoryEfficientSwish()
def set_swish(self, memory_efficient=True):

 self._swish = MemoryEfficientSwish() if memory_efficient else Swish()
for block in self._blocks:
block.set_swish(memory_efficient)
 def extract_features(self, inputs):

 x = self._swish(self._bn0(self._conv_stem(inputs)))
 for idx, block in enumerate(self._blocks):
drop_connect_rate = self._global_params.drop_connect_rate
if drop_connect_rate:
drop_connect_rate *= float(idx)/ len(self._blocks)
 x = block(x, drop_connect_rate=drop_connect_rate)
 x = self._swish(self._bn1(self._conv_head(x)))
 return x
def forward(self, inputs):

 bs = inputs.size(0)
 x = self.extract_features(inputs)
 x = self._avg_pooling(x)
 x = x.view(bs, -1)
 x = self._dropout(x)
 x = self._fc(x)
 return x
@classmethod
def from_name(cls, model_name, override_params=None):
cls._check_model_name_is_valid(model_name)
 blocks_args, global_params = get_model_params(model_name, override_params)
 return cls(blocks_args, global_params)
 @classmethod
def from_pretrained(cls, model_name, advprop=False, num_classes=1000, in_channels=3):
model = cls.from_name(model_name, override_params={'num_classes': num_classes})
 load_pretrained_weights(model, model_name, load_fc=(num_classes == 1000), advprop=advprop)
 model._change_in_channels(in_channels)
 return model
@classmethod
def get_image_size(cls, model_name):
cls._check_model_name_is_valid(model_name)
 _, _, res, _ = efficientnet_params(model_name)
 return res
@classmethod
def _check_model_name_is_valid(cls, model_name):

 valid_models = ['efficientnet-b'+str(i)for i in range(9)]
if model_name not in valid_models:
raise ValueError('model_name should be one of: ' + ', '.join(valid_models))
def _change_in_channels(model, in_channels):
if in_channels != 3:
Conv2d = get_same_padding_conv2d(image_size = model._global_params.image_size)
 out_channels = round_filters(32, model._global_params)
 model._conv_stem = Conv2d(in_channels, out_channels, kernel_size=3, stride=2, bias=False)
 class FeatureEfficientNet(EfficientNet):
def forward(self, inputs):

 bs = inputs.size(0)
 x = self.extract_features(inputs)
 return x
class PandaDataset(D.Dataset):
def __init__(self, images, labels, insts=None, img_size=2, transform=None, bin_label=False,
root_path='', istest=False, return_index=True,
use_cache=False, mixup=False, mixup_alpha=1.0, separate_image=False, cat_insts=False):
assert len(images)== len(labels)
 self.images = images
self.labels = labels
self.insts = insts
self.img_size = img_size
self.transform = transform
self.bin_label = bin_label
self.root = root_path
self.istest = istest
self.return_index = return_index
self.use_cache = use_cache
self.cache = {}
self.mixup = mixup
self.mixup_alpha = mixup_alpha
self.separate_image = separate_image
self.cat_insts = cat_insts
def __len__(self):
return len(self.images)
 def _load_data(self, idx):
cache_loaded = False
if idx in self.cache.keys() :
image = self.cache[idx]
cache_loaded = True
else:
if self.istest:
fpath = str(self.root/'test_images'/f'{self.images[idx]}.tiff')
 else:
fpath = str(self.root/'train_images'/f'{self.images[idx]}.tiff')
 image = skimage.io.MultiImage(fpath)[self.img_size]
if self.use_cache and not self.separate_image:
self.cache[idx] = image
if self.transform:
if self.separate_image:
if cache_loaded:
pass
else:
assert 'tile' in self.transform.keys()
image = self.transform['tile'](image=image)['image']
if self.use_cache:
self.cache[idx] = image
output = []
for tile in image:
output.append(self.transform['augmentation'](image=tile)['image'])
 output = torch.stack(output)
 else:
output = self.transform(image=image)['image']
label = self.labels[idx]
if self.insts is not None and self.cat_insts:
output = torch.flatten(output)
 insts = self.insts[idx]
if insts == 'karolinska':
insts = torch.tensor([0.0])
 elif insts == 'radboud':
insts = torch.tensor([1.0])
 output = torch.cat(( output, insts))
return output, label
def __getitem__(self, idx):
image, label = self._load_data(idx)
 if self.mixup:
idx2 = np.random.randint(0, len(self.images))
lam = np.random.beta(self.mixup_alpha, self.mixup_alpha)
 image2, label2 = self._load_data(idx2)
 image = torch.cat([torch.Tensor([lam]), image.view(-1), image2.view(-1)])
 label = lam * label +(1 - lam)* label2
if self.bin_label:
if self.mixup:
label_dec = label - label_int
label2 = torch.zeros(5)
 label2[:label_int] = 1.0
if label_int < 5:
label2[label_int] = label_dec
else:
label2 = torch.zeros(5)
 label2[:label] = 1
else:
label2 = label
if self.return_index:
return image, label2, idx
else:
return image, label2<set_options>
n=11
commits_df.loc[n,'commit_num'] = 17
commits_df.loc[n,'Dropout_model'] = 0.25
commits_df.loc[n,'FVC_weight'] = 0.5
commits_df.loc[n,'LB_score'] = -6.8283
def seed_everything(seed=2020):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 def print_config(cfg):
items = [
'name',
'patch_size', 'patch_dim',
'resume', 'img_size', 'batch_size', 'lr', 'epochs', 'CV', 'seed',
'use_cache', 'separate_image', 'return_index', 'bin_label', 'mixup',
'model', 'criterion', 'metric', 'log_metrics', 'stopper', 'event', 'transform',
]
print(f'
----- Config -----')
 for key in items:
try:
value = eval(f'cfg.{key}')
 print(f'{key}: {value}')
 except:
print(f'{key}: ERROR')
 print(f'----- Config -----
' )<categorify>
n=12
commits_df.loc[n,'commit_num'] = 18
commits_df.loc[n,'Dropout_model'] = 0.38
commits_df.loc[n,'FVC_weight'] = 0.2
commits_df.loc[n,'LB_score'] = -6.8087
MAX_GRIDS = 200
def encode_gleason(gleason):
if gleason == 'negative':
a = b = 0
else:
a, b = gleason.split('+')
 a, b = int(a), int(b)
 if a >= 3:
a -= 1
else:
a = 1
if b >= 3:
b -= 1
else:
b = 1
a = [1 for _ in range(a)] + [0 for _ in range(4 - a)]
b = [1 for _ in range(b)] + [0 for _ in range(4 - b)]
return a + b
def Rotate(img, degrees=45): 
(h, w)= img.shape[:2]
M = cv2.getRotationMatrix2D(( w // 2, h // 2), degrees, 1.0)
 M_ = M[:, :2].T
corners = np.array([[w // 2, w // 2], [h // 2, -h // 2]])
 new_corners = M_ @ corners
del_y = max(np.abs(new_corners[0])) - w // 2
del_x = max(np.abs(new_corners[1])) - h // 2
pad_x, pad_y = int(max(del_x, 0)) , int(max(del_y, 0))
img = np.pad(img,(( pad_x, pad_x),(pad_y, pad_y),(0, 0)) , constant_values=255)
(h, w)= img.shape[:2]
M = cv2.getRotationMatrix2D(( w // 2, h // 2), degrees, 1.0)
 rotated = cv2.warpAffine(img, M,(w, h), cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT,
borderValue=(255, 255, 255))
return rotated
def get_img(png_path, mode = 0, tile_size = 224, patch_dim = 8):
if '.png' in png_path:
img_ = cv2.imread(png_path)
 else:
img_ = io.MultiImage(png_path)[1]
img = cv2.resize(img_,(img_.shape[1], img_.shape[0]), cv2.INTER_AREA)
 mean = 1 - torch.tensor([.485,.456,.406] ).float().unsqueeze(1 ).unsqueeze(1 ).unsqueeze(0)
 std = torch.tensor([.229,.224,.225] ).float().unsqueeze(1 ).unsqueeze(1 ).unsqueeze(0)
 if mode % 2 == 1:
img = np.pad(img,(( tile_size // 2, tile_size // 2), 
(tile_size // 2, tile_size // 2), 
(0, 0)) , constant_values=255)
 degrees =(mode % 3)* 30
img = Rotate(img, degrees)
 size = tile_size
if img.shape[0] % size == 0:
pad_h = 0
else:
pad_h =(img.shape[0] // size + 1)* size - img.shape[0]
if img.shape[1] % size == 0:
pad_w = 0
else:
pad_w =(img.shape[1] // size + 1)* size - img.shape[1]
img = np.pad(img,(( pad_h, 0),(pad_w, 0),(0, 0)) , constant_values=255)
 h_, w_ = img.shape[0] // size, img.shape[1] // size
imgs = img.reshape(h_, size, w_, size, 3 ).transpose(0, 2, 1, 3, 4 ).reshape(-1, size, size, 3)
 brightness = imgs.reshape(imgs.shape[0], -1 ).mean(1)
 indices = np.argsort(brightness)
 num_valid = np.array(brightness < 250 ).astype(np.uint8 ).sum()
indices = indices[:num_valid]
img = np.concatenate(imgs[indices], axis=1)
 imgs = np.split(img, img.shape[1] // tile_size, axis=1)
 drop =(np.random.rand(len(imgs)) < 0.0 ).astype(np.float32)
 brightness = np.array([tile.mean() for tile in imgs])
 brightness = drop * 255 +(1 - drop)* brightness
indices = np.argsort(brightness)[:patch_dim * patch_dim]
imgs = [imgs[i].astype(np.uint8)for i in indices]
if len(imgs)< patch_dim * patch_dim:
deficit = patch_dim * patch_dim - len(imgs)
 imgs = np.concatenate([imgs + [np.ones([tile_size , tile_size, 3])* 255 for _ in range(deficit)]], axis=1)
 img = 255 - np.stack([img for img in imgs], axis=0)
 img = torch.from_numpy(img ).permute(0, 3, 1, 2 ).float() / 255.
 img =(img - mean)/ std
return img
class PandaDataset_Inference(Dataset):
def __init__(self, df, tta_mode, folder,tile_size = 224, patch_dim = 8):
self.names = df.image_id.values
self.df = df
self.tta_mode = tta_mode
self.folder = folder
self.tile_size = tile_size
self.patch_dim = patch_dim
def __getitem__(self, idx):
tta_mode = random.randint(0,7)
 img = get_img(os.path.join(self.folder, self.names[idx] + '.tiff'), tta_mode, self.tile_size, self.patch_dim)
 return img,img
def __len__(self):
return len(self.names)
<categorify>
n=13
commits_df.loc[n,'commit_num'] = 19
commits_df.loc[n,'Dropout_model'] = 0.39
commits_df.loc[n,'FVC_weight'] = 0.2
commits_df.loc[n,'LB_score'] = -6.8090
def to_mish(model):
for child_name, child in model.named_children() :
if isinstance(child,(nn.ReLU, Swish, MemoryEfficientSwish, Swish_timm)) or child_name == '_swish':
print(child_name)
 setattr(model, child_name, Mish())
 else:
to_mish(child )<choose_model_class>
n=14
commits_df.loc[n,'commit_num'] = 20
commits_df.loc[n,'Dropout_model'] = 0.37
commits_df.loc[n,'FVC_weight'] = 0.2
commits_df.loc[n,'LB_score'] = -6.8092
class Classification:
img_size = 1
patch_size = 224
patch_dim = 8
batch_size = 8
separate_image = True
return_index = False
bin_label = False
cat_insts = False
model = PatchPoolModel2(
 base_model = senet_mod(se_resnext50_32x4d, pretrained=USE_PRETRAINED),
patch_total=patch_dim**2, num_classes=6 
)
 transform = {
'test': {
'tile': Compose([
MakePatches(patch_size, patch_dim, concat=False, always_apply=True)
 ]),
'augmentation': Compose([
ShiftScaleRotate(scale_limit=0.0625, rotate_limit=15, p=0.5),
HorizontalFlip(p=0.5), VerticalFlip(p=0.5),
Normalize([0.910, 0.819, 0.878],
[0.363, 0.499, 0.404], always_apply=True),
ToTensor()
])
 },
}
class Classification2:
img_size = 1
patch_size = 224
patch_dim = 8
batch_size = 8
separate_image = True
return_index = False
bin_label = False
cat_insts = False
model = PatchPoolModel2(
 base_model = xception_mod(in_channel=3, num_classes=1000, pretrained=USE_PRETRAINED),
patch_total=patch_dim**2, num_classes=6 
)
 transform = {
'test': {
'tile': Compose([
MakePatches(patch_size, patch_dim, concat=False, always_apply=True)
 ]),
'augmentation': Compose([
ShiftScaleRotate(scale_limit=0.0625, rotate_limit=15, p=0.5),
HorizontalFlip(p=0.5), VerticalFlip(p=0.5),
Normalize([0.910, 0.819, 0.878],
[0.363, 0.499, 0.404], always_apply=True),
ToTensor()
])
 },
}
class Regression:
img_size = 1
patch_size = 224
patch_dim = 8
batch_size = 12
separate_image = True
return_index = False
bin_label = False
cat_insts = False
model = PatchPoolModel2(
 base_model=senet_mod(se_resnext50_32x4d, pretrained=USE_PRETRAINED),
patch_total=patch_dim**2, num_classes=1 
)
 transform = {
'test': {
'tile': Compose([
MakePatches(patch_size, patch_dim, concat=False, always_apply=True)
 ]),
'augmentation': Compose([
ShiftScaleRotate(scale_limit=0.0625, rotate_limit=15, p=0.5),
HorizontalFlip(p=0.5), VerticalFlip(p=0.5),
Normalize([0.910, 0.819, 0.878],
[0.363, 0.499, 0.404], always_apply=True),
ToTensor()
])
 },
}
class OrdinalRegressionBear:
img_size = 1
patch_size = 224
patch_dim = 8
batch_size = 6
separate_image = True
return_index = False
bin_label = True
cat_insts = False
model = PatchPoolModel2(
 base_model = senet_mod(se_resnext50_32x4d, pretrained=USE_PRETRAINED),
patch_total=patch_dim**2, num_classes=5 
)
 transform = {
'test': {
'tile': Compose([
MakePatches(patch_size, patch_dim, concat=False, always_apply=True)
 ]),
'augmentation': Compose([
ShiftScaleRotate(scale_limit=0.0625, rotate_limit=15, p=0.5),
HorizontalFlip(p=0.5), VerticalFlip(p=0.5),
Normalize([0.910, 0.819, 0.878],
[0.363, 0.499, 0.404], always_apply=True),
ToTensor()
])
 },
}
class OrdinalRegressionBear101:
img_size = 1
patch_size = 224
patch_dim = 8
batch_size = 6
separate_image = True
return_index = False
bin_label = True
cat_insts = False
model = PatchPoolModel2(
 base_model = senet_mod(se_resnext101_32x4d, pretrained=USE_PRETRAINED),
patch_total=patch_dim**2, num_classes=5 
)
 transform = {
'test': {
'tile': Compose([
MakePatches(patch_size, patch_dim, concat=False, always_apply=True)
 ]),
'augmentation': Compose([
ShiftScaleRotate(scale_limit=0.0625, rotate_limit=15, p=0.5),
HorizontalFlip(p=0.5), VerticalFlip(p=0.5),
Normalize([0.910, 0.819, 0.878],
[0.363, 0.499, 0.404], always_apply=True),
ToTensor()
])
 },
}
class OrdinalRegression0:
img_size = 1
patch_size = 256
patch_dim = 6
batch_size = 6
separate_image = True
return_index = False
bin_label = True
cat_insts = False
model = PatchPoolModel2(
 base_model=FeatureEfficientNet.from_name('efficientnet-b0'),
patch_total=patch_dim**2, num_classes=5 
)
 to_mish(model)
 transform = {
'test': {
'tile': Compose([
MakePatches(patch_size, patch_dim, concat=False, always_apply=True)
 ]),
'augmentation': Compose([
ShiftScaleRotate(scale_limit=0.0625, rotate_limit=15, p=0.5),
HorizontalFlip(p=0.5), VerticalFlip(p=0.5),
Normalize([0.910, 0.819, 0.878],
[0.363, 0.499, 0.404], always_apply=True),
ToTensor()
])
 },
}
class OrdinalRegression1:
img_size = 1
patch_size = 224
patch_dim = 8
batch_size = 6
separate_image = True
return_index = False
bin_label = True
cat_insts = False
model = PatchPoolModel2(
 base_model=FeatureEfficientNet.from_name('efficientnet-b0'),
patch_total=patch_dim**2, num_classes=5 
)
 to_mish(model)
 transform = {
'test': {
'tile': Compose([
MakePatches(patch_size, patch_dim, concat=False, always_apply=True)
 ]),
'augmentation': Compose([
ShiftScaleRotate(scale_limit=0.0625, rotate_limit=15, p=0.5),
HorizontalFlip(p=0.5), VerticalFlip(p=0.5),
Normalize([0.910, 0.819, 0.878],
[0.363, 0.499, 0.404], always_apply=True),
ToTensor()
])
 },
}
class OrdinalRegression2:
img_size = 1
patch_size = 192
patch_dim = 8
batch_size = 6
separate_image = True
return_index = False
bin_label = True
cat_insts = False
model = PatchPoolModel2(
 base_model=FeatureEfficientNet.from_name('efficientnet-b0'),
patch_total=patch_dim**2, num_classes=5 
)
 to_mish(model)
 transform = {
'test': {
'tile': Compose([
MakePatches(patch_size, patch_dim, concat=False, always_apply=True)
 ]),
'augmentation': Compose([
ShiftScaleRotate(scale_limit=0.0625, rotate_limit=15, p=0.5),
HorizontalFlip(p=0.5), VerticalFlip(p=0.5),
Normalize([0.910, 0.819, 0.878],
[0.363, 0.499, 0.404], always_apply=True),
ToTensor()
])
 },
}
class MixOrdinalRegression:
img_size = 1
patch_size = 224
patch_dim = 7
batch_size = 8
separate_image = True
return_index = False
bin_label = True
cat_insts = False
model = MixPatchPoolModel(
 base_model=FeatureEfficientNet.from_name('efficientnet-b0'),
patch_size=patch_size, patch_total=patch_dim**2, num_classes=5, 
)
 transform = {
'test': {
'tile': Compose([
MakePatches(patch_size, patch_dim, concat=False, always_apply=True)
 ]),
'augmentation': Compose([
ShiftScaleRotate(scale_limit=0.0625, rotate_limit=15, p=0.5),
HorizontalFlip(p=0.5), VerticalFlip(p=0.5),
Normalize([0.910, 0.819, 0.878],
[0.363, 0.499, 0.404], always_apply=True),
ToTensor()
])
 },
} 
<define_variables>
n=15
commits_df.loc[n,'commit_num'] = 21
commits_df.loc[n,'Dropout_model'] = 0.38
commits_df.loc[n,'FVC_weight'] = 0.19
commits_df.loc[n,'LB_score'] = -6.8093
INPUT_PATH = Path('.. /input/prostate-cancer-grade-assessment/')
 USE_PRETRAINED = False<create_dataframe>
n=16
commits_df.loc[n,'commit_num'] = 23
commits_df.loc[n,'Dropout_model'] = 0.38
commits_df.loc[n,'FVC_weight'] = 0.2
commits_df.loc[n,'LB_score'] = -6.8402
def simple_inference_(snapshots, df, cfg, tta, folder):
print(cfg.patch_size, cfg.patch_dim)
 images = df.image_id.values
insts = df.data_provider.values
labels = np.zeros(len(df), dtype=int)
 predictions = np.zeros(( tta, len(snapshots), len(df)) , dtype=np.float16)
 model = deepcopy(cfg.model)
 model.to(device)
 ds = PandaDataset_Inference(df, None, folder, cfg.patch_size, cfg.patch_dim)
 loader = D.DataLoader(ds, batch_size=cfg.batch_size, shuffle=False, num_workers=2)
 with torch.no_grad() :
for tta_i, tta_fold in enumerate(range(tta)) :
pred_fold = []
for idx,(inputs, _)in enumerate(tqdm(loader, total=len(loader), desc=f'tta{tta_i}')) :
pred_batch = np.zeros(( len(snapshots), len(inputs)) , dtype=np.float16)
 for fold_i, fold_snapshot in enumerate(snapshots):
model.load_state_dict(torch.jit.load(fold_snapshot, map_location='cpu' ).state_dict())
 model.eval()
inputs = inputs.to(device)
 outputs = model(inputs)
 if outputs.shape[1] == 6:
outputs = F.softmax(outputs, dim=1 ).cpu().detach().numpy()
outputs = np.dot(outputs, np.arange(6))
elif outputs.shape[1] == 7:
outputs = F.softmax(outputs[:, :6], dim=1 ).cpu().detach().numpy()
outputs = np.dot(outputs, np.arange(6))
elif outputs.shape[1] == 5:
outputs = outputs.sigmoid().sum(1 ).cpu().detach().numpy()
elif outputs.shape[1] == 1:
outputs = outputs.reshape(-1 ).cpu().detach().numpy()
pred_batch[fold_i] = outputs
pred_fold.append(pred_batch)
 predictions[tta_i] = np.concatenate(pred_fold, axis=1)
 return predictions
def simple_inference(snapshots, df, cfg, tta=1, folder=None):
images = df.image_id.values
insts = df.data_provider.values
labels = np.zeros(len(df), dtype=int)
 ds = PandaDataset(
 images=images, labels=labels, insts=insts,
img_size=cfg.img_size, transform=cfg.transform['test'],
return_index=cfg.return_index, bin_label=cfg.bin_label,
separate_image=cfg.separate_image, cat_insts=cfg.cat_insts,
root_path=INPUT_PATH, istest=IS_TEST)
 loader = D.DataLoader(ds, batch_size=cfg.batch_size, shuffle=False, num_workers=2)
 predictions = np.zeros(( tta, len(snapshots), len(df)) , dtype=np.float16)
 model = deepcopy(cfg.model)
 model.to(device)
 with torch.no_grad() :
for tta_i, tta_fold in enumerate(range(tta)) :
pred_fold = []
for idx,(inputs, _)in enumerate(tqdm(loader, total=len(loader), desc=f'tta{tta_i}')) :
pred_batch = np.zeros(( len(snapshots), len(inputs)) , dtype=np.float16)
 for fold_i, fold_snapshot in enumerate(snapshots):
load_snapshots_to_model(fold_snapshot, model=model)
 model.eval()
inputs = inputs.to(device)
 outputs = model(inputs)
 if outputs.shape[1] == 6:
outputs = F.softmax(outputs, dim=1 ).cpu().detach().numpy()
outputs = np.dot(outputs, np.arange(6))
elif outputs.shape[1] == 7:
outputs = F.softmax(outputs[:, :6], dim=1 ).cpu().detach().numpy()
outputs = np.dot(outputs, np.arange(6))
elif outputs.shape[1] == 5:
outputs = outputs.sigmoid().sum(1 ).cpu().detach().numpy()
elif outputs.shape[1] == 1:
outputs = outputs.reshape(-1 ).cpu().detach().numpy()
pred_batch[fold_i] = outputs
pred_fold.append(pred_batch)
 predictions[tta_i] = np.concatenate(pred_fold, axis=1)
 return predictions 
<load_from_csv>
commits_df['LB_score'] = pd.to_numeric(commits_df['LB_score'])
 commits_df['max'] = 0
commits_df.loc[commits_df['LB_score'].idxmax() , 'max'] = 1
train_df = pd.read_csv(INPUT_PATH/'train.csv')
 test_df = pd.read_csv(INPUT_PATH/'test.csv')
 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 TTA = 2
if(INPUT_PATH/'test_images' ).exists() :
print('Inference')
 target = test_df
IS_TEST = True
folder = INPUT_PATH/'test_images'
else:
print('Debug')
 target = train_df.head(10)
 IS_TEST = False
TTA = 1
folder = INPUT_PATH/'train_images'
seed_everything(2020)
 target.head()
all_predictions_ = []
for c in submission_setting_:
all_predictions_.append(simple_inference_(c['snapshot_path'], target, c['config'], TTA, folder))
all_predictions = np.concatenate(all_predictions_)
 print(all_predictions.shape)
<data_type_conversions>
commits_df['seed'] = 42
commits_df.loc[commits_df['commit_num'] == 23, 'seed'] = 0
def logits_to_predictions(pred):
return np.dot(F.softmax(torch.Tensor(pred), 1 ).numpy() , np.arange(6))
def soft_vote(pred):
return np.clip(np.mean(pred, axis=0 ).round() , 0, 5 ).astype(int)
 def hard_vote(pred):
return np.clip(stats.mode(pred.round().astype(int), axis=0)[0][0], 0, 5 ).astype(int )<prepare_output>
train = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/train.csv' )
predictions = np.zeros(len(target), dtype=int)
 print(all_predictions)
 predictions = soft_vote(all_predictions.mean(1))<compute_test_metric>
def get_tab(df):
vector = [(df.Age.values[0] - 30)/ 30]
if df.Sex.values[0] == 'male':
vector.append(0)
 else:
vector.append(1)
 if df.SmokingStatus.values[0] == 'Never smoked':
vector.extend([0,0])
 elif df.SmokingStatus.values[0] == 'Ex-smoker':
vector.extend([1,1])
 elif df.SmokingStatus.values[0] == 'Currently smokes':
vector.extend([0,1])
 else:
vector.extend([1,0])
 return np.array(vector )
labels_ = target.isup_grade.values
qwk = QWK(6)
 print(qwk(labels_, predictions))
print(predictions )<define_variables>
A = {}
TAB = {}
P = []
for i, p in tqdm(enumerate(train.Patient.unique())) :
sub = train.loc[train.Patient == p, :]
fvc = sub.FVC.values
weeks = sub.Weeks.values
c = np.vstack([weeks, np.ones(len(weeks)) ] ).T
a, b = np.linalg.lstsq(c, fvc)[0]
A[p] = a
TAB[p] = get_tab(sub)
 P.append(p )
OPTIMIZE_QWK = True
HARD_VOTE = False
limit_loss = 1 - 1/12
mean_predictions = all_predictions.mean(1)
 hard_predictions = np.zeros_like(mean_predictions)
 print(mean_predictions.shape)
 print(hard_predictions.shape)
 if OPTIMIZE_QWK:
oof_all_list = []
for ic, c in enumerate(submission_setting_):
oof_list = []
print('============================================')
 for npy in c['oof_path']:
print(npy)
 oof = np.load(npy)
 oof_list.append(oof)
 oof_all_list.append(np.sum(oof_list, axis=0))
all_coefs = []
for i_oof, oof_fold in enumerate(oof_all_list):
print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
 print(len(oof_all_list))
mask_fold = oof_fold.sum(axis=1)!= 0
print(mask_fold.shape)
 print(np.sum(mask_fold))
output_shape = oof_fold.shape
if output_shape[0] == 9555:
_train = pd.read_csv('.. /input/mixup-baseline-b0-224-8-train2/train2.csv')
 _train = _train.query('certain_flag2 == 1')
 labels = _train.isup_grade.values
insts = _train.data_provider.values
elif output_shape[0] == 8493:
_train = pd.read_csv('.. /input/panda-seed-gacha/gatcha_train_nr20.csv')
 _train = _train.query('certain_flag2 == 1')
 labels = _train.isup_grade.values
insts = _train.data_provider.values
elif output_shape[0] == 10086:
_train = pd.read_csv('.. /input/panda-seed-gacha/gatcha_train_nr5.csv')
 labels = _train.isup_grade.values
insts = _train.data_provider.values
elif output_shape[0] == 9024:
_train = pd.read_csv('.. /input/panda-seed-gacha/gatcha_train_nr15.csv')
 _train = _train.query('certain_flag2 == 1')
 labels = _train.isup_grade.values
insts = _train.data_provider.values
labels = df.isup_grade.values
insts = df.data_provider.values
elif output_shape[0] == 10615:
_train = pd.read_csv('.. /input/panda-seed-gacha/train.csv')
 _train = _train[_train.image_id.values != '3790f55cad63053e956fb73027179707'].reset_index(drop=True)
 labels = _train.isup_grade.values
insts = _train.data_provider.values
else:
_train = pd.read_csv('.. /input/panda-seed-gacha/train.csv')
 labels = _train.isup_grade.values
insts = _train.data_provider.values
if output_shape[1] == 5:
oof_fold = torch.from_numpy(oof_fold[mask_fold] ).float()
label_fold = torch.zeros_like(oof_fold)
 for i, t in enumerate(labels[mask_fold]):
if t == 0:
continue
label_fold[i, 0:t] = 1
criterion = nn.BCEWithLogitsLoss(reduction='none')
 loss_values = criterion(oof_fold, label_fold ).mean(1)
 _, certainty_mask = loss_values.topk(round(len(oof_fold)*limit_loss), largest=False)
 oof_to_use = oof_fold[certainty_mask].sigmoid().sum(1 ).numpy()
label_to_use = label_fold[certainty_mask].sum(1 ).numpy()
analyse_results(oof_to_use.round() , label_to_use)
 optR = OptimizedRounder()
optR.fit(oof_to_use, label_to_use)
 coefficients = optR.coefficients()
print(coefficients)
 all_coefs.append(coefficients)
 hard_predictions[i_oof] = optR.predict(mean_predictions[i_oof], coefficients)
 analyse_results(optR.predict(oof_to_use, coefficients), label_to_use)
 all_coefs = np.vstack(all_coefs)
 print(all_coefs)
 print(all_coefs.mean(0))
soft_predictions = optR.predict(mean_predictions.mean(0), all_coefs.mean(0))
if HARD_VOTE:
print('HARD_VOTE')
 print(hard_predictions)
 optimized_predictions = mode(hard_predictions)[0][0]
print(optimized_predictions)
 else:
print('SOFT_PRED')
 print(mean_predictions)
 print(soft_predictions)
 optimized_predictions = soft_predictions
else:
print(all_predictions.mean(0 ).mean(0))
optimized_predictions = all_predictions.mean(0 ).mean(0 ).round()
print(optimized_predictions )<load_from_csv>
class IGenerator(Sequence):
BAD_ID = ['ID00011637202177653955184', 'ID00052637202186188008618']
def __init__(self, keys, a, tab, batch_size=32):
self.keys = [k for k in keys if k not in self.BAD_ID]
self.a = a
self.tab = tab
self.batch_size = batch_size
self.train_data = {}
for p in train.Patient.values:
self.train_data[p] = os.listdir(f'.. /input/osic-pulmonary-fibrosis-progression/train/{p}/')
 def __len__(self):
return 1000
def __getitem__(self, idx):
x = []
a, tab = [], []
keys = np.random.choice(self.keys, size = self.batch_size)
 for k in keys:
try:
i = np.random.choice(self.train_data[k], size=1)[0]
img = get_img(f'.. /input/osic-pulmonary-fibrosis-progression/train/{k}/{i}')
 x.append(img)
 a.append(self.a[k])
 tab.append(self.tab[k])
 except:
print(k, i)
 x,a,tab = np.array(x), np.array(a), np.array(tab)
 x = np.expand_dims(x, axis=-1)
 return [x, tab] , a
submission = pd.read_csv(INPUT_PATH/'sample_submission.csv')
 submission['isup_grade'] = optimized_predictions.astype(int)
 submission.head(20 )<save_to_csv>
Dense, Dropout, Activation, Flatten, Input, BatchNormalization, GlobalAveragePooling2D, Add, Conv2D, AveragePooling2D,
LeakyReLU, Concatenate 
)
 def get_efficientnet(model, shape):
models_dict = {
'b0': efn.EfficientNetB0(input_shape=shape,weights=None,include_top=False),
'b1': efn.EfficientNetB1(input_shape=shape,weights=None,include_top=False),
'b2': efn.EfficientNetB2(input_shape=shape,weights=None,include_top=False),
'b3': efn.EfficientNetB3(input_shape=shape,weights=None,include_top=False),
'b4': efn.EfficientNetB4(input_shape=shape,weights=None,include_top=False),
'b5': efn.EfficientNetB5(input_shape=shape,weights=None,include_top=False),
'b6': efn.EfficientNetB6(input_shape=shape,weights=None,include_top=False),
'b7': efn.EfficientNetB7(input_shape=shape,weights=None,include_top=False)
 }
return models_dict[model]
def build_model(shape=(512, 512, 1), model_class=None):
inp = Input(shape=shape)
 base = get_efficientnet(model_class, shape)
 x = base(inp)
 x = GlobalAveragePooling2D()(x)
 inp2 = Input(shape=(4,))
x2 = tf.keras.layers.GaussianNoise(0.2 )(inp2)
 x = Concatenate()([x, x2])
 x = Dropout(Dropout_model )(x)
 x = Dense(1 )(x)
 model = Model([inp, inp2] , x)
 weights = [w for w in os.listdir('.. /input/osic-model-weights')if model_class in w][0]
model.load_weights('.. /input/osic-model-weights/' + weights)
 return model
model_classes = ['b5']
models = [build_model(shape=(512, 512, 1), model_class=m)for m in model_classes]
print('Number of models: ' + str(len(models)) )
if(INPUT_PATH/'test_images' ).exists() :
if submission['isup_grade'].sum() > 0:
submission.to_csv('submission.csv', index=False)
 else:
pass
else:
submission.to_csv('submission.csv', index=False )<import_modules>
tr_p, vl_p = train_test_split(P, shuffle=True, train_size = 0.8 )
import sys
import os
import glob
from tqdm.notebook import tqdm
import shutil
from pathlib import Path
from openslide import OpenSlide
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from skimage.measure import block_reduce
import cv2.cv2 as cv2
from sys import getsizeof
import collections
import pickle<set_options>
def score(fvc_true, fvc_pred, sigma):
sigma_clip = np.maximum(sigma, 70)
 delta = np.abs(fvc_true - fvc_pred)
 delta = np.minimum(delta, 1000)
 sq2 = np.sqrt(2)
 metric =(delta / sigma_clip)*sq2 + np.log(sigma_clip* sq2)
 return np.mean(metric )
physical_devices = tf.config.list_physical_devices('GPU')
 tf.config.experimental.set_memory_growth(physical_devices[0], True )<load_from_csv>
subs = []
for model in models:
metric = []
for q in tqdm(range(1, 10)) :
m = []
for p in vl_p:
x = []
tab = []
if p in ['ID00011637202177653955184', 'ID00052637202186188008618']:
continue
ldir = os.listdir(f'.. /input/osic-pulmonary-fibrosis-progression/train/{p}/')
 for i in ldir:
if int(i[:-4])/ len(ldir)< 0.8 and int(i[:-4])/ len(ldir)> 0.15:
x.append(get_img(f'.. /input/osic-pulmonary-fibrosis-progression/train/{p}/{i}'))
tab.append(get_tab(train.loc[train.Patient == p, :]))
if len(x)< 1:
continue
tab = np.array(tab)
 x = np.expand_dims(x, axis=-1)
 _a = model.predict([x, tab])
 a = np.quantile(_a, q / 10)
 percent_true = train.Percent.values[train.Patient == p]
fvc_true = train.FVC.values[train.Patient == p]
weeks_true = train.Weeks.values[train.Patient == p]
fvc = a *(weeks_true - weeks_true[0])+ fvc_true[0]
percent = percent_true[0] - a * abs(weeks_true - weeks_true[0])
 m.append(score(fvc_true, fvc, percent))
print(np.mean(m))
metric.append(np.mean(m))
q =(np.argmin(metric)+ 1)/ 10
sub = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/sample_submission.csv')
 test = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/test.csv')
 A_test, B_test, P_test,W, FVC= {}, {}, {},{},{}
STD, WEEK = {}, {}
for p in test.Patient.unique() :
x = []
tab = []
ldir = os.listdir(f'.. /input/osic-pulmonary-fibrosis-progression/test/{p}/')
 for i in ldir:
if int(i[:-4])/ len(ldir)< 0.8 and int(i[:-4])/ len(ldir)> 0.15:
x.append(get_img(f'.. /input/osic-pulmonary-fibrosis-progression/test/{p}/{i}'))
tab.append(get_tab(test.loc[test.Patient == p, :]))
if len(x)<= 1:
continue
tab = np.array(tab)
 x = np.expand_dims(x, axis=-1)
 _a = model.predict([x, tab])
 a = np.quantile(_a, q)
 A_test[p] = a
B_test[p] = test.FVC.values[test.Patient == p] - a*test.Weeks.values[test.Patient == p]
P_test[p] = test.Percent.values[test.Patient == p]
WEEK[p] = test.Weeks.values[test.Patient == p]
for k in sub.Patient_Week.values:
p, w = k.split('_')
 w = int(w)
 fvc = A_test[p] * w + B_test[p]
sub.loc[sub.Patient_Week == k, 'FVC'] = fvc
sub.loc[sub.Patient_Week == k, 'Confidence'] =(
 P_test[p] - A_test[p] * abs(WEEK[p] - w)
)
 _sub = sub[["Patient_Week","FVC","Confidence"]].copy()
subs.append(_sub )
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]
df.head()<normalization>
N = len(subs)
 sub = subs[0].copy()
sub["FVC"] = 0
sub["Confidence"] = 0
for i in range(N):
sub["FVC"] += subs[0]["FVC"] *(1/N)
 sub["Confidence"] += subs[0]["Confidence"] *(1/N )
def roll(a,
window,
dx=1,
dy=1):
shape =(
(( a.shape[0] - window[0])// dy + 1), 
(( a.shape[1] - window[1])// dx + 1),
*window, 
)
 strides =(
 a.strides[0] * dy,
a.strides[1] * dx,
a.strides[0],
a.strides[1], 
)
 return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
 def reduce(image, block, stride):
return roll(image,(block, block), stride, stride ).mean(( -1,-2))
def tile_filter(tile, provider):
if provider[:3] == 'rad':
if(( 1.0 - tile / 255 ).mean(2)< 0.1 ).astype(np.float ).mean() > 0.4:
return False
if(( tile / 255 ).mean(2)< 0.4 ).astype(np.float ).mean() > 0.2:
return False
else:
if(( 1.0 - tile / 255 ).mean(2)< 0.2 ).astype(np.float ).mean() > 0.3:
return False
return True<load_pretrained>
sub[["Patient_Week","FVC","Confidence"]].to_csv("submission_img.csv", index=False )
model = load_model('.. /input/tilenet/checkout_panda_v3.hdf5' )<categorify>
img_sub = sub[["Patient_Week","FVC","Confidence"]].copy()
output = [model.output, model.layers[-2].output]
K = output[1].shape[1:]
mmodel = Model(inputs=model.input, outputs = output)
 print(f'<choose_model_class>
ROOT = ".. /input/osic-pulmonary-fibrosis-progression"
BATCH_SIZE=128
tr = pd.read_csv(f"{ROOT}/train.csv")
 tr.drop_duplicates(keep=False, inplace=True, subset=['Patient','Weeks'])
 chunk = pd.read_csv(f"{ROOT}/test.csv")
 print("add infos")
 sub = pd.read_csv(f"{ROOT}/sample_submission.csv")
 sub['Patient'] = sub['Patient_Week'].apply(lambda x:x.split('_')[0])
 sub['Weeks'] = sub['Patient_Week'].apply(lambda x: int(x.split('_')[-1]))
sub = sub[['Patient','Weeks','Confidence','Patient_Week']]
sub = sub.merge(chunk.drop('Weeks', axis=1), on="Patient" )
class Conv2dBlock(nn.Module):
def __init__(self, in_channels: int, out_channels: int, layers: int = 2,
activation: Type[nn.Module] = nn.ReLU, kernel_size: int = 3,
batchnorm: bool = True):
super(Conv2dBlock, self ).__init__()
self.blocks = []
for i in range(layers):
block_layers = [
nn.Conv2d(( in_channels if i == 0 else out_channels),
out_channels, kernel_size, padding=kernel_size // 2),
activation()
]
if batchnorm:
block_layers.insert(1, nn.BatchNorm2d(out_channels))
block = nn.Sequential(*block_layers)
 self.blocks.append(block)
 self.main = nn.Sequential(*self.blocks)
 def forward(self, inputs: Tensor)-> Tensor:

 return self.main(inputs)
<categorify>
tr['WHERE'] = 'train'
chunk['WHERE'] = 'val'
sub['WHERE'] = 'test'
data = tr.append([chunk, sub] )
class PandaModel(nn.Module):
def __init__(self, n_channels: int, n_classes: Union[int, Tuple[int,...]]):
super().__init__()
self.encoder = nn.Sequential(
 Conv2dBlock(n_channels, 1024),
Conv2dBlock(1024, 512),
Conv2dBlock(512, 256),
Conv2dBlock(256, 128), 
)
 def make_head(n: int):
return nn.Sequential(
 nn.Linear(128, 32),
nn.ReLU() ,
nn.Linear(32, 16),
nn.ReLU() ,
nn.Linear(16, n)
)
 if isinstance(n_classes, Iterable):
self.heads = nn.ModuleList([make_head(n)for n in n_classes])
 else:
self.heads = nn.ModuleList([make_head(n_classes)])
 def forward(self, x):
d = self.encoder(x ).detach()
d = d.max(3 ).values.max(2 ).values.detach()
res = [h(d ).detach() for h in self.heads]
return res if len(res)> 1 else res[0]<load_pretrained>
print(tr.shape, chunk.shape, sub.shape, data.shape)
 print(tr.Patient.nunique() , chunk.Patient.nunique() , sub.Patient.nunique() ,
data.Patient.nunique() )
wsi_model = PandaModel(2048, 6)
 wsi_model.load_state_dict(torch.load('.. /input/wsinet/best_qp2_723.pth', map_location='cpu')['model'] )<compute_test_metric>
data['min_week'] = data['Weeks']
data.loc[data.WHERE=='test','min_week'] = np.nan
data['min_week'] = data.groupby('Patient')['min_week'].transform('min' )
def find_stain_index(reference, w):
dot_products = np.dot(reference, w)
 return np.argmax(np.abs(dot_products))<concatenate>
base = data.loc[data.Weeks == data.min_week]
base = base[['Patient','FVC']].copy()
base.columns = ['Patient','min_FVC']
base['nb'] = 1
base['nb'] = base.groupby('Patient')['nb'].transform('cumsum')
 base = base[base.nb==1]
base.drop('nb', axis=1, inplace=True )
def _reorder_stains(W, stains=None):
stains = ['hematoxylin', 'eosin'] if stains is None else stains
assert len(stains)== 2, "Only two-stain matrices are supported for now."
def _get_channel_order(W):
first = find_stain_index(stain_color_map[stains[0]], W)
 second = 1 - first
third = 2
return first, second, third
def _ordered_stack(mat, order):
return np.stack([mat[..., j] for j in order], -1)
 return _ordered_stack(W, _get_channel_order(W))<categorify>
data = data.merge(base, on='Patient', how='left')
 data['base_week'] = data['Weeks'] - data['min_week']
del base
def convert_image_to_matrix(im):
if im.ndim == 2:
return im
return im.reshape(( -1, im.shape[-1])).T
def convert_matrix_to_image(m, shape):
if len(shape)== 2:
return m
return m.T.reshape(shape[:-1] +(m.shape[0],))
def get_principal_components(m):
return numpy.linalg.svd(m.astype(float), full_matrices=False)[0].astype(m.dtype)
 def magnitude(m):
return numpy.sqrt(( m ** 2 ).sum(0))
def normalize(m):
return m / magnitude(m )<categorify>
COLS = ['Sex','SmokingStatus']
FE = []
for col in COLS:
for mod in data[col].unique() :
FE.append(mod)
 data[mod] =(data[col] == mod ).astype(int )
def rgb_to_sda(im_rgb, I_0, allow_negatives=False):
is_matrix = im_rgb.ndim == 2
if is_matrix:
im_rgb = im_rgb.T
if I_0 is None:
im_rgb = im_rgb.astype(float)+ 1
I_0 = 256
if not allow_negatives:
im_rgb = np.minimum(im_rgb, I_0)
 im_sda = -np.log(im_rgb/(1.*I_0)) * 255/np.log(I_0)
 return im_sda.T if is_matrix else im_sda
def sda_to_rgb(im_sda, I_0):
is_matrix = im_sda.ndim == 2
if is_matrix:
im_sda = im_sda.T
od = I_0 is None
if od:
I_0 = 256
im_rgb = I_0 **(1 - im_sda / 255.)
return(im_rgb.T if is_matrix else im_rgb)- od
def exclude_nonfinite(m):
return m[:, np.isfinite(m ).all(axis=0)]<prepare_x_and_y>
data['age'] =(data['Age'] - data['Age'].min())/(data['Age'].max() - data['Age'].min())
 data['BASE'] =(data['min_FVC'] - data['min_FVC'].min())/(data['min_FVC'].max() - data['min_FVC'].min())
 data['week'] =(data['base_week'] - data['base_week'].min())/(data['base_week'].max() - data['base_week'].min())
 data['percent'] =(data['Percent'] - data['Percent'].min())/(data['Percent'].max() - data['Percent'].min())
 FE += ['age','percent','week','BASE']
def color_convolution(im_stains, w, I_0=None):
m = convert_image_to_matrix(im_stains)
 sda_fwd = rgb_to_sda(m, 255 if I_0 is not None else None,allow_negatives=True)
 sda_conv = np.dot(w, sda_fwd)
 sda_inv = sda_to_rgb(sda_conv, I_0)
 im_rgb =(convert_matrix_to_image(sda_inv, im_stains.shape ).clip(0, 255 ).astype(np.uint8))
return im_rgb<concatenate>
tr = data.loc[data.WHERE=='train']
chunk = data.loc[data.WHERE=='val']
sub = data.loc[data.WHERE=='test']
del data
def complement_stain_matrix(w):
stain0 = w[:, 0]
stain1 = w[:, 1]
stain2 = np.cross(stain0, stain1)
 return np.array([stain0, stain1, stain2 / np.linalg.norm(stain2)] ).T<statistical_test>
C1, C2 = tf.constant(70, dtype='float32'), tf.constant(1000, dtype="float32")
 def score(y_true, y_pred):
tf.dtypes.cast(y_true, tf.float32)
 tf.dtypes.cast(y_pred, tf.float32)
 sigma = y_pred[:, 2] - y_pred[:, 0]
fvc_pred = y_pred[:, 1]
sigma_clip = tf.maximum(sigma, C1)
 delta = tf.abs(y_true[:, 0] - fvc_pred)
 delta = tf.minimum(delta, C2)
 sq2 = tf.sqrt(tf.dtypes.cast(2, dtype=tf.float32))
metric =(delta / sigma_clip)*sq2 + tf.math.log(sigma_clip* sq2)
 return K.mean(metric)
 def qloss(y_true, y_pred):
qs = [0.2, 0.50, 0.8]
q = tf.constant(np.array([qs]), dtype=tf.float32)
 e = y_true - y_pred
v = tf.maximum(q*e,(q-1)*e)
 return K.mean(v)
 def mloss(_lambda):
def loss(y_true, y_pred):
return _lambda * qloss(y_true, y_pred)+(1 - _lambda)*score(y_true, y_pred)
 return loss
def make_model(nh):
z = L.Input(( nh,), name="Patient")
 x = L.Dense(100, activation="relu", name="d1" )(z)
 x = L.Dense(100, activation="relu", name="d2" )(x)
 p1 = L.Dense(3, activation="linear", name="p1" )(x)
 p2 = L.Dense(3, activation="relu", name="p2" )(x)
 preds = L.Lambda(lambda x: x[0] + tf.cumsum(x[1], axis=1),
name="preds" )([p1, p2])
 model = M.Model(z, preds, name="CNN")
 model.compile(loss=mloss(0.65), optimizer=tf.keras.optimizers.Adam(lr=0.1, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.01, amsgrad=False), metrics=[score])
 return model
def rgb_separate_stains_macenko_pca(im_rgb, I_0, *args, **kwargs):
im_sda = rgb_to_sda(im_rgb, I_0)
 return separate_stains_macenko_pca(im_sda, *args, **kwargs )<concatenate>
y = tr['FVC'].values
z = tr[FE].values
ze = sub[FE].values
nh = z.shape[1]
pe = np.zeros(( ze.shape[0], 3))
pred = np.zeros(( z.shape[0], 3))
def separate_stains_macenko_pca(
 im_sda, minimum_magnitude=16, min_angle_percentile=0.01,
max_angle_percentile=0.99, mask_out=None):
m = convert_image_to_matrix(im_sda)
 if mask_out is not None:
keep_mask = numpy.equal(mask_out[..., None], False)
 keep_mask = numpy.tile(keep_mask,(1, 1, 3))
keep_mask = convert_image_to_matrix(keep_mask)
 m = m[:, keep_mask.all(axis=0)]
m = exclude_nonfinite(m)
 pcs = get_principal_components(m)
 proj = pcs.T[:-1].dot(m)
 filt = proj[:, magnitude(proj)> minimum_magnitude]
angles = _get_angles(filt)
 def get_percentile_vector(p):
return pcs[:, :-1].dot(filt[:, argpercentile(angles, p)])
 min_v = get_percentile_vector(min_angle_percentile)
 max_v = get_percentile_vector(max_angle_percentile)
 w = complement_stain_matrix(normalize(numpy.array([min_v, max_v] ).T))
return w
def _get_angles(m):
m = normalize(m)
 return(1 - m[1])* numpy.sign(m[0])
 def argpercentile(arr, p):

 i = int(p * arr.size + 0.5)
 return numpy.argpartition(arr, i)[i]<categorify>
NFOLD = 5
kf = KFold(n_splits=NFOLD )
stain_unmixing_routine_params = {
'stains': ['hematoxylin', 'eosin'],
'stain_unmixing_method': 'macenko_pca',
}
def dbn(im_src, W_source=None, W_target=None, im_target=None,stains=None, mask_out=None, stain_unmixing_routine_params=None):
stains = ['hematoxylin', 'eosin'] if stains is None else stains
stain_unmixing_routine_params =(
 {} if stain_unmixing_routine_params is None else
stain_unmixing_routine_params)
 for k in ['W_source', 'mask_out']:
assert k not in stain_unmixing_routine_params.keys() , \
"%s must be provided as a separate parameter." % k
stain_unmixing_routine_params['stains'] = stains
_, StainsFloat, _ = color_deconvolution_routine(
 im_src, W_source=W_source, mask_out=mask_out,
**stain_unmixing_routine_params)
 if all(j is None for j in [W_target, im_target]):
W_target = np.array(
 [stain_color_map[stains[0]], stain_color_map[stains[1]]] ).T
W_target = complement_stain_matrix(W_target)
 elif im_target is not None:
W_target = stain_unmixing_routine(
 im_target, **stain_unmixing_routine_params)
 im_src_normalized = color_convolution(StainsFloat, W_target)
 if mask_out is not None:
keep_mask = np.not_equal(mask_out, True)
 for i in range(3):
original = im_src[:, :, i].copy()
new = im_src_normalized[:, :, i].copy()
original[keep_mask] = 0
new[mask_out] = 0
im_src_normalized[:, :, i] = new + original
return im_src_normalized<define_variables>
%%time
cnt = 0
EPOCHS = 855
for tr_idx, val_idx in kf.split(z):
cnt += 1
print(f"FOLD {cnt}")
 net = make_model(nh)
 net.fit(z[tr_idx], y[tr_idx], batch_size=BATCH_SIZE, epochs=EPOCHS,
validation_data=(z[val_idx], y[val_idx]), verbose=0)
 print("train", net.evaluate(z[tr_idx], y[tr_idx], verbose=0, batch_size=BATCH_SIZE))
print("val", net.evaluate(z[val_idx], y[val_idx], verbose=0, batch_size=BATCH_SIZE))
print("predict val...")
 pred[val_idx] = net.predict(z[val_idx], batch_size=BATCH_SIZE, verbose=0)
 print("predict test...")
 pe += net.predict(ze, batch_size=BATCH_SIZE, verbose=0)/ NFOLD
LEVEL = 1
TILE_SIZE = 256
res_isup = np.zeros(len(df), dtype=np.int )<save_to_csv>
sigma_opt = mean_absolute_error(y, pred[:, 1])
 unc = pred[:,2] - pred[:, 0]
sigma_mean = np.mean(unc)
 print(sigma_opt, sigma_mean )
df['isup_grade'] = np.array(res_isup, dtype=np.int)
 df[['image_id', 'isup_grade']].to_csv('submission.csv', index=False)
 print(df.head())
 print()
print(df.isup_grade.value_counts() )<install_modules>
sub['FVC1'] = 1.*pe[:, 1]
sub['Confidence1'] = pe[:, 2] - pe[:, 0]
subm = sub[['Patient_Week','FVC','Confidence','FVC1','Confidence1']].copy()
subm.loc[~subm.FVC1.isnull() ].head(10 )
!pip install.. /input/spamspip/spams-2.6.1<install_modules>
subm.loc[~subm.FVC1.isnull() ,'FVC'] = subm.loc[~subm.FVC1.isnull() ,'FVC1']
if sigma_mean<70:
subm['Confidence'] = sigma_opt
else:
subm.loc[~subm.FVC1.isnull() ,'Confidence'] = subm.loc[~subm.FVC1.isnull() ,'Confidence1']
!pip install.. /input/imagecodecs/imagecodecs-2020.5.30-cp37-cp37m-manylinux2010_x86_64.whl<import_modules>
otest = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/test.csv')
 for i in range(len(otest)) :
subm.loc[subm['Patient_Week']==otest.Patient[i]+'_'+str(otest.Weeks[i]), 'FVC'] = otest.FVC[i]
subm.loc[subm['Patient_Week']==otest.Patient[i]+'_'+str(otest.Weeks[i]), 'Confidence'] = 0.1
import numpy as np
import pandas as pd
import os 
<define_variables>
subm[["Patient_Week","FVC","Confidence"]].to_csv("submission_regression.csv", index=False )
DEBUG = False<define_variables>
reg_sub = subm[["Patient_Week","FVC","Confidence"]].copy()
sys.path = [
'.. /input/efficientnet/EfficientNet-PyTorch-master/',
'.. /input/staintoolszip/',
] + sys.path<import_modules>
df1 = img_sub.sort_values(by=['Patient_Week'], ascending=True ).reset_index(drop=True)
 df2 = reg_sub.sort_values(by=['Patient_Week'], ascending=True ).reset_index(drop=True )
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
import cv2<import_modules>
df = df1[['Patient_Week']].copy()
df['FVC'] = FVC_weight*df1['FVC'] +(1-FVC_weight)*df2['FVC']
df['Confidence'] = Confidence_weight*df1['Confidence'] +(1-Confidence_weight)*df2['Confidence']
df.head()
<load_from_csv><EOS> 
df.to_csv('submission.csv', index=False )