Kaggle competition: RANZCR CLiP - Catheter and Line Position Challenge
<SOS> metric: MCAUC Kaggle data source: ranzcr-clip-catheter-and-line-position-challenge<load_from_csv>
OUTPUT_DIR = './'
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
 TEST_PATH = '.. /input/ranzcr-clip-catheter-line-classification/test'
Sub = pd.read_csv(".. /input/covid19-global-forecasting-week-1/submission.csv")
 Sub.columns
sub_new = Sub[["ForecastId"]]<concatenate>
class CFG:
debug=False
num_workers=4
model_name='resnet200d_320'
size=512
batch_size=128
seed=416
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=5
trn_fold=[0]
OP = pd.concat([pred1,pred2,sub_new],axis=1)
 OP.head()
OP.columns = ['ConfirmedCases', 'Fatalities', 'ForecastId']
OP = OP[['ForecastId','ConfirmedCases', 'Fatalities']] 
<data_type_conversions>
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 )
OP["ConfirmedCases"] = OP["ConfirmedCases"].astype(int)
 OP["Fatalities"] = OP["Fatalities"].astype(int)
<save_to_csv>
oof_df = pd.read_csv('.. /input/ranzcr-exp12-step3-fold0/oof_df.csv')
 for fold in CFG.trn_fold:
fold_oof_df = oof_df[oof_df['fold']==fold].reset_index(drop=True)
 LOGGER.info(f"========== fold: {fold} result ==========")
 get_result(fold_oof_df )
OP.to_csv("submission.csv",index=False )<import_modules>
if CFG.debug:
test = pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv', nrows=10)
 else:
test = pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv')
 print(test.shape)
 test.head()
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go
import datetime as dt
import folium<load_pretrained>
train_dataset = TestDataset(test, transform=get_transforms(data='valid'))
for i in range(1):
image = train_dataset[i]
plt.imshow(image[0])
 plt.show()
plt.imshow(image[0].flip(-1))
plt.show()
!kaggle kaggle competitions download -c covid19-global-forecasting-week-1<install_modules>
class CustomResNet200D(nn.Module):
def __init__(self, model_name='resnet200d_320', pretrained=False):
super().__init__()
self.model = timm.create_model(model_name, pretrained=False)
 if pretrained:
pretrained_path = '.. /input/resnet200d-pretrained-weight/resnet200d_ra2-bdba9bf9.pth'
self.model.load_state_dict(torch.load(pretrained_path))
print(f'load {model_name} pretrained model')
 n_features = self.model.fc.in_features
self.model.global_pool = nn.Identity()
self.model.fc = nn.Identity()
self.pooling = nn.AdaptiveAvgPool2d(1)
 self.fc = nn.Linear(n_features, CFG.target_size)
 def forward(self, x):
bs = x.size(0)
 features = self.model(x)
 pooled_features = self.pooling(features ).view(bs, -1)
 output = self.fc(pooled_features)
 return output
!pip install kaggle<set_options>
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
!mkdir.kaggle<load_pretrained>
%%time
model = CustomResNet200D(CFG.model_name, pretrained=False)
 model_path = '.. /input/ranzcr-exp12-step3-fold0/resnet200d_320_fold0_best_loss.pth'
model.load_state_dict(torch.load(model_path)['model'])
 model.eval()
models = [model.to(device)]
token = {"username":'nitingrover425','key':'c22685e02df7d46edd199e441980c448'}
with open('/content/.kaggle/kaggle.json', 'w')as file:
json.dump(token, file )<install_modules>
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(models, test_loader, device )
<load_pretrained><EOS> 
test[CFG.target_cols] = predictions
test[['StudyInstanceUID'] + CFG.target_cols].to_csv(OUTPUT_DIR+'submission.csv', index=False)
 test.head()