Kaggle competition: Global Wheat Detection
<SOS> metric: custom metric Kaggle data source: global-wheat-detection<install_modules>
import logging
import os
import re
import gc
import json
from tqdm.auto import tqdm
import numpy as np
import pandas as pd
import cv2
import matplotlib.pyplot as plt
package_path = '.. /input/efficientnet-pytorch-07/efficientnet_pytorch-0.7.0'
!cp.. /input/gdcm-conda-install/gdcm.tar.
 !tar -xvzf gdcm.tar
!conda install --offline./gdcm/gdcm-2.8.9-py37h71b2a6d_0.tar.bz2<define_variables>
!pip install.. /input/pytorch-16/torch-1.6.0cu101-cp37-cp37m-linux_x86_64.whl
CFG = {
'train': False,
'train_img_path': '.. /input/rsna-str-pulmonary-embolism-detection/train',
'test_img_path': '.. /input/rsna-str-pulmonary-embolism-detection/test',
'cv_fold_path': '.. /input/stratified-validation-strategy/rsna_train_splits_fold_20.csv',
'train_path': '.. /input/rsna-str-pulmonary-embolism-detection/train.csv',
'test_path': '.. /input/rsna-str-pulmonary-embolism-detection/test.csv',
'image_target_cols': [
'pe_present_on_image',
],
'exam_target_cols': [
'negative_exam_for_pe',
'rv_lv_ratio_gte_1',
'rv_lv_ratio_lt_1',
'leftsided_pe',
'chronic_pe',
'rightsided_pe',
'acute_and_chronic_pe',
'central_pe',
'indeterminate'
],
'img_num': 200,
'img_size': 256,
'lr': 0.0005,
'epochs': 2,
'device': 'cuda',
'train_bs': 2,
'accum_iter': 8,
'verbose_step': 1,
'num_workers': 4,
'efbnet': 'efficientnet-b0',
'train_folds': [np.arange(0,16),
np.concatenate([np.arange(0,12), np.arange(16,20)]),
np.concatenate([np.arange(0,8), np.arange(12,20)]),
np.concatenate([np.arange(0,4), np.arange(8,20)]),
np.arange(4,20),
],
'valid_folds': [np.arange(16,20),
np.arange(12,16),
np.arange(8,12),
np.arange(4,8),
np.arange(0,4)
 ],
'model_path': '.. /input/kh-rsna-model',
'tag': 'efb0_stage2_multilabel'
}
SEED = 42321
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
def window(img, WL=50, WW=350):
upper, lower = WL+WW//2, WL-WW//2
X = np.clip(img.copy() , lower, upper)
 X = X - np.min(X)
 X = X / np.max(X)
 return X
def get_img(path):
d = pydicom.read_file(path)


 img =(d.pixel_array * d.RescaleSlope)+ d.RescaleIntercept
r = window(img, -600, 1500)
 g = window(img, 100, 700)
 b = window(img, 40, 400)
 res = np.concatenate([r[:, :, np.newaxis],
g[:, :, np.newaxis],
b[:, :, np.newaxis]], axis=-1)
 res = zoom(res, [CFG['img_size']/res.shape[0], CFG['img_size']/res.shape[1], 1.], prefilter=False, order=1)
 return res
class RSNADatasetStage1(Dataset):
def __init__(
 self, df, label_smoothing, data_root,
image_subsampling=True, transforms=None, output_label=True 
):
super().__init__()
self.df = df.reset_index(drop=True ).copy()
self.label_smoothing = label_smoothing
self.transforms = transforms
self.data_root = data_root
self.output_label = output_label
def __len__(self):
return self.df.shape[0]
def __getitem__(self, index: int):
if self.output_label:
target = self.df[CFG['image_target_cols']].values[index]
path = "{}/{}/{}/{}.dcm".format(self.data_root,
self.df.iloc[index]['StudyInstanceUID'],
self.df.iloc[index]['SeriesInstanceUID'],
self.df.iloc[index]['SOPInstanceUID'])
 img = get_img(path)
 if self.transforms:
img = self.transforms(image=img)['image']
if self.output_label == True:
target = np.clip(target, self.label_smoothing, 1 - self.label_smoothing)
 return img, target
else:
return img
class RSNADataset(Dataset):
def __init__(
 self, df, label_smoothing, data_root,
image_subsampling=True, transforms=None, output_label=True 
):
super().__init__()
self.df = df
self.patients = self.df['StudyInstanceUID'].unique()
self.image_subsampling = image_subsampling
self.label_smoothing = label_smoothing
self.transforms = transforms
self.data_root = data_root
self.output_label = output_label
def get_patients(self):
return self.patients
def __len__(self):
return len(self.patients)
 def __getitem__(self, index: int):
patient = self.patients[index]
df_ = self.df.loc[self.df.StudyInstanceUID == patient]
per_image_feats = get_stage1_columns()
if self.image_subsampling:
img_num = min(CFG['img_num'], df_.shape[0])
 img_ix = np.random.choice(np.arange(df_.shape[0]), replace=False, size=img_num)
 imgs = np.zeros(( CFG['img_num'],), np.float32)
 per_image_preds = np.zeros(( CFG['img_num'], len(per_image_feats)) , np.float32)
 locs = np.zeros(( CFG['img_num'],), np.float32)
 image_masks = np.zeros(( CFG['img_num'],), np.float32)
 image_masks[:img_num] = 1.
 if self.output_label:
exam_label = df_[CFG['exam_target_cols']].values[0]
image_labels = np.zeros(( CFG['img_num'], len(CFG['image_target_cols'])) , np.float32)
 else:
img_num = df_.shape[0]
img_ix = np.arange(df_.shape[0])
 imgs = np.zeros(( img_num,), np.float32)
 per_image_preds = np.zeros(( img_num, len(per_image_feats)) , np.float32)
 locs = np.zeros(( img_num,), np.float32)
 image_masks = np.zeros(( img_num,), np.float32)
 image_masks[:img_num] = 1.
 if self.output_label:
exam_label = df_[CFG['exam_target_cols']].values[0]
image_labels = np.zeros(( img_num, len(CFG['image_target_cols'])) , np.float32)
 for i, im_ix in enumerate(img_ix):
path = "{}/{}/{}/{}.dcm".format(self.data_root,
df_['StudyInstanceUID'].values[im_ix],
df_['SeriesInstanceUID'].values[im_ix],
df_['SOPInstanceUID'].values[im_ix])
 d = pydicom.read_file(path)
 locs[i] = d.ImagePositionPatient[2]
per_image_preds[i,:] = df_[per_image_feats].values[im_ix,:]
if self.output_label == True:
image_labels[i] = df_[CFG['image_target_cols']].values[im_ix]
seq_ix = np.argsort(locs)

locs = locs[seq_ix]
locs[1:img_num] = locs[1:img_num]-locs[0:img_num-1]
locs[0] = 0
per_image_preds = per_image_preds[seq_ix]
if self.output_label == True:
image_labels = image_labels[seq_ix]
image_labels = np.clip(image_labels, self.label_smoothing, 1 - self.label_smoothing)
 exam_label = np.clip(exam_label, self.label_smoothing, 1 - self.label_smoothing)
 return imgs, per_image_preds, locs, image_labels, exam_label, image_masks
else:
return imgs, per_image_preds, locs, img_num, index, seq_ix
HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout 
)
 def get_train_transforms() :
return Compose([
ToTensorV2(p=1.0),
], p=1.)

 def get_valid_transforms() :
return Compose([
ToTensorV2(p=1.0),
], p=1.)

 class RNSAImageFeatureExtractor(nn.Module):
def __init__(self):
super().__init__()
self.cnn_model = EfficientNet.from_name(CFG['efbnet'])
 self.pooling = nn.AdaptiveAvgPool2d(1)
 def get_dim(self):
return self.cnn_model._fc.in_features
def forward(self, x):
feats = self.cnn_model.extract_features(x)
 return self.pooling(feats ).view(x.shape[0], -1)
 class RSNAImgClassifierSingle(nn.Module):
def __init__(self):
super().__init__()
self.cnn_model = RNSAImageFeatureExtractor()
self.image_predictors = nn.Linear(self.cnn_model.get_dim() , 1)
 def forward(self, imgs):
imgs_embdes = self.cnn_model(imgs)
 image_preds = self.image_predictors(imgs_embdes)
 return image_preds
class RSNAImgClassifier(nn.Module):
def __init__(self):
super().__init__()
self.cnn_model = RNSAImageFeatureExtractor()
self.image_predictors = nn.Linear(self.cnn_model.get_dim() , 9)
 def forward(self, imgs):
imgs_embdes = self.cnn_model(imgs)
 image_preds = self.image_predictors(imgs_embdes)
 return image_preds
class TimeDistributed(nn.Module):
def __init__(self, module, batch_first=True):
super(TimeDistributed, self ).__init__()
self.module = module
self.batch_first = batch_first
def forward(self, x):

 x_size= x.size()
c_in = x.contiguous().view(x_size[0] * x_size[1], *x_size[2:])
 c_out = self.module(c_in)
 r_in = c_out.view(x_size[0], x_size[1], -1)
 if self.batch_first is False:
r_in = r_in.permute(1, 0, 2)
 return r_in
class RSNAClassifier(nn.Module):
def __init__(self, hidden_size=64):
super().__init__()
self.gru = nn.GRU(len(get_stage1_columns())+1, hidden_size, bidirectional=True, batch_first=True, num_layers=2)
 self.image_predictors = TimeDistributed(nn.Linear(hidden_size*2, 1))
self.exam_predictor = nn.Linear(hidden_size*2*2, 9)
 def forward(self, img_preds, locs):
embeds = torch.cat([img_preds, locs.view(locs.shape[0], locs.shape[1], 1)], dim=2)
 embeds, _ = self.gru(embeds)
 image_preds = self.image_predictors(embeds)
 avg_pool = torch.mean(embeds, 1)
 max_pool, _ = torch.max(embeds, 1)
 conc = torch.cat([avg_pool, max_pool], 1)
 exam_pred = self.exam_predictor(conc)
 return image_preds, exam_pred
def rsna_wloss_inference(y_true_img, y_true_exam, y_pred_img, y_pred_exam, chunk_sizes):

 y_true_img, y_true_exam, y_pred_img, y_pred_exam = torch.tensor(y_true_img, dtype=torch.float32), torch.tensor(y_true_exam, dtype=torch.float32), torch.tensor(y_pred_img, dtype=torch.float32), torch.tensor(y_pred_exam, dtype=torch.float32)
 y_true_img_chunks, y_true_exam_chunks, y_pred_img_chunks, y_pred_exam_chunks = torch.split(y_true_img, chunk_sizes, dim=0), torch.split(y_true_exam, chunk_sizes, dim=0), torch.split(y_pred_img, chunk_sizes, dim=0), torch.split(y_pred_exam, chunk_sizes, dim=0)
 label_w = torch.tensor([0.0736196319, 0.2346625767, 0.0782208589, 0.06257668712, 0.1042944785, 0.06257668712, 0.1042944785, 0.1877300613, 0.09202453988] ).view(1, -1)
 img_w = 0.07361963
bce_func = torch.nn.BCELoss(reduction='none')
 total_loss = torch.tensor(0, dtype=torch.float32)
 total_weights = torch.tensor(0, dtype=torch.float32)
 for i,(y_true_img_, y_true_exam_, y_pred_img_, y_pred_exam_)in enumerate(zip(y_true_img_chunks, y_true_exam_chunks, y_pred_img_chunks, y_pred_exam_chunks)) :
exam_loss = bce_func(y_pred_exam_[0, :], y_true_exam_[0, :])
 exam_loss = torch.sum(exam_loss*label_w, 1)[0]
image_loss = bce_func(y_pred_img_, y_true_img_)
 img_num = chunk_sizes[i]
qi = torch.sum(y_true_img_)/img_num
image_loss = torch.sum(img_w*qi*image_loss)
 total_loss += exam_loss+image_loss
total_weights += label_w.sum() + img_w*qi*img_num
final_loss = total_loss/total_weights
return final_loss
def rsna_wloss_train(y_true_img, y_true_exam, y_pred_img, y_pred_exam, image_masks, device):
label_w = torch.tensor([0.0736196319, 0.2346625767, 0.0782208589, 0.06257668712, 0.1042944785, 0.06257668712, 0.1042944785, 0.1877300613, 0.09202453988] ).view(1, -1 ).to(device)
 img_w = 0.07361963
bce_func = torch.nn.BCEWithLogitsLoss(reduction='none' ).to(device)
 total_loss = torch.tensor(0, dtype=torch.float32 ).to(device)
 total_weights = torch.tensor(0, dtype=torch.float32 ).to(device)
 for i in range(y_true_img.shape[0]):
exam_loss = bce_func(y_pred_exam[i, :], y_true_exam[i, :])
 exam_loss = torch.sum(exam_loss*label_w, 1)[0]
img_mask = image_masks[i]
image_loss = bce_func(y_pred_img[i,:], y_true_img[i,:] ).flatten()
image_loss = image_loss*img_mask
img_num = torch.sum(img_mask)
 qi = torch.sum(y_true_img[i,:])/img_num
image_loss = torch.sum(img_w*qi*image_loss)
 total_loss += exam_loss+image_loss
total_weights += label_w.sum() + img_w*qi*img_num
final_loss = total_loss/total_weights
return final_loss, total_loss, total_weights
def rsna_wloss_valid(y_true_img, y_true_exam, y_pred_img, y_pred_exam, image_masks, device):
label_w = torch.tensor([0.0736196319, 0.2346625767, 0.0782208589, 0.06257668712, 0.1042944785, 0.06257668712, 0.1042944785, 0.1877300613, 0.09202453988] ).view(1, -1 ).to(device)
 img_w = 0.07361963
bce_func = torch.nn.BCEWithLogitsLoss(reduction='none' ).to(device)
 total_loss = torch.tensor(0, dtype=torch.float32 ).to(device)
 total_weights = torch.tensor(0, dtype=torch.float32 ).to(device)
 for i in range(y_true_img.shape[0]):
exam_loss = bce_func(y_pred_exam[i, :], y_true_exam[i, :])
 exam_loss = torch.sum(exam_loss*label_w, 1)[0]
img_mask = image_masks[i]
image_loss = bce_func(y_pred_img[i,:], y_true_img[i,:] ).flatten()
image_loss = image_loss*img_mask
img_num = torch.sum(img_mask)
 qi = torch.sum(y_true_img[i,:])/img_num
image_loss = torch.sum(img_w*qi*image_loss)
 total_loss += exam_loss+image_loss
total_weights += label_w.sum() + img_w*qi*img_num
final_loss = total_loss/total_weights
return final_loss, total_loss, total_weights
def prepare_train_dataloader(train, cv_df, train_fold, valid_fold):
train_patients = cv_df.loc[cv_df.fold.isin(train_fold), 'StudyInstanceUID'].unique()
valid_patients = cv_df.loc[cv_df.fold.isin(valid_fold), 'StudyInstanceUID'].unique()
train_ = train.loc[train.StudyInstanceUID.isin(train_patients),:].reset_index(drop=True)
 valid_ = train.loc[train.StudyInstanceUID.isin(valid_patients),:].reset_index(drop=True)
 train_ds = RSNADataset(train_, 0.0, CFG['train_img_path'], image_subsampling=True, transforms=get_train_transforms() , output_label=True)
 valid_ds = RSNADataset(valid_, 0.0, CFG['train_img_path'], image_subsampling=False, transforms=get_valid_transforms() , output_label=True)
 train_loader = torch.utils.data.DataLoader(
 train_ds,
batch_size=CFG['train_bs'],
pin_memory=True,
drop_last=False,
shuffle=True,
num_workers=CFG['num_workers'], 
)
 val_loader = torch.utils.data.DataLoader(
 valid_ds,
batch_size=1,
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=True, 
)
 return train_loader, val_loader
def train_one_epoch(epoch, model, device, scaler, optimizer, train_loader):
model.train()
t = time.time()
loss_sum = 0
loss_w_sum = 0
for step,(imgs, per_image_preds, locs, image_labels, exam_label, image_masks)in enumerate(train_loader):
imgs = imgs.to(device ).float()
per_image_preds = per_image_preds.to(device ).float()
locs = locs.to(device ).float()
image_masks = image_masks.to(device ).float()
image_labels = image_labels.to(device ).float()
exam_label = exam_label.to(device ).float()
with autocast() :
image_preds, exam_pred = model(per_image_preds, locs)
 loss, total_loss, total_weights = rsna_wloss_train(image_labels, exam_label, image_preds, exam_pred, image_masks, device)
 scaler.scale(loss ).backward()
loss_sum += total_loss.detach().item()
loss_w_sum += total_weights.detach().item()
if(( step + 1)% CFG['accum_iter'] == 0)or(( step + 1)== len(train_loader)) :
scaler.step(optimizer)
 scaler.update()
optimizer.zero_grad()
if(( step + 1)% CFG['verbose_step'] == 0)or(( step + 1)== len(train_loader)) :
print(
 f'epoch {epoch} train step {step+1}/{len(train_loader)}, ' + \
f'loss: {loss_sum/loss_w_sum:.4f}, ' + \
f'time: {(time.time() - t):.4f}', end= '\r' if(step + 1)!= len(train_loader)else '
' 
)
 def post_process(exam_pred, image_pred):
rv_lv_ratio_lt_1_ix = CFG['exam_target_cols'].index('rv_lv_ratio_lt_1')
 rv_lv_ratio_gte_1_ix = CFG['exam_target_cols'].index('rv_lv_ratio_gte_1')
 central_pe_ix = CFG['exam_target_cols'].index('central_pe')
 rightsided_pe_ix = CFG['exam_target_cols'].index('rightsided_pe')
 leftsided_pe_ix = CFG['exam_target_cols'].index('leftsided_pe')
 acute_and_chronic_pe_ix = CFG['exam_target_cols'].index('acute_and_chronic_pe')
 chronic_pe_ix = CFG['exam_target_cols'].index('chronic_pe')
 negative_exam_for_pe_ix = CFG['exam_target_cols'].index('negative_exam_for_pe')
 indeterminate_ix = CFG['exam_target_cols'].index('indeterminate')
 has_pe_image = torch.max(image_pred, 1)[0][0] > 0
rv_lv_ratios = exam_pred[:, [rv_lv_ratio_lt_1_ix, rv_lv_ratio_gte_1_ix]]
rv_lv_ratios_1_a = nn.functional.softmax(rv_lv_ratios, dim=1)
 rv_lv_ratios_1_a = torch.log(rv_lv_ratios_1_a/(1-rv_lv_ratios_1_a))
exam_pred[:, [rv_lv_ratio_lt_1_ix, rv_lv_ratio_gte_1_ix]] = torch.where(has_pe_image, rv_lv_ratios_1_a, rv_lv_ratios)
 crl_pe = exam_pred[:, [central_pe_ix, rightsided_pe_ix, leftsided_pe_ix]]
has_no_pe = torch.max(crl_pe ,1)[0] <= 0
max_val = torch.max(crl_pe, 1)[0]
crl_pe_1_b = torch.where(crl_pe==max_val, 0.0001-crl_pe+crl_pe, crl_pe)
 exam_pred[:, [central_pe_ix, rightsided_pe_ix, leftsided_pe_ix]] = torch.where(has_pe_image*has_no_pe, crl_pe_1_b, crl_pe)
 ac_pe = exam_pred[:, [acute_and_chronic_pe_ix, chronic_pe_ix]]
both_ac_ch = torch.min(ac_pe ,1)[0] > 0
ac_pe_1_c = nn.functional.softmax(ac_pe, dim=1)
 ac_pe_1_c = torch.log(ac_pe_1_c/(1-ac_pe_1_c))
exam_pred[:, [acute_and_chronic_pe_ix, chronic_pe_ix]] = torch.where(has_pe_image*both_ac_ch, ac_pe_1_c, ac_pe)
 neg_ind = exam_pred[:, [negative_exam_for_pe_ix, indeterminate_ix]]
neg_ind_1d = torch.clamp(neg_ind, max=0)
 exam_pred[:, [negative_exam_for_pe_ix, indeterminate_ix]] = torch.where(has_pe_image, neg_ind_1d, neg_ind)
 ne_inde = exam_pred[:, [negative_exam_for_pe_ix, indeterminate_ix]]
ne_inde_2_a = nn.functional.softmax(ne_inde, dim=1)
 ne_inde_2_a = torch.log(ne_inde_2_a/(1-ne_inde_2_a))
exam_pred[:, [negative_exam_for_pe_ix, indeterminate_ix]] = torch.where(~has_pe_image, ne_inde_2_a, ne_inde)
 all_other_exam_labels = exam_pred[:, [rv_lv_ratio_lt_1_ix, rv_lv_ratio_gte_1_ix,
central_pe_ix, rightsided_pe_ix, leftsided_pe_ix,
acute_and_chronic_pe_ix, chronic_pe_ix]]
all_other_exam_labels_2_b = torch.clamp(all_other_exam_labels, max=0)
 exam_pred[:, [rv_lv_ratio_lt_1_ix, rv_lv_ratio_gte_1_ix,
central_pe_ix, rightsided_pe_ix, leftsided_pe_ix,
acute_and_chronic_pe_ix, chronic_pe_ix]] = torch.where(~has_pe_image, all_other_exam_labels_2_b, all_other_exam_labels)
 return exam_pred, image_pred
def valid_one_epoch(epoch, model, device, scheduler, val_loader, schd_loss_update=False):
model.eval()
t = time.time()
loss_sum = 0
loss_w_sum = 0
for step,(imgs, per_image_preds, locs, image_labels, exam_label, image_masks)in enumerate(val_loader):
imgs = imgs.to(device ).float()
per_image_preds = per_image_preds.to(device ).float()
locs = locs.to(device ).float()
image_masks = image_masks.to(device ).float()
image_labels = image_labels.to(device ).float()
exam_label = exam_label.to(device ).float()
image_preds, exam_pred = model(per_image_preds, locs)
 exam_pred, image_preds= post_process(exam_pred, image_preds)
 loss, total_loss, total_weights = rsna_wloss_valid(image_labels, exam_label, image_preds, exam_pred, image_masks, device)
 loss_sum += total_loss.detach().item()
loss_w_sum += total_weights.detach().item()
if(( step + 1)% CFG['verbose_step'] == 0)or(( step + 1)== len(val_loader)) :
print(
 f'epoch {epoch} valid Step {step+1}/{len(val_loader)}, ' + \
f'loss: {loss_sum/loss_w_sum:.4f}, ' + \
f'time: {(time.time() - t):.4f}', end='\r' if(step + 1)!= len(val_loader)else '
' 
)
 if schd_loss_update:
scheduler.step(loss_sum/loss_w_sum)
 else:
scheduler.step()
def check_label_consistency(checking_df):
df = checking_df.copy()
print(df.shape)
 df['positive_images_in_exam'] = df['StudyInstanceUID'].map(df.groupby(['StudyInstanceUID'] ).pe_present_on_image.max())
 df_pos = df.loc[df.positive_images_in_exam > 0.5]
df_neg = df.loc[df.positive_images_in_exam <= 0.5]
rule1a = df_pos.loc[(( df_pos.rv_lv_ratio_lt_1 > 0.5)& 
(df_pos.rv_lv_ratio_gte_1 > 0.5)) | 
(( df_pos.rv_lv_ratio_lt_1 <= 0.5)& 
(df_pos.rv_lv_ratio_gte_1 <= 0.5)) ].reset_index(drop = True)
 rule1a['broken_rule'] = '1a'
rule1b = df_pos.loc[(df_pos.central_pe <= 0.5)& 
(df_pos.rightsided_pe <= 0.5)& 
(df_pos.leftsided_pe <= 0.5)].reset_index(drop = True)
 rule1b['broken_rule'] = '1b'
rule1c = df_pos.loc[(df_pos.acute_and_chronic_pe > 0.5)& 
(df_pos.chronic_pe > 0.5)].reset_index(drop = True)
 rule1c['broken_rule'] = '1c'
rule1d = df_pos.loc[(df_pos.indeterminate > 0.5)| 
(df_pos.negative_exam_for_pe > 0.5)].reset_index(drop = True)
 rule1d['broken_rule'] = '1d'
rule2a = df_neg.loc[(( df_neg.indeterminate > 0.5)& 
(df_neg.negative_exam_for_pe > 0.5)) | 
(( df_neg.indeterminate <= 0.5)& 
(df_neg.negative_exam_for_pe <= 0.5)) ].reset_index(drop = True)
 rule2a['broken_rule'] = '2a'
rule2b = df_neg.loc[(df_neg.rv_lv_ratio_lt_1 > 0.5)| 
(df_neg.rv_lv_ratio_gte_1 > 0.5)| 
(df_neg.central_pe > 0.5)| 
(df_neg.rightsided_pe > 0.5)| 
(df_neg.leftsided_pe > 0.5)| 
(df_neg.acute_and_chronic_pe > 0.5)| 
(df_neg.chronic_pe > 0.5)].reset_index(drop = True)
 rule2b['broken_rule'] = '2b'
errors = pd.concat([rule1a, rule1b, rule1c, rule1d, rule2a, rule2b], axis = 0)
 print('label in-consistency counts:', errors.shape)
 if errors.shape[0] > 0:
print(errors.broken_rule.value_counts())
 print(errors)
 assert False
def inference(model, device, df, root_path):
model.eval()
t = time.time()
ds = RSNADataset(df, 0.0, root_path, image_subsampling=False, transforms=get_valid_transforms() , output_label=False)
 dataloader = torch.utils.data.DataLoader(
 ds,
batch_size=1,
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=True, 
)
 patients = ds.get_patients()
res_dfs = []
for step,(imgs, per_image_preds, locs, img_num, index, seq_ix)in enumerate(dataloader):
imgs = imgs.to(device ).float()
per_image_preds = per_image_preds.to(device ).float()
locs = locs.to(device ).float()
index = index.detach().numpy() [0]
seq_ix = seq_ix.detach().numpy() [0,:]
patient_filt =(df.StudyInstanceUID == patients[index])
 patient_df = pd.DataFrame()
patient_df['SOPInstanceUID'] = df.loc[patient_filt, 'SOPInstanceUID'].values[seq_ix]
patient_df['SeriesInstanceUID'] = df.loc[patient_filt, 'SeriesInstanceUID'].values
patient_df['StudyInstanceUID'] = patients[index]
for c in CFG['image_target_cols']+CFG['exam_target_cols']:
patient_df[c] = 0.0
image_preds, exam_pred = model(per_image_preds, locs)
 exam_pred, image_preds = post_process(exam_pred, image_preds)
 exam_pred = torch.sigmoid(exam_pred ).cpu().detach().numpy()
image_preds = torch.sigmoid(image_preds ).cpu().detach().numpy()
patient_df[CFG['exam_target_cols']] = exam_pred[0]
patient_df[CFG['image_target_cols']] = image_preds[0,:]
res_dfs += [patient_df]


if(( step + 1)% CFG['verbose_step'] == 0)or(( step + 1)== len(dataloader)) :
print(
 f'Inference Step {step+1}/{len(dataloader)}, ' + \
f'time: {(time.time() - t):.4f}', end='\r' if(step + 1)!= len(dataloader)else '
' 
)
 res_dfs = pd.concat(res_dfs, axis=0 ).reset_index(drop=True)
 res_dfs = df[['SOPInstanceUID', 'SeriesInstanceUID', 'StudyInstanceUID']].merge(res_dfs, on=['SOPInstanceUID', 'SeriesInstanceUID', 'StudyInstanceUID'], how='left')
 print(res_dfs[CFG['image_target_cols']+CFG['exam_target_cols']].head(5))
print(res_dfs[CFG['image_target_cols']+CFG['exam_target_cols']].tail(5))
assert res_dfs.shape[0] == df.shape[0]
check_label_consistency(res_dfs)
 return res_dfs
STAGE1_CFGS = [
{
'tag': 'efb0_stage1',
'model_constructor': RSNAImgClassifierSingle,
'dataset_constructor': RSNADatasetStage1,
'output_len': 1
},
{
'tag': 'efb0_stage1_multilabel',
'model_constructor': RSNAImgClassifier,
'dataset_constructor': RSNADatasetStage1,
'output_len': 9
},
]
STAGE1_CFGS_TAG = 'efb0-stage1-single-multi-label'
def get_stage1_columns() :
new_feats = []
for cfg in STAGE1_CFGS:
for i in range(cfg['output_len']):
f = cfg['tag']+'_'+str(i)
 new_feats += [f]
return new_feats
def update_stage1_oof_preds(df, cv_df):
res_file_name = STAGE1_CFGS_TAG+"-train.csv"
new_feats = get_stage1_columns()
for f in new_feats:
df[f] = 0
if os.path.isfile(res_file_name):
df = pd.read_csv(res_file_name)
 print('img acc:',(( df[new_feats[0]]>0)==df[CFG['image_target_cols'][0]] ).mean())
 return df
for fold,(train_fold, valid_fold)in enumerate(zip(CFG['train_folds'], CFG['valid_folds'])) :
if fold < 0:
continue
valid_patients = cv_df.loc[cv_df.fold.isin(valid_fold), 'StudyInstanceUID'].unique()
filt = df.StudyInstanceUID.isin(valid_patients)
 valid_ = df.loc[filt,:].reset_index(drop=True)
 image_preds_all_list = []
for cfg in STAGE1_CFGS:
valid_ds = cfg['dataset_constructor'](valid_, 0.0, CFG['train_img_path'], image_subsampling=False, transforms=get_valid_transforms() , output_label=True)
 val_loader = torch.utils.data.DataLoader(
 valid_ds,
batch_size=256,
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=False,
sampler=SequentialSampler(valid_ds)
)
 device = torch.device(CFG['device'])
 model = cfg['model_constructor']().to(device)
 model.load_state_dict(torch.load('{}/model_fold_{}_{}'.format(CFG['model_path'], fold, cfg['tag'])))
 model.eval()
image_preds_all = []
correct_count = 0
count = 0
for step,(imgs, target)in enumerate(val_loader):
imgs = imgs.to(device ).float()
target = target.to(device ).float()
image_preds = model(imgs)
 if len(image_preds.shape)== 1:
image_preds = image_preds.view(-1, 1)
 correct_count +=(( image_preds[:,0]>0)== target[:,0] ).sum().detach().item()
count += imgs.shape[0]
image_preds_all += [image_preds.cpu().detach().numpy() ]
print('acc: {:.4f}, {}, {}, {}/{}'.format(correct_count/count, correct_count, count, step+1, len(val_loader)) , end='\r')
 print()
image_preds_all = np.concatenate(image_preds_all, axis=0)
 image_preds_all_list += [image_preds_all]
del model, val_loader
torch.cuda.empty_cache()
image_preds_all_list = np.concatenate(image_preds_all_list, axis=1)
 df.loc[filt, new_feats] = image_preds_all_list
df.to_csv(res_file_name, index=False)
 return df
def update_stage1_test_preds(df):
new_feats = get_stage1_columns()
for f in new_feats:
df[f] = 0
image_preds_all_list = []
for cfg in STAGE1_CFGS:
test_ds = cfg['dataset_constructor'](df, 0.0, CFG['test_img_path'], image_subsampling=False, transforms=get_valid_transforms() , output_label=False)
 test_loader = torch.utils.data.DataLoader(
 test_ds,
batch_size=256,
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=False,
sampler=SequentialSampler(test_ds)
)
 device = torch.device(CFG['device'])
 model = cfg['model_constructor']().to(device)
 model.load_state_dict(torch.load('{}/model_{}'.format(CFG['model_path'], cfg['tag'])))
 model.eval()
image_preds_all = []
for step, imgs in enumerate(tqdm(test_loader)) :
imgs = imgs.to(device ).float()
image_preds = model(imgs)
 image_preds_all += [image_preds.cpu().detach().numpy() ]
image_preds_all = np.concatenate(image_preds_all, axis=0)
 image_preds_all_list += [image_preds_all]
del model, test_loader
torch.cuda.empty_cache()
image_preds_all_list = np.concatenate(image_preds_all_list, axis=1)
 df.loc[:,new_feats] = image_preds_all_list
return df
if __name__ == '__main__':
if CFG['train']:
seed_everything(SEED)
 if CFG['train']:
train_df = pd.read_csv(CFG['train_path'])
 cv_df = pd.read_csv(CFG['cv_fold_path'])
 with torch.no_grad() :
train_df = update_stage1_oof_preds(train_df,cv_df)
 else:
test_df = pd.read_csv(CFG['test_path'])
 with torch.no_grad() :
test_df = update_stage1_test_preds(test_df)
 if CFG['train']:
for fold,(train_fold, valid_fold)in enumerate(zip(CFG['train_folds'], CFG['valid_folds'])) :
train_loader, val_loader = prepare_train_dataloader(train_df, cv_df, train_fold, valid_fold)
 device = torch.device(CFG['device'])
 model = RSNAClassifier().to(device)
 scaler = GradScaler()
optimizer = torch.optim.Adam(model.parameters() , lr=CFG['lr'])
 scheduler = torch.optim.lr_scheduler.StepLR(optimizer, gamma=0.1, step_size=1); schd_loss_update=False
for epoch in range(CFG['epochs']):
train_one_epoch(epoch, model, device, scaler, optimizer, train_loader)
 with torch.no_grad() :
valid_one_epoch(epoch, model, device, scheduler, val_loader, schd_loss_update=schd_loss_update)
 torch.save(model.state_dict() ,'{}/model_fold_{}_{}'.format(CFG['model_path'], fold, CFG['tag']))
model.load_state_dict(torch.load('{}/model_fold_{}_{}'.format(CFG['model_path'], fold, CFG['tag'])))
 valid_patients = cv_df.loc[cv_df.fold.isin(valid_fold), 'StudyInstanceUID'].unique()
valid_ = train_df.loc[train_df.StudyInstanceUID.isin(valid_patients),:].reset_index(drop=True)
 with torch.no_grad() :
val_pred_df = inference(model, device, valid_, CFG['train_img_path'])
 target = valid_[CFG['image_target_cols']].values
pred =(val_pred_df[CFG['image_target_cols']].values > 0.5 ).astype(int)
 print('Image PE Accuracy: {:.3f}'.format(( target==pred ).mean() *100))
loss = rsna_wloss_inference(valid_[CFG['image_target_cols']].values, valid_[CFG['exam_target_cols']].values,
val_pred_df[CFG['image_target_cols']].values, val_pred_df[CFG['exam_target_cols']].values,
list(valid_.groupby('StudyInstanceUID', sort=False)['SOPInstanceUID'].count()))
print('Validation loss = {:.4f}'.format(loss.detach().item()))
del model, optimizer, train_loader, val_loader, scaler, scheduler
torch.cuda.empty_cache()
train_loader, val_loader = prepare_train_dataloader(train_df, cv_df, np.arange(0, 20), np.array([]))
device = torch.device(CFG['device'])
 model = RSNAClassifier().to(device)
 scaler = GradScaler()
optimizer = torch.optim.Adam(model.parameters() , lr=CFG['lr'])
 scheduler = torch.optim.lr_scheduler.StepLR(optimizer, gamma=0.1, step_size=1); schd_loss_update=False
for epoch in range(CFG['epochs']):
train_one_epoch(epoch, model, device, scaler, optimizer, train_loader)
 torch.save(model.state_dict() ,'{}/model_{}'.format(CFG['model_path'], CFG['tag']))
else:
device = torch.device(CFG['device'])
 model = RSNAClassifier().to(device)
 model.load_state_dict(torch.load('{}/model_{}'.format(CFG['model_path'], CFG['tag'])))
 test_pred_df = inference(model, device, test_df, CFG['test_img_path'])
 test_pred_df.to_csv('kh_submission_raw.csv')
 ids = []
labels = []
gp_mean = test_pred_df.loc[:, ['StudyInstanceUID']+CFG['exam_target_cols']].groupby('StudyInstanceUID', sort=False ).mean()
for col in CFG['exam_target_cols']:
ids += [[patient+'_'+col for patient in gp_mean.index]]
labels += [gp_mean[col].values]
ids += [test_pred_df.SOPInstanceUID.values]
labels += [test_pred_df[CFG['image_target_cols']].values[:,0]]
ids = np.concatenate(ids)
 labels = np.concatenate(labels)
 assert len(ids)== len(labels)
 submission = pd.DataFrame()
submission['id'] = ids
submission['label'] = labels
print(submission.head(3))
print(submission.tail(3))
print(submission.shape)
 submission.to_csv('submission.csv', index=False )<install_modules>
!pip install.. /input/pytorch-16/torchvision-0.7.0cu101-cp37-cp37m-linux_x86_64.whl
!pip install.. /input/pytorch-image-models
if Path('.. /working/models' ).exists() :
shutil.rmtree('.. /working/models')
 shutil.copytree('.. /input/rsna2020-module/models', '.. /working/models')
 if Path('.. /working/functions' ).exists() :
shutil.rmtree('.. /working/functions')
 shutil.copytree('.. /input/rsna2020-module/functions', '.. /working/functions')
 print("OK!" )<load_from_zip>
!pip install.. /input/pretrainedmodels/pretrainedmodels-0.7.4/pretrainedmodels-0.7.4/ > /dev/null
!cp.. /input/gdcm-conda-install/gdcm.tar.
 !tar -xvzf gdcm.tar
!conda install --offline./gdcm/gdcm-2.8.9-py37h71b2a6d_0.tar.bz2
print("OK!" )<categorify>
!pip install.. /input/wheat-pkgs/EfficientNet-PyTorch-master/EfficientNet-PyTorch-master/ > /dev/null
class RSNADataset(Dataset):
def __init__(
 self,
csv_path: str,
img_dir: str,
file_extension: str = 'dcm',
mode: str = 'test',
transform=None,
network_type: str = 'cnn_rnn', 
):
self.transform = transform
self.csv_path = Path(csv_path)
 self.img_dir = Path(img_dir)
 self.file_extension = file_extension
self.mode = mode
self.network_type = network_type
if Path('.. /input/rsna-str-pulmonary-embolism-detection/train' ).exists() and not DO_FULL:
df = pd.read_csv(self.csv_path ).head(200)
 else:
print("OK.Test Time Mode.")
 df = pd.read_csv(self.csv_path)
 df["file_name"] = df.SOPInstanceUID + '.' + self.file_extension
df["image_name"] = str(self.img_dir)+ '/' + \
df.StudyInstanceUID + '/' + df.SeriesInstanceUID + '/' + df.file_name
self.df = df
self.df["path_to_series_id"] = str(self.img_dir)+ '/' + \
self.df.StudyInstanceUID + '/' + self.df.SeriesInstanceUID
self.path_to_series_id = self.df["path_to_series_id"].unique()
def __len__(self):
return len(self.path_to_series_id)
 def __getitem__(self, index):
data_path = self.path_to_series_id[index]
dicoms, dicom_files = self._load_dicom_array(data_path)
 imgs = self._get_three_windowing_image(dicoms)
 if self.transform is not None:
imgs = imgs.transpose(0, 2, 3, 1)
 imgs = [self.transform(image=img ).transpose(
 2, 0, 1)for img in imgs]
imgs = np.stack(imgs)
 imgs = imgs.astype('float32')
 exam_level_name, image_level_name = self._get_file_names(dicom_files)
 return imgs, exam_level_name, image_level_name
def _load_dicom_array(self, path_to_series_id):
dicom_files = list(Path(path_to_series_id ).glob('*.dcm'))
dicoms = [pydicom.dcmread(d)for d in dicom_files]
slope = float(dicoms[0].RescaleSlope)
 intercept = float(dicoms[0].RescaleIntercept)
 z_pos = [float(d.ImagePositionPatient[-1])for d in dicoms]
dicoms = np.asarray([d.pixel_array for d in dicoms])
 dicoms = dicoms[np.argsort(z_pos)]
dicoms = dicoms * slope
dicoms = dicoms + intercept
dicom_files = np.array(dicom_files)[np.argsort(z_pos)]
return dicoms, dicom_files
def _windowing(self, img, window_length, window_width):
upper = window_length + window_width // 2
lower = window_length - window_width // 2
x = np.clip(img.copy() , lower, upper)
 x = x - np.min(x)
 x = x / np.max(x)
 x =(x * 255.0 ).astype('uint8')
 return x
def _get_three_windowing_image(self, dicoms):
img_lung = np.expand_dims(
 self._windowing(dicoms, -600, 1500), axis=1)
 img_mediastinal = np.expand_dims(
 self._windowing(dicoms, 40, 400), axis=1)
 img_pe_specific = np.expand_dims(
 self._windowing(dicoms, 100, 700), axis=1)
 return np.concatenate([
img_lung, img_pe_specific, img_mediastinal], axis=1)
 def _get_file_names(self, dicom_files):
exam_level_name = str(dicom_files[0].parent.parent.stem)
 dicom_files = dicom_files.tolist()
image_level_name = list(map(lambda x: str(x.stem), dicom_files))
return exam_level_name, image_level_name 
<normalization>
!pip install.. /input/wheat-pkgs/timm-0.1.20-py3-none-any.whl > /dev/null
class RSNAAugmentation:
def __init__(self, size=512):
self.transform = A.Compose([
A.Resize(size, size, p=1.0),
])
 def __call__(self, **kwargs):
augmented = self.transform(**kwargs)
 img = augmented['image']
return img<set_options>
!pip install.. /input/wheat-pkgs/segmentation_models.pytorch-master/segmentation_models.pytorch-master > /dev/null
def inference(config):
seed = config['seed']
torch.backends.cudnn.deterministic = True
random.seed(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 n_tta = config['n_tta']
transform = RSNAAugmentation
dataset_args = {
'transform': transform(**config["transform"]),
**config["dataset"]
}
dataset = RSNADataset(**dataset_args)
 loader = DataLoader(dataset=dataset,
batch_size=config["batch_size"],
shuffle=False,
num_workers=config["n_workers"],
pin_memory=False)
 device = config['gpu'][0]
checkpoint_list = config['checkpoint']
net_list = []
for ckpt_path_dict in checkpoint_list:
model_args = {}
ckpt_path_cnn = Path(ckpt_path_dict['cnn'])
 cfg_path = ckpt_path_cnn.parents[1] / 'train_config.yaml'
with open(cfg_path, 'r', encoding='utf-8')as f:
cfg = yaml.load(f, Loader=yaml.SafeLoader)
 model_args['cnn_model'] = cfg["model"]["name"]
model_args['cnn_pretrained_path'] = ckpt_path_cnn
model_args['cnn_param'] = cfg["model"]["args"]
model_args['cnn_param']['pretrained'] = False
ckpt_path_cnn = Path(ckpt_path_dict['rnn'])
 cfg_path = ckpt_path_cnn.parents[1] / 'train_config.yaml'
with open(cfg_path, 'r', encoding='utf-8')as f:
cfg = yaml.load(f, Loader=yaml.SafeLoader)
 model_args['rnn_model'] = cfg["model"]["name"]
model_args['rnn_pretrained_path'] = ckpt_path_cnn
model_args['rnn_param'] = cfg["model"]["args"]
net = getattr(models, 'CNN_RNN' )(**model_args)
 net.to(device)
 net.eval()
net_list.append(net)
 chunk_size_list = config['chunk_size_list']
if len(chunk_size_list)== 1:
chunk_size_list = chunk_size_list * len(net_list)
 assert len(chunk_size_list)== len(net_list)
 tta = None
exam_names = []
image_names = []
with torch.no_grad() :
results_image_level = []
results_exam_level = []
for x, exam_name, image_name in tqdm(loader):
exam_names.append(exam_name[0])
 image_name = list(map(lambda x: x[0], image_name))
image_names.extend(image_name)
 n_sequence = x.size() [1]
result_tta_image, result_tta_exam = [], []
for tta_cnt in range(n_tta):
image = x.clone()
result_net_exam, result_net_image = [], []
for net_cnt, net in enumerate(net_list):
embeddings = []
for i in range(0, n_sequence, chunk_size_list[net_cnt]):
embedding = net.cnn(image[:, i:i + chunk_size_list[net_cnt], :, :, :].to(device))
embeddings.append(embedding)
 embeddings = torch.cat(embeddings, dim=1)
 image_level, exam_level = net.rnn(embeddings)
 image_level = torch.sigmoid(
 image_level ).cpu().detach().numpy().reshape(-1)
 exam_level = torch.sigmoid(
 exam_level ).cpu().detach().numpy().reshape(-1)
 exam_level = label_consistency(image_level, exam_level)
 result_net_image.append(image_level)
 result_net_exam.append(exam_level)
 result_net_image = np.array(result_net_image)
 result_tta_image.append(result_net_image)
 result_net_exam = np.array(result_net_exam)
 result_tta_exam.append(result_net_exam)
 result_tta_image = np.array(result_tta_image)
 results_image_level.append(result_tta_image)
 result_tta_exam = np.array(result_tta_exam)
 results_exam_level.append(result_tta_exam)
 results_exam_level = np.array(results_exam_level)
 results_exam_level = results_exam_level.mean(axis=1 ).mean(axis=1)
 results_image_level = list(map(
 lambda x: x.mean(axis=0 ).mean(axis=0), results_image_level))
results_exam_level = np.stack([label_consistency(image_level, exam_level)
 for image_level, exam_level in zip(results_image_level, results_exam_level)])
 results_image_level = np.concatenate(results_image_level)
 results_exam_level = results_exam_level.reshape(-1)
 exam_names = get_exam_names(exam_names)
 assert len(image_names)== len(results_image_level)
 assert len(exam_names)== len(results_exam_level)
 names = image_names + exam_names
results = np.concatenate([results_image_level, results_exam_level])
 submission = pd.DataFrame([names, results], index=['id', 'label'] ).T
if check_consistency(submission, dataset.df):
print("Great! Fanstastic! You are genious!!!")
 submission.to_csv('submission.csv', index=False)
 else:
print("ERROR! submission file doesn't satisfy concistency!!")
 def label_consistency(image_level, exam_level):
p_negative_exam_for_pe = exam_level[0]
p_indeterminate = exam_level[1]
p_chronic_pe = exam_level[2]
p_acute_and_chronic_pe = exam_level[3]
p_central_pe = exam_level[4]
p_leftsided_pe = exam_level[5]
p_rightsided_pe = exam_level[6]
p_rv_lv_ratio_gte_1 = exam_level[7]
p_rv_lv_ratio_lt_1 = exam_level[8]
pe_exist = np.any(image_level > 0.5)
 if pe_exist:
p_negative_exam_for_pe = np.clip(p_negative_exam_for_pe, None, 0.499)
 p_indeterminate = np.clip(p_indeterminate, None, 0.499)
 if p_chronic_pe > 0.5 and p_acute_and_chronic_pe > 0.5:
tmp_list = [p_chronic_pe, p_acute_and_chronic_pe]
tmp_list[np.argmin(tmp_list)] = 0.499
p_chronic_pe, p_acute_and_chronic_pe = tmp_list
if p_central_pe <= 0.5 and p_leftsided_pe <= 0.5 and p_rightsided_pe <= 0.5:
tmp_list = [p_central_pe, p_leftsided_pe, p_rightsided_pe]
tmp_list[np.argmax(tmp_list)] = 0.501
p_central_pe, p_leftsided_pe, p_rightsided_pe = tmp_list
if p_rv_lv_ratio_gte_1 <= 0.5 and p_rv_lv_ratio_lt_1 <= 0.5:
tmp_list = [p_rv_lv_ratio_gte_1, p_rv_lv_ratio_lt_1]
tmp_list[np.argmax(tmp_list)] = 0.501
p_rv_lv_ratio_gte_1, p_rv_lv_ratio_lt_1 = tmp_list
if p_rv_lv_ratio_gte_1 > 0.5 and p_rv_lv_ratio_lt_1 > 0.5:
tmp_list = [p_rv_lv_ratio_gte_1, p_rv_lv_ratio_lt_1]
tmp_list[np.argmin(tmp_list)] = 0.499
p_rv_lv_ratio_gte_1, p_rv_lv_ratio_lt_1 = tmp_list
else:
if p_negative_exam_for_pe <= 0.5 and p_indeterminate <= 0.5:
tmp_list = [p_negative_exam_for_pe, p_indeterminate]
tmp_list[np.argmax(tmp_list)] = 0.501
p_negative_exam_for_pe, p_indeterminate = tmp_list
if p_negative_exam_for_pe > 0.5 and p_indeterminate > 0.5:
tmp_list = [p_negative_exam_for_pe, p_indeterminate]
tmp_list[np.argmin(tmp_list)] = 0.499
p_negative_exam_for_pe, p_indeterminate = tmp_list
p_chronic_pe = np.clip(p_chronic_pe, None, 0.499)
 p_acute_and_chronic_pe = np.clip(p_acute_and_chronic_pe, None, 0.499)
 p_central_pe = np.clip(p_central_pe, None, 0.499)
 p_leftsided_pe = np.clip(p_leftsided_pe, None, 0.499)
 p_rightsided_pe = np.clip(p_rightsided_pe, None, 0.499)
 p_rv_lv_ratio_gte_1 = np.clip(p_rv_lv_ratio_gte_1, None, 0.499)
 p_rv_lv_ratio_lt_1 = np.clip(p_rv_lv_ratio_lt_1, None, 0.499)
 exam_level = np.array([
p_negative_exam_for_pe,
p_indeterminate,
p_chronic_pe,
p_acute_and_chronic_pe,
p_central_pe,
p_leftsided_pe,
p_rightsided_pe,
p_rv_lv_ratio_gte_1,
p_rv_lv_ratio_lt_1,
])
 return exam_level
def get_exam_names(exam_names):
target_cols = [
'negative_exam_for_pe',
'indeterminate',
'chronic_pe', 'acute_and_chronic_pe',
'central_pe', 'leftsided_pe', 'rightsided_pe',
'rv_lv_ratio_gte_1', 'rv_lv_ratio_lt_1',
]
new_exam_names = []
for e in exam_names:
for col in target_cols:
new_exam_names.append(e + '_' + col)
 return new_exam_names
def check_consistency(sub, test_csv):
str_split = sub.id.str.split('_', 1, expand=True)
 str_split.columns = ['StudyInstanceUID', 'label_type']
condition = ~str_split.label_type.isnull()
new_df = pd.concat([sub[condition], str_split[condition]], axis=1)
 del new_df['id']
df_exam = new_df.pivot(index='StudyInstanceUID', columns='label_type', values='label')
 condition = str_split.label_type.isnull()
df_image = sub[condition]
df_image = df_image.merge(test_csv, how='left', left_on='id', right_on='SOPInstanceUID')
 df_image.rename(columns = {"label": "pe_present_on_image"}, inplace=True)
 del df_image['id']
df = df_exam.merge(df_image, how='left', on='StudyInstanceUID')
 ids = ["StudyInstanceUID", "SeriesInstanceUID", "SOPInstanceUID"]
labels = [c for c in df.columns if c not in ids]
df = df[ids + labels]
df['positive_images_in_exam'] = df['StudyInstanceUID'].map(df.groupby(['StudyInstanceUID'] ).pe_present_on_image.max())
 df_pos = df.loc[df.positive_images_in_exam > 0.5]
df_neg = df.loc[df.positive_images_in_exam <= 0.5]
rule1a = df_pos.loc[(( df_pos.rv_lv_ratio_lt_1 > 0.5)& 
(df_pos.rv_lv_ratio_gte_1 > 0.5)) | 
(( df_pos.rv_lv_ratio_lt_1 <= 0.5)& 
(df_pos.rv_lv_ratio_gte_1 <= 0.5)) ].reset_index(drop = True)
 rule1a['broken_rule'] = '1a'
rule1b = df_pos.loc[(df_pos.central_pe <= 0.5)& 
(df_pos.rightsided_pe <= 0.5)& 
(df_pos.leftsided_pe <= 0.5)].reset_index(drop = True)
 rule1b['broken_rule'] = '1b'
rule1c = df_pos.loc[(df_pos.acute_and_chronic_pe > 0.5)& 
(df_pos.chronic_pe > 0.5)].reset_index(drop = True)
 rule1c['broken_rule'] = '1c'
rule1d = df_pos.loc[(df_pos.indeterminate > 0.5)| 
(df_pos.negative_exam_for_pe > 0.5)].reset_index(drop = True)
 rule1d['broken_rule'] = '1d'
rule2a = df_neg.loc[(( df_neg.indeterminate > 0.5)& 
(df_neg.negative_exam_for_pe > 0.5)) | 
(( df_neg.indeterminate <= 0.5)& 
(df_neg.negative_exam_for_pe <= 0.5)) ].reset_index(drop = True)
 rule2a['broken_rule'] = '2a'
rule2b = df_neg.loc[(df_neg.rv_lv_ratio_lt_1 > 0.5)| 
(df_neg.rv_lv_ratio_gte_1 > 0.5)| 
(df_neg.central_pe > 0.5)| 
(df_neg.rightsided_pe > 0.5)| 
(df_neg.leftsided_pe > 0.5)| 
(df_neg.acute_and_chronic_pe > 0.5)| 
(df_neg.chronic_pe > 0.5)].reset_index(drop = True)
 rule2b['broken_rule'] = '2b'
errors = pd.concat([rule1a, rule1b, rule1c, rule1d, rule2a, rule2b], axis = 0)
 if len(errors)== 0:
return True
else:
return False<define_variables>
set_seed,
create_logging,
WheatDataset,
FastDataLoader,
collate,
ModleWithLoss,
CtdetLoss,
ModelEMA,
get_constant_schedule_with_warmup,
train_one_epoch,
get_train_transforms,
freeze_bn
)
config = {}
config['checkpoint'] = [
{
'cnn': '/kaggle/input/rsna2020-models/run-20201020_060628-2cpwvzq3/files/checkpoints/epoch01-val_loss0.095.ckpt',
'rnn': '/kaggle/input/rsna2020-models/run-20201021_135533-33iuaotu/files/checkpoints/epoch32-val_loss0.145.ckpt',
},
{
'cnn': '/kaggle/input/rsna2020-models/run-20201020_150752-28onzftp/files/checkpoints/epoch02-val_loss0.095.ckpt',
'rnn': '/kaggle/input/rsna2020-models/run-20201021_135644-3kch48tn/files/checkpoints/epoch28-val_loss0.146.ckpt',
},
{
'cnn': '/kaggle/input/rsna2020-models/run-20201020_151143-31kxh5r3/files/checkpoints/epoch02-val_loss0.093.ckpt',
'rnn': '/kaggle/input/rsna2020-models/run-20201021_135714-3bjhhbju/files/checkpoints/epoch31-val_loss0.143.ckpt',
},
{
'cnn': '/kaggle/input/rsna2020-models/run-20201022_070959-21nvi91y/files/checkpoints/epoch02-val_loss0.027.ckpt',
'rnn': '/kaggle/input/rsna2020-models/run-20201023_041844-1y4pdiyr/files/checkpoints/epoch36-val_loss0.150.ckpt',
},
{
'cnn': '/kaggle/input/rsna2020-models/run-20201022_071013-3iwenrxd/files/checkpoints/epoch02-val_loss0.027.ckpt',
'rnn': '/kaggle/input/rsna2020-models/run-20201023_041955-1fmrfriu/files/checkpoints/epoch34-val_loss0.150.ckpt',
},
{
'cnn': '/kaggle/input/rsna2020-models/run-20201022_070525-19vyc6gd/files/checkpoints/epoch02-val_loss0.026.ckpt',
'rnn': '/kaggle/input/rsna2020-models/run-20201023_042016-30e7wvxy/files/checkpoints/epoch34-val_loss0.141.ckpt',
},
]
config['batch_size'] = 1
config['chunk_size_list'] = [
64,
64,
64,
64,
64,
64,
]
config['n_workers'] = 1
config['n_tta'] = 1
config['seed'] = 42
config['gpu'] = [0]
config['transform'] = {
'size': 512
}
config['dataset'] = {
"csv_path": "/kaggle/input/rsna-str-pulmonary-embolism-detection/test.csv",
"img_dir": "/kaggle/input/rsna-str-pulmonary-embolism-detection/test",
"file_extension": "dcm",
"mode": "test",
"network_type": "cnn_rnn",
}
DO_FULL = False<import_modules>
bifpn_path_0 = '.. /input/wheat-weights/model_centernet_effnetb5_bifpn_00099.pth'
bifpn_path_1 = '.. /input/wheat-weights/model_centernet_effnetb5_bifpn_fold1_00099.pth'
bifpn_path_3 = '.. /input/wheat-weights/model_centernet_effnetb5_bifpn_fold3_lb_ema_00099.pth'
import sys
import pandas as pd<load_from_csv>
class Config:
arch = 'timm-efficientnet-b5'
heads = {'hm': 1,
'wh': 2,
'reg': 2}
head_conv = 64
reg_offset = True
cat_spec_wh = False
img_size = 1024
in_scale = 1024 / img_size
down_ratio = 4
mean = [0.315290, 0.317253, 0.214556],
std = [0.245211, 0.238036, 0.193879]
num_classes = 1
pad = 63
batch_size = 8
K = 128
max_per_image = 128
fix_res = False
test_scales = [1]
flip_test = False
nms = False
gpus = [0]
amp = True
opt = Config()
device = torch.device('cuda')if opt.gpus[0] >= 0 else torch.device('cpu' )
sample_sub = pd.read_csv('/kaggle/input/rsna-str-pulmonary-embolism-detection/sample_submission.csv' )<define_variables>
def change_key(d):
for _ in range(len(d)) :
k, v = d.popitem(False)
 d['.'.join(k.split('.')[1:])] = v
commit_flag = False<save_to_csv>
DIR_INPUT = '.. /input/global-wheat-detection'
DIR_TRAIN = f'{DIR_INPUT}/train'
DIR_TEST = f'{DIR_INPUT}/test'
train_df = pd.read_csv(f'{DIR_INPUT}/train.csv')
 train_df.shape
if len(sample_sub)==152703:
sample_sub.to_csv('submission.csv', index=False)
 commit_flag = True
sys.exit(1 )<install_modules>
train_df['x'] = -1
train_df['y'] = -1
train_df['w'] = -1
train_df['h'] = -1
def expand_bbox(x):
r = np.array(re.findall("([0-9]+[.]?[0-9]*)", x))
if len(r)== 0:
r = [-1, -1, -1, -1]
return r
train_df[['x', 'y', 'w', 'h']] = np.stack(train_df['bbox'].apply(lambda x: expand_bbox(x)))
 train_df.drop(columns=['bbox'], inplace=True)
 train_df['x'] = train_df['x'].astype(np.float)
 train_df['y'] = train_df['y'].astype(np.float)
 train_df['w'] = train_df['w'].astype(np.float)
 train_df['h'] = train_df['h'].astype(np.float)
 train_df.head()
!cp.. /input/gdcm-conda-install/gdcm.tar.
 !tar -xvzf gdcm.tar
!conda install --offline./gdcm/gdcm-2.8.9-py37h71b2a6d_0.tar.bz2
!pip install /kaggle/input/software/timm-0.2.1-py3-none-any.whl > /dev/null
!pip install /kaggle/input/software/omegaconf-2.0.2-py3-none-any.whl > /dev/null
!pip install /kaggle/input/software/kornia-0.4.0-py2.py3-none-any.whl > /dev/null<import_modules>
class WheatDatasetTest(torch.utils.data.Dataset):
def __init__(self, opt, image_dir, transforms=None,
mean=[0.315290, 0.317253, 0.214556],
std=[0.245211, 0.238036, 0.193879]):
self.opt = opt
self.image_dir = image_dir
self.img_id = os.listdir(self.image_dir)
 self.transforms = transforms
self.mean = np.array(mean, dtype=np.float32 ).reshape(1, 1, 3)
 self.std = np.array(std, dtype=np.float32 ).reshape(1, 1, 3)
 def __len__(self):
return len(self.img_id)
 def __getitem__(self, idx):
img_path = os.path.join(self.image_dir, self.img_id[idx])
 image = cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB)
 h0, w0 = image.shape[0:2]
if self.transforms is not None:
image = self.transforms(image)
 image_resized = cv2.resize(image,(self.opt.img_size, self.opt.img_size))
return image_resized, self.img_id[idx], image, h0, w0
import gc
import math
import pickle
import numpy as np
import pandas as pd
import random
import pydicom
import cv2
import albumentations as A
import torch
import torch.nn as nn
import torch.nn.functional as F
import timm
import kornia
from omegaconf import OmegaConf
from torch.nn import Parameter
from torch.cuda.amp import autocast
from torch.utils.data import Dataset, DataLoader
from pathlib import Path
from albumentations.pytorch import ToTensorV2
from tqdm import tqdm
from joblib import Parallel, delayed<set_options>
def flip_lr(img):
return np.ascontiguousarray(img[:, ::-1, :])
 def deaug_lr(img, boxes):
h, w = img.shape[:2]
boxes[:,(0, 2)] = w - boxes[:,(2, 0)]
return boxes
def flip_ud(img):
return np.ascontiguousarray(img[::-1, :, :])
 def deaug_ud(img, boxes):
h, w = img.shape[:2]
boxes[:,(1, 3)] = w - boxes[:,(3, 1)]
return boxes
seed = 7
random.seed(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu' )<define_variables>
testdataset = WheatDatasetTest(opt, DIR_TEST)
 print('Total number of images in test set: {}'.format(len(testdataset)))
 testdataset_lr = WheatDatasetTest(opt, DIR_TEST, transforms=flip_lr)
 testdataset_ud = WheatDatasetTest(opt, DIR_TEST, transforms=flip_ud )
label_cols = [
"pe_present_on_image",
"negative_exam_for_pe",
"indeterminate",
"chronic_pe",
"acute_and_chronic_pe",
"central_pe",
"leftsided_pe",
"rightsided_pe",
"rv_lv_ratio_gte_1",
"rv_lv_ratio_lt_1",
]
pred_cols = [f'{col}_pred' for col in label_cols] 
<feature_engineering>
def do_predict(opt, model, threshold, flip_type=0, return_ids=False, return_shapes=False):
if flip_type == 0:
test_dataset = testdataset
deaug_transform = None
elif flip_type == 1:
test_dataset = testdataset_lr
deaug_transform = deaug_lr
elif flip_type == 2:
test_dataset = testdataset_ud
deaug_transform = deaug_ud
detector = CtdetDetector(opt, model)
 pred_boxes = []
pred_scores = []
height_list = []
width_list = []
if return_ids:
img_ids = []
for img, img_id, img0, h0, w0 in tqdm(test_dataset):
ret = detector.run(img)
 results = ret['results'][1]
results = results[results[:, 4] > threshold]
pred_box = results[:, :4]
if flip_type != 0:
pred_box = deaug_transform(img, pred_box)
 pred_box[:, 0] = np.clip(pred_box[:, 0] / opt.img_size * w0, 0, w0-1)
 pred_box[:, 1] = np.clip(pred_box[:, 1] / opt.img_size * h0, 0 ,h0-1)
 pred_box[:, 2] = np.clip(pred_box[:, 2] / opt.img_size * w0, 0, w0-1)
 pred_box[:, 3] = np.clip(pred_box[:, 3] / opt.img_size * h0, 0 ,h0-1)
 pred_boxes.append(pred_box)
 pred_scores.append(results[:, 4])
 if return_ids:
img_ids.append(os.path.splitext(img_id)[0])
 if return_shapes:
height_list.append(h0)
 width_list.append(w0)
 if return_shapes:
if return_ids:
return pred_boxes, pred_scores, height_list, width_list, img_ids
else:
return pred_boxes, pred_scores, height_list, width_list
else:
if return_ids:
return pred_boxes, pred_scores, img_ids
else:
return pred_boxes, pred_scores
def softmax(x, axis):
u = np.sum(np.exp(x), axis=axis, keepdims=True)
 return np.exp(x)/u
def postprocess(x, s=2.0):
logit = np.log(x/(1 - x))
logit = logit + s
sigmoid = 1 /(1 + np.exp(-logit))
return sigmoid
def consistency_check(df):
df['positive_images_in_exam'] = df['StudyInstanceUID'].map(df.groupby(['StudyInstanceUID'])['pe_present_on_image'].max())
 df_pos = df.loc[df.positive_images_in_exam > 0.5]
df_neg = df.loc[df.positive_images_in_exam <= 0.5]
rule1a = df_pos.loc[(( df_pos['rv_lv_ratio_lt_1'] > 0.5)& 
(df_pos['rv_lv_ratio_gte_1'] > 0.5)) | 
(( df_pos['rv_lv_ratio_lt_1'] <= 0.5)& 
(df_pos['rv_lv_ratio_gte_1'] <= 0.5)) ].reset_index(drop = True)
 rule1a['broken_rule'] = '1a'
rule1b = df_pos.loc[(df_pos['central_pe'] <= 0.5)& 
(df_pos['rightsided_pe'] <= 0.5)& 
(df_pos['leftsided_pe'] <= 0.5)].reset_index(drop = True)
 rule1b['broken_rule'] = '1b'
rule1c = df_pos.loc[(df_pos['acute_and_chronic_pe'] > 0.5)& 
(df_pos['chronic_pe'] > 0.5)].reset_index(drop = True)
 rule1c['broken_rule'] = '1c'
rule1d = df_pos.loc[(df_pos['indeterminate'] > 0.5)| 
(df_pos['negative_exam_for_pe'] > 0.5)].reset_index(drop = True)
 rule1d['broken_rule'] = '1d'
rule2a = df_neg.loc[(( df_neg['indeterminate'] > 0.5)& 
(df_neg['negative_exam_for_pe'] > 0.5)) | 
(( df_neg['indeterminate'] <= 0.5)& 
(df_neg['negative_exam_for_pe'] <= 0.5)) ].reset_index(drop = True)
 rule2a['broken_rule'] = '2a'
rule2b = df_neg.loc[(df_neg['rv_lv_ratio_lt_1'] > 0.5)| 
(df_neg['rv_lv_ratio_gte_1'] > 0.5)| 
(df_neg['central_pe'] > 0.5)| 
(df_neg['rightsided_pe'] > 0.5)| 
(df_neg['leftsided_pe'] > 0.5)| 
(df_neg['acute_and_chronic_pe'] > 0.5)| 
(df_neg['chronic_pe'] > 0.5)].reset_index(drop = True)
 rule2b['broken_rule'] = '2b'
errors = pd.concat([rule1a, rule1b, rule1c, rule1d, rule2a, rule2b], axis = 0)
 return errors['broken_rule'].value_counts()
def satisfy_label_consistency(df, delta=1):
rule_breaks = consistency_check(df ).index
print(rule_breaks)
 if len(rule_breaks)> 0:
df["positive_exam_for_pe"] = 1 - df["negative_exam_for_pe"]
df.loc[
df.query("positive_exam_for_pe <= pe_present_on_image" ).index,
"pe_present_on_image",
] = df.loc[
df.query("positive_exam_for_pe <= pe_present_on_image" ).index,
"positive_exam_for_pe",
]
rule_breaks = consistency_check(df ).index
df["positive_images_in_exam"] = df["StudyInstanceUID"].map(
 df.groupby(["StudyInstanceUID"])["pe_present_on_image"].max() 
)
 df_pos = df.query("positive_images_in_exam > 0.5")
 df_neg = df.query("positive_images_in_exam <= 0.5")
 if "1a" in rule_breaks:
rv_filter = "rv_lv_ratio_gte_1 > 0.5 & rv_lv_ratio_lt_1 > 0.5"
while len(df_pos.query(rv_filter)) > 0:
df_pos.loc[df_pos.query(rv_filter ).index, "rv_min"] = df_pos.query(
 rv_filter 
)[label_cols[8:]].min(1)
 for rv_col in label_cols[8:]:
df_pos.loc[
df_pos.query(rv_filter + f" & {rv_col} == rv_min" ).index, rv_col
] = postprocess(
 df_pos.query(rv_filter + f" & {rv_col} == rv_min")[
rv_col
].values,
s=-0.1, 
)
 rv_filter = "rv_lv_ratio_gte_1 <= 0.5 & rv_lv_ratio_lt_1 <= 0.5"
while len(df_pos.query(rv_filter)) > 0:
df_pos.loc[df_pos.query(rv_filter ).index, "rv_max"] = df_pos.query(
 rv_filter 
)[label_cols[8:]].max(1)
 for rv_col in label_cols[8:]:
df_pos.loc[
df_pos.query(rv_filter + f" & {rv_col} == rv_max" ).index, rv_col
] = postprocess(
 df_pos.query(rv_filter + f" & {rv_col} == rv_max")[
rv_col
].values,
s=0.1, 
)
 df.loc[df_pos.index, label_cols[8:]] = df_pos[label_cols[8:]]
if "1b" in rule_breaks:
pe_filter = " & ".join([f"{col} <= 0.5" for col in label_cols[5:8]])
 while "1b" in consistency_check(df ).index:
for col in label_cols[5:8]:
df_pos.loc[df_pos.query(pe_filter ).index, col] = postprocess(
 df_pos.loc[df_pos.query(pe_filter ).index, col], s=0.1 
)
 df.loc[df_pos.index, label_cols[5:8]] = df_pos[label_cols[5:8]].values
if "1c" in rule_breaks:
chronic_filter = "chronic_pe > 0.5 & acute_and_chronic_pe > 0.5"
df_pos.loc[df_pos.query(chronic_filter ).index, label_cols[3:5]] = softmax(
 df_pos.query(chronic_filter)[label_cols[3:5]].values, axis=1 
)
 df.loc[df_pos.index, label_cols[3:5]] = df_pos[label_cols[3:5]]
if "1d" in rule_breaks:
neg_filter = "negative_exam_for_pe > 0.5 | indeterminate > 0.5"
while "1d" in consistency_check(df ).index:
for col in label_cols[1:3]:
df_pos.loc[df_pos.query(neg_filter ).index, col] = postprocess(
 df_pos.loc[df_pos.query(neg_filter ).index, col], s=-0.1 
)
 df.loc[df_pos.index, label_cols[1:3]] = df_pos[label_cols[1:3]].values
if "2a" in rule_breaks:
neg_filter = "negative_exam_for_pe > 0.5 & indeterminate > 0.5"
while len(df_neg.query(neg_filter)) > 0:
df_neg.loc[df_neg.query(neg_filter ).index, "neg_min"] = df_neg.query(
 neg_filter 
)[label_cols[1:3]].min(1)
 for neg_col in label_cols[1:3]:
df_neg.loc[
df_neg.query(neg_filter + f" & {neg_col} == neg_min" ).index,
neg_col,
] = postprocess(
 df_neg.query(neg_filter + f" & {neg_col} == neg_min")[
neg_col
].values,
s=-0.1, 
)
 neg_filter = "negative_exam_for_pe <= 0.5 & indeterminate <= 0.5"
while len(df_neg.query(neg_filter)) > 0:
df_neg.loc[df_neg.query(neg_filter ).index, "neg_max"] = df_neg.query(
 neg_filter 
)[label_cols[1:3]].max(1)
 for neg_col in label_cols[1:3]:
df_neg.loc[
df_neg.query(neg_filter + f" & {neg_col} == neg_max" ).index,
neg_col,
] = postprocess(
 df_neg.query(neg_filter + f" & {neg_col} == neg_max")[
neg_col
].values,
s=0.1, 
)
 df.loc[df_neg.index, label_cols[1:3]] = df_neg[label_cols[1:3]]
if "2b" in rule_breaks:
while "2b" in consistency_check(df ).index:
for col in label_cols[3:]:
df_neg.loc[df_neg.query(f"{col} > 0.5" ).index, col] = postprocess(
 df_neg.loc[df_neg.query(f"{col} > 0.5" ).index, col], s=-0.1 
)
 df.loc[df_neg.index, label_cols[3:]] = df_neg[label_cols[3:]].values
return df<randomize_order>
bifpn_model = PoseBiFPNNet(opt.arch, opt.heads, opt.head_conv)
 checkpoint = torch.load(bifpn_path_0, map_location=device)
 change_key(checkpoint['model'])
 bifpn_model.load_state_dict(checkpoint['model'])
 bifpn_model.to(device)
 del checkpoint
gc.collect()
def load_scans(dcm_dir_path):
f = [pydicom.dcmread(file)for file in dcm_dir_path.glob('*.dcm')]
return f
def get_data(dcm_dir_path):
scans = load_scans(dcm_dir_path)
 scans.sort(key = lambda x: float(x.ImagePositionPatient[2]))
M = float(scans[0].RescaleSlope)
 B = float(scans[0].RescaleIntercept)
 meta_columns = [
"SOPInstanceUID",
"KVP",
"XRayTubeCurrent",
"Exposure",
"SliceThickness",
"ImagePositionPatient_x",
"ImagePositionPatient_y",
"ImagePositionPatient_z",
]
meta_data = []
image_data = []
for s in scans:
meta_data.append([s[col].value for col in meta_columns[: -3]])
 meta_data[-1].extend([s['ImagePositionPatient'].value[i] for i in range(3)])
 pixel_array = s.pixel_array * M + B
image_data.append(pixel_array)
 return np.array(image_data), pd.DataFrame(np.array(meta_data), columns = meta_columns)
 def window(img, WL=50, WW=350):
upper, lower = WL+WW//2, WL-WW//2
X = np.clip(img.copy() , lower, upper)
 X = X - np.min(X)
 X = X / np.max(X)
 X =(X*255.0 ).astype('uint8')
 return X
def convert_image(img, WL_list, WW_list):
return np.stack([window(img, WL, WW)for WL, WW in zip(WL_list, WW_list)], axis=2 )<define_variables>
opt.pad = 63
opt.test_scales = [1.1, ]
threshold = 0.30
bifpn0_pred_boxes_0 , bifpn0_pred_scores_0, h0_list, w0_list, img_ids = do_predict(opt, bifpn_model, threshold=threshold, flip_type=0, return_ids=True, return_shapes=True)
 bifpn0_pred_boxes_0_lr, bifpn0_pred_scores_0_lr = do_predict(opt, bifpn_model, threshold=threshold, flip_type=1, return_ids=False, return_shapes=False)
 bifpn0_pred_boxes_0_ud, bifpn0_pred_scores_0_ud = do_predict(opt, bifpn_model, threshold=threshold, flip_type=2, return_ids=False, return_shapes=False)
 opt.test_scales = [1.25, ]
threshold = 0.28
bifpn0_pred_boxes_l , bifpn0_pred_scores_l = do_predict(opt, bifpn_model, threshold=threshold, flip_type=0, return_ids=False, return_shapes=False)
 bifpn0_pred_boxes_l_lr, bifpn0_pred_scores_l_lr = do_predict(opt, bifpn_model, threshold=threshold, flip_type=1, return_ids=False, return_shapes=False)
 bifpn0_pred_boxes_l_ud, bifpn0_pred_scores_l_ud = do_predict(opt, bifpn_model, threshold=threshold, flip_type=2, return_ids=False, return_shapes=False )
MODE = 'private'<define_variables>
del bifpn_model
gc.collect()
torch.cuda.empty_cache()
data_root = Path('/kaggle/input/rsna-str-pulmonary-embolism-detection' )<load_from_csv>
bifpn_model = PoseBiFPNNet(opt.arch, opt.heads, opt.head_conv)
 checkpoint = torch.load(bifpn_path_1, map_location=device)
 change_key(checkpoint['model'])
 bifpn_model.load_state_dict(checkpoint['model'])
 bifpn_model.to(device)
 del checkpoint
gc.collect()
test_df = pd.read_csv(data_root / 'test.csv')
 test_df = test_df[['StudyInstanceUID', 'SeriesInstanceUID']].drop_duplicates().reset_index(drop=True)
 test_df['dcm_dir_path'] = test_df.apply(lambda x: data_root / 'test' / x["StudyInstanceUID"] / x["SeriesInstanceUID"], axis=1 )<load_from_csv>
opt.pad = 63
opt.test_scales = [1.1, ]
threshold = 0.30
bifpn1_pred_boxes_0 , bifpn1_pred_scores_0 = do_predict(opt, bifpn_model, threshold=threshold, flip_type=0, return_ids=False, return_shapes=False)
 bifpn1_pred_boxes_0_lr, bifpn1_pred_scores_0_lr = do_predict(opt, bifpn_model, threshold=threshold, flip_type=1, return_ids=False, return_shapes=False)
 bifpn1_pred_boxes_0_ud, bifpn1_pred_scores_0_ud = do_predict(opt, bifpn_model, threshold=threshold, flip_type=2, return_ids=False, return_shapes=False)
 opt.test_scales = [1.25, ]
threshold = 0.28
bifpn1_pred_boxes_l , bifpn1_pred_scores_l = do_predict(opt, bifpn_model, threshold=threshold, flip_type=0, return_ids=False, return_shapes=False)
 bifpn1_pred_boxes_l_lr, bifpn1_pred_scores_l_lr = do_predict(opt, bifpn_model, threshold=threshold, flip_type=1, return_ids=False, return_shapes=False)
 bifpn1_pred_boxes_l_ud, bifpn1_pred_scores_l_ud = do_predict(opt, bifpn_model, threshold=threshold, flip_type=2, return_ids=False, return_shapes=False )
public_test_df = pd.read_csv('/kaggle/input/rsna2020-public-dataset/test.csv')
 public_test_df = public_test_df[['StudyInstanceUID', 'SeriesInstanceUID']].drop_duplicates().reset_index(drop=True)
 public_test_df['dcm_dir_path'] = public_test_df.apply(lambda x: data_root / 'test' / x["StudyInstanceUID"] / x["SeriesInstanceUID"], axis=1)
 public_sample_sub = pd.read_csv('/kaggle/input/rsna2020-public-dataset/sample_submission.csv' )<data_type_conversions>
del bifpn_model
gc.collect()
torch.cuda.empty_cache()
class ImageDataset(Dataset):
def __init__(self, image):
self.image = image
def __getitem__(self, index):
image = self.image[index].astype(np.float32)/ 255
image = kornia.image_to_tensor(image)
 out = {'image': image}
return out
def __len__(self):
return len(self.image )<define_search_model>
bifpn_model = PoseBiFPNNet(opt.arch, opt.heads, opt.head_conv)
 checkpoint = torch.load(bifpn_path_3, map_location=device)
 change_key(checkpoint['model'])
 bifpn_model.load_state_dict(checkpoint['model'])
 bifpn_model.to(device)
 del checkpoint
gc.collect()
def gem(x, p=3, eps=1e-6):
return F.avg_pool2d(x.clamp(min=eps ).pow(p),(x.size(-2), x.size(-1)) ).pow(1./ p ).squeeze(-1)
 class GeM(nn.Module):
def __init__(self, p=3, eps=1e-6):
super(GeM, self ).__init__()
self.p = Parameter(torch.ones(1)* p)
 self.eps = eps
def forward(self, x):
return gem(x, p=self.p, eps=self.eps)
 class MeanMaxPooling(nn.Module):
def __init__(self):
super(MeanMaxPooling, self ).__init__()
self.pool_mean = nn.AdaptiveAvgPool2d(( 1, 1))
self.pool_max = nn.AdaptiveMaxPool2d(( 1, 1))
def forward(self, x):
mean_x = self.pool_mean(x)
 max_x = self.pool_max(x)
 out = torch.cat(( mean_x, max_x), dim=1)
 return out
class Swish(nn.Module):
def forward(self, x):
return x * torch.sigmoid(x )<choose_model_class>
opt.pad = 63
opt.test_scales = [1.1, ]
threshold = 0.30
bifpn3_pred_boxes_0 , bifpn3_pred_scores_0 = do_predict(opt, bifpn_model, threshold=threshold, flip_type=0, return_ids=False, return_shapes=False)
 bifpn3_pred_boxes_0_lr, bifpn3_pred_scores_0_lr = do_predict(opt, bifpn_model, threshold=threshold, flip_type=1, return_ids=False, return_shapes=False)
 bifpn3_pred_boxes_0_ud, bifpn3_pred_scores_0_ud = do_predict(opt, bifpn_model, threshold=threshold, flip_type=2, return_ids=False, return_shapes=False)
 opt.test_scales = [1.25, ]
threshold = 0.28
bifpn3_pred_boxes_l , bifpn3_pred_scores_l = do_predict(opt, bifpn_model, threshold=threshold, flip_type=0, return_ids=False, return_shapes=False)
 bifpn3_pred_boxes_l_lr, bifpn3_pred_scores_l_lr = do_predict(opt, bifpn_model, threshold=threshold, flip_type=1, return_ids=False, return_shapes=False)
 bifpn3_pred_boxes_l_ud, bifpn3_pred_scores_l_ud = do_predict(opt, bifpn_model, threshold=threshold, flip_type=2, return_ids=False, return_shapes=False )
class ImageModel(nn.Module):
def __init__(self, cfg):
super(ImageModel, self ).__init__()
self.arch = timm.create_model(cfg.model.name, pretrained=False)
 out_channel = self.arch.num_features
self.pool = MeanMaxPooling()
if cfg.model.pool == 'MeanMax':
self.pool = MeanMaxPooling()
out_channel = out_channel * 2
elif cfg.model.pool == 'GeM':
self.pool = GeM()
else:
self.pool = nn.AdaptiveAvgPool2d(( 1, 1))
self.l0 = nn.Linear(out_channel, 256)
 self.bn0 = nn.BatchNorm1d(256)
 self.act = Swish()
self.l1 = nn.Linear(256, 10)
 def forward(self, batch):
image = batch['image']
out = self.arch.forward_features(image)
 out = self.pool(out)
 out = out.view(out.shape[0], -1)
 out = self.act(self.bn0(self.l0(out)))
 out = self.l1(out)
 return out
def get_feature(self, batch):
with torch.no_grad() :
image = batch['image']
out = self.arch.forward_features(image)
 out = self.pool(out)
 out = out.view(out.shape[0], -1)
 return out 
<define_search_model>
del bifpn_model
gc.collect()
torch.cuda.empty_cache()
class ResNet(nn.Module):
def __init__(self, in_features, out_features, kernel_size, dropout=0.0):
super(ResNet, self ).__init__()
assert kernel_size % 2 == 1
self.conv1 = nn.Sequential(
 nn.Conv1d(
 in_features,
out_features,
kernel_size,
stride=1,
padding=(kernel_size - 1)// 2, 
),
nn.BatchNorm1d(out_features),
nn.LeakyReLU(inplace=True),
nn.Dropout(dropout), 
)
 self.conv2 = nn.Sequential(
 nn.Conv1d(
 out_features,
out_features,
kernel_size,
stride=1,
padding=(kernel_size - 1)// 2, 
),
nn.BatchNorm1d(out_features),
nn.LeakyReLU(inplace=True),
nn.Dropout(dropout), 
)
 def forward(self, x):
x = self.conv1(x)
 out = self.conv2(x)+ x
return out
class PositionalEncoding(nn.Module):
def __init__(self, d_model, dropout=0.1, max_len=201):
super(PositionalEncoding, self ).__init__()
self.dropout = nn.Dropout(p=dropout)
 pe = torch.zeros(max_len, d_model)
 position = torch.arange(0, max_len, dtype=torch.float ).unsqueeze(1)
 div_term = torch.exp(
 torch.arange(0, d_model, 2 ).float() *(-math.log(10000.0)/ d_model)
)
 pe[:, 0::2] = torch.sin(position * div_term)
 if d_model % 2 == 0:
pe[:, 1::2] = torch.cos(position * div_term)
 else:
pe[:, 1::2] = torch.cos(position * div_term)[:, : d_model // 2]
pe = pe.unsqueeze(0 ).transpose(0, 1)
 self.register_buffer("pe", pe)
 def forward(self, x):
x = x + self.pe[: x.size(0), :]
return self.dropout(x)
 class DeconvFeatureModel(nn.Module):
def __init__(self, model_config):
super(DeconvFeatureModel, self ).__init__()
self.model_config = model_config
if model_config.backbone in ["cnn", "cnn_rnn"]:
self.resnet1 = ResNet(
 model_config.num_feature, 512, kernel_size=3, dropout=model_config.dropout_rate 
)
 self.deconv1 = nn.ConvTranspose1d(
 512, 512, kernel_size=3, stride=2, padding=1 
)
 self.resnet2 = ResNet(
 512, 256, kernel_size=5, dropout=model_config.dropout_rate 
)
 self.deconv2 = nn.ConvTranspose1d(
 256, 256, kernel_size=3, stride=2, padding=1 
)
 self.resnet3 = ResNet(
 256, 128, kernel_size=7, dropout=model_config.dropout_rate 
)
 self.deconv3 = nn.ConvTranspose1d(
 128, 128, kernel_size=3, stride=2, padding=1 
)
 self.resnet4 = ResNet(
 128, 64, kernel_size=9, dropout=model_config.dropout_rate 
)
 self.deconv4 = nn.ConvTranspose1d(
 64, 64, kernel_size=3, stride=2, padding=1 
)
 self.resnet5 = ResNet(
 64, 32, kernel_size=11, dropout=model_config.dropout_rate 
)
 self.deconv5 = nn.ConvTranspose1d(
 32, 32, kernel_size=3, stride=2, padding=1 
)
 out_features = 512 + 256 + 128 + 64 + 32
elif model_config.backbone == "lstm":
self.rnn1 = nn.LSTM(
 model_config.num_feature,
512,
num_layers=2,
batch_first=True,
bidirectional=True, 
)
 self.deconv1 = nn.ConvTranspose1d(
 1024, 512, kernel_size=3, stride=2, padding=1 
)
 self.rnn2 = nn.LSTM(
 1024,
512,
num_layers=2,
batch_first=True,
bidirectional=True, 
)
 self.deconv2 = nn.ConvTranspose1d(
 1024, 512, kernel_size=3, stride=2, padding=1 
)
 out_features = 1024
elif model_config.backbone == "gru":
self.rnn1 = nn.GRU(
 model_config.num_feature,
512,
num_layers=2,
batch_first=True,
bidirectional=True, 
)
 self.deconv1 = nn.ConvTranspose1d(
 1024, 512, kernel_size=3, stride=2, padding=1 
)
 self.rnn2 = nn.GRU(
 1024,
512,
num_layers=2,
batch_first=True,
bidirectional=True, 
)
 self.deconv2 = nn.ConvTranspose1d(
 1024, 512, kernel_size=3, stride=2, padding=1 
)
 out_features = 1024
elif model_config.backbone in ["transformer", "transformer_rnn"]:
self.linear = nn.Linear(model_config.num_feature, 2048)
 self.scale = math.sqrt(2048)
 self.pe = PositionalEncoding(2048, model_config.dropout_rate)
 encoder_layer1 = nn.TransformerEncoderLayer(
 2048,
nhead=8,
dim_feedforward=1024,
dropout=model_config.dropout_rate,
activation="gelu", 
)
 self.transformer1 = nn.TransformerEncoder(encoder_layer1, 1)
 self.deconv1 = nn.ConvTranspose1d(
 2048, 1024, kernel_size=3, stride=2, padding=1 
)
 encoder_layer2 = nn.TransformerEncoderLayer(
 1024,
nhead=8,
dim_feedforward=1024,
dropout=model_config.dropout_rate,
activation="gelu", 
)
 self.transformer2 = nn.TransformerEncoder(encoder_layer2, 1)
 self.deconv2 = nn.ConvTranspose1d(
 2048, 1024, kernel_size=3, stride=2, padding=1 
)
 out_features = 2048
else:
raise NotImplementedError()
if model_config.backbone in ["cnn_rnn", "transformer_rnn"]:
self.rnn = nn.LSTM(
 out_features,
512,
num_layers=2,
batch_first=True,
bidirectional=True, 
)
 out_features = 1024
self.exam_classfifier = nn.Linear(out_features, model_config.num_classes)
 self.image_classfifier = nn.Linear(out_features, 1)
 def pooling(self, feature, seq_lens):
pool_out = torch.stack(
 [feature[idx, :seq_len].mean(0)for idx, seq_len in enumerate(seq_lens)] 
)
 return pool_out
def forward(self, batch):

 feature = batch["feature"].float()
meta_feature = batch["meta_feature"].float()
feature = torch.cat([feature, meta_feature], dim=-1)
 if self.model_config.backbone in ["cnn", "cnn_rnn"]:
feature = feature.permute(0, 2, 1)
 outs = []
for i in range(1, 6):
feature = getattr(self, f"resnet{i}" )(feature)
 out = getattr(self, f"deconv{i}" )(feature)
 outs.append(out)
 feature = torch.cat(outs, dim=1 ).permute(0, 2, 1)
 elif self.model_config.backbone in ["gru", "lstm"]:
outs = []
feature, _ = self.rnn1(feature)
 out = self.deconv1(feature.permute(0, 2, 1)).permute(0, 2, 1)
 outs.append(out)
 feature, _ = self.rnn2(feature)
 out = self.deconv2(feature.permute(0, 2, 1)).permute(0, 2, 1)
 outs.append(out)
 feature = torch.cat(outs, dim=-1)
 elif self.model_config.backbone in ["transformer", "transformer_rnn"]:
feature = torch.relu(self.linear(feature))
feature = self.pe(feature.permute(1, 0, 2))
outs = []
feature = self.transformer1(feature)
 out = self.deconv1(feature.permute(1, 2, 0)).permute(2, 0, 1)
 outs.append(out)
 feature = self.transformer1(feature)
 out = self.deconv2(feature.permute(1, 2, 0)).permute(2, 0, 1)
 outs.append(out)
 feature = torch.cat(outs, dim=-1 ).permute(1, 0, 2)
 else:
pass
if self.model_config.backbone in ["cnn_rnn", "transformer_rnn"]:
feature, _ = self.rnn(feature)
 pool_out = self.pooling(feature, batch["seq_len"])
 exam_out = self.exam_classfifier(pool_out)
 image_out = self.image_classfifier(feature)
 return exam_out, image_out
class StackingModel(nn.Module):
def __init__(self, model_config):
super(StackingModel, self ).__init__()
self.model_config = model_config
in_features = 120
if model_config.backbone in ["cnn", "cnn_rnn"]:
self.resnet1 = ResNet(
 in_features, 256, kernel_size=3, dropout=model_config.dropout_rate 
)
 self.resnet2 = ResNet(
 256, 128, kernel_size=3, dropout=model_config.dropout_rate 
)
 self.resnet3 = ResNet(
 128, 64, kernel_size=3, dropout=model_config.dropout_rate 
)
 self.resnet4 = ResNet(
 64, 32, kernel_size=3, dropout=model_config.dropout_rate 
)
 self.resnet5 = ResNet(
 32, 16, kernel_size=3, dropout=model_config.dropout_rate 
)
 out_features = 256 + 128 + 64 + 32 + 16
elif model_config.backbone == "lstm":
self.rnn = nn.LSTM(
 in_features,
in_features,
num_layers=2,
batch_first=True,
bidirectional=True, 
)
 out_features = in_features * 2
elif model_config.backbone == "gru":
self.rnn = nn.GRU(
 in_features,
in_features,
num_layers=2,
batch_first=True,
bidirectional=True, 
)
 out_features = in_features * 2
else:
pass
self.exam_classfifier = nn.Linear(out_features, model_config.num_classes)
 self.image_classfifier = nn.Linear(out_features, 1)
 def pooling(self, feature, seq_lens):
pool_out = torch.stack(
 [feature[idx, :seq_len].mean(0)for idx, seq_len in enumerate(seq_lens)] 
)
 return pool_out
def forward(self, batch):

 feature = batch["feature"].float()
if self.model_config.backbone in ["cnn", "cnn_rnn"]:
feature = feature.permute(0, 2, 1)
 outs = []
for i in range(1, 6):
feature = getattr(self, f"resnet{i}" )(feature)
 outs.append(feature)
 feature = torch.cat(outs, dim=1 ).permute(0, 2, 1)
 elif self.model_config.backbone in ["gru", "lstm"]:
feature, _ = self.rnn(feature)
 else:
pass
pool_out = self.pooling(feature, batch["seq_len"])
 exam_out = self.exam_classfifier(pool_out)
 image_out = self.image_classfifier(feature)
 return exam_out, image_out<load_pretrained>
def normalize_boxes(boxes, h0, w0):
boxes[:, 0] = boxes[:, 0] / w0
boxes[:, 1] = boxes[:, 1] / h0
boxes[:, 2] = boxes[:, 2] / w0
boxes[:, 3] = boxes[:, 3] / h0
return boxes
def denormalize_clip_boxes(boxes, h0, w0):
boxes[:, 0] = np.clip(boxes[:, 0] * w0, 0, w0-1)
 boxes[:, 1] = np.clip(boxes[:, 1] * h0, 0, h0-1)
 boxes[:, 2] = np.clip(boxes[:, 2] * w0, 0, w0-1)
 boxes[:, 3] = np.clip(boxes[:, 3] * h0, 0, h0-1)
 return boxes
def load_image_512_model(cfg, data_path):
fold_num = 5
models = [ImageModel(cfg)for i in range(fold_num)]
[m.to(device)for m in models]
states = [torch.load(data_path + '/image_model/fold_{}.ckpt'.format(i+1), map_location=device)['state_dict'] for i in range(fold_num)]
states = [{key[6:]: value for key, value in state.items() } for state in states]
[models[i].load_state_dict(states[i])for i in range(fold_num)]
[m.eval() for m in models]
del states; gc.collect()
return models
def load_image_384_model(cfg, data_path):
fold_num = 5
models = [ImageModel(cfg)for i in range(fold_num)]
[m.to(device)for m in models]
states = [torch.load(data_path + '/fold_{}.ckpt'.format(i+1), map_location=device)['state_dict'] for i in range(fold_num)]
states = [{key[6:]: value for key, value in state.items() } for state in states]
[models[i].load_state_dict(states[i])for i in range(fold_num)]
[m.eval() for m in models]
del states; gc.collect()
return models
def load_seq1_512_model(cfg, data_path):
fold_num = 5
models = [DeconvFeatureModel(cfg)for i in range(fold_num)]
[m.to(device)for m in models]
states = [torch.load(data_path + '/tf_efficientnet_b5_ns_feature_deconv_cnn_scaler_fold{}_state_dict.ckpt'.format(i), map_location=device)for i in range(fold_num)]
[models[i].load_state_dict(states[i], strict=False)for i in range(fold_num)]
[m.eval() for m in models]
del states; gc.collect()
return models
def load_seq2_512_model(cfg, data_path):
fold_num = 5
models = [DeconvFeatureModel(cfg)for i in range(fold_num)]
[m.to(device)for m in models]
states = [torch.load(data_path + '/tf_efficientnet_b5_ns_feature_deconv_rnn_scaler_fold{}_state_dict.ckpt'.format(i), map_location=device)for i in range(fold_num)]
[models[i].load_state_dict(states[i], strict=False)for i in range(fold_num)]
[m.eval() for m in models]
del states; gc.collect()
return models
def load_seq3_512_model(cfg, data_path):
fold_num = 5
models = [DeconvFeatureModel(cfg)for i in range(fold_num)]
[m.to(device)for m in models]
states = [torch.load(data_path + '/tf_efficientnet_b5_ns_feature_deconv_gru_scaler_fold{}_state_dict.ckpt'.format(i), map_location=device)for i in range(fold_num)]
[models[i].load_state_dict(states[i], strict=False)for i in range(fold_num)]
[m.eval() for m in models]
del states; gc.collect()
return models
def load_seq4_512_model(cfg, data_path):
fold_num = 5
models = [DeconvFeatureModel(cfg)for i in range(fold_num)]
[m.to(device)for m in models]
states = [torch.load(data_path + '/tf_efficientnet_b5_ns_feature_deconv_cnn_rnn_scaler_fold{}_state_dict.ckpt'.format(i), map_location=device)for i in range(fold_num)]
[models[i].load_state_dict(states[i], strict=False)for i in range(fold_num)]
[m.eval() for m in models]
del states; gc.collect()
return models
def load_seq1_384_model(cfg, data_path):
fold_num = 5
models = [DeconvFeatureModel(cfg)for i in range(fold_num)]
[m.to(device)for m in models]
states = [torch.load(data_path + '/tf_efficientnet_b3_ns_feature_deconv_cnn_scaler_fold{}_state_dict.ckpt'.format(i), map_location=device)for i in range(fold_num)]
[models[i].load_state_dict(states[i], strict=False)for i in range(fold_num)]
[m.eval() for m in models]
del states; gc.collect()
return models
def load_seq2_384_model(cfg, data_path):
fold_num = 5
models = [DeconvFeatureModel(cfg)for i in range(fold_num)]
[m.to(device)for m in models]
states = [torch.load(data_path + '/tf_efficientnet_b3_ns_feature_deconv_rnn_scaler_fold{}_state_dict.ckpt'.format(i), map_location=device)for i in range(fold_num)]
[models[i].load_state_dict(states[i], strict=False)for i in range(fold_num)]
[m.eval() for m in models]
del states; gc.collect()
return models
def load_seq3_384_model(cfg, data_path):
fold_num = 5
models = [DeconvFeatureModel(cfg)for i in range(fold_num)]
[m.to(device)for m in models]
states = [torch.load(data_path + '/tf_efficientnet_b3_ns_feature_deconv_gru_scaler_fold{}_state_dict.ckpt'.format(i), map_location=device)for i in range(fold_num)]
[models[i].load_state_dict(states[i], strict=False)for i in range(fold_num)]
[m.eval() for m in models]
del states; gc.collect()
return models
def load_seq4_384_model(cfg, data_path):
fold_num = 5
models = [DeconvFeatureModel(cfg)for i in range(fold_num)]
[m.to(device)for m in models]
states = [torch.load(data_path + '/tf_efficientnet_b3_ns_feature_deconv_cnn_rnn_scaler_fold{}_state_dict.ckpt'.format(i), map_location=device)for i in range(fold_num)]
[models[i].load_state_dict(states[i], strict=False)for i in range(fold_num)]
[m.eval() for m in models]
del states; gc.collect()
return models
def load_seq1_concat_model(cfg, data_path):
fold_num = 5
models = [DeconvFeatureModel(cfg)for i in range(fold_num)]
[m.to(device)for m in models]
states = [torch.load(data_path + '/concat_512_384_cnn_fold{}_state_dict.ckpt'.format(i), map_location=device)for i in range(fold_num)]
[models[i].load_state_dict(states[i], strict=False)for i in range(fold_num)]
[m.eval() for m in models]
del states; gc.collect()
return models
def load_seq2_concat_model(cfg, data_path):
fold_num = 5
models = [DeconvFeatureModel(cfg)for i in range(fold_num)]
[m.to(device)for m in models]
states = [torch.load(data_path + '/concat_512_384_rnn_fold{}_state_dict.ckpt'.format(i), map_location=device)for i in range(fold_num)]
[models[i].load_state_dict(states[i], strict=False)for i in range(fold_num)]
[m.eval() for m in models]
del states; gc.collect()
return models
def load_seq3_concat_model(cfg, data_path):
fold_num = 5
models = [DeconvFeatureModel(cfg)for i in range(fold_num)]
[m.to(device)for m in models]
states = [torch.load(data_path + '/concat_512_384_gru_fold{}_state_dict.ckpt'.format(i), map_location=device)for i in range(fold_num)]
[models[i].load_state_dict(states[i], strict=False)for i in range(fold_num)]
[m.eval() for m in models]
del states; gc.collect()
return models
def load_seq4_concat_model(cfg, data_path):
fold_num = 5
models = [DeconvFeatureModel(cfg)for i in range(fold_num)]
[m.to(device)for m in models]
states = [torch.load(data_path + '/concat_512_384_cnn_rnn_fold{}_state_dict.ckpt'.format(i), map_location=device)for i in range(fold_num)]
[models[i].load_state_dict(states[i], strict=False)for i in range(fold_num)]
[m.eval() for m in models]
del states; gc.collect()
return models
def load_stacking_cnn_model(cfg, data_path):
fold_num = 5
models = [StackingModel(cfg)for i in range(fold_num)]
[m.to(device)for m in models]
states = [torch.load(data_path + '/stacking_cnn_final_fold{}_state_dict.ckpt'.format(i), map_location=device)for i in range(fold_num)]
[models[i].load_state_dict(states[i], strict=False)for i in range(fold_num)]
[m.eval() for m in models]
del states; gc.collect()
return models
def load_stacking_rnn_model(cfg, data_path):
fold_num = 5
models = [StackingModel(cfg)for i in range(fold_num)]
[m.to(device)for m in models]
states = [torch.load(data_path + '/stacking_lstm_concat_fold{}_state_dict.ckpt'.format(i), map_location=device)for i in range(fold_num)]
[models[i].load_state_dict(states[i], strict=False)for i in range(fold_num)]
[m.eval() for m in models]
del states; gc.collect()
return models<load_pretrained>
sys.path.insert(0, ".. /input/weightedboxesfusion")
 iou_thr = 0.44
skip_box_thr = 0.00001
pred_boxes_ensemble = []
pred_scores_ensemble = []
for(b00, b01, b02, b03, b04, b05,
b10, b11, b12, b13, b14, b15,
b20, b21, b22, b23, b24, b25,
s00, s01, s02, s03, s04, s05,
s10, s11, s12, s13, s14, s15,
s20, s21, s22, s23, s24, s25,
h0, w0)in zip(
 tqdm(bifpn0_pred_boxes_0),
bifpn0_pred_boxes_0_lr,
bifpn0_pred_boxes_0_ud,
bifpn0_pred_boxes_l,
bifpn0_pred_boxes_l_lr,
bifpn0_pred_boxes_l_ud,
bifpn1_pred_boxes_0,
bifpn1_pred_boxes_0_lr,
bifpn1_pred_boxes_0_ud,
bifpn1_pred_boxes_l,
bifpn1_pred_boxes_l_lr,
bifpn1_pred_boxes_l_ud,
bifpn3_pred_boxes_0,
bifpn3_pred_boxes_0_lr,
bifpn3_pred_boxes_0_ud,
bifpn3_pred_boxes_l,
bifpn3_pred_boxes_l_lr,
bifpn3_pred_boxes_l_ud,
bifpn0_pred_scores_0,
bifpn0_pred_scores_0_lr,
bifpn0_pred_scores_0_ud,
bifpn0_pred_scores_l,
bifpn0_pred_scores_l_lr,
bifpn0_pred_scores_l_ud,
bifpn1_pred_scores_0,
bifpn1_pred_scores_0_lr,
bifpn1_pred_scores_0_ud,
bifpn1_pred_scores_l,
bifpn1_pred_scores_l_lr,
bifpn1_pred_scores_l_ud,
bifpn3_pred_scores_0,
bifpn3_pred_scores_0_lr,
bifpn3_pred_scores_0_ud,
bifpn3_pred_scores_l,
bifpn3_pred_scores_l_lr,
bifpn3_pred_scores_l_ud,
h0_list,
w0_list):
boxes_list = [
normalize_boxes(b00, h0, w0 ).tolist() ,
normalize_boxes(b01, h0, w0 ).tolist() ,
normalize_boxes(b02, h0, w0 ).tolist() ,
normalize_boxes(b03, h0, w0 ).tolist() ,
normalize_boxes(b04, h0, w0 ).tolist() ,
normalize_boxes(b05, h0, w0 ).tolist() ,
normalize_boxes(b10, h0, w0 ).tolist() ,
normalize_boxes(b11, h0, w0 ).tolist() ,
normalize_boxes(b12, h0, w0 ).tolist() ,
normalize_boxes(b13, h0, w0 ).tolist() ,
normalize_boxes(b14, h0, w0 ).tolist() ,
normalize_boxes(b15, h0, w0 ).tolist() ,
normalize_boxes(b20, h0, w0 ).tolist() ,
normalize_boxes(b21, h0, w0 ).tolist() ,
normalize_boxes(b22, h0, w0 ).tolist() ,
normalize_boxes(b23, h0, w0 ).tolist() ,
normalize_boxes(b24, h0, w0 ).tolist() ,
normalize_boxes(b25, h0, w0 ).tolist()
]
scores_list = [
s00.tolist() ,
s01.tolist() ,
s02.tolist() ,
s03.tolist() ,
s04.tolist() ,
s05.tolist() ,
s10.tolist() ,
s11.tolist() ,
s12.tolist() ,
s13.tolist() ,
s14.tolist() ,
s15.tolist() ,
s20.tolist() ,
s21.tolist() ,
s22.tolist() ,
s23.tolist() ,
s24.tolist() ,
s25.tolist()
]
labels_list = [
[0] * len(b00),
[0] * len(b01),
[0] * len(b02),
[0] * len(b03),
[0] * len(b04),
[0] * len(b05),
[0] * len(b10),
[0] * len(b11),
[0] * len(b12),
[0] * len(b13),
[0] * len(b14),
[0] * len(b15),
[0] * len(b20),
[0] * len(b21),
[0] * len(b22),
[0] * len(b23),
[0] * len(b24),
[0] * len(b25)
 ]
boxes, scores, _ = ensemble_boxes.ensemble_boxes_wbf.weighted_boxes_fusion(boxes_list, scores_list, labels_list, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 pred_boxes_ensemble.append(boxes)
 pred_scores_ensemble.append(scores )
if not commit_flag:
image_models_512 = load_image_512_model(cfg_512_b5, data_path = '/kaggle/input/tf-efficientnet-b5-ns-512')
 image_models_384 = load_image_384_model(cfg_384_b3, data_path = '/kaggle/input/tf-efficientnet-b3-ns-384')
 cnn_models_512 = load_seq1_512_model(cfg_cnn_512.model, data_path = '/kaggle/input/rsna-shimacos-sequence-models-final')
 lstm_models_512 = load_seq2_512_model(cfg_lstm_512.model, data_path = '/kaggle/input/rsna-shimacos-sequence-models-final')
 gru_models_512 = load_seq3_512_model(cfg_gru_512.model, data_path = '/kaggle/input/rsna-shimacos-sequence-models-final')
 cnn_rnn_models_512 = load_seq4_512_model(cfg_cnn_rnn_512.model, data_path = '/kaggle/input/rsna-shimacos-sequence-models-final')
 cnn_models_384 = load_seq1_384_model(cfg_cnn_384.model, data_path = '/kaggle/input/rsna-shimacos-sequence-models-final')
 lstm_models_384 = load_seq2_384_model(cfg_lstm_384.model, data_path = '/kaggle/input/rsna-shimacos-sequence-models-final')
 gru_models_384 = load_seq3_384_model(cfg_gru_384.model, data_path = '/kaggle/input/rsna-shimacos-sequence-models-final')
 cnn_rnn_models_384 = load_seq4_384_model(cfg_cnn_rnn_384.model, data_path = '/kaggle/input/rsna-shimacos-sequence-models-final')
 cnn_models_concat = load_seq1_concat_model(cfg_cnn_concat.model, data_path = '/kaggle/input/rsna-shimacos-sequence-models-final')
 lstm_models_concat = load_seq2_concat_model(cfg_lstm_concat.model, data_path = '/kaggle/input/rsna-shimacos-sequence-models-final')
 gru_models_concat = load_seq3_concat_model(cfg_gru_concat.model, data_path = '/kaggle/input/rsna-shimacos-sequence-models-final')
 cnn_rnn_models_concat = load_seq4_concat_model(cfg_cnn_rnn_concat.model, data_path = '/kaggle/input/rsna-shimacos-sequence-models-final' )<load_pretrained>
pred_boxes_ensemble = [denormalize_clip_boxes(a, h0, w0)for a, h0, w0 in zip(pred_boxes_ensemble, h0_list, w0_list)]
pred_scores_ensemble = [a for a in pred_scores_ensemble]
lgb_dicts = []
for label_col in label_cols:
with open(f'/kaggle/input/stacking-models-final/stacking_{label_col}.pkl', 'rb')as f:
lgb_dicts.append(pickle.load(f))
with open(f'/kaggle/input/stacking-models-final/stacking_std.pkl', 'rb')as f:
stacking_std = pickle.load(f )<load_pretrained>
data_dict_pseudo = []
id_generator = 20201000000
for idx,(bboxes, h0, w0)in enumerate(zip(tqdm(pred_boxes_ensemble), h0_list, w0_list)) :
img_dict = {
'file_name': os.path.join(DIR_TEST, testdataset[idx][1]),
'height': h0,
'width': w0,
'id': id_generator,
}
annotations = []
for bbox in bboxes:
xywh = np.round([bbox[0], bbox[1], bbox[2]-bbox[0], bbox[3]-bbox[1]] ).astype(float ).tolist()
annotations.append({
'area': xywh[2] * xywh[3],
'bbox': xywh,
'category_id': 0,
'bbox_mode': 1
})
 img_dict['annotations'] = annotations
id_generator += 1
data_dict_pseudo.append(img_dict )
stacking_cnn_models = load_stacking_cnn_model(cfg_stacking_cnn.model, data_path = '/kaggle/input/stacking-models-final')
 stacking_rnn_models = load_stacking_rnn_model(cfg_stacking_gru.model, data_path = '/kaggle/input/stacking-models-final')
<categorify>
data_dict_pseudo = [d for d in data_dict_pseudo if len(d['annotations'])> 0]
mean = torch.tensor([0.485, 0.456, 0.406])
 std = torch.tensor([0.229, 0.224, 0.225])
 transform_512_b5 = nn.Sequential(kornia.geometry.transform.Resize(( 512, 512)) ,
kornia.augmentation.Normalize(mean, std)
)
 transform_512_b5.to(device)
 transform_384_b3 = nn.Sequential(kornia.geometry.transform.Resize(( 384, 384)) ,
kornia.augmentation.Normalize(mean, std))
transform_384_b3.to(device )<categorify>
class TrainConfig:
seed = 2519
arch = 'timm-efficientnet-b5'
heads = {
'hm': 1,
'wh': 2,
'reg': 2}
head_conv = 64
reg_offset = True
data_root = '.. /input/global-wheat-detection'
crop_size = 896
scale = 0.
 shift = 0.
 rotate = 15.
 shear = 5.
 down_ratio = 4
debug = False
hm_weight = 1
off_weight = 1
wh_weight = 0.1
batch_size = 4
base_lr = 0.25e-4
warmup_iters = 0
total_epochs = 9
stage_epochs = 9
freeze_bn = True
accumulate = 3
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 ema = False
amp = True
output_dir = './'
logs_dir = os.path.join(output_dir, 'logs')
 log_interval = 10
checkpoint = 1
load_model = '.. /input/wheat-weights/model_centernet_effnetb5_bifpn_00099.pth'
resume = ''
train_opt = TrainConfig()
def convert_slice_image(image_array):
instance_num_max = len(image_array)
 seq_len = min(len(image_array), 401)
 image = []
pe_array = np.array(
 [window(i, 100, 700)for i in image_array] 
)
 for instance_num in range(seq_len)[0::2]:
if instance_num > 0:
if instance_num < instance_num_max:
idx_range = range(idx - 1, idx + 2)
 else:
idx_range = range(idx - 2, idx + 1)
 else:
idx_range = range(idx, idx + 3)
 image.append(np.transpose(pe_array[idx_range],(1, 2, 0)))
 image = np.array(image)
 return image
def second_level_predict(models, cnn_feature_dict, meta_feature, seq_len):
preds = [
seq_model({"feature": cnn_feature_dict[i], 'meta_feature': meta_feature, "seq_len": [seq_len]})
 for i, seq_model in enumerate(models)
 ]
per_exam_xs, per_image_xs = zip(*preds)
 per_exam_x = np.mean(
 [
torch.sigmoid(per_exam_x.squeeze() ).cpu().numpy()
for per_exam_x in per_exam_xs
],
axis=0, 
)
 per_image_x = np.mean(
 [
torch.sigmoid(per_image_x.squeeze() ).cpu().numpy()
for per_image_x in per_image_xs
],
axis=0, 
)[:, None]
return per_exam_x, per_image_x
def stacking_predict(lgb_dict, feat):
return np.mean([lgb_dict[i].predict(feat)for i in range(5)], axis=0)
 def inference_2_stage(image_array, meta_data):
N_MODEL = 12
WL_list = [-600, 100, 40]
WW_list = [1500, 700, 400]
meta_feature_cols = [
"KVP",
"XRayTubeCurrent",
"Exposure",
"SliceThickness",
"ImagePositionPatient_x",
"ImagePositionPatient_y",
"ImagePositionPatient_z",
]
meta_mean = np.array(
 [
114.08353157,
419.09953533,
108.65329098,
1.0090654,
-172.34524724,
-141.6034326,
-45.66431551,
] 
)
 meta_std = np.array(
 [
1.09305001e01,
1.92887125e02,
4.97377464e02,
2.65628402e-01,
2.62277483e01,
7.11791545e01,
4.41005297e02,
] 
)
 seq_len = min(len(image_array), 401)
 image = np.array(
 [convert_image(i, WL_list, WW_list)for i in image_array[:401][0::2]] 
)
 dataset = ImageDataset(image)
 dataloader = DataLoader(dataset, batch_size=32, drop_last=False, shuffle=False)
 cnn_feature_dict_512 = {i: [] for i in range(len(cnn_models_512)) }
cnn_feature_dict_384 = {i: [] for i in range(len(cnn_models_384)) }
with torch.no_grad() :
for batch in dataloader:
batch = {k: batch[k].to(device)for k in batch.keys() }
with autocast() :
batch_512_image = transform_512_b5(batch["image"])
 for i in range(len(image_models_512)) :
cnn_feature_dict_512[i].append(
 image_models_512[i].get_feature({"image": batch_512_image})
)
 batch_384_image = transform_384_b3(batch["image"])
 for i in range(len(image_models_384)) :
cnn_feature_dict_384[i].append(
 image_models_384[i].get_feature({"image": batch_384_image})
)
 cnn_feature_dict_512 = {
i: torch.cat(feature, dim=0 ).unsqueeze(0)
 for i, feature in cnn_feature_dict_512.items()
}
cnn_feature_dict_512 = {
i: F.pad(feature,(0, 0, 0, 201 - len(image)))
 for i, feature in cnn_feature_dict_512.items()
}
cnn_feature_dict_384 = {
i: torch.cat(feature, dim=0 ).unsqueeze(0)
 for i, feature in cnn_feature_dict_384.items()
}
cnn_feature_dict_384 = {
i: F.pad(feature,(0, 0, 0, 201 - len(image)))
 for i, feature in cnn_feature_dict_384.items()
}
cnn_feature_dict_concat = {
i: torch.cat([cnn_feature_dict_384[i], cnn_feature_dict_512[i]], dim=-1)
 for i in range(5)
 }
meta_feature = meta_data[meta_feature_cols].values[:401][0::2].astype(np.float32)
 meta_feature =(meta_feature - meta_mean)/ meta_std
meta_feature = F.pad(
 torch.tensor(meta_feature, device=cnn_feature_dict_512[0].device),(0, 0, 0, 201 - len(image))
).unsqueeze(0)
 preds_512 = [second_level_predict(models, cnn_feature_dict_512, meta_feature, seq_len)for models in [cnn_models_512, lstm_models_512, gru_models_512, cnn_rnn_models_512]]
preds_384 = [second_level_predict(models, cnn_feature_dict_384, meta_feature, seq_len)for models in [cnn_models_384, lstm_models_384, gru_models_384, cnn_rnn_models_384]]
preds_concat = [second_level_predict(models, cnn_feature_dict_concat, meta_feature, seq_len)for models in [cnn_models_concat, lstm_models_concat, gru_models_concat, cnn_rnn_models_concat]]
exam_preds_512, image_preds_512 = zip(*preds_512)
 exam_preds_384, image_preds_384 = zip(*preds_384)
 exam_preds_concat, image_preds_concat = zip(*preds_concat)
 exam_preds = exam_preds_512 + exam_preds_384 + exam_preds_concat
image_preds = np.concatenate(image_preds_512 + image_preds_384 + image_preds_concat, axis=1)[:seq_len]
image_feats = []
for i in range(N_MODEL):
image_feats.append(np.concatenate([image_preds[:, [i]], exam_preds[i][None, :].repeat(seq_len, axis=0)], axis=1))
image_feats = np.concatenate(image_feats, axis=1)
 exam_preds = np.concatenate(exam_preds)
 image_agg = pd.DataFrame(image_preds ).agg(['mean', 'std', 'min', 'max'])
 exam_feat = np.concatenate([exam_preds] + [image_agg[i].values for i in range(N_MODEL)])
 lgb_image_pred = stacking_predict(lgb_dicts[0], image_feats)
 lgb_exam_pred = np.squeeze(np.concatenate([stacking_predict(lgb_dict, exam_feat[None, :])for lgb_dict in lgb_dicts[1:]]))
image_feats = stacking_std.transform(image_feats)
 nn_feature = F.pad(torch.tensor(image_feats ).cuda().unsqueeze(0),(0, 0, 0, 401 - seq_len))
cnn_exam_preds, cnn_image_preds = zip(*[model({'feature': nn_feature, 'seq_len': [seq_len]})for model in stacking_cnn_models])
 cnn_exam_pred = np.mean([torch.sigmoid(pred ).detach().cpu().numpy().squeeze() for pred in cnn_exam_preds], axis=0)
 cnn_image_pred = np.mean([torch.sigmoid(pred ).detach().cpu().numpy().squeeze() for pred in cnn_image_preds], axis=0)[:seq_len]
rnn_exam_preds, rnn_image_preds = zip(*[model({'feature': nn_feature, 'seq_len': [seq_len]})for model in stacking_rnn_models])
 rnn_exam_pred = np.mean([torch.sigmoid(pred ).detach().cpu().numpy().squeeze() for pred in rnn_exam_preds], axis=0)
 rnn_image_pred = np.mean([torch.sigmoid(pred ).detach().cpu().numpy().squeeze() for pred in rnn_image_preds], axis=0)[:seq_len]
per_exam_x = np.average([lgb_exam_pred, cnn_exam_pred, rnn_exam_pred], axis=0, weights=[2, 1, 2])
 per_image_x = np.average([lgb_image_pred, cnn_image_pred, rnn_image_pred], axis=0, weights=[2, 1, 2])
 return per_exam_x, per_image_x 
<split>
with open('.. /input/wheat-splits/wheat_train_3.json', 'r')as f:
data_dict_train = json.load(f)
 with open('.. /input/wheat-splits/wheat_valid_3.json', 'r')as f:
data_dict_valid = json.load(f )
def inference_dir(dcm_dir_path):
image_array, meta_data = get_data(dcm_dir_path)
 per_exam_x, per_image_x = inference_2_stage(image_array, meta_data)
 return per_exam_x, per_image_x, meta_data['SOPInstanceUID']<define_variables>
def main(opt):
set_seed(opt.seed)
 torch.backends.cudnn.benchmark = True
create_logging(opt.logs_dir, 'w')
 train_dataset = WheatDataset(
 opt,
opt.data_root,
data_dict_train,
data_dict_pseudo=data_dict_pseudo,
img_size=1024,
transforms=get_train_transforms(opt.crop_size),
is_train=True,
load_to_ram=False)
 logging.info('{} images in training set'.format(len(train_dataset.data_dict)))
 train_loader = FastDataLoader(
 train_dataset,
opt.batch_size,
collate_fn=collate,
shuffle=True,
drop_last=True,
pin_memory=True,
num_workers=2)
 model = ModleWithLoss(PoseBiFPNNet(opt.arch, opt.heads, opt.head_conv), CtdetLoss(opt)).to(opt.device)
 if opt.freeze_bn:
model.apply(freeze_bn)
 if opt.ema:
logging.info('Training with EMA')
 ema = ModelEMA(model)
 else:
ema = None
optimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=opt.base_lr)
 num_training_steps = int(opt.total_epochs * len(train_dataset)/ opt.batch_size / opt.accumulate)
 scheduler = get_constant_schedule_with_warmup(optimizer, opt.warmup_iters)
 current_epoch = 0
if opt.load_model != '':
checkpoint = torch.load(opt.load_model)
 model.load_state_dict(checkpoint['model'])
 elif opt.resume != '':
checkpoint = torch.load(opt.resume)
 model.load_state_dict(checkpoint['model'])
 optimizer.load_state_dict(checkpoint['optimizer'])
 scheduler.load_state_dict(checkpoint['scheduler'])
 current_epoch = checkpoint['epoch'] + 1
scaler = GradScaler() if opt.amp else None
for epoch in tqdm(range(current_epoch, current_epoch + opt.stage_epochs)) :
epoch_start_time = time.time()
train_one_epoch(opt, model, optimizer, scheduler, train_loader, epoch, ema=ema, scaler=scaler)
 logging.info('-' * 89)
 logging.info('end of epoch {:4d} | time: {:5.2f}s |'.format(epoch,(time.time() - epoch_start_time)))
 logging.info('-' * 89)
 return model.state_dict()
per_exam_label_col = label_cols[1:]<define_variables>
gc.collect()
torch.cuda.empty_cache()
pseudo_model = PoseBiFPNNet(train_opt.arch, train_opt.heads, train_opt.head_conv)
 if len(os.listdir(DIR_TEST)) < 20:
train_opt.total_epochs = 2
train_opt.stage_epochs = 2
data_dict_train = data_dict_train[:300]
state_dict = main(train_opt)
 change_key(state_dict)
 pseudo_model.load_state_dict(state_dict)
 pseudo_model.to(device)
 del state_dict
gc.collect()
else:
state_dict = main(train_opt)
 change_key(state_dict)
 pseudo_model.load_state_dict(state_dict)
 pseudo_model.to(device)
 del state_dict
gc.collect()
if not commit_flag:
submits = []
for idx, data in tqdm(test_df_.iterrows() , total=len(test_df_)) :
per_exam_x, per_image_x, SOPInstanceUID = inference_dir(data['dcm_dir_path'])
 if len(SOPInstanceUID)> 401:
out = np.zeros(len(SOPInstanceUID))
out[:401] = per_image_x
per_image_x = out
else:
per_image_x = per_image_x[:len(SOPInstanceUID)]
per_image_x = per_image_x[:]
StudyInstanceUID, SeriesInstanceUID = data['StudyInstanceUID'], data['SeriesInstanceUID']
tmp = pd.DataFrame({'SOPInstanceUID': SOPInstanceUID, 'pe_present_on_image': per_image_x})
 for i, label_col in enumerate(per_exam_label_col):
tmp['StudyInstanceUID'] = StudyInstanceUID
tmp[label_col] = per_exam_x[i]
submits.append(tmp)
 submits = pd.concat(submits ).reset_index(drop=True)
 exam_pred = submits[['StudyInstanceUID'] + per_exam_label_col].drop_duplicates()
submit_dict = {'id': [], 'label': []}
for label_col in per_exam_label_col:
submit_dict['id'].extend(( exam_pred['StudyInstanceUID'] + f'_{label_col}' ).values.tolist())
 submit_dict['label'].extend(exam_pred[label_col].values.tolist())
 submit_dict['id'].extend(submits['SOPInstanceUID'].values.tolist())
 submit_dict['label'].extend(submits['pe_present_on_image'].values.tolist())
 submit = pd.DataFrame(submit_dict)
 if MODE in ['public', 'private']:
submit = pd.merge(submit, sample_sub.rename(columns={'label': 'old_label'}), on='id', how='outer')
 submit['label'] = submit['label'].fillna(submit['old_label'])
 submit = submit[['id', 'label']]
submit.to_csv('submission.csv', index=False )<install_modules>
opt.pad = 63
opt.test_scales = [1.1, ]
threshold = 0.32
pseudo_pred_boxes_0 , pseudo_pred_scores_0, h0_list, w0_list, img_ids = do_predict(opt, pseudo_model, threshold=threshold, flip_type=0, return_ids=True, return_shapes=True)
 pseudo_pred_boxes_0_lr, pseudo_pred_scores_0_lr = do_predict(opt, pseudo_model, threshold=threshold, flip_type=1, return_ids=False, return_shapes=False)
 pseudo_pred_boxes_0_ud, pseudo_pred_scores_0_ud = do_predict(opt, pseudo_model, threshold=threshold, flip_type=2, return_ids=False, return_shapes=False)
 opt.test_scales = [1.25, ]
threshold = 0.30
pseudo_pred_boxes_l , pseudo_pred_scores_l = do_predict(opt, pseudo_model, threshold=threshold, flip_type=0, return_ids=False, return_shapes=False)
 pseudo_pred_boxes_l_lr, pseudo_pred_scores_l_lr = do_predict(opt, pseudo_model, threshold=threshold, flip_type=1, return_ids=False, return_shapes=False)
 pseudo_pred_boxes_l_ud, pseudo_pred_scores_l_ud = do_predict(opt, pseudo_model, threshold=threshold, flip_type=2, return_ids=False, return_shapes=False )
package_path = '.. /input/efficientnet-pytorch-07/efficientnet_pytorch-0.7.0'
!cp.. /input/gdcm-conda-install/gdcm.tar.
 !tar -xvzf gdcm.tar
!conda install --offline./gdcm/gdcm-2.8.9-py37h71b2a6d_0.tar.bz2<define_variables>
pred_boxes_pseudo = [denormalize_clip_boxes(a, h0, w0)for a, h0, w0 in zip(pred_boxes_pseudo, h0_list, w0_list)]
pred_scores_pseudo = [a for a in pred_scores_pseudo]
CFG = {
'train': False,
'train_img_path': '.. /input/rsna-str-pulmonary-embolism-detection/train',
'test_img_path': '.. /input/rsna-str-pulmonary-embolism-detection/test',
'cv_fold_path': '.. /input/stratified-validation-strategy/rsna_train_splits_fold_20.csv',
'train_path': '.. /input/rsna-str-pulmonary-embolism-detection/train.csv',
'test_path': '.. /input/rsna-str-pulmonary-embolism-detection/test.csv',
'image_target_cols': [
'pe_present_on_image',
],
'exam_target_cols': [
'negative_exam_for_pe',
'rv_lv_ratio_gte_1',
'rv_lv_ratio_lt_1',
'leftsided_pe',
'chronic_pe',
'rightsided_pe',
'acute_and_chronic_pe',
'central_pe',
'indeterminate'
],
'img_num': 200,
'img_size': 256,
'lr': 0.0005,
'epochs': 2,
'device': 'cuda',
'train_bs': 2,
'accum_iter': 8,
'verbose_step': 1,
'num_workers': 4,
'efbnet': 'efficientnet-b6',
'train_folds': [np.arange(0,16),
np.concatenate([np.arange(0,12), np.arange(16,20)]),
np.concatenate([np.arange(0,8), np.arange(12,20)]),
np.concatenate([np.arange(0,4), np.arange(8,20)]),
np.arange(4,20),
],
'valid_folds': [np.arange(16,20),
np.arange(12,16),
np.arange(8,12),
np.arange(4,8),
np.arange(0,4)
 ],
'model_path': '.. /input/rsnab6/',
'tag': 'efb6_stage2_multilabel_v2'
}
SEED = 42321<define_variables>
def format_prediction_string(boxes, scores):
pred_strings = []
for s, b in zip(scores, boxes.astype(int)) :
pred_strings.append(f'{s:.4f} {b[0]} {b[1]} {b[2]} {b[3]}')
 return " ".join(pred_strings )
STAGE1_CFGS = [
{
'tag': 'efb6_stage1_example',
'model_constructor': RSNAImgClassifierSingle,
'dataset_constructor': RSNADatasetStage1,
'output_len': 1
},
{
'tag': 'efb6_stage1_multilabel',
'model_constructor': RSNAImgClassifier,
'dataset_constructor': RSNADatasetStage1,
'output_len': 9
},
]
STAGE1_CFGS_TAG = 'efb6-stage1-single-multi-label' 
<feature_engineering>
pred_strs = []
for bboxes, scores in zip(pred_boxes_pseudo, pred_scores_pseudo):
if len(bboxes)> 0:
bboxes[:, 2] -= bboxes[:, 0]
bboxes[:, 3] -= bboxes[:, 1]
bboxes = bboxes.round()
pred_strs.append(format_prediction_string(bboxes, scores))
else:
pred_strs.append('' )
def get_stage1_columns() :
new_feats = []
for cfg in STAGE1_CFGS:
for i in range(cfg['output_len']):
f = cfg['tag']+'_'+str(i)
 new_feats += [f]
return new_feats
def update_stage1_oof_preds(df, cv_df):
res_file_name = STAGE1_CFGS_TAG+"-train.csv"
new_feats = get_stage1_columns()
for f in new_feats:
df[f] = 0
if os.path.isfile(res_file_name):
df = pd.read_csv(res_file_name)
 print('img acc:',(( df[new_feats[0]]>0)==df[CFG['image_target_cols'][0]] ).mean())
 return df
for fold,(train_fold, valid_fold)in enumerate(zip(CFG['train_folds'], CFG['valid_folds'])) :
if fold < 0:
continue
valid_patients = cv_df.loc[cv_df.fold.isin(valid_fold), 'StudyInstanceUID'].unique()
filt = df.StudyInstanceUID.isin(valid_patients)
 valid_ = df.loc[filt,:].reset_index(drop=True)
 image_preds_all_list = []
for cfg in STAGE1_CFGS:
valid_ds = cfg['dataset_constructor'](valid_, 0.0, CFG['train_img_path'], image_subsampling=False, transforms=get_valid_transforms() , output_label=True)
 val_loader = torch.utils.data.DataLoader(
 valid_ds,
batch_size=256,
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=False,
sampler=SequentialSampler(valid_ds)
)
 device = torch.device(CFG['device'])
 model = cfg['model_constructor']().to(device)
 model.load_state_dict(torch.load('{}/model_fold_{}_{}'.format(CFG['model_path'], fold, cfg['tag'])))
 model.eval()
image_preds_all = []
correct_count = 0
count = 0
for step,(imgs, target)in enumerate(val_loader):
imgs = imgs.to(device ).float()
target = target.to(device ).float()
image_preds = model(imgs)
 if len(image_preds.shape)== 1:
image_preds = image_preds.view(-1, 1)
 correct_count +=(( image_preds[:,0]>0)== target[:,0] ).sum().detach().item()
count += imgs.shape[0]
image_preds_all += [image_preds.cpu().detach().numpy() ]
print('acc: {:.4f}, {}, {}, {}/{}'.format(correct_count/count, correct_count, count, step+1, len(val_loader)) , end='\r')
 print()
image_preds_all = np.concatenate(image_preds_all, axis=0)
 image_preds_all_list += [image_preds_all]
del model1, model2, model3, model4, model5, val_loader
torch.cuda.empty_cache()
image_preds_all_list = np.concatenate(image_preds_all_list, axis=1)
 df.loc[filt, new_feats] = image_preds_all_list
df.to_csv(res_file_name, index=False)
 return df 
<categorify>
test_df = pd.DataFrame({'image_id': img_ids, 'PredictionString':pred_strs})
 test_df
<load_from_csv><EOS> 
test_df.to_csv('submission.csv', index=False )