Kaggle competition: RANZCR CLiP - Catheter and Line Position Challenge
<SOS> metric: MCAUC Kaggle data source: ranzcr-clip-catheter-and-line-position-challenge<drop_column>
MODEL_DIR = '.. /input/ranzcr-resnext50-starter-models/'
OUTPUT_DIR = './'
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
 TEST_PATH = '.. /input/ranzcr-clip-catheter-line-classification/test'
train=train.drop(['Country/Region','Province/State','Id'],axis=1)
 test=test.drop(['Country/Region','Province/State','ForecastId'],axis=1 )<concatenate>
class CFG:
debug=False
num_workers=4
model_name='resnext50_32x4d'
size=600
batch_size=64
seed=42
target_size=11
target_cols=['ETT - Abnormal', 'ETT - Borderline', 'ETT - Normal',
'NGT - Abnormal', 'NGT - Borderline', 'NGT - Incompletely Imaged', 'NGT - Normal',
'CVC - Abnormal', 'CVC - Borderline', 'CVC - Normal',
'Swan Ganz Catheter Present']
n_fold=4
trn_fold=[0, 1, 2, 3]
train=pd.concat([train,train_dummies,train1_dummies],axis=1 )<concatenate>
def get_score(y_true, y_pred):
scores = []
for i in range(y_true.shape[1]):
score = roc_auc_score(y_true[:,i], y_pred[:,i])
 scores.append(score)
 avg_score = np.mean(scores)
 return avg_score, scores
def get_result(result_df):
preds = result_df[[f'pred_{c}' for c in CFG.target_cols]].values
labels = result_df[CFG.target_cols].values
score, scores = get_score(labels, preds)
 LOGGER.info(f'Score: {score:<.4f} Scores: {np.round(scores, decimals=4)}')
 @contextmanager
def timer(name):
t0 = time.time()
LOGGER.info(f'[{name}] start')
 yield
LOGGER.info(f'[{name}] done in {time.time() - t0:.0f} s.')
 def init_logger(log_file=OUTPUT_DIR+'inference.log'):
logger = getLogger(__name__)
 logger.setLevel(INFO)
 handler1 = StreamHandler()
handler1.setFormatter(Formatter("%(message)s"))
handler2 = FileHandler(filename=log_file)
 handler2.setFormatter(Formatter("%(message)s"))
logger.addHandler(handler1)
 logger.addHandler(handler2)
 return logger
LOGGER = init_logger()
def seed_torch(seed=42):
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
seed_torch(seed=CFG.seed )
test=pd.concat([test,test_dummies,test1_dummies],axis=1 )<prepare_x_and_y>
train = pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/train.csv')
 folds = train.copy()
Fold = GroupKFold(n_splits=CFG.n_fold)
 groups = folds['PatientID'].values
for n,(train_index, val_index)in enumerate(Fold.split(folds, folds[CFG.target_cols], groups)) :
folds.loc[val_index, 'fold'] = int(n)
 folds['fold'] = folds['fold'].astype(int)
 display(folds.groupby('fold' ).size() )
features=train.drop(['ConfirmedCases','Fatalities'],axis=1)
 target1=train['ConfirmedCases']
target2=train['Fatalities']<choose_model_class>
oof_df = pd.DataFrame()
for fold in CFG.trn_fold:
valid_folds = folds[folds['fold']==fold].reset_index(drop=True)
 check_point = torch.load(MODEL_DIR+f'{CFG.model_name}_fold{fold}_best.pth', map_location=device)
 for c in [f'pred_{c}' for c in CFG.target_cols]:
valid_folds[c] = np.nan
valid_folds[[f'pred_{c}' for c in CFG.target_cols]] = check_point['preds']
LOGGER.info(f"========== fold: {fold} result ==========")
 get_result(valid_folds)
 oof_df = pd.concat([oof_df, valid_folds])
 oof_df = oof_df.reset_index(drop=True)
 LOGGER.info(f"========== CV ==========")
 get_result(oof_df )
rf=DecisionTreeRegressor(criterion='mse', splitter='best' )<train_model>
test = pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv')
 print(test.shape)
 test.head()
rf.fit(features,target1 )<predict_on_test>
def get_transforms(*, data):
if data == 'train':
return Compose([
Resize(CFG.size, CFG.size),
Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225], 
),
ToTensorV2() ,
])
 elif data == 'valid':
return Compose([
Resize(CFG.size, CFG.size),
Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225], 
),
ToTensorV2() ,
] )
predict_cases=rf.predict(test )<prepare_output>
class CustomResNext(nn.Module):
def __init__(self, model_name='resnext50_32x4d', pretrained=False):
super().__init__()
self.model = timm.create_model(model_name, pretrained=pretrained)
 n_features = self.model.fc.in_features
self.model.fc = nn.Linear(n_features, CFG.target_size)
 def forward(self, x):
x = self.model(x)
 return x
submission['ConfirmedCases']=predict_cases<predict_on_test>
def inference(model, states, test_loader, device):
model.to(device)
 tk0 = tqdm(enumerate(test_loader), total=len(test_loader))
probs = []
for i,(images)in tk0:
images = images.to(device)
 avg_preds = []
for state in states:
model.load_state_dict(state['model'])
 model.eval()
with torch.no_grad() :
y_preds = model(images)
 avg_preds.append(y_preds.sigmoid().to('cpu' ).numpy())
 avg_preds = np.mean(avg_preds, axis=0)
 probs.append(avg_preds)
 probs = np.concatenate(probs)
 return probs
<data_type_conversions><EOS> 
model = CustomResNext(CFG.model_name, pretrained=False)
 states = [torch.load(MODEL_DIR+f'{CFG.model_name}_fold{fold}_best.pth')for fold in CFG.trn_fold]
test_dataset = TestDataset(test, transform=get_transforms(data='valid'))
test_loader = DataLoader(test_dataset, batch_size=CFG.batch_size, shuffle=False,
num_workers=CFG.num_workers, pin_memory=True)
 predictions = inference(model, states, test_loader, device)
 test[CFG.target_cols] = predictions
test[['StudyInstanceUID'] + CFG.target_cols].to_csv(OUTPUT_DIR+'submission.csv', index=False)
 test.head()