Kaggle competition: RANZCR CLiP - Catheter and Line Position Challenge
<SOS> metric: MCAUC Kaggle data source: ranzcr-clip-catheter-and-line-position-challenge<count_unique_values>
sys.path.append('.. /input/pytorch-images-seresnet')
 warnings.filterwarnings('ignore')
 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu' )
print('How many places?: ', 'Train: ', len(train['place'].unique()),
'Test: ', len(test['place'].unique()))
print('Unique place similar as test?: ',(train['place'].unique() == test['place'].unique() ).sum() )<drop_column>
IMAGE_SIZE = 640
BATCH_SIZE = 128
TEST_PATH = '.. /input/ranzcr-clip-catheter-line-classification/test'
MODEL_PATH = '.. /input/resnet200d-public/resnet200d_320_CV9632.pth'
china_cases = train[train['place'].str.contains('China')][['date',
'ConfirmedCases',
'Fatalities']].reset_index(drop=True)
 restworld_cases = train[-train['place'].str.contains('China')][['date',
'ConfirmedCases',
'Fatalities']].reset_index(drop=True )<drop_column>
test = pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv' )
us_cases = train[train['place'].str.contains('US')][['date','place',
'ConfirmedCases',
'Fatalities']].reset_index(drop=True )<compute_test_metric>
def get_transforms() :
return Compose([
Resize(IMAGE_SIZE, IMAGE_SIZE),
Normalize(
),
ToTensorV2() ,
] )
def RMSLE(predicted, actual):
return np.sqrt(np.mean(np.power(( np.log(predicted+1)-np.log(actual+1)) ,2)) )<feature_engineering>
class ResNet200D(nn.Module):
def __init__(self, model_name='resnet200d_320'):
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, 11)
 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 = train[['id','place','date','ConfirmedCases','Fatalities']]
train_sub['logConfirmedCases'] = np.log(train_sub['ConfirmedCases'])
 train_sub = train_sub.set_index('date' )<feature_engineering>
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
list= []
for place in train_sub.place.unique() :
a = train_sub[train_sub['place']==place]
a['z_cases'] =(a['logConfirmedCases']- a['logConfirmedCases'].rolling(window=3 ).mean())/a['logConfirmedCases'].rolling(window=3 ).std()
a['zp_cases']= a['z_cases']- a['z_cases'].shift(3)
 a['z_death'] =(a['Fatalities']-a['Fatalities'].rolling(window=3 ).mean())/a['Fatalities'].rolling(window=3 ).std()
a['zp_death']= a['z_death']- a['z_death'].shift(3)
 list.append(a)
 rolling_df = pd.concat(list )<statistical_test>
model = ResNet200D()
model.load_state_dict(torch.load(MODEL_PATH)['model'])
 model.eval()
models = [model.to(device)]
stationary_data =[]
for place in train_sub.place.unique() :
a= rolling_df[(rolling_df['place']==place)&(rolling_df['logConfirmedCases'] > 0)]['logConfirmedCases'].dropna()
try:
dftest = adfuller(a, autolag='AIC')
 if(dftest[1] < 0.001):
stationary_data.append(place)
 else:
pass
except:
pass
print(len(stationary_data))<count_unique_values>
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 )
<define_variables><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()