Kaggle competition: RANZCR CLiP - Catheter and Line Position Challenge
<SOS> metric: MCAUC Kaggle data source: ranzcr-clip-catheter-and-line-position-challenge<predict_on_test>
sys.path.append('.. /input/pytorch-images-seresnet')
 warnings.filterwarnings('ignore')
 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu' )
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)
 opt.test_scales = [1.35, ]
threshold = 0.27
bifpn0_pred_boxes_x , bifpn0_pred_scores_x = do_predict(opt, bifpn_model, threshold=threshold, flip_type=0, return_ids=False, return_shapes=False)
 bifpn0_pred_boxes_x_lr, bifpn0_pred_scores_x_lr = do_predict(opt, bifpn_model, threshold=threshold, flip_type=1, return_ids=False, return_shapes=False)
 bifpn0_pred_boxes_x_ud, bifpn0_pred_scores_x_ud = do_predict(opt, bifpn_model, threshold=threshold, flip_type=2, return_ids=False, return_shapes=False )<set_options>
IMAGE_SIZE = 640
BATCH_SIZE = 64
TEST_PATH = '.. /input/ranzcr-clip-catheter-line-classification/test'
MODEL_PATH = '.. /input/efficientnetb5cv9621/tf_efficientnet_b5_ns_CV96.21.pth'
del bifpn_model
gc.collect()
torch.cuda.empty_cache()<load_pretrained>
test = pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.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()<predict_on_test>
def get_transforms() :
return Compose([
Resize(IMAGE_SIZE, IMAGE_SIZE),
Normalize(
),
ToTensorV2() ,
] )
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)
 opt.test_scales = [1.35, ]
threshold = 0.27
bifpn1_pred_boxes_x , bifpn1_pred_scores_x = do_predict(opt, bifpn_model, threshold=threshold, flip_type=0, return_ids=False, return_shapes=False)
 bifpn1_pred_boxes_x_lr, bifpn1_pred_scores_x_lr = do_predict(opt, bifpn_model, threshold=threshold, flip_type=1, return_ids=False, return_shapes=False)
 bifpn1_pred_boxes_x_ud, bifpn1_pred_scores_x_ud = do_predict(opt, bifpn_model, threshold=threshold, flip_type=2, return_ids=False, return_shapes=False )<set_options>
class EfficientNetB5(nn.Module):
def __init__(self, model_name='tf_efficientnet_b5_ns'):
super().__init__()
self.model = timm.create_model(model_name, pretrained=False)
 n_features = self.model.classifier.in_features
self.model.global_pool = nn.Identity()
self.model.classifier = nn.Identity()
self.pooling = nn.AdaptiveAvgPool2d(1)
 self.classifier = nn.Linear(n_features, 11)
 def forward(self, x):
bs = x.size(0)
 features = self.model(x)
 pooled_features = self.pooling(features ).view(bs, -1)
 output = self.classifier(pooled_features)
 return output
del bifpn_model
gc.collect()
torch.cuda.empty_cache()<load_pretrained>
def inference(models, test_loader, device):
tk0 = tqdm(enumerate(test_loader), total=len(test_loader))
probs = []
for i,(images)in tk0:
images = images.to(device)
 avg_preds = []
for model in models:
with torch.no_grad() :
y_preds1 = model(images)
 y_preds2 = model(images.flip(-1))
y_preds =(y_preds1.sigmoid().to('cpu' ).numpy() + y_preds2.sigmoid().to('cpu' ).numpy())/ 2
avg_preds.append(y_preds)
 avg_preds = np.mean(avg_preds, axis=0)
 probs.append(avg_preds)
 probs = np.concatenate(probs)
 return probs
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()<predict_on_test>
model = EfficientNetB5()
model.load_state_dict(torch.load(MODEL_PATH)['model'])
 model.eval()
models = [model.to(device)]
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)
 opt.test_scales = [1.35, ]
threshold = 0.27
bifpn3_pred_boxes_x , bifpn3_pred_scores_x = do_predict(opt, bifpn_model, threshold=threshold, flip_type=0, return_ids=False, return_shapes=False)
 bifpn3_pred_boxes_x_lr, bifpn3_pred_scores_x_lr = do_predict(opt, bifpn_model, threshold=threshold, flip_type=1, return_ids=False, return_shapes=False)
 bifpn3_pred_boxes_x_ud, bifpn3_pred_scores_x_ud = do_predict(opt, bifpn_model, threshold=threshold, flip_type=2, return_ids=False, return_shapes=False )<set_options>
test_dataset = TestDataset(test, transform=get_transforms())
 test_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False,
num_workers=4 , pin_memory=True)
 predictions = inference(models, test_loader, device )
<categorify><EOS> 
target_cols = test.iloc[:, 1:12].columns.tolist()
test[target_cols] = predictions
test[['StudyInstanceUID'] + target_cols].to_csv('submission.csv', index=False)
 test.head()