Kaggle competition: RANZCR CLiP - Catheter and Line Position Challenge
<SOS> metric: MCAUC Kaggle data source: ranzcr-clip-catheter-and-line-position-challenge<define_variables>
batch_size = 1
image_size = 512
tta = True
submit = True
enet_type = ['resnet200d'] * 5
model_path = ['.. /input/resnet200d-baseline-benchmark-public/resnet200d_fold0_cv953.pth',
'.. /input/resnet200d-baseline-benchmark-public/resnet200d_fold1_cv955.pth',
'.. /input/resnet200d-baseline-benchmark-public/resnet200d_fold2_cv955.pth',
'.. /input/resnet200d-baseline-benchmark-public/resnet200d_fold3_cv957.pth',
'.. /input/resnet200d-baseline-benchmark-public/resnet200d_fold4_cv954.pth']
fast_sub = False
fast_sub_path = '.. /input/xxxxxx/your_submission.csv'
fatalities_lag = ['Hubei_China']<drop_column>
sys.path.append('.. /input/pytorch-image-models/pytorch-image-models-master')
 sys.path.append('.. /input/timm-pytorch-image-models/pytorch-image-models-master')
 DEBUG = False
%matplotlib inline
device = torch.device('cuda')if not DEBUG else torch.device('cpu' )
non_stationary_death_data = [ele for ele in allplaces]
for place in fatalities_lag:
if place in allplaces:
non_stationary_death_data.remove(place)
 print(len(non_stationary_death_data))<feature_engineering>
class RANZCRResNet200D(nn.Module):
def __init__(self, model_name='resnet200d', out_dim=11, pretrained=False):
super().__init__()
self.model = timm.create_model(model_name, pretrained=False)
 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, out_dim)
 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
train_sub['logConfirmedCases']= train_sub['logConfirmedCases'].replace(to_replace=-inf,
value=0 )<merge>
transforms_test = albumentations.Compose([
Resize(image_size, image_size),
Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225], 
),
ToTensorV2()
] )
poly_data = train[['date','place',
'ConfirmedCases','Fatalities']].merge(test[['date','place']],
how='outer',
on=['date','place'] ).sort_values(['place',
'date'])
 print(poly_data.date.min() , test.date.min() , train.date.max() , poly_data.date.max() )<feature_engineering>
test = pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv')
 test['file_path'] = test.StudyInstanceUID.apply(lambda x: os.path.join('.. /input/ranzcr-clip-catheter-line-classification/test', f'{x}.jpg'))
target_cols = test.iloc[:, 1:12].columns.tolist()
test_dataset = RANZCRDataset(test, 'test', transform=transforms_test)
 test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=24 )
label = []
for place in poly_data.place.unique() :
labelrange = range(1,len(poly_data[poly_data['place']==place])+1)
 label.append([i for i in labelrange])
 lab = [item for lab in label for item in lab]
poly_data['label'] = lab
poly_data.head()<create_dataframe>
if submit:
test_preds = []
for i in range(len(enet_type)) :
if enet_type[i] == 'resnet200d':
print('resnet200d loaded')
 model = RANZCRResNet200D(enet_type[i], out_dim=len(target_cols))
model = model.to(device)
 model.load_state_dict(torch.load(model_path[i], map_location='cuda:0'))
if tta:
test_preds += [tta_inference_func(test_loader)]
else:
test_preds += [inference_func(test_loader)]
submission = pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv')
 submission[target_cols] = np.mean(test_preds, axis=0)
 submission.to_csv('submission.csv', index=False)
 else:
pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv' ).to_csv('submission.csv', index=False )
XYtrain['intercept']= -1
result=pd.DataFrame()
for place in poly_data.place.unique() :
for degree in [2,3,4,5]:
features = XYtrain[XYtrain['place']==place][['label','intercept']]
target = XYtrain[XYtrain['place']==place]['ConfirmedCases']
model = make_pipeline(PolynomialFeatures(degree), Ridge())
 model.fit(np.array(features), target)
 y_pred = model.predict(np.array(features))
rmsle = RMSLE(y_pred, target)
 result = result.append(pd.DataFrame({'place':[place],
'degree':[degree],'RMSLE': [rmsle]})) 
<count_unique_values>
if fast_sub:
pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv',usecols=[0],index_col=0 ).join(pd.read_csv(fast_sub_path ).set_index('StudyInstanceUID')).fillna(0 ).to_csv('submission.csv' )