Kaggle competition: Prostate cANcer graDe Assessment (PANDA) Challenge
<SOS> metric: QuadraticWeightedKappa Kaggle data source: prostate-cancer-grade-assessment-(panda)-challenge<categorify>
!pip install /kaggle/input/pretrainedmodels
!pip install /kaggle/input/pytorchimagemodels120
!pip install /kaggle/input/efficientnet
!pip install /kaggle/input/pytorch-lightning-081-whl/pytorch_lightning-0.8.1-py3-none-any.whl
!pip install /kaggle/input/warmup
!pip install /kaggle/input/albumentations
!pip install /kaggle/input/imagecodecs/imagecodecs-2020.5.30-cp37-cp37m-manylinux2014_x86_64.whl
!pip install /kaggle/input/tifffile/tifffile-2020.6.3-py3-none-any.whl
!pip install /kaggle/input/panda-code-2
!mkdir -p /root/.cache/torch/checkpoints/
!cp /kaggle/input/efficientnetb0355c32eb/efficientnet-b0-355c32eb.pth /root/.cache/torch/checkpoints
!cp /kaggle/input/my-efficientnetb008094119/my_efficientnet-b0-08094119.pth /root/.cache/torch/checkpoints
<normalization><EOS> 
def load_models(exp_names):
models = []
for exp_name in exp_names:
model_dir = Path(".. /input/panda-code-2/trained_models")/ exp_name
nr_checkpoints = 1
model_path = get_ckpt_path_from_dir(model_dir, nr=nr_checkpoints)
 model_pathes = model_path if type(model_path)is list else [model_path]
for model_path in model_pathes:
print(f"model_path: {model_path}")
 model = ClassificationModel.load_from_checkpoint(str(model_path))
model.eval()
model.to(device)
 models.append(model)
 return models
exp_names_256 = [f"1um_bhM_reg2_ep25_mfRP_RgIcBl_ts256_se2222_f{f}" for f in range(5)]
exp_names_512 = [f"1um_bhM_cm20_mrK_reg2_RgIcBl_f{f}" for f in range(5)]
data_dir = Path('.. /input/prostate-cancer-grade-assessment')
 df_train = pd.read_csv(data_dir / 'train.csv')
 df_test = pd.read_csv(data_dir / 'test.csv')
 image_folder = data_dir / 'test_images'
is_test = os.path.exists(image_folder)
 image_folder = image_folder if is_test else data_dir / 'train_images'
df = df_test if is_test else df_train.loc[:5]
print(f"is_test: {is_test}")
 input_type = "tiff_1um"
batch_size = 2
num_workers = 2
tta_nr = 1
tile_mode = 0
head_type = "normal"
device = torch.device('cuda')
 models_256 = load_models(exp_names_256)
 models_512 = load_models(exp_names_512)
 normalize = False
if tta_nr > 1:
tfs_tile_train, tfs_all_train, _, _ = get_transforms_classification(False, False, False,
False, False, False,
prob=0.5, intensity="mid", normalize=normalize)
 else:
tfs_tile_train, tfs_all_train = None, None
dataset_256 = TileDataset(image_folder, df, 256, 256, 144, tile_mode=tile_mode,
transform_tile=None, transform_all=None, invert=False,
inference=True, input_type=input_type, overwrite_image_folder=False, tta_nr=tta_nr,
normalize=normalize)
 loader_256 = DataLoaderPytorch(dataset_256, batch_size=batch_size, num_workers=num_workers, shuffle=False)
 dataset_512 = TileDataset(image_folder, df, 512, 512, 36, tile_mode=tile_mode,
transform_tile=None, transform_all=None, invert=False,
inference=True, input_type=input_type, overwrite_image_folder=False, tta_nr=tta_nr,
normalize=normalize)
 loader_512 = DataLoaderPytorch(dataset_512, batch_size=batch_size, num_workers=num_workers, shuffle=False)
 logits = []
with torch.no_grad() :
for idx, data_both in enumerate(zip(loader_256, loader_512)) :
data_256 = data_both[0]
data_512 = data_both[1]
logits_batch_ens = []
for model in models_256:
data = data_256
for tta_idx in range(tta_nr):
if tta_nr > 1:
data_tta = data["data"][tta_idx].to(device)
 else:
data_tta = data["data"].to(device)
 data_raw = model(data_tta)
 if head_type == "hydra":
data_raw = data_raw[0]
logits_batch = data_raw.sigmoid().cpu().numpy()
logits_batch_ens.append(logits_batch)
 for model in models_512:
data = data_512
for tta_idx in range(tta_nr):
if tta_nr > 1:
data_tta = data["data"][tta_idx].to(device)
 else:
data_tta = data["data"].to(device)
 data_raw = model(data_tta)
 if head_type == "hydra":
data_raw = data_raw[0]
logits_batch = data_raw.sigmoid().cpu().numpy()
logits_batch_ens.append(logits_batch)
 logits_batch_ens = np.array(logits_batch_ens)
 logits_batch_ens = logits_batch_ens.mean(axis=0)
 logits.append(logits_batch_ens)
 logits = np.concatenate(logits, axis=0)
 preds = logits.sum(axis=1 ).round()
df['isup_grade'] = preds.astype(int)
 df[['image_id', 'isup_grade']].to_csv('submission.csv', index=False)
 print(df.head())
 print()
print(df.isup_grade.value_counts())